func_frame_trace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 = "TEXT_DATA" />
  58. <enum name = "IMAGE" />
  59. <enum name = "HTML" />
  60. <enum name = "CNG" />
  61. <enum name = "MODEM" />
  62. </enumlist>
  63. </parameter>
  64. </syntax>
  65. <description>
  66. <para>Examples:</para>
  67. <para>exten => 1,1,Set(FRAME_TRACE(white)=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
  68. <para>exten => 1,1,Set(FRAME_TRACE()=DTMF_BEGIN,DTMF_END); view only DTMF frames. </para>
  69. <para>exten => 1,1,Set(FRAME_TRACE(black)=DTMF_BEGIN,DTMF_END); view everything except DTMF frames. </para>
  70. </description>
  71. </function>
  72. ***/
  73. static void print_frame(struct ast_frame *frame);
  74. static struct {
  75. enum ast_frame_type type;
  76. const char *str;
  77. } frametype2str[] = {
  78. { AST_FRAME_DTMF_BEGIN, "DTMF_BEGIN" },
  79. { AST_FRAME_DTMF_END, "DTMF_END" },
  80. { AST_FRAME_VOICE, "VOICE" },
  81. { AST_FRAME_VIDEO, "VIDEO" },
  82. { AST_FRAME_CONTROL, "CONTROL" },
  83. { AST_FRAME_NULL, "NULL" },
  84. { AST_FRAME_IAX, "IAX" },
  85. { AST_FRAME_TEXT, "TEXT" },
  86. { AST_FRAME_TEXT_DATA, "TEXT_DATA" },
  87. { AST_FRAME_IMAGE, "IMAGE" },
  88. { AST_FRAME_HTML, "HTML" },
  89. { AST_FRAME_CNG, "CNG" },
  90. { AST_FRAME_MODEM, "MODEM" },
  91. };
  92. struct frame_trace_data {
  93. int list_type; /* 0 = white, 1 = black */
  94. int values[ARRAY_LEN(frametype2str)];
  95. };
  96. static void datastore_destroy_cb(void *data) {
  97. ast_free(data);
  98. }
  99. static const struct ast_datastore_info frame_trace_datastore = {
  100. .type = "frametrace",
  101. .destroy = datastore_destroy_cb
  102. };
  103. static void hook_destroy_cb(void *framedata)
  104. {
  105. ast_free(framedata);
  106. }
  107. static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
  108. {
  109. int i;
  110. int show_frame = 0;
  111. struct frame_trace_data *framedata = data;
  112. if (!frame) {
  113. return frame;
  114. }
  115. if ((event != AST_FRAMEHOOK_EVENT_WRITE) && (event != AST_FRAMEHOOK_EVENT_READ)) {
  116. return frame;
  117. }
  118. for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
  119. if (frame->frametype == frametype2str[i].type) {
  120. if ((framedata->list_type == 0) && (framedata->values[i])) { /* white list */
  121. show_frame = 1;
  122. } else if ((framedata->list_type == 1) && (!framedata->values[i])){ /* black list */
  123. show_frame = 1;
  124. }
  125. break;
  126. }
  127. }
  128. if (show_frame) {
  129. ast_verbose("%s on Channel %s\n", event == AST_FRAMEHOOK_EVENT_READ ? "<--Read" : "--> Write", ast_channel_name(chan));
  130. print_frame(frame);
  131. }
  132. return frame;
  133. }
  134. static int frame_trace_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  135. {
  136. struct frame_trace_data *framedata;
  137. struct ast_datastore *datastore = NULL;
  138. struct ast_framehook_interface interface = {
  139. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  140. .event_cb = hook_event_cb,
  141. .destroy_cb = hook_destroy_cb,
  142. };
  143. int i = 0;
  144. if (!chan) {
  145. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  146. return -1;
  147. }
  148. if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
  149. return 0;
  150. }
  151. interface.data = framedata;
  152. if (!strcasecmp(data, "black")) {
  153. framedata->list_type = 1;
  154. }
  155. for (i = 0; i < ARRAY_LEN(frametype2str); i++) {
  156. if (strcasestr(value, frametype2str[i].str)) {
  157. framedata->values[i] = 1;
  158. }
  159. }
  160. ast_channel_lock(chan);
  161. i = ast_framehook_attach(chan, &interface);
  162. if (i >= 0) {
  163. int *id;
  164. if ((datastore = ast_channel_datastore_find(chan, &frame_trace_datastore, NULL))) {
  165. id = datastore->data;
  166. ast_framehook_detach(chan, *id);
  167. ast_channel_datastore_remove(chan, datastore);
  168. ast_datastore_free(datastore);
  169. }
  170. if (!(datastore = ast_datastore_alloc(&frame_trace_datastore, NULL))) {
  171. ast_framehook_detach(chan, i);
  172. ast_channel_unlock(chan);
  173. return 0;
  174. }
  175. if (!(id = ast_calloc(1, sizeof(int)))) {
  176. ast_datastore_free(datastore);
  177. ast_framehook_detach(chan, i);
  178. ast_channel_unlock(chan);
  179. return 0;
  180. }
  181. *id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
  182. datastore->data = id;
  183. ast_channel_datastore_add(chan, datastore);
  184. }
  185. ast_channel_unlock(chan);
  186. return 0;
  187. }
  188. static void print_frame(struct ast_frame *frame)
  189. {
  190. switch (frame->frametype) {
  191. case AST_FRAME_DTMF_END:
  192. ast_verbose("FrameType: DTMF END\n");
  193. ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
  194. frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
  195. break;
  196. case AST_FRAME_VOICE:
  197. ast_verbose("FrameType: VOICE\n");
  198. ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
  199. ast_verbose("MS: %ld\n", frame->len);
  200. ast_verbose("Samples: %d\n", frame->samples);
  201. ast_verbose("Bytes: %d\n", frame->datalen);
  202. break;
  203. case AST_FRAME_VIDEO:
  204. ast_verbose("FrameType: VIDEO\n");
  205. ast_verbose("Codec: %s\n", ast_format_get_name(frame->subclass.format));
  206. ast_verbose("MS: %ld\n", frame->len);
  207. ast_verbose("Samples: %d\n", frame->samples);
  208. ast_verbose("Bytes: %d\n", frame->datalen);
  209. break;
  210. case AST_FRAME_CONTROL:
  211. ast_verbose("FrameType: CONTROL\n");
  212. switch ((enum ast_control_frame_type) frame->subclass.integer) {
  213. case AST_CONTROL_HANGUP:
  214. ast_verbose("SubClass: HANGUP\n");
  215. break;
  216. case AST_CONTROL_RING:
  217. ast_verbose("SubClass: RING\n");
  218. break;
  219. case AST_CONTROL_RINGING:
  220. ast_verbose("SubClass: RINGING\n");
  221. break;
  222. case AST_CONTROL_ANSWER:
  223. ast_verbose("SubClass: ANSWER\n");
  224. break;
  225. case AST_CONTROL_BUSY:
  226. ast_verbose("SubClass: BUSY\n");
  227. break;
  228. case AST_CONTROL_TAKEOFFHOOK:
  229. ast_verbose("SubClass: TAKEOFFHOOK\n");
  230. break;
  231. case AST_CONTROL_OFFHOOK:
  232. ast_verbose("SubClass: OFFHOOK\n");
  233. break;
  234. case AST_CONTROL_CONGESTION:
  235. ast_verbose("SubClass: CONGESTION\n");
  236. break;
  237. case AST_CONTROL_FLASH:
  238. ast_verbose("SubClass: FLASH\n");
  239. break;
  240. case AST_CONTROL_WINK:
  241. ast_verbose("SubClass: WINK\n");
  242. break;
  243. case AST_CONTROL_OPTION:
  244. ast_verbose("SubClass: OPTION\n");
  245. break;
  246. case AST_CONTROL_RADIO_KEY:
  247. ast_verbose("SubClass: RADIO KEY\n");
  248. break;
  249. case AST_CONTROL_RADIO_UNKEY:
  250. ast_verbose("SubClass: RADIO UNKEY\n");
  251. break;
  252. case AST_CONTROL_PROGRESS:
  253. ast_verbose("SubClass: PROGRESS\n");
  254. break;
  255. case AST_CONTROL_PROCEEDING:
  256. ast_verbose("SubClass: PROCEEDING\n");
  257. break;
  258. case AST_CONTROL_HOLD:
  259. ast_verbose("SubClass: HOLD\n");
  260. break;
  261. case AST_CONTROL_UNHOLD:
  262. ast_verbose("SubClass: UNHOLD\n");
  263. break;
  264. case AST_CONTROL_VIDUPDATE:
  265. ast_verbose("SubClass: VIDUPDATE\n");
  266. break;
  267. case _XXX_AST_CONTROL_T38:
  268. ast_verbose("SubClass: XXX T38\n");
  269. break;
  270. case AST_CONTROL_SRCUPDATE:
  271. ast_verbose("SubClass: SRCUPDATE\n");
  272. break;
  273. case AST_CONTROL_TRANSFER:
  274. ast_verbose("SubClass: TRANSFER\n");
  275. break;
  276. case AST_CONTROL_CONNECTED_LINE:
  277. ast_verbose("SubClass: CONNECTED LINE\n");
  278. break;
  279. case AST_CONTROL_REDIRECTING:
  280. ast_verbose("SubClass: REDIRECTING\n");
  281. break;
  282. case AST_CONTROL_T38_PARAMETERS:
  283. ast_verbose("SubClass: T38 PARAMETERS\n");
  284. break;
  285. case AST_CONTROL_CC:
  286. ast_verbose("SubClass: CC\n");
  287. break;
  288. case AST_CONTROL_SRCCHANGE:
  289. ast_verbose("SubClass: SRCCHANGE\n");
  290. break;
  291. case AST_CONTROL_READ_ACTION:
  292. ast_verbose("SubClass: READ ACTION\n");
  293. break;
  294. case AST_CONTROL_AOC:
  295. ast_verbose("SubClass: AOC\n");
  296. break;
  297. case AST_CONTROL_MCID:
  298. ast_verbose("SubClass: MCID\n");
  299. break;
  300. case AST_CONTROL_INCOMPLETE:
  301. ast_verbose("SubClass: INCOMPLETE\n");
  302. break;
  303. case AST_CONTROL_END_OF_Q:
  304. ast_verbose("SubClass: END_OF_Q\n");
  305. break;
  306. case AST_CONTROL_UPDATE_RTP_PEER:
  307. ast_verbose("SubClass: UPDATE_RTP_PEER\n");
  308. break;
  309. case AST_CONTROL_PVT_CAUSE_CODE:
  310. ast_verbose("SubClass: PVT_CAUSE_CODE\n");
  311. break;
  312. case AST_CONTROL_MASQUERADE_NOTIFY:
  313. /* Should never happen. */
  314. ast_assert(0);
  315. break;
  316. case AST_CONTROL_STREAM_STOP:
  317. ast_verbose("SubClass: STREAM_STOP\n");
  318. break;
  319. case AST_CONTROL_STREAM_SUSPEND:
  320. ast_verbose("SubClass: STREAM_SUSPEND\n");
  321. break;
  322. case AST_CONTROL_STREAM_RESTART:
  323. ast_verbose("SubClass: STREAM_RESTART\n");
  324. break;
  325. case AST_CONTROL_STREAM_REVERSE:
  326. ast_verbose("SubClass: STREAM_REVERSE\n");
  327. break;
  328. case AST_CONTROL_STREAM_FORWARD:
  329. ast_verbose("SubClass: STREAM_FORWARD\n");
  330. break;
  331. case AST_CONTROL_RECORD_CANCEL:
  332. ast_verbose("SubClass: RECORD_CANCEL\n");
  333. break;
  334. case AST_CONTROL_RECORD_STOP:
  335. ast_verbose("SubClass: RECORD_STOP\n");
  336. break;
  337. case AST_CONTROL_RECORD_SUSPEND:
  338. ast_verbose("SubClass: RECORD_SUSPEND\n");
  339. break;
  340. case AST_CONTROL_RECORD_MUTE:
  341. ast_verbose("SubClass: RECORD_MUTE\n");
  342. break;
  343. }
  344. if (frame->subclass.integer == -1) {
  345. ast_verbose("SubClass: %d\n", frame->subclass.integer);
  346. }
  347. ast_verbose("Bytes: %d\n", frame->datalen);
  348. break;
  349. case AST_FRAME_RTCP:
  350. ast_verbose("FrameType: RTCP\n");
  351. break;
  352. case AST_FRAME_NULL:
  353. ast_verbose("FrameType: NULL\n");
  354. break;
  355. case AST_FRAME_IAX:
  356. ast_verbose("FrameType: IAX\n");
  357. break;
  358. case AST_FRAME_TEXT:
  359. ast_verbose("FrameType: TXT\n");
  360. break;
  361. case AST_FRAME_TEXT_DATA:
  362. ast_verbose("FrameType: TXT_DATA\n");
  363. break;
  364. case AST_FRAME_IMAGE:
  365. ast_verbose("FrameType: IMAGE\n");
  366. break;
  367. case AST_FRAME_HTML:
  368. ast_verbose("FrameType: HTML\n");
  369. break;
  370. case AST_FRAME_CNG:
  371. ast_verbose("FrameType: CNG\n");
  372. break;
  373. case AST_FRAME_MODEM:
  374. ast_verbose("FrameType: MODEM\n");
  375. break;
  376. case AST_FRAME_DTMF_BEGIN:
  377. ast_verbose("FrameType: DTMF BEGIN\n");
  378. ast_verbose("Digit: 0x%02X '%c'\n", (unsigned)frame->subclass.integer,
  379. frame->subclass.integer < ' ' ? ' ' : frame->subclass.integer);
  380. break;
  381. case AST_FRAME_BRIDGE_ACTION:
  382. ast_verbose("FrameType: Bridge\n");
  383. ast_verbose("SubClass: %d\n", frame->subclass.integer);
  384. break;
  385. case AST_FRAME_BRIDGE_ACTION_SYNC:
  386. ast_verbose("Frametype: Synchronous Bridge\n");
  387. ast_verbose("Subclass: %d\n", frame->subclass.integer);
  388. break;
  389. }
  390. ast_verbose("Src: %s\n", ast_strlen_zero(frame->src) ? "NOT PRESENT" : frame->src);
  391. ast_verbose("\n");
  392. }
  393. static struct ast_custom_function frame_trace_function = {
  394. .name = "FRAME_TRACE",
  395. .write = frame_trace_helper,
  396. };
  397. static int unload_module(void)
  398. {
  399. return ast_custom_function_unregister(&frame_trace_function);
  400. }
  401. static int load_module(void)
  402. {
  403. int res = ast_custom_function_register(&frame_trace_function);
  404. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  405. }
  406. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Frame Trace for internal ast_frame debugging.");