constify ui_out_impl
[deliverable/binutils-gdb.git] / gdb / common / print-utils.c
1 /* Cell-based print utility routines for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #ifdef GDBSERVER
21 #include "server.h"
22 #else
23 #include "defs.h"
24 #endif
25
26 #include "print-utils.h"
27
28 #include <string.h>
29 #include <stdint.h>
30
31 /* Temporary storage using circular buffer. */
32
33 #define NUMCELLS 16
34 #define CELLSIZE 50
35
36 /* Return the next entry in the circular buffer. */
37
38 static char *
39 get_cell (void)
40 {
41 static char buf[NUMCELLS][CELLSIZE];
42 static int cell = 0;
43
44 if (++cell >= NUMCELLS)
45 cell = 0;
46 return buf[cell];
47 }
48
49 static char *
50 decimal2str (char *sign, ULONGEST addr, int width)
51 {
52 /* Steal code from valprint.c:print_decimal(). Should this worry
53 about the real size of addr as the above does? */
54 unsigned long temp[3];
55 char *str = get_cell ();
56 int i = 0;
57
58 do
59 {
60 temp[i] = addr % (1000 * 1000 * 1000);
61 addr /= (1000 * 1000 * 1000);
62 i++;
63 width -= 9;
64 }
65 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
66
67 width += 9;
68 if (width < 0)
69 width = 0;
70
71 switch (i)
72 {
73 case 1:
74 xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]);
75 break;
76 case 2:
77 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width,
78 temp[1], temp[0]);
79 break;
80 case 3:
81 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width,
82 temp[2], temp[1], temp[0]);
83 break;
84 default:
85 internal_error (__FILE__, __LINE__,
86 _("failed internal consistency check"));
87 }
88
89 return str;
90 }
91
92 static char *
93 octal2str (ULONGEST addr, int width)
94 {
95 unsigned long temp[3];
96 char *str = get_cell ();
97 int i = 0;
98
99 do
100 {
101 temp[i] = addr % (0100000 * 0100000);
102 addr /= (0100000 * 0100000);
103 i++;
104 width -= 10;
105 }
106 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0])));
107
108 width += 10;
109 if (width < 0)
110 width = 0;
111
112 switch (i)
113 {
114 case 1:
115 if (temp[0] == 0)
116 xsnprintf (str, CELLSIZE, "%*o", width, 0);
117 else
118 xsnprintf (str, CELLSIZE, "0%0*lo", width, temp[0]);
119 break;
120 case 2:
121 xsnprintf (str, CELLSIZE, "0%0*lo%010lo", width, temp[1], temp[0]);
122 break;
123 case 3:
124 xsnprintf (str, CELLSIZE, "0%0*lo%010lo%010lo", width,
125 temp[2], temp[1], temp[0]);
126 break;
127 default:
128 internal_error (__FILE__, __LINE__,
129 _("failed internal consistency check"));
130 }
131
132 return str;
133 }
134
135 /* See print-utils.h. */
136
137 char *
138 pulongest (ULONGEST u)
139 {
140 return decimal2str ("", u, 0);
141 }
142
143 /* See print-utils.h. */
144
145 char *
146 plongest (LONGEST l)
147 {
148 if (l < 0)
149 return decimal2str ("-", -l, 0);
150 else
151 return decimal2str ("", l, 0);
152 }
153
154 /* Eliminate warning from compiler on 32-bit systems. */
155 static int thirty_two = 32;
156
157 /* See print-utils.h. */
158
159 char *
160 phex (ULONGEST l, int sizeof_l)
161 {
162 char *str;
163
164 switch (sizeof_l)
165 {
166 case 8:
167 str = get_cell ();
168 xsnprintf (str, CELLSIZE, "%08lx%08lx",
169 (unsigned long) (l >> thirty_two),
170 (unsigned long) (l & 0xffffffff));
171 break;
172 case 4:
173 str = get_cell ();
174 xsnprintf (str, CELLSIZE, "%08lx", (unsigned long) l);
175 break;
176 case 2:
177 str = get_cell ();
178 xsnprintf (str, CELLSIZE, "%04x", (unsigned short) (l & 0xffff));
179 break;
180 default:
181 str = phex (l, sizeof (l));
182 break;
183 }
184
185 return str;
186 }
187
188 /* See print-utils.h. */
189
190 char *
191 phex_nz (ULONGEST l, int sizeof_l)
192 {
193 char *str;
194
195 switch (sizeof_l)
196 {
197 case 8:
198 {
199 unsigned long high = (unsigned long) (l >> thirty_two);
200
201 str = get_cell ();
202 if (high == 0)
203 xsnprintf (str, CELLSIZE, "%lx",
204 (unsigned long) (l & 0xffffffff));
205 else
206 xsnprintf (str, CELLSIZE, "%lx%08lx", high,
207 (unsigned long) (l & 0xffffffff));
208 break;
209 }
210 case 4:
211 str = get_cell ();
212 xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l);
213 break;
214 case 2:
215 str = get_cell ();
216 xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff));
217 break;
218 default:
219 str = phex_nz (l, sizeof (l));
220 break;
221 }
222
223 return str;
224 }
225
226 /* See print-utils.h. */
227
228 char *
229 hex_string (LONGEST num)
230 {
231 char *result = get_cell ();
232
233 xsnprintf (result, CELLSIZE, "0x%s", phex_nz (num, sizeof (num)));
234 return result;
235 }
236
237 /* See print-utils.h. */
238
239 char *
240 hex_string_custom (LONGEST num, int width)
241 {
242 char *result = get_cell ();
243 char *result_end = result + CELLSIZE - 1;
244 const char *hex = phex_nz (num, sizeof (num));
245 int hex_len = strlen (hex);
246
247 if (hex_len > width)
248 width = hex_len;
249 if (width + 2 >= CELLSIZE)
250 internal_error (__FILE__, __LINE__, _("\
251 hex_string_custom: insufficient space to store result"));
252
253 strcpy (result_end - width - 2, "0x");
254 memset (result_end - width, '0', width);
255 strcpy (result_end - hex_len, hex);
256 return result_end - width - 2;
257 }
258
259 /* See print-utils.h. */
260
261 char *
262 int_string (LONGEST val, int radix, int is_signed, int width,
263 int use_c_format)
264 {
265 switch (radix)
266 {
267 case 16:
268 {
269 char *result;
270
271 if (width == 0)
272 result = hex_string (val);
273 else
274 result = hex_string_custom (val, width);
275 if (! use_c_format)
276 result += 2;
277 return result;
278 }
279 case 10:
280 {
281 if (is_signed && val < 0)
282 return decimal2str ("-", -val, width);
283 else
284 return decimal2str ("", val, width);
285 }
286 case 8:
287 {
288 char *result = octal2str (val, width);
289
290 if (use_c_format || val == 0)
291 return result;
292 else
293 return result + 1;
294 }
295 default:
296 internal_error (__FILE__, __LINE__,
297 _("failed internal consistency check"));
298 }
299 }
300
301 /* See print-utils.h. */
302
303 const char *
304 core_addr_to_string (const CORE_ADDR addr)
305 {
306 char *str = get_cell ();
307
308 strcpy (str, "0x");
309 strcat (str, phex (addr, sizeof (addr)));
310 return str;
311 }
312
313 /* See print-utils.h. */
314
315 const char *
316 core_addr_to_string_nz (const CORE_ADDR addr)
317 {
318 char *str = get_cell ();
319
320 strcpy (str, "0x");
321 strcat (str, phex_nz (addr, sizeof (addr)));
322 return str;
323 }
324
325 /* See print-utils.h. */
326
327 const char *
328 host_address_to_string (const void *addr)
329 {
330 char *str = get_cell ();
331
332 xsnprintf (str, CELLSIZE, "0x%s", phex_nz ((uintptr_t) addr, sizeof (addr)));
333 return str;
334 }
This page took 0.037446 seconds and 4 git commands to generate.