µ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++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2015-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#if defined(__clang__)
14#pragma clang diagnostic ignored "-Wempty-translation-unit"
15#elif defined(__GNUC__)
16#pragma GCC diagnostic ignored "-Wold-style-cast"
17#pragma GCC diagnostic ignored "-Wcast-qual"
18#pragma GCC diagnostic ignored "-Wuseless-cast"
19#endif
20
21// ----------------------------------------------------------------------------
22
23#if defined(__ARM_EABI__)
24
25// ----------------------------------------------------------------------------
26
27#if defined(TRACE)
28
29#if defined(OS_USE_OS_APP_CONFIG_H)
30#include <cmsis-plus/os-app-config.h>
31#endif
32
33#if defined(OS_USE_TRACE_SEMIHOSTING_DEBUG) || 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 capabilities
69 // of your GDB server, and also on specific needs. It is recommended to test
70 // 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 = ((togo < sizeof(tmp)) ? togo : sizeof(tmp) - 1);
120 std::size_t i = 0;
121 for (; i < n; ++i, ++cbuf)
122 {
123 tmp[i] = *cbuf;
124 }
125 tmp[i] = '\0';
126
127 call_host (SEMIHOSTING_SYS_WRITE0, (void*) tmp);
128
129 togo -= n;
130 }
131 }
132
133 // All bytes written.
134 return (ssize_t) nbyte;
135 }
136
137#elif defined(OS_USE_TRACE_SEMIHOSTING_STDOUT)
138
139 ssize_t
140 write (const void* buf, std::size_t nbyte)
141 {
142 if (buf == nullptr || nbyte == 0)
143 {
144 return 0;
145 }
146
147 static int handle; // STATIC!
148
149 void* block[3];
150 int ret;
151
152 if (handle == 0)
153 {
154 // On the very first call get the file handle from the host.
155
156 // Special filename for stdin/out/err.
157 block[0] = (void*) ":tt";
158 block[1] = (void*) 4;// mode "w"
159 // Length of ":tt", except null terminator.
160 block[2] = (void*) (sizeof(":tt") - 1);
161
162 ret = call_host (SEMIHOSTING_SYS_OPEN, (void*) block);
163 if (ret == -1)
164 {
165 return -1;
166 }
167
168 handle = ret;
169 }
170
171 block[0] = (void*) handle;
172 block[1] = (void*) buf;
173 block[2] = (void*) nbyte;
174 // Send character array to host file/device.
175 ret = call_host (SEMIHOSTING_SYS_WRITE, (void*) block);
176 // This call returns the number of bytes NOT written (0 if all ok).
177
178 // -1 is not a legal value, but SEGGER seems to return it
179 if (ret == -1)
180 {
181 return -1;
182 }
183
184 // The compliant way of returning errors.
185 if (ret == (int) nbyte)
186 {
187 return -1;
188 }
189
190 // Return the number of bytes written.
191 return (ssize_t) (nbyte) - (ssize_t) ret;
192 }
193
194#endif /* defined(OS_USE_TRACE_SEMIHOSTING_STDOUT) */
195
196 } /* namespace trace */
197} /* namespace os */
198
199#endif /* defined(OS_USE_TRACE_SEMIHOSTING_DEBUG) || defined(OS_USE_TRACE_SEMIHOSTING_STDOUT) */
200#endif /* defined(TRACE) */
201
202// ----------------------------------------------------------------------------
203
204#endif /* defined(__ARM_EABI__) */
205
206// ----------------------------------------------------------------------------
#define OS_INTEGER_TRACE_SEMIHOSTING_BUFF_ARRAY_SIZE
Define the semihosting debug buffer size.
void initialize(void)
Definition trace.cpp:38
ssize_t write(const void *buf, std::size_t nbyte)
Write the given number of bytes to the trace output channel.
Definition trace.cpp:47
System namespace.
@ SEMIHOSTING_SYS_WRITE
Definition semihosting.h:56
@ SEMIHOSTING_SYS_WRITE0
Definition semihosting.h:58
@ SEMIHOSTING_SYS_OPEN
Definition semihosting.h:46
static int call_host(int reason, void *arg)