2
0

testtrace.c 4.0 KB

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