func_frame_trace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Trace internal ast_frames on a channel.
  21. *
  22. * \author David Vossel <dvossel@digium.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>extended</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/framehook.h"
  35. /*** DOCUMENTATION
  36. <function name="FRAME_TRACE" language="en_US">
  37. <synopsis>
  38. View internal ast_frames as they are read and written on a channel.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="filter list type" required="true">
  42. <para>A filter can be applied to the trace to limit what frames are viewed. This
  43. filter can either be a <literal>white</literal> or <literal>black</literal> list
  44. of frame types. When no filter type is present, <literal>white</literal> is
  45. used. If no arguments are provided at all, all frames will be output.
  46. </para>
  47. <para>Below are the different types of frames that can be filtered.</para>
  48. <enumlist>
  49. <enum name = "DTMF_BEGIN" />
  50. <enum name = "DTMF_END" />
  51. <enum name = "VOICE" />
  52. <enum name = "VIDEO" />
  53. <enum name = "CONTROL" />
  54. <enum name = "NULL" />
  55. <enum name = "IAX" />
  56. <enum name = "TEXT" />
  57. <enum name = "IMAGE" />
  58. <enum name = "HTML" />
  59. <enum name = "CNG" />
  60. <enum name = "MODEM" />
  61. </enumlist>
  62. </parameter>
  63. </syntax>
  64. <description>
  65. <para>Examples:</para>
  66. <para>exten => 1,1,Set(FRAME_TRACE(white)=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
  67. <para>exten => 1,1,Set(FRAME_TRACE()=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
  68. <para>exten => 1,1,Set(FRAME_TRACE(black)=DTMF_BEGIN,DTMF_END); view everything except DTMF frames. </para>
  69. </description>
  70. </function>
  71. ***/
  72. static void print_frame(struct ast_frame *frame);
  73. static struct {
  74. enum ast_frame_type type;
  75. const char *str;
  76. } frametype2str[] = {
  77. { AST_FRAME_DTMF_BEGIN, "DTMF_BEGIN" },
  78. { AST_FRAME_DTMF_END, "DTMF_END" },
  79. { AST_FRAME_VOICE, "VOICE" },
  80. { AST_FRAME_VIDEO, "VIDEO" },
  81. { AST_FRAME_CONTROL, "CONTROL" },
  82. { AST_FRAME_NULL, "NULL" },
  83. { AST_FRAME_IAX, "IAX" },
  84. { AST_FRAME_TEXT, "TEXT" },
  85. { AST_FRAME_IMAGE, "IMAGE" },
  86. { AST_FRAME_HTML, "HTML" },
  87. { AST_FRAME_CNG, "CNG" },
  88. { AST_FRAME_MODEM, "MODEM" },
  89. };
  90. struct frame_trace_data {
  91. int list_type; /* 0 = white, 1 = black */
  92. int values[ARRAY_LEN(frametype2str)];
  93. };
  94. static void datastore_destroy_cb(void *data) {
  95. ast_free(data);
  96. }
  97. static const struct ast_datastore_info frame_trace_datastore = {
  98. .type = "frametrace",
  99. .destroy = datastore_destroy_cb
  100. };
  101. static void hook_destroy_cb(void *framedata)
  102. {
  103. ast_free(framedata);
  104. }
  105. static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
  106. {
  107. int i;
  108. int show_frame = 0;
  109. struct frame_trace_data *framedata = data;
  110. if (!frame) {
  111. return frame;
  112. }
  113. if ((event != AST_FRAMEHOOK_EVENT_WRITE) && (event != AST_FRAMEHOOK_EVENT_READ)) {
  114. return frame;
  115. }
  116. for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
  117. if (frame->frametype == frametype2str[i].type) {
  118. if ((framedata->list_type == 0) && (framedata->values[i])) { /* white list */
  119. show_frame = 1;
  120. } else if ((framedata->list_type == 1) && (!framedata->values[i])){ /* black list */
  121. show_frame = 1;
  122. }
  123. break;
  124. }
  125. }
  126. if (show_frame) {
  127. ast_verbose("%s on Channel %s\n", event == AST_FRAMEHOOK_EVENT_READ ? "<--Read" : "--> Write", ast_channel_name(chan));
  128. print_frame(frame);
  129. }
  130. return frame;
  131. }
  132. static int frame_trace_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  133. {
  134. struct frame_trace_data *framedata;
  135. struct ast_datastore *datastore = NULL;
  136. struct ast_framehook_interface interface = {
  137. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  138. .event_cb = hook_event_cb,
  139. .destroy_cb = hook_destroy_cb,
  140. };
  141. int i = 0;
  142. if (!chan) {
  143. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  144. return -1;
  145. }
  146. if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
  147. return 0;
  148. }
  149. interface.data = framedata;
  150. if (!strcasecmp(data, "black")) {
  151. framedata->list_type = 1;
  152. }
  153. for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
  154. if (strcasestr(value, frametype2str[i].str)) {
  155. framedata->values[i] = 1;
  156. }
  157. }
  158. ast_channel_lock(chan);
  159. i = ast_framehook_attach(chan, &interface);
  160. if (i >= 0) {
  161. int *id;
  162. if ((datastore = ast_channel_datastore_find(chan, &frame_trace_datastore, NULL))) {
  163. id = datastore->data;
  164. ast_framehook_detach(chan, *id);
  165. ast_channel_datastore_remove(chan, datastore);
  166. ast_datastore_free(datastore);
  167. }
  168. if (!(datastore = ast_datastore_alloc(&frame_trace_datastore, NULL))) {
  169. ast_framehook_detach(chan, i);
  170. ast_channel_unlock(chan);
  171. return 0;
  172. }
  173. if (!(id = ast_calloc(1, sizeof(int)))) {
  174. ast_datastore_free(datastore);
  175. ast_framehook_detach(chan, i);
  176. ast_channel_unlock(chan);
  177. return 0;
  178. }
  179. *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
  180. datastore->data = id;
  181. ast_channel_datastore_add(chan, datastore);
  182. }
  183. ast_channel_unlock(chan);
  184. return 0;
  185. }
  186. static void print_frame(struct ast_frame *frame)
  187. {
  188. switch (frame->frametype) {
  189. case AST_FRAME_DTMF_END:
  190. ast_verbose("FrameType: DTMF END\n");
  191. ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
  192. frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
  193. break;
  194. case AST_FRAME_VOICE:
  195. ast_verbose("FrameType: VOICE\n");
  196. ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
  197. ast_verbose("MS: %ld\n", frame->len);
  198. ast_verbose("Samples: %d\n", frame->samples);
  199. ast_verbose("Bytes: %d\n", frame->datalen);
  200. break;
  201. case AST_FRAME_VIDEO:
  202. ast_verbose("FrameType: VIDEO\n");
  203. ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
  204. ast_verbose("MS: %ld\n", frame->len);
  205. ast_verbose("Samples: %d\n", frame->samples);
  206. ast_verbose("Bytes: %d\n", frame->datalen);
  207. break;
  208. case AST_FRAME_CONTROL:
  209. ast_verbose("FrameType: CONTROL\n");
  210. switch ((enum ast_control_frame_type) frame->subclass.integer) {
  211. case AST_CONTROL_HANGUP:
  212. ast_verbose("SubClass: HANGUP\n");
  213. break;
  214. case AST_CONTROL_RING:
  215. ast_verbose("SubClass: RING\n");
  216. break;
  217. case AST_CONTROL_RINGING:
  218. ast_verbose("SubClass: RINGING\n");
  219. break;
  220. case AST_CONTROL_ANSWER:
  221. ast_verbose("SubClass: ANSWER\n");
  222. break;
  223. case AST_CONTROL_BUSY:
  224. ast_verbose("SubClass: BUSY\n");
  225. break;
  226. case AST_CONTROL_TAKEOFFHOOK:
  227. ast_verbose("SubClass: TAKEOFFHOOK\n");
  228. break;
  229. case AST_CONTROL_OFFHOOK:
  230. ast_verbose("SubClass: OFFHOOK\n");
  231. break;
  232. case AST_CONTROL_CONGESTION:
  233. ast_verbose("SubClass: CONGESTION\n");
  234. break;
  235. case AST_CONTROL_FLASH:
  236. ast_verbose("SubClass: FLASH\n");
  237. break;
  238. case AST_CONTROL_WINK:
  239. ast_verbose("SubClass: WINK\n");
  240. break;
  241. case AST_CONTROL_OPTION:
  242. ast_verbose("SubClass: OPTION\n");
  243. break;
  244. case AST_CONTROL_RADIO_KEY:
  245. ast_verbose("SubClass: RADIO KEY\n");
  246. break;
  247. case AST_CONTROL_RADIO_UNKEY:
  248. ast_verbose("SubClass: RADIO UNKEY\n");
  249. break;
  250. case AST_CONTROL_PROGRESS:
  251. ast_verbose("SubClass: PROGRESS\n");
  252. break;
  253. case AST_CONTROL_PROCEEDING:
  254. ast_verbose("SubClass: PROCEEDING\n");
  255. break;
  256. case AST_CONTROL_HOLD:
  257. ast_verbose("SubClass: HOLD\n");
  258. break;
  259. case AST_CONTROL_UNHOLD:
  260. ast_verbose("SubClass: UNHOLD\n");
  261. break;
  262. case AST_CONTROL_VIDUPDATE:
  263. ast_verbose("SubClass: VIDUPDATE\n");
  264. break;
  265. case _XXX_AST_CONTROL_T38:
  266. ast_verbose("SubClass: XXX T38\n");
  267. break;
  268. case AST_CONTROL_SRCUPDATE:
  269. ast_verbose("SubClass: SRCUPDATE\n");
  270. break;
  271. case AST_CONTROL_TRANSFER:
  272. ast_verbose("SubClass: TRANSFER\n");
  273. break;
  274. case AST_CONTROL_CONNECTED_LINE:
  275. ast_verbose("SubClass: CONNECTED LINE\n");
  276. break;
  277. case AST_CONTROL_REDIRECTING:
  278. ast_verbose("SubClass: REDIRECTING\n");
  279. break;
  280. case AST_CONTROL_T38_PARAMETERS:
  281. ast_verbose("SubClass: T38 PARAMETERS\n");
  282. break;
  283. case AST_CONTROL_CC:
  284. ast_verbose("SubClass: CC\n");
  285. break;
  286. case AST_CONTROL_SRCCHANGE:
  287. ast_verbose("SubClass: SRCCHANGE\n");
  288. break;
  289. case AST_CONTROL_READ_ACTION:
  290. ast_verbose("SubClass: READ ACTION\n");
  291. break;
  292. case AST_CONTROL_AOC:
  293. ast_verbose("SubClass: AOC\n");
  294. break;
  295. case AST_CONTROL_MCID:
  296. ast_verbose("SubClass: MCID\n");
  297. break;
  298. case AST_CONTROL_INCOMPLETE:
  299. ast_verbose("SubClass: INCOMPLETE\n");
  300. break;
  301. case AST_CONTROL_END_OF_Q:
  302. ast_verbose("SubClass: END_OF_Q\n");
  303. break;
  304. case AST_CONTROL_UPDATE_RTP_PEER:
  305. ast_verbose("SubClass: UPDATE_RTP_PEER\n");
  306. break;
  307. case AST_CONTROL_PVT_CAUSE_CODE:
  308. ast_verbose("SubClass: PVT_CAUSE_CODE\n");
  309. break;
  310. case AST_CONTROL_MASQUERADE_NOTIFY:
  311. /* Should never happen. */
  312. ast_assert(0);
  313. break;
  314. case AST_CONTROL_STREAM_STOP:
  315. ast_verbose("SubClass: STREAM_STOP\n");
  316. break;
  317. case AST_CONTROL_STREAM_SUSPEND:
  318. ast_verbose("SubClass: STREAM_SUSPEND\n");
  319. break;
  320. case AST_CONTROL_STREAM_RESTART:
  321. ast_verbose("SubClass: STREAM_RESTART\n");
  322. break;
  323. case AST_CONTROL_STREAM_REVERSE:
  324. ast_verbose("SubClass: STREAM_REVERSE\n");
  325. break;
  326. case AST_CONTROL_STREAM_FORWARD:
  327. ast_verbose("SubClass: STREAM_FORWARD\n");
  328. break;
  329. case AST_CONTROL_RECORD_CANCEL:
  330. ast_verbose("SubClass: RECORD_CANCEL\n");
  331. break;
  332. case AST_CONTROL_RECORD_STOP:
  333. ast_verbose("SubClass: RECORD_STOP\n");
  334. break;
  335. case AST_CONTROL_RECORD_SUSPEND:
  336. ast_verbose("SubClass: RECORD_SUSPEND\n");
  337. break;
  338. case AST_CONTROL_RECORD_MUTE:
  339. ast_verbose("SubClass: RECORD_MUTE\n");
  340. break;
  341. }
  342. if (frame->subclass.integer == -1) {
  343. ast_verbose("SubClass: %d\n", frame->subclass.integer);
  344. }
  345. ast_verbose("Bytes: %d\n", frame->datalen);
  346. break;
  347. case AST_FRAME_NULL:
  348. ast_verbose("FrameType: NULL\n");
  349. break;
  350. case AST_FRAME_IAX:
  351. ast_verbose("FrameType: IAX\n");
  352. break;
  353. case AST_FRAME_TEXT:
  354. ast_verbose("FrameType: TXT\n");
  355. break;
  356. case AST_FRAME_IMAGE:
  357. ast_verbose("FrameType: IMAGE\n");
  358. break;
  359. case AST_FRAME_HTML:
  360. ast_verbose("FrameType: HTML\n");
  361. break;
  362. case AST_FRAME_CNG:
  363. ast_verbose("FrameType: CNG\n");
  364. break;
  365. case AST_FRAME_MODEM:
  366. ast_verbose("FrameType: MODEM\n");
  367. break;
  368. case AST_FRAME_DTMF_BEGIN:
  369. ast_verbose("FrameType: DTMF BEGIN\n");
  370. ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
  371. frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
  372. break;
  373. case AST_FRAME_BRIDGE_ACTION:
  374. ast_verbose("FrameType: Bridge\n");
  375. ast_verbose("SubClass: %d\n", frame->subclass.integer);
  376. break;
  377. case AST_FRAME_BRIDGE_ACTION_SYNC:
  378. ast_verbose("Frametype: Synchronous Bridge\n");
  379. ast_verbose("Subclass: %d\n", frame->subclass.integer);
  380. break;
  381. }
  382. ast_verbose("Src: %s\n", ast_strlen_zero(frame->src) ? "NOT PRESENT" : frame->src);
  383. ast_verbose("\n");
  384. }
  385. static struct ast_custom_function frame_trace_function = {
  386. .name = "FRAME_TRACE",
  387. .write = frame_trace_helper,
  388. };
  389. static int unload_module(void)
  390. {
  391. return ast_custom_function_unregister(&frame_trace_function);
  392. }
  393. static int load_module(void)
  394. {
  395. int res = ast_custom_function_register(&frame_trace_function);
  396. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  397. }
  398. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Frame Trace for internal ast_frame debugging.");