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