testtrace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.haxx.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, %d bytes (0x%x)\n", timebuf, text,
  40. (int)size, (int)size);
  41. for(i = 0; i < size; i += width) {
  42. fprintf(stream, "%04x: ", (int)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) && (ptr[i+c+1] == 0x0A)) {
  55. i += (c+2-width);
  56. break;
  57. }
  58. fprintf(stream, "%c", ((ptr[i+c] >= 0x20) && (ptr[i+c] < 0x80)) ?
  59. ptr[i+c] : '.');
  60. /* check again for 0D0A, to avoid an extra \n if it's at width */
  61. if(nohex &&
  62. (i+c+2 < size) && (ptr[i+c+1] == 0x0D) && (ptr[i+c+2] == 0x0A)) {
  63. i += (c+3-width);
  64. break;
  65. }
  66. }
  67. fputc('\n', stream); /* newline */
  68. }
  69. fflush(stream);
  70. }
  71. int libtest_debug_cb(CURL *handle, curl_infotype type,
  72. unsigned char *data, size_t size,
  73. void *userp)
  74. {
  75. struct libtest_trace_cfg *trace_cfg = userp;
  76. const char *text;
  77. struct timeval tv;
  78. struct tm *now;
  79. char timebuf[20];
  80. char *timestr;
  81. time_t secs;
  82. (void)handle;
  83. timebuf[0] = '\0';
  84. timestr = &timebuf[0];
  85. if(trace_cfg->tracetime) {
  86. tv = tutil_tvnow();
  87. if(!known_offset) {
  88. epoch_offset = time(NULL) - tv.tv_sec;
  89. known_offset = 1;
  90. }
  91. secs = epoch_offset + tv.tv_sec;
  92. now = localtime(&secs); /* not thread safe but we don't care */
  93. snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
  94. now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
  95. }
  96. switch(type) {
  97. case CURLINFO_TEXT:
  98. fprintf(stderr, "%s== Info: %s", timestr, (char *)data);
  99. /* FALLTHROUGH */
  100. default: /* in case a new one is introduced to shock us */
  101. return 0;
  102. case CURLINFO_HEADER_OUT:
  103. text = "=> Send header";
  104. break;
  105. case CURLINFO_DATA_OUT:
  106. text = "=> Send data";
  107. break;
  108. case CURLINFO_SSL_DATA_OUT:
  109. text = "=> Send SSL data";
  110. break;
  111. case CURLINFO_HEADER_IN:
  112. text = "<= Recv header";
  113. break;
  114. case CURLINFO_DATA_IN:
  115. text = "<= Recv data";
  116. break;
  117. case CURLINFO_SSL_DATA_IN:
  118. text = "<= Recv SSL data";
  119. break;
  120. }
  121. libtest_debug_dump(timebuf, text, stderr, data, size, trace_cfg->nohex);
  122. return 0;
  123. }