4 * make pr_debug()/dev_dbg() calls runtime configurable based upon their
7 * Copyright (C) 2008 Red Hat, Inc., Jason Baron <jbaron@redhat.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/seq_file.h>
14 #include <linux/debugfs.h>
17 extern struct mod_debug __start___verbose[];
18 extern struct mod_debug __stop___verbose[];
21 struct hlist_node hlist;
22 struct hlist_node hlist2;
30 static int nr_entries;
31 static int num_enabled;
32 int dynamic_enabled = DYNAMIC_ENABLED_NONE;
33 static struct hlist_head module_table[DEBUG_HASH_TABLE_SIZE] =
34 { [0 ... DEBUG_HASH_TABLE_SIZE-1] = HLIST_HEAD_INIT };
35 static struct hlist_head module_table2[DEBUG_HASH_TABLE_SIZE] =
36 { [0 ... DEBUG_HASH_TABLE_SIZE-1] = HLIST_HEAD_INIT };
37 static DECLARE_MUTEX(debug_list_mutex);
39 /* dynamic_printk_enabled, and dynamic_printk_enabled2 are bitmasks in which
40 * bit n is set to 1 if any modname hashes into the bucket n, 0 otherwise. They
41 * use independent hash functions, to reduce the chance of false positives.
43 long long dynamic_printk_enabled;
44 EXPORT_SYMBOL_GPL(dynamic_printk_enabled);
45 long long dynamic_printk_enabled2;
46 EXPORT_SYMBOL_GPL(dynamic_printk_enabled2);
48 /* returns the debug module pointer. */
49 static struct debug_name *find_debug_module(char *module_name)
52 struct hlist_head *head;
53 struct hlist_node *node;
54 struct debug_name *element;
57 for (i = 0; i < DEBUG_HASH_TABLE_SIZE; i++) {
58 head = &module_table[i];
59 hlist_for_each_entry_rcu(element, node, head, hlist)
60 if (!strcmp(element->name, module_name))
66 /* returns the debug module pointer. */
67 static struct debug_name *find_debug_module_hash(char *module_name, int hash)
69 struct hlist_head *head;
70 struct hlist_node *node;
71 struct debug_name *element;
74 head = &module_table[hash];
75 hlist_for_each_entry_rcu(element, node, head, hlist)
76 if (!strcmp(element->name, module_name))
81 /* caller must hold mutex*/
82 static int __add_debug_module(char *mod_name, int hash, int hash2)
84 struct debug_name *new;
88 if (find_debug_module(mod_name)) {
92 module_name = kmalloc(strlen(mod_name) + 1, GFP_KERNEL);
97 module_name = strcpy(module_name, mod_name);
98 module_name[strlen(mod_name)] = '\0';
99 new = kzalloc(sizeof(struct debug_name), GFP_KERNEL);
105 INIT_HLIST_NODE(&new->hlist);
106 INIT_HLIST_NODE(&new->hlist2);
107 new->name = module_name;
110 hlist_add_head_rcu(&new->hlist, &module_table[hash]);
111 hlist_add_head_rcu(&new->hlist2, &module_table2[hash2]);
117 int unregister_dynamic_debug_module(char *mod_name)
119 struct debug_name *element;
122 down(&debug_list_mutex);
123 element = find_debug_module(mod_name);
128 hlist_del_rcu(&element->hlist);
129 hlist_del_rcu(&element->hlist2);
131 kfree(element->name);
137 up(&debug_list_mutex);
140 EXPORT_SYMBOL_GPL(unregister_dynamic_debug_module);
142 int register_dynamic_debug_module(char *mod_name, int type, char *share_name,
143 char *flags, int hash, int hash2)
145 struct debug_name *elem;
148 down(&debug_list_mutex);
149 elem = find_debug_module(mod_name);
151 if (__add_debug_module(mod_name, hash, hash2))
153 elem = find_debug_module(mod_name);
154 if (dynamic_enabled == DYNAMIC_ENABLED_ALL &&
155 !strcmp(mod_name, share_name)) {
162 up(&debug_list_mutex);
165 EXPORT_SYMBOL_GPL(register_dynamic_debug_module);
167 int __dynamic_dbg_enabled_helper(char *mod_name, int type, int value, int hash)
169 struct debug_name *elem;
172 if (dynamic_enabled == DYNAMIC_ENABLED_ALL)
175 elem = find_debug_module_hash(mod_name, hash);
176 if (elem && elem->enable)
181 EXPORT_SYMBOL_GPL(__dynamic_dbg_enabled_helper);
183 static void set_all(bool enable)
185 struct debug_name *e;
186 struct hlist_node *node;
188 long long enable_mask;
190 for (i = 0; i < DEBUG_HASH_TABLE_SIZE; i++) {
191 if (module_table[i].first != NULL) {
192 hlist_for_each_entry(e, node, &module_table[i], hlist) {
198 enable_mask = ULLONG_MAX;
201 dynamic_printk_enabled = enable_mask;
202 dynamic_printk_enabled2 = enable_mask;
205 static int disabled_hash(int i, bool first_table)
207 struct debug_name *e;
208 struct hlist_node *node;
211 hlist_for_each_entry(e, node, &module_table[i], hlist) {
216 hlist_for_each_entry(e, node, &module_table2[i], hlist2) {
224 static ssize_t pr_debug_write(struct file *file, const char __user *buf,
225 size_t length, loff_t *ppos)
227 char *buffer, *s, *value_str, *setting_str;
229 struct debug_name *elem = NULL;
232 if (length > PAGE_SIZE || length < 0)
235 buffer = (char *)__get_free_page(GFP_KERNEL);
240 if (copy_from_user(buffer, buf, length))
244 if (length < PAGE_SIZE)
245 buffer[length] = '\0';
246 else if (buffer[PAGE_SIZE-1])
250 down(&debug_list_mutex);
252 if (strncmp("set", buffer, 3))
255 setting_str = strsep(&s, "=");
258 setting_str = strstrip(setting_str);
259 value_str = strsep(&s, " ");
263 if (!strncmp(s, "all", 3))
266 elem = find_debug_module(s);
267 if (!strncmp(setting_str, "enable", 6)) {
268 value = !!simple_strtol(value_str, NULL, 10);
272 num_enabled = nr_entries;
273 dynamic_enabled = DYNAMIC_ENABLED_ALL;
277 dynamic_enabled = DYNAMIC_ENABLED_NONE;
282 if (value && (elem->enable == 0)) {
283 dynamic_printk_enabled |=
284 (1LL << elem->hash1);
285 dynamic_printk_enabled2 |=
286 (1LL << elem->hash2);
289 dynamic_enabled = DYNAMIC_ENABLED_SOME;
292 "debugging enabled for module %s",
294 } else if (!value && (elem->enable == 1)) {
297 if (disabled_hash(elem->hash1, true))
298 dynamic_printk_enabled &=
299 ~(1LL << elem->hash1);
300 if (disabled_hash(elem->hash2, false))
301 dynamic_printk_enabled2 &=
302 ~(1LL << elem->hash2);
305 DYNAMIC_ENABLED_SOME;
308 DYNAMIC_ENABLED_NONE;
311 "debugging disabled for module "
320 up(&debug_list_mutex);
322 free_page((unsigned long)buffer);
326 static void *pr_debug_seq_start(struct seq_file *f, loff_t *pos)
328 return (*pos < DEBUG_HASH_TABLE_SIZE) ? pos : NULL;
331 static void *pr_debug_seq_next(struct seq_file *s, void *v, loff_t *pos)
334 if (*pos >= DEBUG_HASH_TABLE_SIZE)
339 static void pr_debug_seq_stop(struct seq_file *s, void *v)
344 static int pr_debug_seq_show(struct seq_file *s, void *v)
346 struct hlist_head *head;
347 struct hlist_node *node;
348 struct debug_name *elem;
349 unsigned int i = *(loff_t *) v;
352 head = &module_table[i];
353 hlist_for_each_entry_rcu(elem, node, head, hlist) {
354 seq_printf(s, "%s enabled=%d", elem->name, elem->enable);
361 static struct seq_operations pr_debug_seq_ops = {
362 .start = pr_debug_seq_start,
363 .next = pr_debug_seq_next,
364 .stop = pr_debug_seq_stop,
365 .show = pr_debug_seq_show
368 static int pr_debug_open(struct inode *inode, struct file *filp)
370 return seq_open(filp, &pr_debug_seq_ops);
373 static const struct file_operations pr_debug_operations = {
374 .open = pr_debug_open,
376 .write = pr_debug_write,
378 .release = seq_release,
381 static int __init dynamic_printk_init(void)
383 struct dentry *dir, *file;
384 struct mod_debug *iter;
387 dir = debugfs_create_dir("dynamic_printk", NULL);
390 file = debugfs_create_file("modules", 0644, dir, NULL,
391 &pr_debug_operations);
396 for (value = (unsigned long)__start___verbose;
397 value < (unsigned long)__stop___verbose;
398 value += sizeof(struct mod_debug)) {
399 iter = (struct mod_debug *)value;
400 register_dynamic_debug_module(iter->modname,
402 iter->logical_modname,
403 iter->flag_names, iter->hash, iter->hash2);
405 if (dynamic_enabled == DYNAMIC_ENABLED_ALL)
409 module_init(dynamic_printk_init);
410 /* may want to move this earlier so we can get traces as early as possible */
412 static int __init dynamic_printk_setup(char *str)
416 dynamic_enabled = DYNAMIC_ENABLED_ALL;
419 /* Use early_param(), so we can get debug output as early as possible */
420 early_param("dynamic_printk", dynamic_printk_setup);