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