1 | #include <linux/list.h> |
---|
2 | #include <string.h> |
---|
3 | #include <stdbool.h> |
---|
4 | |
---|
5 | #ifdef DYNAMIC_DEBUG |
---|
6 | |
---|
7 | static LIST_HEAD(debug_funcs); |
---|
8 | |
---|
9 | struct debug_func_entry { |
---|
10 | const char *name; |
---|
11 | struct list_head list; |
---|
12 | }; |
---|
13 | |
---|
14 | static struct debug_func_entry *lookup_entry(const char *func) |
---|
15 | { |
---|
16 | struct debug_func_entry *e, *entry = NULL; |
---|
17 | |
---|
18 | list_for_each_entry(e, &debug_funcs, list) { |
---|
19 | if (!strcmp(e->name, func)) { |
---|
20 | entry = e; |
---|
21 | break; |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | return entry; |
---|
26 | } |
---|
27 | |
---|
28 | bool __syslinux_debug_enabled(const char *func) |
---|
29 | { |
---|
30 | struct debug_func_entry *entry; |
---|
31 | |
---|
32 | entry = lookup_entry(func); |
---|
33 | if (entry) |
---|
34 | return true; |
---|
35 | |
---|
36 | return false; |
---|
37 | } |
---|
38 | |
---|
39 | static int __enable(const char *func) |
---|
40 | { |
---|
41 | struct debug_func_entry *entry; |
---|
42 | |
---|
43 | entry = lookup_entry(func); |
---|
44 | if (entry) |
---|
45 | return 0; /* already enabled */ |
---|
46 | |
---|
47 | entry = malloc(sizeof(*entry)); |
---|
48 | if (!entry) |
---|
49 | return -1; |
---|
50 | |
---|
51 | entry->name = func; |
---|
52 | list_add(&entry->list, &debug_funcs); |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | static int __disable(const char *func) |
---|
57 | { |
---|
58 | struct debug_func_entry *entry; |
---|
59 | |
---|
60 | entry = lookup_entry(func); |
---|
61 | if (!entry) |
---|
62 | return 0; /* already disabled */ |
---|
63 | |
---|
64 | list_del(&entry->list); |
---|
65 | free(entry); |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | /* |
---|
70 | * Enable or disable debug code for function 'func'. |
---|
71 | */ |
---|
72 | int syslinux_debug(const char *func, bool enable) |
---|
73 | { |
---|
74 | int rv; |
---|
75 | |
---|
76 | if (enable) |
---|
77 | rv = __enable(func); |
---|
78 | else |
---|
79 | rv = __disable(func); |
---|
80 | |
---|
81 | return rv; |
---|
82 | } |
---|
83 | |
---|
84 | #else |
---|
85 | |
---|
86 | int syslinux_debug(const char *func, bool enable) |
---|
87 | { |
---|
88 | (void)func; |
---|
89 | (void)enable; |
---|
90 | |
---|
91 | printf("Dynamic debug unavailable\n"); |
---|
92 | return -1; |
---|
93 | } |
---|
94 | |
---|
95 | #endif /* DYNAMIC_DEBUG */ |
---|