From: otto Date: Thu, 22 Jun 2023 11:04:16 +0000 (+0000) Subject: Allow to ask for deeper callers for leak reports using malloc options. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=1be2752a20a36856c3d087c2ce97e64e965d0b1f;p=openbsd Allow to ask for deeper callers for leak reports using malloc options. ok deraadt@ --- diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3 index d8936260510..855f217c276 100644 --- a/lib/libc/stdlib/malloc.3 +++ b/lib/libc/stdlib/malloc.3 @@ -30,9 +30,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: malloc.3,v 1.133 2023/06/04 06:58:33 otto Exp $ +.\" $OpenBSD: malloc.3,v 1.134 2023/06/22 11:04:16 otto Exp $ .\" -.Dd $Mdocdate: June 4 2023 $ +.Dd $Mdocdate: June 22 2023 $ .Dt MALLOC 3 .Os .Sh NAME @@ -294,6 +294,17 @@ To record the dump: To view the leak report: .Pp .Dl $ kdump -u malloc ... +.Pp +By default, the immediate caller of a +.Nm +function will be recorded. +Use malloc options +.Cm 2 +or +.Cm 3 +to record the caller one or two stack frames deeper instead. +These malloc options imply +.Cm D . .It Cm F .Dq Freecheck . Enable more extensive double free and use after free detection. @@ -813,3 +824,14 @@ and .Fn realloc to avoid these problems on .Ox . +.Pp +The mechanism to record caller functions when using malloc options +.Cm 2 +or +.Cm 3 +is not guaranteed to work for all platforms, compilers or compilation +options, +and might even crash your program. +Use +.Em only +for debugging purposes. diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index 04f211c4eff..7bc660525bd 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: malloc.c,v 1.286 2023/06/07 12:56:22 aoyama Exp $ */ +/* $OpenBSD: malloc.c,v 1.287 2023/06/22 11:04:16 otto Exp $ */ /* * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek * Copyright (c) 2012 Matthew Dempsky @@ -255,7 +255,11 @@ void malloc_dump(void); PROTO_NORMAL(malloc_dump); static void malloc_exit(void); #endif -#define CALLER (DO_STATS ? __builtin_return_address(0) : NULL) +#define CALLER ( \ + DO_STATS == 0 ? NULL : (__builtin_extract_return_addr( \ + DO_STATS == 1 ? __builtin_return_address(0) : \ + DO_STATS == 2 ? __builtin_return_address(1) : \ + DO_STATS == 3 ? __builtin_return_address(2) : NULL))) /* low bits of r->p determine size: 0 means >= page size and r->size holding * real size, otherwise low bits is the bucket + 1 @@ -365,8 +369,15 @@ omalloc_parseopt(char opt) mopts.malloc_stats = 0; break; case 'D': + case '1': mopts.malloc_stats = 1; break; + case '2': + mopts.malloc_stats = 2; + break; + case '3': + mopts.malloc_stats = 3; + break; #endif /* MALLOC_STATS */ case 'f': mopts.malloc_freecheck = 0;