Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 1 | Malloc Debug |
| 2 | ============ |
| 3 | |
| 4 | Malloc debug is a method of debugging native memory problems. It can help |
| 5 | detect memory corruption, memory leaks, and use after free issues. |
| 6 | |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 7 | This documentation describes how to enable this feature on API level 24 or later |
Elliott Hughes | 7dc7d2b | 2018-04-06 14:44:49 -0700 | [diff] [blame] | 8 | versions of the Android OS. (See the "Examples" section.) |
Christopher Ferris | eab4803 | 2016-05-25 13:04:29 -0700 | [diff] [blame] | 9 | |
| 10 | The documentation for malloc debug on older versions of Android is |
| 11 | [here](README_marshmallow_and_earlier.md). |
| 12 | |
Christopher Ferris | eab4803 | 2016-05-25 13:04:29 -0700 | [diff] [blame] | 13 | When malloc debug is enabled, it works by adding a shim layer that replaces |
| 14 | the normal allocation calls. The replaced calls are: |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 15 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 16 | * `malloc` |
| 17 | * `free` |
| 18 | * `calloc` |
| 19 | * `realloc` |
| 20 | * `posix_memalign` |
| 21 | * `memalign` |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 22 | * `aligned_alloc` |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 23 | * `malloc_usable_size` |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 24 | |
| 25 | On 32 bit systems, these two deprecated functions are also replaced: |
| 26 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 27 | * `pvalloc` |
| 28 | * `valloc` |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 29 | |
| 30 | Any errors detected by the library are reported in the log. |
| 31 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 32 | NOTE: There is a small behavioral change beginning in P for realloc. |
| 33 | Before, a realloc from one size to a smaller size would not update the |
| 34 | backtrace related to the allocation. Starting in P, every single realloc |
| 35 | call changes the backtrace for the pointer no matter whether the pointer |
| 36 | returned has changed or not. |
| 37 | |
| 38 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 39 | Controlling Malloc Debug Behavior |
| 40 | --------------------------------- |
| 41 | Malloc debug is controlled by individual options. Each option can be enabled |
| 42 | individually, or in a group of other options. Every single option can be |
| 43 | combined with every other option. |
| 44 | |
| 45 | Option Descriptions |
| 46 | ------------------- |
| 47 | ### front\_guard[=SIZE\_BYTES] |
| 48 | Enables a small buffer placed before the allocated data. This is an attempt |
| 49 | to find memory corruption occuring to a region before the original allocation. |
| 50 | On first allocation, this front guard is written with a specific pattern (0xaa). |
| 51 | When the allocation is freed, the guard is checked to verify it has not been |
| 52 | modified. If any part of the front guard is modified, an error will be reported |
| 53 | in the log indicating what bytes changed. |
| 54 | |
| 55 | If the backtrace option is also enabled, then any error message will include |
| 56 | the backtrace of the allocation site. |
| 57 | |
| 58 | If SIZE\_BYTES is present, it indicates the number of bytes in the guard. |
| 59 | The default is 32 bytes, the max bytes is 16384. SIZE\_BYTES will be |
| 60 | padded so that it is a multiple of 8 bytes on 32 bit systems and 16 bytes |
| 61 | on 64 bit systems to make sure that the allocation returned is aligned |
| 62 | properly. |
| 63 | |
| 64 | This option adds a special header to all allocations that contains the guard |
| 65 | and information about the original allocation. |
| 66 | |
| 67 | Example error: |
| 68 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 69 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 72 | |
| 73 | ### rear\_guard[=SIZE\_BYTES] |
| 74 | Enables a small buffer placed after the allocated data. This is an attempt |
| 75 | to find memory corruption occuring to a region after the original allocation. |
| 76 | On first allocation, this rear guard is written with a specific pattern (0xbb). |
| 77 | When the allocation is freed, the guard is checked to verify it has not been |
| 78 | modified. If any part of the rear guard is modified, an error will be reported |
| 79 | in the log indicating what bytes changed. |
| 80 | |
| 81 | If SIZE\_BYTES is present, it indicates the number of bytes in the guard. |
| 82 | The default is 32 bytes, the max bytes is 16384. |
| 83 | |
| 84 | This option adds a special header to all allocations that contains |
| 85 | information about the original allocation. |
| 86 | |
| 87 | Example error: |
| 88 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 89 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 92 | |
| 93 | ### guard[=SIZE\_BYTES] |
| 94 | Enables both a front guard and a rear guard on all allocations. |
| 95 | |
| 96 | If SIZE\_BYTES is present, it indicates the number of bytes in both guards. |
| 97 | The default is 32 bytes, the max bytes is 16384. |
| 98 | |
| 99 | ### backtrace[=MAX\_FRAMES] |
| 100 | Enable capturing the backtrace of each allocation site. |
| 101 | This option will slow down allocations by an order of magnitude. If the |
| 102 | system runs too slowly with this option enabled, decreasing the maximum number |
| 103 | of frames captured will speed the allocations up. |
| 104 | |
| 105 | Note that any backtrace frames that occur within the malloc backtrace library |
| 106 | itself are not recorded. |
| 107 | |
| 108 | If MAX\_FRAMES is present, it indicates the maximum number of frames to |
| 109 | capture in a backtrace. The default is 16 frames, the maximumum value |
| 110 | this can be set to is 256. |
| 111 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 112 | Before P, this option adds a special header to all allocations that contains |
| 113 | the backtrace and information about the original allocation. After that, this |
| 114 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 115 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 116 | As of P, this option will also enable dumping backtrace heap data to a |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 117 | file when the process receives the signal SIGRTMAX - 17 ( which is 47 on |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 118 | Android devices). The format of this dumped data is the same format as |
| 119 | that dumped when running am dumpheap -n. The default is to dump this data |
| 120 | to the file /data/local/tmp/backtrace\_heap.**PID**.txt. This is useful when |
| 121 | used with native only executables that run for a while since these processes |
| 122 | are not spawned from a zygote process. |
| 123 | |
| 124 | Note that when the signal is received, the heap is not dumped until the next |
| 125 | malloc/free occurs. |
| 126 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 127 | ### backtrace\_enable\_on\_signal[=MAX\_FRAMES] |
| 128 | Enable capturing the backtrace of each allocation site. If the |
| 129 | backtrace capture is toggled when the process receives the signal |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 130 | SIGRTMAX - 19 (which is 45 on Android devices). When this |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 131 | option is used alone, backtrace capture starts out disabled until the signal |
| 132 | is received. If both this option and the backtrace option are set, then |
| 133 | backtrace capture is enabled until the signal is received. |
| 134 | |
| 135 | If MAX\_FRAMES is present, it indicates the maximum number of frames to |
| 136 | capture in a backtrace. The default is 16 frames, the maximumum value |
| 137 | this can be set to is 256. |
| 138 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 139 | Before P, this option adds a special header to all allocations that contains |
| 140 | the backtrace and information about the original allocation. After that, this |
| 141 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 142 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 143 | ### backtrace\_dump\_on\_exit |
| 144 | As of P, when the backtrace option has been enabled, this causes the backtrace |
| 145 | dump heap data to be dumped to a file when the program exits. If the backtrace |
| 146 | option has not been enabled, this does nothing. The default is to dump this |
| 147 | to the file named /data/local/tmp/backtrace\_heap.**PID**.exit.txt. |
| 148 | |
| 149 | The file location can be changed by setting the backtrace\_dump\_prefix |
| 150 | option. |
| 151 | |
| 152 | ### backtrace\_dump\_prefix |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 153 | As of P, when one of the backtrace options has been enabled, this sets the |
| 154 | prefix used for dumping files when the signal SIGRTMAX - 17 is received or when |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 155 | the program exits and backtrace\_dump\_on\_exit is set. |
| 156 | |
| 157 | The default is /data/local/tmp/backtrace\_heap. |
| 158 | |
| 159 | When this value is changed from the default, then the filename chosen |
| 160 | on the signal will be backtrace\_dump\_prefix.**PID**.txt. The filename chosen |
| 161 | when the program exits will be backtrace\_dump\_prefix.**PID**.exit.txt. |
| 162 | |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 163 | ### backtrace\_min\_size=ALLOCATION\_SIZE\_BYTES |
| 164 | As of U, setting this in combination with the backtrace option means |
| 165 | that only allocations of a size greater than or equal to |
| 166 | **ALLOCATION\_SIZE\_BYTES** will be backtraced. When used in combination |
| 167 | with the backtrace\_max\_size option, then allocations greater than or |
| 168 | equal to backtrace\_min\_size and less than or equal to |
| 169 | backtrace\_max\_size will be backtraced. The backtrace\_size option |
| 170 | overrides this option, and should not be used at the same time. |
| 171 | |
| 172 | This option can also be used in combination with other tools such |
Elliott Hughes | 9c06d16 | 2023-10-04 23:36:14 +0000 | [diff] [blame] | 173 | as [libmemunreachable](https://android.googlesource.com/platform/system/memory/libmemunreachable/+/main/README.md) |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 174 | to only get backtraces for sizes of allocations listed as being leaked. |
| 175 | |
| 176 | ### backtrace\_max\_size=ALLOCATION\_SIZE\_BYTES |
| 177 | As of U, setting this in combination with the backtrace option means |
| 178 | that only allocations of a size less than or equal to |
| 179 | **ALLOCATION\_SIZE\_BYTES** will be backtraced. When used in combination |
| 180 | with the backtrace\_min\_size option, then allocations greater than or |
| 181 | equal to backtrace\_min\_size and less than or equal to |
| 182 | backtrace\_max\_size will be backtraced. The backtrace\_size option |
| 183 | overrides this option, and should not be used at the same time. |
| 184 | |
| 185 | This option can also be used in combination with other tools such |
Elliott Hughes | 9c06d16 | 2023-10-04 23:36:14 +0000 | [diff] [blame] | 186 | as [libmemunreachable](https://android.googlesource.com/platform/system/memory/libmemunreachable/+/main/README.md) |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 187 | to only get backtraces for sizes of allocations listed as being leaked. |
| 188 | |
| 189 | ### backtrace\_size=ALLOCATION\_SIZE\_BYTES |
| 190 | As of U, setting this in combination with the backtrace option means |
| 191 | that only allocations of size **ALLOCATION\_SIZE\_BYTES** will be backtraced. |
| 192 | This option overrides the backtrace\_min\_size and the backtrace\_max\_size. |
| 193 | |
| 194 | This option can also be used in combination with other tools such |
Elliott Hughes | 9c06d16 | 2023-10-04 23:36:14 +0000 | [diff] [blame] | 195 | as [libmemunreachable](https://android.googlesource.com/platform/system/memory/libmemunreachable/+/main/README.md) |
Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 196 | to only get backtraces for sizes of allocations listed as being leaked. |
| 197 | |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 198 | ### backtrace\_full |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 199 | As of Q, any time that a backtrace is gathered, a different algorithm is used |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 200 | that is extra thorough and can unwind through Java frames. This will run |
| 201 | slower than the normal backtracing function. |
| 202 | |
Junjie Hu | 8655d17 | 2023-01-10 18:30:45 +0800 | [diff] [blame] | 203 | ### bt, bt\_dmp\_on\_ex, bt\_dmp\_pre, bt\_en\_on\_sig, bt\_full, bt\_max\_sz, bt\_min\_sz, bt\_sz |
| 204 | As of U, add shorter aliases for backtrace related options to avoid property length restrictions. |
| 205 | |
| 206 | | Alias | Option | |
| 207 | |:----------------|:------------------------------| |
| 208 | | bt | backtrace | |
| 209 | | bt\_dmp\_on\_ex | backtrace\_dump\_on\_exit | |
| 210 | | bt\_dmp\_pre | backtrace\_dump\_prefix | |
| 211 | | bt\_en\_on\_sig | backtrace\_enable\_on\_signal | |
| 212 | | bt\_full | backtrace\_full | |
| 213 | | bt\_max\_sz | backtrace\_max\_size | |
| 214 | | bt\_min\_sz | backtrace\_min\_size | |
| 215 | | bt\_sz | backtrace\_size | |
| 216 | |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 217 | ### check\_unreachable\_on\_signal |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 218 | As of API level 34, this option will trigger a check for unreachable memory |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 219 | in a process. Specifically, if the signal SIGRTMAX - 16 (which is 48 on |
| 220 | Android devices). The best way to see the exact signal being used is to |
| 221 | enable the verbose option then look at the log for the message: |
| 222 | |
| 223 | Run: 'kill -48 <PID>' to check for unreachable memory. |
| 224 | |
| 225 | When the signal is received, the actual unreachable check only triggers |
| 226 | on the next allocation that happens in the process (malloc/free, etc). |
| 227 | |
| 228 | If a process is not doing any allocations, it can be forced to trigger when |
| 229 | running: |
| 230 | |
| 231 | debuggerd -b <PID> |
| 232 | |
| 233 | **NOTE**: The unreachable check can fail for protected processes, so it |
| 234 | might be necessary to run: |
| 235 | |
| 236 | setenforce 0 |
| 237 | |
| 238 | To get the unreachable data. |
| 239 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 240 | ### fill\_on\_alloc[=MAX\_FILLED\_BYTES] |
| 241 | Any allocation routine, other than calloc, will result in the allocation being |
| 242 | filled with the value 0xeb. When doing a realloc to a larger size, the bytes |
| 243 | above the original usable size will be set to 0xeb. |
| 244 | |
| 245 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 246 | of bytes in the allocation. The default is to fill the entire allocation. |
| 247 | |
| 248 | ### fill\_on\_free[=MAX\_FILLED\_BYTES] |
| 249 | When an allocation is freed, fill it with 0xef. |
| 250 | |
| 251 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 252 | of bytes in the allocation. The default is to fill the entire allocation. |
| 253 | |
| 254 | ### fill[=MAX\_FILLED\_BYTES] |
| 255 | This enables both the fill\_on\_alloc option and the fill\_on\_free option. |
| 256 | |
| 257 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 258 | of bytes in the allocation. The default is to fill the entire allocation. |
| 259 | |
| 260 | ### expand\_alloc[=EXPAND\_BYTES] |
| 261 | Add an extra amount to allocate for every allocation. |
| 262 | |
| 263 | If XX is present, it is the number of bytes to expand the allocation by. |
| 264 | The default is 16 bytes, the max bytes is 16384. |
| 265 | |
| 266 | ### free\_track[=ALLOCATION\_COUNT] |
| 267 | When a pointer is freed, do not free the memory right away, but add it to |
| 268 | a list of freed allocations. In addition to being added to the list, the |
| 269 | entire allocation is filled with the value 0xef, and the backtrace at |
| 270 | the time of the free is recorded. The backtrace recording is completely |
| 271 | separate from the backtrace option, and happens automatically if this |
| 272 | option is enabled. By default, a maximum of 16 frames will be recorded, |
| 273 | but this value can be changed using the free\_track\_backtrace\_num\_frames |
| 274 | option. It can also be completely disabled by setting the option to zero. |
| 275 | See the full description of this option below. |
| 276 | |
| 277 | When the list is full, an allocation is removed from the list and is |
| 278 | checked to make sure that none of the contents have been modified since |
| 279 | being placed on the list. When the program terminates, all of the allocations |
| 280 | left on the list are verified. |
| 281 | |
| 282 | If ALLOCATION\_COUNT is present, it indicates the total number of allocations |
| 283 | in the list. The default is to record 100 freed allocations, the max |
| 284 | allocations to record is 16384. |
| 285 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 286 | Before P, this option adds a special header to all allocations that contains |
| 287 | the backtrace and information about the original allocation. After that, this |
| 288 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 289 | |
| 290 | Example error: |
| 291 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 292 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE |
| 293 | 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[20] = 0xaf (expected 0xef) |
| 294 | 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[99] = 0x12 (expected 0xef) |
| 295 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of free: |
| 296 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 297 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 298 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 299 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 300 | |
| 301 | In addition, there is another type of error message that can occur if |
| 302 | an allocation has a special header applied, and the header is corrupted |
| 303 | before the verification occurs. This is the error message that will be found |
| 304 | in the log: |
| 305 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 306 | 04-15 12:00:31.604 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS CORRUPTED HEADER TAG 0x1cc7dc00 AFTER FREE |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 307 | |
| 308 | ### free\_track\_backtrace\_num\_frames[=MAX\_FRAMES] |
| 309 | This option only has meaning if free\_track is set. It indicates how many |
| 310 | backtrace frames to capture when an allocation is freed. |
| 311 | |
| 312 | If MAX\_FRAMES is present, it indicates the number of frames to capture. |
| 313 | If the value is set to zero, then no backtrace will be captured when the |
| 314 | allocation is freed. The default is to record 16 frames, the max number of |
| 315 | frames to to record is 256. |
| 316 | |
| 317 | ### leak\_track |
| 318 | Track all live allocations. When the program terminates, all of the live |
| 319 | allocations will be dumped to the log. If the backtrace option was enabled, |
| 320 | then the log will include the backtrace of the leaked allocations. This |
| 321 | option is not useful when enabled globally because a lot of programs do not |
| 322 | free everything before the program terminates. |
| 323 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 324 | Before P, this option adds a special header to all allocations that contains |
| 325 | the backtrace and information about the original allocation. After that, this |
| 326 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 327 | |
| 328 | Example leak error found in the log: |
| 329 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 330 | 04-15 12:35:33.304 7412 7412 E malloc_debug: +++ APP leaked block of size 100 at 0x2be3b0b0 (leak 1 of 2) |
| 331 | 04-15 12:35:33.304 7412 7412 E malloc_debug: Backtrace at time of allocation: |
| 332 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 333 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 334 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 335 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
| 336 | 04-15 12:35:33.305 7412 7412 E malloc_debug: +++ APP leaked block of size 24 at 0x7be32380 (leak 2 of 2) |
| 337 | 04-15 12:35:33.305 7412 7412 E malloc_debug: Backtrace at time of allocation: |
| 338 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 339 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 340 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 341 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 342 | |
Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 343 | ### log\_allocator\_stats\_on\_signal |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 344 | As of API level 35, this option will trigger a call to: |
Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 345 | |
| 346 | mallopt(M_LOG_STATS, 0); |
| 347 | |
| 348 | When a process receives the signal SIGRTMAX - 15 (which is 49 on Android |
| 349 | devices). The mallopt call is not async safe and is not called from the |
| 350 | signal handler directly. Instead, the next time any allocation call occurs, |
| 351 | the mallopt is called. |
| 352 | |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 353 | ### record\_allocs[=TOTAL\_ENTRIES] |
| 354 | Keep track of every allocation/free made on every thread and dump them |
Christopher Ferris | b42e8b4 | 2022-05-09 14:00:47 -0700 | [diff] [blame] | 355 | to a file when the signal SIGRTMAX - 18 (which is 46 on Android devices) |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 356 | is received. |
| 357 | |
| 358 | If TOTAL\_ENTRIES is set, then it indicates the total number of |
| 359 | allocation/free records that can be retained. If the number of records |
| 360 | reaches the TOTAL\_ENTRIES value, then any further allocations/frees are |
| 361 | not recorded. The default value is 8,000,000 and the maximum value this |
| 362 | can be set to is 50,000,000. |
| 363 | |
| 364 | Once the signal is received, and the current records are written to the |
| 365 | file, all current records are deleted. Any allocations/frees occuring while |
| 366 | the data is being dumped to the file are ignored. |
| 367 | |
| 368 | **NOTE**: This option is not available until the O release of Android. |
| 369 | |
| 370 | The allocation data is written in a human readable format. Every line begins |
| 371 | with the THREAD\_ID returned by gettid(), which is the thread that is making |
| 372 | the allocation/free. If a new thread is created, no special line is added |
| 373 | to the file. However, when a thread completes, a special entry is added to |
| 374 | the file indicating this. |
| 375 | |
| 376 | The thread complete line is: |
| 377 | |
| 378 | **THREAD\_ID**: thread\_done 0x0 |
| 379 | |
| 380 | Example: |
| 381 | |
| 382 | 187: thread_done 0x0 |
| 383 | |
| 384 | Below is how each type of allocation/free call ends up in the file dump. |
| 385 | |
| 386 | pointer = malloc(size) |
| 387 | |
| 388 | **THREAD\_ID**: malloc pointer size |
| 389 | |
| 390 | Example: |
| 391 | |
| 392 | 186: malloc 0xb6038060 20 |
| 393 | |
| 394 | free(pointer) |
| 395 | |
| 396 | **THREAD\_ID**: free pointer |
| 397 | |
| 398 | Example: |
| 399 | |
| 400 | 186: free 0xb6038060 |
| 401 | |
| 402 | pointer = calloc(nmemb, size) |
| 403 | |
| 404 | **THREAD\_ID**: calloc pointer nmemb size |
| 405 | |
| 406 | Example: |
| 407 | |
| 408 | 186: calloc 0xb609f080 32 4 |
| 409 | |
| 410 | new\_pointer = realloc(old\_pointer, size) |
| 411 | |
| 412 | **THREAD\_ID**: realloc new\_pointer old\_pointer size |
| 413 | |
| 414 | Example: |
| 415 | |
| 416 | 186: realloc 0xb609f080 0xb603e9a0 12 |
| 417 | |
| 418 | pointer = memalign(alignment, size) |
| 419 | |
| 420 | **THREAD\_ID**: memalign pointer alignment size |
| 421 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 422 | pointer = aligned\_alloc(alignment, size) |
| 423 | |
| 424 | **THREAD\_ID**: memalign pointer alignment size |
| 425 | |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 426 | posix\_memalign(&pointer, alignment, size) |
| 427 | |
| 428 | **THREAD\_ID**: memalign pointer alignment size |
| 429 | |
| 430 | Example: |
| 431 | |
| 432 | 186: memalign 0x85423660 16 104 |
| 433 | |
| 434 | pointer = valloc(size) |
| 435 | |
| 436 | **THREAD\_ID**: memalign pointer 4096 size |
| 437 | |
| 438 | Example: |
| 439 | |
| 440 | 186: memalign 0x85423660 4096 112 |
| 441 | |
| 442 | pointer = pvalloc(size) |
| 443 | |
| 444 | **THREAD\_ID**: memalign pointer 4096 <b>SIZE\_ROUNDED\_UP\_TO\_4096</b> |
| 445 | |
| 446 | Example: |
| 447 | |
| 448 | 186: memalign 0x85423660 4096 8192 |
| 449 | |
| 450 | ### record\_allocs\_file[=FILE\_NAME] |
| 451 | This option only has meaning if record\_allocs is set. It indicates the |
| 452 | file where the recorded allocations will be found. |
| 453 | |
| 454 | If FILE\_NAME is set, then it indicates where the record allocation data |
| 455 | will be placed. |
| 456 | |
| 457 | **NOTE**: This option is not available until the O release of Android. |
| 458 | |
Christopher Ferris | 9b88d0a | 2024-06-13 13:22:02 -0700 | [diff] [blame] | 459 | ### record\_allocs\_on\_exit |
| 460 | This option only has meaning if record\_allocs is set. It indicates that |
| 461 | when the process terminates, the record file should be created |
| 462 | automatically. |
| 463 | |
| 464 | The only caveat to this option is that when the process terminates, |
| 465 | the file that will contain the records will be the normal file name |
| 466 | with **.PID** appended. Where PID is the pid of the process that has |
| 467 | terminated. This is to avoid cases where a number of processes exit |
| 468 | at the same time and attempt to write to the same file. |
| 469 | |
| 470 | **NOTE**: This option is not available until the V release of Android. |
| 471 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 472 | ### verify\_pointers |
| 473 | Track all live allocations to determine if a pointer is used that does not |
| 474 | exist. This option is a lightweight way to verify that all |
| 475 | free/malloc\_usable\_size/realloc calls are passed valid pointers. |
| 476 | |
| 477 | Example error: |
| 478 | |
| 479 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 UNKNOWN POINTER (free) |
| 480 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure: |
| 481 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 482 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 483 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 484 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
| 485 | |
| 486 | Where the name of the function varies depending on the function that called |
| 487 | with a bad pointer. Only three functions do this checking: free, |
| 488 | malloc\_usable\_size, realloc. |
| 489 | |
| 490 | **NOTE**: This option is not available until the P release of Android. |
| 491 | |
Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 492 | ### abort\_on\_error |
| 493 | When malloc debug detects an error, abort after sending the error |
| 494 | log message. |
| 495 | |
| 496 | **NOTE**: If leak\_track is enabled, no abort occurs if leaks have been |
| 497 | detected when the process is exiting. |
| 498 | |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 499 | ### verbose |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 500 | As of API level 29, all info messages will be turned off by default. For example, |
| 501 | in API level 28 and older, enabling malloc debug would result in this message |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 502 | in the log: |
| 503 | |
| 504 | 08-16 15:54:16.060 26947 26947 I libc : /system/bin/app_process64: malloc debug enabled |
| 505 | |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 506 | In API level 29, this message will not be displayed because these info messages |
Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 507 | slow down process start up. However, if you want to re-enable these messages, |
| 508 | add the verbose option. All of the "Run XXX" messages are also silenced unless |
| 509 | the verbose option is specified. This is an example of the type |
| 510 | of messages that are no longer displayed: |
| 511 | |
| 512 | 09-10 01:03:50.070 557 557 I malloc_debug: /system/bin/audioserver: Run: 'kill -47 557' to dump the backtrace. |
| 513 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 514 | Additional Errors |
| 515 | ----------------- |
| 516 | There are a few other error messages that might appear in the log. |
| 517 | |
| 518 | ### Use After Free |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 519 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (free) |
| 520 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace of original free: |
| 521 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 522 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 523 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 524 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
| 525 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure: |
| 526 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 527 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 528 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 529 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 530 | |
| 531 | This indicates that code is attempting to free an already freed pointer. The |
| 532 | name in parenthesis indicates that the application called the function |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 533 | *free* with the bad pointer. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 534 | |
| 535 | For example, this message: |
| 536 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 537 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (realloc) |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 538 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 539 | Would indicate that the application called the *realloc* function |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 540 | with an already freed pointer. |
| 541 | |
| 542 | ### Invalid Tag |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 543 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS INVALID TAG 1ee7d000 (malloc_usable_size) |
| 544 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure: |
| 545 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 546 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 547 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 548 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 549 | |
| 550 | This indicates that a function (malloc\_usable\_size) was called with |
| 551 | a pointer that is either not allocated memory, or that the memory of |
| 552 | the pointer has been corrupted. |
| 553 | |
| 554 | As with the other error message, the function in parenthesis is the |
| 555 | function that was called with the bad pointer. |
| 556 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 557 | Backtrace Heap Dump Format |
| 558 | ========================== |
| 559 | |
| 560 | This section describes the format of the backtrace heap dump. This data is |
| 561 | generated by am dumpheap -n or, as of P, by the signal or on exit. |
| 562 | |
| 563 | The data has this header: |
| 564 | |
| 565 | Android Native Heap Dump v1.0 |
| 566 | |
| 567 | Total memory: XXXX |
| 568 | Allocation records: YYYY |
| 569 | Backtrace size: ZZZZ |
| 570 | |
| 571 | Total memory is the total of all of the currently live allocations. |
| 572 | Allocation records is the total number of allocation records. |
| 573 | Backtrace size is the maximum number of backtrace frames that can be present. |
| 574 | |
| 575 | Following this header are two different sections, the first section is the |
| 576 | allocation records, the second section is the map data. |
| 577 | |
| 578 | The allocation record data has this format: |
| 579 | |
| 580 | z ZYGOTE_CHILD_ALLOC sz ALLOCATION_SIZE num NUM_ALLOCATIONS bt FRAMES |
| 581 | |
| 582 | ZYGOTE\_CHILD\_ALLOC is either 0 or 1. 0 means this was allocated by the |
| 583 | zygote process or in a process not spawned from the zygote. 1 means this |
| 584 | was allocated by an application after it forked off from the zygote process. |
| 585 | |
| 586 | ALLOCATION\_SIZE is the size of the allocation. |
| 587 | NUM\_ALLOCATIONS is the number of allocations that have this size and have the |
| 588 | same backtrace. |
| 589 | FRAMES is a list of instruction pointers that represent the backtrace of the |
| 590 | allocation. |
| 591 | |
| 592 | Example: |
| 593 | |
| 594 | z 0 sz 400 num 1 bt 0000a230 0000b500 |
| 595 | z 1 sz 500 num 3 bt 0000b000 0000c000 |
| 596 | |
| 597 | The first allocation record was created by the zygote of size 400 only one |
| 598 | with this backtrace/size and a backtrace of 0xa230, 0xb500. |
| 599 | The second allocation record was create by an application spawned from the |
| 600 | zygote of size 500, where there are three of these allocation with the same |
| 601 | backtrace/size and a backtrace of 0xb000, 0xc000. |
| 602 | |
| 603 | The final section is the map data for the process: |
| 604 | |
| 605 | MAPS |
| 606 | 7fe9181000-7fe91a2000 rw-p 00000000 00:00 0 /system/lib/libc.so |
| 607 | . |
| 608 | . |
| 609 | . |
| 610 | END |
| 611 | |
| 612 | The map data is simply the output of /proc/PID/maps. This data can be used to |
| 613 | decode the frames in the backtraces. |
| 614 | |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 615 | There are now multiple versions of the file: |
| 616 | |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 617 | API level 28 produces version v1.1 of the heap dump. |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 618 | |
| 619 | Android Native Heap Dump v1.1 |
| 620 | |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 621 | The only difference between v1.0 and v1.1 is that the NUM\_ALLOCATIONS |
| 622 | value is always accurate in v1.1. A previous version of malloc debug set |
| 623 | NUM\_ALLOCATIONS to an incorrect value. For heap dump v1.0, the |
| 624 | NUM\_ALLOCATIONS value should be treated as always 1 no matter what is |
| 625 | actually present. |
| 626 | |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 627 | API level 29 introduces v1.2 of the heap dump. The new header looks like this: |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 628 | |
| 629 | Android Native Heap Dump v1.2 |
| 630 | |
| 631 | Build fingerprint: 'google/taimen/taimen:8.1.0/OPM2.171026.006.C1/4769658:user/release-keys' |
| 632 | |
| 633 | The new line fingerprint line is the contents of the ro.build.fingerprint |
| 634 | property. |
| 635 | |
| 636 | The new version no longer 0 pads the backtrace addresses. In v1.0/v1.1: |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 637 | |
| 638 | z 0 sz 400 num 1 bt 0000a230 0000b500 |
| 639 | |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 640 | While v1.2: |
Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 641 | |
| 642 | z 0 sz 400 num 1 bt a230 b500 |
| 643 | |
| 644 | In addition, when the new option backtrace\_full is used, another line will |
| 645 | be added to every backtrace line. The line will be: |
| 646 | |
| 647 | bt_info {"MAP_NAME" RELATIVE_TO_MAP_PC "FUNCTION_NAME" FUNCTION_OFFSET} ... |
| 648 | |
| 649 | For each backtrace pc, there will be one element in braces. |
| 650 | |
| 651 | MAP\_NAME is the name of the map in which the backtrace pc exists. If there is |
| 652 | no valid map name, this will be empty. |
| 653 | RELATIVE\_TO\_MAP\_PC is the hexadecimal value of the relative pc to the map. |
| 654 | FUNCTION\_NAME the name of the function for this pc. If there is no valid |
| 655 | function name, then it will be empty. |
| 656 | FUNCTION\_OFFSET the hexadecimal offset from the beginning of the function. If |
| 657 | the FUNCTION\_NAME is empty, then this value will always be zero. |
| 658 | |
| 659 | An example of this new format: |
| 660 | |
| 661 | z 0 sz 400 num 1 bt a2a0 b510 |
| 662 | bt_info {"/system/libc.so" 2a0 "abort" 24} {"/system/libutils.so" 510 "" 0} |
| 663 | |
| 664 | In this example, the first backtrace frame has a pc of 0xa2a0 and is in the |
| 665 | map named /system/libc.so which starts at 0xa000. The relative pc is 0x2a0, |
| 666 | and it is in the function abort + 0x24. |
| 667 | The second backtrace frame has a pc of 0xb510 and is in the map named |
| 668 | /system/libutils.so which starts at 0xb000. The relative pc is 0x510 and |
| 669 | it is in an unknown function. |
| 670 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 671 | Examples |
| 672 | ======== |
Elliott Hughes | 644275a | 2017-08-15 23:17:35 -0700 | [diff] [blame] | 673 | |
| 674 | ### For platform developers |
| 675 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 676 | Enable backtrace tracking of all allocation for all processes: |
| 677 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 678 | adb shell stop |
| 679 | adb shell setprop libc.debug.malloc.options backtrace |
| 680 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 681 | |
| 682 | Enable backtrace tracking for a specific process (ls): |
| 683 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 684 | adb shell setprop libc.debug.malloc.options backtrace |
| 685 | adb shell setprop libc.debug.malloc.program ls |
| 686 | adb shell ls |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 687 | |
| 688 | Enable backtrace tracking for the zygote and zygote based processes: |
| 689 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 690 | adb shell stop |
| 691 | adb shell setprop libc.debug.malloc.program app_process |
| 692 | adb shell setprop libc.debug.malloc.options backtrace |
| 693 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 694 | |
Mikhail Naganov | 5a1a953 | 2018-01-03 08:50:16 -0800 | [diff] [blame] | 695 | Enable multiple options (backtrace and guard): |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 696 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 697 | adb shell stop |
Mikhail Naganov | 5a1a953 | 2018-01-03 08:50:16 -0800 | [diff] [blame] | 698 | adb shell setprop libc.debug.malloc.options "\"backtrace guard\"" |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 699 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 700 | |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 701 | Note: The two levels of quoting in the adb shell command is necessary. |
| 702 | The outer layer of quoting is for the shell on the host, to ensure that the |
Mikhail Naganov | 5a1a953 | 2018-01-03 08:50:16 -0800 | [diff] [blame] | 703 | inner layer of quoting is sent to the device, to make 'backtrace guard' |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 704 | a single argument. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 705 | |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 706 | Enable malloc debug using an environment variable (pre-O Android release): |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 707 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 708 | adb shell |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 709 | # setprop libc.debug.malloc.env_enabled 1 |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 710 | # setprop libc.debug.malloc.options backtrace |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 711 | # export LIBC_DEBUG_MALLOC_ENABLE=1 |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 712 | # ls |
Christopher Ferris | ac66d16 | 2016-09-28 14:51:12 -0700 | [diff] [blame] | 713 | |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 714 | Enable malloc debug using an environment variable (API level 26 or later): |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 715 | |
| 716 | adb shell |
| 717 | # export LIBC_DEBUG_MALLOC_OPTIONS=backtrace |
| 718 | # ls |
| 719 | |
| 720 | Any process spawned from this shell will run with malloc debug enabled |
| 721 | using the backtrace option. |
Christopher Ferris | ac66d16 | 2016-09-28 14:51:12 -0700 | [diff] [blame] | 722 | |
| 723 | adb shell stop |
| 724 | adb shell setprop libc.debug.malloc.options backtrace |
| 725 | adb shell start |
| 726 | adb shell am dumpheap -n <PID_TO_DUMP> /data/local/tmp/heap.txt |
| 727 | |
| 728 | It is possible to use the backtrace\_enable\_on\_signal option as well, |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 729 | but, obviously, it must be enabled through the signal before the file will |
| 730 | contain any data. |
| 731 | |
Elliott Hughes | 644275a | 2017-08-15 23:17:35 -0700 | [diff] [blame] | 732 | ### For app developers |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 733 | |
Elliott Hughes | 5ad1421 | 2018-04-06 15:13:14 -0700 | [diff] [blame] | 734 | App developers should check the NDK documentation about |
| 735 | [wrap.sh](https://developer.android.com/ndk/guides/wrap-script.html) |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 736 | for the best way to use malloc debug in API level 26 or later on non-rooted |
Elliott Hughes | 5ad1421 | 2018-04-06 15:13:14 -0700 | [diff] [blame] | 737 | devices. |
| 738 | |
Christopher Ferris | 8cbba80 | 2022-03-02 13:23:31 -0800 | [diff] [blame] | 739 | **NOTE**: Android 12 introduced a bug that can cause the wrap.\<APP\> property to |
| 740 | no longer work. Use the commands below so that the wrap.\<APP\> instructions will work: |
| 741 | |
| 742 | adb shell setprop dalvik.vm.force-java-zygote-fork-loop true |
| 743 | adb shell stop |
| 744 | adb shell start |
| 745 | |
Elliott Hughes | 5ad1421 | 2018-04-06 15:13:14 -0700 | [diff] [blame] | 746 | If you do have a rooted device, you can enable malloc debug for a specific |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 747 | program/application (API level 26 or later): |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 748 | |
| 749 | adb shell setprop wrap.<APP> '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"' |
| 750 | |
Christopher Ferris | 1d52a7b | 2018-06-01 13:40:38 -0700 | [diff] [blame] | 751 | If you need to enable multiple options using this method, then you can set |
| 752 | them like so: |
| 753 | |
| 754 | adb shell setprop wrap.<APP> '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace\ leak_track\ fill logwrapper"' |
| 755 | |
Elliott Hughes | 55060f0 | 2025-02-04 09:47:24 -0800 | [diff] [blame] | 756 | For example, to enable malloc debug for the google search box (API level 26 or later): |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 757 | |
| 758 | adb shell setprop wrap.com.google.android.googlequicksearchbox '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"' |
| 759 | adb shell am force-stop com.google.android.googlequicksearchbox |
| 760 | |
Christopher Ferris | ad935c8 | 2018-08-16 15:59:48 -0700 | [diff] [blame] | 761 | If you are setting multiple options and the app does not appear to start |
| 762 | properly, check the logcat looking for this message |
| 763 | (`adb logcat -d | grep "malloc debug"`): |
| 764 | |
| 765 | 08-16 15:54:16.060 26947 26947 I libc : /system/bin/app_process64: malloc debug enabled |
| 766 | |
| 767 | If you do not see this message, then the wrap property was not set correctly. |
| 768 | Run: |
| 769 | |
| 770 | adb shell getprop | grep wrap |
| 771 | |
| 772 | And verify that any spaces are properly escaped. |
| 773 | |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 774 | NOTE: On pre-O versions of the Android OS, property names had a length limit |
| 775 | of 32. This meant that to create a wrap property with the name of the app, it |
| 776 | was necessary to truncate the name to fit. On O, property names can be |
| 777 | an order of magnitude larger, so there should be no need to truncate the name |
| 778 | at all. |
Elliott Hughes | 644275a | 2017-08-15 23:17:35 -0700 | [diff] [blame] | 779 | |
| 780 | To detect leaks while an app is running: |
| 781 | |
| 782 | adb shell dumpsys meminfo --unreachable <PID_OF_APP> |
| 783 | |
| 784 | Without also enabling malloc debug, this command will only tell |
| 785 | you whether it can detect leaked memory, not where those leaks are |
| 786 | occurring. If you enable malloc debug with the backtrace option for your |
| 787 | app before running the dumpsys command, you'll get backtraces showing |
| 788 | where the memory was allocated. |
| 789 | |
| 790 | For backtraces from your app to be useful, you'll want to keep the |
| 791 | symbols in your app's shared libraries rather than stripping them. That |
| 792 | way you'll see the location of the leak directly without having to use |
| 793 | something like the <code>ndk-stack</code> tool. |