debugger_mod.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * This file is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. *
  14. * This file is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "../../sr_module.h"
  29. #include "../../dprint.h"
  30. #include "../../ut.h"
  31. #include "../../mod_fix.h"
  32. #include "../../parser/parse_param.h"
  33. #include "../../shm_init.h"
  34. #include "debugger_api.h"
  35. MODULE_VERSION
  36. static int mod_init(void);
  37. static int child_init(int rank);
  38. static void mod_destroy(void);
  39. static int w_dbg_breakpoint(struct sip_msg* msg, char* point, char* str2);
  40. static int fixup_dbg_breakpoint(void** param, int param_no);
  41. /* parameters */
  42. extern int _dbg_cfgtrace;
  43. extern int _dbg_breakpoint;
  44. extern int _dbg_cfgtrace_level;
  45. extern int _dbg_cfgtrace_facility;
  46. extern char *_dbg_cfgtrace_prefix;
  47. extern int _dbg_step_usleep;
  48. extern int _dbg_step_loops;
  49. static char * _dbg_cfgtrace_facility_str = 0;
  50. static cmd_export_t cmds[]={
  51. {"dbg_breakpoint", (cmd_function)w_dbg_breakpoint, 1,
  52. fixup_dbg_breakpoint, 0, ANY_ROUTE},
  53. {0, 0, 0, 0, 0, 0}
  54. };
  55. static param_export_t params[]={
  56. {"cfgtrace", INT_PARAM, &_dbg_cfgtrace},
  57. {"breakpoint", INT_PARAM, &_dbg_breakpoint},
  58. {"log_level", INT_PARAM, &_dbg_cfgtrace_level},
  59. {"log_facility", STR_PARAM, &_dbg_cfgtrace_facility_str},
  60. {"log_prefix", STR_PARAM, &_dbg_cfgtrace_prefix},
  61. {"step_usleep", INT_PARAM, &_dbg_step_usleep},
  62. {"step_loops", INT_PARAM, &_dbg_step_loops},
  63. {0, 0, 0}
  64. };
  65. struct module_exports exports = {
  66. "debugger",
  67. DEFAULT_DLFLAGS, /* dlopen flags */
  68. cmds,
  69. params,
  70. 0,
  71. 0, /* exported MI functions */
  72. 0, /* exported pseudo-variables */
  73. 0, /* extra processes */
  74. mod_init, /* module initialization function */
  75. 0, /* response function */
  76. mod_destroy, /* destroy function */
  77. child_init /* per child init function */
  78. };
  79. /**
  80. * init module function
  81. */
  82. static int mod_init(void)
  83. {
  84. int fl;
  85. if (_dbg_cfgtrace_facility_str!=NULL)
  86. {
  87. fl = str2facility(_dbg_cfgtrace_facility_str);
  88. if (fl != -1)
  89. {
  90. _dbg_cfgtrace_facility = fl;
  91. } else {
  92. LM_ERR("invalid log facility configured");
  93. return -1;
  94. }
  95. }
  96. if(dbg_init_rpc()!=0)
  97. {
  98. LM_ERR("failed to register RPC commands\n");
  99. return -1;
  100. }
  101. return dbg_init_bp_list();
  102. }
  103. /**
  104. * child init function
  105. */
  106. static int child_init(int rank)
  107. {
  108. LM_DBG("rank is (%d)\n", rank);
  109. if (rank==PROC_INIT)
  110. return dbg_init_pid_list();
  111. return dbg_init_mypid();
  112. }
  113. /**
  114. * destroy module function
  115. */
  116. static void mod_destroy(void)
  117. {
  118. }
  119. /**
  120. * cfg wrapper to set breakpoint (not implemented yet)
  121. */
  122. static int w_dbg_breakpoint(struct sip_msg* msg, char* point, char* str2)
  123. {
  124. return 1;
  125. }
  126. /**
  127. * get the pointer to action structure
  128. */
  129. static struct action *dbg_fixup_get_action(void **param, int param_no)
  130. {
  131. struct action *ac, ac2;
  132. action_u_t *au, au2;
  133. /* param points to au->u.string, get pointer to au */
  134. au = (void*) ((char *)param - ((char *)&au2.u.string-(char *)&au2));
  135. au = au - 1 - param_no;
  136. ac = (void*) ((char *)au - ((char *)&ac2.val-(char *)&ac2));
  137. return ac;
  138. }
  139. /**
  140. * fixup for cfg set breakpoint function
  141. */
  142. static int fixup_dbg_breakpoint(void** param, int param_no)
  143. {
  144. struct action *a;
  145. char *p;
  146. if(param_no!=1)
  147. return -1;
  148. a = dbg_fixup_get_action(param, param_no);
  149. p = (char*)(*param);
  150. return dbg_add_breakpoint(a, (*p=='0')?0:1);
  151. }