master
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"
26#ifdef CURL_MEMDEBUG
28#include <stddef.h> /* for offsetof() */
30#include "urldata.h"
31#include "curl_threads.h"
32#include "curlx/win32-fopen.h" /* for CURLX_FOPEN_*() */
34#ifdef USE_BACKTRACE
35#include <backtrace.h>
36#endif
38struct memdebug {
39 size_t size;
40 union {
41 curl_off_t o;
42 double d;
43 void *p;
44 } mem[1];
45 /* I am hoping this is the thing with the strictest alignment
46 * requirements. That also means we waste some space :-( */
47};
49/*
50 * Note that these debug functions are simple and they are meant to remain so.
51 * For advanced analysis, record a log file and write perl scripts to analyze
52 * them!
53 *
54 * Do not use these with multi-threaded test programs!
55 */
57FILE *curl_dbg_logfile = NULL;
58static bool registered_cleanup = FALSE; /* atexit registered cleanup */
59static bool memlimit = FALSE; /* enable memory limit */
60static long memsize = 0; /* set number of mallocs allowed */
61#ifdef USE_BACKTRACE
62static struct backtrace_state *btstate;
63#endif
65static char membuf[10000];
66static size_t memwidx = 0; /* write index */
68#ifdef USE_MUTEX
69static bool dbg_mutex_init = 0;
70static curl_mutex_t dbg_mutex;
71#endif
73static bool curl_dbg_lock(void)
74{
75#ifdef USE_MUTEX
76 if(dbg_mutex_init) {
77 Curl_mutex_acquire(&dbg_mutex);
78 return TRUE;
79 }
80#endif
81 return FALSE;
82}
84static void curl_dbg_unlock(bool was_locked)
85{
86#ifdef USE_MUTEX
87 if(was_locked)
88 Curl_mutex_release(&dbg_mutex);
89#else
90 (void)was_locked;
91#endif
92}
94static void curl_dbg_log_locked(const char *format, ...) CURL_PRINTF(1, 2);
96/* LeakSantizier (LSAN) calls _exit() instead of exit() when a leak is detected
97 on exit so the logfile must be closed explicitly or data could be lost.
98 Though _exit() does not call atexit handlers such as this, LSAN's call to
99 _exit() comes after the atexit handlers are called. curl/curl#6620 */
100static void curl_dbg_cleanup(void)
101{
102 bool locked = curl_dbg_lock();
103 if(curl_dbg_logfile &&
104 curl_dbg_logfile != stderr &&
105 curl_dbg_logfile != stdout) {
106 if(memwidx)
107 fwrite(membuf, 1, memwidx, curl_dbg_logfile);
108 /* !checksrc! disable BANNEDFUNC 1 */
109 fclose(curl_dbg_logfile);
110 }
111 curl_dbg_logfile = NULL;
112 curl_dbg_unlock(locked);
113#ifdef USE_MUTEX
114 if(dbg_mutex_init) {
115 Curl_mutex_destroy(&dbg_mutex);
116 dbg_mutex_init = FALSE;
117 }
118#endif
119}
121#ifdef USE_BACKTRACE
122static void error_bt_callback(void *data, const char *message,
123 int error_number)
124{
125 (void)data;
126 if(error_number == -1)
127 curl_dbg_log("compile with -g\n\n");
128 else
129 curl_dbg_log("Backtrace error %d: %s\n", error_number, message);
130}
132static int full_callback(void *data, uintptr_t pc, const char *pathname,
133 int line_number, const char *function)
134{
135 (void)data;
136 (void)pc;
137 if(pathname || function || line_number)
138 curl_dbg_log("BT %s:%d -- %s\n", pathname, line_number, function);
139 return 0;
140}
142static void dump_bt(void)
143{
144 backtrace_full(btstate, 0, full_callback, error_bt_callback, NULL);
145}
146#else
147#define dump_bt() /* nothing to do */
148#endif
150/* this sets the log filename */
151void curl_dbg_memdebug(const char *logname)
152{
153 if(!curl_dbg_logfile) {
154 if(logname && *logname)
155 curl_dbg_logfile = CURLX_FOPEN_LOW(logname, FOPEN_WRITETEXT);
156#ifdef MEMDEBUG_LOG_SYNC
157 /* Flush the log file after every line so the log is not lost in a crash */
158 if(curl_dbg_logfile)
159 setbuf(curl_dbg_logfile, (char *)NULL);
160#endif
161 }
162#ifdef USE_MUTEX
163 if(!dbg_mutex_init) {
164 dbg_mutex_init = TRUE;
165 Curl_mutex_init(&dbg_mutex);
166 }
167#endif
168#ifdef USE_BACKTRACE
169 btstate = backtrace_create_state(NULL, 0, error_bt_callback, NULL);
170#endif
171 if(!registered_cleanup)
172 registered_cleanup = !atexit(curl_dbg_cleanup);
173}
175/* This function sets the number of malloc() calls that should return
176 successfully! */
177void curl_dbg_memlimit(long limit)
178{
179 if(!memlimit) {
180 memlimit = TRUE;
181 memsize = limit;
182 }
183}
185/* returns TRUE if this is not allowed! */
186static bool countcheck(const char *func, int line, const char *source)
187{
188 /* if source is NULL, then the call is made internally and this check
189 should not be made */
190 if(memlimit && source) {
191 if(!memsize) {
192 /* log to file */
193 curl_dbg_log("LIMIT %s:%d %s reached memlimit\n", source, line, func);
194 /* log to stderr also */
195 curl_mfprintf(stderr, "LIMIT %s:%d %s reached memlimit\n",
196 source, line, func);
197 dump_bt();
198 fflush(curl_dbg_logfile); /* because it might crash now */
199 /* !checksrc! disable ERRNOVAR 1 */
200 errno = ENOMEM;
201 return TRUE; /* RETURN ERROR! */
202 }
203 else
204 memsize--; /* countdown */
205 }
207 return FALSE; /* allow this */
208}
210ALLOC_FUNC
211void *curl_dbg_malloc(size_t wantedsize, int line, const char *source)
212{
213 struct memdebug *mem;
214 size_t size;
216 DEBUGASSERT(wantedsize != 0);
218 if(countcheck("malloc", line, source))
219 return NULL;
221 /* alloc at least 64 bytes */
222 size = sizeof(struct memdebug) + wantedsize;
224 mem = Curl_cmalloc(size);
225 if(mem) {
226 mem->size = wantedsize;
227 }
229 if(source)
230 curl_dbg_log("MEM %s:%d malloc(%zu) = %p\n",
231 source, line, wantedsize,
232 mem ? (void *)mem->mem : (void *)0);
234 return mem ? mem->mem : NULL;
235}
237ALLOC_FUNC
238void *curl_dbg_calloc(size_t wanted_elements, size_t wanted_size,
239 int line, const char *source)
240{
241 struct memdebug *mem;
242 size_t size, user_size;
244 DEBUGASSERT(wanted_elements != 0);
245 DEBUGASSERT(wanted_size != 0);
247 if(countcheck("calloc", line, source))
248 return NULL;
250 /* alloc at least 64 bytes */
251 user_size = wanted_size * wanted_elements;
252 size = sizeof(struct memdebug) + user_size;
254 mem = Curl_ccalloc(1, size);
255 if(mem)
256 mem->size = user_size;
258 if(source)
259 curl_dbg_log("MEM %s:%d calloc(%zu,%zu) = %p\n",
260 source, line, wanted_elements, wanted_size,
261 mem ? (void *)mem->mem : (void *)0);
263 return mem ? mem->mem : NULL;
264}
266ALLOC_FUNC
267char *curl_dbg_strdup(const char *str, int line, const char *source)
268{
269 char *mem;
270 size_t len;
272 DEBUGASSERT(str);
274 if(countcheck("strdup", line, source))
275 return NULL;
277 len = strlen(str) + 1;
279 mem = curl_dbg_malloc(len, 0, NULL); /* NULL prevents logging */
280 if(mem)
281 memcpy(mem, str, len);
283 if(source)
284 curl_dbg_log("MEM %s:%d strdup(%p) (%zu) = %p\n",
285 source, line, (const void *)str, len, (const void *)mem);
287 return mem;
288}
290#if defined(_WIN32) && defined(UNICODE)
291ALLOC_FUNC
292wchar_t *curl_dbg_wcsdup(const wchar_t *str, int line, const char *source)
293{
294 wchar_t *mem;
295 size_t wsiz, bsiz;
297 DEBUGASSERT(str);
299 if(countcheck("wcsdup", line, source))
300 return NULL;
302 wsiz = wcslen(str) + 1;
303 bsiz = wsiz * sizeof(wchar_t);
305 mem = curl_dbg_malloc(bsiz, 0, NULL); /* NULL prevents logging */
306 if(mem)
307 memcpy(mem, str, bsiz);
309 if(source)
310 curl_dbg_log("MEM %s:%d wcsdup(%p) (%zu) = %p\n",
311 source, line, (const void *)str, bsiz, (void *)mem);
313 return mem;
314}
315#endif
317/* We provide a realloc() that accepts a NULL as pointer, which then
318 performs a malloc(). In order to work with ares. */
319void *curl_dbg_realloc(void *ptr, size_t wantedsize,
320 int line, const char *source)
321{
322 struct memdebug *mem = NULL;
323 bool was_locked;
325 size_t size = sizeof(struct memdebug) + wantedsize;
327 DEBUGASSERT(wantedsize != 0);
329 if(countcheck("realloc", line, source))
330 return NULL;
332 /* need to realloc under lock, as we get out-of-order log
333 * entries otherwise, since another thread might alloc the
334 * memory released by realloc() before otherwise would log it. */
335 was_locked = curl_dbg_lock();
336#ifdef __INTEL_COMPILER
337# pragma warning(push)
338# pragma warning(disable:1684)
339 /* 1684: conversion from pointer to same-sized integral type */
340#endif
342 if(ptr)
343 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
345#ifdef __INTEL_COMPILER
346# pragma warning(pop)
347#endif
349 mem = Curl_crealloc(mem, size);
350 if(source)
351 curl_dbg_log_locked("MEM %s:%d realloc(%p, %zu) = %p\n",
352 source, line, (void *)ptr, wantedsize,
353 mem ? (void *)mem->mem : (void *)0);
355 curl_dbg_unlock(was_locked);
356 if(mem) {
357 mem->size = wantedsize;
358 return mem->mem;
359 }
361 return NULL;
362}
364void curl_dbg_free(void *ptr, int line, const char *source)
365{
366 if(ptr) {
367 struct memdebug *mem;
369 if(source)
370 curl_dbg_log("MEM %s:%d free(%p)\n", source, line, (void *)ptr);
372#ifdef __INTEL_COMPILER
373# pragma warning(push)
374# pragma warning(disable:1684)
375 /* 1684: conversion from pointer to same-sized integral type */
376#endif
378 mem = (void *)((char *)ptr - offsetof(struct memdebug, mem));
380#ifdef __INTEL_COMPILER
381# pragma warning(pop)
382#endif
384 /* free for real */
385 Curl_cfree(mem);
386 }
387}
389curl_socket_t curl_dbg_socket(int domain, int type, int protocol,
390 int line, const char *source)
391{
392 curl_socket_t sockfd;
394 if(countcheck("socket", line, source))
395 return CURL_SOCKET_BAD;
397 /* !checksrc! disable BANNEDFUNC 1 */
398 sockfd = socket(domain, type, protocol);
400 if(source && (sockfd != CURL_SOCKET_BAD))
401 curl_dbg_log("FD %s:%d socket() = %" FMT_SOCKET_T "\n",
402 source, line, sockfd);
404 return sockfd;
405}
407#ifdef HAVE_SOCKETPAIR
408int curl_dbg_socketpair(int domain, int type, int protocol,
409 curl_socket_t socket_vector[2],
410 int line, const char *source)
411{
412 /* !checksrc! disable BANNEDFUNC 1 */
413 int res = socketpair(domain, type, protocol, socket_vector);
415 if(source && (res == 0))
416 curl_dbg_log("FD %s:%d socketpair() = "
417 "%" FMT_SOCKET_T " %" FMT_SOCKET_T "\n",
418 source, line, socket_vector[0], socket_vector[1]);
420 return res;
421}
422#endif
424curl_socket_t curl_dbg_accept(curl_socket_t s, void *saddr, void *saddrlen,
425 int line, const char *source)
426{
427 struct sockaddr *addr = (struct sockaddr *)saddr;
428 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
430 /* !checksrc! disable BANNEDFUNC 1 */
431 curl_socket_t sockfd = accept(s, addr, addrlen);
433 if(source && (sockfd != CURL_SOCKET_BAD))
434 curl_dbg_log("FD %s:%d accept() = %" FMT_SOCKET_T "\n",
435 source, line, sockfd);
437 return sockfd;
438}
440#ifdef HAVE_ACCEPT4
441curl_socket_t curl_dbg_accept4(curl_socket_t s, void *saddr, void *saddrlen,
442 int flags,
443 int line, const char *source)
444{
445 struct sockaddr *addr = (struct sockaddr *)saddr;
446 curl_socklen_t *addrlen = (curl_socklen_t *)saddrlen;
448 /* !checksrc! disable BANNEDFUNC 1 */
449 curl_socket_t sockfd = accept4(s, addr, addrlen, flags);
451 if(source && (sockfd != CURL_SOCKET_BAD))
452 curl_dbg_log("FD %s:%d accept() = %" FMT_SOCKET_T "\n",
453 source, line, sockfd);
455 return sockfd;
456}
457#endif
459/* separate function to allow libcurl to mark a "faked" close */
460void curl_dbg_mark_sclose(curl_socket_t sockfd, int line, const char *source)
461{
462 if(source)
463 curl_dbg_log("FD %s:%d sclose(%" FMT_SOCKET_T ")\n",
464 source, line, sockfd);
465}
467/* this is our own defined way to close sockets on *ALL* platforms */
468int curl_dbg_sclose(curl_socket_t sockfd, int line, const char *source)
469{
470 curl_dbg_mark_sclose(sockfd, line, source);
471 return CURL_SCLOSE(sockfd);
472}
474ALLOC_FUNC
475FILE *curl_dbg_fopen(const char *file, const char *mode,
476 int line, const char *source)
477{
478 FILE *res = CURLX_FOPEN_LOW(file, mode);
479 if(source)
480 curl_dbg_log("FILE %s:%d fopen(\"%s\",\"%s\") = %p\n",
481 source, line, file, mode, (void *)res);
483 return res;
484}
486ALLOC_FUNC
487FILE *curl_dbg_freopen(const char *file, const char *mode, FILE *fh,
488 int line, const char *source)
489{
490 FILE *res = CURLX_FREOPEN_LOW(file, mode, fh);
491 if(source)
492 curl_dbg_log("FILE %s:%d freopen(\"%s\",\"%s\",%p) = %p\n",
493 source, line, file, mode, (void *)fh, (void *)res);
495 return res;
496}
498ALLOC_FUNC
499FILE *curl_dbg_fdopen(int filedes, const char *mode,
500 int line, const char *source)
501{
502 FILE *res = CURLX_FDOPEN_LOW(filedes, mode);
503 if(source)
504 curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
505 source, line, filedes, mode, (void *)res);
506 return res;
507}
509int curl_dbg_fclose(FILE *file, int line, const char *source)
510{
511 int res;
513 DEBUGASSERT(file);
515 if(source)
516 curl_dbg_log("FILE %s:%d fclose(%p)\n", source, line, (void *)file);
518 /* !checksrc! disable BANNEDFUNC 1 */
519 res = fclose(file);
521 return res;
522}
524static void curl_dbg_vlog(const char * const fmt,
525 va_list ap) CURL_PRINTF(1, 0);
527static void curl_dbg_vlog(const char * const fmt, va_list ap)
528{
529 char buf[1024];
530 size_t nchars = curl_mvsnprintf(buf, sizeof(buf), fmt, ap);
532 if(nchars > (int)sizeof(buf) - 1)
533 nchars = (int)sizeof(buf) - 1;
535 if(nchars > 0) {
536 if(sizeof(membuf) - nchars < memwidx) {
537 /* flush */
538 fwrite(membuf, 1, memwidx, curl_dbg_logfile);
539 fflush(curl_dbg_logfile);
540 memwidx = 0;
541 }
542 if(memwidx) {
543 /* the previous line ends with a newline */
544 DEBUGASSERT(membuf[memwidx - 1] == '\n');
545 }
546 memcpy(&membuf[memwidx], buf, nchars);
547 memwidx += nchars;
548 }
549}
551static void curl_dbg_log_locked(const char *format, ...)
552{
553 va_list ap;
555 if(!curl_dbg_logfile)
556 return;
558 va_start(ap, format);
559 curl_dbg_vlog(format, ap);
560 va_end(ap);
561}
563/* this does the writing to the memory tracking log file */
564void curl_dbg_log(const char *format, ...)
565{
566 bool was_locked;
567 va_list ap;
569 if(!curl_dbg_logfile)
570 return;
572 was_locked = curl_dbg_lock();
573 va_start(ap, format);
574 curl_dbg_vlog(format, ap);
575 va_end(ap);
576 curl_dbg_unlock(was_locked);
577}
579#endif /* CURL_MEMDEBUG */
Diagnostics4 git calls · 38 ms
CallStatusSizeTime038 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.5 KiB · hit33 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=33 git.response.bytes=4560 git.response.cache=HIT git.response.duration_ms=33 git.fetch.wants=53c85291e09cb6c84e7e0cd37615de1e236ea2b6 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4560 git.pkt.payload.max=4539 git.sideband.band1.bytes=4538 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e18f751c405e37
- Request
- cf30c74e…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format