µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
trace-semihosting.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2015-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#if defined(__clang__)
13#pragma clang diagnostic ignored "-Wempty-translation-unit"
14#elif defined(__GNUC__)
15#pragma GCC diagnostic ignored "-Wold-style-cast"
16#pragma GCC diagnostic ignored "-Wcast-qual"
17#pragma GCC diagnostic ignored "-Wuseless-cast"
18#endif
19
20// ----------------------------------------------------------------------------
21
22#if defined(__ARM_EABI__)
23
24// ----------------------------------------------------------------------------
25
26#if defined(TRACE)
27
28#if defined(OS_USE_OS_APP_CONFIG_H)
29#include <cmsis-plus/os-app-config.h>
30#endif
31
32#if defined(OS_USE_TRACE_SEMIHOSTING_DEBUG) \
33 || defined(OS_USE_TRACE_SEMIHOSTING_STDOUT)
34
36
37// ----------------------------------------------------------------------------
38
39#if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
40#error "Cannot debug semihosting using semihosting trace; use OS_USE_TRACE_ITM"
41#endif
42
44
45// ----------------------------------------------------------------------------
46
47namespace os
48{
49 namespace trace
50 {
51 // ------------------------------------------------------------------------
52
53 void
54 initialize (void)
55 {
56 // For semihosting, no inits are required.
57 }
58
59 // ------------------------------------------------------------------------
60
61 // Semihosting is another output channel that can be used for the trace
62 // messages. It comes in two flavours: STDOUT and DEBUG. The STDOUT channel
63 // is the equivalent of the stdout in POSIX and in most cases it is
64 // forwarded to the GDB server stdout stream. The debug channel is a
65 // separate channel. STDOUT is buffered, so nothing is displayed until
66 // a \n; DEBUG is not buffered, but can be slow.
67 //
68 // Choosing between semihosting stdout and debug depends on the
69 // capabilities of your GDB server, and also on specific needs. It is
70 // recommended to test DEBUG first, and if too slow, try STDOUT.
71 //
72 // The JLink GDB server fully support semihosting, and both configurations
73 // are available; to activate it, use "monitor semihosting enable" or check
74 // the corresponding button in the JLink Debugging plug-in.
75 // In OpenOCD, support for semihosting can be enabled using
76 // "monitor arm semihosting enable".
77 //
78 // Note: Applications built with semihosting output active normally cannot
79 // be executed without the debugger connected and active, since they use
80 // BKPT to communicate with the host. However, with a carefully written
81 // HardFault_Handler, the semihosting BKPT calls can be processed, making
82 // possible to run semihosting applications as standalone, without being
83 // terminated with hardware faults.
84
85 // ------------------------------------------------------------------------
86
87#if defined(OS_USE_TRACE_SEMIHOSTING_DEBUG)
88
89#if !defined(OS_INTEGER_TRACE_SEMIHOSTING_BUFF_ARRAY_SIZE)
90#define OS_INTEGER_TRACE_SEMIHOSTING_BUFF_ARRAY_SIZE (16)
91#endif
92
93 ssize_t
94 write (const void* buf, std::size_t nbyte)
95 {
96 if (buf == nullptr || nbyte == 0)
97 {
98 return 0;
99 }
100
101 const char* cbuf = (const char*)buf;
102
103 // Since the single character debug channel is quite slow, try to
104 // optimise and send a null terminated string, if possible.
105 if (cbuf[nbyte] == '\0')
106 {
107 // send string
108 call_host (SEMIHOSTING_SYS_WRITE0, (void*)cbuf);
109 }
110 else
111 {
112 // If not, use a local buffer to speed things up.
113 // For re-entrance, this bugger must be allocated on the stack,
114 // so be cautious with the size.
116 size_t togo = nbyte;
117 while (togo > 0)
118 {
119 std::size_t n
120 = ((togo < sizeof (tmp)) ? togo : sizeof (tmp) - 1);
121 std::size_t i = 0;
122 for (; i < n; ++i, ++cbuf)
123 {
124 tmp[i] = *cbuf;
125 }
126 tmp[i] = '\0';
127
128 call_host (SEMIHOSTING_SYS_WRITE0, (void*)tmp);
129
130 togo -= n;
131 }
132 }
133
134 // All bytes written.
135 return (ssize_t)nbyte;
136 }
137
138#elif defined(OS_USE_TRACE_SEMIHOSTING_STDOUT)
139
140 ssize_t
141 write (const void* buf, std::size_t nbyte)
142 {
143 if (buf == nullptr || nbyte == 0)
144 {
145 return 0;
146 }
147
148 static int handle; // STATIC!
149
150 void* block[3];
151 int ret;
152
153 if (handle == 0)
154 {
155 // On the very first call get the file handle from the host.
156
157 // Special filename for stdin/out/err.
158 block[0] = (void*)":tt";
159 block[1] = (void*)4; // mode "w"
160 // Length of ":tt", except null terminator.
161 block[2] = (void*)(sizeof (":tt") - 1);
162
163 ret = call_host (SEMIHOSTING_SYS_OPEN, (void*)block);
164 if (ret == -1)
165 {
166 return -1;
167 }
168
169 handle = ret;
170 }
171
172 block[0] = (void*)handle;
173 block[1] = (void*)buf;
174 block[2] = (void*)nbyte;
175 // Send character array to host file/device.
176 ret = call_host (SEMIHOSTING_SYS_WRITE, (void*)block);
177 // This call returns the number of bytes NOT written (0 if all ok).
178
179 // -1 is not a legal value, but SEGGER seems to return it
180 if (ret == -1)
181 {
182 return -1;
183 }
184
185 // The compliant way of returning errors.
186 if (ret == (int)nbyte)
187 {
188 return -1;
189 }
190
191 // Return the number of bytes written.
192 return (ssize_t)(nbyte) - (ssize_t)ret;
193 }
194
195#endif /* defined(OS_USE_TRACE_SEMIHOSTING_STDOUT) */
196
197 } /* namespace trace */
198} /* namespace os */
199
200#endif /* defined(OS_USE_TRACE_SEMIHOSTING_DEBUG) || \
201 defined(OS_USE_TRACE_SEMIHOSTING_STDOUT) */
202#endif /* defined(TRACE) */
203
204// ----------------------------------------------------------------------------
205
206#endif /* defined(__ARM_EABI__) */
207
208// ----------------------------------------------------------------------------
#define OS_INTEGER_TRACE_SEMIHOSTING_BUFF_ARRAY_SIZE
Define the semihosting debug buffer size.
void initialize(void)
Definition trace.cpp:37
ssize_t write(const void *buf, std::size_t nbyte)
Write the given number of bytes to the trace output channel.
Definition trace.cpp:46
System namespace.
@ SEMIHOSTING_SYS_WRITE
Definition semihosting.h:55
@ SEMIHOSTING_SYS_WRITE0
Definition semihosting.h:57
@ SEMIHOSTING_SYS_OPEN
Definition semihosting.h:45
static int call_host(int reason, void *arg)
Definition semihosting.h:97