1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/syslog/debug.h"
4
5#include "mos/misc/setup.h"
6
7#if MOS_CONFIG(MOS_DYNAMIC_DEBUG)
8#include "mos/filesystem/sysfs/sysfs.h"
9#include "mos/filesystem/sysfs/sysfs_autoinit.h"
10#include "mos/syslog/printk.h"
11#endif
12
13#include <mos/mos_global.h>
14
15#define _check_debug_macro_defined_(name) MOS_STATIC_ASSERT(MOS_CONCAT(MOS_DEBUG_, name) != 0);
16MOS_ALL_DEBUG_MODULES(_check_debug_macro_defined_)
17#undef _check_debug_macro_defined_
18
19#if MOS_CONFIG(MOS_DYNAMIC_DEBUG)
20
21// populate default debug settings
22struct _mos_debug_info mos_debug_info = {
23#define X(_name) ._name = { .id = __COUNTER__ + 1, .name = #_name, .enabled = MOS_DEBUG_FEATURE(_name) },
24 MOS_ALL_DEBUG_MODULES(X)
25#undef X
26};
27
28// expose a sysfs file to enable/disable each debug feature
29
30void debug_print_action(const char *name, bool newstate)
31{
32 pr_info("debug option '%s' has been turned %s", name, newstate ? "on" : "off");
33}
34
35#define debug_show_function(name) \
36 bool debug_show_##name(sysfs_file_t *file) \
37 { \
38 sysfs_printf(file, "%d\n", mos_debug_info.name.enabled); \
39 return true; \
40 }
41
42#define debug_store_function(name) \
43 size_t debug_store_##name(sysfs_file_t *file, const char *buf, size_t count, off_t offset) \
44 { \
45 MOS_UNUSED(file); \
46 MOS_UNUSED(offset); \
47 if (count < 1) \
48 return -EINVAL; \
49 const bool on = buf[0] == '1'; \
50 mos_debug_info.name.enabled = on; \
51 debug_print_action(#name, on); \
52 return count; \
53 }
54
55MOS_ALL_DEBUG_MODULES(debug_show_function)
56MOS_ALL_DEBUG_MODULES(debug_store_function)
57
58static sysfs_item_t sys_debug_items[] = {
59#define X(name) SYSFS_RW_ITEM(#name, debug_show_##name, debug_store_##name),
60 MOS_ALL_DEBUG_MODULES(X)
61#undef X
62};
63
64SYSFS_AUTOREGISTER(debug, sys_debug_items);
65
66#define SETUP_DEBUG_MODULE(name) \
67 static bool setup_debug_##name(const char *value) \
68 { \
69 mos_debug_info.name.enabled = cmdline_string_truthiness(value, true); \
70 return true; \
71 } \
72 MOS_SETUP("debug." #name, setup_debug_##name);
73
74MOS_ALL_DEBUG_MODULES(SETUP_DEBUG_MODULE)
75
76// ! expose debug info id to userspace
77#define debug_show_id_function(name) \
78 bool debug_show_id_##name(sysfs_file_t *file) \
79 { \
80 sysfs_printf(file, "%d\n", mos_debug_info.name.id); \
81 return true; \
82 }
83
84MOS_ALL_DEBUG_MODULES(debug_show_id_function)
85
86static sysfs_item_t sys_debug_id_items[] = {
87#define X(name) SYSFS_RO_ITEM(#name, debug_show_id_##name),
88 MOS_ALL_DEBUG_MODULES(X)
89};
90
91SYSFS_AUTOREGISTER(debug_id, sys_debug_id_items);
92
93#endif
94