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