Skip to content

Commit e625e50

Browse files
committed
Bluetooth: Introduce debug feature when dynamic debug is disabled
In case dynamic debug is disabled, this feature allows a vendor platform to provide debug statement printing. Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
1 parent a10c907 commit e625e50

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎include/net/bluetooth/bluetooth.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ __printf(1, 2)
153153
void bt_warn(const char *fmt, ...);
154154
__printf(1, 2)
155155
void bt_err(const char *fmt, ...);
156+
#if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
157+
void bt_dbg_set(bool enable);
158+
bool bt_dbg_get(void);
159+
__printf(1, 2)
160+
void bt_dbg(const char *fmt, ...);
161+
#endif
156162
__printf(1, 2)
157163
void bt_warn_ratelimited(const char *fmt, ...);
158164
__printf(1, 2)
@@ -161,7 +167,12 @@ void bt_err_ratelimited(const char *fmt, ...);
161167
#define BT_INFO(fmt, ...) bt_info(fmt "\n", ##__VA_ARGS__)
162168
#define BT_WARN(fmt, ...) bt_warn(fmt "\n", ##__VA_ARGS__)
163169
#define BT_ERR(fmt, ...) bt_err(fmt "\n", ##__VA_ARGS__)
170+
171+
#if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
172+
#define BT_DBG(fmt, ...) bt_dbg(fmt "\n", ##__VA_ARGS__)
173+
#else
164174
#define BT_DBG(fmt, ...) pr_debug(fmt "\n", ##__VA_ARGS__)
175+
#endif
165176

166177
#define bt_dev_info(hdev, fmt, ...) \
167178
BT_INFO("%s: " fmt, (hdev)->name, ##__VA_ARGS__)

‎net/bluetooth/Kconfig‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,11 @@ config BT_SELFTEST_SMP
135135
Run test cases for SMP cryptographic functionality, including both
136136
legacy SMP as well as the Secure Connections features.
137137

138+
config BT_FEATURE_DEBUG
139+
bool "Enable runtime option for debugging statements"
140+
depends on BT && !DYNAMIC_DEBUG
141+
help
142+
This provides an option to enable/disable debugging statements
143+
at runtime via the experimental features interface.
144+
138145
source "drivers/bluetooth/Kconfig"

‎net/bluetooth/lib.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,39 @@ void bt_err(const char *format, ...)
183183
}
184184
EXPORT_SYMBOL(bt_err);
185185

186+
#ifdef CONFIG_BT_FEATURE_DEBUG
187+
static bool debug_enable;
188+
189+
void bt_dbg_set(bool enable)
190+
{
191+
debug_enable = enable;
192+
}
193+
194+
bool bt_dbg_get(void)
195+
{
196+
return debug_enable;
197+
}
198+
199+
void bt_dbg(const char *format, ...)
200+
{
201+
struct va_format vaf;
202+
va_list args;
203+
204+
if (likely(!debug_enable))
205+
return;
206+
207+
va_start(args, format);
208+
209+
vaf.fmt = format;
210+
vaf.va = &args;
211+
212+
printk(KERN_DEBUG pr_fmt("%pV"), &vaf);
213+
214+
va_end(args);
215+
}
216+
EXPORT_SYMBOL(bt_dbg);
217+
#endif
218+
186219
void bt_warn_ratelimited(const char *format, ...)
187220
{
188221
struct va_format vaf;

‎net/bluetooth/mgmt.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,6 +3715,14 @@ static int read_security_info(struct sock *sk, struct hci_dev *hdev,
37153715
rp, sizeof(*rp) + sec_len);
37163716
}
37173717

