Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 1 | # Use Qt Creator as an IDE or debugger UI |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 2 | |
| 3 | [Qt Creator](https://www.qt.io/ide/) |
chaopeng | 6122d4c | 2017-09-01 19:40:05 | [diff] [blame] | 4 | ([Wiki](https://en.wikipedia.org/wiki/Qt_Creator)) is a cross-platform C++ IDE. |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 5 | |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 6 | You can use Qt Creator as a daily IDE on Linux or Mac, or just as a GDB/LLDB |
| 7 | frontend (which does not require project configuration). |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 8 | |
| 9 | [TOC] |
| 10 | |
| 11 | ## IDE |
| 12 | |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 13 | ### Workflow features |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 14 | |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 15 | * Built-in code completion. |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 16 | * Navigate to classes, files, or symbols with `ctrl+k` or `cmd+k` (macOS). |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 17 | * Switch between declaration and definition with `F2`. |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 18 | * Build with `ctrl+shift+b` or `shift+cmd+b` (macOS). |
| 19 | * Build and run with `ctrl+r` or `cmd+r` (macOS), or debug with `F5`. |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 20 | * Switch between the header file and cpp file with `F4`. |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 21 | |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 22 | ### Setup |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 23 | |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 24 | 1. Install the latest Qt Creator. |
| 25 | 2. Under chromium/src `gn gen out/Default --ide=qtcreator`. |
| 26 | 3. Start it with `qtcreator out/Default/qtcreator_project/all.creator`. |
| 27 | 4. Help - Plugins - check ClangCodeModel to enable std completion. |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 28 | |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 29 | It takes 3 minutes to parse all of chromium's C++ files on my workstation!!! And |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 30 | it does not block while parsing. |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 31 | |
| 32 | #### Code Style |
| 33 | |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 34 | 1. Help - About Plugins (or app menu on macOS), enable Beautifier. |
| 35 | 2. Tools - Options (Preferences on macOS) - Beautifier |
| 36 | Make sure to tick - Enable auto format on file save" |
| 37 | Select ClangFormat as the tool |
| 38 | Go to Clang Format tab |
| 39 | Change the Clang format command to: `$chromium_checkout_dir/src/buildtools/$os/clang-format`, and |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 40 | set `Use predefined style: file`. You can also set a keyboard shortcut |
| 41 | for it. |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 42 | 3. Tools - Options - C++ - Code Style, import this xml file. |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 43 | |
| 44 | ``` |
| 45 | <?xml version="1.0" encoding="UTF-8"?> |
| 46 | <!DOCTYPE QtCreatorCodeStyle> |
| 47 | <!-- Written by QtCreator 4.2.1, 2017-02-08T19:07:34. --> |
| 48 | <qtcreator> |
| 49 | <data> |
| 50 | <variable>CodeStyleData</variable> |
| 51 | <valuemap type="QVariantMap"> |
| 52 | <value type="bool" key="AlignAssignments">true</value> |
| 53 | <value type="bool" key="AutoSpacesForTabs">false</value> |
| 54 | <value type="bool" key="BindStarToIdentifier">false</value> |
| 55 | <value type="bool" key="BindStarToLeftSpecifier">false</value> |
| 56 | <value type="bool" key="BindStarToRightSpecifier">false</value> |
| 57 | <value type="bool" key="BindStarToTypeName">true</value> |
chaopeng | 6122d4c | 2017-09-01 19:40:05 | [diff] [blame] | 58 | <value type="bool" |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 59 | key="ExtraPaddingForConditionsIfConfusingAlign">true</value> |
| 60 | <value type="bool" key="IndentAccessSpecifiers">true</value> |
| 61 | <value type="bool" key="IndentBlockBody">true</value> |
| 62 | <value type="bool" key="IndentBlockBraces">false</value> |
| 63 | <value type="bool" key="IndentBlocksRelativeToSwitchLabels">false</value> |
| 64 | <value type="bool" key="IndentClassBraces">false</value> |
| 65 | <value type="bool" key="IndentControlFlowRelativeToSwitchLabels">true</value> |
| 66 | <value type="bool" |
| 67 | key="IndentDeclarationsRelativeToAccessSpecifiers">false</value> |
| 68 | <value type="bool" key="IndentEnumBraces">false</value> |
| 69 | <value type="bool" key="IndentFunctionBody">true</value> |
| 70 | <value type="bool" key="IndentFunctionBraces">false</value> |
| 71 | <value type="bool" key="IndentNamespaceBody">false</value> |
| 72 | <value type="bool" key="IndentNamespaceBraces">false</value> |
| 73 | <value type="int" key="IndentSize">2</value> |
| 74 | <value type="bool" key="IndentStatementsRelativeToSwitchLabels">true</value> |
| 75 | <value type="bool" key="IndentSwitchLabels">false</value> |
| 76 | <value type="int" key="PaddingMode">2</value> |
| 77 | <value type="bool" key="ShortGetterName">true</value> |
| 78 | <value type="bool" key="SpacesForTabs">true</value> |
| 79 | <value type="int" key="TabSize">2</value> |
| 80 | </valuemap> |
| 81 | </data> |
| 82 | <data> |
| 83 | <variable>DisplayName</variable> |
| 84 | <value type="QString">chrome</value> |
| 85 | </data> |
| 86 | </qtcreator> |
| 87 | ``` |
| 88 | |
| 89 | #### Build & Run |
| 90 | |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 91 | 1. (Optional) Enable the issues pane for easy navigation to the location of |
| 92 | compiler errors. qtcreator expects to find compiler errors on stderr, but |
| 93 | ninja forwards all subcommand output to stdout. So use the following wrapper |
| 94 | script to forward it to stderr instead. |
| 95 | ``` |
| 96 | #!/bin/sh |
| 97 | /path/to/depot_tools/ninja "$@" >&2 |
| 98 | ``` |
| 99 | 2. In the left panel - Projects, set up the ninja command in the build and |
| 100 | clean steps, and add the path to chrome in the run configuration. |
| 101 | |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 102 | |
| 103 | ## Debugger |
| 104 | |
chaopeng | 6122d4c | 2017-09-01 19:40:05 | [diff] [blame] | 105 | **You can skip the project settings and use QtCreator as a single file |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 106 | standalone GDB or LLDB (macOS) frontend.** |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 107 | |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 108 | For macOS : |
| 109 | 1. Open the file you want to debug. |
| 110 | 2. Debug - Start Debugging - Attach to running Application, you may need to |
| 111 | open chromium's task manager to find the process id. |
| 112 | |
| 113 | For Linux : |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 114 | 1. Tools - Options - Build & Run - Debuggers, make sure GDB is set. |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 115 | 2. Tools - Options - Kits, change the Desktop kit to GDB (LLDB doesn't work on |
| 116 | Linux). |
| 117 | 3. Open the file you want to debug. |
chaopeng | 6122d4c | 2017-09-01 19:40:05 | [diff] [blame] | 118 | 4. Debug - Start Debugging - Attach to running Application, you may need to |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 119 | open chromium's task manager to find the process id. |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 120 | |
| 121 | ### Tips, tricks, and troubleshooting |
| 122 | |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 123 | #### [Linux] The debugger exits immediately |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 124 | |
| 125 | Ensure yama allows you to attach to another process: |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 126 | |
| 127 | ``` |
| 128 | $ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope |
| 129 | ``` |
| 130 | |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 131 | |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 132 | #### [Linux] The debugger does not stop on breakpoints |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 133 | |
Chris Watkins | 7374d03 | 2017-09-27 01:46:39 | [diff] [blame] | 134 | Ensure you are using GDB on Linux, not LLDB. |
chaopeng | 585eef9e | 2017-02-09 15:21:45 | [diff] [blame] | 135 | |
| 136 | #### More |
| 137 | |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 138 | Linux : |
chaopeng | 6122d4c | 2017-09-01 19:40:05 | [diff] [blame] | 139 | See |
John Palmer | 046f987 | 2021-05-24 01:24:56 | [diff] [blame] | 140 | https://chromium.googlesource.com/chromium/src/+/main/docs/linux/debugging.md |
Alexis Menard | 701a565 | 2018-05-08 16:27:49 | [diff] [blame] | 141 | |
| 142 | macOS : |
Robert Sesek | 3b731b1 | 2021-04-14 21:54:03 | [diff] [blame] | 143 | https://chromium.googlesource.com/chromium/src/+/main/docs/mac/debugging.md |