Debian Codegraph @

docs/gdbserver.md

raw symbols SCIP
1 ---
2 title: gdb remote server
3 ---
4
5 The `pyocd gdbserver` subcommand runs a gdb remote serial protocol (RSP) server that allows gdb to debug embedded targets.
6
7 To run a debug session, the pyOCD gdbserver needs to first be started. Then gdb connects to pyOCD and debugging can begin.
8
9
10 ## Running the gdbserver
11
12 The usual way to start the gdbserver is by running `pyocd gdbserver` (or `pyocd gdb`) on the command line. The gdbserver will remain available as long as the `pyocd` process is running.
13
14 If using an IDE like Microsoft Visual Studio Code or Eclipse Embedded with plugins that support pyOCD, the gdbserver will automatically be started when you launch a debug session. Alternatively, the plugin can be configured to connect to an already-running gdbserver.
15
16 From the commander, a gdbserver can be run with the [`gdbserver` command]({% link _docs/command_reference.md %}#gdbserver-1).
17
18 The default gdbserver TCP/IP port number is 3333. This can be changed using the `-p` / `--port` or the `gdbserver_port` session option.
19
20 ### Multicore targets
21
22 pyOCD always runs separate gdbservers for each core on multicore devices. This requires separate gdb instances and debug sessions for the cores being debugged, but is the most reliable method since gdb doesn't support heterogenous multicore.
23
24 By default, a gdbserver is started for all cores on the target. This can be changed using the `--core` argument, which takes a comma-separated list of core numbers.
25
26 The TCP/IP port numbers for each core's gdbserver are determined by adding the core number to the base port number (see above). For a dual core device, core 0 uses the default base port of 3333, and core 1 uses port 3334.
27
28 See the [multicore debug documentation]({% link _docs/multicore_debug.md %}) for more information about multicore targets.
29
30
31 ## Connecting from gdb
32
33 To connect gdb to pyOCD's gdbserver, use the `target remote <host>:<port>` command. These take the server's host name and port number separated by a colon as an argument. For a server running on the same host as gdb, using the default pyOCD port (see above for how to change it), this will be `target remote localhost:3333`.
34
35 PyOCD also support extended remote mode. In this case, the connect command is `target extended-remote <host>:<port>`. Extended remote mode allows using the gdb `disconnect` command to disconnect from pyOCD while keeping the gdbserver running.
36
37 After connecting gdb, perform the following steps to program updated firmware and run the new code.
38
39 1. Program firmware using the `load` command.
40
41 2. Reset the target and halt with `monitor reset halt`. This halts the core at the first instruction.
42
43 3. Set breakpoints and resume, or use a command line `until main` to run to the first line of `main()`.
44
45
46 ## Gdbserver exit
47
48 The pyOCD gdbserver process will normally exit automatically when gdb detaches using any of the `detach`, `kill`, or `disconnect` commands. This can be changed with the `--persist` argument or `persist` session option, so that the gdbserver always remains running until terminated directly (by Control-C or equivalent signal).
49
50 When gdb connects in extended remote mode, the gdb `disconnect` command will detach gdb but keep the gdbserver running even if persist isn't enabled.
51
52
53 ## Useful commands
54
55 Gdb by default restricts memory accesses to regions defined in the target memory map provided by pyOCD. This has the unhappy side effect of preventing access to peripheral registers or other Device memory, since the memory maps do not include those regions.
56
57 To work around this, disable gbd's `mem inaccessible-by-default` setting.
58
59 This line can be added into your `.gdbinit` file:
60
61 (gdb) set mem inaccessible-by-default off
62
63 It can also be useful to add an alias to make monitor commands (below) easier to access:
64
65 (gdb) alias m = monitor
66
67 To catch crashes and unexpected exceptions, use [`set vector-catch`]({% link _docs/command_reference.md %}#vector-catch) to enable the M-profile vector catch feature:
68
69 (gdb) monitor set vector-catch all
70
71 If the core halts in an exception handler, use [`show fault`]({% link _docs/command_reference.md %}#fault) to print out the M-profile fault syndrome registers.
72
73 (gdb) monitor show fault
74
75 For most targets, peripheral registers can be accessed using pyOCD's [`reg`]({% link _docs/command_reference.md %}#reg) and [`wreg`]({% link _docs/command_reference.md %}#wreg) commands.
76
77 (gdb) monitor reg TIM21.PSC
78
79 Enabling access to all memory as described above is required for this to work.
80
81
82 ## Monitor commands
83
84 Commands can be sent directly to pyOCD using the gdb `monitor` command. Any output from pyOCD is returned through gdb and printed on the console. This is effectively the same as running the `pyocd commander` subcommand. All [pyOCD commands]({% link _docs/command_reference.md %}) are available.
85
86 pyOCD initial selects the core controlled by the gdbserver as the target for monitor commands. Similarly, the selected AP is initially set to the gdbserver's core's MEM-AP. PyOCD's [`core` command]({% link _docs/command_reference.md %}#core-1) can be used to select a different core. This might be useful in order to release a secondary core from reset before starting to debug it, although a [user script]({% link _docs/user_scripts.md %}) could be a better choice.
87
88
89 ## Semihosting and RTT
90
91 The gdbserver supports [semihosting]({% link _docs/semihosting.md %}), SEGGER RTT, and [Arm SWV]({% link _docs/swo_swv.md %}).
92
93
94 ## Caching
95
96 Several forms of caching are supported to improve performance when communicating with gdb.
97
98 Note that these caches are currently only used with gdb, not for pyOCD's commander interface or the `Target` Python API. (But they will be used for the `DebugContext` objects return from `SoCTarget.get_target_context()`, if that is used through the Python API.)
99
100 ### Reading memory from the ELF
101
102 If provided the firmware's ELF executable file with the `--elf` argument, pyOCD will read target memory contents present in the ELF from that file instead of reading from the target via SWD/JTAG. This can be faster, especially with slower debug probe connections or wire protocol speeds.
103
104 The `cache.read_code_from_elf` session option (bool) controls whether this feature is enabled. It's turned on by default, but of course requires the ELF to be passed to pyOCD. (Unfortunately, there is no way to access the executable through gdb.)
105
106 ### Memory and register cache
107
108 Caches for target memory and core register values are present and enabled by default. Both caches are invalidated every time the core is resumed or stepped.
109
110 The memory cache will cache any memory region marked as cacheable (all are by default). To disable caching for a memory region, a user script can be used to set its `is_cacheable` property to False.
111
112 For example, to disable caching of the "iram" region:
113
114 ```py
115 def will_connect():
116 target.memory_map.get_first_matching_region(name="iram").is_cacheable = False
117 ```
118
119 (Use the `show map` command to see the list of memory regions.)
120
121 These session options allow control over the memory and register caches. Both are enabled by default.
122
123 - `cache.enable_memory` (bool)
124 - `cache.enable_register` (bool)
125
126
127 ## RTOS thread awareness
128
129 The gdbserver supports thread awareness for several RTOSes. Additional RTOS support can be added with plugins.
130
131 When gdb connects, pyOCD will attempt to enable thread awareness. By default, all available RTOS plugins are queried. When the first one is successfully enabled, the process stops. If the `rtos.name` session option is set to the name of an RTOS plugin, only that one will be queried. To completely disable thread awareness, set the `rtos.enable` session option to false.
132
133 Builtin RTOS plugins are shown in the following table.
134
135 RTOS Plugin Name | Description
136 ---------------------|----------------
137 argon | Argon RTOS
138 freertos | FreeRTOS
139 rtx5 | RTX5
140 threadx | ThreadX
141 zephyr | Zephyr
142
143
144 ### Viewing and selecting threads
145
146 Within gdb, the set of current threads can be printed with `info threads`. gdb assigns each thread a unique integer identifier used to reference the thread in other commands.
147
148 This example shows a gdb thread listing for a Zephyr RTOS program.
149
150 <div class="highlight"><pre class="highlight"><code> Id Target Id Frame
151 * 3 Thread 536871784 "idle" (Running; Priority 15) <span style="color: yellow">arch_cpu_idle</span> ()
152 at <span style="color: green">/Users/creed/projects/zephyrproject/zephyr/arch/arm/core/aarch32/cpu_idle.S</span>:126
153 4 Thread 536871584 "uart_out_id" (Pending; Priority 7) <span style="color: yellow">arch_swap</span> (<span style="color: cyan">key</span>=0)
154 at <span style="color: green">/Users/creed/projects/zephyrproject/zephyr/arch/arm/core/aarch32/swap.c</span>:53
155 5 Thread 536871416 "blink1_id" (Suspended; Priority 7) <span style="color: yellow">arch_swap</span> (<span style="color: cyan">key=key@entry</span>=0)
156 at <span style="color: green">/Users/creed/projects/zephyrproject/zephyr/arch/arm/core/aarch32/swap.c</span>:53
157 6 Thread 536871248 "blink0_id" (Suspended; Priority 7) <span style="color: yellow">arch_swap</span> (<span style="color: cyan">key=key@entry</span>=0)
158 at <span style="color: green">/Users/creed/projects/zephyrproject/zephyr/arch/arm/core/aarch32/swap.c</span>:53
159 7 Thread 536872152 "sysworkq" (Pending; Priority -1) <span style="color: yellow">arch_swap</span> (<span style="color: cyan">key</span>=0)
160 at <span style="color: green">/Users/creed/projects/zephyrproject/zephyr/arch/arm/core/aarch32/swap.c</span>:53
161 </code></pre></div>
162
163 Note how the thread name is shown in quotes, and thread state and priority are shown in parentheses. The actual description of threads is specific to each RTOS plugin.
164
165 The `thread` command takes a gdb thread ID to switches between threads; after switching, `backtrace` will show the selected thread's state.
166
167 ### Thread reporting
168
169 There can be issues caused by a mismatch between the target's current memory contents and the expected location of RTOS related symbols. For instance, if a new version of firmware is being debugged but the target's flash has not been reprogrammed yet, or if there is stale data in RAM. If this happens, an out of date or corrupt view of the RTOS state could be reported.
170
171 To prevent this from happening, pyOCD will disable reporting threads to gdb (and reading the RTOS data from target memory) until the first time the target is resumed after any of these events:
172 - gdb connects to pyOCD
173 - Target reset
174 - Flash is reprogrammed using gdb commands
175
176 Note that a single instruction step will not enable thread reporting. A full resume is required. Gdb will see the list of threads when the target halts after the first resume, eg which a breakpoint is hit or the target is manually halted.
177
178 The actual behaviour depends on the plugin, so there is some slight variation between RTOSes. But the general sequence applies.
179
180 This logic not perfect, so there is a [`threads` command]({% link _docs/command_reference.md %}#threads-1) that provides manual control. It takes an argument with one of these actions:
181 - `status`: Show whether thread reporting is enabled.
182 - `enable`: Enable thread reporting.
183 - `disable`: Disallow thread reporting.
184 - `flush`: Forcibly invalidate the threads list.
185
186 If `threads` is executed and an RTOS plugin has not successfully loaded, it will print "Threads are unavailable".
187
188 After thread reporting is enabled or disabled manually, you must step or resume in gdb to force gdb to refresh its view of threads. (There is no flush or invalidate for threads in gdb.)
189
190 When thread reporting is disabled, gdb will see a single thread just like when no RTOS plugin is loaded.
191
192
193 ### RTOS notes
194
195 These sections document specific features or requirements for using thread awareness with different RTOSes.
196
197 #### Zephyr RTOS
198
199 To enable thread awareness, the `CONFIG_DEBUG_THREAD_INFO` Kconfig setting must be enabled.
200
201
202 ### Handler mode thread
203
204 The RTOS plugins will report an artificial thread when an M-profile core is in Handler mode, eg in an exception or interrupt handler. This lets you view the state of the exception handler separately from RTOS threads.
205
206