blob: a904767d06409bbeb5bcbc8d559302371ab42481 [file] [log] [blame] [view]
Robert Sesek92b004c2021-04-08 20:12:341# Debugging Chromium on macOS
2
3[TOC]
4
Robert Sesek3b731b12021-04-14 21:54:035## Debug vs. Release Builds
Robert Sesek92b004c2021-04-08 20:12:346
Robert Sesek3b731b12021-04-14 21:54:037Debug builds are the default configuration for Chromium and can be explicitly
8specified with `is_debug=true` in the `args.gn` file of the out directory. Debug
9builds are larger and non-portable because they default to
10`is_component_build=true`, but they contain full debug information.
Robert Sesek92b004c2021-04-08 20:12:3411
Robert Sesek3b731b12021-04-14 21:54:0312If you set `is_debug=false`, a release build will be created with no symbol
13information, which cannot be used for effective debugging.
Robert Sesek92b004c2021-04-08 20:12:3414
Robert Sesek3b731b12021-04-14 21:54:0315A middle-ground is to set `symbol_level=1`, which will produce a minimal symbol
16table, capable of creating backtraces, but without frame-level local variables.
17This is faster to build than a debug build, but it is less useful for debugging.
Robert Sesek92b004c2021-04-08 20:12:3418
Robert Sesek3b731b12021-04-14 21:54:0319When doing an `is_official_build=true` build (which is meant for producing
20builds with full compiler optimization suitable for shipping to users),
21`enable_dsyms` and `enable_stripping` both get set to `true`. The binary itself
22will be stripped of its symbols, but the debug information will be saved off
23into a dSYM file. Producing a dSYM is rather slow, so it is uncommon for
24developers to build with this configuration.
Robert Sesek92b004c2021-04-08 20:12:3425
Robert Sesek3b731b12021-04-14 21:54:0326### Chrome Builds
Robert Sesek92b004c2021-04-08 20:12:3427
Robert Sesek3b731b12021-04-14 21:54:0328The official Google Chrome build has published dSYMs that can be downloaded with
29the script at `tools/mac/download_symbols.py` or by using the LLDB integration
30at `tools/lldb/lldb_chrome_symbols.py`.
Robert Sesek92b004c2021-04-08 20:12:3431
Robert Sesek3b731b12021-04-14 21:54:0332However, the official Chrome build is
33[codesigned](../../chrome/installer/mac/signing/README.md) with the `restrict`
34and `runtime` options, which generally prohibit debuggers from attaching.
Robert Sesek92b004c2021-04-08 20:12:3435
Robert Liao45bc0532025-06-03 00:35:3636In order to debug production/released Chrome, you need to do one of two things
37while Chrome is not running:
Robert Sesek92b004c2021-04-08 20:12:3438
Robert Sesek3b731b12021-04-14 21:54:03391. Disable [System Integrity
40Protection](https://developer.apple.com/documentation/security/disabling_and_enabling_system_integrity_protection),
41by:
42 1. Rebooting into macOS recovery mode
43 2. Launching Terminal
44 3. Running `csrutil enable --without debug`
45 4. Rebooting
462. Stripping or force-re-codesigning the binary to not use those options:
Robert Liao45bc0532025-06-03 00:35:3647 `codesign --sign=- --deep --force path/to/Google\ Chrome.app`
Robert Sesek92b004c2021-04-08 20:12:3448
Robert Sesek3b731b12021-04-14 21:54:0349If you will frequently debug official builds, (1) is recommended. Note that
50disabling SIP reduces the overall security of the system, so your system
51administrator may frown upon it.
Robert Sesek92b004c2021-04-08 20:12:3452
Robert Sesek3b731b12021-04-14 21:54:0353## The Debugger
Robert Sesek92b004c2021-04-08 20:12:3454
Robert Sesek3b731b12021-04-14 21:54:0355The debugger on macOS is `lldb` and it is included in both a full Xcode install
56and the Command Line Tools package. There are two ways to use LLDB: either
57launching Chromium directly in LLDB, or attaching to an existing process:
Robert Sesek92b004c2021-04-08 20:12:3458
Robert Sesek3b731b12021-04-14 21:54:0359 lldb ./out/debug/Chromium.app/Contents/MacOS/Chromium
60 lldb -p <pid>
Robert Sesek92b004c2021-04-08 20:12:3461
Robert Sesek3b731b12021-04-14 21:54:0362LLDB has an extensive help system which you can access by typing `help` at the
63`(lldb)` command prompt. The commands are organized into a functional hierarchy,
64and you can explore the subcommands via `(lldb) help breakpoint`, etc. Commands
65can take arguments in a command-line flag style. Many commands also have short
66mnemonics that match the `gdb` equivalents. You can also just use enough letters
67to form a unique prefix of the command hierarchy. E.g., these are equivalent:
Robert Sesek92b004c2021-04-08 20:12:3468
Robert Sesek3b731b12021-04-14 21:54:0369 (lldb) help breakpoint set
70 (lldb) h br s
Robert Sesek92b004c2021-04-08 20:12:3471
Robert Sesek3b731b12021-04-14 21:54:0372When the program is running, you can use **Ctrl-C** to interrupt it and pause
73the debugger.
Robert Sesek92b004c2021-04-08 20:12:3474
Robert Sesek3b731b12021-04-14 21:54:0375### Passing Arguments
Robert Sesek92b004c2021-04-08 20:12:3476
Robert Sesek3b731b12021-04-14 21:54:0377To pass arguments to LLDB when starting Chromium, use a `--`:
Robert Sesek92b004c2021-04-08 20:12:3478
Robert Sesek3b731b12021-04-14 21:54:0379 lldb ./out/debug/Chromium.app/contents/MacOS/Chromium -- --renderer-startup-dialog
Robert Sesek92b004c2021-04-08 20:12:3480
Robert Sesek3b731b12021-04-14 21:54:0381### Breakpoints
Robert Sesek92b004c2021-04-08 20:12:3482
Robert Sesek3b731b12021-04-14 21:54:0383Simple function-name breakpoints can be specified with a short mnemonic:
Robert Sesek92b004c2021-04-08 20:12:3484
Robert Sesek3b731b12021-04-14 21:54:0385 (lldb) b BrowserWindow::Close
Robert Sesek92b004c2021-04-08 20:12:3486
Robert Sesek3b731b12021-04-14 21:54:0387But there are a range of other options for setting breakpoints using the, such
88as:
89* `-t` to limit the breakpoint to a specific thread
90* `-s` to specify a specific shared library, if the same symbol name is exported
91 by multiple libraries
92* `-o` for a one-shot breakpoint (delete after first hit)
Robert Sesek92b004c2021-04-08 20:12:3493
Robert Sesek3b731b12021-04-14 21:54:0394See `(lldb) help br set` for full details.
Robert Sesek92b004c2021-04-08 20:12:3495
Robert Sesek3b731b12021-04-14 21:54:0396### Navigating the Stack
Robert Sesek92b004c2021-04-08 20:12:3497
Robert Sesek3b731b12021-04-14 21:54:0398When the debugger is paused, you can get a backtrace by typing `bt`. To navigate
99the stack by-1 type either `up` or `down`. You can also jump to a specific index
100in the stack by typing `f #` (short for `frame select #`).
101
102To see local variables, type `v` (short for `frame variable`).
103
104### Examining Execution
105
106To step through the program, use:
107
108* `s` or `si` for step-in
109* `n` for step-over
110* `c` to continue (resume or go to next breakpoint)
111
112### Printing Values
113
114To print values, use the `p <value>` command, where `<value>` can be a variable,
115a variable expression like `object->member_->sub_member_.value`, or an address.
116
117If `<value>` is a pointer to a structure, `p <value>` will usually just print
118the address. To show the contents of the structure, dereference the value. E.g.:
119
120 (lldb) p item
121 (HistoryMenuBridge::HistoryItem *) $3 = 0x0000000245ef5b30
122 (lldb) p *item
123 (HistoryMenuBridge::HistoryItem) $4 = {
124 title = u"Google"
125 url = {
126 spec_ = "https://www.google.com/"
127 is_valid_ = true
128
129
130Note above that LLDB has also stored the results of the expressions passed to
131`p` into the variables `$3` and `$4`, which can be referenced in other LLDB
132expressions.
133
134Often (and always when printing addresses) there is not type information to
135enable printing the full structure of the referenced memory. In these cases, use
136a C-style cast:
137
138 (lldb) p 0x0000000245ef5b30 # Does not have type information
139 (long) $5 = 9763248944
140 (lldb) p (HistoryMenuBridge::HistoryItem*)0x0000000245ef5b30
141 (HistoryMenuBridge::HistoryItem *) $6 = 0x0000000245ef5b30
142 (lldb) p *$6
143 (HistoryMenuBridge::HistoryItem) $7 = {
144 title = u"Google"
145
146
147* For printing Cocoa NSObjects, use the `po` command to get the `-[NSObject description]`.
148* If `uptr` is a `std::unique_ptr`, the address it wraps is accessible as
149 `uptr.__ptr_.__value_`.
150* To pretty-print `std::u16string`, follow the instructions [here](../lldbinit.md).
151
152## Multi-Process Debugging
153
154Chrome is split into multiple processes, which can mean that the logic you want
155to debug is in a different process than the main browser/GUI process. There are
156a few ways to debug the multi-process architecture, discussed below.
157
158### (a) Attach to a Running Process
159
160You can use Chrome's Task Manager to associate specific sites with their PID.
161Then simply attach with LLDB:
162
163 lldb -p <pid>
164
165Or, if you have already been debugging a Chrome process and want to retain your
166breakpoints:
167
168 (lldb) attach <pid>
169
170### (b) Debug Process Startup
171
172If you need to attach early in the child process's lifetime, you can use one of
173these startup-dialog switches for the relevant process type:
174
175* `--renderer-startup-dialog`
176* `--utility-startup-dialog`
Alex Gough20926742021-05-13 20:11:30177* `--utility-startup-dialog=data_decoder.mojom.DataDecoderService`
Robert Sesek3b731b12021-04-14 21:54:03178
179After the process launches, it will print a message like this to standard error:
180
181 [80156:775:0414/130021.862239:ERROR:content_switches_internal.cc(112)] Renderer (80156) paused waiting for debugger to attach. Send SIGUSR1 to unpause.
182
183Then attach the the process like above in **(a)**, using the PID in parenthesis
184(e.g. *80156* above).
185
186### (c) Run Chrome in a single process
187
188> This option is not recommended. Single-process mode is not tested and is
189> frequently broken.
190
191Chrome has an option to run all child processes as threads inside a single
192process, using the `--single-process` command line flag. This can make debugging
193easier.
194
195## Debugging Out-of-Process Tests:
196
197Similar to debugging the renderer process, simply attaching LLDB to a
Robert Sesek92b004c2021-04-08 20:12:34198out-of-process test like browser\_tests will not hit the test code. In order to
199debug a browser test, you need to run the test binary with  "`--single_process`"
200(note the underscore in `single_process`). Because you can only run one browser
201test in the same process, you're probably going to need to add `--gtest_filter`
202as well. So your command will look like this:
203
Robert Sesek3b731b12021-04-14 21:54:03204 /path/to/src/out/debug/browser_tests --single_process --gtest_filter=GoatTeleporterTest.DontTeleportSheep
Robert Sesek92b004c2021-04-08 20:12:34205
Robert Sesek3b731b12021-04-14 21:54:03206## Working with Xcode
Robert Sesek92b004c2021-04-08 20:12:34207
Robert Sesek3b731b12021-04-14 21:54:03208If you'd prefer to use Xcode GUI to use the debugger, there are two options:
Robert Sesek92b004c2021-04-08 20:12:34209
Robert Sesek3b731b12021-04-14 21:54:03210### (1) Empty Xcode Project
Robert Sesek92b004c2021-04-08 20:12:34211
Robert Sesek3b731b12021-04-14 21:54:03212This approach creates an empty Xcode project that only provides a GUI debugger:
Robert Sesek92b004c2021-04-08 20:12:34213
Robert Sesek3b731b12021-04-14 21:54:032141. Select **File** > **New** > **Project...** and make a new project. Dump it
215 anywhere, call it anything. It doesn't matter.
2162. Launch Chromium.
2173. In Xcode, select **Debug** > **Attach to Process** > *Chromium*.
2184. You can now pause the process and set breakpoints. The debugger will also
219 activate if a crash occurs.
Robert Sesek92b004c2021-04-08 20:12:34220
Robert Sesek3b731b12021-04-14 21:54:03221### (2) Use *gn*
Robert Sesek92b004c2021-04-08 20:12:34222
Robert Sesek3b731b12021-04-14 21:54:032231. Tell `gn` to generate an Xcode project for your out directory:
Fumitoshi Ukaia9d3d3a2025-05-07 08:51:50224 `gn gen --ide=xcode out/debug --ninja-executable=autoninja`
Robert Sesek3b731b12021-04-14 21:54:032252. Open *out/debug/all.xcodeproj*
2263. Have it automatically generate schemes for you
2274. You can now build targets from within Xcode, which will simply call out to
Fumitoshi Ukaia9d3d3a2025-05-07 08:51:50228 `autoninja` via an Xcode script. But the resulting binaries are available as
Robert Sesek3b731b12021-04-14 21:54:03229 debuggable targets in Xcode.
Robert Sesek92b004c2021-04-08 20:12:34230
Robert Sesek3b731b12021-04-14 21:54:03231Note that any changes to the .xcodeproj will be overwritten; all changes to the
232build system need to be done in GN.
Robert Sesek92b004c2021-04-08 20:12:34233
Robert Sesek3b731b12021-04-14 21:54:03234## Debugging the Sandbox
Robert Sesek92b004c2021-04-08 20:12:34235
Robert Sesek3b731b12021-04-14 21:54:03236See the page on [sandbox debugging](sandbox_debugging.md).
Robert Sesek92b004c2021-04-08 20:12:34237
Robert Sesek3b731b12021-04-14 21:54:03238## System Permission Prompts; Transparency, Consent, and Control (TCC)
Robert Sesek92b004c2021-04-08 20:12:34239
Robert Sesek3b731b12021-04-14 21:54:03240When debugging issues with OS-mediated permissions (e.g. Location, Camera,
241etc.), you need to launch Chromium with LaunchServices rather than through a
242shell. If you launch Chromium as a subprocess of your terminal shell, the
243permission requests get attributed to the terminal app rather than Chromium.
Robert Sesek92b004c2021-04-08 20:12:34244
Robert Sesek3b731b12021-04-14 21:54:03245To launch Chromium via launch services, use the `open(1)` command:
Robert Sesek92b004c2021-04-08 20:12:34246
Robert Sesek3b731b12021-04-14 21:54:03247 open ./out/debug/Chromium.app
Robert Sesek92b004c2021-04-08 20:12:34248
Robert Sesek3b731b12021-04-14 21:54:03249To pass command line arguments:
Robert Sesek92b004c2021-04-08 20:12:34250
Robert Sesek3b731b12021-04-14 21:54:03251 open ./out/debug/Chromium.app -- --enable-features=MyCoolFeature
Robert Sesek92b004c2021-04-08 20:12:34252
253## Taking CPU Samples
254
255A quick and easy way to investigate slow or hung processes is to use the sample
256facility, which will generate a CPU sample trace. This can be done either in the
257Terminal with the sample(1) command or by using Activity Monitor:
258
Robert Sesek3b731b12021-04-14 21:54:032591. Open Activity Monitor
2602. Find the process you want to sample (for "Helper" processes, you may want to
261 consult the Chrome Task Manager)
2623. Double-click on the row
2634. Click the **Sample** button in the process's information window
Robert Sesek92b004c2021-04-08 20:12:34264
265After a few seconds, the sample will be completed. For official Google Chrome
266builds, the sample should be symbolized using
267[crsym](https://goto.google.com/crsym/). If you do not have access to crsym,
268save the *entirecontents as a file and attach it to a bug report for later
269analysis.
270
271See also [How to Obtain a Heap
272Dump](../memory-infra/heap_profiler.md#how-to-obtain-a-heap-dump-m66_linux_macos_windows).
Robert Sesek3b731b12021-04-14 21:54:03273
Keren Zhuf3ba98be6a2021-09-29 17:09:31274## Profiling using Instruments Time Profiler
275
276For more sophisticated CPU sampling, use the Time Profiler tool from Instruments. Instruments is macOS's performance analysis toolkit that comes with Xcode. The following steps assume that you've installed Xcode 12.0 or later.
277
278After installing Xcode, run `xcode-select -s XCODE_FOLDER` from the command line to set up the Xcode folder for the command line tools. `XCODE_FOLDER` is where the Xcode is installed, and should be something like `/Applications/Xcode.app/Contents/Developer`.
279
280Time Profiler provides a GUI to record traces, but is likely to be janky. To generate a trace from the terminal,
281
2821. Start Chrome.
2832. Run `xcrun xctrace record --template 'Time Profiler' --all-processes` in terminal to start tracing.
2843. Perform actions that you want to profile, e.g. open a new tab page.
2854. Back to the terminal, press Ctrl+C to terminate the profiling. A `.trace` profile file will be generated at the current working path.
286
287In Step 2, if you know which process you are looking at, you can change the `--all-processes` to `--attach PID`.
288
289To visualize a trace,
290
2911. In Time Profiler, load the profile file by File > Open. You will see the traces of all threads and other stats like the Thermal State. You may filter the threads of your interest.
2922. For official Google Chrome builds, you need to follow [this doc](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/mac/debugging.md#chrome-builds) to download the dSYMs files for symbolization.
2933. In Time Profiler, use File > Symbols to load symbols. Loading symbols only for the main executable (Google Chrome) should be sufficient for other executables (Google Chrome Helper and Renderer) as well.
294
Robert Sesek3b731b12021-04-14 21:54:03295## Working with Minidumps
296
297[See this
298page.](https://sites.google.com/a/chromium.org/dev/developers/crash-reports)
299
300## Disabling ReportCrash
301
302macOS helpfully tries to write a crash report every time a binary crashes
303which happens for example when a test in unit\_tests fails. Since Chromium's
304debug binaries are huge, this takes forever. If this happens, "ReportCrash" will
305be the top cpu consuming process in Activity Monitor. You should disable
306ReportCrash while you work on Chromium. Run `man ReportCrash` to learn how to do
307this on your version of macOS. On 10.15, the command is
308
309 launchctl unload -w /System/Library/LaunchAgents/com.apple.ReportCrash.plist
310 sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.ReportCrash.Root.plist
311
312Yes, you need to run this for both the normal user and the admin user.
313
314## Processing Apple Crash Reports
315
316If you get a Google Chrome crash report caught by ReportCrash/macOS, it will not
317have symbols (every frame will be ChromeMain). To get a symbolized stack trace,
318use the internal [crsym](httsp://goto.google.com/crsym) tool by simply pasting
319the contents of an entire Apple crash report.
320
321## Testing Other Locales
322
323To test Chrome in a different locale, change your system locale via the System
324Preferences. (Keep the preferences window open so that you can change the
325locale back without needing to navigate through menus in a language you may not
326know.)
327
328## Using DTrace
329
330DTrace is a powerful, kernel-level profiling and dynamic tracing utility. In
331order to use DTrace, you need to (at least partially) disable System Integrity
332Protection with ([see above](#Chrome-Builds)):
333
334 csrutil enable --without dtrace
335
336Using DTrace is beyond the scope of this document, but the following resources
337are useful:
338
339* [jgm's awesome introductory article](http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html)
340* [Big Nerd Ranch's four-part series](https://www.bignerdranch.com/blog/hooked-on-dtrace-part-1/)
341* [Defining static probes on macOS](http://www.macresearch.org/tuning-cocoa-applications-using-dtrace-custom-static-probes-and-instruments)
342* [Examples](http://www.brendangregg.com/dtrace.html#Examples)
343* [Tips from Sun](http://blogs.sun.com/bmc/resource/dtrace_tips.pdf)
344
345DTrace examples on macOS: `/usr/share/examples/DTTk`
346
347To get truss on macOS, use dtruss. That requires root, so use `sudo dtruss -p`
348and to attach to a running non-root program.
349
350## Memory/Heap Inspection
351
352Chrome has [built-in memory instrumentation](../memory-infra/README.md) that can
353be used to identify allocations and potential leaks.
354
355MacOS also provides several low-level command-line tools that can be used to
Sunny Sachanandanib7a2bf42024-06-03 13:28:46356inspect what's going on with memory inside a process. Note that most of these
357tools only work effectively with system malloc and not PartitionAlloc. Since
358[PartitionAlloc Everywhere](https://docs.google.com/document/d/1R1H9z5IVUAnXJgDjnts3nTJVcRbufWWT9ByXLgecSUM/preview),
359you should additionally disable PartitionAlloc with these GN args:
Robert Sesek3b731b12021-04-14 21:54:03360
Sunny Sachanandanib7a2bf42024-06-03 13:28:46361```
362use_partition_alloc_as_malloc = false
363enable_backup_ref_ptr_support = false
364```
365
366Note that PartitionAlloc will still be used in Blink, just not for `malloc` in
367other places anymore. See [PartitionAlloc build config](../../base/allocator/partition_allocator/build_config.md)
368for disabling PartitionAlloc completely via GN arg `use_partition_alloc`.
369
370**`heap`** summarizes what's currently in the malloc heap(s) of a process. It
Robert Sesek3b731b12021-04-14 21:54:03371shows a number of useful things:
372
373* How much of the heap is used or free
374* The distribution of block sizes
375* A listing of every C++, Objective-C and CoreFoundation class found in the
376 heap, with the number of instances, total size and average size.
377
378It identifies C++ objects by their vtables, so it can't identify vtable-less
379classes, including a lot of the lower-level WebCore ones like StringImpl. To
380work around, temporarily added the `virtual` keyword to `WTF::RefCounted`'s
381destructor method, which forces every ref-counted object to include a vtable
382pointer identifying its class.
383
384**`malloc_history`** identifies the stack backtrace that allocated every malloc
385block in the heap. It lists every unique backtrace together with its number of
386blocks and their total size. It requires that the process use malloc stack
387logging, which is enabled if the environment variable MallocStackLogging is set
388when it launches. The `env` command is handy for this:
389
390 $ env MallocStackLogging=1 Chromium.app/Contents/MacOS/Chromium
391
392Then in another shell you run
393
Sunny Sachanandanib7a2bf42024-06-03 13:28:46394 $ malloc_history <pid> -allBySize
Robert Sesek3b731b12021-04-14 21:54:03395
396Watch out: the output is *big*.
397
398**`leaks`** finds malloc blocks that have no pointers to them and are probably
399leaked. It doesn't require MallocStackLogging, but it's more useful if it's on
400because it can then show the backtrace that allocated each leaked block.
401
402**`vmmap`** shows all the virtual-memory regions in the process's address space.
403This is less useful since it doesn't say anything about individual malloc blocks
404(except huge ones) but it can be useful for looking at things like static data
405size, mapped files, and how much memory is paged out. The "-resident" flag shows
406how much of each allocation is currently paged into RAM. See the man page for
407details.
408
409Notes:
410
411* These are not going to be very useful on stripped binaries, and they're less
412 useful in release builds.
413* All of these except vmmap take several *minutes* to run, apparently because
414 of the number of symbols in Chrome. They spend most of their time pegging
415 one CPU down inside system code that's reading symbol tables from the
416 binary. Be patient.
417* Instruments is an application bundled with Xcode that provides GUI interfaces
418 for many of these tools, including `sample` and `leaks`. Most Chromies prefer
419 the command line tools, but Instruments can be useful. If running Instruments
420 on a local build, expect to wait a few minutes for it to load symbols before
421 it starts recording useful data
422
423## Resources
424
425The [Mac OS X Debugging Magic
426Technote](https://developer.apple.com/technotes/tn2004/tn2124.html) contains a
427wealth of (mostly outdated) information about various debugging options built in
428to macOS.