1 /* Cell-based print utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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 "print-utils.h"
21 /* Temporary storage using circular buffer. */
23 /* Number of cells in the circular buffer. */
26 /* Return the next entry in the circular buffer. */
31 static char buf
[NUMCELLS
][PRINT_CELL_SIZE
];
34 if (++cell
>= NUMCELLS
)
40 decimal2str (const char *sign
, ULONGEST addr
, int width
)
42 /* Steal code from valprint.c:print_decimal(). Should this worry
43 about the real size of addr as the above does? */
44 unsigned long temp
[3];
45 char *str
= get_print_cell ();
50 temp
[i
] = addr
% (1000 * 1000 * 1000);
51 addr
/= (1000 * 1000 * 1000);
55 while (addr
!= 0 && i
< (sizeof (temp
) / sizeof (temp
[0])));
64 xsnprintf (str
, PRINT_CELL_SIZE
, "%s%0*lu", sign
, width
, temp
[0]);
67 xsnprintf (str
, PRINT_CELL_SIZE
, "%s%0*lu%09lu", sign
, width
,
71 xsnprintf (str
, PRINT_CELL_SIZE
, "%s%0*lu%09lu%09lu", sign
, width
,
72 temp
[2], temp
[1], temp
[0]);
75 internal_error (_("failed internal consistency check"));
82 octal2str (ULONGEST addr
, int width
)
84 unsigned long temp
[3];
85 char *str
= get_print_cell ();
90 temp
[i
] = addr
% (0100000 * 0100000);
91 addr
/= (0100000 * 0100000);
95 while (addr
!= 0 && i
< (sizeof (temp
) / sizeof (temp
[0])));
105 xsnprintf (str
, PRINT_CELL_SIZE
, "%*o", width
, 0);
107 xsnprintf (str
, PRINT_CELL_SIZE
, "0%0*lo", width
, temp
[0]);
110 xsnprintf (str
, PRINT_CELL_SIZE
, "0%0*lo%010lo", width
, temp
[1], temp
[0]);
113 xsnprintf (str
, PRINT_CELL_SIZE
, "0%0*lo%010lo%010lo", width
,
114 temp
[2], temp
[1], temp
[0]);
117 internal_error (_("failed internal consistency check"));
123 /* See print-utils.h. */
126 pulongest (ULONGEST u
)
128 return decimal2str ("", u
, 0);
131 /* See print-utils.h. */
137 return decimal2str ("-", -l
, 0);
139 return decimal2str ("", l
, 0);
142 /* Eliminate warning from compiler on 32-bit systems. */
143 static int thirty_two
= 32;
145 /* See print-utils.h. */
148 phex (ULONGEST l
, int sizeof_l
)
155 str
= get_print_cell ();
156 xsnprintf (str
, PRINT_CELL_SIZE
, "%08lx%08lx",
157 (unsigned long) (l
>> thirty_two
),
158 (unsigned long) (l
& 0xffffffff));
161 str
= get_print_cell ();
162 xsnprintf (str
, PRINT_CELL_SIZE
, "%08lx", (unsigned long) l
);
165 str
= get_print_cell ();
166 xsnprintf (str
, PRINT_CELL_SIZE
, "%04x", (unsigned short) (l
& 0xffff));
169 str
= get_print_cell ();
170 xsnprintf (str
, PRINT_CELL_SIZE
, "%02x", (unsigned short) (l
& 0xff));
173 return phex (l
, sizeof (l
));
180 /* See print-utils.h. */
183 phex_nz (ULONGEST l
, int sizeof_l
)
191 unsigned long high
= (unsigned long) (l
>> thirty_two
);
193 str
= get_print_cell ();
195 xsnprintf (str
, PRINT_CELL_SIZE
, "%lx",
196 (unsigned long) (l
& 0xffffffff));
198 xsnprintf (str
, PRINT_CELL_SIZE
, "%lx%08lx", high
,
199 (unsigned long) (l
& 0xffffffff));
203 str
= get_print_cell ();
204 xsnprintf (str
, PRINT_CELL_SIZE
, "%lx", (unsigned long) l
);
207 str
= get_print_cell ();
208 xsnprintf (str
, PRINT_CELL_SIZE
, "%x", (unsigned short) (l
& 0xffff));
211 str
= get_print_cell ();
212 xsnprintf (str
, PRINT_CELL_SIZE
, "%x", (unsigned short) (l
& 0xff));
215 return phex_nz (l
, sizeof (l
));
222 /* See print-utils.h. */
225 hex_string (LONGEST num
)
227 char *result
= get_print_cell ();
229 xsnprintf (result
, PRINT_CELL_SIZE
, "0x%s", phex_nz (num
, sizeof (num
)));
233 /* See print-utils.h. */
236 hex_string_custom (LONGEST num
, int width
)
238 char *result
= get_print_cell ();
239 char *result_end
= result
+ PRINT_CELL_SIZE
- 1;
240 const char *hex
= phex_nz (num
, sizeof (num
));
241 int hex_len
= strlen (hex
);
245 if (width
+ 2 >= PRINT_CELL_SIZE
)
247 hex_string_custom: insufficient space to store result"));
249 strcpy (result_end
- width
- 2, "0x");
250 memset (result_end
- width
, '0', width
);
251 strcpy (result_end
- hex_len
, hex
);
252 return result_end
- width
- 2;
255 /* See print-utils.h. */
258 int_string (LONGEST val
, int radix
, int is_signed
, int width
,
268 result
= hex_string (val
);
270 result
= hex_string_custom (val
, width
);
277 if (is_signed
&& val
< 0)
278 /* Cast to unsigned before negating, to prevent runtime error:
279 negation of -9223372036854775808 cannot be represented in type
280 'long int'; cast to an unsigned type to negate this value to
282 return decimal2str ("-", -(ULONGEST
)val
, width
);
284 return decimal2str ("", val
, width
);
288 char *result
= octal2str (val
, width
);
290 if (use_c_format
|| val
== 0)
296 internal_error (_("failed internal consistency check"));
300 /* See print-utils.h. */
303 core_addr_to_string (const CORE_ADDR addr
)
305 char *str
= get_print_cell ();
308 strcat (str
, phex (addr
, sizeof (addr
)));
312 /* See print-utils.h. */
315 core_addr_to_string_nz (const CORE_ADDR addr
)
317 char *str
= get_print_cell ();
320 strcat (str
, phex_nz (addr
, sizeof (addr
)));
324 /* See print-utils.h. */
327 host_address_to_string_1 (const void *addr
)
329 char *str
= get_print_cell ();
331 xsnprintf (str
, PRINT_CELL_SIZE
, "0x%s",
332 phex_nz ((uintptr_t) addr
, sizeof (addr
)));