1 /**
2  * @file debug_cmd.c  Debug commands
3  *
4  * Copyright (C) 2010 - 2016 Creytiv.com
5  */
6 #include <stdlib.h>
7 #include <time.h>
8 #ifdef USE_OPENSSL
9 #include <openssl/crypto.h>
10 #endif
11 #include <re.h>
12 #include <baresip.h>
13 
14 
15 /**
16  * @defgroup debug_cmd debug_cmd
17  *
18  * Advanced debug commands
19  */
20 
21 
22 static uint64_t start_ticks;          /**< Ticks when app started         */
23 static time_t start_time;             /**< Start time of application      */
24 
25 
cmd_net_debug(struct re_printf * pf,void * unused)26 static int cmd_net_debug(struct re_printf *pf, void *unused)
27 {
28 	(void)unused;
29 	return net_debug(pf, baresip_network());
30 }
31 
32 
print_system_info(struct re_printf * pf,void * arg)33 static int print_system_info(struct re_printf *pf, void *arg)
34 {
35 	uint32_t uptime;
36 	int err = 0;
37 
38 	(void)arg;
39 
40 	uptime = (uint32_t)((long long)(tmr_jiffies() - start_ticks)/1000);
41 
42 	err |= re_hprintf(pf, "\n--- System info: ---\n");
43 
44 	err |= re_hprintf(pf, " Machine:  %s/%s\n", sys_arch_get(),
45 			  sys_os_get());
46 	err |= re_hprintf(pf, " Version:  %s (libre v%s)\n",
47 			  BARESIP_VERSION, sys_libre_version_get());
48 	err |= re_hprintf(pf, " Build:    %H\n", sys_build_get, NULL);
49 	err |= re_hprintf(pf, " Kernel:   %H\n", sys_kernel_get, NULL);
50 	err |= re_hprintf(pf, " Uptime:   %H\n", fmt_human_time, &uptime);
51 	err |= re_hprintf(pf, " Started:  %s", ctime(&start_time));
52 
53 #ifdef __VERSION__
54 	err |= re_hprintf(pf, " Compiler: %s\n", __VERSION__);
55 #endif
56 
57 #ifdef USE_OPENSSL
58 	err |= re_hprintf(pf, " OpenSSL:  %s\n",
59 			  SSLeay_version(SSLEAY_VERSION));
60 #endif
61 
62 	return err;
63 }
64 
65 
cmd_config_print(struct re_printf * pf,void * unused)66 static int cmd_config_print(struct re_printf *pf, void *unused)
67 {
68 	(void)unused;
69 	return config_print(pf, conf_config());
70 }
71 
72 
cmd_ua_debug(struct re_printf * pf,void * unused)73 static int cmd_ua_debug(struct re_printf *pf, void *unused)
74 {
75 	(void)unused;
76 	return ua_debug(pf, uag_current());
77 }
78 
79 
cmd_play_file(struct re_printf * pf,void * arg)80 static int cmd_play_file(struct re_printf *pf, void *arg)
81 {
82 	struct cmd_arg *carg = arg;
83 	const char *filename = carg->prm;
84 	int err;
85 
86 	err = re_hprintf(pf, "playing audio file \"%s\" ..\n", filename);
87 	if (err)
88 		return err;
89 
90 	err = play_file(NULL, baresip_player(), filename, 0);
91 	if (err) {
92 		warning("debug_cmd: play_file(%s) failed (%m)\n",
93 			filename, err);
94 		return err;
95 	}
96 
97 	return err;
98 }
99 
100 
reload_config(struct re_printf * pf,void * arg)101 static int reload_config(struct re_printf *pf, void *arg)
102 {
103 	int err;
104 	(void)arg;
105 
106 	err = re_hprintf(pf, "reloading config file ..\n");
107 	if (err)
108 		return err;
109 
110 	err = conf_configure();
111 	if (err) {
112 		(void)re_hprintf(pf, "reload_config failed: %m\n", err);
113 		return err;
114 	}
115 
116 	(void)re_hprintf(pf, "done\n");
117 
118 	return 0;
119 }
120 
121 
122 static const struct cmd debugcmdv[] = {
123 {"main",     0,       0, "Main loop debug",          re_debug             },
124 {"config",   0,       0, "Print configuration",      cmd_config_print     },
125 {"sipstat", 'i',      0, "SIP debug",                ua_print_sip_status  },
126 {"modules",  0,       0, "Module debug",             mod_debug            },
127 {"netstat", 'n',      0, "Network debug",            cmd_net_debug        },
128 {"sysinfo", 's',      0, "System info",              print_system_info    },
129 {"timers",   0,       0, "Timer debug",              tmr_status           },
130 {"uastat",  'u',      0, "UA debug",                 cmd_ua_debug         },
131 {"memstat", 'y',      0, "Memory status",            mem_status           },
132 {"play",     0, CMD_PRM, "Play audio file",          cmd_play_file        },
133 {"conf_reload",0,     0, "Reload config file",       reload_config        },
134 };
135 
136 
module_init(void)137 static int module_init(void)
138 {
139 	int err;
140 
141 	start_ticks = tmr_jiffies();
142 	(void)time(&start_time);
143 
144 	err = cmd_register(baresip_commands(),
145 			   debugcmdv, ARRAY_SIZE(debugcmdv));
146 
147 	return err;
148 }
149 
150 
module_close(void)151 static int module_close(void)
152 {
153 	cmd_unregister(baresip_commands(), debugcmdv);
154 
155 	return 0;
156 }
157 
158 
159 const struct mod_export DECL_EXPORTS(debug_cmd) = {
160 	"debug_cmd",
161 	"application",
162 	module_init,
163 	module_close
164 };
165