2
0

testtrace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  22. #include "test.h"
  23. #include "testutil.h"
  24. #include "testtrace.h"
  25. #include "memdebug.h"
  26. struct libtest_trace_cfg libtest_debug_config;
  27. static time_t epoch_offset; /* for test time tracing */
  28. static int known_offset; /* for test time tracing */
  29. static
  30. void libtest_debug_dump(const char *timebuf, const char *text, FILE *stream,
  31. const unsigned char *ptr, size_t size, int nohex)
  32. {
  33. size_t i;
  34. size_t c;
  35. unsigned int width = 0x10;
  36. if(nohex)
  37. /* without the hex output, we can fit more on screen */
  38. width = 0x40;
  39. fprintf(stream, "%s%s, %zu bytes (0x%zx)\n", timebuf, text,
  40. size, size);
  41. for(i = 0; i < size; i += width) {
  42. fprintf(stream, "%04zx: ", i);
  43. if(!nohex) {
  44. /* hex not disabled, show it */
  45. for(c = 0; c < width; c++)
  46. if(i + c < size)
  47. fprintf(stream, "%02x ", ptr[i + c]);
  48. else
  49. fputs(" ", stream);
  50. }
  51. for(c = 0; (c < width) && (i + c < size); c++) {
  52. /* check for 0D0A; if found, skip past and start a new line of output */
  53. if(nohex &&
  54. (i + c + 1 < size) && (ptr[i + c] == 0x0D) &&
  55. (ptr[i + c + 1] == 0x0A)) {
  56. i += (c + 2 - width);
  57. break;
  58. }
  59. fprintf(stream, "%c", ((ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80)) ?
  60. ptr[i + c] : '.');
  61. /* check again for 0D0A, to avoid an extra \n if it's at width */
  62. if(nohex &&
  63. (i + c + 2 < size) && (ptr[i + c + 1] == 0x0D) &&
  64. (ptr[i + c + 2] == 0x0A)) {
  65. i += (c + 3 - width);
  66. break;
  67. }
  68. }
  69. fputc('\n', stream); /* newline */
  70. }
  71. fflush(stream);
  72. }
  73. int libtest_debug_cb(CURL *handle, curl_infotype type,
  74. unsigned char *data, size_t size,
  75. void *userp)
  76. {
  77. struct libtest_trace_cfg *trace_cfg = userp;
  78. const char *text;
  79. struct timeval tv;
  80. char timebuf[20];
  81. char *timestr;
  82. time_t secs;
  83. (void)handle;
  84. timebuf[0] = '\0';
  85. timestr = &timebuf[0];
  86. if(trace_cfg->tracetime) {
  87. struct tm *now;
  88. tv = tutil_tvnow();
  89. if(!known_offset) {
  90. epoch_offset = time(NULL) - tv.tv_sec;
  91. known_offset = 1;
  92. }
  93. secs = epoch_offset + tv.tv_sec;
  94. now = localtime(&secs); /* not thread safe but we don't care */
  95. msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
  96. now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
  97. }
  98. switch(type) {
  99. case CURLINFO_TEXT:
  100. fprintf(stderr, "%s== Info: %s", timestr, (char *)data);
  101. /* FALLTHROUGH */
  102. default: /* in case a new one is introduced to shock us */
  103. return 0;
  104. case CURLINFO_HEADER_OUT:
  105. text = "=> Send header";
  106. break;
  107. case CURLINFO_DATA_OUT:
  108. text = "=> Send data";
  109. break;
  110. case CURLINFO_SSL_DATA_OUT:
  111. text = "=> Send SSL data";
  112. break;
  113. case CURLINFO_HEADER_IN:
  114. text = "<= Recv header";
  115. break;
  116. case CURLINFO_DATA_IN:
  117. text = "<= Recv data";
  118. break;
  119. case CURLINFO_SSL_DATA_IN:
  120. text = "<= Recv SSL data";
  121. break;
  122. }
  123. libtest_debug_dump(timebuf, text, stderr, data, size, trace_cfg->nohex);
  124. return 0;
  125. }