GDB (API)
|
00001 /* General utility routines for GDB, the GNU debugger. 00002 00003 Copyright (C) 1986-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "dyn-string.h" 00022 #include "gdb_assert.h" 00023 #include <ctype.h> 00024 #include "gdb_string.h" 00025 #include "gdb_wait.h" 00026 #include "event-top.h" 00027 #include "exceptions.h" 00028 #include "gdbthread.h" 00029 #include "fnmatch.h" 00030 #include "gdb_bfd.h" 00031 #ifdef HAVE_SYS_RESOURCE_H 00032 #include <sys/resource.h> 00033 #endif /* HAVE_SYS_RESOURCE_H */ 00034 00035 #ifdef TUI 00036 #include "tui/tui.h" /* For tui_get_command_dimension. */ 00037 #endif 00038 00039 #ifdef __GO32__ 00040 #include <pc.h> 00041 #endif 00042 00043 /* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */ 00044 #ifdef reg 00045 #undef reg 00046 #endif 00047 00048 #include <signal.h> 00049 #include "timeval-utils.h" 00050 #include "gdbcmd.h" 00051 #include "serial.h" 00052 #include "bfd.h" 00053 #include "target.h" 00054 #include "gdb-demangle.h" 00055 #include "expression.h" 00056 #include "language.h" 00057 #include "charset.h" 00058 #include "annotate.h" 00059 #include "filenames.h" 00060 #include "symfile.h" 00061 #include "gdb_obstack.h" 00062 #include "gdbcore.h" 00063 #include "top.h" 00064 #include "main.h" 00065 #include "solist.h" 00066 00067 #include "inferior.h" /* for signed_pointer_to_address */ 00068 00069 #include "gdb_curses.h" 00070 00071 #include "readline/readline.h" 00072 00073 #include <sys/time.h> 00074 #include <time.h> 00075 00076 #include "gdb_usleep.h" 00077 #include "interps.h" 00078 #include "gdb_regex.h" 00079 00080 #if !HAVE_DECL_MALLOC 00081 extern PTR malloc (); /* ARI: PTR */ 00082 #endif 00083 #if !HAVE_DECL_REALLOC 00084 extern PTR realloc (); /* ARI: PTR */ 00085 #endif 00086 #if !HAVE_DECL_FREE 00087 extern void free (); 00088 #endif 00089 00090 void (*deprecated_error_begin_hook) (void); 00091 00092 /* Prototypes for local functions */ 00093 00094 static void vfprintf_maybe_filtered (struct ui_file *, const char *, 00095 va_list, int) ATTRIBUTE_PRINTF (2, 0); 00096 00097 static void fputs_maybe_filtered (const char *, struct ui_file *, int); 00098 00099 static void prompt_for_continue (void); 00100 00101 static void set_screen_size (void); 00102 static void set_width (void); 00103 00104 /* Time spent in prompt_for_continue in the currently executing command 00105 waiting for user to respond. 00106 Initialized in make_command_stats_cleanup. 00107 Modified in prompt_for_continue and defaulted_query. 00108 Used in report_command_stats. */ 00109 00110 static struct timeval prompt_for_continue_wait_time; 00111 00112 /* A flag indicating whether to timestamp debugging messages. */ 00113 00114 static int debug_timestamp = 0; 00115 00116 /* Nonzero if we have job control. */ 00117 00118 int job_control; 00119 00120 #ifndef HAVE_PYTHON 00121 /* Nonzero means a quit has been requested. */ 00122 00123 int quit_flag; 00124 #endif /* HAVE_PYTHON */ 00125 00126 /* Nonzero means quit immediately if Control-C is typed now, rather 00127 than waiting until QUIT is executed. Be careful in setting this; 00128 code which executes with immediate_quit set has to be very careful 00129 about being able to deal with being interrupted at any time. It is 00130 almost always better to use QUIT; the only exception I can think of 00131 is being able to quit out of a system call (using EINTR loses if 00132 the SIGINT happens between the previous QUIT and the system call). 00133 To immediately quit in the case in which a SIGINT happens between 00134 the previous QUIT and setting immediate_quit (desirable anytime we 00135 expect to block), call QUIT after setting immediate_quit. */ 00136 00137 int immediate_quit; 00138 00139 #ifndef HAVE_PYTHON 00140 00141 /* Clear the quit flag. */ 00142 00143 void 00144 clear_quit_flag (void) 00145 { 00146 quit_flag = 0; 00147 } 00148 00149 /* Set the quit flag. */ 00150 00151 void 00152 set_quit_flag (void) 00153 { 00154 quit_flag = 1; 00155 } 00156 00157 /* Return true if the quit flag has been set, false otherwise. */ 00158 00159 int 00160 check_quit_flag (void) 00161 { 00162 /* This is written in a particular way to avoid races. */ 00163 if (quit_flag) 00164 { 00165 quit_flag = 0; 00166 return 1; 00167 } 00168 00169 return 0; 00170 } 00171 00172 #endif /* HAVE_PYTHON */ 00173 00174 /* Nonzero means that strings with character values >0x7F should be printed 00175 as octal escapes. Zero means just print the value (e.g. it's an 00176 international character, and the terminal or window can cope.) */ 00177 00178 int sevenbit_strings = 0; 00179 static void 00180 show_sevenbit_strings (struct ui_file *file, int from_tty, 00181 struct cmd_list_element *c, const char *value) 00182 { 00183 fprintf_filtered (file, _("Printing of 8-bit characters " 00184 "in strings as \\nnn is %s.\n"), 00185 value); 00186 } 00187 00188 /* String to be printed before warning messages, if any. */ 00189 00190 char *warning_pre_print = "\nwarning: "; 00191 00192 int pagination_enabled = 1; 00193 static void 00194 show_pagination_enabled (struct ui_file *file, int from_tty, 00195 struct cmd_list_element *c, const char *value) 00196 { 00197 fprintf_filtered (file, _("State of pagination is %s.\n"), value); 00198 } 00199 00200 00201 /* Cleanup utilities. 00202 00203 These are not defined in cleanups.c (nor declared in cleanups.h) 00204 because while they use the "cleanup API" they are not part of the 00205 "cleanup API". */ 00206 00207 static void 00208 do_freeargv (void *arg) 00209 { 00210 freeargv ((char **) arg); 00211 } 00212 00213 struct cleanup * 00214 make_cleanup_freeargv (char **arg) 00215 { 00216 return make_cleanup (do_freeargv, arg); 00217 } 00218 00219 static void 00220 do_dyn_string_delete (void *arg) 00221 { 00222 dyn_string_delete ((dyn_string_t) arg); 00223 } 00224 00225 struct cleanup * 00226 make_cleanup_dyn_string_delete (dyn_string_t arg) 00227 { 00228 return make_cleanup (do_dyn_string_delete, arg); 00229 } 00230 00231 static void 00232 do_bfd_close_cleanup (void *arg) 00233 { 00234 gdb_bfd_unref (arg); 00235 } 00236 00237 struct cleanup * 00238 make_cleanup_bfd_unref (bfd *abfd) 00239 { 00240 return make_cleanup (do_bfd_close_cleanup, abfd); 00241 } 00242 00243 static void 00244 do_close_cleanup (void *arg) 00245 { 00246 int *fd = arg; 00247 00248 close (*fd); 00249 } 00250 00251 struct cleanup * 00252 make_cleanup_close (int fd) 00253 { 00254 int *saved_fd = xmalloc (sizeof (fd)); 00255 00256 *saved_fd = fd; 00257 return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree); 00258 } 00259 00260 /* Helper function which does the work for make_cleanup_fclose. */ 00261 00262 static void 00263 do_fclose_cleanup (void *arg) 00264 { 00265 FILE *file = arg; 00266 00267 fclose (file); 00268 } 00269 00270 /* Return a new cleanup that closes FILE. */ 00271 00272 struct cleanup * 00273 make_cleanup_fclose (FILE *file) 00274 { 00275 return make_cleanup (do_fclose_cleanup, file); 00276 } 00277 00278 /* Helper function which does the work for make_cleanup_obstack_free. */ 00279 00280 static void 00281 do_obstack_free (void *arg) 00282 { 00283 struct obstack *ob = arg; 00284 00285 obstack_free (ob, NULL); 00286 } 00287 00288 /* Return a new cleanup that frees OBSTACK. */ 00289 00290 struct cleanup * 00291 make_cleanup_obstack_free (struct obstack *obstack) 00292 { 00293 return make_cleanup (do_obstack_free, obstack); 00294 } 00295 00296 static void 00297 do_ui_file_delete (void *arg) 00298 { 00299 ui_file_delete (arg); 00300 } 00301 00302 struct cleanup * 00303 make_cleanup_ui_file_delete (struct ui_file *arg) 00304 { 00305 return make_cleanup (do_ui_file_delete, arg); 00306 } 00307 00308 /* Helper function for make_cleanup_ui_out_redirect_pop. */ 00309 00310 static void 00311 do_ui_out_redirect_pop (void *arg) 00312 { 00313 struct ui_out *uiout = arg; 00314 00315 if (ui_out_redirect (uiout, NULL) < 0) 00316 warning (_("Cannot restore redirection of the current output protocol")); 00317 } 00318 00319 /* Return a new cleanup that pops the last redirection by ui_out_redirect 00320 with NULL parameter. */ 00321 00322 struct cleanup * 00323 make_cleanup_ui_out_redirect_pop (struct ui_out *uiout) 00324 { 00325 return make_cleanup (do_ui_out_redirect_pop, uiout); 00326 } 00327 00328 static void 00329 do_free_section_addr_info (void *arg) 00330 { 00331 free_section_addr_info (arg); 00332 } 00333 00334 struct cleanup * 00335 make_cleanup_free_section_addr_info (struct section_addr_info *addrs) 00336 { 00337 return make_cleanup (do_free_section_addr_info, addrs); 00338 } 00339 00340 struct restore_integer_closure 00341 { 00342 int *variable; 00343 int value; 00344 }; 00345 00346 static void 00347 restore_integer (void *p) 00348 { 00349 struct restore_integer_closure *closure = p; 00350 00351 *(closure->variable) = closure->value; 00352 } 00353 00354 /* Remember the current value of *VARIABLE and make it restored when 00355 the cleanup is run. */ 00356 00357 struct cleanup * 00358 make_cleanup_restore_integer (int *variable) 00359 { 00360 struct restore_integer_closure *c = 00361 xmalloc (sizeof (struct restore_integer_closure)); 00362 00363 c->variable = variable; 00364 c->value = *variable; 00365 00366 return make_cleanup_dtor (restore_integer, (void *) c, xfree); 00367 } 00368 00369 /* Remember the current value of *VARIABLE and make it restored when 00370 the cleanup is run. */ 00371 00372 struct cleanup * 00373 make_cleanup_restore_uinteger (unsigned int *variable) 00374 { 00375 return make_cleanup_restore_integer ((int *) variable); 00376 } 00377 00378 /* Helper for make_cleanup_unpush_target. */ 00379 00380 static void 00381 do_unpush_target (void *arg) 00382 { 00383 struct target_ops *ops = arg; 00384 00385 unpush_target (ops); 00386 } 00387 00388 /* Return a new cleanup that unpushes OPS. */ 00389 00390 struct cleanup * 00391 make_cleanup_unpush_target (struct target_ops *ops) 00392 { 00393 return make_cleanup (do_unpush_target, ops); 00394 } 00395 00396 /* Helper for make_cleanup_htab_delete compile time checking the types. */ 00397 00398 static void 00399 do_htab_delete_cleanup (void *htab_voidp) 00400 { 00401 htab_t htab = htab_voidp; 00402 00403 htab_delete (htab); 00404 } 00405 00406 /* Return a new cleanup that deletes HTAB. */ 00407 00408 struct cleanup * 00409 make_cleanup_htab_delete (htab_t htab) 00410 { 00411 return make_cleanup (do_htab_delete_cleanup, htab); 00412 } 00413 00414 struct restore_ui_file_closure 00415 { 00416 struct ui_file **variable; 00417 struct ui_file *value; 00418 }; 00419 00420 static void 00421 do_restore_ui_file (void *p) 00422 { 00423 struct restore_ui_file_closure *closure = p; 00424 00425 *(closure->variable) = closure->value; 00426 } 00427 00428 /* Remember the current value of *VARIABLE and make it restored when 00429 the cleanup is run. */ 00430 00431 struct cleanup * 00432 make_cleanup_restore_ui_file (struct ui_file **variable) 00433 { 00434 struct restore_ui_file_closure *c = XNEW (struct restore_ui_file_closure); 00435 00436 c->variable = variable; 00437 c->value = *variable; 00438 00439 return make_cleanup_dtor (do_restore_ui_file, (void *) c, xfree); 00440 } 00441 00442 /* Helper for make_cleanup_value_free_to_mark. */ 00443 00444 static void 00445 do_value_free_to_mark (void *value) 00446 { 00447 value_free_to_mark ((struct value *) value); 00448 } 00449 00450 /* Free all values allocated since MARK was obtained by value_mark 00451 (except for those released) when the cleanup is run. */ 00452 00453 struct cleanup * 00454 make_cleanup_value_free_to_mark (struct value *mark) 00455 { 00456 return make_cleanup (do_value_free_to_mark, mark); 00457 } 00458 00459 /* Helper for make_cleanup_value_free. */ 00460 00461 static void 00462 do_value_free (void *value) 00463 { 00464 value_free (value); 00465 } 00466 00467 /* Free VALUE. */ 00468 00469 struct cleanup * 00470 make_cleanup_value_free (struct value *value) 00471 { 00472 return make_cleanup (do_value_free, value); 00473 } 00474 00475 /* Helper for make_cleanup_free_so. */ 00476 00477 static void 00478 do_free_so (void *arg) 00479 { 00480 struct so_list *so = arg; 00481 00482 free_so (so); 00483 } 00484 00485 /* Make cleanup handler calling free_so for SO. */ 00486 00487 struct cleanup * 00488 make_cleanup_free_so (struct so_list *so) 00489 { 00490 return make_cleanup (do_free_so, so); 00491 } 00492 00493 /* Helper for make_cleanup_restore_current_language. */ 00494 00495 static void 00496 do_restore_current_language (void *p) 00497 { 00498 enum language saved_lang = (uintptr_t) p; 00499 00500 set_language (saved_lang); 00501 } 00502 00503 /* Remember the current value of CURRENT_LANGUAGE and make it restored when 00504 the cleanup is run. */ 00505 00506 struct cleanup * 00507 make_cleanup_restore_current_language (void) 00508 { 00509 enum language saved_lang = current_language->la_language; 00510 00511 return make_cleanup (do_restore_current_language, 00512 (void *) (uintptr_t) saved_lang); 00513 } 00514 00515 /* This function is useful for cleanups. 00516 Do 00517 00518 foo = xmalloc (...); 00519 old_chain = make_cleanup (free_current_contents, &foo); 00520 00521 to arrange to free the object thus allocated. */ 00522 00523 void 00524 free_current_contents (void *ptr) 00525 { 00526 void **location = ptr; 00527 00528 if (location == NULL) 00529 internal_error (__FILE__, __LINE__, 00530 _("free_current_contents: NULL pointer")); 00531 if (*location != NULL) 00532 { 00533 xfree (*location); 00534 *location = NULL; 00535 } 00536 } 00537 00538 00539 00540 /* Print a warning message. The first argument STRING is the warning 00541 message, used as an fprintf format string, the second is the 00542 va_list of arguments for that string. A warning is unfiltered (not 00543 paginated) so that the user does not need to page through each 00544 screen full of warnings when there are lots of them. */ 00545 00546 void 00547 vwarning (const char *string, va_list args) 00548 { 00549 if (deprecated_warning_hook) 00550 (*deprecated_warning_hook) (string, args); 00551 else 00552 { 00553 target_terminal_ours (); 00554 wrap_here (""); /* Force out any buffered output. */ 00555 gdb_flush (gdb_stdout); 00556 if (warning_pre_print) 00557 fputs_unfiltered (warning_pre_print, gdb_stderr); 00558 vfprintf_unfiltered (gdb_stderr, string, args); 00559 fprintf_unfiltered (gdb_stderr, "\n"); 00560 va_end (args); 00561 } 00562 } 00563 00564 void 00565 warning (const char *string, ...) 00566 { 00567 va_list args; 00568 00569 va_start (args, string); 00570 vwarning (string, args); 00571 va_end (args); 00572 } 00573 00574 /* Print an error message and return to command level. 00575 The first argument STRING is the error message, used as a fprintf string, 00576 and the remaining args are passed as arguments to it. */ 00577 00578 void 00579 verror (const char *string, va_list args) 00580 { 00581 throw_verror (GENERIC_ERROR, string, args); 00582 } 00583 00584 void 00585 error (const char *string, ...) 00586 { 00587 va_list args; 00588 00589 va_start (args, string); 00590 throw_verror (GENERIC_ERROR, string, args); 00591 va_end (args); 00592 } 00593 00594 /* Print an error message and quit. 00595 The first argument STRING is the error message, used as a fprintf string, 00596 and the remaining args are passed as arguments to it. */ 00597 00598 void 00599 vfatal (const char *string, va_list args) 00600 { 00601 throw_vfatal (string, args); 00602 } 00603 00604 void 00605 fatal (const char *string, ...) 00606 { 00607 va_list args; 00608 00609 va_start (args, string); 00610 throw_vfatal (string, args); 00611 va_end (args); 00612 } 00613 00614 void 00615 error_stream (struct ui_file *stream) 00616 { 00617 char *message = ui_file_xstrdup (stream, NULL); 00618 00619 make_cleanup (xfree, message); 00620 error (("%s"), message); 00621 } 00622 00623 /* Dump core trying to increase the core soft limit to hard limit first. */ 00624 00625 static void 00626 dump_core (void) 00627 { 00628 #ifdef HAVE_SETRLIMIT 00629 struct rlimit rlim = { RLIM_INFINITY, RLIM_INFINITY }; 00630 00631 setrlimit (RLIMIT_CORE, &rlim); 00632 #endif /* HAVE_SETRLIMIT */ 00633 00634 abort (); /* NOTE: GDB has only three calls to abort(). */ 00635 } 00636 00637 /* Check whether GDB will be able to dump core using the dump_core 00638 function. */ 00639 00640 static int 00641 can_dump_core (const char *reason) 00642 { 00643 #ifdef HAVE_GETRLIMIT 00644 struct rlimit rlim; 00645 00646 /* Be quiet and assume we can dump if an error is returned. */ 00647 if (getrlimit (RLIMIT_CORE, &rlim) != 0) 00648 return 1; 00649 00650 if (rlim.rlim_max == 0) 00651 { 00652 fprintf_unfiltered (gdb_stderr, 00653 _("%s\nUnable to dump core, use `ulimit -c" 00654 " unlimited' before executing GDB next time.\n"), 00655 reason); 00656 return 0; 00657 } 00658 #endif /* HAVE_GETRLIMIT */ 00659 00660 return 1; 00661 } 00662 00663 /* Allow the user to configure the debugger behavior with respect to 00664 what to do when an internal problem is detected. */ 00665 00666 const char internal_problem_ask[] = "ask"; 00667 const char internal_problem_yes[] = "yes"; 00668 const char internal_problem_no[] = "no"; 00669 static const char *const internal_problem_modes[] = 00670 { 00671 internal_problem_ask, 00672 internal_problem_yes, 00673 internal_problem_no, 00674 NULL 00675 }; 00676 00677 /* Print a message reporting an internal error/warning. Ask the user 00678 if they want to continue, dump core, or just exit. Return 00679 something to indicate a quit. */ 00680 00681 struct internal_problem 00682 { 00683 const char *name; 00684 const char *should_quit; 00685 const char *should_dump_core; 00686 }; 00687 00688 /* Report a problem, internal to GDB, to the user. Once the problem 00689 has been reported, and assuming GDB didn't quit, the caller can 00690 either allow execution to resume or throw an error. */ 00691 00692 static void ATTRIBUTE_PRINTF (4, 0) 00693 internal_vproblem (struct internal_problem *problem, 00694 const char *file, int line, const char *fmt, va_list ap) 00695 { 00696 static int dejavu; 00697 int quit_p; 00698 int dump_core_p; 00699 char *reason; 00700 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL); 00701 00702 /* Don't allow infinite error/warning recursion. */ 00703 { 00704 static char msg[] = "Recursive internal problem.\n"; 00705 00706 switch (dejavu) 00707 { 00708 case 0: 00709 dejavu = 1; 00710 break; 00711 case 1: 00712 dejavu = 2; 00713 fputs_unfiltered (msg, gdb_stderr); 00714 abort (); /* NOTE: GDB has only three calls to abort(). */ 00715 default: 00716 dejavu = 3; 00717 /* Newer GLIBC versions put the warn_unused_result attribute 00718 on write, but this is one of those rare cases where 00719 ignoring the return value is correct. Casting to (void) 00720 does not fix this problem. This is the solution suggested 00721 at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509. */ 00722 if (write (STDERR_FILENO, msg, sizeof (msg)) != sizeof (msg)) 00723 abort (); /* NOTE: GDB has only three calls to abort(). */ 00724 exit (1); 00725 } 00726 } 00727 00728 /* Try to get the message out and at the start of a new line. */ 00729 target_terminal_ours (); 00730 begin_line (); 00731 00732 /* Create a string containing the full error/warning message. Need 00733 to call query with this full string, as otherwize the reason 00734 (error/warning) and question become separated. Format using a 00735 style similar to a compiler error message. Include extra detail 00736 so that the user knows that they are living on the edge. */ 00737 { 00738 char *msg; 00739 00740 msg = xstrvprintf (fmt, ap); 00741 reason = xstrprintf ("%s:%d: %s: %s\n" 00742 "A problem internal to GDB has been detected,\n" 00743 "further debugging may prove unreliable.", 00744 file, line, problem->name, msg); 00745 xfree (msg); 00746 make_cleanup (xfree, reason); 00747 } 00748 00749 if (problem->should_quit == internal_problem_ask) 00750 { 00751 /* Default (yes/batch case) is to quit GDB. When in batch mode 00752 this lessens the likelihood of GDB going into an infinite 00753 loop. */ 00754 if (!confirm) 00755 { 00756 /* Emit the message and quit. */ 00757 fputs_unfiltered (reason, gdb_stderr); 00758 fputs_unfiltered ("\n", gdb_stderr); 00759 quit_p = 1; 00760 } 00761 else 00762 quit_p = query (_("%s\nQuit this debugging session? "), reason); 00763 } 00764 else if (problem->should_quit == internal_problem_yes) 00765 quit_p = 1; 00766 else if (problem->should_quit == internal_problem_no) 00767 quit_p = 0; 00768 else 00769 internal_error (__FILE__, __LINE__, _("bad switch")); 00770 00771 if (problem->should_dump_core == internal_problem_ask) 00772 { 00773 if (!can_dump_core (reason)) 00774 dump_core_p = 0; 00775 else 00776 { 00777 /* Default (yes/batch case) is to dump core. This leaves a GDB 00778 `dropping' so that it is easier to see that something went 00779 wrong in GDB. */ 00780 dump_core_p = query (_("%s\nCreate a core file of GDB? "), reason); 00781 } 00782 } 00783 else if (problem->should_dump_core == internal_problem_yes) 00784 dump_core_p = can_dump_core (reason); 00785 else if (problem->should_dump_core == internal_problem_no) 00786 dump_core_p = 0; 00787 else 00788 internal_error (__FILE__, __LINE__, _("bad switch")); 00789 00790 if (quit_p) 00791 { 00792 if (dump_core_p) 00793 dump_core (); 00794 else 00795 exit (1); 00796 } 00797 else 00798 { 00799 if (dump_core_p) 00800 { 00801 #ifdef HAVE_WORKING_FORK 00802 if (fork () == 0) 00803 dump_core (); 00804 #endif 00805 } 00806 } 00807 00808 dejavu = 0; 00809 do_cleanups (cleanup); 00810 } 00811 00812 static struct internal_problem internal_error_problem = { 00813 "internal-error", internal_problem_ask, internal_problem_ask 00814 }; 00815 00816 void 00817 internal_verror (const char *file, int line, const char *fmt, va_list ap) 00818 { 00819 internal_vproblem (&internal_error_problem, file, line, fmt, ap); 00820 fatal (_("Command aborted.")); 00821 } 00822 00823 void 00824 internal_error (const char *file, int line, const char *string, ...) 00825 { 00826 va_list ap; 00827 00828 va_start (ap, string); 00829 internal_verror (file, line, string, ap); 00830 va_end (ap); 00831 } 00832 00833 static struct internal_problem internal_warning_problem = { 00834 "internal-warning", internal_problem_ask, internal_problem_ask 00835 }; 00836 00837 void 00838 internal_vwarning (const char *file, int line, const char *fmt, va_list ap) 00839 { 00840 internal_vproblem (&internal_warning_problem, file, line, fmt, ap); 00841 } 00842 00843 void 00844 internal_warning (const char *file, int line, const char *string, ...) 00845 { 00846 va_list ap; 00847 00848 va_start (ap, string); 00849 internal_vwarning (file, line, string, ap); 00850 va_end (ap); 00851 } 00852 00853 /* Dummy functions to keep add_prefix_cmd happy. */ 00854 00855 static void 00856 set_internal_problem_cmd (char *args, int from_tty) 00857 { 00858 } 00859 00860 static void 00861 show_internal_problem_cmd (char *args, int from_tty) 00862 { 00863 } 00864 00865 /* When GDB reports an internal problem (error or warning) it gives 00866 the user the opportunity to quit GDB and/or create a core file of 00867 the current debug session. This function registers a few commands 00868 that make it possible to specify that GDB should always or never 00869 quit or create a core file, without asking. The commands look 00870 like: 00871 00872 maint set PROBLEM-NAME quit ask|yes|no 00873 maint show PROBLEM-NAME quit 00874 maint set PROBLEM-NAME corefile ask|yes|no 00875 maint show PROBLEM-NAME corefile 00876 00877 Where PROBLEM-NAME is currently "internal-error" or 00878 "internal-warning". */ 00879 00880 static void 00881 add_internal_problem_command (struct internal_problem *problem) 00882 { 00883 struct cmd_list_element **set_cmd_list; 00884 struct cmd_list_element **show_cmd_list; 00885 char *set_doc; 00886 char *show_doc; 00887 00888 set_cmd_list = xmalloc (sizeof (*set_cmd_list)); 00889 show_cmd_list = xmalloc (sizeof (*set_cmd_list)); 00890 *set_cmd_list = NULL; 00891 *show_cmd_list = NULL; 00892 00893 set_doc = xstrprintf (_("Configure what GDB does when %s is detected."), 00894 problem->name); 00895 00896 show_doc = xstrprintf (_("Show what GDB does when %s is detected."), 00897 problem->name); 00898 00899 add_prefix_cmd ((char*) problem->name, 00900 class_maintenance, set_internal_problem_cmd, set_doc, 00901 set_cmd_list, 00902 concat ("maintenance set ", problem->name, " ", 00903 (char *) NULL), 00904 0/*allow-unknown*/, &maintenance_set_cmdlist); 00905 00906 add_prefix_cmd ((char*) problem->name, 00907 class_maintenance, show_internal_problem_cmd, show_doc, 00908 show_cmd_list, 00909 concat ("maintenance show ", problem->name, " ", 00910 (char *) NULL), 00911 0/*allow-unknown*/, &maintenance_show_cmdlist); 00912 00913 set_doc = xstrprintf (_("Set whether GDB should quit " 00914 "when an %s is detected"), 00915 problem->name); 00916 show_doc = xstrprintf (_("Show whether GDB will quit " 00917 "when an %s is detected"), 00918 problem->name); 00919 add_setshow_enum_cmd ("quit", class_maintenance, 00920 internal_problem_modes, 00921 &problem->should_quit, 00922 set_doc, 00923 show_doc, 00924 NULL, /* help_doc */ 00925 NULL, /* setfunc */ 00926 NULL, /* showfunc */ 00927 set_cmd_list, 00928 show_cmd_list); 00929 00930 xfree (set_doc); 00931 xfree (show_doc); 00932 00933 set_doc = xstrprintf (_("Set whether GDB should create a core " 00934 "file of GDB when %s is detected"), 00935 problem->name); 00936 show_doc = xstrprintf (_("Show whether GDB will create a core " 00937 "file of GDB when %s is detected"), 00938 problem->name); 00939 add_setshow_enum_cmd ("corefile", class_maintenance, 00940 internal_problem_modes, 00941 &problem->should_dump_core, 00942 set_doc, 00943 show_doc, 00944 NULL, /* help_doc */ 00945 NULL, /* setfunc */ 00946 NULL, /* showfunc */ 00947 set_cmd_list, 00948 show_cmd_list); 00949 00950 xfree (set_doc); 00951 xfree (show_doc); 00952 } 00953 00954 /* Return a newly allocated string, containing the PREFIX followed 00955 by the system error message for errno (separated by a colon). 00956 00957 The result must be deallocated after use. */ 00958 00959 static char * 00960 perror_string (const char *prefix) 00961 { 00962 char *err; 00963 char *combined; 00964 00965 err = safe_strerror (errno); 00966 combined = (char *) xmalloc (strlen (err) + strlen (prefix) + 3); 00967 strcpy (combined, prefix); 00968 strcat (combined, ": "); 00969 strcat (combined, err); 00970 00971 return combined; 00972 } 00973 00974 /* Print the system error message for errno, and also mention STRING 00975 as the file name for which the error was encountered. Use ERRCODE 00976 for the thrown exception. Then return to command level. */ 00977 00978 void 00979 throw_perror_with_name (enum errors errcode, const char *string) 00980 { 00981 char *combined; 00982 00983 combined = perror_string (string); 00984 make_cleanup (xfree, combined); 00985 00986 /* I understand setting these is a matter of taste. Still, some people 00987 may clear errno but not know about bfd_error. Doing this here is not 00988 unreasonable. */ 00989 bfd_set_error (bfd_error_no_error); 00990 errno = 0; 00991 00992 throw_error (errcode, _("%s."), combined); 00993 } 00994 00995 /* See throw_perror_with_name, ERRCODE defaults here to GENERIC_ERROR. */ 00996 00997 void 00998 perror_with_name (const char *string) 00999 { 01000 throw_perror_with_name (GENERIC_ERROR, string); 01001 } 01002 01003 /* Same as perror_with_name except that it prints a warning instead 01004 of throwing an error. */ 01005 01006 void 01007 perror_warning_with_name (const char *string) 01008 { 01009 char *combined; 01010 01011 combined = perror_string (string); 01012 warning (_("%s"), combined); 01013 xfree (combined); 01014 } 01015 01016 /* Print the system error message for ERRCODE, and also mention STRING 01017 as the file name for which the error was encountered. */ 01018 01019 void 01020 print_sys_errmsg (const char *string, int errcode) 01021 { 01022 char *err; 01023 char *combined; 01024 01025 err = safe_strerror (errcode); 01026 combined = (char *) alloca (strlen (err) + strlen (string) + 3); 01027 strcpy (combined, string); 01028 strcat (combined, ": "); 01029 strcat (combined, err); 01030 01031 /* We want anything which was printed on stdout to come out first, before 01032 this message. */ 01033 gdb_flush (gdb_stdout); 01034 fprintf_unfiltered (gdb_stderr, "%s.\n", combined); 01035 } 01036 01037 /* Control C eventually causes this to be called, at a convenient time. */ 01038 01039 void 01040 quit (void) 01041 { 01042 #ifdef __MSDOS__ 01043 /* No steenking SIGINT will ever be coming our way when the 01044 program is resumed. Don't lie. */ 01045 fatal ("Quit"); 01046 #else 01047 if (job_control 01048 /* If there is no terminal switching for this target, then we can't 01049 possibly get screwed by the lack of job control. */ 01050 || current_target.to_terminal_ours == NULL) 01051 fatal ("Quit"); 01052 else 01053 fatal ("Quit (expect signal SIGINT when the program is resumed)"); 01054 #endif 01055 } 01056 01057 01058 /* Called when a memory allocation fails, with the number of bytes of 01059 memory requested in SIZE. */ 01060 01061 void 01062 malloc_failure (long size) 01063 { 01064 if (size > 0) 01065 { 01066 internal_error (__FILE__, __LINE__, 01067 _("virtual memory exhausted: can't allocate %ld bytes."), 01068 size); 01069 } 01070 else 01071 { 01072 internal_error (__FILE__, __LINE__, _("virtual memory exhausted.")); 01073 } 01074 } 01075 01076 /* My replacement for the read system call. 01077 Used like `read' but keeps going if `read' returns too soon. */ 01078 01079 int 01080 myread (int desc, char *addr, int len) 01081 { 01082 int val; 01083 int orglen = len; 01084 01085 while (len > 0) 01086 { 01087 val = read (desc, addr, len); 01088 if (val < 0) 01089 return val; 01090 if (val == 0) 01091 return orglen - len; 01092 len -= val; 01093 addr += val; 01094 } 01095 return orglen; 01096 } 01097 01098 void 01099 print_spaces (int n, struct ui_file *file) 01100 { 01101 fputs_unfiltered (n_spaces (n), file); 01102 } 01103 01104 /* Print a host address. */ 01105 01106 void 01107 gdb_print_host_address (const void *addr, struct ui_file *stream) 01108 { 01109 fprintf_filtered (stream, "%s", host_address_to_string (addr)); 01110 } 01111 01112 01113 /* A cleanup function that calls regfree. */ 01114 01115 static void 01116 do_regfree_cleanup (void *r) 01117 { 01118 regfree (r); 01119 } 01120 01121 /* Create a new cleanup that frees the compiled regular expression R. */ 01122 01123 struct cleanup * 01124 make_regfree_cleanup (regex_t *r) 01125 { 01126 return make_cleanup (do_regfree_cleanup, r); 01127 } 01128 01129 /* Return an xmalloc'd error message resulting from a regular 01130 expression compilation failure. */ 01131 01132 char * 01133 get_regcomp_error (int code, regex_t *rx) 01134 { 01135 size_t length = regerror (code, rx, NULL, 0); 01136 char *result = xmalloc (length); 01137 01138 regerror (code, rx, result, length); 01139 return result; 01140 } 01141 01142 /* Compile a regexp and throw an exception on error. This returns a 01143 cleanup to free the resulting pattern on success. RX must not be 01144 NULL. */ 01145 01146 struct cleanup * 01147 compile_rx_or_error (regex_t *pattern, const char *rx, const char *message) 01148 { 01149 int code; 01150 01151 gdb_assert (rx != NULL); 01152 01153 code = regcomp (pattern, rx, REG_NOSUB); 01154 if (code != 0) 01155 { 01156 char *err = get_regcomp_error (code, pattern); 01157 01158 make_cleanup (xfree, err); 01159 error (("%s: %s"), message, err); 01160 } 01161 01162 return make_regfree_cleanup (pattern); 01163 } 01164 01165 01166 01167 /* This function supports the query, nquery, and yquery functions. 01168 Ask user a y-or-n question and return 0 if answer is no, 1 if 01169 answer is yes, or default the answer to the specified default 01170 (for yquery or nquery). DEFCHAR may be 'y' or 'n' to provide a 01171 default answer, or '\0' for no default. 01172 CTLSTR is the control string and should end in "? ". It should 01173 not say how to answer, because we do that. 01174 ARGS are the arguments passed along with the CTLSTR argument to 01175 printf. */ 01176 01177 static int ATTRIBUTE_PRINTF (1, 0) 01178 defaulted_query (const char *ctlstr, const char defchar, va_list args) 01179 { 01180 int answer; 01181 int ans2; 01182 int retval; 01183 int def_value; 01184 char def_answer, not_def_answer; 01185 char *y_string, *n_string, *question; 01186 /* Used to add duration we waited for user to respond to 01187 prompt_for_continue_wait_time. */ 01188 struct timeval prompt_started, prompt_ended, prompt_delta; 01189 01190 /* Set up according to which answer is the default. */ 01191 if (defchar == '\0') 01192 { 01193 def_value = 1; 01194 def_answer = 'Y'; 01195 not_def_answer = 'N'; 01196 y_string = "y"; 01197 n_string = "n"; 01198 } 01199 else if (defchar == 'y') 01200 { 01201 def_value = 1; 01202 def_answer = 'Y'; 01203 not_def_answer = 'N'; 01204 y_string = "[y]"; 01205 n_string = "n"; 01206 } 01207 else 01208 { 01209 def_value = 0; 01210 def_answer = 'N'; 01211 not_def_answer = 'Y'; 01212 y_string = "y"; 01213 n_string = "[n]"; 01214 } 01215 01216 /* Automatically answer the default value if the user did not want 01217 prompts or the command was issued with the server prefix. */ 01218 if (!confirm || server_command) 01219 return def_value; 01220 01221 /* If input isn't coming from the user directly, just say what 01222 question we're asking, and then answer the default automatically. This 01223 way, important error messages don't get lost when talking to GDB 01224 over a pipe. */ 01225 if (! input_from_terminal_p ()) 01226 { 01227 wrap_here (""); 01228 vfprintf_filtered (gdb_stdout, ctlstr, args); 01229 01230 printf_filtered (_("(%s or %s) [answered %c; " 01231 "input not from terminal]\n"), 01232 y_string, n_string, def_answer); 01233 gdb_flush (gdb_stdout); 01234 01235 return def_value; 01236 } 01237 01238 if (deprecated_query_hook) 01239 { 01240 return deprecated_query_hook (ctlstr, args); 01241 } 01242 01243 /* Format the question outside of the loop, to avoid reusing args. */ 01244 question = xstrvprintf (ctlstr, args); 01245 01246 /* Used for calculating time spend waiting for user. */ 01247 gettimeofday (&prompt_started, NULL); 01248 01249 while (1) 01250 { 01251 wrap_here (""); /* Flush any buffered output. */ 01252 gdb_flush (gdb_stdout); 01253 01254 if (annotation_level > 1) 01255 printf_filtered (("\n\032\032pre-query\n")); 01256 01257 fputs_filtered (question, gdb_stdout); 01258 printf_filtered (_("(%s or %s) "), y_string, n_string); 01259 01260 if (annotation_level > 1) 01261 printf_filtered (("\n\032\032query\n")); 01262 01263 wrap_here (""); 01264 gdb_flush (gdb_stdout); 01265 01266 answer = fgetc (stdin); 01267 01268 /* We expect fgetc to block until a character is read. But 01269 this may not be the case if the terminal was opened with 01270 the NONBLOCK flag. In that case, if there is nothing to 01271 read on stdin, fgetc returns EOF, but also sets the error 01272 condition flag on stdin and errno to EAGAIN. With a true 01273 EOF, stdin's error condition flag is not set. 01274 01275 A situation where this behavior was observed is a pseudo 01276 terminal on AIX. */ 01277 while (answer == EOF && ferror (stdin) && errno == EAGAIN) 01278 { 01279 /* Not a real EOF. Wait a little while and try again until 01280 we read something. */ 01281 clearerr (stdin); 01282 gdb_usleep (10000); 01283 answer = fgetc (stdin); 01284 } 01285 01286 clearerr (stdin); /* in case of C-d */ 01287 if (answer == EOF) /* C-d */ 01288 { 01289 printf_filtered ("EOF [assumed %c]\n", def_answer); 01290 retval = def_value; 01291 break; 01292 } 01293 /* Eat rest of input line, to EOF or newline. */ 01294 if (answer != '\n') 01295 do 01296 { 01297 ans2 = fgetc (stdin); 01298 clearerr (stdin); 01299 } 01300 while (ans2 != EOF && ans2 != '\n' && ans2 != '\r'); 01301 01302 if (answer >= 'a') 01303 answer -= 040; 01304 /* Check answer. For the non-default, the user must specify 01305 the non-default explicitly. */ 01306 if (answer == not_def_answer) 01307 { 01308 retval = !def_value; 01309 break; 01310 } 01311 /* Otherwise, if a default was specified, the user may either 01312 specify the required input or have it default by entering 01313 nothing. */ 01314 if (answer == def_answer 01315 || (defchar != '\0' && 01316 (answer == '\n' || answer == '\r' || answer == EOF))) 01317 { 01318 retval = def_value; 01319 break; 01320 } 01321 /* Invalid entries are not defaulted and require another selection. */ 01322 printf_filtered (_("Please answer %s or %s.\n"), 01323 y_string, n_string); 01324 } 01325 01326 /* Add time spend in this routine to prompt_for_continue_wait_time. */ 01327 gettimeofday (&prompt_ended, NULL); 01328 timeval_sub (&prompt_delta, &prompt_ended, &prompt_started); 01329 timeval_add (&prompt_for_continue_wait_time, 01330 &prompt_for_continue_wait_time, &prompt_delta); 01331 01332 xfree (question); 01333 if (annotation_level > 1) 01334 printf_filtered (("\n\032\032post-query\n")); 01335 return retval; 01336 } 01337 01338 01339 /* Ask user a y-or-n question and return 0 if answer is no, 1 if 01340 answer is yes, or 0 if answer is defaulted. 01341 Takes three args which are given to printf to print the question. 01342 The first, a control string, should end in "? ". 01343 It should not say how to answer, because we do that. */ 01344 01345 int 01346 nquery (const char *ctlstr, ...) 01347 { 01348 va_list args; 01349 int ret; 01350 01351 va_start (args, ctlstr); 01352 ret = defaulted_query (ctlstr, 'n', args); 01353 va_end (args); 01354 return ret; 01355 } 01356 01357 /* Ask user a y-or-n question and return 0 if answer is no, 1 if 01358 answer is yes, or 1 if answer is defaulted. 01359 Takes three args which are given to printf to print the question. 01360 The first, a control string, should end in "? ". 01361 It should not say how to answer, because we do that. */ 01362 01363 int 01364 yquery (const char *ctlstr, ...) 01365 { 01366 va_list args; 01367 int ret; 01368 01369 va_start (args, ctlstr); 01370 ret = defaulted_query (ctlstr, 'y', args); 01371 va_end (args); 01372 return ret; 01373 } 01374 01375 /* Ask user a y-or-n question and return 1 iff answer is yes. 01376 Takes three args which are given to printf to print the question. 01377 The first, a control string, should end in "? ". 01378 It should not say how to answer, because we do that. */ 01379 01380 int 01381 query (const char *ctlstr, ...) 01382 { 01383 va_list args; 01384 int ret; 01385 01386 va_start (args, ctlstr); 01387 ret = defaulted_query (ctlstr, '\0', args); 01388 va_end (args); 01389 return ret; 01390 } 01391 01392 /* A helper for parse_escape that converts a host character to a 01393 target character. C is the host character. If conversion is 01394 possible, then the target character is stored in *TARGET_C and the 01395 function returns 1. Otherwise, the function returns 0. */ 01396 01397 static int 01398 host_char_to_target (struct gdbarch *gdbarch, int c, int *target_c) 01399 { 01400 struct obstack host_data; 01401 char the_char = c; 01402 struct cleanup *cleanups; 01403 int result = 0; 01404 01405 obstack_init (&host_data); 01406 cleanups = make_cleanup_obstack_free (&host_data); 01407 01408 convert_between_encodings (target_charset (gdbarch), host_charset (), 01409 (gdb_byte *) &the_char, 1, 1, 01410 &host_data, translit_none); 01411 01412 if (obstack_object_size (&host_data) == 1) 01413 { 01414 result = 1; 01415 *target_c = *(char *) obstack_base (&host_data); 01416 } 01417 01418 do_cleanups (cleanups); 01419 return result; 01420 } 01421 01422 int 01423 parse_escape (struct gdbarch *gdbarch, const char **string_ptr) 01424 { 01425 int target_char = -2; /* Initialize to avoid GCC warnings. */ 01426 int c = *(*string_ptr)++; 01427 01428 switch (c) 01429 { 01430 case '\n': 01431 return -2; 01432 case 0: 01433 (*string_ptr)--; 01434 return 0; 01435 01436 case '0': 01437 case '1': 01438 case '2': 01439 case '3': 01440 case '4': 01441 case '5': 01442 case '6': 01443 case '7': 01444 { 01445 int i = host_hex_value (c); 01446 int count = 0; 01447 while (++count < 3) 01448 { 01449 c = (**string_ptr); 01450 if (isdigit (c) && c != '8' && c != '9') 01451 { 01452 (*string_ptr)++; 01453 i *= 8; 01454 i += host_hex_value (c); 01455 } 01456 else 01457 { 01458 break; 01459 } 01460 } 01461 return i; 01462 } 01463 01464 case 'a': 01465 c = '\a'; 01466 break; 01467 case 'b': 01468 c = '\b'; 01469 break; 01470 case 'f': 01471 c = '\f'; 01472 break; 01473 case 'n': 01474 c = '\n'; 01475 break; 01476 case 'r': 01477 c = '\r'; 01478 break; 01479 case 't': 01480 c = '\t'; 01481 break; 01482 case 'v': 01483 c = '\v'; 01484 break; 01485 01486 default: 01487 break; 01488 } 01489 01490 if (!host_char_to_target (gdbarch, c, &target_char)) 01491 error (_("The escape sequence `\\%c' is equivalent to plain `%c'," 01492 " which has no equivalent\nin the `%s' character set."), 01493 c, c, target_charset (gdbarch)); 01494 return target_char; 01495 } 01496 01497 /* Print the character C on STREAM as part of the contents of a literal 01498 string whose delimiter is QUOTER. Note that this routine should only 01499 be call for printing things which are independent of the language 01500 of the program being debugged. */ 01501 01502 static void 01503 printchar (int c, void (*do_fputs) (const char *, struct ui_file *), 01504 void (*do_fprintf) (struct ui_file *, const char *, ...) 01505 ATTRIBUTE_FPTR_PRINTF_2, struct ui_file *stream, int quoter) 01506 { 01507 c &= 0xFF; /* Avoid sign bit follies */ 01508 01509 if (c < 0x20 || /* Low control chars */ 01510 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ 01511 (sevenbit_strings && c >= 0x80)) 01512 { /* high order bit set */ 01513 switch (c) 01514 { 01515 case '\n': 01516 do_fputs ("\\n", stream); 01517 break; 01518 case '\b': 01519 do_fputs ("\\b", stream); 01520 break; 01521 case '\t': 01522 do_fputs ("\\t", stream); 01523 break; 01524 case '\f': 01525 do_fputs ("\\f", stream); 01526 break; 01527 case '\r': 01528 do_fputs ("\\r", stream); 01529 break; 01530 case '\033': 01531 do_fputs ("\\e", stream); 01532 break; 01533 case '\007': 01534 do_fputs ("\\a", stream); 01535 break; 01536 default: 01537 do_fprintf (stream, "\\%.3o", (unsigned int) c); 01538 break; 01539 } 01540 } 01541 else 01542 { 01543 if (c == '\\' || c == quoter) 01544 do_fputs ("\\", stream); 01545 do_fprintf (stream, "%c", c); 01546 } 01547 } 01548 01549 /* Print the character C on STREAM as part of the contents of a 01550 literal string whose delimiter is QUOTER. Note that these routines 01551 should only be call for printing things which are independent of 01552 the language of the program being debugged. */ 01553 01554 void 01555 fputstr_filtered (const char *str, int quoter, struct ui_file *stream) 01556 { 01557 while (*str) 01558 printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter); 01559 } 01560 01561 void 01562 fputstr_unfiltered (const char *str, int quoter, struct ui_file *stream) 01563 { 01564 while (*str) 01565 printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter); 01566 } 01567 01568 void 01569 fputstrn_filtered (const char *str, int n, int quoter, 01570 struct ui_file *stream) 01571 { 01572 int i; 01573 01574 for (i = 0; i < n; i++) 01575 printchar (str[i], fputs_filtered, fprintf_filtered, stream, quoter); 01576 } 01577 01578 void 01579 fputstrn_unfiltered (const char *str, int n, int quoter, 01580 struct ui_file *stream) 01581 { 01582 int i; 01583 01584 for (i = 0; i < n; i++) 01585 printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter); 01586 } 01587 01588 01589 /* Number of lines per page or UINT_MAX if paging is disabled. */ 01590 static unsigned int lines_per_page; 01591 static void 01592 show_lines_per_page (struct ui_file *file, int from_tty, 01593 struct cmd_list_element *c, const char *value) 01594 { 01595 fprintf_filtered (file, 01596 _("Number of lines gdb thinks are in a page is %s.\n"), 01597 value); 01598 } 01599 01600 /* Number of chars per line or UINT_MAX if line folding is disabled. */ 01601 static unsigned int chars_per_line; 01602 static void 01603 show_chars_per_line (struct ui_file *file, int from_tty, 01604 struct cmd_list_element *c, const char *value) 01605 { 01606 fprintf_filtered (file, 01607 _("Number of characters gdb thinks " 01608 "are in a line is %s.\n"), 01609 value); 01610 } 01611 01612 /* Current count of lines printed on this page, chars on this line. */ 01613 static unsigned int lines_printed, chars_printed; 01614 01615 /* Buffer and start column of buffered text, for doing smarter word- 01616 wrapping. When someone calls wrap_here(), we start buffering output 01617 that comes through fputs_filtered(). If we see a newline, we just 01618 spit it out and forget about the wrap_here(). If we see another 01619 wrap_here(), we spit it out and remember the newer one. If we see 01620 the end of the line, we spit out a newline, the indent, and then 01621 the buffered output. */ 01622 01623 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which 01624 are waiting to be output (they have already been counted in chars_printed). 01625 When wrap_buffer[0] is null, the buffer is empty. */ 01626 static char *wrap_buffer; 01627 01628 /* Pointer in wrap_buffer to the next character to fill. */ 01629 static char *wrap_pointer; 01630 01631 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column 01632 is non-zero. */ 01633 static char *wrap_indent; 01634 01635 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping 01636 is not in effect. */ 01637 static int wrap_column; 01638 01639 01640 /* Inialize the number of lines per page and chars per line. */ 01641 01642 void 01643 init_page_info (void) 01644 { 01645 if (batch_flag) 01646 { 01647 lines_per_page = UINT_MAX; 01648 chars_per_line = UINT_MAX; 01649 } 01650 else 01651 #if defined(TUI) 01652 if (!tui_get_command_dimension (&chars_per_line, &lines_per_page)) 01653 #endif 01654 { 01655 int rows, cols; 01656 01657 #if defined(__GO32__) 01658 rows = ScreenRows (); 01659 cols = ScreenCols (); 01660 lines_per_page = rows; 01661 chars_per_line = cols; 01662 #else 01663 /* Make sure Readline has initialized its terminal settings. */ 01664 rl_reset_terminal (NULL); 01665 01666 /* Get the screen size from Readline. */ 01667 rl_get_screen_size (&rows, &cols); 01668 lines_per_page = rows; 01669 chars_per_line = cols; 01670 01671 /* Readline should have fetched the termcap entry for us. 01672 Only try to use tgetnum function if rl_get_screen_size 01673 did not return a useful value. */ 01674 if (((rows <= 0) && (tgetnum ("li") < 0)) 01675 /* Also disable paging if inside EMACS. */ 01676 || getenv ("EMACS")) 01677 { 01678 /* The number of lines per page is not mentioned in the terminal 01679 description or EMACS evironment variable is set. This probably 01680 means that paging is not useful, so disable paging. */ 01681 lines_per_page = UINT_MAX; 01682 } 01683 01684 /* If the output is not a terminal, don't paginate it. */ 01685 if (!ui_file_isatty (gdb_stdout)) 01686 lines_per_page = UINT_MAX; 01687 #endif 01688 } 01689 01690 set_screen_size (); 01691 set_width (); 01692 } 01693 01694 /* Helper for make_cleanup_restore_page_info. */ 01695 01696 static void 01697 do_restore_page_info_cleanup (void *arg) 01698 { 01699 set_screen_size (); 01700 set_width (); 01701 } 01702 01703 /* Provide cleanup for restoring the terminal size. */ 01704 01705 struct cleanup * 01706 make_cleanup_restore_page_info (void) 01707 { 01708 struct cleanup *back_to; 01709 01710 back_to = make_cleanup (do_restore_page_info_cleanup, NULL); 01711 make_cleanup_restore_uinteger (&lines_per_page); 01712 make_cleanup_restore_uinteger (&chars_per_line); 01713 01714 return back_to; 01715 } 01716 01717 /* Temporarily set BATCH_FLAG and the associated unlimited terminal size. 01718 Provide cleanup for restoring the original state. */ 01719 01720 struct cleanup * 01721 set_batch_flag_and_make_cleanup_restore_page_info (void) 01722 { 01723 struct cleanup *back_to = make_cleanup_restore_page_info (); 01724 01725 make_cleanup_restore_integer (&batch_flag); 01726 batch_flag = 1; 01727 init_page_info (); 01728 01729 return back_to; 01730 } 01731 01732 /* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE. */ 01733 01734 static void 01735 set_screen_size (void) 01736 { 01737 int rows = lines_per_page; 01738 int cols = chars_per_line; 01739 01740 if (rows <= 0) 01741 rows = INT_MAX; 01742 01743 if (cols <= 0) 01744 cols = INT_MAX; 01745 01746 /* Update Readline's idea of the terminal size. */ 01747 rl_set_screen_size (rows, cols); 01748 } 01749 01750 /* Reinitialize WRAP_BUFFER according to the current value of 01751 CHARS_PER_LINE. */ 01752 01753 static void 01754 set_width (void) 01755 { 01756 if (chars_per_line == 0) 01757 init_page_info (); 01758 01759 if (!wrap_buffer) 01760 { 01761 wrap_buffer = (char *) xmalloc (chars_per_line + 2); 01762 wrap_buffer[0] = '\0'; 01763 } 01764 else 01765 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); 01766 wrap_pointer = wrap_buffer; /* Start it at the beginning. */ 01767 } 01768 01769 static void 01770 set_width_command (char *args, int from_tty, struct cmd_list_element *c) 01771 { 01772 set_screen_size (); 01773 set_width (); 01774 } 01775 01776 static void 01777 set_height_command (char *args, int from_tty, struct cmd_list_element *c) 01778 { 01779 set_screen_size (); 01780 } 01781 01782 /* Wait, so the user can read what's on the screen. Prompt the user 01783 to continue by pressing RETURN. */ 01784 01785 static void 01786 prompt_for_continue (void) 01787 { 01788 char *ignore; 01789 char cont_prompt[120]; 01790 /* Used to add duration we waited for user to respond to 01791 prompt_for_continue_wait_time. */ 01792 struct timeval prompt_started, prompt_ended, prompt_delta; 01793 01794 gettimeofday (&prompt_started, NULL); 01795 01796 if (annotation_level > 1) 01797 printf_unfiltered (("\n\032\032pre-prompt-for-continue\n")); 01798 01799 strcpy (cont_prompt, 01800 "---Type <return> to continue, or q <return> to quit---"); 01801 if (annotation_level > 1) 01802 strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); 01803 01804 /* We must do this *before* we call gdb_readline, else it will eventually 01805 call us -- thinking that we're trying to print beyond the end of the 01806 screen. */ 01807 reinitialize_more_filter (); 01808 01809 immediate_quit++; 01810 QUIT; 01811 /* On a real operating system, the user can quit with SIGINT. 01812 But not on GO32. 01813 01814 'q' is provided on all systems so users don't have to change habits 01815 from system to system, and because telling them what to do in 01816 the prompt is more user-friendly than expecting them to think of 01817 SIGINT. */ 01818 /* Call readline, not gdb_readline, because GO32 readline handles control-C 01819 whereas control-C to gdb_readline will cause the user to get dumped 01820 out to DOS. */ 01821 ignore = gdb_readline_wrapper (cont_prompt); 01822 01823 /* Add time spend in this routine to prompt_for_continue_wait_time. */ 01824 gettimeofday (&prompt_ended, NULL); 01825 timeval_sub (&prompt_delta, &prompt_ended, &prompt_started); 01826 timeval_add (&prompt_for_continue_wait_time, 01827 &prompt_for_continue_wait_time, &prompt_delta); 01828 01829 if (annotation_level > 1) 01830 printf_unfiltered (("\n\032\032post-prompt-for-continue\n")); 01831 01832 if (ignore) 01833 { 01834 char *p = ignore; 01835 01836 while (*p == ' ' || *p == '\t') 01837 ++p; 01838 if (p[0] == 'q') 01839 quit (); 01840 xfree (ignore); 01841 } 01842 immediate_quit--; 01843 01844 /* Now we have to do this again, so that GDB will know that it doesn't 01845 need to save the ---Type <return>--- line at the top of the screen. */ 01846 reinitialize_more_filter (); 01847 01848 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ 01849 } 01850 01851 /* Initalize timer to keep track of how long we waited for the user. */ 01852 01853 void 01854 reset_prompt_for_continue_wait_time (void) 01855 { 01856 static const struct timeval zero_timeval = { 0 }; 01857 01858 prompt_for_continue_wait_time = zero_timeval; 01859 } 01860 01861 /* Fetch the cumulative time spent in prompt_for_continue. */ 01862 01863 struct timeval 01864 get_prompt_for_continue_wait_time (void) 01865 { 01866 return prompt_for_continue_wait_time; 01867 } 01868 01869 /* Reinitialize filter; ie. tell it to reset to original values. */ 01870 01871 void 01872 reinitialize_more_filter (void) 01873 { 01874 lines_printed = 0; 01875 chars_printed = 0; 01876 } 01877 01878 /* Indicate that if the next sequence of characters overflows the line, 01879 a newline should be inserted here rather than when it hits the end. 01880 If INDENT is non-null, it is a string to be printed to indent the 01881 wrapped part on the next line. INDENT must remain accessible until 01882 the next call to wrap_here() or until a newline is printed through 01883 fputs_filtered(). 01884 01885 If the line is already overfull, we immediately print a newline and 01886 the indentation, and disable further wrapping. 01887 01888 If we don't know the width of lines, but we know the page height, 01889 we must not wrap words, but should still keep track of newlines 01890 that were explicitly printed. 01891 01892 INDENT should not contain tabs, as that will mess up the char count 01893 on the next line. FIXME. 01894 01895 This routine is guaranteed to force out any output which has been 01896 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be 01897 used to force out output from the wrap_buffer. */ 01898 01899 void 01900 wrap_here (char *indent) 01901 { 01902 /* This should have been allocated, but be paranoid anyway. */ 01903 if (!wrap_buffer) 01904 internal_error (__FILE__, __LINE__, 01905 _("failed internal consistency check")); 01906 01907 if (wrap_buffer[0]) 01908 { 01909 *wrap_pointer = '\0'; 01910 fputs_unfiltered (wrap_buffer, gdb_stdout); 01911 } 01912 wrap_pointer = wrap_buffer; 01913 wrap_buffer[0] = '\0'; 01914 if (chars_per_line == UINT_MAX) /* No line overflow checking. */ 01915 { 01916 wrap_column = 0; 01917 } 01918 else if (chars_printed >= chars_per_line) 01919 { 01920 puts_filtered ("\n"); 01921 if (indent != NULL) 01922 puts_filtered (indent); 01923 wrap_column = 0; 01924 } 01925 else 01926 { 01927 wrap_column = chars_printed; 01928 if (indent == NULL) 01929 wrap_indent = ""; 01930 else 01931 wrap_indent = indent; 01932 } 01933 } 01934 01935 void 01936 puts_filtered_tabular (char *string, int width, int right) 01937 { 01938 int spaces = 0; 01939 int stringlen; 01940 char *spacebuf; 01941 01942 gdb_assert (chars_per_line > 0); 01943 if (chars_per_line == UINT_MAX) 01944 { 01945 fputs_filtered (string, gdb_stdout); 01946 fputs_filtered ("\n", gdb_stdout); 01947 return; 01948 } 01949 01950 if (((chars_printed - 1) / width + 2) * width >= chars_per_line) 01951 fputs_filtered ("\n", gdb_stdout); 01952 01953 if (width >= chars_per_line) 01954 width = chars_per_line - 1; 01955 01956 stringlen = strlen (string); 01957 01958 if (chars_printed > 0) 01959 spaces = width - (chars_printed - 1) % width - 1; 01960 if (right) 01961 spaces += width - stringlen; 01962 01963 spacebuf = alloca (spaces + 1); 01964 spacebuf[spaces] = '\0'; 01965 while (spaces--) 01966 spacebuf[spaces] = ' '; 01967 01968 fputs_filtered (spacebuf, gdb_stdout); 01969 fputs_filtered (string, gdb_stdout); 01970 } 01971 01972 01973 /* Ensure that whatever gets printed next, using the filtered output 01974 commands, starts at the beginning of the line. I.e. if there is 01975 any pending output for the current line, flush it and start a new 01976 line. Otherwise do nothing. */ 01977 01978 void 01979 begin_line (void) 01980 { 01981 if (chars_printed > 0) 01982 { 01983 puts_filtered ("\n"); 01984 } 01985 } 01986 01987 01988 /* Like fputs but if FILTER is true, pause after every screenful. 01989 01990 Regardless of FILTER can wrap at points other than the final 01991 character of a line. 01992 01993 Unlike fputs, fputs_maybe_filtered does not return a value. 01994 It is OK for LINEBUFFER to be NULL, in which case just don't print 01995 anything. 01996 01997 Note that a longjmp to top level may occur in this routine (only if 01998 FILTER is true) (since prompt_for_continue may do so) so this 01999 routine should not be called when cleanups are not in place. */ 02000 02001 static void 02002 fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, 02003 int filter) 02004 { 02005 const char *lineptr; 02006 02007 if (linebuffer == 0) 02008 return; 02009 02010 /* Don't do any filtering if it is disabled. */ 02011 if (stream != gdb_stdout 02012 || !pagination_enabled 02013 || batch_flag 02014 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX) 02015 || top_level_interpreter () == NULL 02016 || ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))) 02017 { 02018 fputs_unfiltered (linebuffer, stream); 02019 return; 02020 } 02021 02022 /* Go through and output each character. Show line extension 02023 when this is necessary; prompt user for new page when this is 02024 necessary. */ 02025 02026 lineptr = linebuffer; 02027 while (*lineptr) 02028 { 02029 /* Possible new page. */ 02030 if (filter && (lines_printed >= lines_per_page - 1)) 02031 prompt_for_continue (); 02032 02033 while (*lineptr && *lineptr != '\n') 02034 { 02035 /* Print a single line. */ 02036 if (*lineptr == '\t') 02037 { 02038 if (wrap_column) 02039 *wrap_pointer++ = '\t'; 02040 else 02041 fputc_unfiltered ('\t', stream); 02042 /* Shifting right by 3 produces the number of tab stops 02043 we have already passed, and then adding one and 02044 shifting left 3 advances to the next tab stop. */ 02045 chars_printed = ((chars_printed >> 3) + 1) << 3; 02046 lineptr++; 02047 } 02048 else 02049 { 02050 if (wrap_column) 02051 *wrap_pointer++ = *lineptr; 02052 else 02053 fputc_unfiltered (*lineptr, stream); 02054 chars_printed++; 02055 lineptr++; 02056 } 02057 02058 if (chars_printed >= chars_per_line) 02059 { 02060 unsigned int save_chars = chars_printed; 02061 02062 chars_printed = 0; 02063 lines_printed++; 02064 /* If we aren't actually wrapping, don't output newline -- 02065 if chars_per_line is right, we probably just overflowed 02066 anyway; if it's wrong, let us keep going. */ 02067 if (wrap_column) 02068 fputc_unfiltered ('\n', stream); 02069 02070 /* Possible new page. */ 02071 if (lines_printed >= lines_per_page - 1) 02072 prompt_for_continue (); 02073 02074 /* Now output indentation and wrapped string. */ 02075 if (wrap_column) 02076 { 02077 fputs_unfiltered (wrap_indent, stream); 02078 *wrap_pointer = '\0'; /* Null-terminate saved stuff, */ 02079 fputs_unfiltered (wrap_buffer, stream); /* and eject it. */ 02080 /* FIXME, this strlen is what prevents wrap_indent from 02081 containing tabs. However, if we recurse to print it 02082 and count its chars, we risk trouble if wrap_indent is 02083 longer than (the user settable) chars_per_line. 02084 Note also that this can set chars_printed > chars_per_line 02085 if we are printing a long string. */ 02086 chars_printed = strlen (wrap_indent) 02087 + (save_chars - wrap_column); 02088 wrap_pointer = wrap_buffer; /* Reset buffer */ 02089 wrap_buffer[0] = '\0'; 02090 wrap_column = 0; /* And disable fancy wrap */ 02091 } 02092 } 02093 } 02094 02095 if (*lineptr == '\n') 02096 { 02097 chars_printed = 0; 02098 wrap_here ((char *) 0); /* Spit out chars, cancel 02099 further wraps. */ 02100 lines_printed++; 02101 fputc_unfiltered ('\n', stream); 02102 lineptr++; 02103 } 02104 } 02105 } 02106 02107 void 02108 fputs_filtered (const char *linebuffer, struct ui_file *stream) 02109 { 02110 fputs_maybe_filtered (linebuffer, stream, 1); 02111 } 02112 02113 int 02114 putchar_unfiltered (int c) 02115 { 02116 char buf = c; 02117 02118 ui_file_write (gdb_stdout, &buf, 1); 02119 return c; 02120 } 02121 02122 /* Write character C to gdb_stdout using GDB's paging mechanism and return C. 02123 May return nonlocally. */ 02124 02125 int 02126 putchar_filtered (int c) 02127 { 02128 return fputc_filtered (c, gdb_stdout); 02129 } 02130 02131 int 02132 fputc_unfiltered (int c, struct ui_file *stream) 02133 { 02134 char buf = c; 02135 02136 ui_file_write (stream, &buf, 1); 02137 return c; 02138 } 02139 02140 int 02141 fputc_filtered (int c, struct ui_file *stream) 02142 { 02143 char buf[2]; 02144 02145 buf[0] = c; 02146 buf[1] = 0; 02147 fputs_filtered (buf, stream); 02148 return c; 02149 } 02150 02151 /* puts_debug is like fputs_unfiltered, except it prints special 02152 characters in printable fashion. */ 02153 02154 void 02155 puts_debug (char *prefix, char *string, char *suffix) 02156 { 02157 int ch; 02158 02159 /* Print prefix and suffix after each line. */ 02160 static int new_line = 1; 02161 static int return_p = 0; 02162 static char *prev_prefix = ""; 02163 static char *prev_suffix = ""; 02164 02165 if (*string == '\n') 02166 return_p = 0; 02167 02168 /* If the prefix is changing, print the previous suffix, a new line, 02169 and the new prefix. */ 02170 if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line) 02171 { 02172 fputs_unfiltered (prev_suffix, gdb_stdlog); 02173 fputs_unfiltered ("\n", gdb_stdlog); 02174 fputs_unfiltered (prefix, gdb_stdlog); 02175 } 02176 02177 /* Print prefix if we printed a newline during the previous call. */ 02178 if (new_line) 02179 { 02180 new_line = 0; 02181 fputs_unfiltered (prefix, gdb_stdlog); 02182 } 02183 02184 prev_prefix = prefix; 02185 prev_suffix = suffix; 02186 02187 /* Output characters in a printable format. */ 02188 while ((ch = *string++) != '\0') 02189 { 02190 switch (ch) 02191 { 02192 default: 02193 if (isprint (ch)) 02194 fputc_unfiltered (ch, gdb_stdlog); 02195 02196 else 02197 fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff); 02198 break; 02199 02200 case '\\': 02201 fputs_unfiltered ("\\\\", gdb_stdlog); 02202 break; 02203 case '\b': 02204 fputs_unfiltered ("\\b", gdb_stdlog); 02205 break; 02206 case '\f': 02207 fputs_unfiltered ("\\f", gdb_stdlog); 02208 break; 02209 case '\n': 02210 new_line = 1; 02211 fputs_unfiltered ("\\n", gdb_stdlog); 02212 break; 02213 case '\r': 02214 fputs_unfiltered ("\\r", gdb_stdlog); 02215 break; 02216 case '\t': 02217 fputs_unfiltered ("\\t", gdb_stdlog); 02218 break; 02219 case '\v': 02220 fputs_unfiltered ("\\v", gdb_stdlog); 02221 break; 02222 } 02223 02224 return_p = ch == '\r'; 02225 } 02226 02227 /* Print suffix if we printed a newline. */ 02228 if (new_line) 02229 { 02230 fputs_unfiltered (suffix, gdb_stdlog); 02231 fputs_unfiltered ("\n", gdb_stdlog); 02232 } 02233 } 02234 02235 02236 /* Print a variable number of ARGS using format FORMAT. If this 02237 information is going to put the amount written (since the last call 02238 to REINITIALIZE_MORE_FILTER or the last page break) over the page size, 02239 call prompt_for_continue to get the users permision to continue. 02240 02241 Unlike fprintf, this function does not return a value. 02242 02243 We implement three variants, vfprintf (takes a vararg list and stream), 02244 fprintf (takes a stream to write on), and printf (the usual). 02245 02246 Note also that a longjmp to top level may occur in this routine 02247 (since prompt_for_continue may do so) so this routine should not be 02248 called when cleanups are not in place. */ 02249 02250 static void 02251 vfprintf_maybe_filtered (struct ui_file *stream, const char *format, 02252 va_list args, int filter) 02253 { 02254 char *linebuffer; 02255 struct cleanup *old_cleanups; 02256 02257 linebuffer = xstrvprintf (format, args); 02258 old_cleanups = make_cleanup (xfree, linebuffer); 02259 fputs_maybe_filtered (linebuffer, stream, filter); 02260 do_cleanups (old_cleanups); 02261 } 02262 02263 02264 void 02265 vfprintf_filtered (struct ui_file *stream, const char *format, va_list args) 02266 { 02267 vfprintf_maybe_filtered (stream, format, args, 1); 02268 } 02269 02270 void 02271 vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) 02272 { 02273 char *linebuffer; 02274 struct cleanup *old_cleanups; 02275 02276 linebuffer = xstrvprintf (format, args); 02277 old_cleanups = make_cleanup (xfree, linebuffer); 02278 if (debug_timestamp && stream == gdb_stdlog) 02279 { 02280 struct timeval tm; 02281 char *timestamp; 02282 int len, need_nl; 02283 02284 gettimeofday (&tm, NULL); 02285 02286 len = strlen (linebuffer); 02287 need_nl = (len > 0 && linebuffer[len - 1] != '\n'); 02288 02289 timestamp = xstrprintf ("%ld:%ld %s%s", 02290 (long) tm.tv_sec, (long) tm.tv_usec, 02291 linebuffer, 02292 need_nl ? "\n": ""); 02293 make_cleanup (xfree, timestamp); 02294 fputs_unfiltered (timestamp, stream); 02295 } 02296 else 02297 fputs_unfiltered (linebuffer, stream); 02298 do_cleanups (old_cleanups); 02299 } 02300 02301 void 02302 vprintf_filtered (const char *format, va_list args) 02303 { 02304 vfprintf_maybe_filtered (gdb_stdout, format, args, 1); 02305 } 02306 02307 void 02308 vprintf_unfiltered (const char *format, va_list args) 02309 { 02310 vfprintf_unfiltered (gdb_stdout, format, args); 02311 } 02312 02313 void 02314 fprintf_filtered (struct ui_file *stream, const char *format, ...) 02315 { 02316 va_list args; 02317 02318 va_start (args, format); 02319 vfprintf_filtered (stream, format, args); 02320 va_end (args); 02321 } 02322 02323 void 02324 fprintf_unfiltered (struct ui_file *stream, const char *format, ...) 02325 { 02326 va_list args; 02327 02328 va_start (args, format); 02329 vfprintf_unfiltered (stream, format, args); 02330 va_end (args); 02331 } 02332 02333 /* Like fprintf_filtered, but prints its result indented. 02334 Called as fprintfi_filtered (spaces, stream, format, ...); */ 02335 02336 void 02337 fprintfi_filtered (int spaces, struct ui_file *stream, const char *format, 02338 ...) 02339 { 02340 va_list args; 02341 02342 va_start (args, format); 02343 print_spaces_filtered (spaces, stream); 02344 02345 vfprintf_filtered (stream, format, args); 02346 va_end (args); 02347 } 02348 02349 02350 void 02351 printf_filtered (const char *format, ...) 02352 { 02353 va_list args; 02354 02355 va_start (args, format); 02356 vfprintf_filtered (gdb_stdout, format, args); 02357 va_end (args); 02358 } 02359 02360 02361 void 02362 printf_unfiltered (const char *format, ...) 02363 { 02364 va_list args; 02365 02366 va_start (args, format); 02367 vfprintf_unfiltered (gdb_stdout, format, args); 02368 va_end (args); 02369 } 02370 02371 /* Like printf_filtered, but prints it's result indented. 02372 Called as printfi_filtered (spaces, format, ...); */ 02373 02374 void 02375 printfi_filtered (int spaces, const char *format, ...) 02376 { 02377 va_list args; 02378 02379 va_start (args, format); 02380 print_spaces_filtered (spaces, gdb_stdout); 02381 vfprintf_filtered (gdb_stdout, format, args); 02382 va_end (args); 02383 } 02384 02385 /* Easy -- but watch out! 02386 02387 This routine is *not* a replacement for puts()! puts() appends a newline. 02388 This one doesn't, and had better not! */ 02389 02390 void 02391 puts_filtered (const char *string) 02392 { 02393 fputs_filtered (string, gdb_stdout); 02394 } 02395 02396 void 02397 puts_unfiltered (const char *string) 02398 { 02399 fputs_unfiltered (string, gdb_stdout); 02400 } 02401 02402 /* Return a pointer to N spaces and a null. The pointer is good 02403 until the next call to here. */ 02404 char * 02405 n_spaces (int n) 02406 { 02407 char *t; 02408 static char *spaces = 0; 02409 static int max_spaces = -1; 02410 02411 if (n > max_spaces) 02412 { 02413 if (spaces) 02414 xfree (spaces); 02415 spaces = (char *) xmalloc (n + 1); 02416 for (t = spaces + n; t != spaces;) 02417 *--t = ' '; 02418 spaces[n] = '\0'; 02419 max_spaces = n; 02420 } 02421 02422 return spaces + max_spaces - n; 02423 } 02424 02425 /* Print N spaces. */ 02426 void 02427 print_spaces_filtered (int n, struct ui_file *stream) 02428 { 02429 fputs_filtered (n_spaces (n), stream); 02430 } 02431 02432 /* C++/ObjC demangler stuff. */ 02433 02434 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language 02435 LANG, using demangling args ARG_MODE, and print it filtered to STREAM. 02436 If the name is not mangled, or the language for the name is unknown, or 02437 demangling is off, the name is printed in its "raw" form. */ 02438 02439 void 02440 fprintf_symbol_filtered (struct ui_file *stream, const char *name, 02441 enum language lang, int arg_mode) 02442 { 02443 char *demangled; 02444 02445 if (name != NULL) 02446 { 02447 /* If user wants to see raw output, no problem. */ 02448 if (!demangle) 02449 { 02450 fputs_filtered (name, stream); 02451 } 02452 else 02453 { 02454 demangled = language_demangle (language_def (lang), name, arg_mode); 02455 fputs_filtered (demangled ? demangled : name, stream); 02456 if (demangled != NULL) 02457 { 02458 xfree (demangled); 02459 } 02460 } 02461 } 02462 } 02463 02464 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any 02465 differences in whitespace. Returns 0 if they match, non-zero if they 02466 don't (slightly different than strcmp()'s range of return values). 02467 02468 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". 02469 This "feature" is useful when searching for matching C++ function names 02470 (such as if the user types 'break FOO', where FOO is a mangled C++ 02471 function). */ 02472 02473 int 02474 strcmp_iw (const char *string1, const char *string2) 02475 { 02476 while ((*string1 != '\0') && (*string2 != '\0')) 02477 { 02478 while (isspace (*string1)) 02479 { 02480 string1++; 02481 } 02482 while (isspace (*string2)) 02483 { 02484 string2++; 02485 } 02486 if (case_sensitivity == case_sensitive_on && *string1 != *string2) 02487 break; 02488 if (case_sensitivity == case_sensitive_off 02489 && (tolower ((unsigned char) *string1) 02490 != tolower ((unsigned char) *string2))) 02491 break; 02492 if (*string1 != '\0') 02493 { 02494 string1++; 02495 string2++; 02496 } 02497 } 02498 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); 02499 } 02500 02501 int 02502 strcmp_iw_ordered (const char *string1, const char *string2) 02503 { 02504 const char *saved_string1 = string1, *saved_string2 = string2; 02505 enum case_sensitivity case_pass = case_sensitive_off; 02506 02507 for (;;) 02508 { 02509 /* C1 and C2 are valid only if *string1 != '\0' && *string2 != '\0'. 02510 Provide stub characters if we are already at the end of one of the 02511 strings. */ 02512 char c1 = 'X', c2 = 'X'; 02513 02514 while (*string1 != '\0' && *string2 != '\0') 02515 { 02516 while (isspace (*string1)) 02517 string1++; 02518 while (isspace (*string2)) 02519 string2++; 02520 02521 switch (case_pass) 02522 { 02523 case case_sensitive_off: 02524 c1 = tolower ((unsigned char) *string1); 02525 c2 = tolower ((unsigned char) *string2); 02526 break; 02527 case case_sensitive_on: 02528 c1 = *string1; 02529 c2 = *string2; 02530 break; 02531 } 02532 if (c1 != c2) 02533 break; 02534 02535 if (*string1 != '\0') 02536 { 02537 string1++; 02538 string2++; 02539 } 02540 } 02541 02542 switch (*string1) 02543 { 02544 /* Characters are non-equal unless they're both '\0'; we want to 02545 make sure we get the comparison right according to our 02546 comparison in the cases where one of them is '\0' or '('. */ 02547 case '\0': 02548 if (*string2 == '\0') 02549 break; 02550 else 02551 return -1; 02552 case '(': 02553 if (*string2 == '\0') 02554 return 1; 02555 else 02556 return -1; 02557 default: 02558 if (*string2 == '\0' || *string2 == '(') 02559 return 1; 02560 else if (c1 > c2) 02561 return 1; 02562 else if (c1 < c2) 02563 return -1; 02564 /* PASSTHRU */ 02565 } 02566 02567 if (case_pass == case_sensitive_on) 02568 return 0; 02569 02570 /* Otherwise the strings were equal in case insensitive way, make 02571 a more fine grained comparison in a case sensitive way. */ 02572 02573 case_pass = case_sensitive_on; 02574 string1 = saved_string1; 02575 string2 = saved_string2; 02576 } 02577 } 02578 02579 int 02580 streq (const char *lhs, const char *rhs) 02581 { 02582 return !strcmp (lhs, rhs); 02583 } 02584 02585 02586 int 02587 subset_compare (char *string_to_compare, char *template_string) 02588 { 02589 int match; 02590 02591 if (template_string != (char *) NULL && string_to_compare != (char *) NULL 02592 && strlen (string_to_compare) <= strlen (template_string)) 02593 match = 02594 (strncmp 02595 (template_string, string_to_compare, strlen (string_to_compare)) == 0); 02596 else 02597 match = 0; 02598 return match; 02599 } 02600 02601 static void 02602 pagination_on_command (char *arg, int from_tty) 02603 { 02604 pagination_enabled = 1; 02605 } 02606 02607 static void 02608 pagination_off_command (char *arg, int from_tty) 02609 { 02610 pagination_enabled = 0; 02611 } 02612 02613 static void 02614 show_debug_timestamp (struct ui_file *file, int from_tty, 02615 struct cmd_list_element *c, const char *value) 02616 { 02617 fprintf_filtered (file, _("Timestamping debugging messages is %s.\n"), 02618 value); 02619 } 02620 02621 02622 void 02623 initialize_utils (void) 02624 { 02625 add_setshow_uinteger_cmd ("width", class_support, &chars_per_line, _("\ 02626 Set number of characters where GDB should wrap lines of its output."), _("\ 02627 Show number of characters where GDB should wrap lines of its output."), _("\ 02628 This affects where GDB wraps its output to fit the screen width.\n\ 02629 Setting this to \"unlimited\" or zero prevents GDB from wrapping its output."), 02630 set_width_command, 02631 show_chars_per_line, 02632 &setlist, &showlist); 02633 02634 add_setshow_uinteger_cmd ("height", class_support, &lines_per_page, _("\ 02635 Set number of lines in a page for GDB output pagination."), _("\ 02636 Show number of lines in a page for GDB output pagination."), _("\ 02637 This affects the number of lines after which GDB will pause\n\ 02638 its output and ask you whether to continue.\n\ 02639 Setting this to \"unlimited\" or zero causes GDB never pause during output."), 02640 set_height_command, 02641 show_lines_per_page, 02642 &setlist, &showlist); 02643 02644 init_page_info (); 02645 02646 add_setshow_boolean_cmd ("pagination", class_support, 02647 &pagination_enabled, _("\ 02648 Set state of GDB output pagination."), _("\ 02649 Show state of GDB output pagination."), _("\ 02650 When pagination is ON, GDB pauses at end of each screenful of\n\ 02651 its output and asks you whether to continue.\n\ 02652 Turning pagination off is an alternative to \"set height unlimited\"."), 02653 NULL, 02654 show_pagination_enabled, 02655 &setlist, &showlist); 02656 02657 if (xdb_commands) 02658 { 02659 add_com ("am", class_support, pagination_on_command, 02660 _("Enable pagination")); 02661 add_com ("sm", class_support, pagination_off_command, 02662 _("Disable pagination")); 02663 } 02664 02665 add_setshow_boolean_cmd ("sevenbit-strings", class_support, 02666 &sevenbit_strings, _("\ 02667 Set printing of 8-bit characters in strings as \\nnn."), _("\ 02668 Show printing of 8-bit characters in strings as \\nnn."), NULL, 02669 NULL, 02670 show_sevenbit_strings, 02671 &setprintlist, &showprintlist); 02672 02673 add_setshow_boolean_cmd ("timestamp", class_maintenance, 02674 &debug_timestamp, _("\ 02675 Set timestamping of debugging messages."), _("\ 02676 Show timestamping of debugging messages."), _("\ 02677 When set, debugging messages will be marked with seconds and microseconds."), 02678 NULL, 02679 show_debug_timestamp, 02680 &setdebuglist, &showdebuglist); 02681 } 02682 02683 /* Print routines to handle variable size regs, etc. */ 02684 /* Temporary storage using circular buffer. */ 02685 #define NUMCELLS 16 02686 #define CELLSIZE 50 02687 static char * 02688 get_cell (void) 02689 { 02690 static char buf[NUMCELLS][CELLSIZE]; 02691 static int cell = 0; 02692 02693 if (++cell >= NUMCELLS) 02694 cell = 0; 02695 return buf[cell]; 02696 } 02697 02698 const char * 02699 paddress (struct gdbarch *gdbarch, CORE_ADDR addr) 02700 { 02701 /* Truncate address to the size of a target address, avoiding shifts 02702 larger or equal than the width of a CORE_ADDR. The local 02703 variable ADDR_BIT stops the compiler reporting a shift overflow 02704 when it won't occur. */ 02705 /* NOTE: This assumes that the significant address information is 02706 kept in the least significant bits of ADDR - the upper bits were 02707 either zero or sign extended. Should gdbarch_address_to_pointer or 02708 some ADDRESS_TO_PRINTABLE() be used to do the conversion? */ 02709 02710 int addr_bit = gdbarch_addr_bit (gdbarch); 02711 02712 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) 02713 addr &= ((CORE_ADDR) 1 << addr_bit) - 1; 02714 return hex_string (addr); 02715 } 02716 02717 /* This function is described in "defs.h". */ 02718 02719 const char * 02720 print_core_address (struct gdbarch *gdbarch, CORE_ADDR address) 02721 { 02722 int addr_bit = gdbarch_addr_bit (gdbarch); 02723 02724 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) 02725 address &= ((CORE_ADDR) 1 << addr_bit) - 1; 02726 02727 /* FIXME: cagney/2002-05-03: Need local_address_string() function 02728 that returns the language localized string formatted to a width 02729 based on gdbarch_addr_bit. */ 02730 if (addr_bit <= 32) 02731 return hex_string_custom (address, 8); 02732 else 02733 return hex_string_custom (address, 16); 02734 } 02735 02736 /* Callback hash_f for htab_create_alloc or htab_create_alloc_ex. */ 02737 02738 hashval_t 02739 core_addr_hash (const void *ap) 02740 { 02741 const CORE_ADDR *addrp = ap; 02742 02743 return *addrp; 02744 } 02745 02746 /* Callback eq_f for htab_create_alloc or htab_create_alloc_ex. */ 02747 02748 int 02749 core_addr_eq (const void *ap, const void *bp) 02750 { 02751 const CORE_ADDR *addr_ap = ap; 02752 const CORE_ADDR *addr_bp = bp; 02753 02754 return *addr_ap == *addr_bp; 02755 } 02756 02757 static char * 02758 decimal2str (char *sign, ULONGEST addr, int width) 02759 { 02760 /* Steal code from valprint.c:print_decimal(). Should this worry 02761 about the real size of addr as the above does? */ 02762 unsigned long temp[3]; 02763 char *str = get_cell (); 02764 int i = 0; 02765 02766 do 02767 { 02768 temp[i] = addr % (1000 * 1000 * 1000); 02769 addr /= (1000 * 1000 * 1000); 02770 i++; 02771 width -= 9; 02772 } 02773 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); 02774 02775 width += 9; 02776 if (width < 0) 02777 width = 0; 02778 02779 switch (i) 02780 { 02781 case 1: 02782 xsnprintf (str, CELLSIZE, "%s%0*lu", sign, width, temp[0]); 02783 break; 02784 case 2: 02785 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu", sign, width, 02786 temp[1], temp[0]); 02787 break; 02788 case 3: 02789 xsnprintf (str, CELLSIZE, "%s%0*lu%09lu%09lu", sign, width, 02790 temp[2], temp[1], temp[0]); 02791 break; 02792 default: 02793 internal_error (__FILE__, __LINE__, 02794 _("failed internal consistency check")); 02795 } 02796 02797 return str; 02798 } 02799 02800 static char * 02801 octal2str (ULONGEST addr, int width) 02802 { 02803 unsigned long temp[3]; 02804 char *str = get_cell (); 02805 int i = 0; 02806 02807 do 02808 { 02809 temp[i] = addr % (0100000 * 0100000); 02810 addr /= (0100000 * 0100000); 02811 i++; 02812 width -= 10; 02813 } 02814 while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); 02815 02816 width += 10; 02817 if (width < 0) 02818 width = 0; 02819 02820 switch (i) 02821 { 02822 case 1: 02823 if (temp[0] == 0) 02824 xsnprintf (str, CELLSIZE, "%*o", width, 0); 02825 else 02826 xsnprintf (str, CELLSIZE, "0%0*lo", width, temp[0]); 02827 break; 02828 case 2: 02829 xsnprintf (str, CELLSIZE, "0%0*lo%010lo", width, temp[1], temp[0]); 02830 break; 02831 case 3: 02832 xsnprintf (str, CELLSIZE, "0%0*lo%010lo%010lo", width, 02833 temp[2], temp[1], temp[0]); 02834 break; 02835 default: 02836 internal_error (__FILE__, __LINE__, 02837 _("failed internal consistency check")); 02838 } 02839 02840 return str; 02841 } 02842 02843 char * 02844 pulongest (ULONGEST u) 02845 { 02846 return decimal2str ("", u, 0); 02847 } 02848 02849 char * 02850 plongest (LONGEST l) 02851 { 02852 if (l < 0) 02853 return decimal2str ("-", -l, 0); 02854 else 02855 return decimal2str ("", l, 0); 02856 } 02857 02858 /* Eliminate warning from compiler on 32-bit systems. */ 02859 static int thirty_two = 32; 02860 02861 char * 02862 phex (ULONGEST l, int sizeof_l) 02863 { 02864 char *str; 02865 02866 switch (sizeof_l) 02867 { 02868 case 8: 02869 str = get_cell (); 02870 xsnprintf (str, CELLSIZE, "%08lx%08lx", 02871 (unsigned long) (l >> thirty_two), 02872 (unsigned long) (l & 0xffffffff)); 02873 break; 02874 case 4: 02875 str = get_cell (); 02876 xsnprintf (str, CELLSIZE, "%08lx", (unsigned long) l); 02877 break; 02878 case 2: 02879 str = get_cell (); 02880 xsnprintf (str, CELLSIZE, "%04x", (unsigned short) (l & 0xffff)); 02881 break; 02882 default: 02883 str = phex (l, sizeof (l)); 02884 break; 02885 } 02886 02887 return str; 02888 } 02889 02890 char * 02891 phex_nz (ULONGEST l, int sizeof_l) 02892 { 02893 char *str; 02894 02895 switch (sizeof_l) 02896 { 02897 case 8: 02898 { 02899 unsigned long high = (unsigned long) (l >> thirty_two); 02900 02901 str = get_cell (); 02902 if (high == 0) 02903 xsnprintf (str, CELLSIZE, "%lx", 02904 (unsigned long) (l & 0xffffffff)); 02905 else 02906 xsnprintf (str, CELLSIZE, "%lx%08lx", high, 02907 (unsigned long) (l & 0xffffffff)); 02908 break; 02909 } 02910 case 4: 02911 str = get_cell (); 02912 xsnprintf (str, CELLSIZE, "%lx", (unsigned long) l); 02913 break; 02914 case 2: 02915 str = get_cell (); 02916 xsnprintf (str, CELLSIZE, "%x", (unsigned short) (l & 0xffff)); 02917 break; 02918 default: 02919 str = phex_nz (l, sizeof (l)); 02920 break; 02921 } 02922 02923 return str; 02924 } 02925 02926 /* Converts a LONGEST to a C-format hexadecimal literal and stores it 02927 in a static string. Returns a pointer to this string. */ 02928 char * 02929 hex_string (LONGEST num) 02930 { 02931 char *result = get_cell (); 02932 02933 xsnprintf (result, CELLSIZE, "0x%s", phex_nz (num, sizeof (num))); 02934 return result; 02935 } 02936 02937 /* Converts a LONGEST number to a C-format hexadecimal literal and 02938 stores it in a static string. Returns a pointer to this string 02939 that is valid until the next call. The number is padded on the 02940 left with 0s to at least WIDTH characters. */ 02941 char * 02942 hex_string_custom (LONGEST num, int width) 02943 { 02944 char *result = get_cell (); 02945 char *result_end = result + CELLSIZE - 1; 02946 const char *hex = phex_nz (num, sizeof (num)); 02947 int hex_len = strlen (hex); 02948 02949 if (hex_len > width) 02950 width = hex_len; 02951 if (width + 2 >= CELLSIZE) 02952 internal_error (__FILE__, __LINE__, _("\ 02953 hex_string_custom: insufficient space to store result")); 02954 02955 strcpy (result_end - width - 2, "0x"); 02956 memset (result_end - width, '0', width); 02957 strcpy (result_end - hex_len, hex); 02958 return result_end - width - 2; 02959 } 02960 02961 char * 02962 int_string (LONGEST val, int radix, int is_signed, int width, 02963 int use_c_format) 02964 { 02965 switch (radix) 02966 { 02967 case 16: 02968 { 02969 char *result; 02970 02971 if (width == 0) 02972 result = hex_string (val); 02973 else 02974 result = hex_string_custom (val, width); 02975 if (! use_c_format) 02976 result += 2; 02977 return result; 02978 } 02979 case 10: 02980 { 02981 if (is_signed && val < 0) 02982 return decimal2str ("-", -val, width); 02983 else 02984 return decimal2str ("", val, width); 02985 } 02986 case 8: 02987 { 02988 char *result = octal2str (val, width); 02989 02990 if (use_c_format || val == 0) 02991 return result; 02992 else 02993 return result + 1; 02994 } 02995 default: 02996 internal_error (__FILE__, __LINE__, 02997 _("failed internal consistency check")); 02998 } 02999 } 03000 03001 const char * 03002 core_addr_to_string (const CORE_ADDR addr) 03003 { 03004 char *str = get_cell (); 03005 03006 strcpy (str, "0x"); 03007 strcat (str, phex (addr, sizeof (addr))); 03008 return str; 03009 } 03010 03011 const char * 03012 core_addr_to_string_nz (const CORE_ADDR addr) 03013 { 03014 char *str = get_cell (); 03015 03016 strcpy (str, "0x"); 03017 strcat (str, phex_nz (addr, sizeof (addr))); 03018 return str; 03019 } 03020 03021 /* Convert a string back into a CORE_ADDR. */ 03022 CORE_ADDR 03023 string_to_core_addr (const char *my_string) 03024 { 03025 CORE_ADDR addr = 0; 03026 03027 if (my_string[0] == '0' && tolower (my_string[1]) == 'x') 03028 { 03029 /* Assume that it is in hex. */ 03030 int i; 03031 03032 for (i = 2; my_string[i] != '\0'; i++) 03033 { 03034 if (isdigit (my_string[i])) 03035 addr = (my_string[i] - '0') + (addr * 16); 03036 else if (isxdigit (my_string[i])) 03037 addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16); 03038 else 03039 error (_("invalid hex \"%s\""), my_string); 03040 } 03041 } 03042 else 03043 { 03044 /* Assume that it is in decimal. */ 03045 int i; 03046 03047 for (i = 0; my_string[i] != '\0'; i++) 03048 { 03049 if (isdigit (my_string[i])) 03050 addr = (my_string[i] - '0') + (addr * 10); 03051 else 03052 error (_("invalid decimal \"%s\""), my_string); 03053 } 03054 } 03055 03056 return addr; 03057 } 03058 03059 const char * 03060 host_address_to_string (const void *addr) 03061 { 03062 char *str = get_cell (); 03063 03064 xsnprintf (str, CELLSIZE, "0x%s", phex_nz ((uintptr_t) addr, sizeof (addr))); 03065 return str; 03066 } 03067 03068 char * 03069 gdb_realpath (const char *filename) 03070 { 03071 /* Method 1: The system has a compile time upper bound on a filename 03072 path. Use that and realpath() to canonicalize the name. This is 03073 the most common case. Note that, if there isn't a compile time 03074 upper bound, you want to avoid realpath() at all costs. */ 03075 #if defined (HAVE_REALPATH) && defined (PATH_MAX) 03076 { 03077 char buf[PATH_MAX]; 03078 const char *rp = realpath (filename, buf); 03079 03080 if (rp == NULL) 03081 rp = filename; 03082 return xstrdup (rp); 03083 } 03084 #endif /* HAVE_REALPATH */ 03085 03086 /* Method 2: The host system (i.e., GNU) has the function 03087 canonicalize_file_name() which malloc's a chunk of memory and 03088 returns that, use that. */ 03089 #if defined(HAVE_CANONICALIZE_FILE_NAME) 03090 { 03091 char *rp = canonicalize_file_name (filename); 03092 03093 if (rp == NULL) 03094 return xstrdup (filename); 03095 else 03096 return rp; 03097 } 03098 #endif 03099 03100 /* FIXME: cagney/2002-11-13: 03101 03102 Method 2a: Use realpath() with a NULL buffer. Some systems, due 03103 to the problems described in method 3, have modified their 03104 realpath() implementation so that it will allocate a buffer when 03105 NULL is passed in. Before this can be used, though, some sort of 03106 configure time test would need to be added. Otherwize the code 03107 will likely core dump. */ 03108 03109 /* Method 3: Now we're getting desperate! The system doesn't have a 03110 compile time buffer size and no alternative function. Query the 03111 OS, using pathconf(), for the buffer limit. Care is needed 03112 though, some systems do not limit PATH_MAX (return -1 for 03113 pathconf()) making it impossible to pass a correctly sized buffer 03114 to realpath() (it could always overflow). On those systems, we 03115 skip this. */ 03116 #if defined (HAVE_REALPATH) && defined (_PC_PATH_MAX) && defined(HAVE_ALLOCA) 03117 { 03118 /* Find out the max path size. */ 03119 long path_max = pathconf ("/", _PC_PATH_MAX); 03120 03121 if (path_max > 0) 03122 { 03123 /* PATH_MAX is bounded. */ 03124 char *buf = alloca (path_max); 03125 char *rp = realpath (filename, buf); 03126 03127 return xstrdup (rp ? rp : filename); 03128 } 03129 } 03130 #endif 03131 03132 /* The MS Windows method. If we don't have realpath, we assume we 03133 don't have symlinks and just canonicalize to a Windows absolute 03134 path. GetFullPath converts ../ and ./ in relative paths to 03135 absolute paths, filling in current drive if one is not given 03136 or using the current directory of a specified drive (eg, "E:foo"). 03137 It also converts all forward slashes to back slashes. */ 03138 /* The file system is case-insensitive but case-preserving. 03139 So we do not lowercase the path. Otherwise, we might not 03140 be able to display the original casing in a given path. */ 03141 #if defined (_WIN32) 03142 { 03143 char buf[MAX_PATH]; 03144 DWORD len = GetFullPathName (filename, MAX_PATH, buf, NULL); 03145 03146 if (len > 0 && len < MAX_PATH) 03147 return xstrdup (buf); 03148 } 03149 #endif 03150 03151 /* This system is a lost cause, just dup the buffer. */ 03152 return xstrdup (filename); 03153 } 03154 03155 ULONGEST 03156 align_up (ULONGEST v, int n) 03157 { 03158 /* Check that N is really a power of two. */ 03159 gdb_assert (n && (n & (n-1)) == 0); 03160 return (v + n - 1) & -n; 03161 } 03162 03163 ULONGEST 03164 align_down (ULONGEST v, int n) 03165 { 03166 /* Check that N is really a power of two. */ 03167 gdb_assert (n && (n & (n-1)) == 0); 03168 return (v & -n); 03169 } 03170 03171 /* See utils.h. */ 03172 03173 LONGEST 03174 gdb_sign_extend (LONGEST value, int bit) 03175 { 03176 gdb_assert (bit >= 1 && bit <= 8 * sizeof (LONGEST)); 03177 03178 if (((value >> (bit - 1)) & 1) != 0) 03179 { 03180 LONGEST signbit = ((LONGEST) 1) << (bit - 1); 03181 03182 value = (value ^ signbit) - signbit; 03183 } 03184 03185 return value; 03186 } 03187 03188 /* Allocation function for the libiberty hash table which uses an 03189 obstack. The obstack is passed as DATA. */ 03190 03191 void * 03192 hashtab_obstack_allocate (void *data, size_t size, size_t count) 03193 { 03194 unsigned int total = size * count; 03195 void *ptr = obstack_alloc ((struct obstack *) data, total); 03196 03197 memset (ptr, 0, total); 03198 return ptr; 03199 } 03200 03201 /* Trivial deallocation function for the libiberty splay tree and hash 03202 table - don't deallocate anything. Rely on later deletion of the 03203 obstack. DATA will be the obstack, although it is not needed 03204 here. */ 03205 03206 void 03207 dummy_obstack_deallocate (void *object, void *data) 03208 { 03209 return; 03210 } 03211 03212 /* The bit offset of the highest byte in a ULONGEST, for overflow 03213 checking. */ 03214 03215 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT) 03216 03217 /* True (non-zero) iff DIGIT is a valid digit in radix BASE, 03218 where 2 <= BASE <= 36. */ 03219 03220 static int 03221 is_digit_in_base (unsigned char digit, int base) 03222 { 03223 if (!isalnum (digit)) 03224 return 0; 03225 if (base <= 10) 03226 return (isdigit (digit) && digit < base + '0'); 03227 else 03228 return (isdigit (digit) || tolower (digit) < base - 10 + 'a'); 03229 } 03230 03231 static int 03232 digit_to_int (unsigned char c) 03233 { 03234 if (isdigit (c)) 03235 return c - '0'; 03236 else 03237 return tolower (c) - 'a' + 10; 03238 } 03239 03240 ULONGEST 03241 strtoulst (const char *num, const char **trailer, int base) 03242 { 03243 unsigned int high_part; 03244 ULONGEST result; 03245 int minus = 0; 03246 int i = 0; 03247 03248 /* Skip leading whitespace. */ 03249 while (isspace (num[i])) 03250 i++; 03251 03252 /* Handle prefixes. */ 03253 if (num[i] == '+') 03254 i++; 03255 else if (num[i] == '-') 03256 { 03257 minus = 1; 03258 i++; 03259 } 03260 03261 if (base == 0 || base == 16) 03262 { 03263 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X')) 03264 { 03265 i += 2; 03266 if (base == 0) 03267 base = 16; 03268 } 03269 } 03270 03271 if (base == 0 && num[i] == '0') 03272 base = 8; 03273 03274 if (base == 0) 03275 base = 10; 03276 03277 if (base < 2 || base > 36) 03278 { 03279 errno = EINVAL; 03280 return 0; 03281 } 03282 03283 result = high_part = 0; 03284 for (; is_digit_in_base (num[i], base); i += 1) 03285 { 03286 result = result * base + digit_to_int (num[i]); 03287 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN); 03288 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1; 03289 if (high_part > 0xff) 03290 { 03291 errno = ERANGE; 03292 result = ~ (ULONGEST) 0; 03293 high_part = 0; 03294 minus = 0; 03295 break; 03296 } 03297 } 03298 03299 if (trailer != NULL) 03300 *trailer = &num[i]; 03301 03302 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN); 03303 if (minus) 03304 return -result; 03305 else 03306 return result; 03307 } 03308 03309 /* Simple, portable version of dirname that does not modify its 03310 argument. */ 03311 03312 char * 03313 ldirname (const char *filename) 03314 { 03315 const char *base = lbasename (filename); 03316 char *dirname; 03317 03318 while (base > filename && IS_DIR_SEPARATOR (base[-1])) 03319 --base; 03320 03321 if (base == filename) 03322 return NULL; 03323 03324 dirname = xmalloc (base - filename + 2); 03325 memcpy (dirname, filename, base - filename); 03326 03327 /* On DOS based file systems, convert "d:foo" to "d:.", so that we 03328 create "d:./bar" later instead of the (different) "d:/bar". */ 03329 if (base - filename == 2 && IS_ABSOLUTE_PATH (base) 03330 && !IS_DIR_SEPARATOR (filename[0])) 03331 dirname[base++ - filename] = '.'; 03332 03333 dirname[base - filename] = '\0'; 03334 return dirname; 03335 } 03336 03337 char ** 03338 gdb_buildargv (const char *s) 03339 { 03340 char **argv = buildargv (s); 03341 03342 if (s != NULL && argv == NULL) 03343 malloc_failure (0); 03344 return argv; 03345 } 03346 03347 /* See utils.h. */ 03348 03349 int 03350 compare_positive_ints (const void *ap, const void *bp) 03351 { 03352 /* Because we know we're comparing two ints which are positive, 03353 there's no danger of overflow here. */ 03354 return * (int *) ap - * (int *) bp; 03355 } 03356 03357 /* See utils.h. */ 03358 03359 int 03360 compare_strings (const void *arg1, const void *arg2) 03361 { 03362 const char **s1 = (const char **) arg1; 03363 const char **s2 = (const char **) arg2; 03364 03365 return strcmp (*s1, *s2); 03366 } 03367 03368 #define AMBIGUOUS_MESS1 ".\nMatching formats:" 03369 #define AMBIGUOUS_MESS2 \ 03370 ".\nUse \"set gnutarget format-name\" to specify the format." 03371 03372 const char * 03373 gdb_bfd_errmsg (bfd_error_type error_tag, char **matching) 03374 { 03375 char *ret, *retp; 03376 int ret_len; 03377 char **p; 03378 03379 /* Check if errmsg just need simple return. */ 03380 if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL) 03381 return bfd_errmsg (error_tag); 03382 03383 ret_len = strlen (bfd_errmsg (error_tag)) + strlen (AMBIGUOUS_MESS1) 03384 + strlen (AMBIGUOUS_MESS2); 03385 for (p = matching; *p; p++) 03386 ret_len += strlen (*p) + 1; 03387 ret = xmalloc (ret_len + 1); 03388 retp = ret; 03389 make_cleanup (xfree, ret); 03390 03391 strcpy (retp, bfd_errmsg (error_tag)); 03392 retp += strlen (retp); 03393 03394 strcpy (retp, AMBIGUOUS_MESS1); 03395 retp += strlen (retp); 03396 03397 for (p = matching; *p; p++) 03398 { 03399 sprintf (retp, " %s", *p); 03400 retp += strlen (retp); 03401 } 03402 xfree (matching); 03403 03404 strcpy (retp, AMBIGUOUS_MESS2); 03405 03406 return ret; 03407 } 03408 03409 /* See utils.h. */ 03410 03411 int 03412 parse_pid_to_attach (char *args) 03413 { 03414 unsigned long pid; 03415 char *dummy; 03416 03417 if (!args) 03418 error_no_arg (_("process-id to attach")); 03419 03420 dummy = args; 03421 pid = strtoul (args, &dummy, 0); 03422 /* Some targets don't set errno on errors, grrr! */ 03423 if ((pid == 0 && dummy == args) || dummy != &args[strlen (args)]) 03424 error (_("Illegal process-id: %s."), args); 03425 03426 return pid; 03427 } 03428 03429 /* Helper for make_bpstat_clear_actions_cleanup. */ 03430 03431 static void 03432 do_bpstat_clear_actions_cleanup (void *unused) 03433 { 03434 bpstat_clear_actions (); 03435 } 03436 03437 /* Call bpstat_clear_actions for the case an exception is throw. You should 03438 discard_cleanups if no exception is caught. */ 03439 03440 struct cleanup * 03441 make_bpstat_clear_actions_cleanup (void) 03442 { 03443 return make_cleanup (do_bpstat_clear_actions_cleanup, NULL); 03444 } 03445 03446 /* See utils.h. */ 03447 03448 int 03449 producer_is_gcc_ge_4 (const char *producer) 03450 { 03451 const char *cs; 03452 int major, minor; 03453 03454 if (producer == NULL) 03455 { 03456 /* For unknown compilers expect their behavior is not compliant. For GCC 03457 this case can also happen for -gdwarf-4 type units supported since 03458 gcc-4.5. */ 03459 03460 return -1; 03461 } 03462 03463 /* Skip any identifier after "GNU " - such as "C++" or "Java". */ 03464 03465 if (strncmp (producer, "GNU ", strlen ("GNU ")) != 0) 03466 { 03467 /* For non-GCC compilers expect their behavior is not compliant. */ 03468 03469 return -1; 03470 } 03471 cs = &producer[strlen ("GNU ")]; 03472 while (*cs && !isdigit (*cs)) 03473 cs++; 03474 if (sscanf (cs, "%d.%d", &major, &minor) != 2) 03475 { 03476 /* Not recognized as GCC. */ 03477 03478 return -1; 03479 } 03480 03481 if (major < 4) 03482 return -1; 03483 if (major > 4) 03484 return INT_MAX; 03485 return minor; 03486 } 03487 03488 /* Helper for make_cleanup_free_char_ptr_vec. */ 03489 03490 static void 03491 do_free_char_ptr_vec (void *arg) 03492 { 03493 VEC (char_ptr) *char_ptr_vec = arg; 03494 03495 free_char_ptr_vec (char_ptr_vec); 03496 } 03497 03498 /* Make cleanup handler calling xfree for each element of CHAR_PTR_VEC and 03499 final VEC_free for CHAR_PTR_VEC itself. 03500 03501 You must not modify CHAR_PTR_VEC after this cleanup registration as the 03502 CHAR_PTR_VEC base address may change on its updates. Contrary to VEC_free 03503 this function does not (cannot) clear the pointer. */ 03504 03505 struct cleanup * 03506 make_cleanup_free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec) 03507 { 03508 return make_cleanup (do_free_char_ptr_vec, char_ptr_vec); 03509 } 03510 03511 /* Substitute all occurences of string FROM by string TO in *STRINGP. *STRINGP 03512 must come from xrealloc-compatible allocator and it may be updated. FROM 03513 needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be 03514 located at the start or end of *STRINGP. */ 03515 03516 void 03517 substitute_path_component (char **stringp, const char *from, const char *to) 03518 { 03519 char *string = *stringp, *s; 03520 const size_t from_len = strlen (from); 03521 const size_t to_len = strlen (to); 03522 03523 for (s = string;;) 03524 { 03525 s = strstr (s, from); 03526 if (s == NULL) 03527 break; 03528 03529 if ((s == string || IS_DIR_SEPARATOR (s[-1]) 03530 || s[-1] == DIRNAME_SEPARATOR) 03531 && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len]) 03532 || s[from_len] == DIRNAME_SEPARATOR)) 03533 { 03534 char *string_new; 03535 03536 string_new = xrealloc (string, (strlen (string) + to_len + 1)); 03537 03538 /* Relocate the current S pointer. */ 03539 s = s - string + string_new; 03540 string = string_new; 03541 03542 /* Replace from by to. */ 03543 memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1); 03544 memcpy (s, to, to_len); 03545 03546 s += to_len; 03547 } 03548 else 03549 s++; 03550 } 03551 03552 *stringp = string; 03553 } 03554 03555 #ifdef HAVE_WAITPID 03556 03557 #ifdef SIGALRM 03558 03559 /* SIGALRM handler for waitpid_with_timeout. */ 03560 03561 static void 03562 sigalrm_handler (int signo) 03563 { 03564 /* Nothing to do. */ 03565 } 03566 03567 #endif 03568 03569 /* Wrapper to wait for child PID to die with TIMEOUT. 03570 TIMEOUT is the time to stop waiting in seconds. 03571 If TIMEOUT is zero, pass WNOHANG to waitpid. 03572 Returns PID if it was successfully waited for, otherwise -1. 03573 03574 Timeouts are currently implemented with alarm and SIGALRM. 03575 If the host does not support them, this waits "forever". 03576 It would be odd though for a host to have waitpid and not SIGALRM. */ 03577 03578 pid_t 03579 wait_to_die_with_timeout (pid_t pid, int *status, int timeout) 03580 { 03581 pid_t waitpid_result; 03582 03583 gdb_assert (pid > 0); 03584 gdb_assert (timeout >= 0); 03585 03586 if (timeout > 0) 03587 { 03588 #ifdef SIGALRM 03589 #if defined (HAVE_SIGACTION) && defined (SA_RESTART) 03590 struct sigaction sa, old_sa; 03591 03592 sa.sa_handler = sigalrm_handler; 03593 sigemptyset (&sa.sa_mask); 03594 sa.sa_flags = 0; 03595 sigaction (SIGALRM, &sa, &old_sa); 03596 #else 03597 void (*ofunc) (); 03598 03599 ofunc = (void (*)()) signal (SIGALRM, sigalrm_handler); 03600 #endif 03601 03602 alarm (timeout); 03603 #endif 03604 03605 waitpid_result = waitpid (pid, status, 0); 03606 03607 #ifdef SIGALRM 03608 alarm (0); 03609 #if defined (HAVE_SIGACTION) && defined (SA_RESTART) 03610 sigaction (SIGALRM, &old_sa, NULL); 03611 #else 03612 signal (SIGALRM, ofunc); 03613 #endif 03614 #endif 03615 } 03616 else 03617 waitpid_result = waitpid (pid, status, WNOHANG); 03618 03619 if (waitpid_result == pid) 03620 return pid; 03621 else 03622 return -1; 03623 } 03624 03625 #endif /* HAVE_WAITPID */ 03626 03627 /* Provide fnmatch compatible function for FNM_FILE_NAME matching of host files. 03628 Both FNM_FILE_NAME and FNM_NOESCAPE must be set in FLAGS. 03629 03630 It handles correctly HAVE_DOS_BASED_FILE_SYSTEM and 03631 HAVE_CASE_INSENSITIVE_FILE_SYSTEM. */ 03632 03633 int 03634 gdb_filename_fnmatch (const char *pattern, const char *string, int flags) 03635 { 03636 gdb_assert ((flags & FNM_FILE_NAME) != 0); 03637 03638 /* It is unclear how '\' escaping vs. directory separator should coexist. */ 03639 gdb_assert ((flags & FNM_NOESCAPE) != 0); 03640 03641 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 03642 { 03643 char *pattern_slash, *string_slash; 03644 03645 /* Replace '\' by '/' in both strings. */ 03646 03647 pattern_slash = alloca (strlen (pattern) + 1); 03648 strcpy (pattern_slash, pattern); 03649 pattern = pattern_slash; 03650 for (; *pattern_slash != 0; pattern_slash++) 03651 if (IS_DIR_SEPARATOR (*pattern_slash)) 03652 *pattern_slash = '/'; 03653 03654 string_slash = alloca (strlen (string) + 1); 03655 strcpy (string_slash, string); 03656 string = string_slash; 03657 for (; *string_slash != 0; string_slash++) 03658 if (IS_DIR_SEPARATOR (*string_slash)) 03659 *string_slash = '/'; 03660 } 03661 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */ 03662 03663 #ifdef HAVE_CASE_INSENSITIVE_FILE_SYSTEM 03664 flags |= FNM_CASEFOLD; 03665 #endif /* HAVE_CASE_INSENSITIVE_FILE_SYSTEM */ 03666 03667 return fnmatch (pattern, string, flags); 03668 } 03669 03670 /* Provide a prototype to silence -Wmissing-prototypes. */ 03671 extern initialize_file_ftype _initialize_utils; 03672 03673 void 03674 _initialize_utils (void) 03675 { 03676 add_internal_problem_command (&internal_error_problem); 03677 add_internal_problem_command (&internal_warning_problem); 03678 }