debugger: updated to the new mod interface
[sip-router] / src / modules / debugger / debugger_mod.c
1 /**
2  * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version
10  *
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "../cfgt/cfgt.h"
29 #include "../../core/sr_module.h"
30 #include "../../core/dprint.h"
31 #include "../../core/ut.h"
32 #include "../../core/mod_fix.h"
33 #include "../../core/parser/parse_param.h"
34 #include "../../core/shm_init.h"
35 #include "../../core/script_cb.h"
36 #include "../../core/msg_translator.h"
37 #include "../../core/kemi.h"
38
39 #include "debugger_api.h"
40 #include "debugger_config.h"
41 #include "debugger_json.h"
42
43 MODULE_VERSION
44
45 static int mod_init(void);
46 static int child_init(int rank);
47 static void mod_destroy(void);
48
49 static int w_dbg_breakpoint(struct sip_msg* msg, char* point, char* str2);
50 static int fixup_dbg_breakpoint(void** param, int param_no);
51 static int dbg_mod_level_param(modparam_t type, void *val);
52 static int dbg_mod_facility_param(modparam_t type, void *val);
53
54 static int fixup_dbg_pv_dump(void** param, int param_no);
55 static int w_dbg_dump(struct sip_msg* msg, char* mask, char* level);
56
57 static struct action *dbg_fixup_get_action(void **param, int param_no);
58 static int fixup_dbg_sip_msg(void** param, int param_no);
59 static int w_dbg_sip_msg(struct sip_msg* msg, char *level, char *facility);
60
61 extern char* dump_lump_list(struct lump *list, int s_offset, char *s_buf);
62
63 /* parameters */
64 extern int _dbg_cfgtrace;
65 extern int _dbg_cfgpkgcheck;
66 extern int _dbg_breakpoint;
67 extern int _dbg_cfgtrace_level;
68 extern int _dbg_cfgtrace_facility;
69 extern char *_dbg_cfgtrace_prefix;
70 extern char *_dbg_cfgtrace_lname;
71 extern int _dbg_step_usleep;
72 extern int _dbg_step_loops;
73 extern int _dbg_reset_msgid;
74 extern int _dbg_cfgtest;
75
76 /* cfgt api */
77 extern cfgt_api_t _dbg_cfgt;
78
79 static int _dbg_sip_msg_cline;
80 static char * _dbg_cfgtrace_facility_str = 0;
81 static int _dbg_log_assign = 0;
82
83 static cmd_export_t cmds[]={
84         {"dbg_breakpoint", (cmd_function)w_dbg_breakpoint, 1,
85                 fixup_dbg_breakpoint, 0, ANY_ROUTE},
86         {"dbg_pv_dump", (cmd_function)w_dbg_dump, 0,
87                 fixup_dbg_pv_dump, 0, ANY_ROUTE},
88         {"dbg_pv_dump", (cmd_function)w_dbg_dump, 1,
89                 fixup_dbg_pv_dump, 0, ANY_ROUTE},
90         {"dbg_pv_dump", (cmd_function)w_dbg_dump, 2,
91                 fixup_dbg_pv_dump, 0, ANY_ROUTE},
92         {"dbg_sip_msg", (cmd_function)w_dbg_sip_msg, 0,
93                 fixup_dbg_sip_msg, 0, REQUEST_ROUTE|ONREPLY_ROUTE},
94         {"dbg_sip_msg", (cmd_function)w_dbg_sip_msg, 1,
95                 fixup_dbg_sip_msg, 0, REQUEST_ROUTE|ONREPLY_ROUTE},
96         {"dbg_sip_msg", (cmd_function)w_dbg_sip_msg, 2,
97                 fixup_dbg_sip_msg, 0, REQUEST_ROUTE|ONREPLY_ROUTE},
98         {0, 0, 0, 0, 0, 0}
99 };
100
101 static param_export_t params[]={
102         {"cfgtrace",          INT_PARAM, &_dbg_cfgtrace},
103         {"breakpoint",        INT_PARAM, &_dbg_breakpoint},
104         {"log_level",         INT_PARAM, &_dbg_cfgtrace_level},
105         {"log_facility",      PARAM_STRING, &_dbg_cfgtrace_facility_str},
106         {"log_prefix",        PARAM_STRING, &_dbg_cfgtrace_prefix},
107         {"log_level_name",    PARAM_STRING, &_dbg_cfgtrace_lname},
108         {"log_assign",        INT_PARAM, &_dbg_log_assign},
109         {"step_usleep",       INT_PARAM, &_dbg_step_usleep},
110         {"step_loops",        INT_PARAM, &_dbg_step_loops},
111         {"mod_hash_size",     INT_PARAM, &default_dbg_cfg.mod_hash_size},
112         {"mod_level_mode",    INT_PARAM, &default_dbg_cfg.mod_level_mode},
113         {"mod_level",         PARAM_STRING|USE_FUNC_PARAM, (void*)dbg_mod_level_param},
114         {"mod_facility_mode", INT_PARAM, &default_dbg_cfg.mod_facility_mode},
115         {"mod_facility",      PARAM_STRING|USE_FUNC_PARAM, (void*)dbg_mod_facility_param},
116         {"reset_msgid",       INT_PARAM, &_dbg_reset_msgid},
117         {"cfgpkgcheck",       INT_PARAM, &_dbg_cfgpkgcheck},
118         {"cfgtest",           INT_PARAM, &_dbg_cfgtest},
119         {0, 0, 0}
120 };
121
122 struct module_exports exports = {
123         "debugger",      /* module name */
124         DEFAULT_DLFLAGS, /* dlopen flags */
125         cmds,            /* cmd (cfg function) exports */
126         params,          /* param exports */
127         0,               /* RPC method exports */
128         0,               /* pseudo-variables exports */
129         0,               /* response handling function */
130         mod_init,        /* module init function */
131         child_init,      /* per-child init function */
132         mod_destroy      /* module destroy function */
133 };
134
135
136 /**
137  * init module function
138  */
139 static int mod_init(void)
140 {
141         int fl;
142         bind_cfgt_t bind_cfgt;
143
144         if (_dbg_cfgtrace_facility_str!=NULL)
145         {
146                 fl = str2facility(_dbg_cfgtrace_facility_str);
147                 if (fl != -1)
148                 {
149                         _dbg_cfgtrace_facility = fl;
150                 } else {
151                         LM_ERR("invalid log facility configured");
152                         return -1;
153                 }
154         }
155
156         if(dbg_init_rpc()!=0)
157         {
158                 LM_ERR("failed to register RPC commands\n");
159                 return -1;
160         }
161
162         if(cfg_declare("dbg", dbg_cfg_def, &default_dbg_cfg, cfg_sizeof(dbg), &dbg_cfg))
163         {
164                 LM_ERR("Fail to declare the configuration\n");
165                 return -1;
166         }
167
168         /* anyhow, should fail before */
169         if (!dbg_cfg) {
170                 return -1;
171         }
172
173         LM_DBG("cfg level_mode:%d facility_mode:%d hash_size:%d\n",
174                 cfg_get(dbg, dbg_cfg, mod_level_mode),
175                 cfg_get(dbg, dbg_cfg, mod_facility_mode),
176                 cfg_get(dbg, dbg_cfg, mod_hash_size));
177
178         if(dbg_init_mod_levels(cfg_get(dbg, dbg_cfg, mod_hash_size))<0)
179         {
180                 LM_ERR("failed to init per module log level\n");
181                 return -1;
182         }
183
184         if(_dbg_log_assign>0)
185         {
186                 if(dbg_init_pvcache()!=0)
187                 {
188                         LM_ERR("failed to create pvcache\n");
189                         return -1;
190                 }
191         }
192         if(_dbg_reset_msgid==1)
193         {
194                 unsigned int ALL = REQUEST_CB+FAILURE_CB+ONREPLY_CB
195                   +BRANCH_CB+ONSEND_CB+ERROR_CB+LOCAL_CB+EVENT_CB+BRANCH_FAILURE_CB;
196                 if (register_script_cb(dbg_msgid_filter, PRE_SCRIPT_CB|ALL, 0) != 0) {
197                         LM_ERR("could not insert callback");
198                         return -1;
199                 }
200         }
201         if(_dbg_cfgtest==1)
202         {
203                 bind_cfgt = (bind_cfgt_t)find_export("cfgt_bind_cfgt", 1, 0);
204                 if (!bind_cfgt) {
205                         LM_ERR("can't find cfgt module\n");
206                         return -1;
207                 }
208
209                 if (bind_cfgt(&_dbg_cfgt) < 0) {
210                         return -1;
211                 }
212                 LM_INFO("bind to cfgt module\n");
213         }
214         return dbg_init_bp_list();
215 }
216
217 /**
218  * child init function
219  */
220 static int child_init(int rank)
221 {
222         LM_DBG("rank is (%d)\n", rank);
223         if (rank==PROC_INIT) {
224                 dbg_enable_mod_levels();
225                 dbg_enable_mod_facilities();
226                 dbg_enable_log_assign();
227                 return dbg_init_pid_list();
228         }
229         return dbg_init_mypid();
230 }
231
232 /**
233  * destroy module function
234  */
235 static void mod_destroy(void)
236 {
237         dbg_cfg = NULL;
238         dbg_destroy_mod_levels();
239 }
240
241 /**
242  * cfg wrapper to set breakpoint (not implemented yet)
243  */
244 static int w_dbg_breakpoint(struct sip_msg* msg, char* point, char* str2)
245 {
246         return 1;
247 }
248
249 /**
250  * fixup for cfg dbg_pv_dump
251  */
252 static int fixup_dbg_pv_dump(void** param, int param_no)
253 {
254         unsigned int mask;
255         int level;
256         str s = STR_NULL;
257
258         switch(param_no)
259         {
260                 case 2:
261                         switch(((char*)(*param))[2])
262                         {
263                                 case 'A': level = L_ALERT; break;
264                                 case 'B': level = L_BUG; break;
265                                 case 'C': level = L_CRIT2; break;
266                                 case 'E': level = L_ERR; break;
267                                 case 'W': level = L_WARN; break;
268                                 case 'N': level = L_NOTICE; break;
269                                 case 'I': level = L_INFO; break;
270                                 case 'D': level = L_DBG; break;
271                                 default:
272                                         LM_ERR("unknown log level\n");
273                                         return E_UNSPEC;
274                         }
275                         *param = (void*)(long)level;
276                 break;
277                 case 1:
278                         s.s = *param;
279                         s.len = strlen(s.s);
280                         if(str2int(&s, &mask) == 0) {
281                                 *param = (void*)(long)mask;
282                         }
283                         else return E_UNSPEC;
284                 break;
285         }
286
287         return 0;
288 }
289
290 /**
291  * dump pv_cache contents as json
292  */
293 static int w_dbg_dump(struct sip_msg* msg, char* mask, char* level)
294 {
295         unsigned int umask = DBG_DP_ALL;
296         int ilevel = L_DBG;
297         if(level!=NULL){
298                 ilevel = (int)(long)level;
299         }
300         if(mask!=NULL){
301                 umask = (unsigned int)(unsigned long)mask;
302         }
303         dbg_dump_json(msg, umask, ilevel);
304         return 1;
305 }
306
307 /**
308  * get the pointer to action structure
309  */
310 static struct action *dbg_fixup_get_action(void **param, int param_no)
311 {
312         struct action *ac, ac2;
313         action_u_t *au, au2;
314         /* param points to au->u.string, get pointer to au */
315         au = (void*) ((char *)param - ((char *)&au2.u.string-(char *)&au2));
316         au = au - 1 - param_no;
317         ac = (void*) ((char *)au - ((char *)&ac2.val-(char *)&ac2));
318         return ac;
319 }
320
321
322 /**
323  * fixup for cfg set breakpoint function
324  */
325 static int fixup_dbg_breakpoint(void** param, int param_no)
326 {
327         struct action *a;
328         char *p;
329
330         if(param_no!=1)
331                 return -1;
332         a = dbg_fixup_get_action(param, param_no);
333         p = (char*)(*param);
334
335         return dbg_add_breakpoint(a, (*p=='0')?0:1);
336 }
337
338 static int dbg_mod_level_param(modparam_t type, void *val)
339 {
340         char *p;
341         str s;
342         int l;
343         if(val==NULL)
344                 return -1;
345
346         p = strchr((char*)val, '=');
347         if(p==NULL) {
348                 LM_ERR("invalid parameter value: %s\n", (char*)val);
349                 return -1;
350         }
351         s.s = p + 1;
352         s.len = strlen(s.s);
353
354         if(str2sint(&s, &l)<0) {
355                 LM_ERR("invalid parameter - level value: %s\n", (char*)val);
356                 return -1;
357         }
358         s.s = (char*)val;
359         s.len = p - s.s;
360
361         if (!dbg_cfg) {
362                 return -1;
363         }
364
365         LM_DBG("cfg level_mode:%d hash_size:%d\n",
366                 cfg_get(dbg, dbg_cfg, mod_level_mode),
367                 cfg_get(dbg, dbg_cfg, mod_hash_size));
368
369         if(dbg_init_mod_levels(cfg_get(dbg, dbg_cfg, mod_hash_size))<0)
370         {
371                 LM_ERR("failed to init per module log level\n");
372                 return -1;
373         }
374
375         if(dbg_set_mod_debug_level(s.s, s.len, &l)<0)
376         {
377                 LM_ERR("cannot store parameter: %s\n", (char*)val);
378                 return -1;
379         }
380
381         return 0;
382
383 }
384
385 static int dbg_mod_facility_param(modparam_t type, void *val)
386 {
387         char *p;
388         str s;
389         int fl;
390         if(val==NULL)
391                 return -1;
392
393         p = strchr((char*)val, '=');
394         if(p==NULL) {
395                 LM_ERR("invalid parameter value: %s\n", (char*)val);
396                 return -1;
397         }
398         s.s = p + 1;
399         s.len = strlen(s.s);
400
401         if ((fl = str2facility(s.s)) == -1) {
402                 LM_ERR("invalid parameter - facility value: %s\n", (char*)val);
403                 return -1;
404         }
405
406         s.s = (char*)val;
407         s.len = p - s.s;
408
409         if (!dbg_cfg) {
410                 return -1;
411         }
412
413         LM_DBG("cfg facility_mode:%d hash_size:%d\n",
414                 cfg_get(dbg, dbg_cfg, mod_facility_mode),
415                 cfg_get(dbg, dbg_cfg, mod_hash_size));
416
417         if(dbg_init_mod_levels(cfg_get(dbg, dbg_cfg, mod_hash_size))<0)
418         {
419                 LM_ERR("failed to init per module log level\n");
420                 return -1;
421         }
422
423         if(dbg_set_mod_debug_facility(s.s, s.len, &fl)<0)
424         {
425                 LM_ERR("cannot store parameter: %s\n", (char*)val);
426                 return -1;
427         }
428
429         return 0;
430 }
431
432 static int fixup_dbg_sip_msg(void** param, int param_no)
433 {
434         int facility;
435         int level;
436         struct action *dbg_sip_msg_action;
437
438         LM_DBG("dbg_sip_msg() called with %d params\n", param_no);
439
440         switch(param_no)
441         {
442                 case 2:
443                         facility = str2facility((char*)*(param));
444                         if (facility == -1) {
445                                 LM_ERR("invalid log facility configured");
446                                 return E_UNSPEC;
447                         }
448
449                         *param = (void*)(long)facility;
450                         break;
451
452                 case 1:
453                         switch(((char*)(*param))[2])
454                         {
455                                 /* add L_OFFSET because L_WARN is consdered null pointer */
456                                 case 'A': level = L_ALERT + L_OFFSET; break;
457                                 case 'B': level = L_BUG + L_OFFSET; break;
458                                 case 'C': level = L_CRIT2 + L_OFFSET; break;
459                                 case 'E': level = L_ERR + L_OFFSET; break;
460                                 case 'W': level = L_WARN + L_OFFSET; break;
461                                 case 'N': level = L_NOTICE + L_OFFSET; break;
462                                 case 'I': level = L_INFO + L_OFFSET; break;
463                                 case 'D': level = L_DBG + L_OFFSET; break;
464                                 default:
465                                         LM_ERR("unknown log level\n");
466                                         return E_UNSPEC;
467                         }
468
469                         *param = (void*)(long)level;
470                         break;
471
472                 case 0:
473                         _dbg_sip_msg_cline = -1;
474                         return 0;
475
476                 default:
477                         // should not reach here
478                         _dbg_sip_msg_cline = -1;
479                         return -1;
480         }
481
482         /* save the config line where this config function was called */
483         dbg_sip_msg_action = dbg_fixup_get_action(param, param_no);
484         _dbg_sip_msg_cline = dbg_sip_msg_action->cline;
485
486         return 0;
487 }
488
489 /**
490   * dump current SIP message and a diff lump list
491   * part of the code taken from msg_apply_changes_f
492   */
493 static int w_dbg_sip_msg(struct sip_msg* msg, char *level, char *facility)
494 {
495         int ilevel = cfg_get(core, core_cfg, debug);
496         int ifacility= cfg_get(core, core_cfg, log_facility);
497         int flag = FLAG_MSG_LUMPS_ONLY; // copy lumps only, not the whole message
498         unsigned int new_buf_offs=0, orig_offs = 0;
499         char *hdr_lumps = NULL;
500         char *bdy_lumps = NULL;
501         const char *start_txt = "------------------------- START OF SIP message debug --------------------------\n";
502         const char *hdr_txt =   "------------------------------ SIP header diffs -------------------------------\n";
503         const char *bdy_txt =   "------------------------------- SIP body diffs --------------------------------\n";
504         const char *end_txt =   "-------------------------- END OF SIP message debug ---------------------------\n\n";
505         struct dest_info send_info;
506         str obuf;
507
508         if (msg->first_line.type != SIP_REPLY && get_route_type() != REQUEST_ROUTE) {
509                 LM_ERR("invalid usage - not in request route\n");
510                 return -1;
511         }
512
513         if (level != NULL) {
514                 /* substract L_OFFSET previously added */
515                 ilevel = (int)(long)level - L_OFFSET;
516         }
517
518         if (facility != NULL) {
519                 ifacility = (int)(long)facility;
520         }
521
522         /* msg_apply_changes_f code needed to get the current msg */
523         init_dest_info(&send_info);
524         send_info.proto = PROTO_UDP;
525         if(msg->first_line.type == SIP_REPLY) {
526                 obuf.s = generate_res_buf_from_sip_res(msg,
527                                 (unsigned int*)&obuf.len, BUILD_NO_VIA1_UPDATE);
528         } else {
529                 obuf.s = build_req_buf_from_sip_req(msg,
530                                 (unsigned int*)&obuf.len, &send_info,
531                                 BUILD_NO_PATH|BUILD_NO_LOCAL_VIA|BUILD_NO_VIA1_UPDATE);
532         }
533
534         if(obuf.s == NULL)
535         {
536                 LM_ERR("couldn't update msg buffer content\n");
537                 return -1;
538         }
539
540         if(obuf.len >= BUF_SIZE)
541         {
542                 LM_ERR("new buffer overflow (%d)\n", obuf.len);
543                 pkg_free(obuf.s);
544                 return -1;
545         }
546
547         /* skip original uri */
548         if(msg->first_line.type == SIP_REQUEST) {
549                 if(msg->new_uri.s) {
550                         orig_offs = msg->first_line.u.request.uri.s - msg->buf;
551                         orig_offs += msg->first_line.u.request.uri.len;
552                 }
553         }
554
555         /* alloc private mem and copy lumps */
556         hdr_lumps = pkg_malloc(BUF_SIZE);
557         bdy_lumps = pkg_malloc(BUF_SIZE);
558
559         new_buf_offs = 0;
560         process_lumps(msg, msg->add_rm, hdr_lumps, &new_buf_offs, &orig_offs, &send_info, flag);
561
562         new_buf_offs = 0;
563         process_lumps(msg, msg->body_lumps, bdy_lumps, &new_buf_offs, &orig_offs, &send_info, flag);
564
565         /* do the print */
566         if (_dbg_sip_msg_cline < 0 ) {
567                 LOG_FC(ifacility, ilevel, "CONFIG LINE unknown\n%s%.*s%s%s%s%s%s",
568                         start_txt,
569                         obuf.len, obuf.s,
570                         hdr_txt, hdr_lumps,
571                         bdy_txt, bdy_lumps,
572                         end_txt);
573         } else {
574                 LOG_FC(ifacility, ilevel, "CONFIG LINE %d\n%s%.*s%s%s%s%s%s",
575                         _dbg_sip_msg_cline,
576                         start_txt,
577                         obuf.len, obuf.s,
578                         hdr_txt, hdr_lumps,
579                         bdy_txt, bdy_lumps,
580                         end_txt);
581         }
582
583         /* free lumps */
584         if (hdr_lumps) {
585                 pkg_free(hdr_lumps);
586         }
587
588         if (bdy_lumps) {
589                 pkg_free(bdy_lumps);
590         }
591
592         return 1;
593 }
594
595 /**
596  * dump pv_cache contents as json with default parameters
597  */
598 static int ki_dbg_pv_dump(sip_msg_t* msg)
599 {
600         dbg_dump_json(msg, DBG_DP_ALL, L_DBG);
601         return 1;
602 }
603
604 /**
605  * dump pv_cache contents as json with explicit parameters
606  */
607 static int ki_dbg_pv_dump_ex(sip_msg_t* msg, int mask, int level)
608 {
609         dbg_dump_json(msg, (unsigned int)mask, level);
610         return 1;
611 }
612
613 /**
614  *
615  */
616 /* clang-format off */
617 static sr_kemi_t sr_kemi_debugger_exports[] = {
618         { str_init("debugger"), str_init("dbg_pv_dump"),
619                 SR_KEMIP_INT, ki_dbg_pv_dump,
620                 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
621                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
622         },
623         { str_init("debugger"), str_init("dbg_pv_dump_ex"),
624                 SR_KEMIP_INT, ki_dbg_pv_dump_ex,
625                 { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE,
626                         SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
627         },
628
629         { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
630 };
631 /* clang-format on */
632
633 /**
634  *
635  */
636 int mod_register(char *path, int *dlflags, void *p1, void *p2)
637 {
638         sr_kemi_modules_add(sr_kemi_debugger_exports);
639         return 0;
640 }