]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* General utility routines for GDB, the GNU debugger. |
b6ba6518 KB |
2 | Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, |
3 | 1997, 1998, 1999, 2000, 2001 | |
d9fcf2fb | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
39424bef | 24 | #include "gdb_assert.h" |
c906108c SS |
25 | #include <ctype.h> |
26 | #include "gdb_string.h" | |
c2c6d25f | 27 | #include "event-top.h" |
c906108c SS |
28 | |
29 | #ifdef HAVE_CURSES_H | |
30 | #include <curses.h> | |
31 | #endif | |
32 | #ifdef HAVE_TERM_H | |
33 | #include <term.h> | |
34 | #endif | |
35 | ||
9d271fd8 AC |
36 | #ifdef __GO32__ |
37 | #include <pc.h> | |
38 | #endif | |
39 | ||
c906108c SS |
40 | /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */ |
41 | #ifdef reg | |
42 | #undef reg | |
43 | #endif | |
44 | ||
042be3a9 | 45 | #include <signal.h> |
c906108c SS |
46 | #include "gdbcmd.h" |
47 | #include "serial.h" | |
48 | #include "bfd.h" | |
49 | #include "target.h" | |
50 | #include "demangle.h" | |
51 | #include "expression.h" | |
52 | #include "language.h" | |
53 | #include "annotate.h" | |
54 | ||
ac2e2ef7 AC |
55 | #include "inferior.h" /* for signed_pointer_to_address */ |
56 | ||
c906108c SS |
57 | #include <readline/readline.h> |
58 | ||
81b8eb80 | 59 | #ifndef MALLOC_INCOMPATIBLE |
3c37485b AC |
60 | #ifdef NEED_DECLARATION_MALLOC |
61 | extern PTR malloc (); | |
62 | #endif | |
0e52036f AC |
63 | #ifdef NEED_DECLARATION_REALLOC |
64 | extern PTR realloc (); | |
65 | #endif | |
81b8eb80 AC |
66 | #ifdef NEED_DECLARATION_FREE |
67 | extern void free (); | |
68 | #endif | |
69 | #endif | |
70 | ||
917317f4 JM |
71 | #undef XMALLOC |
72 | #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE))) | |
73 | ||
c906108c SS |
74 | /* readline defines this. */ |
75 | #undef savestring | |
76 | ||
507f3c78 | 77 | void (*error_begin_hook) (void); |
c906108c | 78 | |
2acceee2 JM |
79 | /* Holds the last error message issued by gdb */ |
80 | ||
d9fcf2fb | 81 | static struct ui_file *gdb_lasterr; |
2acceee2 | 82 | |
c906108c SS |
83 | /* Prototypes for local functions */ |
84 | ||
d9fcf2fb JM |
85 | static void vfprintf_maybe_filtered (struct ui_file *, const char *, |
86 | va_list, int); | |
c906108c | 87 | |
d9fcf2fb | 88 | static void fputs_maybe_filtered (const char *, struct ui_file *, int); |
c906108c SS |
89 | |
90 | #if defined (USE_MMALLOC) && !defined (NO_MMCHECK) | |
a14ed312 | 91 | static void malloc_botch (void); |
c906108c SS |
92 | #endif |
93 | ||
a14ed312 | 94 | static void prompt_for_continue (void); |
c906108c | 95 | |
a14ed312 | 96 | static void set_width_command (char *, int, struct cmd_list_element *); |
c906108c | 97 | |
a14ed312 | 98 | static void set_width (void); |
c906108c | 99 | |
c906108c SS |
100 | /* Chain of cleanup actions established with make_cleanup, |
101 | to be executed if an error happens. */ | |
102 | ||
c5aa993b JM |
103 | static struct cleanup *cleanup_chain; /* cleaned up after a failed command */ |
104 | static struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */ | |
105 | static struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */ | |
106 | static struct cleanup *exec_cleanup_chain; /* cleaned up on each execution command */ | |
6426a772 JM |
107 | /* cleaned up on each error from within an execution command */ |
108 | static struct cleanup *exec_error_cleanup_chain; | |
43ff13b4 JM |
109 | |
110 | /* Pointer to what is left to do for an execution command after the | |
111 | target stops. Used only in asynchronous mode, by targets that | |
112 | support async execution. The finish and until commands use it. So | |
113 | does the target extended-remote command. */ | |
114 | struct continuation *cmd_continuation; | |
c2d11a7d | 115 | struct continuation *intermediate_continuation; |
c906108c SS |
116 | |
117 | /* Nonzero if we have job control. */ | |
118 | ||
119 | int job_control; | |
120 | ||
121 | /* Nonzero means a quit has been requested. */ | |
122 | ||
123 | int quit_flag; | |
124 | ||
125 | /* Nonzero means quit immediately if Control-C is typed now, rather | |
126 | than waiting until QUIT is executed. Be careful in setting this; | |
127 | code which executes with immediate_quit set has to be very careful | |
128 | about being able to deal with being interrupted at any time. It is | |
129 | almost always better to use QUIT; the only exception I can think of | |
130 | is being able to quit out of a system call (using EINTR loses if | |
131 | the SIGINT happens between the previous QUIT and the system call). | |
132 | To immediately quit in the case in which a SIGINT happens between | |
133 | the previous QUIT and setting immediate_quit (desirable anytime we | |
134 | expect to block), call QUIT after setting immediate_quit. */ | |
135 | ||
136 | int immediate_quit; | |
137 | ||
138 | /* Nonzero means that encoded C++ names should be printed out in their | |
139 | C++ form rather than raw. */ | |
140 | ||
141 | int demangle = 1; | |
142 | ||
143 | /* Nonzero means that encoded C++ names should be printed out in their | |
144 | C++ form even in assembler language displays. If this is set, but | |
145 | DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ | |
146 | ||
147 | int asm_demangle = 0; | |
148 | ||
149 | /* Nonzero means that strings with character values >0x7F should be printed | |
150 | as octal escapes. Zero means just print the value (e.g. it's an | |
151 | international character, and the terminal or window can cope.) */ | |
152 | ||
153 | int sevenbit_strings = 0; | |
154 | ||
155 | /* String to be printed before error messages, if any. */ | |
156 | ||
157 | char *error_pre_print; | |
158 | ||
159 | /* String to be printed before quit messages, if any. */ | |
160 | ||
161 | char *quit_pre_print; | |
162 | ||
163 | /* String to be printed before warning messages, if any. */ | |
164 | ||
165 | char *warning_pre_print = "\nwarning: "; | |
166 | ||
167 | int pagination_enabled = 1; | |
c906108c | 168 | \f |
c5aa993b | 169 | |
c906108c SS |
170 | /* Add a new cleanup to the cleanup_chain, |
171 | and return the previous chain pointer | |
172 | to be passed later to do_cleanups or discard_cleanups. | |
173 | Args are FUNCTION to clean up with, and ARG to pass to it. */ | |
174 | ||
175 | struct cleanup * | |
e4005526 | 176 | make_cleanup (make_cleanup_ftype *function, void *arg) |
c906108c | 177 | { |
c5aa993b | 178 | return make_my_cleanup (&cleanup_chain, function, arg); |
c906108c SS |
179 | } |
180 | ||
181 | struct cleanup * | |
e4005526 | 182 | make_final_cleanup (make_cleanup_ftype *function, void *arg) |
c906108c | 183 | { |
c5aa993b | 184 | return make_my_cleanup (&final_cleanup_chain, function, arg); |
c906108c | 185 | } |
7a292a7a | 186 | |
c906108c | 187 | struct cleanup * |
e4005526 | 188 | make_run_cleanup (make_cleanup_ftype *function, void *arg) |
c906108c | 189 | { |
c5aa993b | 190 | return make_my_cleanup (&run_cleanup_chain, function, arg); |
c906108c | 191 | } |
7a292a7a | 192 | |
43ff13b4 | 193 | struct cleanup * |
e4005526 | 194 | make_exec_cleanup (make_cleanup_ftype *function, void *arg) |
43ff13b4 | 195 | { |
c5aa993b | 196 | return make_my_cleanup (&exec_cleanup_chain, function, arg); |
43ff13b4 JM |
197 | } |
198 | ||
6426a772 | 199 | struct cleanup * |
e4005526 | 200 | make_exec_error_cleanup (make_cleanup_ftype *function, void *arg) |
6426a772 JM |
201 | { |
202 | return make_my_cleanup (&exec_error_cleanup_chain, function, arg); | |
203 | } | |
204 | ||
7a292a7a | 205 | static void |
fba45db2 | 206 | do_freeargv (void *arg) |
7a292a7a | 207 | { |
c5aa993b | 208 | freeargv ((char **) arg); |
7a292a7a SS |
209 | } |
210 | ||
211 | struct cleanup * | |
fba45db2 | 212 | make_cleanup_freeargv (char **arg) |
7a292a7a SS |
213 | { |
214 | return make_my_cleanup (&cleanup_chain, do_freeargv, arg); | |
215 | } | |
216 | ||
5c65bbb6 AC |
217 | static void |
218 | do_bfd_close_cleanup (void *arg) | |
219 | { | |
220 | bfd_close (arg); | |
221 | } | |
222 | ||
223 | struct cleanup * | |
224 | make_cleanup_bfd_close (bfd *abfd) | |
225 | { | |
226 | return make_cleanup (do_bfd_close_cleanup, abfd); | |
227 | } | |
228 | ||
f5ff8c83 AC |
229 | static void |
230 | do_close_cleanup (void *arg) | |
231 | { | |
f042532c AC |
232 | int *fd = arg; |
233 | close (*fd); | |
234 | xfree (fd); | |
f5ff8c83 AC |
235 | } |
236 | ||
237 | struct cleanup * | |
238 | make_cleanup_close (int fd) | |
239 | { | |
f042532c AC |
240 | int *saved_fd = xmalloc (sizeof (fd)); |
241 | *saved_fd = fd; | |
242 | return make_cleanup (do_close_cleanup, saved_fd); | |
f5ff8c83 AC |
243 | } |
244 | ||
11cf8741 | 245 | static void |
d9fcf2fb | 246 | do_ui_file_delete (void *arg) |
11cf8741 | 247 | { |
d9fcf2fb | 248 | ui_file_delete (arg); |
11cf8741 JM |
249 | } |
250 | ||
251 | struct cleanup * | |
d9fcf2fb | 252 | make_cleanup_ui_file_delete (struct ui_file *arg) |
11cf8741 | 253 | { |
d9fcf2fb | 254 | return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg); |
11cf8741 JM |
255 | } |
256 | ||
c906108c | 257 | struct cleanup * |
e4005526 AC |
258 | make_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function, |
259 | void *arg) | |
c906108c SS |
260 | { |
261 | register struct cleanup *new | |
c5aa993b | 262 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); |
c906108c SS |
263 | register struct cleanup *old_chain = *pmy_chain; |
264 | ||
265 | new->next = *pmy_chain; | |
266 | new->function = function; | |
267 | new->arg = arg; | |
268 | *pmy_chain = new; | |
269 | ||
270 | return old_chain; | |
271 | } | |
272 | ||
273 | /* Discard cleanups and do the actions they describe | |
274 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
275 | ||
276 | void | |
fba45db2 | 277 | do_cleanups (register struct cleanup *old_chain) |
c906108c | 278 | { |
c5aa993b | 279 | do_my_cleanups (&cleanup_chain, old_chain); |
c906108c SS |
280 | } |
281 | ||
282 | void | |
fba45db2 | 283 | do_final_cleanups (register struct cleanup *old_chain) |
c906108c | 284 | { |
c5aa993b | 285 | do_my_cleanups (&final_cleanup_chain, old_chain); |
c906108c SS |
286 | } |
287 | ||
288 | void | |
fba45db2 | 289 | do_run_cleanups (register struct cleanup *old_chain) |
c906108c | 290 | { |
c5aa993b | 291 | do_my_cleanups (&run_cleanup_chain, old_chain); |
c906108c SS |
292 | } |
293 | ||
43ff13b4 | 294 | void |
fba45db2 | 295 | do_exec_cleanups (register struct cleanup *old_chain) |
43ff13b4 | 296 | { |
c5aa993b | 297 | do_my_cleanups (&exec_cleanup_chain, old_chain); |
43ff13b4 JM |
298 | } |
299 | ||
6426a772 | 300 | void |
fba45db2 | 301 | do_exec_error_cleanups (register struct cleanup *old_chain) |
6426a772 JM |
302 | { |
303 | do_my_cleanups (&exec_error_cleanup_chain, old_chain); | |
304 | } | |
305 | ||
c906108c | 306 | void |
fba45db2 KB |
307 | do_my_cleanups (register struct cleanup **pmy_chain, |
308 | register struct cleanup *old_chain) | |
c906108c SS |
309 | { |
310 | register struct cleanup *ptr; | |
311 | while ((ptr = *pmy_chain) != old_chain) | |
312 | { | |
313 | *pmy_chain = ptr->next; /* Do this first incase recursion */ | |
314 | (*ptr->function) (ptr->arg); | |
b8c9b27d | 315 | xfree (ptr); |
c906108c SS |
316 | } |
317 | } | |
318 | ||
319 | /* Discard cleanups, not doing the actions they describe, | |
320 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
321 | ||
322 | void | |
fba45db2 | 323 | discard_cleanups (register struct cleanup *old_chain) |
c906108c | 324 | { |
c5aa993b | 325 | discard_my_cleanups (&cleanup_chain, old_chain); |
c906108c SS |
326 | } |
327 | ||
328 | void | |
fba45db2 | 329 | discard_final_cleanups (register struct cleanup *old_chain) |
c906108c | 330 | { |
c5aa993b | 331 | discard_my_cleanups (&final_cleanup_chain, old_chain); |
c906108c SS |
332 | } |
333 | ||
6426a772 | 334 | void |
fba45db2 | 335 | discard_exec_error_cleanups (register struct cleanup *old_chain) |
6426a772 JM |
336 | { |
337 | discard_my_cleanups (&exec_error_cleanup_chain, old_chain); | |
338 | } | |
339 | ||
c906108c | 340 | void |
fba45db2 KB |
341 | discard_my_cleanups (register struct cleanup **pmy_chain, |
342 | register struct cleanup *old_chain) | |
c906108c SS |
343 | { |
344 | register struct cleanup *ptr; | |
345 | while ((ptr = *pmy_chain) != old_chain) | |
346 | { | |
347 | *pmy_chain = ptr->next; | |
b8c9b27d | 348 | xfree (ptr); |
c906108c SS |
349 | } |
350 | } | |
351 | ||
352 | /* Set the cleanup_chain to 0, and return the old cleanup chain. */ | |
353 | struct cleanup * | |
fba45db2 | 354 | save_cleanups (void) |
c906108c | 355 | { |
c5aa993b | 356 | return save_my_cleanups (&cleanup_chain); |
c906108c SS |
357 | } |
358 | ||
359 | struct cleanup * | |
fba45db2 | 360 | save_final_cleanups (void) |
c906108c | 361 | { |
c5aa993b | 362 | return save_my_cleanups (&final_cleanup_chain); |
c906108c SS |
363 | } |
364 | ||
365 | struct cleanup * | |
fba45db2 | 366 | save_my_cleanups (struct cleanup **pmy_chain) |
c906108c SS |
367 | { |
368 | struct cleanup *old_chain = *pmy_chain; | |
369 | ||
370 | *pmy_chain = 0; | |
371 | return old_chain; | |
372 | } | |
373 | ||
374 | /* Restore the cleanup chain from a previously saved chain. */ | |
375 | void | |
fba45db2 | 376 | restore_cleanups (struct cleanup *chain) |
c906108c | 377 | { |
c5aa993b | 378 | restore_my_cleanups (&cleanup_chain, chain); |
c906108c SS |
379 | } |
380 | ||
381 | void | |
fba45db2 | 382 | restore_final_cleanups (struct cleanup *chain) |
c906108c | 383 | { |
c5aa993b | 384 | restore_my_cleanups (&final_cleanup_chain, chain); |
c906108c SS |
385 | } |
386 | ||
387 | void | |
fba45db2 | 388 | restore_my_cleanups (struct cleanup **pmy_chain, struct cleanup *chain) |
c906108c SS |
389 | { |
390 | *pmy_chain = chain; | |
391 | } | |
392 | ||
393 | /* This function is useful for cleanups. | |
394 | Do | |
395 | ||
c5aa993b JM |
396 | foo = xmalloc (...); |
397 | old_chain = make_cleanup (free_current_contents, &foo); | |
c906108c SS |
398 | |
399 | to arrange to free the object thus allocated. */ | |
400 | ||
401 | void | |
2f9429ae | 402 | free_current_contents (void *ptr) |
c906108c | 403 | { |
2f9429ae | 404 | void **location = ptr; |
e2f9c474 | 405 | if (location == NULL) |
8e65ff28 AC |
406 | internal_error (__FILE__, __LINE__, |
407 | "free_current_contents: NULL pointer"); | |
2f9429ae | 408 | if (*location != NULL) |
e2f9c474 | 409 | { |
b8c9b27d | 410 | xfree (*location); |
e2f9c474 AC |
411 | *location = NULL; |
412 | } | |
c906108c SS |
413 | } |
414 | ||
415 | /* Provide a known function that does nothing, to use as a base for | |
416 | for a possibly long chain of cleanups. This is useful where we | |
417 | use the cleanup chain for handling normal cleanups as well as dealing | |
418 | with cleanups that need to be done as a result of a call to error(). | |
419 | In such cases, we may not be certain where the first cleanup is, unless | |
420 | we have a do-nothing one to always use as the base. */ | |
421 | ||
422 | /* ARGSUSED */ | |
423 | void | |
e4005526 | 424 | null_cleanup (void *arg) |
c906108c SS |
425 | { |
426 | } | |
427 | ||
74f832da | 428 | /* Add a continuation to the continuation list, the global list |
c2d11a7d | 429 | cmd_continuation. The new continuation will be added at the front.*/ |
43ff13b4 | 430 | void |
74f832da KB |
431 | add_continuation (void (*continuation_hook) (struct continuation_arg *), |
432 | struct continuation_arg *arg_list) | |
43ff13b4 | 433 | { |
c5aa993b | 434 | struct continuation *continuation_ptr; |
43ff13b4 | 435 | |
c5aa993b JM |
436 | continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation)); |
437 | continuation_ptr->continuation_hook = continuation_hook; | |
438 | continuation_ptr->arg_list = arg_list; | |
439 | continuation_ptr->next = cmd_continuation; | |
440 | cmd_continuation = continuation_ptr; | |
43ff13b4 JM |
441 | } |
442 | ||
443 | /* Walk down the cmd_continuation list, and execute all the | |
c2d11a7d JM |
444 | continuations. There is a problem though. In some cases new |
445 | continuations may be added while we are in the middle of this | |
446 | loop. If this happens they will be added in the front, and done | |
447 | before we have a chance of exhausting those that were already | |
448 | there. We need to then save the beginning of the list in a pointer | |
449 | and do the continuations from there on, instead of using the | |
450 | global beginning of list as our iteration pointer.*/ | |
c5aa993b | 451 | void |
fba45db2 | 452 | do_all_continuations (void) |
c2d11a7d JM |
453 | { |
454 | struct continuation *continuation_ptr; | |
455 | struct continuation *saved_continuation; | |
456 | ||
457 | /* Copy the list header into another pointer, and set the global | |
458 | list header to null, so that the global list can change as a side | |
459 | effect of invoking the continuations and the processing of | |
460 | the preexisting continuations will not be affected. */ | |
461 | continuation_ptr = cmd_continuation; | |
462 | cmd_continuation = NULL; | |
463 | ||
464 | /* Work now on the list we have set aside. */ | |
465 | while (continuation_ptr) | |
466 | { | |
467 | (continuation_ptr->continuation_hook) (continuation_ptr->arg_list); | |
468 | saved_continuation = continuation_ptr; | |
469 | continuation_ptr = continuation_ptr->next; | |
b8c9b27d | 470 | xfree (saved_continuation); |
c2d11a7d JM |
471 | } |
472 | } | |
473 | ||
474 | /* Walk down the cmd_continuation list, and get rid of all the | |
475 | continuations. */ | |
476 | void | |
fba45db2 | 477 | discard_all_continuations (void) |
43ff13b4 | 478 | { |
c5aa993b | 479 | struct continuation *continuation_ptr; |
43ff13b4 | 480 | |
c5aa993b JM |
481 | while (cmd_continuation) |
482 | { | |
c5aa993b JM |
483 | continuation_ptr = cmd_continuation; |
484 | cmd_continuation = continuation_ptr->next; | |
b8c9b27d | 485 | xfree (continuation_ptr); |
c5aa993b | 486 | } |
43ff13b4 | 487 | } |
c2c6d25f | 488 | |
57e687d9 | 489 | /* Add a continuation to the continuation list, the global list |
c2d11a7d JM |
490 | intermediate_continuation. The new continuation will be added at the front.*/ |
491 | void | |
74f832da KB |
492 | add_intermediate_continuation (void (*continuation_hook) |
493 | (struct continuation_arg *), | |
494 | struct continuation_arg *arg_list) | |
c2d11a7d JM |
495 | { |
496 | struct continuation *continuation_ptr; | |
497 | ||
498 | continuation_ptr = (struct continuation *) xmalloc (sizeof (struct continuation)); | |
499 | continuation_ptr->continuation_hook = continuation_hook; | |
500 | continuation_ptr->arg_list = arg_list; | |
501 | continuation_ptr->next = intermediate_continuation; | |
502 | intermediate_continuation = continuation_ptr; | |
503 | } | |
504 | ||
505 | /* Walk down the cmd_continuation list, and execute all the | |
506 | continuations. There is a problem though. In some cases new | |
507 | continuations may be added while we are in the middle of this | |
508 | loop. If this happens they will be added in the front, and done | |
509 | before we have a chance of exhausting those that were already | |
510 | there. We need to then save the beginning of the list in a pointer | |
511 | and do the continuations from there on, instead of using the | |
512 | global beginning of list as our iteration pointer.*/ | |
513 | void | |
fba45db2 | 514 | do_all_intermediate_continuations (void) |
c2d11a7d JM |
515 | { |
516 | struct continuation *continuation_ptr; | |
517 | struct continuation *saved_continuation; | |
518 | ||
519 | /* Copy the list header into another pointer, and set the global | |
520 | list header to null, so that the global list can change as a side | |
521 | effect of invoking the continuations and the processing of | |
522 | the preexisting continuations will not be affected. */ | |
523 | continuation_ptr = intermediate_continuation; | |
524 | intermediate_continuation = NULL; | |
525 | ||
526 | /* Work now on the list we have set aside. */ | |
527 | while (continuation_ptr) | |
528 | { | |
529 | (continuation_ptr->continuation_hook) (continuation_ptr->arg_list); | |
530 | saved_continuation = continuation_ptr; | |
531 | continuation_ptr = continuation_ptr->next; | |
b8c9b27d | 532 | xfree (saved_continuation); |
c2d11a7d JM |
533 | } |
534 | } | |
535 | ||
c2c6d25f JM |
536 | /* Walk down the cmd_continuation list, and get rid of all the |
537 | continuations. */ | |
538 | void | |
fba45db2 | 539 | discard_all_intermediate_continuations (void) |
c2c6d25f JM |
540 | { |
541 | struct continuation *continuation_ptr; | |
542 | ||
c2d11a7d | 543 | while (intermediate_continuation) |
c2c6d25f | 544 | { |
c2d11a7d JM |
545 | continuation_ptr = intermediate_continuation; |
546 | intermediate_continuation = continuation_ptr->next; | |
b8c9b27d | 547 | xfree (continuation_ptr); |
c2c6d25f JM |
548 | } |
549 | } | |
550 | ||
c906108c | 551 | \f |
c5aa993b | 552 | |
c906108c SS |
553 | /* Print a warning message. Way to use this is to call warning_begin, |
554 | output the warning message (use unfiltered output to gdb_stderr), | |
555 | ending in a newline. There is not currently a warning_end that you | |
556 | call afterwards, but such a thing might be added if it is useful | |
557 | for a GUI to separate warning messages from other output. | |
558 | ||
559 | FIXME: Why do warnings use unfiltered output and errors filtered? | |
560 | Is this anything other than a historical accident? */ | |
561 | ||
562 | void | |
fba45db2 | 563 | warning_begin (void) |
c906108c SS |
564 | { |
565 | target_terminal_ours (); | |
c5aa993b | 566 | wrap_here (""); /* Force out any buffered output */ |
c906108c SS |
567 | gdb_flush (gdb_stdout); |
568 | if (warning_pre_print) | |
569 | fprintf_unfiltered (gdb_stderr, warning_pre_print); | |
570 | } | |
571 | ||
572 | /* Print a warning message. | |
573 | The first argument STRING is the warning message, used as a fprintf string, | |
574 | and the remaining args are passed as arguments to it. | |
575 | The primary difference between warnings and errors is that a warning | |
576 | does not force the return to command level. */ | |
577 | ||
c906108c | 578 | void |
c5aa993b | 579 | warning (const char *string,...) |
c906108c SS |
580 | { |
581 | va_list args; | |
c906108c | 582 | va_start (args, string); |
c906108c SS |
583 | if (warning_hook) |
584 | (*warning_hook) (string, args); | |
585 | else | |
c5aa993b JM |
586 | { |
587 | warning_begin (); | |
588 | vfprintf_unfiltered (gdb_stderr, string, args); | |
589 | fprintf_unfiltered (gdb_stderr, "\n"); | |
590 | va_end (args); | |
591 | } | |
c906108c SS |
592 | } |
593 | ||
594 | /* Start the printing of an error message. Way to use this is to call | |
595 | this, output the error message (use filtered output to gdb_stderr | |
596 | (FIXME: Some callers, like memory_error, use gdb_stdout)), ending | |
597 | in a newline, and then call return_to_top_level (RETURN_ERROR). | |
598 | error() provides a convenient way to do this for the special case | |
599 | that the error message can be formatted with a single printf call, | |
600 | but this is more general. */ | |
601 | void | |
fba45db2 | 602 | error_begin (void) |
c906108c SS |
603 | { |
604 | if (error_begin_hook) | |
605 | error_begin_hook (); | |
606 | ||
607 | target_terminal_ours (); | |
c5aa993b | 608 | wrap_here (""); /* Force out any buffered output */ |
c906108c SS |
609 | gdb_flush (gdb_stdout); |
610 | ||
611 | annotate_error_begin (); | |
612 | ||
613 | if (error_pre_print) | |
614 | fprintf_filtered (gdb_stderr, error_pre_print); | |
615 | } | |
616 | ||
617 | /* Print an error message and return to command level. | |
618 | The first argument STRING is the error message, used as a fprintf string, | |
619 | and the remaining args are passed as arguments to it. */ | |
620 | ||
4ce44c66 JM |
621 | NORETURN void |
622 | verror (const char *string, va_list args) | |
623 | { | |
c2d11a7d JM |
624 | char *err_string; |
625 | struct cleanup *err_string_cleanup; | |
4ce44c66 | 626 | /* FIXME: cagney/1999-11-10: All error calls should come here. |
e26cc349 | 627 | Unfortunately some code uses the sequence: error_begin(); print |
4ce44c66 JM |
628 | error message; return_to_top_level. That code should be |
629 | flushed. */ | |
630 | error_begin (); | |
c2d11a7d JM |
631 | /* NOTE: It's tempting to just do the following... |
632 | vfprintf_filtered (gdb_stderr, string, args); | |
633 | and then follow with a similar looking statement to cause the message | |
634 | to also go to gdb_lasterr. But if we do this, we'll be traversing the | |
635 | va_list twice which works on some platforms and fails miserably on | |
636 | others. */ | |
637 | /* Save it as the last error */ | |
d9fcf2fb | 638 | ui_file_rewind (gdb_lasterr); |
4ce44c66 | 639 | vfprintf_filtered (gdb_lasterr, string, args); |
c2d11a7d JM |
640 | /* Retrieve the last error and print it to gdb_stderr */ |
641 | err_string = error_last_message (); | |
b8c9b27d | 642 | err_string_cleanup = make_cleanup (xfree, err_string); |
c2d11a7d JM |
643 | fputs_filtered (err_string, gdb_stderr); |
644 | fprintf_filtered (gdb_stderr, "\n"); | |
645 | do_cleanups (err_string_cleanup); | |
4ce44c66 JM |
646 | return_to_top_level (RETURN_ERROR); |
647 | } | |
648 | ||
c906108c | 649 | NORETURN void |
c5aa993b | 650 | error (const char *string,...) |
c906108c SS |
651 | { |
652 | va_list args; | |
c906108c | 653 | va_start (args, string); |
4ce44c66 JM |
654 | verror (string, args); |
655 | va_end (args); | |
c906108c SS |
656 | } |
657 | ||
2acceee2 | 658 | NORETURN void |
d9fcf2fb | 659 | error_stream (struct ui_file *stream) |
2acceee2 | 660 | { |
4ce44c66 | 661 | long size; |
d9fcf2fb | 662 | char *msg = ui_file_xstrdup (stream, &size); |
b8c9b27d | 663 | make_cleanup (xfree, msg); |
4ce44c66 | 664 | error ("%s", msg); |
2acceee2 JM |
665 | } |
666 | ||
667 | /* Get the last error message issued by gdb */ | |
668 | ||
669 | char * | |
670 | error_last_message (void) | |
671 | { | |
4ce44c66 | 672 | long len; |
d9fcf2fb | 673 | return ui_file_xstrdup (gdb_lasterr, &len); |
2acceee2 | 674 | } |
4ce44c66 | 675 | |
2acceee2 JM |
676 | /* This is to be called by main() at the very beginning */ |
677 | ||
678 | void | |
679 | error_init (void) | |
680 | { | |
4ce44c66 | 681 | gdb_lasterr = mem_fileopen (); |
2acceee2 | 682 | } |
c906108c | 683 | |
96baa820 JM |
684 | /* Print a message reporting an internal error. Ask the user if they |
685 | want to continue, dump core, or just exit. */ | |
c906108c | 686 | |
c906108c | 687 | NORETURN void |
8e65ff28 AC |
688 | internal_verror (const char *file, int line, |
689 | const char *fmt, va_list ap) | |
c906108c | 690 | { |
96baa820 JM |
691 | static char msg[] = "Internal GDB error: recursive internal error.\n"; |
692 | static int dejavu = 0; | |
7be570e7 JM |
693 | int continue_p; |
694 | int dump_core_p; | |
c906108c | 695 | |
96baa820 JM |
696 | /* don't allow infinite error recursion. */ |
697 | switch (dejavu) | |
698 | { | |
699 | case 0: | |
700 | dejavu = 1; | |
701 | break; | |
702 | case 1: | |
703 | dejavu = 2; | |
704 | fputs_unfiltered (msg, gdb_stderr); | |
e1e9e218 | 705 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
96baa820 JM |
706 | default: |
707 | dejavu = 3; | |
708 | write (STDERR_FILENO, msg, sizeof (msg)); | |
709 | exit (1); | |
710 | } | |
c906108c | 711 | |
96baa820 | 712 | /* Try to get the message out */ |
4261bedc | 713 | target_terminal_ours (); |
8e65ff28 | 714 | fprintf_unfiltered (gdb_stderr, "%s:%d: gdb-internal-error: ", file, line); |
4ce44c66 | 715 | vfprintf_unfiltered (gdb_stderr, fmt, ap); |
96baa820 | 716 | fputs_unfiltered ("\n", gdb_stderr); |
c906108c | 717 | |
7be570e7 JM |
718 | /* Default (no case) is to quit GDB. When in batch mode this |
719 | lessens the likelhood of GDB going into an infinate loop. */ | |
720 | continue_p = query ("\ | |
62fd9fad | 721 | An internal GDB error was detected. This may make further\n\ |
7be570e7 JM |
722 | debugging unreliable. Continue this debugging session? "); |
723 | ||
724 | /* Default (no case) is to not dump core. Lessen the chance of GDB | |
725 | leaving random core files around. */ | |
726 | dump_core_p = query ("\ | |
727 | Create a core file containing the current state of GDB? "); | |
728 | ||
729 | if (continue_p) | |
730 | { | |
731 | if (dump_core_p) | |
732 | { | |
733 | if (fork () == 0) | |
e1e9e218 | 734 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
7be570e7 JM |
735 | } |
736 | } | |
737 | else | |
738 | { | |
739 | if (dump_core_p) | |
e1e9e218 | 740 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
7be570e7 JM |
741 | else |
742 | exit (1); | |
743 | } | |
96baa820 JM |
744 | |
745 | dejavu = 0; | |
746 | return_to_top_level (RETURN_ERROR); | |
c906108c SS |
747 | } |
748 | ||
4ce44c66 | 749 | NORETURN void |
8e65ff28 | 750 | internal_error (const char *file, int line, const char *string, ...) |
4ce44c66 JM |
751 | { |
752 | va_list ap; | |
753 | va_start (ap, string); | |
4261bedc | 754 | |
8e65ff28 | 755 | internal_verror (file, line, string, ap); |
4ce44c66 JM |
756 | va_end (ap); |
757 | } | |
758 | ||
c906108c SS |
759 | /* The strerror() function can return NULL for errno values that are |
760 | out of range. Provide a "safe" version that always returns a | |
761 | printable string. */ | |
762 | ||
763 | char * | |
fba45db2 | 764 | safe_strerror (int errnum) |
c906108c SS |
765 | { |
766 | char *msg; | |
767 | static char buf[32]; | |
768 | ||
769 | if ((msg = strerror (errnum)) == NULL) | |
770 | { | |
771 | sprintf (buf, "(undocumented errno %d)", errnum); | |
772 | msg = buf; | |
773 | } | |
774 | return (msg); | |
775 | } | |
776 | ||
c906108c SS |
777 | /* Print the system error message for errno, and also mention STRING |
778 | as the file name for which the error was encountered. | |
779 | Then return to command level. */ | |
780 | ||
781 | NORETURN void | |
fba45db2 | 782 | perror_with_name (char *string) |
c906108c SS |
783 | { |
784 | char *err; | |
785 | char *combined; | |
786 | ||
787 | err = safe_strerror (errno); | |
788 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
789 | strcpy (combined, string); | |
790 | strcat (combined, ": "); | |
791 | strcat (combined, err); | |
792 | ||
793 | /* I understand setting these is a matter of taste. Still, some people | |
794 | may clear errno but not know about bfd_error. Doing this here is not | |
795 | unreasonable. */ | |
796 | bfd_set_error (bfd_error_no_error); | |
797 | errno = 0; | |
798 | ||
c5aa993b | 799 | error ("%s.", combined); |
c906108c SS |
800 | } |
801 | ||
802 | /* Print the system error message for ERRCODE, and also mention STRING | |
803 | as the file name for which the error was encountered. */ | |
804 | ||
805 | void | |
fba45db2 | 806 | print_sys_errmsg (char *string, int errcode) |
c906108c SS |
807 | { |
808 | char *err; | |
809 | char *combined; | |
810 | ||
811 | err = safe_strerror (errcode); | |
812 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
813 | strcpy (combined, string); | |
814 | strcat (combined, ": "); | |
815 | strcat (combined, err); | |
816 | ||
817 | /* We want anything which was printed on stdout to come out first, before | |
818 | this message. */ | |
819 | gdb_flush (gdb_stdout); | |
820 | fprintf_unfiltered (gdb_stderr, "%s.\n", combined); | |
821 | } | |
822 | ||
823 | /* Control C eventually causes this to be called, at a convenient time. */ | |
824 | ||
825 | void | |
fba45db2 | 826 | quit (void) |
c906108c SS |
827 | { |
828 | serial_t gdb_stdout_serial = serial_fdopen (1); | |
829 | ||
830 | target_terminal_ours (); | |
831 | ||
832 | /* We want all output to appear now, before we print "Quit". We | |
833 | have 3 levels of buffering we have to flush (it's possible that | |
834 | some of these should be changed to flush the lower-level ones | |
835 | too): */ | |
836 | ||
837 | /* 1. The _filtered buffer. */ | |
c5aa993b | 838 | wrap_here ((char *) 0); |
c906108c SS |
839 | |
840 | /* 2. The stdio buffer. */ | |
841 | gdb_flush (gdb_stdout); | |
842 | gdb_flush (gdb_stderr); | |
843 | ||
844 | /* 3. The system-level buffer. */ | |
845 | SERIAL_DRAIN_OUTPUT (gdb_stdout_serial); | |
846 | SERIAL_UN_FDOPEN (gdb_stdout_serial); | |
847 | ||
848 | annotate_error_begin (); | |
849 | ||
850 | /* Don't use *_filtered; we don't want to prompt the user to continue. */ | |
851 | if (quit_pre_print) | |
852 | fprintf_unfiltered (gdb_stderr, quit_pre_print); | |
853 | ||
7be570e7 JM |
854 | #ifdef __MSDOS__ |
855 | /* No steenking SIGINT will ever be coming our way when the | |
856 | program is resumed. Don't lie. */ | |
857 | fprintf_unfiltered (gdb_stderr, "Quit\n"); | |
858 | #else | |
c906108c | 859 | if (job_control |
c5aa993b JM |
860 | /* If there is no terminal switching for this target, then we can't |
861 | possibly get screwed by the lack of job control. */ | |
c906108c SS |
862 | || current_target.to_terminal_ours == NULL) |
863 | fprintf_unfiltered (gdb_stderr, "Quit\n"); | |
864 | else | |
865 | fprintf_unfiltered (gdb_stderr, | |
c5aa993b | 866 | "Quit (expect signal SIGINT when the program is resumed)\n"); |
7be570e7 | 867 | #endif |
c906108c SS |
868 | return_to_top_level (RETURN_QUIT); |
869 | } | |
870 | ||
871 | ||
7be570e7 | 872 | #if defined(_MSC_VER) /* should test for wingdb instead? */ |
c906108c SS |
873 | |
874 | /* | |
875 | * Windows translates all keyboard and mouse events | |
876 | * into a message which is appended to the message | |
877 | * queue for the process. | |
878 | */ | |
879 | ||
c5aa993b | 880 | void |
fba45db2 | 881 | notice_quit (void) |
c906108c | 882 | { |
c5aa993b | 883 | int k = win32pollquit (); |
c906108c SS |
884 | if (k == 1) |
885 | quit_flag = 1; | |
886 | else if (k == 2) | |
887 | immediate_quit = 1; | |
888 | } | |
889 | ||
4ce44c66 | 890 | #else /* !defined(_MSC_VER) */ |
c906108c | 891 | |
c5aa993b | 892 | void |
fba45db2 | 893 | notice_quit (void) |
c906108c SS |
894 | { |
895 | /* Done by signals */ | |
896 | } | |
897 | ||
4ce44c66 | 898 | #endif /* !defined(_MSC_VER) */ |
c906108c | 899 | |
c906108c | 900 | /* Control C comes here */ |
c906108c | 901 | void |
fba45db2 | 902 | request_quit (int signo) |
c906108c SS |
903 | { |
904 | quit_flag = 1; | |
905 | /* Restore the signal handler. Harmless with BSD-style signals, needed | |
906 | for System V-style signals. So just always do it, rather than worrying | |
907 | about USG defines and stuff like that. */ | |
908 | signal (signo, request_quit); | |
909 | ||
910 | #ifdef REQUEST_QUIT | |
911 | REQUEST_QUIT; | |
912 | #else | |
c5aa993b | 913 | if (immediate_quit) |
c906108c SS |
914 | quit (); |
915 | #endif | |
916 | } | |
c906108c SS |
917 | \f |
918 | /* Memory management stuff (malloc friends). */ | |
919 | ||
c906108c SS |
920 | #if !defined (USE_MMALLOC) |
921 | ||
c0e61796 AC |
922 | /* NOTE: These must use PTR so that their definition matches the |
923 | declaration found in "mmalloc.h". */ | |
ed9a39eb | 924 | |
c906108c | 925 | PTR |
fba45db2 | 926 | mmalloc (PTR md, size_t size) |
c906108c | 927 | { |
c0e61796 | 928 | return malloc (size); /* NOTE: GDB's only call to malloc() */ |
c906108c SS |
929 | } |
930 | ||
931 | PTR | |
fba45db2 | 932 | mrealloc (PTR md, PTR ptr, size_t size) |
c906108c | 933 | { |
c5aa993b | 934 | if (ptr == 0) /* Guard against old realloc's */ |
c0e61796 | 935 | return mmalloc (md, size); |
c906108c | 936 | else |
c0e61796 AC |
937 | return realloc (ptr, size); /* NOTE: GDB's only call to ralloc() */ |
938 | } | |
939 | ||
940 | PTR | |
941 | mcalloc (PTR md, size_t number, size_t size) | |
942 | { | |
943 | return calloc (number, size); /* NOTE: GDB's only call to calloc() */ | |
c906108c SS |
944 | } |
945 | ||
946 | void | |
fba45db2 | 947 | mfree (PTR md, PTR ptr) |
c906108c | 948 | { |
c0e61796 | 949 | free (ptr); /* NOTE: GDB's only call to free() */ |
c906108c SS |
950 | } |
951 | ||
c5aa993b | 952 | #endif /* USE_MMALLOC */ |
c906108c SS |
953 | |
954 | #if !defined (USE_MMALLOC) || defined (NO_MMCHECK) | |
955 | ||
956 | void | |
082faf24 | 957 | init_malloc (void *md) |
c906108c SS |
958 | { |
959 | } | |
960 | ||
961 | #else /* Have mmalloc and want corruption checking */ | |
962 | ||
963 | static void | |
fba45db2 | 964 | malloc_botch (void) |
c906108c | 965 | { |
96baa820 | 966 | fprintf_unfiltered (gdb_stderr, "Memory corruption\n"); |
e1e9e218 | 967 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c SS |
968 | } |
969 | ||
970 | /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified | |
971 | by MD, to detect memory corruption. Note that MD may be NULL to specify | |
972 | the default heap that grows via sbrk. | |
973 | ||
974 | Note that for freshly created regions, we must call mmcheckf prior to any | |
975 | mallocs in the region. Otherwise, any region which was allocated prior to | |
976 | installing the checking hooks, which is later reallocated or freed, will | |
977 | fail the checks! The mmcheck function only allows initial hooks to be | |
978 | installed before the first mmalloc. However, anytime after we have called | |
979 | mmcheck the first time to install the checking hooks, we can call it again | |
980 | to update the function pointer to the memory corruption handler. | |
981 | ||
982 | Returns zero on failure, non-zero on success. */ | |
983 | ||
984 | #ifndef MMCHECK_FORCE | |
985 | #define MMCHECK_FORCE 0 | |
986 | #endif | |
987 | ||
988 | void | |
082faf24 | 989 | init_malloc (void *md) |
c906108c SS |
990 | { |
991 | if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE)) | |
992 | { | |
993 | /* Don't use warning(), which relies on current_target being set | |
c5aa993b JM |
994 | to something other than dummy_target, until after |
995 | initialize_all_files(). */ | |
c906108c SS |
996 | |
997 | fprintf_unfiltered | |
998 | (gdb_stderr, "warning: failed to install memory consistency checks; "); | |
999 | fprintf_unfiltered | |
1000 | (gdb_stderr, "configuration should define NO_MMCHECK or MMCHECK_FORCE\n"); | |
1001 | } | |
1002 | ||
1003 | mmtrace (); | |
1004 | } | |
1005 | ||
1006 | #endif /* Have mmalloc and want corruption checking */ | |
1007 | ||
1008 | /* Called when a memory allocation fails, with the number of bytes of | |
1009 | memory requested in SIZE. */ | |
1010 | ||
1011 | NORETURN void | |
fba45db2 | 1012 | nomem (long size) |
c906108c SS |
1013 | { |
1014 | if (size > 0) | |
1015 | { | |
8e65ff28 AC |
1016 | internal_error (__FILE__, __LINE__, |
1017 | "virtual memory exhausted: can't allocate %ld bytes.", size); | |
c906108c SS |
1018 | } |
1019 | else | |
1020 | { | |
8e65ff28 AC |
1021 | internal_error (__FILE__, __LINE__, |
1022 | "virtual memory exhausted."); | |
c906108c SS |
1023 | } |
1024 | } | |
1025 | ||
c0e61796 | 1026 | /* The xmmalloc() family of memory management routines. |
c906108c | 1027 | |
c0e61796 AC |
1028 | These are are like the mmalloc() family except that they implement |
1029 | consistent semantics and guard against typical memory management | |
1030 | problems: if a malloc fails, an internal error is thrown; if | |
1031 | free(NULL) is called, it is ignored; if *alloc(0) is called, NULL | |
1032 | is returned. | |
1033 | ||
1034 | All these routines are implemented using the mmalloc() family. */ | |
1035 | ||
1036 | void * | |
1037 | xmmalloc (void *md, size_t size) | |
c906108c | 1038 | { |
c0e61796 | 1039 | void *val; |
c906108c SS |
1040 | |
1041 | if (size == 0) | |
1042 | { | |
1043 | val = NULL; | |
1044 | } | |
c0e61796 | 1045 | else |
c906108c | 1046 | { |
c0e61796 AC |
1047 | val = mmalloc (md, size); |
1048 | if (val == NULL) | |
1049 | nomem (size); | |
c906108c SS |
1050 | } |
1051 | return (val); | |
1052 | } | |
1053 | ||
c0e61796 AC |
1054 | void * |
1055 | xmrealloc (void *md, void *ptr, size_t size) | |
c906108c | 1056 | { |
c0e61796 | 1057 | void *val; |
c906108c | 1058 | |
d7fa9de0 | 1059 | if (size == 0) |
c906108c | 1060 | { |
d7fa9de0 KB |
1061 | if (ptr != NULL) |
1062 | mfree (md, ptr); | |
1063 | val = NULL; | |
c906108c SS |
1064 | } |
1065 | else | |
1066 | { | |
d7fa9de0 KB |
1067 | if (ptr != NULL) |
1068 | { | |
1069 | val = mrealloc (md, ptr, size); | |
1070 | } | |
1071 | else | |
1072 | { | |
1073 | val = mmalloc (md, size); | |
1074 | } | |
1075 | if (val == NULL) | |
1076 | { | |
1077 | nomem (size); | |
1078 | } | |
c906108c SS |
1079 | } |
1080 | return (val); | |
1081 | } | |
1082 | ||
c0e61796 AC |
1083 | void * |
1084 | xmcalloc (void *md, size_t number, size_t size) | |
ed9a39eb | 1085 | { |
d7fa9de0 | 1086 | void *mem; |
d7fa9de0 KB |
1087 | if (number == 0 || size == 0) |
1088 | mem = NULL; | |
1089 | else | |
1090 | { | |
c0e61796 | 1091 | mem = mcalloc (md, number, size); |
d7fa9de0 KB |
1092 | if (mem == NULL) |
1093 | nomem (number * size); | |
1094 | } | |
ed9a39eb JM |
1095 | return mem; |
1096 | } | |
1097 | ||
c0e61796 AC |
1098 | void |
1099 | xmfree (void *md, void *ptr) | |
1100 | { | |
1101 | if (ptr != NULL) | |
1102 | mfree (md, ptr); | |
1103 | } | |
1104 | ||
1105 | /* The xmalloc() (libiberty.h) family of memory management routines. | |
1106 | ||
1107 | These are like the ISO-C malloc() family except that they implement | |
1108 | consistent semantics and guard against typical memory management | |
1109 | problems. See xmmalloc() above for further information. | |
1110 | ||
1111 | All these routines are wrappers to the xmmalloc() family. */ | |
1112 | ||
1113 | /* NOTE: These are declared using PTR to ensure consistency with | |
1114 | "libiberty.h". xfree() is GDB local. */ | |
1115 | ||
1116 | PTR | |
1117 | xmalloc (size_t size) | |
1118 | { | |
1119 | return xmmalloc (NULL, size); | |
1120 | } | |
c906108c SS |
1121 | |
1122 | PTR | |
fba45db2 | 1123 | xrealloc (PTR ptr, size_t size) |
c906108c | 1124 | { |
c0e61796 | 1125 | return xmrealloc (NULL, ptr, size); |
c906108c | 1126 | } |
b8c9b27d | 1127 | |
c0e61796 AC |
1128 | PTR |
1129 | xcalloc (size_t number, size_t size) | |
1130 | { | |
1131 | return xmcalloc (NULL, number, size); | |
1132 | } | |
b8c9b27d KB |
1133 | |
1134 | void | |
1135 | xfree (void *ptr) | |
1136 | { | |
c0e61796 | 1137 | xmfree (NULL, ptr); |
b8c9b27d | 1138 | } |
c906108c | 1139 | \f |
c5aa993b | 1140 | |
76995688 AC |
1141 | /* Like asprintf/vasprintf but get an internal_error if the call |
1142 | fails. */ | |
1143 | ||
1144 | void | |
1145 | xasprintf (char **ret, const char *format, ...) | |
1146 | { | |
1147 | va_list args; | |
1148 | va_start (args, format); | |
1149 | xvasprintf (ret, format, args); | |
1150 | va_end (args); | |
1151 | } | |
1152 | ||
1153 | void | |
1154 | xvasprintf (char **ret, const char *format, va_list ap) | |
1155 | { | |
1156 | int status = vasprintf (ret, format, ap); | |
1157 | /* NULL could be returned due to a memory allocation problem; a | |
1158 | badly format string; or something else. */ | |
1159 | if ((*ret) == NULL) | |
8e65ff28 AC |
1160 | internal_error (__FILE__, __LINE__, |
1161 | "vasprintf returned NULL buffer (errno %d)", | |
1162 | errno); | |
76995688 AC |
1163 | /* A negative status with a non-NULL buffer shouldn't never |
1164 | happen. But to be sure. */ | |
1165 | if (status < 0) | |
8e65ff28 AC |
1166 | internal_error (__FILE__, __LINE__, |
1167 | "vasprintf call failed (errno %d)", | |
1168 | errno); | |
76995688 AC |
1169 | } |
1170 | ||
1171 | ||
c906108c SS |
1172 | /* My replacement for the read system call. |
1173 | Used like `read' but keeps going if `read' returns too soon. */ | |
1174 | ||
1175 | int | |
fba45db2 | 1176 | myread (int desc, char *addr, int len) |
c906108c SS |
1177 | { |
1178 | register int val; | |
1179 | int orglen = len; | |
1180 | ||
1181 | while (len > 0) | |
1182 | { | |
1183 | val = read (desc, addr, len); | |
1184 | if (val < 0) | |
1185 | return val; | |
1186 | if (val == 0) | |
1187 | return orglen - len; | |
1188 | len -= val; | |
1189 | addr += val; | |
1190 | } | |
1191 | return orglen; | |
1192 | } | |
1193 | \f | |
1194 | /* Make a copy of the string at PTR with SIZE characters | |
1195 | (and add a null character at the end in the copy). | |
1196 | Uses malloc to get the space. Returns the address of the copy. */ | |
1197 | ||
1198 | char * | |
5565b556 | 1199 | savestring (const char *ptr, size_t size) |
c906108c SS |
1200 | { |
1201 | register char *p = (char *) xmalloc (size + 1); | |
1202 | memcpy (p, ptr, size); | |
1203 | p[size] = 0; | |
1204 | return p; | |
1205 | } | |
1206 | ||
1207 | char * | |
5565b556 | 1208 | msavestring (void *md, const char *ptr, size_t size) |
c906108c SS |
1209 | { |
1210 | register char *p = (char *) xmmalloc (md, size + 1); | |
1211 | memcpy (p, ptr, size); | |
1212 | p[size] = 0; | |
1213 | return p; | |
1214 | } | |
1215 | ||
c906108c | 1216 | char * |
082faf24 | 1217 | mstrsave (void *md, const char *ptr) |
c906108c SS |
1218 | { |
1219 | return (msavestring (md, ptr, strlen (ptr))); | |
1220 | } | |
1221 | ||
1222 | void | |
fba45db2 | 1223 | print_spaces (register int n, register struct ui_file *file) |
c906108c | 1224 | { |
392a587b | 1225 | fputs_unfiltered (n_spaces (n), file); |
c906108c SS |
1226 | } |
1227 | ||
1228 | /* Print a host address. */ | |
1229 | ||
1230 | void | |
d9fcf2fb | 1231 | gdb_print_host_address (void *addr, struct ui_file *stream) |
c906108c SS |
1232 | { |
1233 | ||
1234 | /* We could use the %p conversion specifier to fprintf if we had any | |
1235 | way of knowing whether this host supports it. But the following | |
1236 | should work on the Alpha and on 32 bit machines. */ | |
1237 | ||
c5aa993b | 1238 | fprintf_filtered (stream, "0x%lx", (unsigned long) addr); |
c906108c SS |
1239 | } |
1240 | ||
1241 | /* Ask user a y-or-n question and return 1 iff answer is yes. | |
1242 | Takes three args which are given to printf to print the question. | |
1243 | The first, a control string, should end in "? ". | |
1244 | It should not say how to answer, because we do that. */ | |
1245 | ||
1246 | /* VARARGS */ | |
1247 | int | |
c5aa993b | 1248 | query (char *ctlstr,...) |
c906108c SS |
1249 | { |
1250 | va_list args; | |
1251 | register int answer; | |
1252 | register int ans2; | |
1253 | int retval; | |
1254 | ||
c906108c | 1255 | va_start (args, ctlstr); |
c906108c SS |
1256 | |
1257 | if (query_hook) | |
1258 | { | |
1259 | return query_hook (ctlstr, args); | |
1260 | } | |
1261 | ||
1262 | /* Automatically answer "yes" if input is not from a terminal. */ | |
1263 | if (!input_from_terminal_p ()) | |
1264 | return 1; | |
1265 | #ifdef MPW | |
1266 | /* FIXME Automatically answer "yes" if called from MacGDB. */ | |
1267 | if (mac_app) | |
1268 | return 1; | |
1269 | #endif /* MPW */ | |
1270 | ||
1271 | while (1) | |
1272 | { | |
1273 | wrap_here (""); /* Flush any buffered output */ | |
1274 | gdb_flush (gdb_stdout); | |
1275 | ||
1276 | if (annotation_level > 1) | |
1277 | printf_filtered ("\n\032\032pre-query\n"); | |
1278 | ||
1279 | vfprintf_filtered (gdb_stdout, ctlstr, args); | |
1280 | printf_filtered ("(y or n) "); | |
1281 | ||
1282 | if (annotation_level > 1) | |
1283 | printf_filtered ("\n\032\032query\n"); | |
1284 | ||
1285 | #ifdef MPW | |
1286 | /* If not in MacGDB, move to a new line so the entered line doesn't | |
c5aa993b | 1287 | have a prompt on the front of it. */ |
c906108c SS |
1288 | if (!mac_app) |
1289 | fputs_unfiltered ("\n", gdb_stdout); | |
1290 | #endif /* MPW */ | |
1291 | ||
c5aa993b | 1292 | wrap_here (""); |
c906108c SS |
1293 | gdb_flush (gdb_stdout); |
1294 | ||
1295 | #if defined(TUI) | |
c5aa993b | 1296 | if (!tui_version || cmdWin == tuiWinWithFocus ()) |
c906108c SS |
1297 | #endif |
1298 | answer = fgetc (stdin); | |
1299 | #if defined(TUI) | |
1300 | else | |
c5aa993b | 1301 | answer = (unsigned char) tuiBufferGetc (); |
c906108c SS |
1302 | |
1303 | #endif | |
1304 | clearerr (stdin); /* in case of C-d */ | |
1305 | if (answer == EOF) /* C-d */ | |
c5aa993b | 1306 | { |
c906108c SS |
1307 | retval = 1; |
1308 | break; | |
1309 | } | |
1310 | /* Eat rest of input line, to EOF or newline */ | |
1311 | if ((answer != '\n') || (tui_version && answer != '\r')) | |
c5aa993b | 1312 | do |
c906108c SS |
1313 | { |
1314 | #if defined(TUI) | |
c5aa993b | 1315 | if (!tui_version || cmdWin == tuiWinWithFocus ()) |
c906108c SS |
1316 | #endif |
1317 | ans2 = fgetc (stdin); | |
1318 | #if defined(TUI) | |
1319 | else | |
c5aa993b | 1320 | ans2 = (unsigned char) tuiBufferGetc (); |
c906108c SS |
1321 | #endif |
1322 | clearerr (stdin); | |
1323 | } | |
c5aa993b JM |
1324 | while (ans2 != EOF && ans2 != '\n' && ans2 != '\r'); |
1325 | TUIDO (((TuiOpaqueFuncPtr) tui_vStartNewLines, 1)); | |
c906108c SS |
1326 | |
1327 | if (answer >= 'a') | |
1328 | answer -= 040; | |
1329 | if (answer == 'Y') | |
1330 | { | |
1331 | retval = 1; | |
1332 | break; | |
1333 | } | |
1334 | if (answer == 'N') | |
1335 | { | |
1336 | retval = 0; | |
1337 | break; | |
1338 | } | |
1339 | printf_filtered ("Please answer y or n.\n"); | |
1340 | } | |
1341 | ||
1342 | if (annotation_level > 1) | |
1343 | printf_filtered ("\n\032\032post-query\n"); | |
1344 | return retval; | |
1345 | } | |
c906108c | 1346 | \f |
c5aa993b | 1347 | |
c906108c SS |
1348 | /* Parse a C escape sequence. STRING_PTR points to a variable |
1349 | containing a pointer to the string to parse. That pointer | |
1350 | should point to the character after the \. That pointer | |
1351 | is updated past the characters we use. The value of the | |
1352 | escape sequence is returned. | |
1353 | ||
1354 | A negative value means the sequence \ newline was seen, | |
1355 | which is supposed to be equivalent to nothing at all. | |
1356 | ||
1357 | If \ is followed by a null character, we return a negative | |
1358 | value and leave the string pointer pointing at the null character. | |
1359 | ||
1360 | If \ is followed by 000, we return 0 and leave the string pointer | |
1361 | after the zeros. A value of 0 does not mean end of string. */ | |
1362 | ||
1363 | int | |
fba45db2 | 1364 | parse_escape (char **string_ptr) |
c906108c SS |
1365 | { |
1366 | register int c = *(*string_ptr)++; | |
1367 | switch (c) | |
1368 | { | |
1369 | case 'a': | |
1370 | return 007; /* Bell (alert) char */ | |
1371 | case 'b': | |
1372 | return '\b'; | |
1373 | case 'e': /* Escape character */ | |
1374 | return 033; | |
1375 | case 'f': | |
1376 | return '\f'; | |
1377 | case 'n': | |
1378 | return '\n'; | |
1379 | case 'r': | |
1380 | return '\r'; | |
1381 | case 't': | |
1382 | return '\t'; | |
1383 | case 'v': | |
1384 | return '\v'; | |
1385 | case '\n': | |
1386 | return -2; | |
1387 | case 0: | |
1388 | (*string_ptr)--; | |
1389 | return 0; | |
1390 | case '^': | |
1391 | c = *(*string_ptr)++; | |
1392 | if (c == '\\') | |
1393 | c = parse_escape (string_ptr); | |
1394 | if (c == '?') | |
1395 | return 0177; | |
1396 | return (c & 0200) | (c & 037); | |
c5aa993b | 1397 | |
c906108c SS |
1398 | case '0': |
1399 | case '1': | |
1400 | case '2': | |
1401 | case '3': | |
1402 | case '4': | |
1403 | case '5': | |
1404 | case '6': | |
1405 | case '7': | |
1406 | { | |
1407 | register int i = c - '0'; | |
1408 | register int count = 0; | |
1409 | while (++count < 3) | |
1410 | { | |
1411 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') | |
1412 | { | |
1413 | i *= 8; | |
1414 | i += c - '0'; | |
1415 | } | |
1416 | else | |
1417 | { | |
1418 | (*string_ptr)--; | |
1419 | break; | |
1420 | } | |
1421 | } | |
1422 | return i; | |
1423 | } | |
1424 | default: | |
1425 | return c; | |
1426 | } | |
1427 | } | |
1428 | \f | |
1429 | /* Print the character C on STREAM as part of the contents of a literal | |
1430 | string whose delimiter is QUOTER. Note that this routine should only | |
1431 | be call for printing things which are independent of the language | |
1432 | of the program being debugged. */ | |
1433 | ||
43e526b9 | 1434 | static void |
74f832da KB |
1435 | printchar (int c, void (*do_fputs) (const char *, struct ui_file *), |
1436 | void (*do_fprintf) (struct ui_file *, const char *, ...), | |
1437 | struct ui_file *stream, int quoter) | |
c906108c SS |
1438 | { |
1439 | ||
1440 | c &= 0xFF; /* Avoid sign bit follies */ | |
1441 | ||
c5aa993b JM |
1442 | if (c < 0x20 || /* Low control chars */ |
1443 | (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ | |
1444 | (sevenbit_strings && c >= 0x80)) | |
1445 | { /* high order bit set */ | |
1446 | switch (c) | |
1447 | { | |
1448 | case '\n': | |
43e526b9 | 1449 | do_fputs ("\\n", stream); |
c5aa993b JM |
1450 | break; |
1451 | case '\b': | |
43e526b9 | 1452 | do_fputs ("\\b", stream); |
c5aa993b JM |
1453 | break; |
1454 | case '\t': | |
43e526b9 | 1455 | do_fputs ("\\t", stream); |
c5aa993b JM |
1456 | break; |
1457 | case '\f': | |
43e526b9 | 1458 | do_fputs ("\\f", stream); |
c5aa993b JM |
1459 | break; |
1460 | case '\r': | |
43e526b9 | 1461 | do_fputs ("\\r", stream); |
c5aa993b JM |
1462 | break; |
1463 | case '\033': | |
43e526b9 | 1464 | do_fputs ("\\e", stream); |
c5aa993b JM |
1465 | break; |
1466 | case '\007': | |
43e526b9 | 1467 | do_fputs ("\\a", stream); |
c5aa993b JM |
1468 | break; |
1469 | default: | |
43e526b9 | 1470 | do_fprintf (stream, "\\%.3o", (unsigned int) c); |
c5aa993b JM |
1471 | break; |
1472 | } | |
1473 | } | |
1474 | else | |
1475 | { | |
1476 | if (c == '\\' || c == quoter) | |
43e526b9 JM |
1477 | do_fputs ("\\", stream); |
1478 | do_fprintf (stream, "%c", c); | |
c5aa993b | 1479 | } |
c906108c | 1480 | } |
43e526b9 JM |
1481 | |
1482 | /* Print the character C on STREAM as part of the contents of a | |
1483 | literal string whose delimiter is QUOTER. Note that these routines | |
1484 | should only be call for printing things which are independent of | |
1485 | the language of the program being debugged. */ | |
1486 | ||
1487 | void | |
fba45db2 | 1488 | fputstr_filtered (const char *str, int quoter, struct ui_file *stream) |
43e526b9 JM |
1489 | { |
1490 | while (*str) | |
1491 | printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter); | |
1492 | } | |
1493 | ||
1494 | void | |
fba45db2 | 1495 | fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream) |
43e526b9 JM |
1496 | { |
1497 | while (*str) | |
1498 | printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter); | |
1499 | } | |
1500 | ||
1501 | void | |
fba45db2 | 1502 | fputstrn_unfiltered (const char *str, int n, int quoter, struct ui_file *stream) |
43e526b9 JM |
1503 | { |
1504 | int i; | |
1505 | for (i = 0; i < n; i++) | |
1506 | printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter); | |
1507 | } | |
1508 | ||
c906108c | 1509 | \f |
c5aa993b | 1510 | |
c906108c SS |
1511 | /* Number of lines per page or UINT_MAX if paging is disabled. */ |
1512 | static unsigned int lines_per_page; | |
cbfbd72a | 1513 | /* Number of chars per line or UINT_MAX if line folding is disabled. */ |
c906108c SS |
1514 | static unsigned int chars_per_line; |
1515 | /* Current count of lines printed on this page, chars on this line. */ | |
1516 | static unsigned int lines_printed, chars_printed; | |
1517 | ||
1518 | /* Buffer and start column of buffered text, for doing smarter word- | |
1519 | wrapping. When someone calls wrap_here(), we start buffering output | |
1520 | that comes through fputs_filtered(). If we see a newline, we just | |
1521 | spit it out and forget about the wrap_here(). If we see another | |
1522 | wrap_here(), we spit it out and remember the newer one. If we see | |
1523 | the end of the line, we spit out a newline, the indent, and then | |
1524 | the buffered output. */ | |
1525 | ||
1526 | /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which | |
1527 | are waiting to be output (they have already been counted in chars_printed). | |
1528 | When wrap_buffer[0] is null, the buffer is empty. */ | |
1529 | static char *wrap_buffer; | |
1530 | ||
1531 | /* Pointer in wrap_buffer to the next character to fill. */ | |
1532 | static char *wrap_pointer; | |
1533 | ||
1534 | /* String to indent by if the wrap occurs. Must not be NULL if wrap_column | |
1535 | is non-zero. */ | |
1536 | static char *wrap_indent; | |
1537 | ||
1538 | /* Column number on the screen where wrap_buffer begins, or 0 if wrapping | |
1539 | is not in effect. */ | |
1540 | static int wrap_column; | |
c906108c | 1541 | \f |
c5aa993b | 1542 | |
c906108c SS |
1543 | /* Inialize the lines and chars per page */ |
1544 | void | |
fba45db2 | 1545 | init_page_info (void) |
c906108c SS |
1546 | { |
1547 | #if defined(TUI) | |
c5aa993b | 1548 | if (tui_version && m_winPtrNotNull (cmdWin)) |
c906108c SS |
1549 | { |
1550 | lines_per_page = cmdWin->generic.height; | |
1551 | chars_per_line = cmdWin->generic.width; | |
1552 | } | |
1553 | else | |
1554 | #endif | |
1555 | { | |
1556 | /* These defaults will be used if we are unable to get the correct | |
1557 | values from termcap. */ | |
1558 | #if defined(__GO32__) | |
c5aa993b JM |
1559 | lines_per_page = ScreenRows (); |
1560 | chars_per_line = ScreenCols (); | |
1561 | #else | |
c906108c SS |
1562 | lines_per_page = 24; |
1563 | chars_per_line = 80; | |
1564 | ||
1565 | #if !defined (MPW) && !defined (_WIN32) | |
1566 | /* No termcap under MPW, although might be cool to do something | |
1567 | by looking at worksheet or console window sizes. */ | |
1568 | /* Initialize the screen height and width from termcap. */ | |
1569 | { | |
c5aa993b | 1570 | char *termtype = getenv ("TERM"); |
c906108c | 1571 | |
c5aa993b JM |
1572 | /* Positive means success, nonpositive means failure. */ |
1573 | int status; | |
c906108c | 1574 | |
c5aa993b JM |
1575 | /* 2048 is large enough for all known terminals, according to the |
1576 | GNU termcap manual. */ | |
1577 | char term_buffer[2048]; | |
c906108c | 1578 | |
c5aa993b JM |
1579 | if (termtype) |
1580 | { | |
c906108c SS |
1581 | status = tgetent (term_buffer, termtype); |
1582 | if (status > 0) | |
1583 | { | |
c5aa993b | 1584 | int val; |
c906108c | 1585 | int running_in_emacs = getenv ("EMACS") != NULL; |
c5aa993b JM |
1586 | |
1587 | val = tgetnum ("li"); | |
1588 | if (val >= 0 && !running_in_emacs) | |
1589 | lines_per_page = val; | |
1590 | else | |
1591 | /* The number of lines per page is not mentioned | |
c906108c SS |
1592 | in the terminal description. This probably means |
1593 | that paging is not useful (e.g. emacs shell window), | |
1594 | so disable paging. */ | |
c5aa993b JM |
1595 | lines_per_page = UINT_MAX; |
1596 | ||
1597 | val = tgetnum ("co"); | |
1598 | if (val >= 0) | |
1599 | chars_per_line = val; | |
c906108c | 1600 | } |
c5aa993b | 1601 | } |
c906108c SS |
1602 | } |
1603 | #endif /* MPW */ | |
1604 | ||
1605 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) | |
1606 | ||
1607 | /* If there is a better way to determine the window size, use it. */ | |
1608 | SIGWINCH_HANDLER (SIGWINCH); | |
1609 | #endif | |
1610 | #endif | |
1611 | /* If the output is not a terminal, don't paginate it. */ | |
d9fcf2fb | 1612 | if (!ui_file_isatty (gdb_stdout)) |
c5aa993b JM |
1613 | lines_per_page = UINT_MAX; |
1614 | } /* the command_line_version */ | |
1615 | set_width (); | |
c906108c SS |
1616 | } |
1617 | ||
1618 | static void | |
fba45db2 | 1619 | set_width (void) |
c906108c SS |
1620 | { |
1621 | if (chars_per_line == 0) | |
c5aa993b | 1622 | init_page_info (); |
c906108c SS |
1623 | |
1624 | if (!wrap_buffer) | |
1625 | { | |
1626 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); | |
1627 | wrap_buffer[0] = '\0'; | |
1628 | } | |
1629 | else | |
1630 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); | |
c5aa993b | 1631 | wrap_pointer = wrap_buffer; /* Start it at the beginning */ |
c906108c SS |
1632 | } |
1633 | ||
1634 | /* ARGSUSED */ | |
c5aa993b | 1635 | static void |
fba45db2 | 1636 | set_width_command (char *args, int from_tty, struct cmd_list_element *c) |
c906108c SS |
1637 | { |
1638 | set_width (); | |
1639 | } | |
1640 | ||
1641 | /* Wait, so the user can read what's on the screen. Prompt the user | |
1642 | to continue by pressing RETURN. */ | |
1643 | ||
1644 | static void | |
fba45db2 | 1645 | prompt_for_continue (void) |
c906108c SS |
1646 | { |
1647 | char *ignore; | |
1648 | char cont_prompt[120]; | |
1649 | ||
1650 | if (annotation_level > 1) | |
1651 | printf_unfiltered ("\n\032\032pre-prompt-for-continue\n"); | |
1652 | ||
1653 | strcpy (cont_prompt, | |
1654 | "---Type <return> to continue, or q <return> to quit---"); | |
1655 | if (annotation_level > 1) | |
1656 | strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); | |
1657 | ||
1658 | /* We must do this *before* we call gdb_readline, else it will eventually | |
1659 | call us -- thinking that we're trying to print beyond the end of the | |
1660 | screen. */ | |
1661 | reinitialize_more_filter (); | |
1662 | ||
1663 | immediate_quit++; | |
1664 | /* On a real operating system, the user can quit with SIGINT. | |
1665 | But not on GO32. | |
1666 | ||
1667 | 'q' is provided on all systems so users don't have to change habits | |
1668 | from system to system, and because telling them what to do in | |
1669 | the prompt is more user-friendly than expecting them to think of | |
1670 | SIGINT. */ | |
1671 | /* Call readline, not gdb_readline, because GO32 readline handles control-C | |
1672 | whereas control-C to gdb_readline will cause the user to get dumped | |
1673 | out to DOS. */ | |
1674 | ignore = readline (cont_prompt); | |
1675 | ||
1676 | if (annotation_level > 1) | |
1677 | printf_unfiltered ("\n\032\032post-prompt-for-continue\n"); | |
1678 | ||
1679 | if (ignore) | |
1680 | { | |
1681 | char *p = ignore; | |
1682 | while (*p == ' ' || *p == '\t') | |
1683 | ++p; | |
1684 | if (p[0] == 'q') | |
0f71a2f6 | 1685 | { |
6426a772 | 1686 | if (!event_loop_p) |
0f71a2f6 JM |
1687 | request_quit (SIGINT); |
1688 | else | |
c5aa993b | 1689 | async_request_quit (0); |
0f71a2f6 | 1690 | } |
b8c9b27d | 1691 | xfree (ignore); |
c906108c SS |
1692 | } |
1693 | immediate_quit--; | |
1694 | ||
1695 | /* Now we have to do this again, so that GDB will know that it doesn't | |
1696 | need to save the ---Type <return>--- line at the top of the screen. */ | |
1697 | reinitialize_more_filter (); | |
1698 | ||
1699 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ | |
1700 | } | |
1701 | ||
1702 | /* Reinitialize filter; ie. tell it to reset to original values. */ | |
1703 | ||
1704 | void | |
fba45db2 | 1705 | reinitialize_more_filter (void) |
c906108c SS |
1706 | { |
1707 | lines_printed = 0; | |
1708 | chars_printed = 0; | |
1709 | } | |
1710 | ||
1711 | /* Indicate that if the next sequence of characters overflows the line, | |
1712 | a newline should be inserted here rather than when it hits the end. | |
1713 | If INDENT is non-null, it is a string to be printed to indent the | |
1714 | wrapped part on the next line. INDENT must remain accessible until | |
1715 | the next call to wrap_here() or until a newline is printed through | |
1716 | fputs_filtered(). | |
1717 | ||
1718 | If the line is already overfull, we immediately print a newline and | |
1719 | the indentation, and disable further wrapping. | |
1720 | ||
1721 | If we don't know the width of lines, but we know the page height, | |
1722 | we must not wrap words, but should still keep track of newlines | |
1723 | that were explicitly printed. | |
1724 | ||
1725 | INDENT should not contain tabs, as that will mess up the char count | |
1726 | on the next line. FIXME. | |
1727 | ||
1728 | This routine is guaranteed to force out any output which has been | |
1729 | squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be | |
1730 | used to force out output from the wrap_buffer. */ | |
1731 | ||
1732 | void | |
fba45db2 | 1733 | wrap_here (char *indent) |
c906108c SS |
1734 | { |
1735 | /* This should have been allocated, but be paranoid anyway. */ | |
1736 | if (!wrap_buffer) | |
e1e9e218 | 1737 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
c906108c SS |
1738 | |
1739 | if (wrap_buffer[0]) | |
1740 | { | |
1741 | *wrap_pointer = '\0'; | |
1742 | fputs_unfiltered (wrap_buffer, gdb_stdout); | |
1743 | } | |
1744 | wrap_pointer = wrap_buffer; | |
1745 | wrap_buffer[0] = '\0'; | |
c5aa993b | 1746 | if (chars_per_line == UINT_MAX) /* No line overflow checking */ |
c906108c SS |
1747 | { |
1748 | wrap_column = 0; | |
1749 | } | |
1750 | else if (chars_printed >= chars_per_line) | |
1751 | { | |
1752 | puts_filtered ("\n"); | |
1753 | if (indent != NULL) | |
1754 | puts_filtered (indent); | |
1755 | wrap_column = 0; | |
1756 | } | |
1757 | else | |
1758 | { | |
1759 | wrap_column = chars_printed; | |
1760 | if (indent == NULL) | |
1761 | wrap_indent = ""; | |
1762 | else | |
1763 | wrap_indent = indent; | |
1764 | } | |
1765 | } | |
1766 | ||
1767 | /* Ensure that whatever gets printed next, using the filtered output | |
1768 | commands, starts at the beginning of the line. I.E. if there is | |
1769 | any pending output for the current line, flush it and start a new | |
1770 | line. Otherwise do nothing. */ | |
1771 | ||
1772 | void | |
fba45db2 | 1773 | begin_line (void) |
c906108c SS |
1774 | { |
1775 | if (chars_printed > 0) | |
1776 | { | |
1777 | puts_filtered ("\n"); | |
1778 | } | |
1779 | } | |
1780 | ||
ac9a91a7 | 1781 | |
c906108c SS |
1782 | /* Like fputs but if FILTER is true, pause after every screenful. |
1783 | ||
1784 | Regardless of FILTER can wrap at points other than the final | |
1785 | character of a line. | |
1786 | ||
1787 | Unlike fputs, fputs_maybe_filtered does not return a value. | |
1788 | It is OK for LINEBUFFER to be NULL, in which case just don't print | |
1789 | anything. | |
1790 | ||
1791 | Note that a longjmp to top level may occur in this routine (only if | |
1792 | FILTER is true) (since prompt_for_continue may do so) so this | |
1793 | routine should not be called when cleanups are not in place. */ | |
1794 | ||
1795 | static void | |
fba45db2 KB |
1796 | fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, |
1797 | int filter) | |
c906108c SS |
1798 | { |
1799 | const char *lineptr; | |
1800 | ||
1801 | if (linebuffer == 0) | |
1802 | return; | |
1803 | ||
1804 | /* Don't do any filtering if it is disabled. */ | |
7a292a7a | 1805 | if ((stream != gdb_stdout) || !pagination_enabled |
c5aa993b | 1806 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) |
c906108c SS |
1807 | { |
1808 | fputs_unfiltered (linebuffer, stream); | |
1809 | return; | |
1810 | } | |
1811 | ||
1812 | /* Go through and output each character. Show line extension | |
1813 | when this is necessary; prompt user for new page when this is | |
1814 | necessary. */ | |
c5aa993b | 1815 | |
c906108c SS |
1816 | lineptr = linebuffer; |
1817 | while (*lineptr) | |
1818 | { | |
1819 | /* Possible new page. */ | |
1820 | if (filter && | |
1821 | (lines_printed >= lines_per_page - 1)) | |
1822 | prompt_for_continue (); | |
1823 | ||
1824 | while (*lineptr && *lineptr != '\n') | |
1825 | { | |
1826 | /* Print a single line. */ | |
1827 | if (*lineptr == '\t') | |
1828 | { | |
1829 | if (wrap_column) | |
1830 | *wrap_pointer++ = '\t'; | |
1831 | else | |
1832 | fputc_unfiltered ('\t', stream); | |
1833 | /* Shifting right by 3 produces the number of tab stops | |
1834 | we have already passed, and then adding one and | |
c5aa993b | 1835 | shifting left 3 advances to the next tab stop. */ |
c906108c SS |
1836 | chars_printed = ((chars_printed >> 3) + 1) << 3; |
1837 | lineptr++; | |
1838 | } | |
1839 | else | |
1840 | { | |
1841 | if (wrap_column) | |
1842 | *wrap_pointer++ = *lineptr; | |
1843 | else | |
c5aa993b | 1844 | fputc_unfiltered (*lineptr, stream); |
c906108c SS |
1845 | chars_printed++; |
1846 | lineptr++; | |
1847 | } | |
c5aa993b | 1848 | |
c906108c SS |
1849 | if (chars_printed >= chars_per_line) |
1850 | { | |
1851 | unsigned int save_chars = chars_printed; | |
1852 | ||
1853 | chars_printed = 0; | |
1854 | lines_printed++; | |
1855 | /* If we aren't actually wrapping, don't output newline -- | |
c5aa993b JM |
1856 | if chars_per_line is right, we probably just overflowed |
1857 | anyway; if it's wrong, let us keep going. */ | |
c906108c SS |
1858 | if (wrap_column) |
1859 | fputc_unfiltered ('\n', stream); | |
1860 | ||
1861 | /* Possible new page. */ | |
1862 | if (lines_printed >= lines_per_page - 1) | |
1863 | prompt_for_continue (); | |
1864 | ||
1865 | /* Now output indentation and wrapped string */ | |
1866 | if (wrap_column) | |
1867 | { | |
1868 | fputs_unfiltered (wrap_indent, stream); | |
c5aa993b JM |
1869 | *wrap_pointer = '\0'; /* Null-terminate saved stuff */ |
1870 | fputs_unfiltered (wrap_buffer, stream); /* and eject it */ | |
c906108c SS |
1871 | /* FIXME, this strlen is what prevents wrap_indent from |
1872 | containing tabs. However, if we recurse to print it | |
1873 | and count its chars, we risk trouble if wrap_indent is | |
1874 | longer than (the user settable) chars_per_line. | |
1875 | Note also that this can set chars_printed > chars_per_line | |
1876 | if we are printing a long string. */ | |
1877 | chars_printed = strlen (wrap_indent) | |
c5aa993b | 1878 | + (save_chars - wrap_column); |
c906108c SS |
1879 | wrap_pointer = wrap_buffer; /* Reset buffer */ |
1880 | wrap_buffer[0] = '\0'; | |
c5aa993b JM |
1881 | wrap_column = 0; /* And disable fancy wrap */ |
1882 | } | |
c906108c SS |
1883 | } |
1884 | } | |
1885 | ||
1886 | if (*lineptr == '\n') | |
1887 | { | |
1888 | chars_printed = 0; | |
c5aa993b | 1889 | wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */ |
c906108c SS |
1890 | lines_printed++; |
1891 | fputc_unfiltered ('\n', stream); | |
1892 | lineptr++; | |
1893 | } | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | void | |
fba45db2 | 1898 | fputs_filtered (const char *linebuffer, struct ui_file *stream) |
c906108c SS |
1899 | { |
1900 | fputs_maybe_filtered (linebuffer, stream, 1); | |
1901 | } | |
1902 | ||
1903 | int | |
fba45db2 | 1904 | putchar_unfiltered (int c) |
c906108c | 1905 | { |
11cf8741 | 1906 | char buf = c; |
d9fcf2fb | 1907 | ui_file_write (gdb_stdout, &buf, 1); |
c906108c SS |
1908 | return c; |
1909 | } | |
1910 | ||
d1f4cff8 AC |
1911 | /* Write character C to gdb_stdout using GDB's paging mechanism and return C. |
1912 | May return nonlocally. */ | |
1913 | ||
1914 | int | |
1915 | putchar_filtered (int c) | |
1916 | { | |
1917 | return fputc_filtered (c, gdb_stdout); | |
1918 | } | |
1919 | ||
c906108c | 1920 | int |
fba45db2 | 1921 | fputc_unfiltered (int c, struct ui_file *stream) |
c906108c | 1922 | { |
11cf8741 | 1923 | char buf = c; |
d9fcf2fb | 1924 | ui_file_write (stream, &buf, 1); |
c906108c SS |
1925 | return c; |
1926 | } | |
1927 | ||
1928 | int | |
fba45db2 | 1929 | fputc_filtered (int c, struct ui_file *stream) |
c906108c SS |
1930 | { |
1931 | char buf[2]; | |
1932 | ||
1933 | buf[0] = c; | |
1934 | buf[1] = 0; | |
1935 | fputs_filtered (buf, stream); | |
1936 | return c; | |
1937 | } | |
1938 | ||
1939 | /* puts_debug is like fputs_unfiltered, except it prints special | |
1940 | characters in printable fashion. */ | |
1941 | ||
1942 | void | |
fba45db2 | 1943 | puts_debug (char *prefix, char *string, char *suffix) |
c906108c SS |
1944 | { |
1945 | int ch; | |
1946 | ||
1947 | /* Print prefix and suffix after each line. */ | |
1948 | static int new_line = 1; | |
1949 | static int return_p = 0; | |
1950 | static char *prev_prefix = ""; | |
1951 | static char *prev_suffix = ""; | |
1952 | ||
1953 | if (*string == '\n') | |
1954 | return_p = 0; | |
1955 | ||
1956 | /* If the prefix is changing, print the previous suffix, a new line, | |
1957 | and the new prefix. */ | |
c5aa993b | 1958 | if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line) |
c906108c | 1959 | { |
9846de1b JM |
1960 | fputs_unfiltered (prev_suffix, gdb_stdlog); |
1961 | fputs_unfiltered ("\n", gdb_stdlog); | |
1962 | fputs_unfiltered (prefix, gdb_stdlog); | |
c906108c SS |
1963 | } |
1964 | ||
1965 | /* Print prefix if we printed a newline during the previous call. */ | |
1966 | if (new_line) | |
1967 | { | |
1968 | new_line = 0; | |
9846de1b | 1969 | fputs_unfiltered (prefix, gdb_stdlog); |
c906108c SS |
1970 | } |
1971 | ||
1972 | prev_prefix = prefix; | |
1973 | prev_suffix = suffix; | |
1974 | ||
1975 | /* Output characters in a printable format. */ | |
1976 | while ((ch = *string++) != '\0') | |
1977 | { | |
1978 | switch (ch) | |
c5aa993b | 1979 | { |
c906108c SS |
1980 | default: |
1981 | if (isprint (ch)) | |
9846de1b | 1982 | fputc_unfiltered (ch, gdb_stdlog); |
c906108c SS |
1983 | |
1984 | else | |
9846de1b | 1985 | fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff); |
c906108c SS |
1986 | break; |
1987 | ||
c5aa993b JM |
1988 | case '\\': |
1989 | fputs_unfiltered ("\\\\", gdb_stdlog); | |
1990 | break; | |
1991 | case '\b': | |
1992 | fputs_unfiltered ("\\b", gdb_stdlog); | |
1993 | break; | |
1994 | case '\f': | |
1995 | fputs_unfiltered ("\\f", gdb_stdlog); | |
1996 | break; | |
1997 | case '\n': | |
1998 | new_line = 1; | |
1999 | fputs_unfiltered ("\\n", gdb_stdlog); | |
2000 | break; | |
2001 | case '\r': | |
2002 | fputs_unfiltered ("\\r", gdb_stdlog); | |
2003 | break; | |
2004 | case '\t': | |
2005 | fputs_unfiltered ("\\t", gdb_stdlog); | |
2006 | break; | |
2007 | case '\v': | |
2008 | fputs_unfiltered ("\\v", gdb_stdlog); | |
2009 | break; | |
2010 | } | |
c906108c SS |
2011 | |
2012 | return_p = ch == '\r'; | |
2013 | } | |
2014 | ||
2015 | /* Print suffix if we printed a newline. */ | |
2016 | if (new_line) | |
2017 | { | |
9846de1b JM |
2018 | fputs_unfiltered (suffix, gdb_stdlog); |
2019 | fputs_unfiltered ("\n", gdb_stdlog); | |
c906108c SS |
2020 | } |
2021 | } | |
2022 | ||
2023 | ||
2024 | /* Print a variable number of ARGS using format FORMAT. If this | |
2025 | information is going to put the amount written (since the last call | |
2026 | to REINITIALIZE_MORE_FILTER or the last page break) over the page size, | |
2027 | call prompt_for_continue to get the users permision to continue. | |
2028 | ||
2029 | Unlike fprintf, this function does not return a value. | |
2030 | ||
2031 | We implement three variants, vfprintf (takes a vararg list and stream), | |
2032 | fprintf (takes a stream to write on), and printf (the usual). | |
2033 | ||
2034 | Note also that a longjmp to top level may occur in this routine | |
2035 | (since prompt_for_continue may do so) so this routine should not be | |
2036 | called when cleanups are not in place. */ | |
2037 | ||
2038 | static void | |
fba45db2 KB |
2039 | vfprintf_maybe_filtered (struct ui_file *stream, const char *format, |
2040 | va_list args, int filter) | |
c906108c SS |
2041 | { |
2042 | char *linebuffer; | |
2043 | struct cleanup *old_cleanups; | |
2044 | ||
76995688 | 2045 | xvasprintf (&linebuffer, format, args); |
b8c9b27d | 2046 | old_cleanups = make_cleanup (xfree, linebuffer); |
c906108c SS |
2047 | fputs_maybe_filtered (linebuffer, stream, filter); |
2048 | do_cleanups (old_cleanups); | |
2049 | } | |
2050 | ||
2051 | ||
2052 | void | |
fba45db2 | 2053 | vfprintf_filtered (struct ui_file *stream, const char *format, va_list args) |
c906108c SS |
2054 | { |
2055 | vfprintf_maybe_filtered (stream, format, args, 1); | |
2056 | } | |
2057 | ||
2058 | void | |
fba45db2 | 2059 | vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) |
c906108c SS |
2060 | { |
2061 | char *linebuffer; | |
2062 | struct cleanup *old_cleanups; | |
2063 | ||
76995688 | 2064 | xvasprintf (&linebuffer, format, args); |
b8c9b27d | 2065 | old_cleanups = make_cleanup (xfree, linebuffer); |
c906108c SS |
2066 | fputs_unfiltered (linebuffer, stream); |
2067 | do_cleanups (old_cleanups); | |
2068 | } | |
2069 | ||
2070 | void | |
fba45db2 | 2071 | vprintf_filtered (const char *format, va_list args) |
c906108c SS |
2072 | { |
2073 | vfprintf_maybe_filtered (gdb_stdout, format, args, 1); | |
2074 | } | |
2075 | ||
2076 | void | |
fba45db2 | 2077 | vprintf_unfiltered (const char *format, va_list args) |
c906108c SS |
2078 | { |
2079 | vfprintf_unfiltered (gdb_stdout, format, args); | |
2080 | } | |
2081 | ||
c906108c | 2082 | void |
d9fcf2fb | 2083 | fprintf_filtered (struct ui_file * stream, const char *format,...) |
c906108c SS |
2084 | { |
2085 | va_list args; | |
c906108c | 2086 | va_start (args, format); |
c906108c SS |
2087 | vfprintf_filtered (stream, format, args); |
2088 | va_end (args); | |
2089 | } | |
2090 | ||
c906108c | 2091 | void |
d9fcf2fb | 2092 | fprintf_unfiltered (struct ui_file * stream, const char *format,...) |
c906108c SS |
2093 | { |
2094 | va_list args; | |
c906108c | 2095 | va_start (args, format); |
c906108c SS |
2096 | vfprintf_unfiltered (stream, format, args); |
2097 | va_end (args); | |
2098 | } | |
2099 | ||
2100 | /* Like fprintf_filtered, but prints its result indented. | |
2101 | Called as fprintfi_filtered (spaces, stream, format, ...); */ | |
2102 | ||
c906108c | 2103 | void |
d9fcf2fb | 2104 | fprintfi_filtered (int spaces, struct ui_file * stream, const char *format,...) |
c906108c SS |
2105 | { |
2106 | va_list args; | |
c906108c | 2107 | va_start (args, format); |
c906108c SS |
2108 | print_spaces_filtered (spaces, stream); |
2109 | ||
2110 | vfprintf_filtered (stream, format, args); | |
2111 | va_end (args); | |
2112 | } | |
2113 | ||
2114 | ||
c906108c | 2115 | void |
c5aa993b | 2116 | printf_filtered (const char *format,...) |
c906108c SS |
2117 | { |
2118 | va_list args; | |
c906108c | 2119 | va_start (args, format); |
c906108c SS |
2120 | vfprintf_filtered (gdb_stdout, format, args); |
2121 | va_end (args); | |
2122 | } | |
2123 | ||
2124 | ||
c906108c | 2125 | void |
c5aa993b | 2126 | printf_unfiltered (const char *format,...) |
c906108c SS |
2127 | { |
2128 | va_list args; | |
c906108c | 2129 | va_start (args, format); |
c906108c SS |
2130 | vfprintf_unfiltered (gdb_stdout, format, args); |
2131 | va_end (args); | |
2132 | } | |
2133 | ||
2134 | /* Like printf_filtered, but prints it's result indented. | |
2135 | Called as printfi_filtered (spaces, format, ...); */ | |
2136 | ||
c906108c | 2137 | void |
c5aa993b | 2138 | printfi_filtered (int spaces, const char *format,...) |
c906108c SS |
2139 | { |
2140 | va_list args; | |
c906108c | 2141 | va_start (args, format); |
c906108c SS |
2142 | print_spaces_filtered (spaces, gdb_stdout); |
2143 | vfprintf_filtered (gdb_stdout, format, args); | |
2144 | va_end (args); | |
2145 | } | |
2146 | ||
2147 | /* Easy -- but watch out! | |
2148 | ||
2149 | This routine is *not* a replacement for puts()! puts() appends a newline. | |
2150 | This one doesn't, and had better not! */ | |
2151 | ||
2152 | void | |
fba45db2 | 2153 | puts_filtered (const char *string) |
c906108c SS |
2154 | { |
2155 | fputs_filtered (string, gdb_stdout); | |
2156 | } | |
2157 | ||
2158 | void | |
fba45db2 | 2159 | puts_unfiltered (const char *string) |
c906108c SS |
2160 | { |
2161 | fputs_unfiltered (string, gdb_stdout); | |
2162 | } | |
2163 | ||
2164 | /* Return a pointer to N spaces and a null. The pointer is good | |
2165 | until the next call to here. */ | |
2166 | char * | |
fba45db2 | 2167 | n_spaces (int n) |
c906108c | 2168 | { |
392a587b JM |
2169 | char *t; |
2170 | static char *spaces = 0; | |
2171 | static int max_spaces = -1; | |
c906108c SS |
2172 | |
2173 | if (n > max_spaces) | |
2174 | { | |
2175 | if (spaces) | |
b8c9b27d | 2176 | xfree (spaces); |
c5aa993b JM |
2177 | spaces = (char *) xmalloc (n + 1); |
2178 | for (t = spaces + n; t != spaces;) | |
c906108c SS |
2179 | *--t = ' '; |
2180 | spaces[n] = '\0'; | |
2181 | max_spaces = n; | |
2182 | } | |
2183 | ||
2184 | return spaces + max_spaces - n; | |
2185 | } | |
2186 | ||
2187 | /* Print N spaces. */ | |
2188 | void | |
fba45db2 | 2189 | print_spaces_filtered (int n, struct ui_file *stream) |
c906108c SS |
2190 | { |
2191 | fputs_filtered (n_spaces (n), stream); | |
2192 | } | |
2193 | \f | |
2194 | /* C++ demangler stuff. */ | |
2195 | ||
2196 | /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language | |
2197 | LANG, using demangling args ARG_MODE, and print it filtered to STREAM. | |
2198 | If the name is not mangled, or the language for the name is unknown, or | |
2199 | demangling is off, the name is printed in its "raw" form. */ | |
2200 | ||
2201 | void | |
fba45db2 KB |
2202 | fprintf_symbol_filtered (struct ui_file *stream, char *name, enum language lang, |
2203 | int arg_mode) | |
c906108c SS |
2204 | { |
2205 | char *demangled; | |
2206 | ||
2207 | if (name != NULL) | |
2208 | { | |
2209 | /* If user wants to see raw output, no problem. */ | |
2210 | if (!demangle) | |
2211 | { | |
2212 | fputs_filtered (name, stream); | |
2213 | } | |
2214 | else | |
2215 | { | |
2216 | switch (lang) | |
2217 | { | |
2218 | case language_cplus: | |
2219 | demangled = cplus_demangle (name, arg_mode); | |
2220 | break; | |
2221 | case language_java: | |
2222 | demangled = cplus_demangle (name, arg_mode | DMGL_JAVA); | |
2223 | break; | |
2224 | case language_chill: | |
2225 | demangled = chill_demangle (name); | |
2226 | break; | |
2227 | default: | |
2228 | demangled = NULL; | |
2229 | break; | |
2230 | } | |
2231 | fputs_filtered (demangled ? demangled : name, stream); | |
2232 | if (demangled != NULL) | |
2233 | { | |
b8c9b27d | 2234 | xfree (demangled); |
c906108c SS |
2235 | } |
2236 | } | |
2237 | } | |
2238 | } | |
2239 | ||
2240 | /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any | |
2241 | differences in whitespace. Returns 0 if they match, non-zero if they | |
2242 | don't (slightly different than strcmp()'s range of return values). | |
c5aa993b | 2243 | |
c906108c SS |
2244 | As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". |
2245 | This "feature" is useful when searching for matching C++ function names | |
2246 | (such as if the user types 'break FOO', where FOO is a mangled C++ | |
2247 | function). */ | |
2248 | ||
2249 | int | |
fba45db2 | 2250 | strcmp_iw (const char *string1, const char *string2) |
c906108c SS |
2251 | { |
2252 | while ((*string1 != '\0') && (*string2 != '\0')) | |
2253 | { | |
2254 | while (isspace (*string1)) | |
2255 | { | |
2256 | string1++; | |
2257 | } | |
2258 | while (isspace (*string2)) | |
2259 | { | |
2260 | string2++; | |
2261 | } | |
2262 | if (*string1 != *string2) | |
2263 | { | |
2264 | break; | |
2265 | } | |
2266 | if (*string1 != '\0') | |
2267 | { | |
2268 | string1++; | |
2269 | string2++; | |
2270 | } | |
2271 | } | |
2272 | return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); | |
2273 | } | |
c906108c | 2274 | \f |
c5aa993b | 2275 | |
c906108c | 2276 | /* |
c5aa993b JM |
2277 | ** subset_compare() |
2278 | ** Answer whether string_to_compare is a full or partial match to | |
2279 | ** template_string. The partial match must be in sequence starting | |
2280 | ** at index 0. | |
2281 | */ | |
c906108c | 2282 | int |
fba45db2 | 2283 | subset_compare (char *string_to_compare, char *template_string) |
7a292a7a SS |
2284 | { |
2285 | int match; | |
c5aa993b JM |
2286 | if (template_string != (char *) NULL && string_to_compare != (char *) NULL && |
2287 | strlen (string_to_compare) <= strlen (template_string)) | |
2288 | match = (strncmp (template_string, | |
2289 | string_to_compare, | |
2290 | strlen (string_to_compare)) == 0); | |
7a292a7a SS |
2291 | else |
2292 | match = 0; | |
2293 | return match; | |
2294 | } | |
c906108c SS |
2295 | |
2296 | ||
a14ed312 | 2297 | static void pagination_on_command (char *arg, int from_tty); |
7a292a7a | 2298 | static void |
fba45db2 | 2299 | pagination_on_command (char *arg, int from_tty) |
c906108c SS |
2300 | { |
2301 | pagination_enabled = 1; | |
2302 | } | |
2303 | ||
a14ed312 | 2304 | static void pagination_on_command (char *arg, int from_tty); |
7a292a7a | 2305 | static void |
fba45db2 | 2306 | pagination_off_command (char *arg, int from_tty) |
c906108c SS |
2307 | { |
2308 | pagination_enabled = 0; | |
2309 | } | |
c906108c | 2310 | \f |
c5aa993b | 2311 | |
c906108c | 2312 | void |
fba45db2 | 2313 | initialize_utils (void) |
c906108c SS |
2314 | { |
2315 | struct cmd_list_element *c; | |
2316 | ||
c5aa993b JM |
2317 | c = add_set_cmd ("width", class_support, var_uinteger, |
2318 | (char *) &chars_per_line, | |
2319 | "Set number of characters gdb thinks are in a line.", | |
2320 | &setlist); | |
c906108c SS |
2321 | add_show_from_set (c, &showlist); |
2322 | c->function.sfunc = set_width_command; | |
2323 | ||
2324 | add_show_from_set | |
2325 | (add_set_cmd ("height", class_support, | |
c5aa993b | 2326 | var_uinteger, (char *) &lines_per_page, |
c906108c SS |
2327 | "Set number of lines gdb thinks are in a page.", &setlist), |
2328 | &showlist); | |
c5aa993b | 2329 | |
c906108c SS |
2330 | init_page_info (); |
2331 | ||
2332 | /* If the output is not a terminal, don't paginate it. */ | |
d9fcf2fb | 2333 | if (!ui_file_isatty (gdb_stdout)) |
c906108c SS |
2334 | lines_per_page = UINT_MAX; |
2335 | ||
c5aa993b | 2336 | set_width_command ((char *) NULL, 0, c); |
c906108c SS |
2337 | |
2338 | add_show_from_set | |
c5aa993b JM |
2339 | (add_set_cmd ("demangle", class_support, var_boolean, |
2340 | (char *) &demangle, | |
2341 | "Set demangling of encoded C++ names when displaying symbols.", | |
c906108c SS |
2342 | &setprintlist), |
2343 | &showprintlist); | |
2344 | ||
2345 | add_show_from_set | |
2346 | (add_set_cmd ("pagination", class_support, | |
c5aa993b | 2347 | var_boolean, (char *) &pagination_enabled, |
c906108c SS |
2348 | "Set state of pagination.", &setlist), |
2349 | &showlist); | |
4261bedc | 2350 | |
c906108c SS |
2351 | if (xdb_commands) |
2352 | { | |
c5aa993b JM |
2353 | add_com ("am", class_support, pagination_on_command, |
2354 | "Enable pagination"); | |
2355 | add_com ("sm", class_support, pagination_off_command, | |
2356 | "Disable pagination"); | |
c906108c SS |
2357 | } |
2358 | ||
2359 | add_show_from_set | |
c5aa993b JM |
2360 | (add_set_cmd ("sevenbit-strings", class_support, var_boolean, |
2361 | (char *) &sevenbit_strings, | |
2362 | "Set printing of 8-bit characters in strings as \\nnn.", | |
c906108c SS |
2363 | &setprintlist), |
2364 | &showprintlist); | |
2365 | ||
2366 | add_show_from_set | |
c5aa993b JM |
2367 | (add_set_cmd ("asm-demangle", class_support, var_boolean, |
2368 | (char *) &asm_demangle, | |
2369 | "Set demangling of C++ names in disassembly listings.", | |
c906108c SS |
2370 | &setprintlist), |
2371 | &showprintlist); | |
2372 | } | |
2373 | ||
2374 | /* Machine specific function to handle SIGWINCH signal. */ | |
2375 | ||
2376 | #ifdef SIGWINCH_HANDLER_BODY | |
c5aa993b | 2377 | SIGWINCH_HANDLER_BODY |
c906108c SS |
2378 | #endif |
2379 | \f | |
2380 | /* Support for converting target fp numbers into host DOUBLEST format. */ | |
2381 | ||
2382 | /* XXX - This code should really be in libiberty/floatformat.c, however | |
2383 | configuration issues with libiberty made this very difficult to do in the | |
2384 | available time. */ | |
2385 | ||
2386 | #include "floatformat.h" | |
2387 | #include <math.h> /* ldexp */ | |
2388 | ||
2389 | /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not | |
2390 | going to bother with trying to muck around with whether it is defined in | |
2391 | a system header, what we do if not, etc. */ | |
2392 | #define FLOATFORMAT_CHAR_BIT 8 | |
2393 | ||
a14ed312 KB |
2394 | static unsigned long get_field (unsigned char *, |
2395 | enum floatformat_byteorders, | |
2396 | unsigned int, unsigned int, unsigned int); | |
c906108c SS |
2397 | |
2398 | /* Extract a field which starts at START and is LEN bytes long. DATA and | |
2399 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
2400 | static unsigned long | |
fba45db2 KB |
2401 | get_field (unsigned char *data, enum floatformat_byteorders order, |
2402 | unsigned int total_len, unsigned int start, unsigned int len) | |
c906108c SS |
2403 | { |
2404 | unsigned long result; | |
2405 | unsigned int cur_byte; | |
2406 | int cur_bitshift; | |
2407 | ||
2408 | /* Start at the least significant part of the field. */ | |
c906108c | 2409 | if (order == floatformat_little || order == floatformat_littlebyte_bigword) |
0fda6bd2 JM |
2410 | { |
2411 | /* We start counting from the other end (i.e, from the high bytes | |
2412 | rather than the low bytes). As such, we need to be concerned | |
2413 | with what happens if bit 0 doesn't start on a byte boundary. | |
2414 | I.e, we need to properly handle the case where total_len is | |
2415 | not evenly divisible by 8. So we compute ``excess'' which | |
2416 | represents the number of bits from the end of our starting | |
2417 | byte needed to get to bit 0. */ | |
2418 | int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); | |
2419 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) | |
2420 | - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); | |
2421 | cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) | |
2422 | - FLOATFORMAT_CHAR_BIT; | |
2423 | } | |
2424 | else | |
2425 | { | |
2426 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
2427 | cur_bitshift = | |
2428 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
2429 | } | |
2430 | if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) | |
2431 | result = *(data + cur_byte) >> (-cur_bitshift); | |
2432 | else | |
2433 | result = 0; | |
c906108c SS |
2434 | cur_bitshift += FLOATFORMAT_CHAR_BIT; |
2435 | if (order == floatformat_little || order == floatformat_littlebyte_bigword) | |
2436 | ++cur_byte; | |
2437 | else | |
2438 | --cur_byte; | |
2439 | ||
2440 | /* Move towards the most significant part of the field. */ | |
2441 | while (cur_bitshift < len) | |
2442 | { | |
0fda6bd2 | 2443 | result |= (unsigned long)*(data + cur_byte) << cur_bitshift; |
c906108c SS |
2444 | cur_bitshift += FLOATFORMAT_CHAR_BIT; |
2445 | if (order == floatformat_little || order == floatformat_littlebyte_bigword) | |
2446 | ++cur_byte; | |
2447 | else | |
2448 | --cur_byte; | |
2449 | } | |
0fda6bd2 JM |
2450 | if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT) |
2451 | /* Mask out bits which are not part of the field */ | |
2452 | result &= ((1UL << len) - 1); | |
c906108c SS |
2453 | return result; |
2454 | } | |
c5aa993b | 2455 | |
c906108c SS |
2456 | /* Convert from FMT to a DOUBLEST. |
2457 | FROM is the address of the extended float. | |
2458 | Store the DOUBLEST in *TO. */ | |
2459 | ||
2460 | void | |
fba45db2 KB |
2461 | floatformat_to_doublest (const struct floatformat *fmt, char *from, |
2462 | DOUBLEST *to) | |
c906108c | 2463 | { |
c5aa993b | 2464 | unsigned char *ufrom = (unsigned char *) from; |
c906108c SS |
2465 | DOUBLEST dto; |
2466 | long exponent; | |
2467 | unsigned long mant; | |
2468 | unsigned int mant_bits, mant_off; | |
2469 | int mant_bits_left; | |
2470 | int special_exponent; /* It's a NaN, denorm or zero */ | |
2471 | ||
2472 | /* If the mantissa bits are not contiguous from one end of the | |
2473 | mantissa to the other, we need to make a private copy of the | |
2474 | source bytes that is in the right order since the unpacking | |
2475 | algorithm assumes that the bits are contiguous. | |
2476 | ||
2477 | Swap the bytes individually rather than accessing them through | |
2478 | "long *" since we have no guarantee that they start on a long | |
2479 | alignment, and also sizeof(long) for the host could be different | |
2480 | than sizeof(long) for the target. FIXME: Assumes sizeof(long) | |
2481 | for the target is 4. */ | |
2482 | ||
c5aa993b | 2483 | if (fmt->byteorder == floatformat_littlebyte_bigword) |
c906108c SS |
2484 | { |
2485 | static unsigned char *newfrom; | |
2486 | unsigned char *swapin, *swapout; | |
2487 | int longswaps; | |
2488 | ||
c5aa993b | 2489 | longswaps = fmt->totalsize / FLOATFORMAT_CHAR_BIT; |
c906108c | 2490 | longswaps >>= 3; |
c5aa993b | 2491 | |
c906108c SS |
2492 | if (newfrom == NULL) |
2493 | { | |
c5aa993b | 2494 | newfrom = (unsigned char *) xmalloc (fmt->totalsize); |
c906108c SS |
2495 | } |
2496 | swapout = newfrom; | |
2497 | swapin = ufrom; | |
2498 | ufrom = newfrom; | |
2499 | while (longswaps-- > 0) | |
2500 | { | |
2501 | /* This is ugly, but efficient */ | |
2502 | *swapout++ = swapin[4]; | |
2503 | *swapout++ = swapin[5]; | |
2504 | *swapout++ = swapin[6]; | |
2505 | *swapout++ = swapin[7]; | |
2506 | *swapout++ = swapin[0]; | |
2507 | *swapout++ = swapin[1]; | |
2508 | *swapout++ = swapin[2]; | |
2509 | *swapout++ = swapin[3]; | |
2510 | swapin += 8; | |
2511 | } | |
2512 | } | |
2513 | ||
2514 | exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
2515 | fmt->exp_start, fmt->exp_len); | |
2516 | /* Note that if exponent indicates a NaN, we can't really do anything useful | |
2517 | (not knowing if the host has NaN's, or how to build one). So it will | |
2518 | end up as an infinity or something close; that is OK. */ | |
2519 | ||
2520 | mant_bits_left = fmt->man_len; | |
2521 | mant_off = fmt->man_start; | |
2522 | dto = 0.0; | |
2523 | ||
2524 | special_exponent = exponent == 0 || exponent == fmt->exp_nan; | |
2525 | ||
11cf8741 JM |
2526 | /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity, |
2527 | we don't check for zero as the exponent doesn't matter. */ | |
c906108c SS |
2528 | if (!special_exponent) |
2529 | exponent -= fmt->exp_bias; | |
11cf8741 JM |
2530 | else if (exponent == 0) |
2531 | exponent = 1 - fmt->exp_bias; | |
c906108c SS |
2532 | |
2533 | /* Build the result algebraically. Might go infinite, underflow, etc; | |
2534 | who cares. */ | |
2535 | ||
2536 | /* If this format uses a hidden bit, explicitly add it in now. Otherwise, | |
2537 | increment the exponent by one to account for the integer bit. */ | |
2538 | ||
2539 | if (!special_exponent) | |
7a292a7a SS |
2540 | { |
2541 | if (fmt->intbit == floatformat_intbit_no) | |
2542 | dto = ldexp (1.0, exponent); | |
2543 | else | |
2544 | exponent++; | |
2545 | } | |
c906108c SS |
2546 | |
2547 | while (mant_bits_left > 0) | |
2548 | { | |
2549 | mant_bits = min (mant_bits_left, 32); | |
2550 | ||
2551 | mant = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
c5aa993b | 2552 | mant_off, mant_bits); |
c906108c | 2553 | |
c5aa993b | 2554 | dto += ldexp ((double) mant, exponent - mant_bits); |
c906108c SS |
2555 | exponent -= mant_bits; |
2556 | mant_off += mant_bits; | |
2557 | mant_bits_left -= mant_bits; | |
2558 | } | |
2559 | ||
2560 | /* Negate it if negative. */ | |
2561 | if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1)) | |
2562 | dto = -dto; | |
2563 | *to = dto; | |
2564 | } | |
2565 | \f | |
a14ed312 KB |
2566 | static void put_field (unsigned char *, enum floatformat_byteorders, |
2567 | unsigned int, | |
2568 | unsigned int, unsigned int, unsigned long); | |
c906108c SS |
2569 | |
2570 | /* Set a field which starts at START and is LEN bytes long. DATA and | |
2571 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
2572 | static void | |
fba45db2 KB |
2573 | put_field (unsigned char *data, enum floatformat_byteorders order, |
2574 | unsigned int total_len, unsigned int start, unsigned int len, | |
2575 | unsigned long stuff_to_put) | |
c906108c SS |
2576 | { |
2577 | unsigned int cur_byte; | |
2578 | int cur_bitshift; | |
2579 | ||
2580 | /* Start at the least significant part of the field. */ | |
c906108c | 2581 | if (order == floatformat_little || order == floatformat_littlebyte_bigword) |
0fda6bd2 JM |
2582 | { |
2583 | int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); | |
2584 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) | |
2585 | - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); | |
2586 | cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) | |
2587 | - FLOATFORMAT_CHAR_BIT; | |
2588 | } | |
2589 | else | |
2590 | { | |
2591 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
2592 | cur_bitshift = | |
2593 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
2594 | } | |
2595 | if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) | |
2596 | { | |
2597 | *(data + cur_byte) &= | |
2598 | ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) | |
2599 | << (-cur_bitshift)); | |
2600 | *(data + cur_byte) |= | |
2601 | (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); | |
2602 | } | |
c906108c SS |
2603 | cur_bitshift += FLOATFORMAT_CHAR_BIT; |
2604 | if (order == floatformat_little || order == floatformat_littlebyte_bigword) | |
2605 | ++cur_byte; | |
2606 | else | |
2607 | --cur_byte; | |
2608 | ||
2609 | /* Move towards the most significant part of the field. */ | |
2610 | while (cur_bitshift < len) | |
2611 | { | |
2612 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
2613 | { | |
2614 | /* This is the last byte. */ | |
2615 | *(data + cur_byte) &= | |
2616 | ~((1 << (len - cur_bitshift)) - 1); | |
2617 | *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); | |
2618 | } | |
2619 | else | |
2620 | *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) | |
2621 | & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); | |
2622 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
2623 | if (order == floatformat_little || order == floatformat_littlebyte_bigword) | |
2624 | ++cur_byte; | |
2625 | else | |
2626 | --cur_byte; | |
2627 | } | |
2628 | } | |
2629 | ||
2630 | #ifdef HAVE_LONG_DOUBLE | |
2631 | /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR. | |
2632 | The range of the returned value is >= 0.5 and < 1.0. This is equivalent to | |
2633 | frexp, but operates on the long double data type. */ | |
2634 | ||
a14ed312 | 2635 | static long double ldfrexp (long double value, int *eptr); |
c906108c SS |
2636 | |
2637 | static long double | |
fba45db2 | 2638 | ldfrexp (long double value, int *eptr) |
c906108c SS |
2639 | { |
2640 | long double tmp; | |
2641 | int exp; | |
2642 | ||
2643 | /* Unfortunately, there are no portable functions for extracting the exponent | |
2644 | of a long double, so we have to do it iteratively by multiplying or dividing | |
2645 | by two until the fraction is between 0.5 and 1.0. */ | |
2646 | ||
2647 | if (value < 0.0l) | |
2648 | value = -value; | |
2649 | ||
2650 | tmp = 1.0l; | |
2651 | exp = 0; | |
2652 | ||
2653 | if (value >= tmp) /* Value >= 1.0 */ | |
2654 | while (value >= tmp) | |
2655 | { | |
2656 | tmp *= 2.0l; | |
2657 | exp++; | |
2658 | } | |
2659 | else if (value != 0.0l) /* Value < 1.0 and > 0.0 */ | |
2660 | { | |
2661 | while (value < tmp) | |
2662 | { | |
2663 | tmp /= 2.0l; | |
2664 | exp--; | |
2665 | } | |
2666 | tmp *= 2.0l; | |
2667 | exp++; | |
2668 | } | |
2669 | ||
2670 | *eptr = exp; | |
c5aa993b | 2671 | return value / tmp; |
c906108c SS |
2672 | } |
2673 | #endif /* HAVE_LONG_DOUBLE */ | |
2674 | ||
2675 | ||
2676 | /* The converse: convert the DOUBLEST *FROM to an extended float | |
2677 | and store where TO points. Neither FROM nor TO have any alignment | |
2678 | restrictions. */ | |
2679 | ||
2680 | void | |
fba45db2 KB |
2681 | floatformat_from_doublest (CONST struct floatformat *fmt, DOUBLEST *from, |
2682 | char *to) | |
c906108c SS |
2683 | { |
2684 | DOUBLEST dfrom; | |
2685 | int exponent; | |
2686 | DOUBLEST mant; | |
2687 | unsigned int mant_bits, mant_off; | |
2688 | int mant_bits_left; | |
c5aa993b | 2689 | unsigned char *uto = (unsigned char *) to; |
c906108c SS |
2690 | |
2691 | memcpy (&dfrom, from, sizeof (dfrom)); | |
ba8966d6 KB |
2692 | memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) |
2693 | / FLOATFORMAT_CHAR_BIT); | |
c906108c SS |
2694 | if (dfrom == 0) |
2695 | return; /* Result is zero */ | |
2696 | if (dfrom != dfrom) /* Result is NaN */ | |
2697 | { | |
2698 | /* From is NaN */ | |
2699 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, | |
2700 | fmt->exp_len, fmt->exp_nan); | |
2701 | /* Be sure it's not infinity, but NaN value is irrel */ | |
2702 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start, | |
2703 | 32, 1); | |
2704 | return; | |
2705 | } | |
2706 | ||
2707 | /* If negative, set the sign bit. */ | |
2708 | if (dfrom < 0) | |
2709 | { | |
2710 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1); | |
2711 | dfrom = -dfrom; | |
2712 | } | |
2713 | ||
2714 | if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity */ | |
2715 | { | |
2716 | /* Infinity exponent is same as NaN's. */ | |
2717 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, | |
2718 | fmt->exp_len, fmt->exp_nan); | |
2719 | /* Infinity mantissa is all zeroes. */ | |
2720 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start, | |
2721 | fmt->man_len, 0); | |
2722 | return; | |
2723 | } | |
2724 | ||
2725 | #ifdef HAVE_LONG_DOUBLE | |
2726 | mant = ldfrexp (dfrom, &exponent); | |
2727 | #else | |
2728 | mant = frexp (dfrom, &exponent); | |
2729 | #endif | |
2730 | ||
2731 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len, | |
2732 | exponent + fmt->exp_bias - 1); | |
2733 | ||
2734 | mant_bits_left = fmt->man_len; | |
2735 | mant_off = fmt->man_start; | |
2736 | while (mant_bits_left > 0) | |
2737 | { | |
2738 | unsigned long mant_long; | |
2739 | mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; | |
2740 | ||
2741 | mant *= 4294967296.0; | |
ba8966d6 | 2742 | mant_long = ((unsigned long) mant) & 0xffffffffL; |
c906108c SS |
2743 | mant -= mant_long; |
2744 | ||
2745 | /* If the integer bit is implicit, then we need to discard it. | |
c5aa993b JM |
2746 | If we are discarding a zero, we should be (but are not) creating |
2747 | a denormalized number which means adjusting the exponent | |
2748 | (I think). */ | |
c906108c SS |
2749 | if (mant_bits_left == fmt->man_len |
2750 | && fmt->intbit == floatformat_intbit_no) | |
2751 | { | |
2752 | mant_long <<= 1; | |
ba8966d6 | 2753 | mant_long &= 0xffffffffL; |
c906108c SS |
2754 | mant_bits -= 1; |
2755 | } | |
2756 | ||
2757 | if (mant_bits < 32) | |
2758 | { | |
2759 | /* The bits we want are in the most significant MANT_BITS bits of | |
2760 | mant_long. Move them to the least significant. */ | |
2761 | mant_long >>= 32 - mant_bits; | |
2762 | } | |
2763 | ||
2764 | put_field (uto, fmt->byteorder, fmt->totalsize, | |
2765 | mant_off, mant_bits, mant_long); | |
2766 | mant_off += mant_bits; | |
2767 | mant_bits_left -= mant_bits; | |
2768 | } | |
c5aa993b | 2769 | if (fmt->byteorder == floatformat_littlebyte_bigword) |
c906108c SS |
2770 | { |
2771 | int count; | |
2772 | unsigned char *swaplow = uto; | |
2773 | unsigned char *swaphigh = uto + 4; | |
2774 | unsigned char tmp; | |
2775 | ||
2776 | for (count = 0; count < 4; count++) | |
2777 | { | |
2778 | tmp = *swaplow; | |
2779 | *swaplow++ = *swaphigh; | |
2780 | *swaphigh++ = tmp; | |
2781 | } | |
2782 | } | |
2783 | } | |
2784 | ||
39424bef MK |
2785 | /* Check if VAL (which is assumed to be a floating point number whose |
2786 | format is described by FMT) is negative. */ | |
2787 | ||
2788 | int | |
2789 | floatformat_is_negative (const struct floatformat *fmt, char *val) | |
2790 | { | |
2791 | unsigned char *uval = (unsigned char *) val; | |
2792 | ||
2793 | return get_field (uval, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1); | |
2794 | } | |
2795 | ||
2796 | /* Check if VAL is "not a number" (NaN) for FMT. */ | |
2797 | ||
2798 | int | |
2799 | floatformat_is_nan (const struct floatformat *fmt, char *val) | |
2800 | { | |
2801 | unsigned char *uval = (unsigned char *) val; | |
2802 | long exponent; | |
2803 | unsigned long mant; | |
2804 | unsigned int mant_bits, mant_off; | |
2805 | int mant_bits_left; | |
2806 | ||
2807 | if (! fmt->exp_nan) | |
2808 | return 0; | |
2809 | ||
2810 | exponent = get_field (uval, fmt->byteorder, fmt->totalsize, | |
2811 | fmt->exp_start, fmt->exp_len); | |
2812 | ||
2813 | if (exponent != fmt->exp_nan) | |
2814 | return 0; | |
2815 | ||
2816 | mant_bits_left = fmt->man_len; | |
2817 | mant_off = fmt->man_start; | |
2818 | ||
2819 | while (mant_bits_left > 0) | |
2820 | { | |
2821 | mant_bits = min (mant_bits_left, 32); | |
2822 | ||
2823 | mant = get_field (uval, fmt->byteorder, fmt->totalsize, | |
2824 | mant_off, mant_bits); | |
2825 | ||
2826 | /* If there is an explicit integer bit, mask it off. */ | |
2827 | if (mant_off == fmt->man_start | |
2828 | && fmt->intbit == floatformat_intbit_yes) | |
2829 | mant &= ~(1 << (mant_bits - 1)); | |
2830 | ||
2831 | if (mant) | |
2832 | return 1; | |
2833 | ||
2834 | mant_off += mant_bits; | |
2835 | mant_bits_left -= mant_bits; | |
2836 | } | |
2837 | ||
2838 | return 0; | |
2839 | } | |
2840 | ||
2841 | /* Convert the mantissa of VAL (which is assumed to be a floating | |
2842 | point number whose format is described by FMT) into a hexadecimal | |
2843 | and store it in a static string. Return a pointer to that string. */ | |
2844 | ||
2845 | char * | |
2846 | floatformat_mantissa (const struct floatformat *fmt, char *val) | |
2847 | { | |
2848 | unsigned char *uval = (unsigned char *) val; | |
2849 | unsigned long mant; | |
2850 | unsigned int mant_bits, mant_off; | |
2851 | int mant_bits_left; | |
2852 | static char res[50]; | |
2853 | char buf[9]; | |
2854 | ||
2855 | /* Make sure we have enough room to store the mantissa. */ | |
2856 | gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2); | |
2857 | ||
2858 | mant_off = fmt->man_start; | |
2859 | mant_bits_left = fmt->man_len; | |
2860 | mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32; | |
2861 | ||
2862 | mant = get_field (uval, fmt->byteorder, fmt->totalsize, | |
2863 | mant_off, mant_bits); | |
2864 | ||
2865 | sprintf (res, "%lx", mant); | |
2866 | ||
2867 | mant_off += mant_bits; | |
2868 | mant_bits_left -= mant_bits; | |
2869 | ||
2870 | while (mant_bits_left > 0) | |
2871 | { | |
2872 | mant = get_field (uval, fmt->byteorder, fmt->totalsize, | |
2873 | mant_off, 32); | |
2874 | ||
2875 | sprintf (buf, "%08lx", mant); | |
2876 | strcat (res, buf); | |
2877 | ||
2878 | mant_off += 32; | |
2879 | mant_bits_left -= 32; | |
2880 | } | |
2881 | ||
2882 | return res; | |
2883 | } | |
2884 | ||
5683e87a AC |
2885 | /* print routines to handle variable size regs, etc. */ |
2886 | ||
c906108c SS |
2887 | /* temporary storage using circular buffer */ |
2888 | #define NUMCELLS 16 | |
2889 | #define CELLSIZE 32 | |
c5aa993b | 2890 | static char * |
fba45db2 | 2891 | get_cell (void) |
c906108c SS |
2892 | { |
2893 | static char buf[NUMCELLS][CELLSIZE]; | |
c5aa993b JM |
2894 | static int cell = 0; |
2895 | if (++cell >= NUMCELLS) | |
2896 | cell = 0; | |
c906108c SS |
2897 | return buf[cell]; |
2898 | } | |
2899 | ||
d4f3574e SS |
2900 | int |
2901 | strlen_paddr (void) | |
2902 | { | |
79496e2f | 2903 | return (TARGET_ADDR_BIT / 8 * 2); |
d4f3574e SS |
2904 | } |
2905 | ||
c5aa993b | 2906 | char * |
104c1213 | 2907 | paddr (CORE_ADDR addr) |
c906108c | 2908 | { |
79496e2f | 2909 | return phex (addr, TARGET_ADDR_BIT / 8); |
c906108c SS |
2910 | } |
2911 | ||
c5aa993b | 2912 | char * |
104c1213 | 2913 | paddr_nz (CORE_ADDR addr) |
c906108c | 2914 | { |
79496e2f | 2915 | return phex_nz (addr, TARGET_ADDR_BIT / 8); |
c906108c SS |
2916 | } |
2917 | ||
104c1213 JM |
2918 | static void |
2919 | decimal2str (char *paddr_str, char *sign, ULONGEST addr) | |
2920 | { | |
2921 | /* steal code from valprint.c:print_decimal(). Should this worry | |
2922 | about the real size of addr as the above does? */ | |
2923 | unsigned long temp[3]; | |
2924 | int i = 0; | |
2925 | do | |
2926 | { | |
2927 | temp[i] = addr % (1000 * 1000 * 1000); | |
2928 | addr /= (1000 * 1000 * 1000); | |
2929 | i++; | |
2930 | } | |
2931 | while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); | |
2932 | switch (i) | |
2933 | { | |
2934 | case 1: | |
2935 | sprintf (paddr_str, "%s%lu", | |
2936 | sign, temp[0]); | |
2937 | break; | |
2938 | case 2: | |
2939 | sprintf (paddr_str, "%s%lu%09lu", | |
2940 | sign, temp[1], temp[0]); | |
2941 | break; | |
2942 | case 3: | |
2943 | sprintf (paddr_str, "%s%lu%09lu%09lu", | |
2944 | sign, temp[2], temp[1], temp[0]); | |
2945 | break; | |
2946 | default: | |
e1e9e218 | 2947 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
104c1213 JM |
2948 | } |
2949 | } | |
2950 | ||
2951 | char * | |
2952 | paddr_u (CORE_ADDR addr) | |
2953 | { | |
2954 | char *paddr_str = get_cell (); | |
2955 | decimal2str (paddr_str, "", addr); | |
2956 | return paddr_str; | |
2957 | } | |
2958 | ||
2959 | char * | |
2960 | paddr_d (LONGEST addr) | |
2961 | { | |
2962 | char *paddr_str = get_cell (); | |
2963 | if (addr < 0) | |
2964 | decimal2str (paddr_str, "-", -addr); | |
2965 | else | |
2966 | decimal2str (paddr_str, "", addr); | |
2967 | return paddr_str; | |
2968 | } | |
2969 | ||
5683e87a AC |
2970 | /* eliminate warning from compiler on 32-bit systems */ |
2971 | static int thirty_two = 32; | |
2972 | ||
104c1213 | 2973 | char * |
5683e87a | 2974 | phex (ULONGEST l, int sizeof_l) |
104c1213 | 2975 | { |
5683e87a AC |
2976 | char *str = get_cell (); |
2977 | switch (sizeof_l) | |
104c1213 JM |
2978 | { |
2979 | case 8: | |
5683e87a AC |
2980 | sprintf (str, "%08lx%08lx", |
2981 | (unsigned long) (l >> thirty_two), | |
2982 | (unsigned long) (l & 0xffffffff)); | |
104c1213 JM |
2983 | break; |
2984 | case 4: | |
5683e87a | 2985 | sprintf (str, "%08lx", (unsigned long) l); |
104c1213 JM |
2986 | break; |
2987 | case 2: | |
5683e87a | 2988 | sprintf (str, "%04x", (unsigned short) (l & 0xffff)); |
104c1213 JM |
2989 | break; |
2990 | default: | |
5683e87a AC |
2991 | phex (l, sizeof (l)); |
2992 | break; | |
104c1213 | 2993 | } |
5683e87a | 2994 | return str; |
104c1213 JM |
2995 | } |
2996 | ||
c5aa993b | 2997 | char * |
5683e87a | 2998 | phex_nz (ULONGEST l, int sizeof_l) |
c906108c | 2999 | { |
5683e87a AC |
3000 | char *str = get_cell (); |
3001 | switch (sizeof_l) | |
c906108c | 3002 | { |
c5aa993b JM |
3003 | case 8: |
3004 | { | |
5683e87a | 3005 | unsigned long high = (unsigned long) (l >> thirty_two); |
c5aa993b | 3006 | if (high == 0) |
5683e87a | 3007 | sprintf (str, "%lx", (unsigned long) (l & 0xffffffff)); |
c5aa993b | 3008 | else |
5683e87a AC |
3009 | sprintf (str, "%lx%08lx", |
3010 | high, (unsigned long) (l & 0xffffffff)); | |
c906108c | 3011 | break; |
c5aa993b JM |
3012 | } |
3013 | case 4: | |
5683e87a | 3014 | sprintf (str, "%lx", (unsigned long) l); |
c5aa993b JM |
3015 | break; |
3016 | case 2: | |
5683e87a | 3017 | sprintf (str, "%x", (unsigned short) (l & 0xffff)); |
c5aa993b JM |
3018 | break; |
3019 | default: | |
5683e87a AC |
3020 | phex_nz (l, sizeof (l)); |
3021 | break; | |
c906108c | 3022 | } |
5683e87a | 3023 | return str; |
c906108c | 3024 | } |
ac2e2ef7 AC |
3025 | |
3026 | ||
3027 | /* Convert to / from the hosts pointer to GDB's internal CORE_ADDR | |
3028 | using the target's conversion routines. */ | |
3029 | CORE_ADDR | |
3030 | host_pointer_to_address (void *ptr) | |
3031 | { | |
3032 | if (sizeof (ptr) != TYPE_LENGTH (builtin_type_ptr)) | |
8e65ff28 AC |
3033 | internal_error (__FILE__, __LINE__, |
3034 | "core_addr_to_void_ptr: bad cast"); | |
ac2e2ef7 AC |
3035 | return POINTER_TO_ADDRESS (builtin_type_ptr, &ptr); |
3036 | } | |
3037 | ||
3038 | void * | |
3039 | address_to_host_pointer (CORE_ADDR addr) | |
3040 | { | |
3041 | void *ptr; | |
3042 | if (sizeof (ptr) != TYPE_LENGTH (builtin_type_ptr)) | |
8e65ff28 AC |
3043 | internal_error (__FILE__, __LINE__, |
3044 | "core_addr_to_void_ptr: bad cast"); | |
ac2e2ef7 AC |
3045 | ADDRESS_TO_POINTER (builtin_type_ptr, &ptr, addr); |
3046 | return ptr; | |
3047 | } |