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