3718+
#ifdef CONFIG_BT_FEATURE_DEBUG
3719+
/* d4992530-b9ec-469f-ab01-6c481c47da1c */
3720+
static const u8 debug_uuid[16] = {
3721+
0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab,
3722+
0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4,
3723+
};
3724+
#endif
3725+
37183726
static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev,
37193727
void *data, u16 data_len)
37203728
{
@@ -3726,6 +3734,16 @@ static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev,
37263734

37273735
memset(&buf, 0, sizeof(buf));
37283736

3737+
#ifdef CONFIG_BT_FEATURE_DEBUG
3738+
if (!hdev) {
3739+
u32 flags = bt_dbg_get() ? BIT(0) : 0;
3740+
3741+
memcpy(rp->features[idx].uuid, debug_uuid, 16);
3742+
rp->features[idx].flags = cpu_to_le32(flags);
3743+
idx++;
3744+
}
3745+
#endif
3746+
37293747
rp->feature_count = cpu_to_le16(idx);
37303748

37313749
/* After reading the experimental features information, enable
@@ -3738,6 +3756,21 @@ static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev,
37383756
0, rp, sizeof(*rp) + (20 * idx));
37393757
}
37403758

3759+
#ifdef CONFIG_BT_FEATURE_DEBUG
3760+
static int exp_debug_feature_changed(bool enabled, struct sock *skip)
3761+
{
3762+
struct mgmt_ev_exp_feature_changed ev;
3763+
3764+
memset(&ev, 0, sizeof(ev));
3765+
memcpy(ev.uuid, debug_uuid, 16);
3766+
ev.flags = cpu_to_le32(enabled ? BIT(0) : 0);
3767+
3768+
return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL,
3769+
&ev, sizeof(ev),
3770+
HCI_MGMT_EXP_FEATURE_EVENTS, skip);
3771+
}
3772+
#endif
3773+
37413774
static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
37423775
void *data, u16 data_len)
37433776
{
@@ -3750,13 +3783,67 @@ static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
37503783
memset(rp.uuid, 0, 16);
37513784
rp.flags = cpu_to_le32(0);
37523785

3786+
#ifdef CONFIG_BT_FEATURE_DEBUG
3787+
if (!hdev) {
3788+
bool changed = bt_dbg_get();
3789+
3790+
bt_dbg_set(false);
3791+
3792+
if (changed)
3793+
exp_debug_feature_changed(false, sk);
3794+
}
3795+
#endif
3796+
37533797
hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
37543798

37553799
return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
37563800
MGMT_OP_SET_EXP_FEATURE, 0,
37573801
&rp, sizeof(rp));
37583802
}
37593803

3804+
#ifdef CONFIG_BT_FEATURE_DEBUG
3805+
if (!memcmp(cp->uuid, debug_uuid, 16)) {
3806+
bool val, changed;
3807+
int err;
3808+
3809+
/* Command requires to use the non-controller index */
3810+
if (hdev)
3811+
return mgmt_cmd_status(sk, hdev->id,
3812+
MGMT_OP_SET_EXP_FEATURE,
3813+
MGMT_STATUS_INVALID_INDEX);
3814+
3815+
/* Parameters are limited to a single octet */
3816+
if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
3817+
return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
3818+
MGMT_OP_SET_EXP_FEATURE,
3819+
MGMT_STATUS_INVALID_PARAMS);
3820+
3821+
/* Only boolean on/off is supported */
3822+
if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
3823+
return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
3824+
MGMT_OP_SET_EXP_FEATURE,
3825+
MGMT_STATUS_INVALID_PARAMS);
3826+
3827+
val = !!cp->param[0];
3828+
changed = val ? !bt_dbg_get() : bt_dbg_get();
3829+
bt_dbg_set(val);
3830+
3831+
memcpy(rp.uuid, debug_uuid, 16);
3832+
rp.flags = cpu_to_le32(val ? BIT(0) : 0);
3833+
3834+
hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
3835+
3836+
err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
3837+
MGMT_OP_SET_EXP_FEATURE, 0,
3838+
&rp, sizeof(rp));
3839+
3840+
if (changed)
3841+
exp_debug_feature_changed(val, sk);
3842+
3843+
return err;
3844+
}
3845+
#endif
3846+
37603847
return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
37613848
MGMT_OP_SET_EXP_FEATURE,
37623849
MGMT_STATUS_NOT_SUPPORTED);

0 commit comments

Comments
 (0)