tool_cb_dbg.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #define ENABLE_CURLX_PRINTF
  26. /* use our own printf() functions */
  27. #include "curlx.h"
  28. #include "tool_cfgable.h"
  29. #include "tool_msgs.h"
  30. #include "tool_cb_dbg.h"
  31. #include "tool_util.h"
  32. #include "memdebug.h" /* keep this as LAST include */
  33. static void dump(const char *timebuf, const char *text,
  34. FILE *stream, const unsigned char *ptr, size_t size,
  35. trace tracetype, curl_infotype infotype);
  36. /*
  37. ** callback for CURLOPT_DEBUGFUNCTION
  38. */
  39. int tool_debug_cb(CURL *handle, curl_infotype type,
  40. char *data, size_t size,
  41. void *userdata)
  42. {
  43. struct OperationConfig *operation = userdata;
  44. struct GlobalConfig *config = operation->global;
  45. FILE *output = config->errors;
  46. const char *text;
  47. struct timeval tv;
  48. char timebuf[20];
  49. time_t secs;
  50. (void)handle; /* not used */
  51. if(config->tracetime) {
  52. struct tm *now;
  53. static time_t epoch_offset;
  54. static int known_offset;
  55. tv = tvnow();
  56. if(!known_offset) {
  57. epoch_offset = time(NULL) - tv.tv_sec;
  58. known_offset = 1;
  59. }
  60. secs = epoch_offset + tv.tv_sec;
  61. /* !checksrc! disable BANNEDFUNC 1 */
  62. now = localtime(&secs); /* not thread safe but we don't care */
  63. msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
  64. now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
  65. }
  66. else
  67. timebuf[0] = 0;
  68. if(!config->trace_stream) {
  69. /* open for append */
  70. if(!strcmp("-", config->trace_dump))
  71. config->trace_stream = stdout;
  72. else if(!strcmp("%", config->trace_dump))
  73. /* Ok, this is somewhat hackish but we do it undocumented for now */
  74. config->trace_stream = config->errors; /* aka stderr */
  75. else {
  76. config->trace_stream = fopen(config->trace_dump, FOPEN_WRITETEXT);
  77. config->trace_fopened = TRUE;
  78. }
  79. }
  80. if(config->trace_stream)
  81. output = config->trace_stream;
  82. if(!output) {
  83. warnf(config, "Failed to create/open output");
  84. return 0;
  85. }
  86. if(config->tracetype == TRACE_PLAIN) {
  87. /*
  88. * This is the trace look that is similar to what libcurl makes on its
  89. * own.
  90. */
  91. static const char * const s_infotype[] = {
  92. "*", "<", ">", "{", "}", "{", "}"
  93. };
  94. static bool newl = FALSE;
  95. static bool traced_data = FALSE;
  96. switch(type) {
  97. case CURLINFO_HEADER_OUT:
  98. if(size > 0) {
  99. size_t st = 0;
  100. size_t i;
  101. for(i = 0; i < size - 1; i++) {
  102. if(data[i] == '\n') { /* LF */
  103. if(!newl) {
  104. fprintf(output, "%s%s ", timebuf, s_infotype[type]);
  105. }
  106. (void)fwrite(data + st, i - st + 1, 1, output);
  107. st = i + 1;
  108. newl = FALSE;
  109. }
  110. }
  111. if(!newl)
  112. fprintf(output, "%s%s ", timebuf, s_infotype[type]);
  113. (void)fwrite(data + st, i - st + 1, 1, output);
  114. }
  115. newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
  116. traced_data = FALSE;
  117. break;
  118. case CURLINFO_TEXT:
  119. case CURLINFO_HEADER_IN:
  120. if(!newl)
  121. fprintf(output, "%s%s ", timebuf, s_infotype[type]);
  122. (void)fwrite(data, size, 1, output);
  123. newl = (size && (data[size - 1] != '\n')) ? TRUE : FALSE;
  124. traced_data = FALSE;
  125. break;
  126. case CURLINFO_DATA_OUT:
  127. case CURLINFO_DATA_IN:
  128. case CURLINFO_SSL_DATA_IN:
  129. case CURLINFO_SSL_DATA_OUT:
  130. if(!traced_data) {
  131. /* if the data is output to a tty and we're sending this debug trace
  132. to stderr or stdout, we don't display the alert about the data not
  133. being shown as the data _is_ shown then just not via this
  134. function */
  135. if(!config->isatty || ((output != stderr) && (output != stdout))) {
  136. if(!newl)
  137. fprintf(output, "%s%s ", timebuf, s_infotype[type]);
  138. fprintf(output, "[%zu bytes data]\n", size);
  139. newl = FALSE;
  140. traced_data = TRUE;
  141. }
  142. }
  143. break;
  144. default: /* nada */
  145. newl = FALSE;
  146. traced_data = FALSE;
  147. break;
  148. }
  149. return 0;
  150. }
  151. switch(type) {
  152. case CURLINFO_TEXT:
  153. fprintf(output, "%s== Info: %.*s", timebuf, (int)size, data);
  154. /* FALLTHROUGH */
  155. default: /* in case a new one is introduced to shock us */
  156. return 0;
  157. case CURLINFO_HEADER_OUT:
  158. text = "=> Send header";
  159. break;
  160. case CURLINFO_DATA_OUT:
  161. text = "=> Send data";
  162. break;
  163. case CURLINFO_HEADER_IN:
  164. text = "<= Recv header";
  165. break;
  166. case CURLINFO_DATA_IN:
  167. text = "<= Recv data";
  168. break;
  169. case CURLINFO_SSL_DATA_IN:
  170. text = "<= Recv SSL data";
  171. break;
  172. case CURLINFO_SSL_DATA_OUT:
  173. text = "=> Send SSL data";
  174. break;
  175. }
  176. dump(timebuf, text, output, (unsigned char *) data, size, config->tracetype,
  177. type);
  178. return 0;
  179. }
  180. static void dump(const char *timebuf, const char *text,
  181. FILE *stream, const unsigned char *ptr, size_t size,
  182. trace tracetype, curl_infotype infotype)
  183. {
  184. size_t i;
  185. size_t c;
  186. unsigned int width = 0x10;
  187. if(tracetype == TRACE_ASCII)
  188. /* without the hex output, we can fit more on screen */
  189. width = 0x40;
  190. fprintf(stream, "%s%s, %zu bytes (0x%zx)\n", timebuf, text, size, size);
  191. for(i = 0; i < size; i += width) {
  192. fprintf(stream, "%04zx: ", i);
  193. if(tracetype == TRACE_BIN) {
  194. /* hex not disabled, show it */
  195. for(c = 0; c < width; c++)
  196. if(i + c < size)
  197. fprintf(stream, "%02x ", ptr[i + c]);
  198. else
  199. fputs(" ", stream);
  200. }
  201. for(c = 0; (c < width) && (i + c < size); c++) {
  202. /* check for 0D0A; if found, skip past and start a new line of output */
  203. if((tracetype == TRACE_ASCII) &&
  204. (i + c + 1 < size) && (ptr[i + c] == 0x0D) &&
  205. (ptr[i + c + 1] == 0x0A)) {
  206. i += (c + 2 - width);
  207. break;
  208. }
  209. (void)infotype;
  210. fprintf(stream, "%c", ((ptr[i + c] >= 0x20) && (ptr[i + c] < 0x7F)) ?
  211. ptr[i + c] : UNPRINTABLE_CHAR);
  212. /* check again for 0D0A, to avoid an extra \n if it's at width */
  213. if((tracetype == TRACE_ASCII) &&
  214. (i + c + 2 < size) && (ptr[i + c + 1] == 0x0D) &&
  215. (ptr[i + c + 2] == 0x0A)) {
  216. i += (c + 3 - width);
  217. break;
  218. }
  219. }
  220. fputc('\n', stream); /* newline */
  221. }
  222. fflush(stream);
  223. }