Skip to content

Commit 8ba6ebf

Browse files
bvanasschegregkh
authored andcommitted
Dynamic debug: Add more flags
Add flags that allow the user to specify via debugfs whether or not the module name, function name, line number and/or thread ID have to be included in the printed message. Signed-off-by: Bart Van Assche <bvanassche@acm.org> Cc: Greg Banks <gnb@fmeh.org> Cc: Konrad Rzeszutek Wilk <konrad@darnok.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 9b99b7f commit 8ba6ebf

3 files changed

Lines changed: 68 additions & 12 deletions

File tree

‎Documentation/dynamic-debug-howto.txt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,20 @@ of the characters:
205205

206206
The flags are:
207207

208+
f
209+
Include the function name in the printed message
210+
l
211+
Include line number in the printed message
212+
m
213+
Include module name in the printed message
208214
p
209215
Causes a printk() message to be emitted to dmesg
216+
t
217+
Include thread ID in messages not generated from interrupt context
210218

211-
Note the regexp ^[-+=][scp]+$ matches a flags specification.
219+
Note the regexp ^[-+=][flmpt]+$ matches a flags specification.
212220
Note also that there is no convenient syntax to remove all
213-
the flags at once, you need to use "-psc".
221+
the flags at once, you need to use "-flmpt".
214222

215223

216224
Debug messages during boot process

‎include/linux/dynamic_debug.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ struct _ddebug {
3131
* writes commands to <debugfs>/dynamic_debug/control
3232
*/
3333
#define _DPRINTK_FLAGS_PRINT (1<<0) /* printk() a message using the format */
34+
#define _DPRINTK_FLAGS_INCL_MODNAME (1<<1)
35+
#define _DPRINTK_FLAGS_INCL_FUNCNAME (1<<2)
36+
#define _DPRINTK_FLAGS_INCL_LINENO (1<<3)
37+
#define _DPRINTK_FLAGS_INCL_TID (1<<4)
3438
#define _DPRINTK_FLAGS_DEFAULT 0
3539
unsigned int flags:8;
3640
char enabled;
@@ -42,6 +46,8 @@ int ddebug_add_module(struct _ddebug *tab, unsigned int n,
4246

4347
#if defined(CONFIG_DYNAMIC_DEBUG)
4448
extern int ddebug_remove_module(const char *mod_name);
49+
extern int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
50+
__attribute__ ((format (printf, 2, 3)));
4551

4652
#define dynamic_pr_debug(fmt, ...) do { \
4753
static struct _ddebug descriptor \
@@ -50,7 +56,7 @@ extern int ddebug_remove_module(const char *mod_name);
5056
{ KBUILD_MODNAME, __func__, __FILE__, fmt, __LINE__, \
5157
_DPRINTK_FLAGS_DEFAULT }; \
5258
if (unlikely(descriptor.enabled)) \
53-
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
59+
__dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
5460
} while (0)
5561

5662

‎lib/dynamic_debug.c‎

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Copyright (C) 2008 Jason Baron <jbaron@redhat.com>
88
* By Greg Banks <gnb@melbourne.sgi.com>
99
* Copyright (c) 2008 Silicon Graphics Inc. All Rights Reserved.
10+
* Copyright (C) 2011 Bart Van Assche. All Rights Reserved.
1011
*/
1112

1213
#include <linux/kernel.h>
@@ -27,6 +28,7 @@
2728
#include <linux/debugfs.h>
2829
#include <linux/slab.h>
2930
#include <linux/jump_label.h>
31+
#include <linux/hardirq.h>
3032

3133
extern struct _ddebug __start___verbose[];
3234
extern struct _ddebug __stop___verbose[];
@@ -63,15 +65,25 @@ static inline const char *basename(const char *path)
6365
return tail ? tail+1 : path;
6466
}
6567

68+
static struct { unsigned flag:8; char opt_char; } opt_array[] = {
69+
{ _DPRINTK_FLAGS_PRINT, 'p' },
70+
{ _DPRINTK_FLAGS_INCL_MODNAME, 'm' },
71+
{ _DPRINTK_FLAGS_INCL_FUNCNAME, 'f' },
72+
{ _DPRINTK_FLAGS_INCL_LINENO, 'l' },
73+
{ _DPRINTK_FLAGS_INCL_TID, 't' },
74+
};
75+
6676
/* format a string into buf[] which describes the _ddebug's flags */
6777
static char *ddebug_describe_flags(struct _ddebug *dp, char *buf,
6878
size_t maxlen)
6979
{
7080
char *p = buf;
81+
int i;
7182

7283
BUG_ON(maxlen < 4);
73-
if (dp->flags & _DPRINTK_FLAGS_PRINT)
74-
*p++ = 'p';
84+
for (i = 0; i < ARRAY_SIZE(opt_array); ++i)
85+
if (dp->flags & opt_array[i].flag)
86+
*p++ = opt_array[i].opt_char;
7587
if (p == buf)
7688
*p++ = '-';
7789
*p = '\0';
@@ -343,7 +355,7 @@ static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
343355
unsigned int *maskp)
344356
{
345357
unsigned flags = 0;
346-
int op = '=';
358+
int op = '=', i;
347359

348360
switch (*str) {
349361
case '+':
@@ -358,13 +370,14 @@ static int ddebug_parse_flags(const char *str, unsigned int *flagsp,
358370
printk(KERN_INFO "%s: op='%c'\n", __func__, op);
359371

360372
for ( ; *str ; ++str) {
361-
switch (*str) {
362-
case 'p':
363-
flags |= _DPRINTK_FLAGS_PRINT;
364-
break;
365-
default:
366-
return -EINVAL;
373+
for (i = ARRAY_SIZE(opt_array) - 1; i >= 0; i--) {
374+
if (*str == opt_array[i].opt_char) {
375+
flags |= opt_array[i].flag;
376+
break;
377+
}
367378
}
379+
if (i < 0)
380+
return -EINVAL;
368381
}
369382
if (flags == 0)
370383
return -EINVAL;
@@ -413,6 +426,35 @@ static int ddebug_exec_query(char *query_string)
413426
return 0;
414427
}
415428

429+
int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
430+
{
431+
va_list args;
432+
int res;
433+
434+
BUG_ON(!descriptor);
435+
BUG_ON(!fmt);
436+
437+
va_start(args, fmt);
438+
res = printk(KERN_DEBUG);
439+
if (descriptor->flags & _DPRINTK_FLAGS_INCL_TID) {
440+
if (in_interrupt())
441+
res += printk(KERN_CONT "<intr> ");
442+
else
443+
res += printk(KERN_CONT "[%d] ", task_pid_vnr(current));
444+
}
445+
if (descriptor->flags & _DPRINTK_FLAGS_INCL_MODNAME)
446+
res += printk(KERN_CONT "%s:", descriptor->modname);
447+
if (descriptor->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
448+
res += printk(KERN_CONT "%s:", descriptor->function);
449+
if (descriptor->flags & _DPRINTK_FLAGS_INCL_LINENO)
450+
res += printk(KERN_CONT "%d ", descriptor->lineno);
451+
res += vprintk(fmt, args);
452+
va_end(args);
453+
454+
return res;
455+
}
456+
EXPORT_SYMBOL(__dynamic_pr_debug);
457+
416458
static __initdata char ddebug_setup_string[1024];
417459
static __init int ddebug_setup_query(char *str)
418460
{

0 commit comments

Comments
 (0)