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