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