summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorotto <>2016-11-04 09:11:20 +0000
committerotto <>2016-11-04 09:11:20 +0000
commit00872265b9546fcf2d5795aa3a120c35142d268b (patch)
treef86afa44c965d8f85cb92b4b2636da7408d446fb /src/lib
parenta8106796715b31d81e2e9fee9883de54a4053ec0 (diff)
downloadopenbsd-00872265b9546fcf2d5795aa3a120c35142d268b.tar.gz
openbsd-00872265b9546fcf2d5795aa3a120c35142d268b.tar.bz2
openbsd-00872265b9546fcf2d5795aa3a120c35142d268b.zip
MALLOC_STATS tweaks, by default not compiled in
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/stdlib/malloc.c42
1 files changed, 29 insertions, 13 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 873db60dd5..a353c933ef 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc.c,v 1.210 2016/11/03 18:51:49 otto Exp $ */ 1/* $OpenBSD: malloc.c,v 1.211 2016/11/04 09:11:20 otto Exp $ */
2/* 2/*
3 * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net>
4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> 4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
@@ -201,7 +201,7 @@ static __dead void wrterror(struct dir_info *d, char *msg, ...)
201 __attribute__((__format__ (printf, 2, 3))); 201 __attribute__((__format__ (printf, 2, 3)));
202 202
203#ifdef MALLOC_STATS 203#ifdef MALLOC_STATS
204void malloc_dump(int, struct dir_info *); 204void malloc_dump(int, int, struct dir_info *);
205PROTO_NORMAL(malloc_dump); 205PROTO_NORMAL(malloc_dump);
206static void malloc_exit(void); 206static void malloc_exit(void);
207#define CALLER __builtin_return_address(0) 207#define CALLER __builtin_return_address(0)
@@ -287,7 +287,7 @@ wrterror(struct dir_info *d, char *msg, ...)
287 int i; 287 int i;
288 288
289 for (i = 0; i < _MALLOC_MUTEXES; i++) 289 for (i = 0; i < _MALLOC_MUTEXES; i++)
290 malloc_dump(STDERR_FILENO, mopts.malloc_pool[i]); 290 malloc_dump(STDERR_FILENO, i, mopts.malloc_pool[i]);
291 } 291 }
292#endif /* MALLOC_STATS */ 292#endif /* MALLOC_STATS */
293 293
@@ -1918,12 +1918,12 @@ dump_free_page_info(int fd, struct dir_info *d)
1918} 1918}
1919 1919
1920static void 1920static void
1921malloc_dump1(int fd, struct dir_info *d) 1921malloc_dump1(int fd, int poolno, struct dir_info *d)
1922{ 1922{
1923 char buf[100]; 1923 char buf[100];
1924 size_t i, realsize; 1924 size_t i, realsize;
1925 1925
1926 snprintf(buf, sizeof(buf), "Malloc dir of %s at %p\n", __progname, d); 1926 snprintf(buf, sizeof(buf), "Malloc dir of %s pool %d at %p\n", __progname, poolno, d);
1927 write(fd, buf, strlen(buf)); 1927 write(fd, buf, strlen(buf));
1928 if (d == NULL) 1928 if (d == NULL)
1929 return; 1929 return;
@@ -1942,6 +1942,10 @@ malloc_dump1(int fd, struct dir_info *d)
1942 snprintf(buf, sizeof(buf), "Cheap reallocs %zu/%zu\n", 1942 snprintf(buf, sizeof(buf), "Cheap reallocs %zu/%zu\n",
1943 d->cheap_reallocs, d->cheap_realloc_tries); 1943 d->cheap_reallocs, d->cheap_realloc_tries);
1944 write(fd, buf, strlen(buf)); 1944 write(fd, buf, strlen(buf));
1945 snprintf(buf, sizeof(buf), "In use %zu\n", d->malloc_used);
1946 write(fd, buf, strlen(buf));
1947 snprintf(buf, sizeof(buf), "Guarded %zu\n", d->malloc_guarded);
1948 write(fd, buf, strlen(buf));
1945 dump_free_chunk_info(fd, d); 1949 dump_free_chunk_info(fd, d);
1946 dump_free_page_info(fd, d); 1950 dump_free_page_info(fd, d);
1947 writestr(fd, 1951 writestr(fd,
@@ -1957,7 +1961,7 @@ malloc_dump1(int fd, struct dir_info *d)
1957 if (realsize > MALLOC_MAXCHUNK) { 1961 if (realsize > MALLOC_MAXCHUNK) {
1958 putleakinfo(d->r[i].f, realsize, 1); 1962 putleakinfo(d->r[i].f, realsize, 1);
1959 snprintf(buf, sizeof(buf), 1963 snprintf(buf, sizeof(buf),
1960 "pages %12p %12p %zu\n", d->r[i].p, 1964 "pages %18p %18p %zu\n", d->r[i].p,
1961 d->r[i].f, realsize); 1965 d->r[i].f, realsize);
1962 write(fd, buf, strlen(buf)); 1966 write(fd, buf, strlen(buf));
1963 } else 1967 } else
@@ -1966,16 +1970,12 @@ malloc_dump1(int fd, struct dir_info *d)
1966 d->r[i].f, 0); 1970 d->r[i].f, 0);
1967 } 1971 }
1968 } 1972 }
1969 snprintf(buf, sizeof(buf), "In use %zu\n", d->malloc_used);
1970 write(fd, buf, strlen(buf));
1971 snprintf(buf, sizeof(buf), "Guarded %zu\n", d->malloc_guarded);
1972 write(fd, buf, strlen(buf));
1973 dump_leaks(fd); 1973 dump_leaks(fd);
1974 write(fd, "\n", 1); 1974 write(fd, "\n", 1);
1975} 1975}
1976 1976
1977void 1977void
1978malloc_dump(int fd, struct dir_info *pool) 1978malloc_dump(int fd, int poolno, struct dir_info *pool)
1979{ 1979{
1980 int i; 1980 int i;
1981 void *p; 1981 void *p;
@@ -1996,7 +1996,7 @@ malloc_dump(int fd, struct dir_info *pool)
1996 } 1996 }
1997 /* XXX leak when run multiple times */ 1997 /* XXX leak when run multiple times */
1998 RB_INIT(&leakhead); 1998 RB_INIT(&leakhead);
1999 malloc_dump1(fd, pool); 1999 malloc_dump1(fd, poolno, pool);
2000 errno = saved_errno; 2000 errno = saved_errno;
2001} 2001}
2002DEF_WEAK(malloc_dump); 2002DEF_WEAK(malloc_dump);
@@ -2006,11 +2006,27 @@ malloc_exit(void)
2006{ 2006{
2007 static const char q[] = "malloc() warning: Couldn't dump stats\n"; 2007 static const char q[] = "malloc() warning: Couldn't dump stats\n";
2008 int save_errno = errno, fd, i; 2008 int save_errno = errno, fd, i;
2009 char buf[100];
2009 2010
2010 fd = open("malloc.out", O_RDWR|O_APPEND); 2011 fd = open("malloc.out", O_RDWR|O_APPEND);
2011 if (fd != -1) { 2012 if (fd != -1) {
2013 snprintf(buf, sizeof(buf), "******** Start dump %s *******\n",
2014 __progname);
2015 write(fd, buf, strlen(buf));
2016 snprintf(buf, sizeof(buf),
2017 "MT=%d F=%d U=%d J=%d R=%d X=%d C=%d cache=%u G=%zu\n",
2018 mopts.malloc_mt, mopts.malloc_freenow,
2019 mopts.malloc_freeunmap, mopts.malloc_junk,
2020 mopts.malloc_realloc, mopts.malloc_xmalloc,
2021 mopts.chunk_canaries, mopts.malloc_cache,
2022 mopts.malloc_guard);
2023 write(fd, buf, strlen(buf));
2024
2012 for (i = 0; i < _MALLOC_MUTEXES; i++) 2025 for (i = 0; i < _MALLOC_MUTEXES; i++)
2013 malloc_dump(fd, mopts.malloc_pool[i]); 2026 malloc_dump(fd, i, mopts.malloc_pool[i]);
2027 snprintf(buf, sizeof(buf), "******** End dump %s *******\n",
2028 __progname);
2029 write(fd, buf, strlen(buf));
2014 close(fd); 2030 close(fd);
2015 } else 2031 } else
2016 write(STDERR_FILENO, q, sizeof(q) - 1); 2032 write(STDERR_FILENO, q, sizeof(q) - 1);