blob: 93b9b1ea1c9d40f637569c238c89331e32d8dc6c [file] [log] [blame] [view]
Christopher Ferris713a8e32016-03-18 14:29:51 -07001Malloc Debug
2============
3
4Malloc debug is a method of debugging native memory problems. It can help
5detect memory corruption, memory leaks, and use after free issues.
6
Christopher Ferriseab48032016-05-25 13:04:29 -07007This documentation describes how to enable this feature on Android N or later
Elliott Hughes7dc7d2b2018-04-06 14:44:49 -07008versions of the Android OS. (See the "Examples" section.)
Christopher Ferriseab48032016-05-25 13:04:29 -07009
10The documentation for malloc debug on older versions of Android is
11[here](README_marshmallow_and_earlier.md).
12
Christopher Ferriseab48032016-05-25 13:04:29 -070013When malloc debug is enabled, it works by adding a shim layer that replaces
14the normal allocation calls. The replaced calls are:
Christopher Ferris713a8e32016-03-18 14:29:51 -070015
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070016* `malloc`
17* `free`
18* `calloc`
19* `realloc`
20* `posix_memalign`
21* `memalign`
Christopher Ferriscae21a92018-02-05 18:14:55 -080022* `aligned_alloc`
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070023* `malloc_usable_size`
Christopher Ferris713a8e32016-03-18 14:29:51 -070024
25On 32 bit systems, these two deprecated functions are also replaced:
26
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070027* `pvalloc`
28* `valloc`
Christopher Ferris713a8e32016-03-18 14:29:51 -070029
30Any errors detected by the library are reported in the log.
31
Christopher Ferris4da25032018-03-07 13:38:48 -080032NOTE: There is a small behavioral change beginning in P for realloc.
33Before, a realloc from one size to a smaller size would not update the
34backtrace related to the allocation. Starting in P, every single realloc
35call changes the backtrace for the pointer no matter whether the pointer
36returned has changed or not.
37
38
Christopher Ferris713a8e32016-03-18 14:29:51 -070039Controlling Malloc Debug Behavior
40---------------------------------
41Malloc debug is controlled by individual options. Each option can be enabled
42individually, or in a group of other options. Every single option can be
43combined with every other option.
44
45Option Descriptions
46-------------------
47### front\_guard[=SIZE\_BYTES]
48Enables a small buffer placed before the allocated data. This is an attempt
49to find memory corruption occuring to a region before the original allocation.
50On first allocation, this front guard is written with a specific pattern (0xaa).
51When the allocation is freed, the guard is checked to verify it has not been
52modified. If any part of the front guard is modified, an error will be reported
53in the log indicating what bytes changed.
54
55If the backtrace option is also enabled, then any error message will include
56the backtrace of the allocation site.
57
58If SIZE\_BYTES is present, it indicates the number of bytes in the guard.
59The default is 32 bytes, the max bytes is 16384. SIZE\_BYTES will be
60padded so that it is a multiple of 8 bytes on 32 bit systems and 16 bytes
61on 64 bit systems to make sure that the allocation returned is aligned
62properly.
63
64This option adds a special header to all allocations that contains the guard
65and information about the original allocation.
66
67Example error:
68
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070069 04-10 12:00:45.621 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 SIZE 100 HAS A CORRUPTED FRONT GUARD
70 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[-32] = 0x00 (expected 0xaa)
71 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[-15] = 0x02 (expected 0xaa)
Christopher Ferris713a8e32016-03-18 14:29:51 -070072
73### rear\_guard[=SIZE\_BYTES]
74Enables a small buffer placed after the allocated data. This is an attempt
75to find memory corruption occuring to a region after the original allocation.
76On first allocation, this rear guard is written with a specific pattern (0xbb).
77When the allocation is freed, the guard is checked to verify it has not been
78modified. If any part of the rear guard is modified, an error will be reported
79in the log indicating what bytes changed.
80
81If SIZE\_BYTES is present, it indicates the number of bytes in the guard.
82The default is 32 bytes, the max bytes is 16384.
83
84This option adds a special header to all allocations that contains
85information about the original allocation.
86
87Example error:
88
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070089 04-10 12:00:45.621 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 SIZE 100 HAS A CORRUPTED REAR GUARD
90 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[130] = 0xbf (expected 0xbb)
91 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[131] = 0x00 (expected 0xbb)
Christopher Ferris713a8e32016-03-18 14:29:51 -070092
93### guard[=SIZE\_BYTES]
94Enables both a front guard and a rear guard on all allocations.
95
96If SIZE\_BYTES is present, it indicates the number of bytes in both guards.
97The default is 32 bytes, the max bytes is 16384.
98
99### backtrace[=MAX\_FRAMES]
100Enable capturing the backtrace of each allocation site.
101This option will slow down allocations by an order of magnitude. If the
102system runs too slowly with this option enabled, decreasing the maximum number
103of frames captured will speed the allocations up.
104
105Note that any backtrace frames that occur within the malloc backtrace library
106itself are not recorded.
107
108If MAX\_FRAMES is present, it indicates the maximum number of frames to
109capture in a backtrace. The default is 16 frames, the maximumum value
110this can be set to is 256.
111
Christopher Ferris4da25032018-03-07 13:38:48 -0800112Before P, this option adds a special header to all allocations that contains
113the backtrace and information about the original allocation. After that, this
114option will not add a special header.
Christopher Ferris713a8e32016-03-18 14:29:51 -0700115
Christopher Ferris602b88c2017-08-04 13:04:04 -0700116As of P, this option will also enable dumping backtrace heap data to a
117file when the process receives the signal SIGRTMAX - 17 ( which is 47 on most
118Android devices). The format of this dumped data is the same format as
119that dumped when running am dumpheap -n. The default is to dump this data
120to the file /data/local/tmp/backtrace\_heap.**PID**.txt. This is useful when
121used with native only executables that run for a while since these processes
122are not spawned from a zygote process.
123
124Note that when the signal is received, the heap is not dumped until the next
125malloc/free occurs.
126
Christopher Ferris713a8e32016-03-18 14:29:51 -0700127### backtrace\_enable\_on\_signal[=MAX\_FRAMES]
128Enable capturing the backtrace of each allocation site. If the
129backtrace capture is toggled when the process receives the signal
130SIGRTMAX - 19 (which is 45 on most Android devices). When this
131option is used alone, backtrace capture starts out disabled until the signal
132is received. If both this option and the backtrace option are set, then
133backtrace capture is enabled until the signal is received.
134
135If MAX\_FRAMES is present, it indicates the maximum number of frames to
136capture in a backtrace. The default is 16 frames, the maximumum value
137this can be set to is 256.
138
Christopher Ferris4da25032018-03-07 13:38:48 -0800139Before P, this option adds a special header to all allocations that contains
140the backtrace and information about the original allocation. After that, this
141option will not add a special header.
Christopher Ferris713a8e32016-03-18 14:29:51 -0700142
Christopher Ferris602b88c2017-08-04 13:04:04 -0700143### backtrace\_dump\_on\_exit
144As of P, when the backtrace option has been enabled, this causes the backtrace
145dump heap data to be dumped to a file when the program exits. If the backtrace
146option has not been enabled, this does nothing. The default is to dump this
147to the file named /data/local/tmp/backtrace\_heap.**PID**.exit.txt.
148
149The file location can be changed by setting the backtrace\_dump\_prefix
150option.
151
152### backtrace\_dump\_prefix
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700153As of P, when one of the backtrace options has been enabled, this sets the
154prefix used for dumping files when the signal SIGRTMAX - 17 is received or when
Christopher Ferris602b88c2017-08-04 13:04:04 -0700155the program exits and backtrace\_dump\_on\_exit is set.
156
157The default is /data/local/tmp/backtrace\_heap.
158
159When this value is changed from the default, then the filename chosen
160on the signal will be backtrace\_dump\_prefix.**PID**.txt. The filename chosen
161when the program exits will be backtrace\_dump\_prefix.**PID**.exit.txt.
162
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700163### backtrace\_full
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700164As of Q, any time that a backtrace is gathered, a different algorithm is used
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700165that is extra thorough and can unwind through Java frames. This will run
166slower than the normal backtracing function.
167
Christopher Ferris713a8e32016-03-18 14:29:51 -0700168### fill\_on\_alloc[=MAX\_FILLED\_BYTES]
169Any allocation routine, other than calloc, will result in the allocation being
170filled with the value 0xeb. When doing a realloc to a larger size, the bytes
171above the original usable size will be set to 0xeb.
172
173If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number
174of bytes in the allocation. The default is to fill the entire allocation.
175
176### fill\_on\_free[=MAX\_FILLED\_BYTES]
177When an allocation is freed, fill it with 0xef.
178
179If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number
180of bytes in the allocation. The default is to fill the entire allocation.
181
182### fill[=MAX\_FILLED\_BYTES]
183This enables both the fill\_on\_alloc option and the fill\_on\_free option.
184
185If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number
186of bytes in the allocation. The default is to fill the entire allocation.
187
188### expand\_alloc[=EXPAND\_BYTES]
189Add an extra amount to allocate for every allocation.
190
191If XX is present, it is the number of bytes to expand the allocation by.
192The default is 16 bytes, the max bytes is 16384.
193
194### free\_track[=ALLOCATION\_COUNT]
195When a pointer is freed, do not free the memory right away, but add it to
196a list of freed allocations. In addition to being added to the list, the
197entire allocation is filled with the value 0xef, and the backtrace at
198the time of the free is recorded. The backtrace recording is completely
199separate from the backtrace option, and happens automatically if this
200option is enabled. By default, a maximum of 16 frames will be recorded,
201but this value can be changed using the free\_track\_backtrace\_num\_frames
202option. It can also be completely disabled by setting the option to zero.
203See the full description of this option below.
204
205When the list is full, an allocation is removed from the list and is
206checked to make sure that none of the contents have been modified since
207being placed on the list. When the program terminates, all of the allocations
208left on the list are verified.
209
210If ALLOCATION\_COUNT is present, it indicates the total number of allocations
211in the list. The default is to record 100 freed allocations, the max
212allocations to record is 16384.
213
Christopher Ferris4da25032018-03-07 13:38:48 -0800214Before P, this option adds a special header to all allocations that contains
215the backtrace and information about the original allocation. After that, this
216option will not add a special header.
Christopher Ferris713a8e32016-03-18 14:29:51 -0700217
218Example error:
219
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700220 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE
221 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[20] = 0xaf (expected 0xef)
222 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[99] = 0x12 (expected 0xef)
223 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of free:
224 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
225 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
226 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
227 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700228
229In addition, there is another type of error message that can occur if
230an allocation has a special header applied, and the header is corrupted
231before the verification occurs. This is the error message that will be found
232in the log:
233
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700234 04-15 12:00:31.604 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS CORRUPTED HEADER TAG 0x1cc7dc00 AFTER FREE
Christopher Ferris713a8e32016-03-18 14:29:51 -0700235
236### free\_track\_backtrace\_num\_frames[=MAX\_FRAMES]
237This option only has meaning if free\_track is set. It indicates how many
238backtrace frames to capture when an allocation is freed.
239
240If MAX\_FRAMES is present, it indicates the number of frames to capture.
241If the value is set to zero, then no backtrace will be captured when the
242allocation is freed. The default is to record 16 frames, the max number of
243frames to to record is 256.
244
245### leak\_track
246Track all live allocations. When the program terminates, all of the live
247allocations will be dumped to the log. If the backtrace option was enabled,
248then the log will include the backtrace of the leaked allocations. This
249option is not useful when enabled globally because a lot of programs do not
250free everything before the program terminates.
251
Christopher Ferris4da25032018-03-07 13:38:48 -0800252Before P, this option adds a special header to all allocations that contains
253the backtrace and information about the original allocation. After that, this
254option will not add a special header.
Christopher Ferris713a8e32016-03-18 14:29:51 -0700255
256Example leak error found in the log:
257
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700258 04-15 12:35:33.304 7412 7412 E malloc_debug: +++ APP leaked block of size 100 at 0x2be3b0b0 (leak 1 of 2)
259 04-15 12:35:33.304 7412 7412 E malloc_debug: Backtrace at time of allocation:
260 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
261 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
262 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
263 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
264 04-15 12:35:33.305 7412 7412 E malloc_debug: +++ APP leaked block of size 24 at 0x7be32380 (leak 2 of 2)
265 04-15 12:35:33.305 7412 7412 E malloc_debug: Backtrace at time of allocation:
266 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
267 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
268 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
269 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700270
Christopher Ferris7bd01782016-04-20 12:30:58 -0700271### record\_allocs[=TOTAL\_ENTRIES]
272Keep track of every allocation/free made on every thread and dump them
273to a file when the signal SIGRTMAX - 18 (which is 46 on most Android devices)
274is received.
275
276If TOTAL\_ENTRIES is set, then it indicates the total number of
277allocation/free records that can be retained. If the number of records
278reaches the TOTAL\_ENTRIES value, then any further allocations/frees are
279not recorded. The default value is 8,000,000 and the maximum value this
280can be set to is 50,000,000.
281
282Once the signal is received, and the current records are written to the
283file, all current records are deleted. Any allocations/frees occuring while
284the data is being dumped to the file are ignored.
285
286**NOTE**: This option is not available until the O release of Android.
287
288The allocation data is written in a human readable format. Every line begins
289with the THREAD\_ID returned by gettid(), which is the thread that is making
290the allocation/free. If a new thread is created, no special line is added
291to the file. However, when a thread completes, a special entry is added to
292the file indicating this.
293
294The thread complete line is:
295
296**THREAD\_ID**: thread\_done 0x0
297
298Example:
299
300 187: thread_done 0x0
301
302Below is how each type of allocation/free call ends up in the file dump.
303
304pointer = malloc(size)
305
306**THREAD\_ID**: malloc pointer size
307
308Example:
309
310 186: malloc 0xb6038060 20
311
312free(pointer)
313
314**THREAD\_ID**: free pointer
315
316Example:
317
318 186: free 0xb6038060
319
320pointer = calloc(nmemb, size)
321
322**THREAD\_ID**: calloc pointer nmemb size
323
324Example:
325
326 186: calloc 0xb609f080 32 4
327
328new\_pointer = realloc(old\_pointer, size)
329
330**THREAD\_ID**: realloc new\_pointer old\_pointer size
331
332Example:
333
334 186: realloc 0xb609f080 0xb603e9a0 12
335
336pointer = memalign(alignment, size)
337
338**THREAD\_ID**: memalign pointer alignment size
339
Christopher Ferriscae21a92018-02-05 18:14:55 -0800340pointer = aligned\_alloc(alignment, size)
341
342**THREAD\_ID**: memalign pointer alignment size
343
Christopher Ferris7bd01782016-04-20 12:30:58 -0700344posix\_memalign(&pointer, alignment, size)
345
346**THREAD\_ID**: memalign pointer alignment size
347
348Example:
349
350 186: memalign 0x85423660 16 104
351
352pointer = valloc(size)
353
354**THREAD\_ID**: memalign pointer 4096 size
355
356Example:
357
358 186: memalign 0x85423660 4096 112
359
360pointer = pvalloc(size)
361
362**THREAD\_ID**: memalign pointer 4096 <b>SIZE\_ROUNDED\_UP\_TO\_4096</b>
363
364Example:
365
366 186: memalign 0x85423660 4096 8192
367
368### record\_allocs\_file[=FILE\_NAME]
369This option only has meaning if record\_allocs is set. It indicates the
370file where the recorded allocations will be found.
371
372If FILE\_NAME is set, then it indicates where the record allocation data
373will be placed.
374
375**NOTE**: This option is not available until the O release of Android.
376
Christopher Ferris4da25032018-03-07 13:38:48 -0800377### verify\_pointers
378Track all live allocations to determine if a pointer is used that does not
379exist. This option is a lightweight way to verify that all
380free/malloc\_usable\_size/realloc calls are passed valid pointers.
381
382Example error:
383
384 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 UNKNOWN POINTER (free)
385 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure:
386 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
387 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
388 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
389 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
390
391Where the name of the function varies depending on the function that called
392with a bad pointer. Only three functions do this checking: free,
393malloc\_usable\_size, realloc.
394
395**NOTE**: This option is not available until the P release of Android.
396
Iris Chang7f209a92019-01-16 11:17:15 +0800397### abort\_on\_error
398When malloc debug detects an error, abort after sending the error
399log message.
400
401**NOTE**: If leak\_track is enabled, no abort occurs if leaks have been
402detected when the process is exiting.
403
Christopher Ferris713a8e32016-03-18 14:29:51 -0700404Additional Errors
405-----------------
406There are a few other error messages that might appear in the log.
407
408### Use After Free
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700409 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (free)
410 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace of original free:
411 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
412 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
413 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
414 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
415 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure:
416 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
417 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
418 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
419 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700420
421This indicates that code is attempting to free an already freed pointer. The
422name in parenthesis indicates that the application called the function
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700423*free* with the bad pointer.
Christopher Ferris713a8e32016-03-18 14:29:51 -0700424
425For example, this message:
426
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700427 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (realloc)
Christopher Ferris713a8e32016-03-18 14:29:51 -0700428
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700429Would indicate that the application called the *realloc* function
Christopher Ferris713a8e32016-03-18 14:29:51 -0700430with an already freed pointer.
431
432### Invalid Tag
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700433 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS INVALID TAG 1ee7d000 (malloc_usable_size)
434 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure:
435 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
436 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
437 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
438 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700439
440This indicates that a function (malloc\_usable\_size) was called with
441a pointer that is either not allocated memory, or that the memory of
442the pointer has been corrupted.
443
444As with the other error message, the function in parenthesis is the
445function that was called with the bad pointer.
446
Christopher Ferris602b88c2017-08-04 13:04:04 -0700447Backtrace Heap Dump Format
448==========================
449
450This section describes the format of the backtrace heap dump. This data is
451generated by am dumpheap -n or, as of P, by the signal or on exit.
452
453The data has this header:
454
455 Android Native Heap Dump v1.0
456
457 Total memory: XXXX
458 Allocation records: YYYY
459 Backtrace size: ZZZZ
460
461Total memory is the total of all of the currently live allocations.
462Allocation records is the total number of allocation records.
463Backtrace size is the maximum number of backtrace frames that can be present.
464
465Following this header are two different sections, the first section is the
466allocation records, the second section is the map data.
467
468The allocation record data has this format:
469
470 z ZYGOTE_CHILD_ALLOC sz ALLOCATION_SIZE num NUM_ALLOCATIONS bt FRAMES
471
472ZYGOTE\_CHILD\_ALLOC is either 0 or 1. 0 means this was allocated by the
473zygote process or in a process not spawned from the zygote. 1 means this
474was allocated by an application after it forked off from the zygote process.
475
476ALLOCATION\_SIZE is the size of the allocation.
477NUM\_ALLOCATIONS is the number of allocations that have this size and have the
478same backtrace.
479FRAMES is a list of instruction pointers that represent the backtrace of the
480allocation.
481
482Example:
483
484 z 0 sz 400 num 1 bt 0000a230 0000b500
485 z 1 sz 500 num 3 bt 0000b000 0000c000
486
487The first allocation record was created by the zygote of size 400 only one
488with this backtrace/size and a backtrace of 0xa230, 0xb500.
489The second allocation record was create by an application spawned from the
490zygote of size 500, where there are three of these allocation with the same
491backtrace/size and a backtrace of 0xb000, 0xc000.
492
493The final section is the map data for the process:
494
495 MAPS
496 7fe9181000-7fe91a2000 rw-p 00000000 00:00 0 /system/lib/libc.so
497 .
498 .
499 .
500 END
501
502The map data is simply the output of /proc/PID/maps. This data can be used to
503decode the frames in the backtraces.
504
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700505There are now multiple versions of the file:
506
507Android P produces version v1.1 of the heap dump.
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700508
509 Android Native Heap Dump v1.1
510
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700511The only difference between v1.0 and v1.1 is that the NUM\_ALLOCATIONS
512value is always accurate in v1.1. A previous version of malloc debug set
513NUM\_ALLOCATIONS to an incorrect value. For heap dump v1.0, the
514NUM\_ALLOCATIONS value should be treated as always 1 no matter what is
515actually present.
516
517Android Q introduces v1.2 of the heap dump. The new header looks like this:
518
519 Android Native Heap Dump v1.2
520
521 Build fingerprint: 'google/taimen/taimen:8.1.0/OPM2.171026.006.C1/4769658:user/release-keys'
522
523The new line fingerprint line is the contents of the ro.build.fingerprint
524property.
525
526The new version no longer 0 pads the backtrace addresses. In v1.0/v1.1:
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700527
528 z 0 sz 400 num 1 bt 0000a230 0000b500
529
Christopher Ferris2e1a40a2018-06-13 10:46:34 -0700530While v1.2:
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700531
532 z 0 sz 400 num 1 bt a230 b500
533
534In addition, when the new option backtrace\_full is used, another line will
535be added to every backtrace line. The line will be:
536
537 bt_info {"MAP_NAME" RELATIVE_TO_MAP_PC "FUNCTION_NAME" FUNCTION_OFFSET} ...
538
539For each backtrace pc, there will be one element in braces.
540
541MAP\_NAME is the name of the map in which the backtrace pc exists. If there is
542no valid map name, this will be empty.
543RELATIVE\_TO\_MAP\_PC is the hexadecimal value of the relative pc to the map.
544FUNCTION\_NAME the name of the function for this pc. If there is no valid
545function name, then it will be empty.
546FUNCTION\_OFFSET the hexadecimal offset from the beginning of the function. If
547the FUNCTION\_NAME is empty, then this value will always be zero.
548
549An example of this new format:
550
551 z 0 sz 400 num 1 bt a2a0 b510
552 bt_info {"/system/libc.so" 2a0 "abort" 24} {"/system/libutils.so" 510 "" 0}
553
554In this example, the first backtrace frame has a pc of 0xa2a0 and is in the
555map named /system/libc.so which starts at 0xa000. The relative pc is 0x2a0,
556and it is in the function abort + 0x24.
557The second backtrace frame has a pc of 0xb510 and is in the map named
558/system/libutils.so which starts at 0xb000. The relative pc is 0x510 and
559it is in an unknown function.
560
Elliott Hughes14967182018-05-24 18:41:22 -0700561There is a tool to visualize this data,
562[native\_heapdump\_viewer.py](https://android.googlesource.com/platform/development/+/master/scripts/native_heapdump_viewer.py).
Christopher Ferris602b88c2017-08-04 13:04:04 -0700563
Christopher Ferris713a8e32016-03-18 14:29:51 -0700564Examples
565========
Elliott Hughes644275a2017-08-15 23:17:35 -0700566
567### For platform developers
568
Christopher Ferris713a8e32016-03-18 14:29:51 -0700569Enable backtrace tracking of all allocation for all processes:
570
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700571 adb shell stop
572 adb shell setprop libc.debug.malloc.options backtrace
573 adb shell start
Christopher Ferris713a8e32016-03-18 14:29:51 -0700574
575Enable backtrace tracking for a specific process (ls):
576
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700577 adb shell setprop libc.debug.malloc.options backtrace
578 adb shell setprop libc.debug.malloc.program ls
579 adb shell ls
Christopher Ferris713a8e32016-03-18 14:29:51 -0700580
581Enable backtrace tracking for the zygote and zygote based processes:
582
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700583 adb shell stop
584 adb shell setprop libc.debug.malloc.program app_process
585 adb shell setprop libc.debug.malloc.options backtrace
586 adb shell start
Christopher Ferris713a8e32016-03-18 14:29:51 -0700587
Mikhail Naganov5a1a9532018-01-03 08:50:16 -0800588Enable multiple options (backtrace and guard):
Christopher Ferris713a8e32016-03-18 14:29:51 -0700589
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700590 adb shell stop
Mikhail Naganov5a1a9532018-01-03 08:50:16 -0800591 adb shell setprop libc.debug.malloc.options "\"backtrace guard\""
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700592 adb shell start
Christopher Ferris713a8e32016-03-18 14:29:51 -0700593
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700594Note: The two levels of quoting in the adb shell command is necessary.
595The outer layer of quoting is for the shell on the host, to ensure that the
Mikhail Naganov5a1a9532018-01-03 08:50:16 -0800596inner layer of quoting is sent to the device, to make 'backtrace guard'
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700597a single argument.
Christopher Ferris713a8e32016-03-18 14:29:51 -0700598
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700599Enable malloc debug using an environment variable (pre-O Android release):
Christopher Ferris713a8e32016-03-18 14:29:51 -0700600
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700601 adb shell
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700602 # setprop libc.debug.malloc.env_enabled 1
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700603 # setprop libc.debug.malloc.options backtrace
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700604 # export LIBC_DEBUG_MALLOC_ENABLE=1
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700605 # ls
Christopher Ferrisac66d162016-09-28 14:51:12 -0700606
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700607Enable malloc debug using an environment variable (Android O or later):
608
609 adb shell
610 # export LIBC_DEBUG_MALLOC_OPTIONS=backtrace
611 # ls
612
613Any process spawned from this shell will run with malloc debug enabled
614using the backtrace option.
Christopher Ferrisac66d162016-09-28 14:51:12 -0700615
616 adb shell stop
617 adb shell setprop libc.debug.malloc.options backtrace
618 adb shell start
619 adb shell am dumpheap -n <PID_TO_DUMP> /data/local/tmp/heap.txt
620
621It is possible to use the backtrace\_enable\_on\_signal option as well,
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700622but, obviously, it must be enabled through the signal before the file will
623contain any data.
624
Elliott Hughes644275a2017-08-15 23:17:35 -0700625### For app developers
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700626
Elliott Hughes5ad14212018-04-06 15:13:14 -0700627App developers should check the NDK documentation about
628[wrap.sh](https://developer.android.com/ndk/guides/wrap-script.html)
629for the best way to use malloc debug in Android O or later on non-rooted
630devices.
631
632If you do have a rooted device, you can enable malloc debug for a specific
633program/application (Android O or later):
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700634
635 adb shell setprop wrap.<APP> '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"'
636
Christopher Ferris1d52a7b2018-06-01 13:40:38 -0700637If you need to enable multiple options using this method, then you can set
638them like so:
639
640 adb shell setprop wrap.<APP> '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace\ leak_track\ fill logwrapper"'
641
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700642For example, to enable malloc debug for the google search box (Android O or later):
643
644 adb shell setprop wrap.com.google.android.googlequicksearchbox '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"'
645 adb shell am force-stop com.google.android.googlequicksearchbox
646
Christopher Ferrisad935c82018-08-16 15:59:48 -0700647If you are setting multiple options and the app does not appear to start
648properly, check the logcat looking for this message
649(`adb logcat -d | grep "malloc debug"`):
650
651 08-16 15:54:16.060 26947 26947 I libc : /system/bin/app_process64: malloc debug enabled
652
653If you do not see this message, then the wrap property was not set correctly.
654Run:
655
656 adb shell getprop | grep wrap
657
658And verify that any spaces are properly escaped.
659
Christopher Ferris4c65669a2017-05-24 19:04:33 -0700660NOTE: On pre-O versions of the Android OS, property names had a length limit
661of 32. This meant that to create a wrap property with the name of the app, it
662was necessary to truncate the name to fit. On O, property names can be
663an order of magnitude larger, so there should be no need to truncate the name
664at all.
Elliott Hughes644275a2017-08-15 23:17:35 -0700665
666To detect leaks while an app is running:
667
668 adb shell dumpsys meminfo --unreachable <PID_OF_APP>
669
670Without also enabling malloc debug, this command will only tell
671you whether it can detect leaked memory, not where those leaks are
672occurring. If you enable malloc debug with the backtrace option for your
673app before running the dumpsys command, you'll get backtraces showing
674where the memory was allocated.
675
676For backtraces from your app to be useful, you'll want to keep the
677symbols in your app's shared libraries rather than stripping them. That
678way you'll see the location of the leak directly without having to use
679something like the <code>ndk-stack</code> tool.
680
681### Analyzing heap dumps
682
Elliott Hughes14967182018-05-24 18:41:22 -0700683To analyze the data produced by the dumpheap command, run
684[development/scripts/native\_heapdump\_viewer.py](https://android.googlesource.com/platform/development/+/master/scripts/native_heapdump_viewer.py)
Elliott Hughes644275a2017-08-15 23:17:35 -0700685
686In order for the script to properly symbolize the stacks in the file,
687make sure the script is executed from the tree that built the image.
688
689To collect, transfer, and analyze a dump:
690
691 adb shell am dumpheap -n <PID_TO_DUMP> /data/local/tmp/heap.txt
692 adb shell pull /data/local/tmp/heap.txt .
693 python development/scripts/native_heapdump_viewer.py --symbols /some/path/to/symbols/ heap.txt > heap_info.txt
694
695At the moment, the script will look for symbols in the given directory,
696using the path the .so file would have on the device. So if your .so file
697is at `/data/app/.../lib/arm/libx.so` on the device, it will need to be at
698`/some/path/to/symbols/data/app/.../lib/arm/libx.so` locally given the
699command line above. That is: you need to mirror the directory structure
700for the app in the symbols directory.