.\" 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
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.
.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.
-/* $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 <otto@drijf.net>
* Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
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
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;