curl_trc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "curl_trc.h"
  27. #include "urldata.h"
  28. #include "easyif.h"
  29. #include "cfilters.h"
  30. #include "timeval.h"
  31. #include "multiif.h"
  32. #include "strcase.h"
  33. #include "cf-socket.h"
  34. #include "connect.h"
  35. #include "doh.h"
  36. #include "http2.h"
  37. #include "http_proxy.h"
  38. #include "cf-h1-proxy.h"
  39. #include "cf-h2-proxy.h"
  40. #include "cf-haproxy.h"
  41. #include "cf-https-connect.h"
  42. #include "socks.h"
  43. #include "strtok.h"
  44. #include "vtls/vtls.h"
  45. #include "vquic/vquic.h"
  46. /* The last 3 #include files should be in this order */
  47. #include "curl_printf.h"
  48. #include "curl_memory.h"
  49. #include "memdebug.h"
  50. void Curl_debug(struct Curl_easy *data, curl_infotype type,
  51. char *ptr, size_t size)
  52. {
  53. if(data->set.verbose) {
  54. static const char s_infotype[CURLINFO_END][3] = {
  55. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  56. if(data->set.fdebug) {
  57. bool inCallback = Curl_is_in_callback(data);
  58. Curl_set_in_callback(data, true);
  59. (void)(*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  60. Curl_set_in_callback(data, inCallback);
  61. }
  62. else {
  63. switch(type) {
  64. case CURLINFO_TEXT:
  65. case CURLINFO_HEADER_OUT:
  66. case CURLINFO_HEADER_IN:
  67. fwrite(s_infotype[type], 2, 1, data->set.err);
  68. fwrite(ptr, size, 1, data->set.err);
  69. break;
  70. default: /* nada */
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. /* Curl_failf() is for messages stating why we failed.
  77. * The message SHALL NOT include any LF or CR.
  78. */
  79. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  80. {
  81. DEBUGASSERT(!strchr(fmt, '\n'));
  82. if(data->set.verbose || data->set.errorbuffer) {
  83. va_list ap;
  84. int len;
  85. char error[CURL_ERROR_SIZE + 2];
  86. va_start(ap, fmt);
  87. len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  88. if(data->set.errorbuffer && !data->state.errorbuf) {
  89. strcpy(data->set.errorbuffer, error);
  90. data->state.errorbuf = TRUE; /* wrote error string */
  91. }
  92. error[len++] = '\n';
  93. error[len] = '\0';
  94. Curl_debug(data, CURLINFO_TEXT, error, len);
  95. va_end(ap);
  96. }
  97. }
  98. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  99. /* Curl_infof() is for info message along the way */
  100. #define MAXINFO 2048
  101. static void trc_infof(struct Curl_easy *data, struct curl_trc_feat *feat,
  102. const char * const fmt, va_list ap) CURL_PRINTF(3, 0);
  103. static void trc_infof(struct Curl_easy *data, struct curl_trc_feat *feat,
  104. const char * const fmt, va_list ap)
  105. {
  106. int len = 0;
  107. char buffer[MAXINFO + 2];
  108. if(feat)
  109. len = msnprintf(buffer, MAXINFO, "[%s] ", feat->name);
  110. len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
  111. buffer[len++] = '\n';
  112. buffer[len] = '\0';
  113. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  114. }
  115. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  116. {
  117. DEBUGASSERT(!strchr(fmt, '\n'));
  118. if(Curl_trc_is_verbose(data)) {
  119. va_list ap;
  120. va_start(ap, fmt);
  121. trc_infof(data, data->state.feat, fmt, ap);
  122. va_end(ap);
  123. }
  124. }
  125. void Curl_trc_cf_infof(struct Curl_easy *data, struct Curl_cfilter *cf,
  126. const char *fmt, ...)
  127. {
  128. DEBUGASSERT(cf);
  129. if(Curl_trc_cf_is_verbose(cf, data)) {
  130. va_list ap;
  131. int len = 0;
  132. char buffer[MAXINFO + 2];
  133. if(data->state.feat)
  134. len += msnprintf(buffer + len, MAXINFO - len, "[%s] ",
  135. data->state.feat->name);
  136. if(cf->sockindex)
  137. len += msnprintf(buffer + len, MAXINFO - len, "[%s-%d] ",
  138. cf->cft->name, cf->sockindex);
  139. else
  140. len += msnprintf(buffer + len, MAXINFO - len, "[%s] ", cf->cft->name);
  141. va_start(ap, fmt);
  142. len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
  143. va_end(ap);
  144. buffer[len++] = '\n';
  145. buffer[len] = '\0';
  146. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  147. }
  148. }
  149. struct curl_trc_feat Curl_trc_feat_read = {
  150. "READ",
  151. CURL_LOG_LVL_NONE,
  152. };
  153. struct curl_trc_feat Curl_trc_feat_write = {
  154. "WRITE",
  155. CURL_LOG_LVL_NONE,
  156. };
  157. void Curl_trc_read(struct Curl_easy *data, const char *fmt, ...)
  158. {
  159. DEBUGASSERT(!strchr(fmt, '\n'));
  160. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_read)) {
  161. va_list ap;
  162. va_start(ap, fmt);
  163. trc_infof(data, &Curl_trc_feat_read, fmt, ap);
  164. va_end(ap);
  165. }
  166. }
  167. void Curl_trc_write(struct Curl_easy *data, const char *fmt, ...)
  168. {
  169. DEBUGASSERT(!strchr(fmt, '\n'));
  170. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_write)) {
  171. va_list ap;
  172. va_start(ap, fmt);
  173. trc_infof(data, &Curl_trc_feat_write, fmt, ap);
  174. va_end(ap);
  175. }
  176. }
  177. #ifndef CURL_DISABLE_FTP
  178. struct curl_trc_feat Curl_trc_feat_ftp = {
  179. "FTP",
  180. CURL_LOG_LVL_NONE,
  181. };
  182. void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...)
  183. {
  184. DEBUGASSERT(!strchr(fmt, '\n'));
  185. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ftp)) {
  186. va_list ap;
  187. va_start(ap, fmt);
  188. trc_infof(data, &Curl_trc_feat_ftp, fmt, ap);
  189. va_end(ap);
  190. }
  191. }
  192. #endif /* !CURL_DISABLE_FTP */
  193. static struct curl_trc_feat *trc_feats[] = {
  194. &Curl_trc_feat_read,
  195. &Curl_trc_feat_write,
  196. #ifndef CURL_DISABLE_FTP
  197. &Curl_trc_feat_ftp,
  198. #endif
  199. #ifndef CURL_DISABLE_DOH
  200. &Curl_doh_trc,
  201. #endif
  202. NULL,
  203. };
  204. static struct Curl_cftype *cf_types[] = {
  205. &Curl_cft_tcp,
  206. &Curl_cft_udp,
  207. &Curl_cft_unix,
  208. &Curl_cft_tcp_accept,
  209. &Curl_cft_happy_eyeballs,
  210. &Curl_cft_setup,
  211. #ifdef USE_NGHTTP2
  212. &Curl_cft_nghttp2,
  213. #endif
  214. #ifdef USE_SSL
  215. &Curl_cft_ssl,
  216. #ifndef CURL_DISABLE_PROXY
  217. &Curl_cft_ssl_proxy,
  218. #endif
  219. #endif
  220. #if !defined(CURL_DISABLE_PROXY)
  221. #if !defined(CURL_DISABLE_HTTP)
  222. &Curl_cft_h1_proxy,
  223. #ifdef USE_NGHTTP2
  224. &Curl_cft_h2_proxy,
  225. #endif
  226. &Curl_cft_http_proxy,
  227. #endif /* !CURL_DISABLE_HTTP */
  228. &Curl_cft_haproxy,
  229. &Curl_cft_socks_proxy,
  230. #endif /* !CURL_DISABLE_PROXY */
  231. #ifdef USE_HTTP3
  232. &Curl_cft_http3,
  233. #endif
  234. #if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
  235. &Curl_cft_http_connect,
  236. #endif
  237. NULL,
  238. };
  239. CURLcode Curl_trc_opt(const char *config)
  240. {
  241. char *token, *tok_buf, *tmp;
  242. size_t i;
  243. int lvl;
  244. tmp = strdup(config);
  245. if(!tmp)
  246. return CURLE_OUT_OF_MEMORY;
  247. token = strtok_r(tmp, ", ", &tok_buf);
  248. while(token) {
  249. switch(*token) {
  250. case '-':
  251. lvl = CURL_LOG_LVL_NONE;
  252. ++token;
  253. break;
  254. case '+':
  255. lvl = CURL_LOG_LVL_INFO;
  256. ++token;
  257. break;
  258. default:
  259. lvl = CURL_LOG_LVL_INFO;
  260. break;
  261. }
  262. for(i = 0; cf_types[i]; ++i) {
  263. if(strcasecompare(token, "all")) {
  264. cf_types[i]->log_level = lvl;
  265. }
  266. else if(strcasecompare(token, cf_types[i]->name)) {
  267. cf_types[i]->log_level = lvl;
  268. break;
  269. }
  270. }
  271. for(i = 0; trc_feats[i]; ++i) {
  272. if(strcasecompare(token, "all")) {
  273. trc_feats[i]->log_level = lvl;
  274. }
  275. else if(strcasecompare(token, trc_feats[i]->name)) {
  276. trc_feats[i]->log_level = lvl;
  277. break;
  278. }
  279. }
  280. token = strtok_r(NULL, ", ", &tok_buf);
  281. }
  282. free(tmp);
  283. return CURLE_OK;
  284. }
  285. CURLcode Curl_trc_init(void)
  286. {
  287. #ifdef DEBUGBUILD
  288. /* WIP: we use the auto-init from an env var only in DEBUG builds for
  289. * convenience. */
  290. const char *config = getenv("CURL_DEBUG");
  291. if(config) {
  292. return Curl_trc_opt(config);
  293. }
  294. #endif /* DEBUGBUILD */
  295. return CURLE_OK;
  296. }
  297. #else /* defined(CURL_DISABLE_VERBOSE_STRINGS) */
  298. CURLcode Curl_trc_init(void)
  299. {
  300. return CURLE_OK;
  301. }
  302. #endif /* !defined(CURL_DISABLE_VERBOSE_STRINGS) */