µ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 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#if defined(__clang__)
29#pragma clang diagnostic ignored "-Wempty-translation-unit"
30#endif
31
32// ----------------------------------------------------------------------------
33
34#if defined(__ARM_EABI__)
35
36// ----------------------------------------------------------------------------
37
38#if defined(TRACE)
39
40#include <cmsis-plus/os-app-config.h>
41
42#if defined(OS_USE_TRACE_SEMIHOSTING_DEBUG) || defined(OS_USE_TRACE_SEMIHOSTING_STDOUT)
43
45
46// ----------------------------------------------------------------------------
47
48#if defined(OS_DEBUG_SEMIHOSTING_FAULTS)
49#error "Cannot debug semihosting using semihosting trace; use OS_USE_TRACE_ITM"
50#endif
51
53
54// ----------------------------------------------------------------------------
55
56namespace os
57{
58 namespace trace
59 {
60 // ----------------------------------------------------------------------
61
62 void
63 initialize (void)
64 {
65 // For semihosting, no inits are required.
66 }
67
68 // ----------------------------------------------------------------------
69
70 // Semihosting is another output channel that can be used for the trace
71 // messages. It comes in two flavours: STDOUT and DEBUG. The STDOUT channel
72 // is the equivalent of the stdout in POSIX and in most cases it is
73 // forwarded to the GDB server stdout stream. The debug channel is a
74 // separate channel. STDOUT is buffered, so nothing is displayed until
75 // a \n; DEBUG is not buffered, but can be slow.
76 //
77 // Choosing between semihosting stdout and debug depends on the capabilities
78 // of your GDB server, and also on specific needs. It is recommended to test
79 // DEBUG first, and if too slow, try STDOUT.
80 //
81 // The JLink GDB server fully support semihosting, and both configurations
82 // are available; to activate it, use "monitor semihosting enable" or check
83 // the corresponding button in the JLink Debugging plug-in.
84 // In OpenOCD, support for semihosting can be enabled using
85 // "monitor arm semihosting enable".
86 //
87 // Note: Applications built with semihosting output active normally cannot
88 // be executed without the debugger connected and active, since they use
89 // BKPT to communicate with the host. However, with a carefully written
90 // HardFault_Handler, the semihosting BKPT calls can be processed, making
91 // possible to run semihosting applications as standalone, without being
92 // terminated with hardware faults.
93
94 // ----------------------------------------------------------------------
95
96#if defined(OS_USE_TRACE_SEMIHOSTING_DEBUG)
97
98#if !defined(OS_INTEGER_TRACE_SEMIHOSTING_BUFF_ARRAY_SIZE)
99#define OS_INTEGER_TRACE_SEMIHOSTING_BUFF_ARRAY_SIZE (16)
100#endif
101
102 ssize_t
103 write (const void* buf, std::size_t nbyte)
104 {
105 if (buf == nullptr || nbyte == 0)
106 {
107 return 0;
108 }
109
110 const char* cbuf = (const char*) buf;
111
112 // Since the single character debug channel is quite slow, try to
113 // optimise and send a null terminated string, if possible.
114 if (cbuf[nbyte] == '\0')
115 {
116 // send string
117 call_host (SEMIHOSTING_SYS_WRITE0, (void*) cbuf);
118 }
119 else
120 {
121 // If not, use a local buffer to speed things up.
122 // For re-entrance, this bugger must be allocated on the stack,
123 // so be cautious with the size.
125 size_t togo = nbyte;
126 while (togo > 0)
127 {
128 std::size_t n = ((togo < sizeof(tmp)) ? togo : sizeof(tmp) - 1);
129 std::size_t i = 0;
130 for (; i < n; ++i, ++cbuf)
131 {
132 tmp[i] = *cbuf;
133 }
134 tmp[i] = '\0';
135
136 call_host (SEMIHOSTING_SYS_WRITE0, (void*) tmp);
137
138 togo -= n;
139 }
140 }
141
142 // All bytes written.
143 return (ssize_t) nbyte;
144 }
145
146#elif defined(OS_USE_TRACE_SEMIHOSTING_STDOUT)
147
148 ssize_t
149 write (const void* buf, std::size_t nbyte)
150 {
151 if (buf == nullptr || nbyte == 0)
152 {
153 return 0;
154 }
155
156 static int handle; // STATIC!
157
158 void* block[3];
159 int ret;
160
161 if (handle == 0)
162 {
163 // On the very first call get the file handle from the host.
164
165 // Special filename for stdin/out/err.
166 block[0] = (void*) ":tt";
167 block[1] = (void*) 4;// mode "w"
168 // Length of ":tt", except null terminator.
169 block[2] = (void*) (sizeof(":tt") - 1);
170
171 ret = call_host (SEMIHOSTING_SYS_OPEN, (void*) block);
172 if (ret == -1)
173 {
174 return -1;
175 }
176
177 handle = ret;
178 }
179
180 block[0] = (void*) handle;
181 block[1] = (void*) buf;
182 block[2] = (void*) nbyte;
183 // Send character array to host file/device.
184 ret = call_host (SEMIHOSTING_SYS_WRITE, (void*) block);
185 // This call returns the number of bytes NOT written (0 if all ok).
186
187 // -1 is not a legal value, but SEGGER seems to return it
188 if (ret == -1)
189 {
190 return -1;
191 }
192
193 // The compliant way of returning errors.
194 if (ret == (int) nbyte)
195 {
196 return -1;
197 }
198
199 // Return the number of bytes written.
200 return (ssize_t) (nbyte) - (ssize_t) ret;
201 }
202
203#endif /* defined(OS_USE_TRACE_SEMIHOSTING_STDOUT) */
204
205 } /* namespace trace */
206} /* namespace os */
207
208#endif /* defined(OS_USE_TRACE_SEMIHOSTING_DEBUG) || defined(OS_USE_TRACE_SEMIHOSTING_STDOUT) */
209#endif /* defined(TRACE) */
210
211// ----------------------------------------------------------------------------
212
213#endif /* defined(__ARM_EABI__) */
214
215// ----------------------------------------------------------------------------
#define OS_INTEGER_TRACE_SEMIHOSTING_BUFF_ARRAY_SIZE
Define the semihosting debug buffer size.
void initialize(void)
Definition trace.cpp:50
ssize_t write(const void *buf, std::size_t nbyte)
Write the given number of bytes to the trace output channel.
Definition trace.cpp:60
System namespace.
@ SEMIHOSTING_SYS_WRITE
Definition semihosting.h:58
@ SEMIHOSTING_SYS_WRITE0
Definition semihosting.h:60
@ SEMIHOSTING_SYS_OPEN
Definition semihosting.h:48
static int call_host(int reason, void *arg)
Definition semihosting.h:99