Debian Codegraph @

docs/semihosting.md

raw symbols SCIP
1 ---
2 title: Semihosting
3 ---
4
5 Semihosting is a technique for routing I/O requests performed on the target to the host. In addition to I/O, some other
6 operations are supported. Most commonly, this means integration of the standard C library with the host's console and
7 file system. It allows you to log debug messages before you have peripheral drivers fully functioning or on hardware
8 that does not have other debug output capabilities. It can be used to redirect `printf()` and other standard I/O to or
9 from the host, read keyboard input, access configuration files, transfer data to and from the target, and so on.
10
11 The primary downside to semihosting is that it can be quite slow, depending on how it is used and considerations such
12 as the debug probe's latency and SWD/JTAG clock speed.
13
14 The [Arm semihosting documentation](https://developer.arm.com/documentation/dui0471/i/semihosting/what-is-semihosting-?lang=en)
15 has more information about semihosting.
16
17
18 ### How it works
19
20 To implement semihosting, the debugger intercepts requests from code running on the target and performs
21 the specified action on the target's behalf. Figure 1 below shows how semihosting requests work.
22
23 ![](resources/semihosting.svg)\
24 **Figure 1**. Semihosting block diagram.
25
26 The figure demonstrates and example of how a C standard library call is made to `printf()`. This is routed to a
27 semihosting handler, which prepares the request parameters and triggers the debugger to perform the request, in this
28 case writing "hello world" to the console.
29
30 For Arm Cortex-M devices, a `BKPT` instruction with a parameter of 0xAB serves as the trigger detected by the debugger.
31
32
33 ### Enabling
34
35 To use semihosting with pyOCD, it first must be enabled. This can be done in several ways:
36
37 - Pass `-S`/`--semihosting` on the `pyocd gdbserver` command line.
38 - Set the `enable_semihosting` session option to true.
39 - From gdb, issue a `monitor arm semihosting enable` command. This command is supported for compatibility with OpenOCD.
40
41 Assuming the default routing options and telnet port number are acceptable, no further configuration is required. When
42
43 <div class="alert alert-info">
44 Currently semihosting only works when pyOCD's gdbserver is running and the target is resumed, with the gdbserver
45 waiting for it to halt. However, the <code>pyocd.debug.semihost.SemihostAgent</code> class is reusable if a custom
46 target control loop is implemented.
47 </div>
48
49
50 ### Routing
51
52 There several ways to route semihosting I/O requests in pyOCD, depending on the file on which the request is operating.
53
54 First, an I/O request is classified as either "console" or "syscall". Console requests are those that operate on the
55 first three file descriptions: stdin (0), stdout (1), and stderr (2). Certain semihosting requests are always console.
56 All other requests, which are always for explicitly opened files, are classified as syscalls.
57
58 Console requests have two options for routing, controlled with the `semihost_console_type` session option's value.
59
60 - "telnet": I/O is connected to the "telnet" server. (In fact, it's not a true telnet server because it doesn't implement
61 the telnet control requests.) This server runs on the port indicated by the `telnet_port` session option or the
62 `-T`/`--telnet-port` command line argument of the gdbserver subcommand.
63 - "console": pyOCD's standard I/O is used.
64
65 Syscall requests also have two routing options. The `semihost_use_syscalls` option controls this depending on whether it
66 is true or false.
67
68 - _false_: File I/O requests are handled by pyOCD itself, and therefore are performed on the system on which the pyOCD
69 process is running.
70 - _true_: File I/O is passed to gdb for handling.
71
72 Relative paths passed to the `SYS_OPEN` request are interpreted relative to the syscall handler's working directory.
73 In addition, if pyOCD and gdb are running on different systems, absolute paths will be interpreter according to the
74 appropriate system's root filesystem.
75
76 Defaults are for console to be routed to telnet and syscalls handled by gdb.
77
78
79 ### Building into firmware
80
81 These are the steps, in short, for how to include semihosting support when linking firmware with gcc
82 and newlib.
83
84 - On the linker command line, add `--specs=rdimon.specs` and ensure `-nostartfiles` is not present.
85 - Call `initialise_monitor_handles()` from your firmware before using semihosting.
86
87 The C runtime stdio will now be connected with semihosting.
88
89 This small example shows how to init and use semihosting from gcc.
90
91 ```c
92 #include <stdio.h>
93
94 extern void initialise_monitor_handles(void);
95
96 void main(void) {
97 // init semihosting console
98 initialise_monitor_handles();
99
100 // say hello via semihosting
101 printf("hello, world!\n");
102 }
103 ```
104
105 These blog articles are also very helpful guides for using semihosting with gcc:
106
107 - [Introduction to ARM Semihosting](https://interrupt.memfault.com/blog/arm-semihosting) is an excellent
108 overall introduction, including how to link in semihosting support for gcc and newlib-based projects.
109 - [Semihosting with GNU ARM Embedded (LaunchPad) and GNU ARM Eclipse Debug Plugins](https://mcuoneclipse.com/2014/09/11/semihosting-with-gnu-arm-embedded-launchpad-and-gnu-arm-eclipse-debug-plugins/)
110
111 For other toolchains, please see the vendor's documentation.
112
113
114 ### Trace logging
115
116 The semihosting agent supports a trace logger, `pyocd.debug.semihost.trace` that will output a log message every time a
117 semihosting request is processed. This can be enabled on the command line with `-Lpyocd.debug.semihost.trace=debug`.
118
119
120 ### Session options
121
122 These are the session options that control semihosting:
123
124 - `enable_semihosting` - Set to true to handle semihosting requests.
125 - `semihost_console_type` - If set to 'telnet' then the semihosting telnet server will be started. If set to 'console' then semihosting will print to pyOCD's console.
126 - `semihost.commandline` - Command line string return to the target for the `SYS_GET_CMDLINE` request.
127 - `semihost_use_syscalls` - Whether to use GDB syscalls for semihosting file access operations, or to have pyOCD perform the operations.)
128 - `telnet_port` - Base TCP port number for the semihosting telnet server. The core number, which will be 0 for the primary core, is added to this value.
129
130
131 ### Supported requests
132
133 The majority of standard Arm-defined semihosting requests are supported by pyOCD.
134
135 - `SYS_OPEN` (0x01): syscall
136 - The special file name `:tt` is used to open standard I/O files, per the Arm semihosting specification. The open
137 mode selects which standard I/O file is opened. "r" (0) is stdin, "w" (4) is stdout, "a" (8) is stderr. With pyOCD's
138 implementation, explicitly opening the standard I/O files is not required.
139 - The special file name `:semihosting-features` will open the semihosting feature bits file. Supported
140 feature bits are:
141 - `SH_EXT_EXIT_EXTENDED` (byte 0 bit 0): =0, meaning `SYS_EXIT_EXTENDED` is not supported
142 - `SH_EXT_STDOUT_STDERR` (byte 0 bit 1): =1, meaning both stdout and stderr are supported
143 - Standard I/O files opened via this request are only accessible when not routing console to the telnet server.
144 - `SYS_CLOSE` (0x02): syscall
145 - `SYS_WRITEC` (0x03): console
146 - `SYS_WRITE0` (0x04): console
147 - `SYS_WRITE` (0x05): console for stdout and stderr, otherwise syscall
148 - `SYS_READ` (0x06): console for stdin, otherwise syscall
149 - `SYS_READC` (0x07): console
150 - `SYS_ISTTY` (0x09): syscall
151 - `SYS_SEEK` (0x0a): syscall
152 - `SYS_FLEN` (0x0c): syscall
153 - `SYS_REMOVE` (0x0e): syscall
154 - `SYS_RENAME` (0x0f): syscall
155 - `SYS_CLOCK` (0x10): returns the number of centiseconds since pyOCD started (technically, since the semihosting agent
156 object was created, so this will not line up with timestamps in pyOCD's log output)
157 - `SYS_TIME` (0x11): returns the number of seconds since midnight, January 1, 1970
158 - `SYS_ERRNO` (0x13): syscall
159 - `SYS_GET_CMDLINE` (0x15): returns the value of the `semihost.commandline` session option. If that option
160 is not set, the request fails (returns -1).
161 - `SYS_HEAPINFO` (0x16): stubbed to return all 0 values
162
163
164 The following semihosting requests are not supported. If invoked, the return code is -1 and pyOCD logs a
165 warning message, such as "Semihost: unimplemented request pc=\<x> r0=\<y> r1=\<z>".
166
167 - `SYS_ISERROR` (0x08)
168 - `SYS_TMPNAM` (0x0d)
169 - `SYS_SYSTEM` (0x12)
170 - `angel_SWIreason_EnterSVC` (0x17)
171 - `SYS_EXIT` (0x18), also called `angel_SWIreason_ReportException`
172 - `SYS_EXIT_EXTENDED` (0x20)
173 - `SYS_ELAPSED` (0x30)
174 - `SYS_TICKFREQ` (0x31)
175
176 The [Arm semihosting operations](https://developer.arm.com/documentation/dui0471/i/semihosting/semihosting-operations?lang=en)
177 documentation has the full specification of each request.
178
179