gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gdbsupport / print-utils.cc
blob7bbb6deea749f077352481c67c7fa090890091e0
1 /* Cell-based print utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
21 #include "print-utils.h"
22 /* Temporary storage using circular buffer. */
24 /* Number of cells in the circular buffer. */
25 #define NUMCELLS 16
27 /* Return the next entry in the circular buffer. */
29 char *
30 get_print_cell (void)
32 static char buf[NUMCELLS][PRINT_CELL_SIZE];
33 static int cell = 0;
35 if (++cell >= NUMCELLS)
36 cell = 0;
37 return buf[cell];
40 static char *
41 decimal2str (const char *sign, ULONGEST addr, int width)
43 /* Steal code from valprint.c:print_decimal(). Should this worry
44 about the real size of addr as the above does? */
45 unsigned long temp[3];
46 char *str = get_print_cell ();
47 int i = 0;
51 temp[i] = addr % (1000 * 1000 * 1000);
52 addr /= (1000 * 1000 * 1000);
53 i++;
54 width -= 9;
56 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
58 width += 9;
59 if (width < 0)
60 width = 0;
62 switch (i)
64 case 1:
65 xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu", sign, width, temp[0]);
66 break;
67 case 2:
68 xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu%09lu", sign, width,
69 temp[1], temp[0]);
70 break;
71 case 3:
72 xsnprintf (str, PRINT_CELL_SIZE, "%s%0*lu%09lu%09lu", sign, width,
73 temp[2], temp[1], temp[0]);
74 break;
75 default:
76 internal_error (__FILE__, __LINE__,
77 _("failed internal consistency check"));
80 return str;
83 static char *
84 octal2str (ULONGEST addr, int width)
86 unsigned long temp[3];
87 char *str = get_print_cell ();
88 int i = 0;
92 temp[i] = addr % (0100000 * 0100000);
93 addr /= (0100000 * 0100000);
94 i++;
95 width -= 10;
97 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
99 width += 10;
100 if (width < 0)
101 width = 0;
103 switch (i)
105 case 1:
106 if (temp[0] == 0)
107 xsnprintf (str, PRINT_CELL_SIZE, "%*o", width, 0);
108 else
109 xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo", width, temp[0]);
110 break;
111 case 2:
112 xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo%010lo", width, temp[1], temp[0]);
113 break;
114 case 3:
115 xsnprintf (str, PRINT_CELL_SIZE, "0%0*lo%010lo%010lo", width,
116 temp[2], temp[1], temp[0]);
117 break;
118 default:
119 internal_error (__FILE__, __LINE__,
120 _("failed internal consistency check"));
123 return str;
126 /* See print-utils.h. */
128 char *
129 pulongest (ULONGEST u)
131 return decimal2str ("", u, 0);
134 /* See print-utils.h. */
136 char *
137 plongest (LONGEST l)
139 if (l < 0)
140 return decimal2str ("-", -l, 0);
141 else
142 return decimal2str ("", l, 0);
145 /* Eliminate warning from compiler on 32-bit systems. */
146 static int thirty_two = 32;
148 /* See print-utils.h. */
150 char *
151 phex (ULONGEST l, int sizeof_l)
153 char *str;
155 switch (sizeof_l)
157 case 8:
158 str = get_print_cell ();
159 xsnprintf (str, PRINT_CELL_SIZE, "%08lx%08lx",
160 (unsigned long) (l >> thirty_two),
161 (unsigned long) (l & 0xffffffff));
162 break;
163 case 4:
164 str = get_print_cell ();
165 xsnprintf (str, PRINT_CELL_SIZE, "%08lx", (unsigned long) l);
166 break;
167 case 2:
168 str = get_print_cell ();
169 xsnprintf (str, PRINT_CELL_SIZE, "%04x", (unsigned short) (l & 0xffff));
170 break;
171 case 1:
172 str = get_print_cell ();
173 xsnprintf (str, PRINT_CELL_SIZE, "%02x", (unsigned short) (l & 0xff));
174 break;
175 default:
176 str = phex (l, sizeof (l));
177 break;
180 return str;
183 /* See print-utils.h. */
185 char *
186 phex_nz (ULONGEST l, int sizeof_l)
188 char *str;
190 switch (sizeof_l)
192 case 8:
194 unsigned long high = (unsigned long) (l >> thirty_two);
196 str = get_print_cell ();
197 if (high == 0)
198 xsnprintf (str, PRINT_CELL_SIZE, "%lx",
199 (unsigned long) (l & 0xffffffff));
200 else
201 xsnprintf (str, PRINT_CELL_SIZE, "%lx%08lx", high,
202 (unsigned long) (l & 0xffffffff));
203 break;
205 case 4:
206 str = get_print_cell ();
207 xsnprintf (str, PRINT_CELL_SIZE, "%lx", (unsigned long) l);
208 break;
209 case 2:
210 str = get_print_cell ();
211 xsnprintf (str, PRINT_CELL_SIZE, "%x", (unsigned short) (l & 0xffff));
212 break;
213 case 1:
214 str = get_print_cell ();
215 xsnprintf (str, PRINT_CELL_SIZE, "%x", (unsigned short) (l & 0xff));
216 break;
217 default:
218 str = phex_nz (l, sizeof (l));
219 break;
222 return str;
225 /* See print-utils.h. */
227 char *
228 hex_string (LONGEST num)
230 char *result = get_print_cell ();
232 xsnprintf (result, PRINT_CELL_SIZE, "0x%s", phex_nz (num, sizeof (num)));
233 return result;
236 /* See print-utils.h. */
238 char *
239 hex_string_custom (LONGEST num, int width)
241 char *result = get_print_cell ();
242 char *result_end = result + PRINT_CELL_SIZE - 1;
243 const char *hex = phex_nz (num, sizeof (num));
244 int hex_len = strlen (hex);
246 if (hex_len > width)
247 width = hex_len;
248 if (width + 2 >= PRINT_CELL_SIZE)
249 internal_error (__FILE__, __LINE__, _("\
250 hex_string_custom: insufficient space to store result"));
252 strcpy (result_end - width - 2, "0x");
253 memset (result_end - width, '0', width);
254 strcpy (result_end - hex_len, hex);
255 return result_end - width - 2;
258 /* See print-utils.h. */
260 char *
261 int_string (LONGEST val, int radix, int is_signed, int width,
262 int use_c_format)
264 switch (radix)
266 case 16:
268 char *result;
270 if (width == 0)
271 result = hex_string (val);
272 else
273 result = hex_string_custom (val, width);
274 if (! use_c_format)
275 result += 2;
276 return result;
278 case 10:
280 if (is_signed && val < 0)
281 /* Cast to unsigned before negating, to prevent runtime error:
282 negation of -9223372036854775808 cannot be represented in type
283 'long int'; cast to an unsigned type to negate this value to
284 itself. */
285 return decimal2str ("-", -(ULONGEST)val, width);
286 else
287 return decimal2str ("", val, width);
289 case 8:
291 char *result = octal2str (val, width);
293 if (use_c_format || val == 0)
294 return result;
295 else
296 return result + 1;
298 default:
299 internal_error (__FILE__, __LINE__,
300 _("failed internal consistency check"));
304 /* See print-utils.h. */
306 const char *
307 core_addr_to_string (const CORE_ADDR addr)
309 char *str = get_print_cell ();
311 strcpy (str, "0x");
312 strcat (str, phex (addr, sizeof (addr)));
313 return str;
316 /* See print-utils.h. */
318 const char *
319 core_addr_to_string_nz (const CORE_ADDR addr)
321 char *str = get_print_cell ();
323 strcpy (str, "0x");
324 strcat (str, phex_nz (addr, sizeof (addr)));
325 return str;
328 /* See print-utils.h. */
330 const char *
331 host_address_to_string_1 (const void *addr)
333 char *str = get_print_cell ();
335 xsnprintf (str, PRINT_CELL_SIZE, "0x%s",
336 phex_nz ((uintptr_t) addr, sizeof (addr)));
337 return str;