2
0

curl_trc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #ifndef ARRAYSIZE
  51. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  52. #endif
  53. void Curl_debug(struct Curl_easy *data, curl_infotype type,
  54. char *ptr, size_t size)
  55. {
  56. if(data->set.verbose) {
  57. static const char s_infotype[CURLINFO_END][3] = {
  58. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  59. if(data->set.fdebug) {
  60. bool inCallback = Curl_is_in_callback(data);
  61. Curl_set_in_callback(data, TRUE);
  62. (void)(*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  63. Curl_set_in_callback(data, inCallback);
  64. }
  65. else {
  66. switch(type) {
  67. case CURLINFO_TEXT:
  68. case CURLINFO_HEADER_OUT:
  69. case CURLINFO_HEADER_IN:
  70. fwrite(s_infotype[type], 2, 1, data->set.err);
  71. fwrite(ptr, size, 1, data->set.err);
  72. break;
  73. default: /* nada */
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. /* Curl_failf() is for messages stating why we failed.
  80. * The message SHALL NOT include any LF or CR.
  81. */
  82. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  83. {
  84. DEBUGASSERT(!strchr(fmt, '\n'));
  85. if(data->set.verbose || data->set.errorbuffer) {
  86. va_list ap;
  87. int len;
  88. char error[CURL_ERROR_SIZE + 2];
  89. va_start(ap, fmt);
  90. len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  91. if(data->set.errorbuffer && !data->state.errorbuf) {
  92. strcpy(data->set.errorbuffer, error);
  93. data->state.errorbuf = TRUE; /* wrote error string */
  94. }
  95. error[len++] = '\n';
  96. error[len] = '\0';
  97. Curl_debug(data, CURLINFO_TEXT, error, len);
  98. va_end(ap);
  99. }
  100. }
  101. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  102. /* Curl_infof() is for info message along the way */
  103. #define MAXINFO 2048
  104. static void trc_infof(struct Curl_easy *data, struct curl_trc_feat *feat,
  105. const char * const fmt, va_list ap) CURL_PRINTF(3, 0);
  106. static void trc_infof(struct Curl_easy *data, struct curl_trc_feat *feat,
  107. const char * const fmt, va_list ap)
  108. {
  109. int len = 0;
  110. char buffer[MAXINFO + 5];
  111. if(feat)
  112. len = msnprintf(buffer, (MAXINFO + 1), "[%s] ", feat->name);
  113. len += mvsnprintf(buffer + len, (MAXINFO + 1) - len, fmt, ap);
  114. if(len >= MAXINFO) { /* too long, shorten with '...' */
  115. --len;
  116. buffer[len++] = '.';
  117. buffer[len++] = '.';
  118. buffer[len++] = '.';
  119. }
  120. buffer[len++] = '\n';
  121. buffer[len] = '\0';
  122. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  123. }
  124. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  125. {
  126. DEBUGASSERT(!strchr(fmt, '\n'));
  127. if(Curl_trc_is_verbose(data)) {
  128. va_list ap;
  129. va_start(ap, fmt);
  130. trc_infof(data, data->state.feat, fmt, ap);
  131. va_end(ap);
  132. }
  133. }
  134. void Curl_trc_cf_infof(struct Curl_easy *data, struct Curl_cfilter *cf,
  135. const char *fmt, ...)
  136. {
  137. DEBUGASSERT(cf);
  138. if(Curl_trc_cf_is_verbose(cf, data)) {
  139. va_list ap;
  140. int len = 0;
  141. char buffer[MAXINFO + 2];
  142. if(data->state.feat)
  143. len += msnprintf(buffer + len, MAXINFO - len, "[%s] ",
  144. data->state.feat->name);
  145. if(cf->sockindex)
  146. len += msnprintf(buffer + len, MAXINFO - len, "[%s-%d] ",
  147. cf->cft->name, cf->sockindex);
  148. else
  149. len += msnprintf(buffer + len, MAXINFO - len, "[%s] ", cf->cft->name);
  150. va_start(ap, fmt);
  151. len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
  152. va_end(ap);
  153. buffer[len++] = '\n';
  154. buffer[len] = '\0';
  155. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  156. }
  157. }
  158. struct curl_trc_feat Curl_trc_feat_read = {
  159. "READ",
  160. CURL_LOG_LVL_NONE,
  161. };
  162. struct curl_trc_feat Curl_trc_feat_write = {
  163. "WRITE",
  164. CURL_LOG_LVL_NONE,
  165. };
  166. void Curl_trc_read(struct Curl_easy *data, const char *fmt, ...)
  167. {
  168. DEBUGASSERT(!strchr(fmt, '\n'));
  169. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_read)) {
  170. va_list ap;
  171. va_start(ap, fmt);
  172. trc_infof(data, &Curl_trc_feat_read, fmt, ap);
  173. va_end(ap);
  174. }
  175. }
  176. void Curl_trc_write(struct Curl_easy *data, const char *fmt, ...)
  177. {
  178. DEBUGASSERT(!strchr(fmt, '\n'));
  179. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_write)) {
  180. va_list ap;
  181. va_start(ap, fmt);
  182. trc_infof(data, &Curl_trc_feat_write, fmt, ap);
  183. va_end(ap);
  184. }
  185. }
  186. #ifndef CURL_DISABLE_FTP
  187. struct curl_trc_feat Curl_trc_feat_ftp = {
  188. "FTP",
  189. CURL_LOG_LVL_NONE,
  190. };
  191. void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...)
  192. {
  193. DEBUGASSERT(!strchr(fmt, '\n'));
  194. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ftp)) {
  195. va_list ap;
  196. va_start(ap, fmt);
  197. trc_infof(data, &Curl_trc_feat_ftp, fmt, ap);
  198. va_end(ap);
  199. }
  200. }
  201. #endif /* !CURL_DISABLE_FTP */
  202. #ifndef CURL_DISABLE_SMTP
  203. struct curl_trc_feat Curl_trc_feat_smtp = {
  204. "SMTP",
  205. CURL_LOG_LVL_NONE,
  206. };
  207. void Curl_trc_smtp(struct Curl_easy *data, const char *fmt, ...)
  208. {
  209. DEBUGASSERT(!strchr(fmt, '\n'));
  210. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_smtp)) {
  211. va_list ap;
  212. va_start(ap, fmt);
  213. trc_infof(data, &Curl_trc_feat_smtp, fmt, ap);
  214. va_end(ap);
  215. }
  216. }
  217. #endif /* !CURL_DISABLE_SMTP */
  218. #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  219. struct curl_trc_feat Curl_trc_feat_ws = {
  220. "WS",
  221. CURL_LOG_LVL_NONE,
  222. };
  223. void Curl_trc_ws(struct Curl_easy *data, const char *fmt, ...)
  224. {
  225. DEBUGASSERT(!strchr(fmt, '\n'));
  226. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ws)) {
  227. va_list ap;
  228. va_start(ap, fmt);
  229. trc_infof(data, &Curl_trc_feat_ws, fmt, ap);
  230. va_end(ap);
  231. }
  232. }
  233. #endif /* !CURL_DISABLE_WEBSOCKETS && !CURL_DISABLE_HTTP */
  234. #define TRC_CT_NONE (0)
  235. #define TRC_CT_PROTOCOL (1<<(0))
  236. #define TRC_CT_NETWORK (1<<(1))
  237. #define TRC_CT_PROXY (1<<(2))
  238. struct trc_feat_def {
  239. struct curl_trc_feat *feat;
  240. unsigned int category;
  241. };
  242. static struct trc_feat_def trc_feats[] = {
  243. { &Curl_trc_feat_read, TRC_CT_NONE },
  244. { &Curl_trc_feat_write, TRC_CT_NONE },
  245. #ifndef CURL_DISABLE_FTP
  246. { &Curl_trc_feat_ftp, TRC_CT_PROTOCOL },
  247. #endif
  248. #ifndef CURL_DISABLE_DOH
  249. { &Curl_doh_trc, TRC_CT_NETWORK },
  250. #endif
  251. #ifndef CURL_DISABLE_SMTP
  252. { &Curl_trc_feat_smtp, TRC_CT_PROTOCOL },
  253. #endif
  254. #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  255. { &Curl_trc_feat_ws, TRC_CT_PROTOCOL },
  256. #endif
  257. };
  258. struct trc_cft_def {
  259. struct Curl_cftype *cft;
  260. unsigned int category;
  261. };
  262. static struct trc_cft_def trc_cfts[] = {
  263. { &Curl_cft_tcp, TRC_CT_NETWORK },
  264. { &Curl_cft_udp, TRC_CT_NETWORK },
  265. { &Curl_cft_unix, TRC_CT_NETWORK },
  266. { &Curl_cft_tcp_accept, TRC_CT_NETWORK },
  267. { &Curl_cft_happy_eyeballs, TRC_CT_NETWORK },
  268. { &Curl_cft_setup, TRC_CT_PROTOCOL },
  269. #ifdef USE_NGHTTP2
  270. { &Curl_cft_nghttp2, TRC_CT_PROTOCOL },
  271. #endif
  272. #ifdef USE_SSL
  273. { &Curl_cft_ssl, TRC_CT_NETWORK },
  274. #ifndef CURL_DISABLE_PROXY
  275. { &Curl_cft_ssl_proxy, TRC_CT_PROXY },
  276. #endif
  277. #endif
  278. #if !defined(CURL_DISABLE_PROXY)
  279. #if !defined(CURL_DISABLE_HTTP)
  280. { &Curl_cft_h1_proxy, TRC_CT_PROXY },
  281. #ifdef USE_NGHTTP2
  282. { &Curl_cft_h2_proxy, TRC_CT_PROXY },
  283. #endif
  284. { &Curl_cft_http_proxy, TRC_CT_PROXY },
  285. #endif /* !CURL_DISABLE_HTTP */
  286. { &Curl_cft_haproxy, TRC_CT_PROXY },
  287. { &Curl_cft_socks_proxy, TRC_CT_PROXY },
  288. #endif /* !CURL_DISABLE_PROXY */
  289. #ifdef USE_HTTP3
  290. { &Curl_cft_http3, TRC_CT_PROTOCOL },
  291. #endif
  292. #if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
  293. { &Curl_cft_http_connect, TRC_CT_PROTOCOL },
  294. #endif
  295. };
  296. static void trc_apply_level_by_name(const char * const token, int lvl)
  297. {
  298. size_t i;
  299. for(i = 0; i < ARRAYSIZE(trc_cfts); ++i) {
  300. if(strcasecompare(token, trc_cfts[i].cft->name)) {
  301. trc_cfts[i].cft->log_level = lvl;
  302. break;
  303. }
  304. }
  305. for(i = 0; i < ARRAYSIZE(trc_feats); ++i) {
  306. if(strcasecompare(token, trc_feats[i].feat->name)) {
  307. trc_feats[i].feat->log_level = lvl;
  308. break;
  309. }
  310. }
  311. }
  312. static void trc_apply_level_by_category(int category, int lvl)
  313. {
  314. size_t i;
  315. for(i = 0; i < ARRAYSIZE(trc_cfts); ++i) {
  316. if(!category || (trc_cfts[i].category & category))
  317. trc_cfts[i].cft->log_level = lvl;
  318. }
  319. for(i = 0; i < ARRAYSIZE(trc_feats); ++i) {
  320. if(!category || (trc_feats[i].category & category))
  321. trc_feats[i].feat->log_level = lvl;
  322. }
  323. }
  324. static CURLcode trc_opt(const char *config)
  325. {
  326. char *token, *tok_buf, *tmp;
  327. int lvl;
  328. tmp = strdup(config);
  329. if(!tmp)
  330. return CURLE_OUT_OF_MEMORY;
  331. token = strtok_r(tmp, ", ", &tok_buf);
  332. while(token) {
  333. switch(*token) {
  334. case '-':
  335. lvl = CURL_LOG_LVL_NONE;
  336. ++token;
  337. break;
  338. case '+':
  339. lvl = CURL_LOG_LVL_INFO;
  340. ++token;
  341. break;
  342. default:
  343. lvl = CURL_LOG_LVL_INFO;
  344. break;
  345. }
  346. if(strcasecompare(token, "all"))
  347. trc_apply_level_by_category(TRC_CT_NONE, lvl);
  348. else if(strcasecompare(token, "protocol"))
  349. trc_apply_level_by_category(TRC_CT_PROTOCOL, lvl);
  350. else if(strcasecompare(token, "network"))
  351. trc_apply_level_by_category(TRC_CT_NETWORK, lvl);
  352. else if(strcasecompare(token, "proxy"))
  353. trc_apply_level_by_category(TRC_CT_PROXY, lvl);
  354. else
  355. trc_apply_level_by_name(token, lvl);
  356. token = strtok_r(NULL, ", ", &tok_buf);
  357. }
  358. free(tmp);
  359. return CURLE_OK;
  360. }
  361. CURLcode Curl_trc_opt(const char *config)
  362. {
  363. CURLcode result = config ? trc_opt(config) : CURLE_OK;
  364. #ifdef DEBUGBUILD
  365. /* CURL_DEBUG can override anything */
  366. if(!result) {
  367. const char *dbg_config = getenv("CURL_DEBUG");
  368. if(dbg_config)
  369. result = trc_opt(dbg_config);
  370. }
  371. #endif /* DEBUGBUILD */
  372. return result;
  373. }
  374. CURLcode Curl_trc_init(void)
  375. {
  376. #ifdef DEBUGBUILD
  377. return Curl_trc_opt(NULL);
  378. #else
  379. return CURLE_OK;
  380. #endif
  381. }
  382. #else /* defined(CURL_DISABLE_VERBOSE_STRINGS) */
  383. CURLcode Curl_trc_init(void)
  384. {
  385. return CURLE_OK;
  386. }
  387. #endif /* !defined(CURL_DISABLE_VERBOSE_STRINGS) */