* Contribute CGEN simulator build support code.
[binutils-gdb.git] / gdb / rdi-share / logging.c
blob79b70ef348f1d5248846ed14f63dec09a49fd43b
1 /*
2 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
3 *
4 * This software may be freely used, copied, modified, and distributed
5 * provided that the above copyright notice is preserved in all copies of the
6 * software.
7 */
9 /* -*-C-*-
11 * $Revision$
12 * $Date$
15 * logging.c - methods for logging warnings, errors and trace info
19 #include <stdarg.h> /* ANSI varargs support */
21 #ifdef TARGET
22 # include "angel.h"
23 # include "devconf.h"
24 #else
25 # include "host.h"
26 #endif
28 #include "logging.h" /* Header file for this source code */
30 #ifndef UNUSED
31 # define UNUSED(x) ((x)=(x))
32 #endif
35 * __rt_warning
36 * ------------
37 * This routine is provided as a standard method of generating
38 * run-time system warnings. The actual action taken by this code can
39 * be board or target application specific, e.g. internal logging,
40 * debug message, etc.
43 #ifdef DEBUG
45 # ifdef DEBUG_METHOD
47 # define STRINGIFY2(x) #x
48 # define STRINGIFY(x) STRINGIFY2(x)
49 # define DEBUG_METHOD_HEADER STRINGIFY(DEBUG_METHOD##.h)
51 # include DEBUG_METHOD_HEADER
53 # define METHOD_EXPAND_2(m, p, c) m##p(c)
54 # define METHOD_EXPAND(m, p, c) METHOD_EXPAND_2(m, p, c)
56 # define CHAROUT(c) METHOD_EXPAND(DEBUG_METHOD, _PutChar, (c))
57 # define PRE_DEBUG(l) METHOD_EXPAND(DEBUG_METHOD, _PreWarn, (l))
58 # define POST_DEBUG(n) METHOD_EXPAND(DEBUG_METHOD, _PostWarn, (n))
60 # else
61 # error Must define DEBUG_METHOD
62 # endif
64 #endif /* def DEBUG */
67 * the guts of __rt_warning
70 #pragma no_check_stack
71 #ifdef DEBUG
73 static const char hextab[] = "0123456789ABCDEF";
76 * If debugging, then we break va_warn into sub-functions which
77 * allow us to get an easy breakpoint on the formatted string
79 static int va_warn0(char *format, va_list args)
81 int len = 0;
83 while ((format != NULL) && (*format != '\0'))
85 if (*format == '%')
87 char fch = *(++format); /* get format character (skipping '%') */
88 int ival; /* holder for integer arguments */
89 char *string; /* holder for string arguments */
90 int width = 0; /* No field width by default */
91 int padzero = FALSE; /* By default we pad with spaces */
94 * Check if the format has a width specified. NOTE: We do
95 * not use the "isdigit" function here, since it will
96 * require run-time support. The current ARM Ltd header
97 * defines "isdigit" as a macro, that uses a fixed
98 * character description table.
100 if ((fch >= '0') && (fch <= '9'))
102 if (fch == '0')
104 /* Leading zeroes padding */
105 padzero = TRUE;
106 fch = *(++format);
109 while ((fch >= '0') && (fch <= '9'))
111 width = ((width * 10) + (fch - '0'));
112 fch = *(++format);
116 if (fch == 'l')
117 /* skip 'l' in "%lx", etc. */
118 fch = *(++format);
120 switch (fch)
122 case 'c':
123 /* char */
124 ival = va_arg(args, int);
125 CHAROUT((char)ival);
126 len++;
127 break;
129 case 'x':
130 case 'X':
132 /* hexadecimal */
133 unsigned int uval = va_arg(args, unsigned int);
134 int loop;
136 UNUSED(uval);
138 if ((width == 0) || (width > 8))
139 width = 8;
141 for(loop = (width * 4); (loop != 0); loop -= 4)
143 CHAROUT(hextab[(uval >> (loop - 4)) & 0xF]);
144 len++;
148 break;
150 case 'd':
151 /* decimal */
152 ival = va_arg(args, int);
154 if (ival < 0)
156 ival = -ival;
157 CHAROUT('-');
158 len++;
161 if (ival == 0)
163 CHAROUT('0');
164 len++;
166 else
169 * The simplest method of displaying numbers is
170 * to provide a small recursive routine, that
171 * nests until the most-significant digit is
172 * reached, and then falls back out displaying
173 * individual digits. However, we want to avoid
174 * using recursive code within the lo-level
175 * parts of Angel (to minimise the stack
176 * usage). The following number conversion is a
177 * non-recursive solution.
179 char buffer[16]; /* stack space used to hold number */
180 int count = 0; /* pointer into buffer */
183 * Place the conversion into the buffer in
184 * reverse order:
186 while (ival != 0)
188 buffer[count++] = ('0' + ((unsigned int)ival % 10));
189 ival = ((unsigned int)ival / 10);
193 * Check if we are placing the data in a
194 * fixed width field:
196 if (width != 0)
198 width -= count;
200 for (; (width != 0); width--)
202 CHAROUT(padzero ? '0': ' ');
203 len++;
207 /* then display the buffer in reverse order */
208 for (; (count != 0); count--)
210 CHAROUT(buffer[count - 1]);
211 len++;
215 break;
217 case 's':
218 /* string */
219 string = va_arg(args, char *);
221 /* we only need this test once */
222 if (string != NULL)
223 /* whilst we check this for every character */
224 while (*string)
226 CHAROUT(*string);
227 len++;
228 string++;
231 * NOTE: We do not use "*string++" as the macro
232 * parameter, since we do not know how many times
233 *the parameter may be expanded within the macro.
237 break;
239 case '\0':
241 * string terminated by '%' character, bodge things
242 * to prepare for default "format++" below
244 format--;
246 break;
248 default:
249 /* just display the character */
250 CHAROUT(*format);
251 len++;
253 break;
256 format++; /* step over format character */
258 else
260 CHAROUT(*format);
261 len++;
262 format++;
265 return len;
269 * this routine is simply here as a good breakpoint for dumping msg -
270 * can be used by DEBUG_METHOD macros or functions, if required.
272 # ifdef DEBUG_NEED_VA_WARN1
273 static void va_warn1(int len, char *msg)
275 UNUSED(len); UNUSED(msg);
277 # endif
279 void va_warn(WarnLevel level, char *format, va_list args)
281 int len;
283 if ( PRE_DEBUG( level ) )
285 len = va_warn0(format, args);
286 POST_DEBUG( len );
290 #else /* ndef DEBUG */
292 void va_warn(WarnLevel level, char *format, va_list args)
294 UNUSED(level); UNUSED(format); UNUSED(args);
297 #endif /* ... else ndef(DEBUG) ... */
298 #pragma check_stack
300 #pragma no_check_stack
301 void __rt_warning(char *format, ...)
303 va_list args;
306 * For a multi-threaded system we should provide a lock at this point
307 * to ensure that the warning messages are sequenced properly.
310 va_start(args, format);
311 va_warn(WL_WARN, format, args);
312 va_end(args);
314 return;
316 #pragma check_stack
318 #ifdef TARGET
320 #pragma no_check_stack
321 void __rt_uninterruptable_loop( void ); /* in suppasm.s */
323 void __rt_error(char *format, ...)
325 va_list args;
327 va_start(args, format);
329 /* Display warning message */
330 va_warn(WL_ERROR, format, args);
332 __rt_uninterruptable_loop();
334 va_end(args);
335 return;
337 #pragma check_stack
339 #endif /* def TARGET */
341 #ifdef DO_TRACE
343 static bool trace_on = FALSE; /* must be set true in debugger if req'd */
345 #pragma no_check_stack
346 void __rt_trace(char *format, ...)
348 va_list args;
351 * For a multi-threaded system we should provide a lock at this point
352 * to ensure that the warning messages are sequenced properly.
355 if (trace_on)
357 va_start(args, format);
358 va_warn(WL_TRACE, format, args);
359 va_end(args);
362 return;
364 #pragma check_stack
366 #endif /* def DO_TRACE */
369 /* EOF logging.c */