testtrace.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. char *data, size_t size, void *userp)
  77. {
  78. struct libtest_trace_cfg *trace_cfg = userp;
  79. const char *text;
  80. struct timeval tv;
  81. char timebuf[20];
  82. char *timestr;
  83. time_t secs;
  84. (void)handle;
  85. timebuf[0] = '\0';
  86. timestr = &timebuf[0];
  87. if(trace_cfg->tracetime) {
  88. struct tm *now;
  89. tv = tutil_tvnow();
  90. if(!known_offset) {
  91. epoch_offset = time(NULL) - tv.tv_sec;
  92. known_offset = 1;
  93. }
  94. secs = epoch_offset + tv.tv_sec;
  95. now = localtime(&secs); /* not thread safe but we don't care */
  96. msnprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
  97. now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
  98. }
  99. switch(type) {
  100. case CURLINFO_TEXT:
  101. fprintf(stderr, "%s== Info: %s", timestr, (char *)data);
  102. return 0;
  103. case CURLINFO_HEADER_OUT:
  104. text = "=> Send header";
  105. break;
  106. case CURLINFO_DATA_OUT:
  107. text = "=> Send data";
  108. break;
  109. case CURLINFO_SSL_DATA_OUT:
  110. text = "=> Send SSL data";
  111. break;
  112. case CURLINFO_HEADER_IN:
  113. text = "<= Recv header";
  114. break;
  115. case CURLINFO_DATA_IN:
  116. text = "<= Recv data";
  117. break;
  118. case CURLINFO_SSL_DATA_IN:
  119. text = "<= Recv SSL data";
  120. break;
  121. default: /* in case a new one is introduced to shock us */
  122. return 0;
  123. }
  124. libtest_debug_dump(timebuf, text, stderr, (unsigned char *)data, size,
  125. trace_cfg->nohex);
  126. return 0;
  127. }