119370Spst/* General utility routines for GDB, the GNU debugger. 2130809Smarcel 398948Sobrien Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 4130809Smarcel 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software 5130809Smarcel Foundation, Inc. 619370Spst 798948Sobrien This file is part of GDB. 819370Spst 998948Sobrien This program is free software; you can redistribute it and/or modify 1098948Sobrien it under the terms of the GNU General Public License as published by 1198948Sobrien the Free Software Foundation; either version 2 of the License, or 1298948Sobrien (at your option) any later version. 1319370Spst 1498948Sobrien This program is distributed in the hope that it will be useful, 1598948Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1698948Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1798948Sobrien GNU General Public License for more details. 1819370Spst 1998948Sobrien You should have received a copy of the GNU General Public License 2098948Sobrien along with this program; if not, write to the Free Software 2198948Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2298948Sobrien Boston, MA 02111-1307, USA. */ 2319370Spst 2498948Sobrien#include "defs.h" 2598948Sobrien#include "gdb_assert.h" 2698948Sobrien#include <ctype.h> 2798948Sobrien#include "gdb_string.h" 2898948Sobrien#include "event-top.h" 2998948Sobrien 30130809Smarcel#ifdef TUI 31130809Smarcel#include "tui/tui.h" /* For tui_get_command_dimension. */ 32130809Smarcel#endif 33130809Smarcel 3498948Sobrien#ifdef __GO32__ 3598948Sobrien#include <pc.h> 3698948Sobrien#endif 3798948Sobrien 3846283Sdfr/* SunOS's curses.h has a '#define reg register' in it. Thank you Sun. */ 3946283Sdfr#ifdef reg 4046283Sdfr#undef reg 4146283Sdfr#endif 4246283Sdfr 4398948Sobrien#include <signal.h> 4419370Spst#include "gdbcmd.h" 4519370Spst#include "serial.h" 4619370Spst#include "bfd.h" 4719370Spst#include "target.h" 4819370Spst#include "demangle.h" 4919370Spst#include "expression.h" 5019370Spst#include "language.h" 51130809Smarcel#include "charset.h" 5219370Spst#include "annotate.h" 53130809Smarcel#include "filenames.h" 5419370Spst 55130809Smarcel#include "inferior.h" /* for signed_pointer_to_address */ 5698948Sobrien 5798948Sobrien#include <sys/param.h> /* For MAXPATHLEN */ 5898948Sobrien 59130809Smarcel#ifdef HAVE_CURSES_H 60130809Smarcel#include <curses.h> 6198948Sobrien#endif 62130809Smarcel#ifdef HAVE_TERM_H 63130809Smarcel#include <term.h> 64130809Smarcel#endif 6598948Sobrien 66130809Smarcel#include "readline/readline.h" 67130809Smarcel 6898948Sobrien#ifdef NEED_DECLARATION_MALLOC 69130809Smarcelextern PTR malloc (); /* OK: PTR */ 7098948Sobrien#endif 7198948Sobrien#ifdef NEED_DECLARATION_REALLOC 72130809Smarcelextern PTR realloc (); /* OK: PTR */ 7398948Sobrien#endif 7498948Sobrien#ifdef NEED_DECLARATION_FREE 7598948Sobrienextern void free (); 7698948Sobrien#endif 7798948Sobrien/* Actually, we'll never have the decl, since we don't define _GNU_SOURCE. */ 7898948Sobrien#if defined(HAVE_CANONICALIZE_FILE_NAME) \ 7998948Sobrien && defined(NEED_DECLARATION_CANONICALIZE_FILE_NAME) 8098948Sobrienextern char *canonicalize_file_name (const char *); 8198948Sobrien#endif 8298948Sobrien 8319370Spst/* readline defines this. */ 8419370Spst#undef savestring 8519370Spst 8698948Sobrienvoid (*error_begin_hook) (void); 8746283Sdfr 8898948Sobrien/* Holds the last error message issued by gdb */ 8998948Sobrien 9098948Sobrienstatic struct ui_file *gdb_lasterr; 9198948Sobrien 9219370Spst/* Prototypes for local functions */ 9319370Spst 9498948Sobrienstatic void vfprintf_maybe_filtered (struct ui_file *, const char *, 9598948Sobrien va_list, int); 9619370Spst 9798948Sobrienstatic void fputs_maybe_filtered (const char *, struct ui_file *, int); 9819370Spst 99130809Smarcelstatic void do_my_cleanups (struct cleanup **, struct cleanup *); 10019370Spst 10198948Sobrienstatic void prompt_for_continue (void); 10219370Spst 103130809Smarcelstatic void set_screen_size (void); 10498948Sobrienstatic void set_width (void); 10519370Spst 10619370Spst/* Chain of cleanup actions established with make_cleanup, 10719370Spst to be executed if an error happens. */ 10819370Spst 10998948Sobrienstatic struct cleanup *cleanup_chain; /* cleaned up after a failed command */ 11098948Sobrienstatic struct cleanup *final_cleanup_chain; /* cleaned up when gdb exits */ 11198948Sobrienstatic struct cleanup *run_cleanup_chain; /* cleaned up on each 'run' */ 11298948Sobrienstatic struct cleanup *exec_cleanup_chain; /* cleaned up on each execution command */ 11398948Sobrien/* cleaned up on each error from within an execution command */ 114130809Smarcelstatic struct cleanup *exec_error_cleanup_chain; 11519370Spst 11698948Sobrien/* Pointer to what is left to do for an execution command after the 11798948Sobrien target stops. Used only in asynchronous mode, by targets that 11898948Sobrien support async execution. The finish and until commands use it. So 11998948Sobrien does the target extended-remote command. */ 12098948Sobrienstruct continuation *cmd_continuation; 12198948Sobrienstruct continuation *intermediate_continuation; 12298948Sobrien 12319370Spst/* Nonzero if we have job control. */ 12419370Spst 12519370Spstint job_control; 12619370Spst 12719370Spst/* Nonzero means a quit has been requested. */ 12819370Spst 12919370Spstint quit_flag; 13019370Spst 13119370Spst/* Nonzero means quit immediately if Control-C is typed now, rather 13219370Spst than waiting until QUIT is executed. Be careful in setting this; 13319370Spst code which executes with immediate_quit set has to be very careful 13419370Spst about being able to deal with being interrupted at any time. It is 13519370Spst almost always better to use QUIT; the only exception I can think of 13619370Spst is being able to quit out of a system call (using EINTR loses if 13719370Spst the SIGINT happens between the previous QUIT and the system call). 13819370Spst To immediately quit in the case in which a SIGINT happens between 13919370Spst the previous QUIT and setting immediate_quit (desirable anytime we 14019370Spst expect to block), call QUIT after setting immediate_quit. */ 14119370Spst 14219370Spstint immediate_quit; 14319370Spst 144130809Smarcel/* Nonzero means that encoded C++/ObjC names should be printed out in their 145130809Smarcel C++/ObjC form rather than raw. */ 14619370Spst 14719370Spstint demangle = 1; 14819370Spst 149130809Smarcel/* Nonzero means that encoded C++/ObjC names should be printed out in their 150130809Smarcel C++/ObjC form even in assembler language displays. If this is set, but 15119370Spst DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ 15219370Spst 15319370Spstint asm_demangle = 0; 15419370Spst 15519370Spst/* Nonzero means that strings with character values >0x7F should be printed 15619370Spst as octal escapes. Zero means just print the value (e.g. it's an 15719370Spst international character, and the terminal or window can cope.) */ 15819370Spst 15919370Spstint sevenbit_strings = 0; 16019370Spst 16119370Spst/* String to be printed before error messages, if any. */ 16219370Spst 16319370Spstchar *error_pre_print; 16419370Spst 16519370Spst/* String to be printed before quit messages, if any. */ 16619370Spst 16719370Spstchar *quit_pre_print; 16819370Spst 16919370Spst/* String to be printed before warning messages, if any. */ 17019370Spst 17119370Spstchar *warning_pre_print = "\nwarning: "; 17246283Sdfr 17346283Sdfrint pagination_enabled = 1; 17498948Sobrien 17546283Sdfr 17619370Spst/* Add a new cleanup to the cleanup_chain, 17719370Spst and return the previous chain pointer 17819370Spst to be passed later to do_cleanups or discard_cleanups. 17919370Spst Args are FUNCTION to clean up with, and ARG to pass to it. */ 18019370Spst 18119370Spststruct cleanup * 18298948Sobrienmake_cleanup (make_cleanup_ftype *function, void *arg) 18319370Spst{ 18498948Sobrien return make_my_cleanup (&cleanup_chain, function, arg); 18546283Sdfr} 18646283Sdfr 18746283Sdfrstruct cleanup * 18898948Sobrienmake_final_cleanup (make_cleanup_ftype *function, void *arg) 18946283Sdfr{ 19098948Sobrien return make_my_cleanup (&final_cleanup_chain, function, arg); 19146283Sdfr} 19298948Sobrien 19346283Sdfrstruct cleanup * 19498948Sobrienmake_run_cleanup (make_cleanup_ftype *function, void *arg) 19546283Sdfr{ 19698948Sobrien return make_my_cleanup (&run_cleanup_chain, function, arg); 19746283Sdfr} 19898948Sobrien 19946283Sdfrstruct cleanup * 20098948Sobrienmake_exec_cleanup (make_cleanup_ftype *function, void *arg) 20146283Sdfr{ 20298948Sobrien return make_my_cleanup (&exec_cleanup_chain, function, arg); 20398948Sobrien} 20498948Sobrien 20598948Sobrienstruct cleanup * 20698948Sobrienmake_exec_error_cleanup (make_cleanup_ftype *function, void *arg) 20798948Sobrien{ 20898948Sobrien return make_my_cleanup (&exec_error_cleanup_chain, function, arg); 20998948Sobrien} 21098948Sobrien 21198948Sobrienstatic void 21298948Sobriendo_freeargv (void *arg) 21398948Sobrien{ 21498948Sobrien freeargv ((char **) arg); 21598948Sobrien} 21698948Sobrien 21798948Sobrienstruct cleanup * 21898948Sobrienmake_cleanup_freeargv (char **arg) 21998948Sobrien{ 22098948Sobrien return make_my_cleanup (&cleanup_chain, do_freeargv, arg); 22198948Sobrien} 22298948Sobrien 22398948Sobrienstatic void 22498948Sobriendo_bfd_close_cleanup (void *arg) 22598948Sobrien{ 22698948Sobrien bfd_close (arg); 22798948Sobrien} 22898948Sobrien 22998948Sobrienstruct cleanup * 23098948Sobrienmake_cleanup_bfd_close (bfd *abfd) 23198948Sobrien{ 23298948Sobrien return make_cleanup (do_bfd_close_cleanup, abfd); 23398948Sobrien} 23498948Sobrien 23598948Sobrienstatic void 23698948Sobriendo_close_cleanup (void *arg) 23798948Sobrien{ 23898948Sobrien int *fd = arg; 23998948Sobrien close (*fd); 24098948Sobrien xfree (fd); 24198948Sobrien} 24298948Sobrien 24398948Sobrienstruct cleanup * 24498948Sobrienmake_cleanup_close (int fd) 24598948Sobrien{ 24698948Sobrien int *saved_fd = xmalloc (sizeof (fd)); 24798948Sobrien *saved_fd = fd; 24898948Sobrien return make_cleanup (do_close_cleanup, saved_fd); 24998948Sobrien} 25098948Sobrien 25198948Sobrienstatic void 25298948Sobriendo_ui_file_delete (void *arg) 25398948Sobrien{ 25498948Sobrien ui_file_delete (arg); 25598948Sobrien} 25698948Sobrien 25798948Sobrienstruct cleanup * 25898948Sobrienmake_cleanup_ui_file_delete (struct ui_file *arg) 25998948Sobrien{ 26098948Sobrien return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg); 26198948Sobrien} 26298948Sobrien 26398948Sobrienstruct cleanup * 26498948Sobrienmake_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function, 26598948Sobrien void *arg) 26698948Sobrien{ 267130809Smarcel struct cleanup *new 268130809Smarcel = (struct cleanup *) xmalloc (sizeof (struct cleanup)); 269130809Smarcel struct cleanup *old_chain = *pmy_chain; 27019370Spst 27146283Sdfr new->next = *pmy_chain; 27219370Spst new->function = function; 27319370Spst new->arg = arg; 27446283Sdfr *pmy_chain = new; 27519370Spst 27619370Spst return old_chain; 27719370Spst} 27819370Spst 27919370Spst/* Discard cleanups and do the actions they describe 28019370Spst until we get back to the point OLD_CHAIN in the cleanup_chain. */ 28119370Spst 28219370Spstvoid 283130809Smarceldo_cleanups (struct cleanup *old_chain) 28419370Spst{ 28598948Sobrien do_my_cleanups (&cleanup_chain, old_chain); 28646283Sdfr} 28746283Sdfr 28846283Sdfrvoid 289130809Smarceldo_final_cleanups (struct cleanup *old_chain) 29046283Sdfr{ 29198948Sobrien do_my_cleanups (&final_cleanup_chain, old_chain); 29246283Sdfr} 29346283Sdfr 29446283Sdfrvoid 295130809Smarceldo_run_cleanups (struct cleanup *old_chain) 29646283Sdfr{ 29798948Sobrien do_my_cleanups (&run_cleanup_chain, old_chain); 29846283Sdfr} 29946283Sdfr 30046283Sdfrvoid 301130809Smarceldo_exec_cleanups (struct cleanup *old_chain) 30246283Sdfr{ 30398948Sobrien do_my_cleanups (&exec_cleanup_chain, old_chain); 30498948Sobrien} 30598948Sobrien 30698948Sobrienvoid 307130809Smarceldo_exec_error_cleanups (struct cleanup *old_chain) 30898948Sobrien{ 30998948Sobrien do_my_cleanups (&exec_error_cleanup_chain, old_chain); 31098948Sobrien} 31198948Sobrien 312130809Smarcelstatic void 313130809Smarceldo_my_cleanups (struct cleanup **pmy_chain, 314130809Smarcel struct cleanup *old_chain) 31598948Sobrien{ 316130809Smarcel struct cleanup *ptr; 31746283Sdfr while ((ptr = *pmy_chain) != old_chain) 31819370Spst { 31946283Sdfr *pmy_chain = ptr->next; /* Do this first incase recursion */ 32019370Spst (*ptr->function) (ptr->arg); 32198948Sobrien xfree (ptr); 32219370Spst } 32319370Spst} 32419370Spst 32519370Spst/* Discard cleanups, not doing the actions they describe, 32619370Spst until we get back to the point OLD_CHAIN in the cleanup_chain. */ 32719370Spst 32819370Spstvoid 329130809Smarceldiscard_cleanups (struct cleanup *old_chain) 33019370Spst{ 33198948Sobrien discard_my_cleanups (&cleanup_chain, old_chain); 33246283Sdfr} 33346283Sdfr 33446283Sdfrvoid 335130809Smarceldiscard_final_cleanups (struct cleanup *old_chain) 33646283Sdfr{ 33798948Sobrien discard_my_cleanups (&final_cleanup_chain, old_chain); 33846283Sdfr} 33946283Sdfr 34046283Sdfrvoid 341130809Smarceldiscard_exec_error_cleanups (struct cleanup *old_chain) 34246283Sdfr{ 34398948Sobrien discard_my_cleanups (&exec_error_cleanup_chain, old_chain); 34498948Sobrien} 34598948Sobrien 34698948Sobrienvoid 347130809Smarceldiscard_my_cleanups (struct cleanup **pmy_chain, 348130809Smarcel struct cleanup *old_chain) 34998948Sobrien{ 350130809Smarcel struct cleanup *ptr; 35146283Sdfr while ((ptr = *pmy_chain) != old_chain) 35219370Spst { 35346283Sdfr *pmy_chain = ptr->next; 35498948Sobrien xfree (ptr); 35519370Spst } 35619370Spst} 35719370Spst 35819370Spst/* Set the cleanup_chain to 0, and return the old cleanup chain. */ 35919370Spststruct cleanup * 36098948Sobriensave_cleanups (void) 36119370Spst{ 36298948Sobrien return save_my_cleanups (&cleanup_chain); 36346283Sdfr} 36419370Spst 36546283Sdfrstruct cleanup * 36698948Sobriensave_final_cleanups (void) 36746283Sdfr{ 36898948Sobrien return save_my_cleanups (&final_cleanup_chain); 36946283Sdfr} 37046283Sdfr 37146283Sdfrstruct cleanup * 37298948Sobriensave_my_cleanups (struct cleanup **pmy_chain) 37346283Sdfr{ 37446283Sdfr struct cleanup *old_chain = *pmy_chain; 37546283Sdfr 37646283Sdfr *pmy_chain = 0; 37719370Spst return old_chain; 37819370Spst} 37919370Spst 38019370Spst/* Restore the cleanup chain from a previously saved chain. */ 38119370Spstvoid 38298948Sobrienrestore_cleanups (struct cleanup *chain) 38319370Spst{ 38498948Sobrien restore_my_cleanups (&cleanup_chain, chain); 38519370Spst} 38619370Spst 38746283Sdfrvoid 38898948Sobrienrestore_final_cleanups (struct cleanup *chain) 38946283Sdfr{ 39098948Sobrien restore_my_cleanups (&final_cleanup_chain, chain); 39146283Sdfr} 39246283Sdfr 39346283Sdfrvoid 39498948Sobrienrestore_my_cleanups (struct cleanup **pmy_chain, struct cleanup *chain) 39546283Sdfr{ 39646283Sdfr *pmy_chain = chain; 39746283Sdfr} 39846283Sdfr 39919370Spst/* This function is useful for cleanups. 40019370Spst Do 40119370Spst 40298948Sobrien foo = xmalloc (...); 40398948Sobrien old_chain = make_cleanup (free_current_contents, &foo); 40419370Spst 40519370Spst to arrange to free the object thus allocated. */ 40619370Spst 40719370Spstvoid 40898948Sobrienfree_current_contents (void *ptr) 40919370Spst{ 41098948Sobrien void **location = ptr; 41198948Sobrien if (location == NULL) 41298948Sobrien internal_error (__FILE__, __LINE__, 41398948Sobrien "free_current_contents: NULL pointer"); 41498948Sobrien if (*location != NULL) 41598948Sobrien { 41698948Sobrien xfree (*location); 41798948Sobrien *location = NULL; 41898948Sobrien } 41919370Spst} 42019370Spst 42119370Spst/* Provide a known function that does nothing, to use as a base for 42219370Spst for a possibly long chain of cleanups. This is useful where we 42319370Spst use the cleanup chain for handling normal cleanups as well as dealing 42419370Spst with cleanups that need to be done as a result of a call to error(). 42519370Spst In such cases, we may not be certain where the first cleanup is, unless 42619370Spst we have a do-nothing one to always use as the base. */ 42719370Spst 42819370Spstvoid 42998948Sobriennull_cleanup (void *arg) 43019370Spst{ 43119370Spst} 43219370Spst 43398948Sobrien/* Add a continuation to the continuation list, the global list 43498948Sobrien cmd_continuation. The new continuation will be added at the front.*/ 43598948Sobrienvoid 43698948Sobrienadd_continuation (void (*continuation_hook) (struct continuation_arg *), 43798948Sobrien struct continuation_arg *arg_list) 43898948Sobrien{ 43998948Sobrien struct continuation *continuation_ptr; 44019370Spst 441130809Smarcel continuation_ptr = 442130809Smarcel (struct continuation *) xmalloc (sizeof (struct continuation)); 44398948Sobrien continuation_ptr->continuation_hook = continuation_hook; 44498948Sobrien continuation_ptr->arg_list = arg_list; 44598948Sobrien continuation_ptr->next = cmd_continuation; 44698948Sobrien cmd_continuation = continuation_ptr; 44798948Sobrien} 44819370Spst 44998948Sobrien/* Walk down the cmd_continuation list, and execute all the 45098948Sobrien continuations. There is a problem though. In some cases new 45198948Sobrien continuations may be added while we are in the middle of this 45298948Sobrien loop. If this happens they will be added in the front, and done 45398948Sobrien before we have a chance of exhausting those that were already 45498948Sobrien there. We need to then save the beginning of the list in a pointer 45598948Sobrien and do the continuations from there on, instead of using the 45698948Sobrien global beginning of list as our iteration pointer.*/ 45719370Spstvoid 45898948Sobriendo_all_continuations (void) 45919370Spst{ 46098948Sobrien struct continuation *continuation_ptr; 46198948Sobrien struct continuation *saved_continuation; 46298948Sobrien 46398948Sobrien /* Copy the list header into another pointer, and set the global 46498948Sobrien list header to null, so that the global list can change as a side 46598948Sobrien effect of invoking the continuations and the processing of 46698948Sobrien the preexisting continuations will not be affected. */ 46798948Sobrien continuation_ptr = cmd_continuation; 46898948Sobrien cmd_continuation = NULL; 46998948Sobrien 47098948Sobrien /* Work now on the list we have set aside. */ 47198948Sobrien while (continuation_ptr) 472130809Smarcel { 473130809Smarcel (continuation_ptr->continuation_hook) (continuation_ptr->arg_list); 474130809Smarcel saved_continuation = continuation_ptr; 475130809Smarcel continuation_ptr = continuation_ptr->next; 476130809Smarcel xfree (saved_continuation); 477130809Smarcel } 47819370Spst} 47919370Spst 48098948Sobrien/* Walk down the cmd_continuation list, and get rid of all the 48198948Sobrien continuations. */ 48298948Sobrienvoid 48398948Sobriendiscard_all_continuations (void) 48498948Sobrien{ 48598948Sobrien struct continuation *continuation_ptr; 48619370Spst 48798948Sobrien while (cmd_continuation) 48898948Sobrien { 48998948Sobrien continuation_ptr = cmd_continuation; 49098948Sobrien cmd_continuation = continuation_ptr->next; 49198948Sobrien xfree (continuation_ptr); 49298948Sobrien } 49398948Sobrien} 49498948Sobrien 49598948Sobrien/* Add a continuation to the continuation list, the global list 49698948Sobrien intermediate_continuation. The new continuation will be added at the front.*/ 49719370Spstvoid 49898948Sobrienadd_intermediate_continuation (void (*continuation_hook) 49998948Sobrien (struct continuation_arg *), 50098948Sobrien struct continuation_arg *arg_list) 50119370Spst{ 50298948Sobrien struct continuation *continuation_ptr; 50319370Spst 504130809Smarcel continuation_ptr = 505130809Smarcel (struct continuation *) xmalloc (sizeof (struct continuation)); 50698948Sobrien continuation_ptr->continuation_hook = continuation_hook; 50798948Sobrien continuation_ptr->arg_list = arg_list; 50898948Sobrien continuation_ptr->next = intermediate_continuation; 50998948Sobrien intermediate_continuation = continuation_ptr; 51019370Spst} 51119370Spst 51298948Sobrien/* Walk down the cmd_continuation list, and execute all the 51398948Sobrien continuations. There is a problem though. In some cases new 51498948Sobrien continuations may be added while we are in the middle of this 51598948Sobrien loop. If this happens they will be added in the front, and done 51698948Sobrien before we have a chance of exhausting those that were already 51798948Sobrien there. We need to then save the beginning of the list in a pointer 51898948Sobrien and do the continuations from there on, instead of using the 51998948Sobrien global beginning of list as our iteration pointer.*/ 52019370Spstvoid 52198948Sobriendo_all_intermediate_continuations (void) 52219370Spst{ 52398948Sobrien struct continuation *continuation_ptr; 52498948Sobrien struct continuation *saved_continuation; 52546283Sdfr 52698948Sobrien /* Copy the list header into another pointer, and set the global 52798948Sobrien list header to null, so that the global list can change as a side 52898948Sobrien effect of invoking the continuations and the processing of 52998948Sobrien the preexisting continuations will not be affected. */ 53098948Sobrien continuation_ptr = intermediate_continuation; 53198948Sobrien intermediate_continuation = NULL; 53219370Spst 53398948Sobrien /* Work now on the list we have set aside. */ 53498948Sobrien while (continuation_ptr) 535130809Smarcel { 536130809Smarcel (continuation_ptr->continuation_hook) (continuation_ptr->arg_list); 537130809Smarcel saved_continuation = continuation_ptr; 538130809Smarcel continuation_ptr = continuation_ptr->next; 539130809Smarcel xfree (saved_continuation); 540130809Smarcel } 54198948Sobrien} 54219370Spst 54398948Sobrien/* Walk down the cmd_continuation list, and get rid of all the 54498948Sobrien continuations. */ 54598948Sobrienvoid 54698948Sobriendiscard_all_intermediate_continuations (void) 54798948Sobrien{ 54898948Sobrien struct continuation *continuation_ptr; 54998948Sobrien 55098948Sobrien while (intermediate_continuation) 55198948Sobrien { 55298948Sobrien continuation_ptr = intermediate_continuation; 55398948Sobrien intermediate_continuation = continuation_ptr->next; 55498948Sobrien xfree (continuation_ptr); 55598948Sobrien } 55619370Spst} 55798948Sobrien 55819370Spst 559130809Smarcel 56098948Sobrien/* Print a warning message. The first argument STRING is the warning 56198948Sobrien message, used as an fprintf format string, the second is the 56298948Sobrien va_list of arguments for that string. A warning is unfiltered (not 56398948Sobrien paginated) so that the user does not need to page through each 56498948Sobrien screen full of warnings when there are lots of them. */ 56598948Sobrien 56698948Sobrienvoid 56798948Sobrienvwarning (const char *string, va_list args) 56819370Spst{ 56998948Sobrien if (warning_hook) 57098948Sobrien (*warning_hook) (string, args); 57198948Sobrien else 57219370Spst { 57398948Sobrien target_terminal_ours (); 57498948Sobrien wrap_here (""); /* Force out any buffered output */ 57598948Sobrien gdb_flush (gdb_stdout); 57698948Sobrien if (warning_pre_print) 577130809Smarcel fputs_unfiltered (warning_pre_print, gdb_stderr); 57898948Sobrien vfprintf_unfiltered (gdb_stderr, string, args); 57998948Sobrien fprintf_unfiltered (gdb_stderr, "\n"); 58019370Spst va_end (args); 58119370Spst } 58219370Spst} 58319370Spst 58498948Sobrien/* Print a warning message. 58598948Sobrien The first argument STRING is the warning message, used as a fprintf string, 58698948Sobrien and the remaining args are passed as arguments to it. 58798948Sobrien The primary difference between warnings and errors is that a warning 58898948Sobrien does not force the return to command level. */ 58919370Spst 59098948Sobrienvoid 591130809Smarcelwarning (const char *string, ...) 59298948Sobrien{ 59398948Sobrien va_list args; 59498948Sobrien va_start (args, string); 59598948Sobrien vwarning (string, args); 59698948Sobrien va_end (args); 59798948Sobrien} 59819370Spst 59998948Sobrien/* Print an error message and return to command level. 60098948Sobrien The first argument STRING is the error message, used as a fprintf string, 60198948Sobrien and the remaining args are passed as arguments to it. */ 60219370Spst 60319370SpstNORETURN void 60498948Sobrienverror (const char *string, va_list args) 60519370Spst{ 60698948Sobrien struct ui_file *tmp_stream = mem_fileopen (); 60798948Sobrien make_cleanup_ui_file_delete (tmp_stream); 60898948Sobrien vfprintf_unfiltered (tmp_stream, string, args); 60998948Sobrien error_stream (tmp_stream); 61098948Sobrien} 61198948Sobrien 61298948SobrienNORETURN void 613130809Smarcelerror (const char *string, ...) 61498948Sobrien{ 61519370Spst va_list args; 61619370Spst va_start (args, string); 61798948Sobrien verror (string, args); 61819370Spst va_end (args); 61919370Spst} 62019370Spst 62119370Spststatic void 62298948Sobriendo_write (void *data, const char *buffer, long length_buffer) 62319370Spst{ 62498948Sobrien ui_file_write (data, buffer, length_buffer); 62598948Sobrien} 62619370Spst 627130809Smarcel/* Cause a silent error to occur. Any error message is recorded 628130809Smarcel though it is not issued. */ 62998948SobrienNORETURN void 630130809Smarcelerror_silent (const char *string, ...) 631130809Smarcel{ 632130809Smarcel va_list args; 633130809Smarcel struct ui_file *tmp_stream = mem_fileopen (); 634130809Smarcel va_start (args, string); 635130809Smarcel make_cleanup_ui_file_delete (tmp_stream); 636130809Smarcel vfprintf_unfiltered (tmp_stream, string, args); 637130809Smarcel /* Copy the stream into the GDB_LASTERR buffer. */ 638130809Smarcel ui_file_rewind (gdb_lasterr); 639130809Smarcel ui_file_put (tmp_stream, do_write, gdb_lasterr); 640130809Smarcel va_end (args); 641130809Smarcel 642130809Smarcel throw_exception (RETURN_ERROR); 643130809Smarcel} 644130809Smarcel 645130809Smarcel/* Output an error message including any pre-print text to gdb_stderr. */ 646130809Smarcelvoid 647130809Smarcelerror_output_message (char *pre_print, char *msg) 648130809Smarcel{ 649130809Smarcel target_terminal_ours (); 650130809Smarcel wrap_here (""); /* Force out any buffered output */ 651130809Smarcel gdb_flush (gdb_stdout); 652130809Smarcel annotate_error_begin (); 653130809Smarcel if (pre_print) 654130809Smarcel fputs_filtered (pre_print, gdb_stderr); 655130809Smarcel fputs_filtered (msg, gdb_stderr); 656130809Smarcel fprintf_filtered (gdb_stderr, "\n"); 657130809Smarcel} 658130809Smarcel 659130809SmarcelNORETURN void 66098948Sobrienerror_stream (struct ui_file *stream) 66198948Sobrien{ 66298948Sobrien if (error_begin_hook) 66398948Sobrien error_begin_hook (); 66419370Spst 66598948Sobrien /* Copy the stream into the GDB_LASTERR buffer. */ 66698948Sobrien ui_file_rewind (gdb_lasterr); 66798948Sobrien ui_file_put (stream, do_write, gdb_lasterr); 66898948Sobrien 66998948Sobrien /* Write the message plus any error_pre_print to gdb_stderr. */ 67098948Sobrien target_terminal_ours (); 67198948Sobrien wrap_here (""); /* Force out any buffered output */ 67298948Sobrien gdb_flush (gdb_stdout); 67398948Sobrien annotate_error_begin (); 67498948Sobrien if (error_pre_print) 675130809Smarcel fputs_filtered (error_pre_print, gdb_stderr); 67698948Sobrien ui_file_put (stream, do_write, gdb_stderr); 67798948Sobrien fprintf_filtered (gdb_stderr, "\n"); 67898948Sobrien 67998948Sobrien throw_exception (RETURN_ERROR); 68019370Spst} 68119370Spst 68298948Sobrien/* Get the last error message issued by gdb */ 68319370Spst 68419370Spstchar * 68598948Sobrienerror_last_message (void) 68619370Spst{ 68798948Sobrien long len; 68898948Sobrien return ui_file_xstrdup (gdb_lasterr, &len); 68998948Sobrien} 690130809Smarcel 69198948Sobrien/* This is to be called by main() at the very beginning */ 69219370Spst 69398948Sobrienvoid 69498948Sobrienerror_init (void) 69598948Sobrien{ 69698948Sobrien gdb_lasterr = mem_fileopen (); 69798948Sobrien} 69898948Sobrien 699130809Smarcel/* Print a message reporting an internal error/warning. Ask the user 700130809Smarcel if they want to continue, dump core, or just exit. Return 701130809Smarcel something to indicate a quit. */ 70298948Sobrien 703130809Smarcelstruct internal_problem 70498948Sobrien{ 705130809Smarcel const char *name; 706130809Smarcel /* FIXME: cagney/2002-08-15: There should be ``maint set/show'' 707130809Smarcel commands available for controlling these variables. */ 708130809Smarcel enum auto_boolean should_quit; 709130809Smarcel enum auto_boolean should_dump_core; 710130809Smarcel}; 711130809Smarcel 712130809Smarcel/* Report a problem, internal to GDB, to the user. Once the problem 713130809Smarcel has been reported, and assuming GDB didn't quit, the caller can 714130809Smarcel either allow execution to resume or throw an error. */ 715130809Smarcel 716130809Smarcelstatic void 717130809Smarcelinternal_vproblem (struct internal_problem *problem, 718130809Smarcel const char *file, int line, const char *fmt, va_list ap) 719130809Smarcel{ 720130809Smarcel static int dejavu; 72198948Sobrien int quit_p; 72298948Sobrien int dump_core_p; 723130809Smarcel char *reason; 72498948Sobrien 725130809Smarcel /* Don't allow infinite error/warning recursion. */ 726130809Smarcel { 727130809Smarcel static char msg[] = "Recursive internal problem.\n"; 728130809Smarcel switch (dejavu) 729130809Smarcel { 730130809Smarcel case 0: 731130809Smarcel dejavu = 1; 732130809Smarcel break; 733130809Smarcel case 1: 734130809Smarcel dejavu = 2; 735130809Smarcel fputs_unfiltered (msg, gdb_stderr); 736130809Smarcel abort (); /* NOTE: GDB has only three calls to abort(). */ 737130809Smarcel default: 738130809Smarcel dejavu = 3; 739130809Smarcel write (STDERR_FILENO, msg, sizeof (msg)); 740130809Smarcel exit (1); 741130809Smarcel } 742130809Smarcel } 743130809Smarcel 744130809Smarcel /* Try to get the message out and at the start of a new line. */ 745130809Smarcel target_terminal_ours (); 746130809Smarcel begin_line (); 747130809Smarcel 748130809Smarcel /* Create a string containing the full error/warning message. Need 749130809Smarcel to call query with this full string, as otherwize the reason 750130809Smarcel (error/warning) and question become separated. Format using a 751130809Smarcel style similar to a compiler error message. Include extra detail 752130809Smarcel so that the user knows that they are living on the edge. */ 753130809Smarcel { 754130809Smarcel char *msg; 755130809Smarcel xvasprintf (&msg, fmt, ap); 756130809Smarcel xasprintf (&reason, "\ 757130809Smarcel%s:%d: %s: %s\n\ 758130809SmarcelA problem internal to GDB has been detected,\n\ 759130809Smarcelfurther debugging may prove unreliable.", file, line, problem->name, msg); 760130809Smarcel xfree (msg); 761130809Smarcel make_cleanup (xfree, reason); 762130809Smarcel } 763130809Smarcel 764130809Smarcel switch (problem->should_quit) 76519370Spst { 766130809Smarcel case AUTO_BOOLEAN_AUTO: 767130809Smarcel /* Default (yes/batch case) is to quit GDB. When in batch mode 768130809Smarcel this lessens the likelhood of GDB going into an infinate 769130809Smarcel loop. */ 770130809Smarcel quit_p = query ("%s\nQuit this debugging session? ", reason); 77198948Sobrien break; 772130809Smarcel case AUTO_BOOLEAN_TRUE: 773130809Smarcel quit_p = 1; 774130809Smarcel break; 775130809Smarcel case AUTO_BOOLEAN_FALSE: 776130809Smarcel quit_p = 0; 777130809Smarcel break; 77898948Sobrien default: 779130809Smarcel internal_error (__FILE__, __LINE__, "bad switch"); 78019370Spst } 78198948Sobrien 782130809Smarcel switch (problem->should_dump_core) 783130809Smarcel { 784130809Smarcel case AUTO_BOOLEAN_AUTO: 785130809Smarcel /* Default (yes/batch case) is to dump core. This leaves a GDB 786130809Smarcel `dropping' so that it is easier to see that something went 787130809Smarcel wrong in GDB. */ 788130809Smarcel dump_core_p = query ("%s\nCreate a core file of GDB? ", reason); 789130809Smarcel break; 790130809Smarcel break; 791130809Smarcel case AUTO_BOOLEAN_TRUE: 792130809Smarcel dump_core_p = 1; 793130809Smarcel break; 794130809Smarcel case AUTO_BOOLEAN_FALSE: 795130809Smarcel dump_core_p = 0; 796130809Smarcel break; 797130809Smarcel default: 798130809Smarcel internal_error (__FILE__, __LINE__, "bad switch"); 799130809Smarcel } 80098948Sobrien 80198948Sobrien if (quit_p) 80298948Sobrien { 80398948Sobrien if (dump_core_p) 804130809Smarcel abort (); /* NOTE: GDB has only three calls to abort(). */ 80598948Sobrien else 80698948Sobrien exit (1); 80798948Sobrien } 80898948Sobrien else 80998948Sobrien { 81098948Sobrien if (dump_core_p) 81198948Sobrien { 81298948Sobrien if (fork () == 0) 813130809Smarcel abort (); /* NOTE: GDB has only three calls to abort(). */ 81498948Sobrien } 81598948Sobrien } 81698948Sobrien 81798948Sobrien dejavu = 0; 818130809Smarcel} 819130809Smarcel 820130809Smarcelstatic struct internal_problem internal_error_problem = { 821130809Smarcel "internal-error", AUTO_BOOLEAN_AUTO, AUTO_BOOLEAN_AUTO 822130809Smarcel}; 823130809Smarcel 824130809SmarcelNORETURN void 825130809Smarcelinternal_verror (const char *file, int line, const char *fmt, va_list ap) 826130809Smarcel{ 827130809Smarcel internal_vproblem (&internal_error_problem, file, line, fmt, ap); 82898948Sobrien throw_exception (RETURN_ERROR); 82919370Spst} 83019370Spst 83198948SobrienNORETURN void 83298948Sobrieninternal_error (const char *file, int line, const char *string, ...) 83398948Sobrien{ 83498948Sobrien va_list ap; 83598948Sobrien va_start (ap, string); 83698948Sobrien internal_verror (file, line, string, ap); 83798948Sobrien va_end (ap); 83898948Sobrien} 83998948Sobrien 840130809Smarcelstatic struct internal_problem internal_warning_problem = { 841130809Smarcel "internal-error", AUTO_BOOLEAN_AUTO, AUTO_BOOLEAN_AUTO 842130809Smarcel}; 843130809Smarcel 844130809Smarcelvoid 845130809Smarcelinternal_vwarning (const char *file, int line, const char *fmt, va_list ap) 846130809Smarcel{ 847130809Smarcel internal_vproblem (&internal_warning_problem, file, line, fmt, ap); 848130809Smarcel} 849130809Smarcel 850130809Smarcelvoid 851130809Smarcelinternal_warning (const char *file, int line, const char *string, ...) 852130809Smarcel{ 853130809Smarcel va_list ap; 854130809Smarcel va_start (ap, string); 855130809Smarcel internal_vwarning (file, line, string, ap); 856130809Smarcel va_end (ap); 857130809Smarcel} 858130809Smarcel 85998948Sobrien/* The strerror() function can return NULL for errno values that are 86019370Spst out of range. Provide a "safe" version that always returns a 86119370Spst printable string. */ 86219370Spst 86319370Spstchar * 86498948Sobriensafe_strerror (int errnum) 86519370Spst{ 86619370Spst char *msg; 86719370Spst static char buf[32]; 86819370Spst 869130809Smarcel msg = strerror (errnum); 870130809Smarcel if (msg == NULL) 87119370Spst { 87298948Sobrien sprintf (buf, "(undocumented errno %d)", errnum); 87319370Spst msg = buf; 87419370Spst } 87519370Spst return (msg); 87619370Spst} 87719370Spst 87819370Spst/* Print the system error message for errno, and also mention STRING 87919370Spst as the file name for which the error was encountered. 88019370Spst Then return to command level. */ 88119370Spst 88246283SdfrNORETURN void 88398948Sobrienperror_with_name (const char *string) 88419370Spst{ 88519370Spst char *err; 88619370Spst char *combined; 88719370Spst 88819370Spst err = safe_strerror (errno); 88919370Spst combined = (char *) alloca (strlen (err) + strlen (string) + 3); 89019370Spst strcpy (combined, string); 89119370Spst strcat (combined, ": "); 89219370Spst strcat (combined, err); 89319370Spst 89419370Spst /* I understand setting these is a matter of taste. Still, some people 89519370Spst may clear errno but not know about bfd_error. Doing this here is not 89619370Spst unreasonable. */ 89719370Spst bfd_set_error (bfd_error_no_error); 89819370Spst errno = 0; 89919370Spst 90098948Sobrien error ("%s.", combined); 90119370Spst} 90219370Spst 90319370Spst/* Print the system error message for ERRCODE, and also mention STRING 90419370Spst as the file name for which the error was encountered. */ 90519370Spst 90619370Spstvoid 90798948Sobrienprint_sys_errmsg (const char *string, int errcode) 90819370Spst{ 90919370Spst char *err; 91019370Spst char *combined; 91119370Spst 91219370Spst err = safe_strerror (errcode); 91319370Spst combined = (char *) alloca (strlen (err) + strlen (string) + 3); 91419370Spst strcpy (combined, string); 91519370Spst strcat (combined, ": "); 91619370Spst strcat (combined, err); 91719370Spst 91819370Spst /* We want anything which was printed on stdout to come out first, before 91919370Spst this message. */ 92019370Spst gdb_flush (gdb_stdout); 92119370Spst fprintf_unfiltered (gdb_stderr, "%s.\n", combined); 92219370Spst} 92319370Spst 92419370Spst/* Control C eventually causes this to be called, at a convenient time. */ 92519370Spst 92619370Spstvoid 92798948Sobrienquit (void) 92819370Spst{ 92998948Sobrien struct serial *gdb_stdout_serial = serial_fdopen (1); 93019370Spst 93119370Spst target_terminal_ours (); 93219370Spst 93319370Spst /* We want all output to appear now, before we print "Quit". We 93419370Spst have 3 levels of buffering we have to flush (it's possible that 93519370Spst some of these should be changed to flush the lower-level ones 93619370Spst too): */ 93719370Spst 93819370Spst /* 1. The _filtered buffer. */ 93998948Sobrien wrap_here ((char *) 0); 94019370Spst 94119370Spst /* 2. The stdio buffer. */ 94219370Spst gdb_flush (gdb_stdout); 94319370Spst gdb_flush (gdb_stderr); 94419370Spst 94519370Spst /* 3. The system-level buffer. */ 94698948Sobrien serial_drain_output (gdb_stdout_serial); 94798948Sobrien serial_un_fdopen (gdb_stdout_serial); 94819370Spst 94919370Spst annotate_error_begin (); 95019370Spst 95119370Spst /* Don't use *_filtered; we don't want to prompt the user to continue. */ 95219370Spst if (quit_pre_print) 953130809Smarcel fputs_unfiltered (quit_pre_print, gdb_stderr); 95419370Spst 95598948Sobrien#ifdef __MSDOS__ 95698948Sobrien /* No steenking SIGINT will ever be coming our way when the 95798948Sobrien program is resumed. Don't lie. */ 95898948Sobrien fprintf_unfiltered (gdb_stderr, "Quit\n"); 95998948Sobrien#else 96019370Spst if (job_control 961130809Smarcel /* If there is no terminal switching for this target, then we can't 962130809Smarcel possibly get screwed by the lack of job control. */ 96319370Spst || current_target.to_terminal_ours == NULL) 96419370Spst fprintf_unfiltered (gdb_stderr, "Quit\n"); 96519370Spst else 96619370Spst fprintf_unfiltered (gdb_stderr, 967130809Smarcel "Quit (expect signal SIGINT when the program is resumed)\n"); 96898948Sobrien#endif 96998948Sobrien throw_exception (RETURN_QUIT); 97019370Spst} 97119370Spst 97219370Spst/* Control C comes here */ 97319370Spstvoid 97498948Sobrienrequest_quit (int signo) 97519370Spst{ 97619370Spst quit_flag = 1; 97719370Spst /* Restore the signal handler. Harmless with BSD-style signals, needed 97819370Spst for System V-style signals. So just always do it, rather than worrying 97919370Spst about USG defines and stuff like that. */ 98019370Spst signal (signo, request_quit); 98119370Spst 98298948Sobrien if (immediate_quit) 98319370Spst quit (); 98419370Spst} 98519370Spst 98619370Spst/* Memory management stuff (malloc friends). */ 98719370Spst 98898948Sobrienstatic void * 98998948Sobrienmmalloc (void *md, size_t size) 99019370Spst{ 991130809Smarcel return malloc (size); /* NOTE: GDB's only call to malloc() */ 99219370Spst} 99319370Spst 99498948Sobrienstatic void * 99598948Sobrienmrealloc (void *md, void *ptr, size_t size) 99619370Spst{ 99798948Sobrien if (ptr == 0) /* Guard against old realloc's */ 99898948Sobrien return mmalloc (md, size); 99919370Spst else 1000130809Smarcel return realloc (ptr, size); /* NOTE: GDB's only call to ralloc() */ 100119370Spst} 100219370Spst 100398948Sobrienstatic void * 100498948Sobrienmcalloc (void *md, size_t number, size_t size) 100519370Spst{ 1006130809Smarcel return calloc (number, size); /* NOTE: GDB's only call to calloc() */ 100719370Spst} 100819370Spst 100998948Sobrienstatic void 101098948Sobrienmfree (void *md, void *ptr) 101198948Sobrien{ 1012130809Smarcel free (ptr); /* NOTE: GDB's only call to free() */ 101398948Sobrien} 101419370Spst 1015130809Smarcel/* This used to do something interesting with USE_MMALLOC. 1016130809Smarcel * It can be retired any time. -- chastain 2004-01-19. */ 101719370Spstvoid 101898948Sobrieninit_malloc (void *md) 101919370Spst{ 102019370Spst} 102119370Spst 102219370Spst/* Called when a memory allocation fails, with the number of bytes of 102319370Spst memory requested in SIZE. */ 102419370Spst 102519370SpstNORETURN void 102698948Sobriennomem (long size) 102719370Spst{ 102819370Spst if (size > 0) 102919370Spst { 103098948Sobrien internal_error (__FILE__, __LINE__, 1031130809Smarcel "virtual memory exhausted: can't allocate %ld bytes.", 1032130809Smarcel size); 103319370Spst } 103419370Spst else 103519370Spst { 1036130809Smarcel internal_error (__FILE__, __LINE__, "virtual memory exhausted."); 103719370Spst } 103819370Spst} 103919370Spst 104098948Sobrien/* The xmmalloc() family of memory management routines. 104119370Spst 104298948Sobrien These are are like the mmalloc() family except that they implement 104398948Sobrien consistent semantics and guard against typical memory management 104498948Sobrien problems: if a malloc fails, an internal error is thrown; if 104598948Sobrien free(NULL) is called, it is ignored; if *alloc(0) is called, NULL 104698948Sobrien is returned. 104798948Sobrien 104898948Sobrien All these routines are implemented using the mmalloc() family. */ 104998948Sobrien 105098948Sobrienvoid * 105198948Sobrienxmmalloc (void *md, size_t size) 105219370Spst{ 105398948Sobrien void *val; 105419370Spst 1055130809Smarcel /* See libiberty/xmalloc.c. This function need's to match that's 1056130809Smarcel semantics. It never returns NULL. */ 105719370Spst if (size == 0) 1058130809Smarcel size = 1; 1059130809Smarcel 1060130809Smarcel val = mmalloc (md, size); 1061130809Smarcel if (val == NULL) 1062130809Smarcel nomem (size); 1063130809Smarcel 106419370Spst return (val); 106519370Spst} 106619370Spst 106798948Sobrienvoid * 106898948Sobrienxmrealloc (void *md, void *ptr, size_t size) 106919370Spst{ 107098948Sobrien void *val; 107119370Spst 1072130809Smarcel /* See libiberty/xmalloc.c. This function need's to match that's 1073130809Smarcel semantics. It never returns NULL. */ 107498948Sobrien if (size == 0) 1075130809Smarcel size = 1; 1076130809Smarcel 1077130809Smarcel if (ptr != NULL) 1078130809Smarcel val = mrealloc (md, ptr, size); 107919370Spst else 1080130809Smarcel val = mmalloc (md, size); 1081130809Smarcel if (val == NULL) 1082130809Smarcel nomem (size); 1083130809Smarcel 108498948Sobrien return (val); 108598948Sobrien} 108698948Sobrien 108798948Sobrienvoid * 108898948Sobrienxmcalloc (void *md, size_t number, size_t size) 108998948Sobrien{ 109098948Sobrien void *mem; 1091130809Smarcel 1092130809Smarcel /* See libiberty/xmalloc.c. This function need's to match that's 1093130809Smarcel semantics. It never returns NULL. */ 109498948Sobrien if (number == 0 || size == 0) 109519370Spst { 1096130809Smarcel number = 1; 1097130809Smarcel size = 1; 109819370Spst } 1099130809Smarcel 1100130809Smarcel mem = mcalloc (md, number, size); 1101130809Smarcel if (mem == NULL) 1102130809Smarcel nomem (number * size); 1103130809Smarcel 110498948Sobrien return mem; 110519370Spst} 110619370Spst 110798948Sobrienvoid 110898948Sobrienxmfree (void *md, void *ptr) 110998948Sobrien{ 111098948Sobrien if (ptr != NULL) 111198948Sobrien mfree (md, ptr); 111298948Sobrien} 111319370Spst 111498948Sobrien/* The xmalloc() (libiberty.h) family of memory management routines. 111598948Sobrien 111698948Sobrien These are like the ISO-C malloc() family except that they implement 111798948Sobrien consistent semantics and guard against typical memory management 111898948Sobrien problems. See xmmalloc() above for further information. 111998948Sobrien 112098948Sobrien All these routines are wrappers to the xmmalloc() family. */ 112198948Sobrien 112298948Sobrien/* NOTE: These are declared using PTR to ensure consistency with 112398948Sobrien "libiberty.h". xfree() is GDB local. */ 112498948Sobrien 1125130809SmarcelPTR /* OK: PTR */ 112698948Sobrienxmalloc (size_t size) 112719370Spst{ 112898948Sobrien return xmmalloc (NULL, size); 112919370Spst} 113019370Spst 1131130809SmarcelPTR /* OK: PTR */ 1132130809Smarcelxrealloc (PTR ptr, size_t size) /* OK: PTR */ 113398948Sobrien{ 113498948Sobrien return xmrealloc (NULL, ptr, size); 113598948Sobrien} 113619370Spst 1137130809SmarcelPTR /* OK: PTR */ 113898948Sobrienxcalloc (size_t number, size_t size) 113919370Spst{ 114098948Sobrien return xmcalloc (NULL, number, size); 114119370Spst} 114219370Spst 114398948Sobrienvoid 114498948Sobrienxfree (void *ptr) 114598948Sobrien{ 114698948Sobrien xmfree (NULL, ptr); 114798948Sobrien} 114819370Spst 114998948Sobrien 115098948Sobrien/* Like asprintf/vasprintf but get an internal_error if the call 115198948Sobrien fails. */ 115298948Sobrien 1153130809Smarcelchar * 1154130809Smarcelxstrprintf (const char *format, ...) 1155130809Smarcel{ 1156130809Smarcel char *ret; 1157130809Smarcel va_list args; 1158130809Smarcel va_start (args, format); 1159130809Smarcel xvasprintf (&ret, format, args); 1160130809Smarcel va_end (args); 1161130809Smarcel return ret; 1162130809Smarcel} 1163130809Smarcel 116498948Sobrienvoid 116598948Sobrienxasprintf (char **ret, const char *format, ...) 116698948Sobrien{ 116798948Sobrien va_list args; 116898948Sobrien va_start (args, format); 116998948Sobrien xvasprintf (ret, format, args); 117098948Sobrien va_end (args); 117198948Sobrien} 117298948Sobrien 117398948Sobrienvoid 117498948Sobrienxvasprintf (char **ret, const char *format, va_list ap) 117598948Sobrien{ 117698948Sobrien int status = vasprintf (ret, format, ap); 117798948Sobrien /* NULL could be returned due to a memory allocation problem; a 117898948Sobrien badly format string; or something else. */ 117998948Sobrien if ((*ret) == NULL) 118098948Sobrien internal_error (__FILE__, __LINE__, 1181130809Smarcel "vasprintf returned NULL buffer (errno %d)", errno); 118298948Sobrien /* A negative status with a non-NULL buffer shouldn't never 118398948Sobrien happen. But to be sure. */ 118498948Sobrien if (status < 0) 118598948Sobrien internal_error (__FILE__, __LINE__, 1186130809Smarcel "vasprintf call failed (errno %d)", errno); 118798948Sobrien} 118898948Sobrien 118998948Sobrien 119019370Spst/* My replacement for the read system call. 119119370Spst Used like `read' but keeps going if `read' returns too soon. */ 119219370Spst 119319370Spstint 119498948Sobrienmyread (int desc, char *addr, int len) 119519370Spst{ 1196130809Smarcel int val; 119719370Spst int orglen = len; 119819370Spst 119919370Spst while (len > 0) 120019370Spst { 120119370Spst val = read (desc, addr, len); 120219370Spst if (val < 0) 120319370Spst return val; 120419370Spst if (val == 0) 120519370Spst return orglen - len; 120619370Spst len -= val; 120719370Spst addr += val; 120819370Spst } 120919370Spst return orglen; 121019370Spst} 121119370Spst 121219370Spst/* Make a copy of the string at PTR with SIZE characters 121319370Spst (and add a null character at the end in the copy). 121419370Spst Uses malloc to get the space. Returns the address of the copy. */ 121519370Spst 121619370Spstchar * 121798948Sobriensavestring (const char *ptr, size_t size) 121819370Spst{ 1219130809Smarcel char *p = (char *) xmalloc (size + 1); 122019370Spst memcpy (p, ptr, size); 122119370Spst p[size] = 0; 122219370Spst return p; 122319370Spst} 122419370Spst 122519370Spstchar * 122698948Sobrienmsavestring (void *md, const char *ptr, size_t size) 122719370Spst{ 1228130809Smarcel char *p = (char *) xmmalloc (md, size + 1); 122919370Spst memcpy (p, ptr, size); 123019370Spst p[size] = 0; 123119370Spst return p; 123219370Spst} 123319370Spst 123419370Spstchar * 123598948Sobrienmstrsave (void *md, const char *ptr) 123619370Spst{ 123719370Spst return (msavestring (md, ptr, strlen (ptr))); 123819370Spst} 123919370Spst 124019370Spstvoid 1241130809Smarcelprint_spaces (int n, struct ui_file *file) 124219370Spst{ 124398948Sobrien fputs_unfiltered (n_spaces (n), file); 124419370Spst} 124519370Spst 124619370Spst/* Print a host address. */ 124719370Spst 124819370Spstvoid 1249130809Smarcelgdb_print_host_address (const void *addr, struct ui_file *stream) 125019370Spst{ 125119370Spst 125219370Spst /* We could use the %p conversion specifier to fprintf if we had any 125319370Spst way of knowing whether this host supports it. But the following 125419370Spst should work on the Alpha and on 32 bit machines. */ 125519370Spst 125698948Sobrien fprintf_filtered (stream, "0x%lx", (unsigned long) addr); 125719370Spst} 125819370Spst 125919370Spst/* Ask user a y-or-n question and return 1 iff answer is yes. 126019370Spst Takes three args which are given to printf to print the question. 126119370Spst The first, a control string, should end in "? ". 126219370Spst It should not say how to answer, because we do that. */ 126319370Spst 126419370Spst/* VARARGS */ 126519370Spstint 1266130809Smarcelquery (const char *ctlstr, ...) 126719370Spst{ 126819370Spst va_list args; 1269130809Smarcel int answer; 1270130809Smarcel int ans2; 127119370Spst int retval; 127219370Spst 127319370Spst va_start (args, ctlstr); 127419370Spst 127519370Spst if (query_hook) 127619370Spst { 127719370Spst return query_hook (ctlstr, args); 127819370Spst } 127919370Spst 128019370Spst /* Automatically answer "yes" if input is not from a terminal. */ 128119370Spst if (!input_from_terminal_p ()) 128219370Spst return 1; 128319370Spst 128419370Spst while (1) 128519370Spst { 128619370Spst wrap_here (""); /* Flush any buffered output */ 128719370Spst gdb_flush (gdb_stdout); 128819370Spst 128919370Spst if (annotation_level > 1) 129019370Spst printf_filtered ("\n\032\032pre-query\n"); 129119370Spst 129219370Spst vfprintf_filtered (gdb_stdout, ctlstr, args); 129319370Spst printf_filtered ("(y or n) "); 129419370Spst 129519370Spst if (annotation_level > 1) 129619370Spst printf_filtered ("\n\032\032query\n"); 129719370Spst 129898948Sobrien wrap_here (""); 129919370Spst gdb_flush (gdb_stdout); 130046283Sdfr 130198948Sobrien answer = fgetc (stdin); 130219370Spst clearerr (stdin); /* in case of C-d */ 130319370Spst if (answer == EOF) /* C-d */ 130498948Sobrien { 130519370Spst retval = 1; 130619370Spst break; 130719370Spst } 130846283Sdfr /* Eat rest of input line, to EOF or newline */ 130998948Sobrien if (answer != '\n') 131098948Sobrien do 131119370Spst { 1312130809Smarcel ans2 = fgetc (stdin); 131319370Spst clearerr (stdin); 131419370Spst } 131598948Sobrien while (ans2 != EOF && ans2 != '\n' && ans2 != '\r'); 131646283Sdfr 131719370Spst if (answer >= 'a') 131819370Spst answer -= 040; 131919370Spst if (answer == 'Y') 132019370Spst { 132119370Spst retval = 1; 132219370Spst break; 132319370Spst } 132419370Spst if (answer == 'N') 132519370Spst { 132619370Spst retval = 0; 132719370Spst break; 132819370Spst } 132919370Spst printf_filtered ("Please answer y or n.\n"); 133019370Spst } 133119370Spst 133219370Spst if (annotation_level > 1) 133319370Spst printf_filtered ("\n\032\032post-query\n"); 133419370Spst return retval; 133519370Spst} 133698948Sobrien 133719370Spst 1338130809Smarcel/* This function supports the nquery() and yquery() functions. 1339130809Smarcel Ask user a y-or-n question and return 0 if answer is no, 1 if 1340130809Smarcel answer is yes, or default the answer to the specified default. 1341130809Smarcel DEFCHAR is either 'y' or 'n' and refers to the default answer. 1342130809Smarcel CTLSTR is the control string and should end in "? ". It should 1343130809Smarcel not say how to answer, because we do that. 1344130809Smarcel ARGS are the arguments passed along with the CTLSTR argument to 1345130809Smarcel printf. */ 1346130809Smarcel 1347130809Smarcelstatic int 1348130809Smarceldefaulted_query (const char *ctlstr, const char defchar, va_list args) 1349130809Smarcel{ 1350130809Smarcel int answer; 1351130809Smarcel int ans2; 1352130809Smarcel int retval; 1353130809Smarcel int def_value; 1354130809Smarcel char def_answer, not_def_answer; 1355130809Smarcel char *y_string, *n_string; 1356130809Smarcel 1357130809Smarcel /* Set up according to which answer is the default. */ 1358130809Smarcel if (defchar == 'y') 1359130809Smarcel { 1360130809Smarcel def_value = 1; 1361130809Smarcel def_answer = 'Y'; 1362130809Smarcel not_def_answer = 'N'; 1363130809Smarcel y_string = "[y]"; 1364130809Smarcel n_string = "n"; 1365130809Smarcel } 1366130809Smarcel else 1367130809Smarcel { 1368130809Smarcel def_value = 0; 1369130809Smarcel def_answer = 'N'; 1370130809Smarcel not_def_answer = 'Y'; 1371130809Smarcel y_string = "y"; 1372130809Smarcel n_string = "[n]"; 1373130809Smarcel } 1374130809Smarcel 1375130809Smarcel if (query_hook) 1376130809Smarcel { 1377130809Smarcel return query_hook (ctlstr, args); 1378130809Smarcel } 1379130809Smarcel 1380130809Smarcel /* Automatically answer default value if input is not from a terminal. */ 1381130809Smarcel if (!input_from_terminal_p ()) 1382130809Smarcel return def_value; 1383130809Smarcel 1384130809Smarcel while (1) 1385130809Smarcel { 1386130809Smarcel wrap_here (""); /* Flush any buffered output */ 1387130809Smarcel gdb_flush (gdb_stdout); 1388130809Smarcel 1389130809Smarcel if (annotation_level > 1) 1390130809Smarcel printf_filtered ("\n\032\032pre-query\n"); 1391130809Smarcel 1392130809Smarcel vfprintf_filtered (gdb_stdout, ctlstr, args); 1393130809Smarcel printf_filtered ("(%s or %s) ", y_string, n_string); 1394130809Smarcel 1395130809Smarcel if (annotation_level > 1) 1396130809Smarcel printf_filtered ("\n\032\032query\n"); 1397130809Smarcel 1398130809Smarcel wrap_here (""); 1399130809Smarcel gdb_flush (gdb_stdout); 1400130809Smarcel 1401130809Smarcel answer = fgetc (stdin); 1402130809Smarcel clearerr (stdin); /* in case of C-d */ 1403130809Smarcel if (answer == EOF) /* C-d */ 1404130809Smarcel { 1405130809Smarcel retval = def_value; 1406130809Smarcel break; 1407130809Smarcel } 1408130809Smarcel /* Eat rest of input line, to EOF or newline */ 1409130809Smarcel if (answer != '\n') 1410130809Smarcel do 1411130809Smarcel { 1412130809Smarcel ans2 = fgetc (stdin); 1413130809Smarcel clearerr (stdin); 1414130809Smarcel } 1415130809Smarcel while (ans2 != EOF && ans2 != '\n' && ans2 != '\r'); 1416130809Smarcel 1417130809Smarcel if (answer >= 'a') 1418130809Smarcel answer -= 040; 1419130809Smarcel /* Check answer. For the non-default, the user must specify 1420130809Smarcel the non-default explicitly. */ 1421130809Smarcel if (answer == not_def_answer) 1422130809Smarcel { 1423130809Smarcel retval = !def_value; 1424130809Smarcel break; 1425130809Smarcel } 1426130809Smarcel /* Otherwise, for the default, the user may either specify 1427130809Smarcel the required input or have it default by entering nothing. */ 1428130809Smarcel if (answer == def_answer || answer == '\n' || 1429130809Smarcel answer == '\r' || answer == EOF) 1430130809Smarcel { 1431130809Smarcel retval = def_value; 1432130809Smarcel break; 1433130809Smarcel } 1434130809Smarcel /* Invalid entries are not defaulted and require another selection. */ 1435130809Smarcel printf_filtered ("Please answer %s or %s.\n", 1436130809Smarcel y_string, n_string); 1437130809Smarcel } 1438130809Smarcel 1439130809Smarcel if (annotation_level > 1) 1440130809Smarcel printf_filtered ("\n\032\032post-query\n"); 1441130809Smarcel return retval; 1442130809Smarcel} 1443130809Smarcel 1444130809Smarcel 1445130809Smarcel/* Ask user a y-or-n question and return 0 if answer is no, 1 if 1446130809Smarcel answer is yes, or 0 if answer is defaulted. 1447130809Smarcel Takes three args which are given to printf to print the question. 1448130809Smarcel The first, a control string, should end in "? ". 1449130809Smarcel It should not say how to answer, because we do that. */ 1450130809Smarcel 1451130809Smarcelint 1452130809Smarcelnquery (const char *ctlstr, ...) 1453130809Smarcel{ 1454130809Smarcel va_list args; 1455130809Smarcel 1456130809Smarcel va_start (args, ctlstr); 1457130809Smarcel return defaulted_query (ctlstr, 'n', args); 1458130809Smarcel va_end (args); 1459130809Smarcel} 1460130809Smarcel 1461130809Smarcel/* Ask user a y-or-n question and return 0 if answer is no, 1 if 1462130809Smarcel answer is yes, or 1 if answer is defaulted. 1463130809Smarcel Takes three args which are given to printf to print the question. 1464130809Smarcel The first, a control string, should end in "? ". 1465130809Smarcel It should not say how to answer, because we do that. */ 1466130809Smarcel 1467130809Smarcelint 1468130809Smarcelyquery (const char *ctlstr, ...) 1469130809Smarcel{ 1470130809Smarcel va_list args; 1471130809Smarcel 1472130809Smarcel va_start (args, ctlstr); 1473130809Smarcel return defaulted_query (ctlstr, 'y', args); 1474130809Smarcel va_end (args); 1475130809Smarcel} 1476130809Smarcel 1477130809Smarcel/* Print an error message saying that we couldn't make sense of a 1478130809Smarcel \^mumble sequence in a string or character constant. START and END 1479130809Smarcel indicate a substring of some larger string that contains the 1480130809Smarcel erroneous backslash sequence, missing the initial backslash. */ 1481130809Smarcelstatic NORETURN int 1482130809Smarcelno_control_char_error (const char *start, const char *end) 1483130809Smarcel{ 1484130809Smarcel int len = end - start; 1485130809Smarcel char *copy = alloca (end - start + 1); 1486130809Smarcel 1487130809Smarcel memcpy (copy, start, len); 1488130809Smarcel copy[len] = '\0'; 1489130809Smarcel 1490130809Smarcel error ("There is no control character `\\%s' in the `%s' character set.", 1491130809Smarcel copy, target_charset ()); 1492130809Smarcel} 1493130809Smarcel 149419370Spst/* Parse a C escape sequence. STRING_PTR points to a variable 149519370Spst containing a pointer to the string to parse. That pointer 149619370Spst should point to the character after the \. That pointer 149719370Spst is updated past the characters we use. The value of the 149819370Spst escape sequence is returned. 149919370Spst 150019370Spst A negative value means the sequence \ newline was seen, 150119370Spst which is supposed to be equivalent to nothing at all. 150219370Spst 150319370Spst If \ is followed by a null character, we return a negative 150419370Spst value and leave the string pointer pointing at the null character. 150519370Spst 150619370Spst If \ is followed by 000, we return 0 and leave the string pointer 150719370Spst after the zeros. A value of 0 does not mean end of string. */ 150819370Spst 150919370Spstint 151098948Sobrienparse_escape (char **string_ptr) 151119370Spst{ 1512130809Smarcel int target_char; 1513130809Smarcel int c = *(*string_ptr)++; 1514130809Smarcel if (c_parse_backslash (c, &target_char)) 1515130809Smarcel return target_char; 1516130809Smarcel else 1517130809Smarcel switch (c) 1518130809Smarcel { 1519130809Smarcel case '\n': 1520130809Smarcel return -2; 1521130809Smarcel case 0: 1522130809Smarcel (*string_ptr)--; 1523130809Smarcel return 0; 1524130809Smarcel case '^': 1525130809Smarcel { 1526130809Smarcel /* Remember where this escape sequence started, for reporting 1527130809Smarcel errors. */ 1528130809Smarcel char *sequence_start_pos = *string_ptr - 1; 152998948Sobrien 1530130809Smarcel c = *(*string_ptr)++; 1531130809Smarcel 1532130809Smarcel if (c == '?') 1533130809Smarcel { 1534130809Smarcel /* XXXCHARSET: What is `delete' in the host character set? */ 1535130809Smarcel c = 0177; 1536130809Smarcel 1537130809Smarcel if (!host_char_to_target (c, &target_char)) 1538130809Smarcel error ("There is no character corresponding to `Delete' " 1539130809Smarcel "in the target character set `%s'.", host_charset ()); 1540130809Smarcel 1541130809Smarcel return target_char; 1542130809Smarcel } 1543130809Smarcel else if (c == '\\') 1544130809Smarcel target_char = parse_escape (string_ptr); 1545130809Smarcel else 1546130809Smarcel { 1547130809Smarcel if (!host_char_to_target (c, &target_char)) 1548130809Smarcel no_control_char_error (sequence_start_pos, *string_ptr); 1549130809Smarcel } 1550130809Smarcel 1551130809Smarcel /* Now target_char is something like `c', and we want to find 1552130809Smarcel its control-character equivalent. */ 1553130809Smarcel if (!target_char_to_control_char (target_char, &target_char)) 1554130809Smarcel no_control_char_error (sequence_start_pos, *string_ptr); 1555130809Smarcel 1556130809Smarcel return target_char; 1557130809Smarcel } 1558130809Smarcel 1559130809Smarcel /* XXXCHARSET: we need to use isdigit and value-of-digit 1560130809Smarcel methods of the host character set here. */ 1561130809Smarcel 1562130809Smarcel case '0': 1563130809Smarcel case '1': 1564130809Smarcel case '2': 1565130809Smarcel case '3': 1566130809Smarcel case '4': 1567130809Smarcel case '5': 1568130809Smarcel case '6': 1569130809Smarcel case '7': 1570130809Smarcel { 1571130809Smarcel int i = c - '0'; 1572130809Smarcel int count = 0; 1573130809Smarcel while (++count < 3) 1574130809Smarcel { 1575130809Smarcel c = (**string_ptr); 1576130809Smarcel if (c >= '0' && c <= '7') 1577130809Smarcel { 1578130809Smarcel (*string_ptr)++; 1579130809Smarcel i *= 8; 1580130809Smarcel i += c - '0'; 1581130809Smarcel } 1582130809Smarcel else 1583130809Smarcel { 1584130809Smarcel break; 1585130809Smarcel } 1586130809Smarcel } 1587130809Smarcel return i; 1588130809Smarcel } 1589130809Smarcel default: 1590130809Smarcel if (!host_char_to_target (c, &target_char)) 1591130809Smarcel error 1592130809Smarcel ("The escape sequence `\%c' is equivalent to plain `%c', which" 1593130809Smarcel " has no equivalent\n" "in the `%s' character set.", c, c, 1594130809Smarcel target_charset ()); 1595130809Smarcel return target_char; 159619370Spst } 159719370Spst} 159819370Spst 159919370Spst/* Print the character C on STREAM as part of the contents of a literal 160019370Spst string whose delimiter is QUOTER. Note that this routine should only 160119370Spst be call for printing things which are independent of the language 160219370Spst of the program being debugged. */ 160319370Spst 160498948Sobrienstatic void 160598948Sobrienprintchar (int c, void (*do_fputs) (const char *, struct ui_file *), 160698948Sobrien void (*do_fprintf) (struct ui_file *, const char *, ...), 160798948Sobrien struct ui_file *stream, int quoter) 160819370Spst{ 160919370Spst 161019370Spst c &= 0xFF; /* Avoid sign bit follies */ 161119370Spst 161298948Sobrien if (c < 0x20 || /* Low control chars */ 161398948Sobrien (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ 161498948Sobrien (sevenbit_strings && c >= 0x80)) 161598948Sobrien { /* high order bit set */ 161698948Sobrien switch (c) 161798948Sobrien { 161898948Sobrien case '\n': 161998948Sobrien do_fputs ("\\n", stream); 162098948Sobrien break; 162198948Sobrien case '\b': 162298948Sobrien do_fputs ("\\b", stream); 162398948Sobrien break; 162498948Sobrien case '\t': 162598948Sobrien do_fputs ("\\t", stream); 162698948Sobrien break; 162798948Sobrien case '\f': 162898948Sobrien do_fputs ("\\f", stream); 162998948Sobrien break; 163098948Sobrien case '\r': 163198948Sobrien do_fputs ("\\r", stream); 163298948Sobrien break; 163398948Sobrien case '\033': 163498948Sobrien do_fputs ("\\e", stream); 163598948Sobrien break; 163698948Sobrien case '\007': 163798948Sobrien do_fputs ("\\a", stream); 163898948Sobrien break; 163998948Sobrien default: 164098948Sobrien do_fprintf (stream, "\\%.3o", (unsigned int) c); 164198948Sobrien break; 164298948Sobrien } 164398948Sobrien } 164498948Sobrien else 164598948Sobrien { 164698948Sobrien if (c == '\\' || c == quoter) 164798948Sobrien do_fputs ("\\", stream); 164898948Sobrien do_fprintf (stream, "%c", c); 164998948Sobrien } 165019370Spst} 165146283Sdfr 165298948Sobrien/* Print the character C on STREAM as part of the contents of a 165398948Sobrien literal string whose delimiter is QUOTER. Note that these routines 165498948Sobrien should only be call for printing things which are independent of 165598948Sobrien the language of the program being debugged. */ 165646283Sdfr 165798948Sobrienvoid 165898948Sobrienfputstr_filtered (const char *str, int quoter, struct ui_file *stream) 165998948Sobrien{ 166098948Sobrien while (*str) 166198948Sobrien printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter); 166298948Sobrien} 166346283Sdfr 166498948Sobrienvoid 166598948Sobrienfputstr_unfiltered (const char *str, int quoter, struct ui_file *stream) 166698948Sobrien{ 166798948Sobrien while (*str) 166898948Sobrien printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter); 166998948Sobrien} 167046283Sdfr 167198948Sobrienvoid 1672130809Smarcelfputstrn_unfiltered (const char *str, int n, int quoter, 1673130809Smarcel struct ui_file *stream) 167446283Sdfr{ 167598948Sobrien int i; 167698948Sobrien for (i = 0; i < n; i++) 167798948Sobrien printchar (str[i], fputs_unfiltered, fprintf_unfiltered, stream, quoter); 167846283Sdfr} 167919370Spst 168098948Sobrien 168119370Spst/* Number of lines per page or UINT_MAX if paging is disabled. */ 168219370Spststatic unsigned int lines_per_page; 1683130809Smarcel 168498948Sobrien/* Number of chars per line or UINT_MAX if line folding is disabled. */ 168519370Spststatic unsigned int chars_per_line; 1686130809Smarcel 168719370Spst/* Current count of lines printed on this page, chars on this line. */ 168819370Spststatic unsigned int lines_printed, chars_printed; 168919370Spst 169019370Spst/* Buffer and start column of buffered text, for doing smarter word- 169119370Spst wrapping. When someone calls wrap_here(), we start buffering output 169219370Spst that comes through fputs_filtered(). If we see a newline, we just 169319370Spst spit it out and forget about the wrap_here(). If we see another 169419370Spst wrap_here(), we spit it out and remember the newer one. If we see 169519370Spst the end of the line, we spit out a newline, the indent, and then 169619370Spst the buffered output. */ 169719370Spst 169819370Spst/* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which 169919370Spst are waiting to be output (they have already been counted in chars_printed). 170019370Spst When wrap_buffer[0] is null, the buffer is empty. */ 170119370Spststatic char *wrap_buffer; 170219370Spst 170319370Spst/* Pointer in wrap_buffer to the next character to fill. */ 170419370Spststatic char *wrap_pointer; 170519370Spst 170619370Spst/* String to indent by if the wrap occurs. Must not be NULL if wrap_column 170719370Spst is non-zero. */ 170819370Spststatic char *wrap_indent; 170919370Spst 171019370Spst/* Column number on the screen where wrap_buffer begins, or 0 if wrapping 171119370Spst is not in effect. */ 171219370Spststatic int wrap_column; 171398948Sobrien 171419370Spst 1715130809Smarcel/* Inialize the number of lines per page and chars per line. */ 1716130809Smarcel 171746283Sdfrvoid 171898948Sobrieninit_page_info (void) 171919370Spst{ 172046283Sdfr#if defined(TUI) 172198948Sobrien if (!tui_get_command_dimension (&chars_per_line, &lines_per_page)) 172246283Sdfr#endif 172346283Sdfr { 1724130809Smarcel int rows, cols; 1725130809Smarcel 172646283Sdfr#if defined(__GO32__) 1727130809Smarcel rows = ScreenRows (); 1728130809Smarcel cols = ScreenCols (); 1729130809Smarcel lines_per_page = rows; 1730130809Smarcel chars_per_line = cols; 173198948Sobrien#else 1732130809Smarcel /* Make sure Readline has initialized its terminal settings. */ 1733130809Smarcel rl_reset_terminal (NULL); 173446283Sdfr 1735130809Smarcel /* Get the screen size from Readline. */ 1736130809Smarcel rl_get_screen_size (&rows, &cols); 1737130809Smarcel lines_per_page = rows; 1738130809Smarcel chars_per_line = cols; 173946283Sdfr 1740130809Smarcel /* Readline should have fetched the termcap entry for us. */ 1741130809Smarcel if (tgetnum ("li") < 0 || getenv ("EMACS")) 1742130809Smarcel { 1743130809Smarcel /* The number of lines per page is not mentioned in the 1744130809Smarcel terminal description. This probably means that paging is 1745130809Smarcel not useful (e.g. emacs shell window), so disable paging. */ 1746130809Smarcel lines_per_page = UINT_MAX; 1747130809Smarcel } 174846283Sdfr 1749130809Smarcel /* FIXME: Get rid of this junk. */ 175046283Sdfr#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) 175146283Sdfr SIGWINCH_HANDLER (SIGWINCH); 175246283Sdfr#endif 1753130809Smarcel 175446283Sdfr /* If the output is not a terminal, don't paginate it. */ 175598948Sobrien if (!ui_file_isatty (gdb_stdout)) 175698948Sobrien lines_per_page = UINT_MAX; 1757130809Smarcel#endif 1758130809Smarcel } 1759130809Smarcel 1760130809Smarcel set_screen_size (); 176198948Sobrien set_width (); 176246283Sdfr} 176346283Sdfr 1764130809Smarcel/* Set the screen size based on LINES_PER_PAGE and CHARS_PER_LINE. */ 1765130809Smarcel 176646283Sdfrstatic void 1767130809Smarcelset_screen_size (void) 1768130809Smarcel{ 1769130809Smarcel int rows = lines_per_page; 1770130809Smarcel int cols = chars_per_line; 1771130809Smarcel 1772130809Smarcel if (rows <= 0) 1773130809Smarcel rows = INT_MAX; 1774130809Smarcel 1775130809Smarcel if (cols <= 0) 1776130809Smarcel rl_get_screen_size (NULL, &cols); 1777130809Smarcel 1778130809Smarcel /* Update Readline's idea of the terminal size. */ 1779130809Smarcel rl_set_screen_size (rows, cols); 1780130809Smarcel} 1781130809Smarcel 1782130809Smarcel/* Reinitialize WRAP_BUFFER according to the current value of 1783130809Smarcel CHARS_PER_LINE. */ 1784130809Smarcel 1785130809Smarcelstatic void 178698948Sobrienset_width (void) 178746283Sdfr{ 178846283Sdfr if (chars_per_line == 0) 178998948Sobrien init_page_info (); 179046283Sdfr 179119370Spst if (!wrap_buffer) 179219370Spst { 179319370Spst wrap_buffer = (char *) xmalloc (chars_per_line + 2); 179419370Spst wrap_buffer[0] = '\0'; 179519370Spst } 179619370Spst else 179719370Spst wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); 1798130809Smarcel wrap_pointer = wrap_buffer; /* Start it at the beginning. */ 179919370Spst} 180019370Spst 180198948Sobrienstatic void 180298948Sobrienset_width_command (char *args, int from_tty, struct cmd_list_element *c) 180346283Sdfr{ 1804130809Smarcel set_screen_size (); 180546283Sdfr set_width (); 180646283Sdfr} 180746283Sdfr 1808130809Smarcelstatic void 1809130809Smarcelset_height_command (char *args, int from_tty, struct cmd_list_element *c) 1810130809Smarcel{ 1811130809Smarcel set_screen_size (); 1812130809Smarcel} 1813130809Smarcel 181419370Spst/* Wait, so the user can read what's on the screen. Prompt the user 181519370Spst to continue by pressing RETURN. */ 181619370Spst 181719370Spststatic void 181898948Sobrienprompt_for_continue (void) 181919370Spst{ 182019370Spst char *ignore; 182119370Spst char cont_prompt[120]; 182219370Spst 182319370Spst if (annotation_level > 1) 182419370Spst printf_unfiltered ("\n\032\032pre-prompt-for-continue\n"); 182519370Spst 182619370Spst strcpy (cont_prompt, 182719370Spst "---Type <return> to continue, or q <return> to quit---"); 182819370Spst if (annotation_level > 1) 182919370Spst strcat (cont_prompt, "\n\032\032prompt-for-continue\n"); 183019370Spst 183119370Spst /* We must do this *before* we call gdb_readline, else it will eventually 183219370Spst call us -- thinking that we're trying to print beyond the end of the 183319370Spst screen. */ 183419370Spst reinitialize_more_filter (); 183519370Spst 183619370Spst immediate_quit++; 183719370Spst /* On a real operating system, the user can quit with SIGINT. 183819370Spst But not on GO32. 183919370Spst 184019370Spst 'q' is provided on all systems so users don't have to change habits 184119370Spst from system to system, and because telling them what to do in 184219370Spst the prompt is more user-friendly than expecting them to think of 184319370Spst SIGINT. */ 184419370Spst /* Call readline, not gdb_readline, because GO32 readline handles control-C 184519370Spst whereas control-C to gdb_readline will cause the user to get dumped 184619370Spst out to DOS. */ 1847130809Smarcel ignore = gdb_readline_wrapper (cont_prompt); 184819370Spst 184919370Spst if (annotation_level > 1) 185019370Spst printf_unfiltered ("\n\032\032post-prompt-for-continue\n"); 185119370Spst 185219370Spst if (ignore) 185319370Spst { 185419370Spst char *p = ignore; 185519370Spst while (*p == ' ' || *p == '\t') 185619370Spst ++p; 185719370Spst if (p[0] == 'q') 185898948Sobrien { 185998948Sobrien if (!event_loop_p) 186098948Sobrien request_quit (SIGINT); 186198948Sobrien else 186298948Sobrien async_request_quit (0); 186398948Sobrien } 186498948Sobrien xfree (ignore); 186519370Spst } 186619370Spst immediate_quit--; 186719370Spst 186819370Spst /* Now we have to do this again, so that GDB will know that it doesn't 186919370Spst need to save the ---Type <return>--- line at the top of the screen. */ 187019370Spst reinitialize_more_filter (); 187119370Spst 187219370Spst dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ 187319370Spst} 187419370Spst 187519370Spst/* Reinitialize filter; ie. tell it to reset to original values. */ 187619370Spst 187719370Spstvoid 187898948Sobrienreinitialize_more_filter (void) 187919370Spst{ 188019370Spst lines_printed = 0; 188119370Spst chars_printed = 0; 188219370Spst} 188319370Spst 188419370Spst/* Indicate that if the next sequence of characters overflows the line, 188519370Spst a newline should be inserted here rather than when it hits the end. 188619370Spst If INDENT is non-null, it is a string to be printed to indent the 188719370Spst wrapped part on the next line. INDENT must remain accessible until 188819370Spst the next call to wrap_here() or until a newline is printed through 188919370Spst fputs_filtered(). 189019370Spst 189119370Spst If the line is already overfull, we immediately print a newline and 189219370Spst the indentation, and disable further wrapping. 189319370Spst 189419370Spst If we don't know the width of lines, but we know the page height, 189519370Spst we must not wrap words, but should still keep track of newlines 189619370Spst that were explicitly printed. 189719370Spst 189819370Spst INDENT should not contain tabs, as that will mess up the char count 189919370Spst on the next line. FIXME. 190019370Spst 190119370Spst This routine is guaranteed to force out any output which has been 190219370Spst squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be 190319370Spst used to force out output from the wrap_buffer. */ 190419370Spst 190519370Spstvoid 190698948Sobrienwrap_here (char *indent) 190719370Spst{ 190819370Spst /* This should have been allocated, but be paranoid anyway. */ 190919370Spst if (!wrap_buffer) 191098948Sobrien internal_error (__FILE__, __LINE__, "failed internal consistency check"); 191119370Spst 191219370Spst if (wrap_buffer[0]) 191319370Spst { 191419370Spst *wrap_pointer = '\0'; 191519370Spst fputs_unfiltered (wrap_buffer, gdb_stdout); 191619370Spst } 191719370Spst wrap_pointer = wrap_buffer; 191819370Spst wrap_buffer[0] = '\0'; 191998948Sobrien if (chars_per_line == UINT_MAX) /* No line overflow checking */ 192019370Spst { 192119370Spst wrap_column = 0; 192219370Spst } 192319370Spst else if (chars_printed >= chars_per_line) 192419370Spst { 192519370Spst puts_filtered ("\n"); 192619370Spst if (indent != NULL) 192719370Spst puts_filtered (indent); 192819370Spst wrap_column = 0; 192919370Spst } 193019370Spst else 193119370Spst { 193219370Spst wrap_column = chars_printed; 193319370Spst if (indent == NULL) 193419370Spst wrap_indent = ""; 193519370Spst else 193619370Spst wrap_indent = indent; 193719370Spst } 193819370Spst} 193919370Spst 1940130809Smarcel/* Print input string to gdb_stdout, filtered, with wrap, 1941130809Smarcel arranging strings in columns of n chars. String can be 1942130809Smarcel right or left justified in the column. Never prints 1943130809Smarcel trailing spaces. String should never be longer than 1944130809Smarcel width. FIXME: this could be useful for the EXAMINE 1945130809Smarcel command, which currently doesn't tabulate very well */ 1946130809Smarcel 1947130809Smarcelvoid 1948130809Smarcelputs_filtered_tabular (char *string, int width, int right) 1949130809Smarcel{ 1950130809Smarcel int spaces = 0; 1951130809Smarcel int stringlen; 1952130809Smarcel char *spacebuf; 1953130809Smarcel 1954130809Smarcel gdb_assert (chars_per_line > 0); 1955130809Smarcel if (chars_per_line == UINT_MAX) 1956130809Smarcel { 1957130809Smarcel fputs_filtered (string, gdb_stdout); 1958130809Smarcel fputs_filtered ("\n", gdb_stdout); 1959130809Smarcel return; 1960130809Smarcel } 1961130809Smarcel 1962130809Smarcel if (((chars_printed - 1) / width + 2) * width >= chars_per_line) 1963130809Smarcel fputs_filtered ("\n", gdb_stdout); 1964130809Smarcel 1965130809Smarcel if (width >= chars_per_line) 1966130809Smarcel width = chars_per_line - 1; 1967130809Smarcel 1968130809Smarcel stringlen = strlen (string); 1969130809Smarcel 1970130809Smarcel if (chars_printed > 0) 1971130809Smarcel spaces = width - (chars_printed - 1) % width - 1; 1972130809Smarcel if (right) 1973130809Smarcel spaces += width - stringlen; 1974130809Smarcel 1975130809Smarcel spacebuf = alloca (spaces + 1); 1976130809Smarcel spacebuf[spaces] = '\0'; 1977130809Smarcel while (spaces--) 1978130809Smarcel spacebuf[spaces] = ' '; 1979130809Smarcel 1980130809Smarcel fputs_filtered (spacebuf, gdb_stdout); 1981130809Smarcel fputs_filtered (string, gdb_stdout); 1982130809Smarcel} 1983130809Smarcel 1984130809Smarcel 198519370Spst/* Ensure that whatever gets printed next, using the filtered output 198619370Spst commands, starts at the beginning of the line. I.E. if there is 198719370Spst any pending output for the current line, flush it and start a new 198819370Spst line. Otherwise do nothing. */ 198919370Spst 199019370Spstvoid 199198948Sobrienbegin_line (void) 199219370Spst{ 199319370Spst if (chars_printed > 0) 199419370Spst { 199519370Spst puts_filtered ("\n"); 199619370Spst } 199719370Spst} 199819370Spst 199919370Spst 200019370Spst/* Like fputs but if FILTER is true, pause after every screenful. 200119370Spst 200219370Spst Regardless of FILTER can wrap at points other than the final 200319370Spst character of a line. 200419370Spst 200519370Spst Unlike fputs, fputs_maybe_filtered does not return a value. 200619370Spst It is OK for LINEBUFFER to be NULL, in which case just don't print 200719370Spst anything. 200819370Spst 200919370Spst Note that a longjmp to top level may occur in this routine (only if 201019370Spst FILTER is true) (since prompt_for_continue may do so) so this 201119370Spst routine should not be called when cleanups are not in place. */ 201219370Spst 201319370Spststatic void 201498948Sobrienfputs_maybe_filtered (const char *linebuffer, struct ui_file *stream, 201598948Sobrien int filter) 201619370Spst{ 201719370Spst const char *lineptr; 201819370Spst 201919370Spst if (linebuffer == 0) 202019370Spst return; 202119370Spst 202219370Spst /* Don't do any filtering if it is disabled. */ 202398948Sobrien if ((stream != gdb_stdout) || !pagination_enabled 202498948Sobrien || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) 202519370Spst { 202619370Spst fputs_unfiltered (linebuffer, stream); 202719370Spst return; 202819370Spst } 202919370Spst 203019370Spst /* Go through and output each character. Show line extension 203119370Spst when this is necessary; prompt user for new page when this is 203219370Spst necessary. */ 203398948Sobrien 203419370Spst lineptr = linebuffer; 203519370Spst while (*lineptr) 203619370Spst { 203719370Spst /* Possible new page. */ 2038130809Smarcel if (filter && (lines_printed >= lines_per_page - 1)) 203919370Spst prompt_for_continue (); 204019370Spst 204119370Spst while (*lineptr && *lineptr != '\n') 204219370Spst { 204319370Spst /* Print a single line. */ 204419370Spst if (*lineptr == '\t') 204519370Spst { 204619370Spst if (wrap_column) 204719370Spst *wrap_pointer++ = '\t'; 204819370Spst else 204919370Spst fputc_unfiltered ('\t', stream); 205019370Spst /* Shifting right by 3 produces the number of tab stops 205119370Spst we have already passed, and then adding one and 205298948Sobrien shifting left 3 advances to the next tab stop. */ 205319370Spst chars_printed = ((chars_printed >> 3) + 1) << 3; 205419370Spst lineptr++; 205519370Spst } 205619370Spst else 205719370Spst { 205819370Spst if (wrap_column) 205919370Spst *wrap_pointer++ = *lineptr; 206019370Spst else 206198948Sobrien fputc_unfiltered (*lineptr, stream); 206219370Spst chars_printed++; 206319370Spst lineptr++; 206419370Spst } 206598948Sobrien 206619370Spst if (chars_printed >= chars_per_line) 206719370Spst { 206819370Spst unsigned int save_chars = chars_printed; 206919370Spst 207019370Spst chars_printed = 0; 207119370Spst lines_printed++; 207219370Spst /* If we aren't actually wrapping, don't output newline -- 207398948Sobrien if chars_per_line is right, we probably just overflowed 207498948Sobrien anyway; if it's wrong, let us keep going. */ 207519370Spst if (wrap_column) 207619370Spst fputc_unfiltered ('\n', stream); 207719370Spst 207819370Spst /* Possible new page. */ 207919370Spst if (lines_printed >= lines_per_page - 1) 208019370Spst prompt_for_continue (); 208119370Spst 208219370Spst /* Now output indentation and wrapped string */ 208319370Spst if (wrap_column) 208419370Spst { 208519370Spst fputs_unfiltered (wrap_indent, stream); 2086130809Smarcel *wrap_pointer = '\0'; /* Null-terminate saved stuff */ 208798948Sobrien fputs_unfiltered (wrap_buffer, stream); /* and eject it */ 208819370Spst /* FIXME, this strlen is what prevents wrap_indent from 208919370Spst containing tabs. However, if we recurse to print it 209019370Spst and count its chars, we risk trouble if wrap_indent is 209119370Spst longer than (the user settable) chars_per_line. 209219370Spst Note also that this can set chars_printed > chars_per_line 209319370Spst if we are printing a long string. */ 209419370Spst chars_printed = strlen (wrap_indent) 209598948Sobrien + (save_chars - wrap_column); 209619370Spst wrap_pointer = wrap_buffer; /* Reset buffer */ 209719370Spst wrap_buffer[0] = '\0'; 209898948Sobrien wrap_column = 0; /* And disable fancy wrap */ 209998948Sobrien } 210019370Spst } 210119370Spst } 210219370Spst 210319370Spst if (*lineptr == '\n') 210419370Spst { 210519370Spst chars_printed = 0; 210698948Sobrien wrap_here ((char *) 0); /* Spit out chars, cancel further wraps */ 210719370Spst lines_printed++; 210819370Spst fputc_unfiltered ('\n', stream); 210919370Spst lineptr++; 211019370Spst } 211119370Spst } 211219370Spst} 211319370Spst 211419370Spstvoid 211598948Sobrienfputs_filtered (const char *linebuffer, struct ui_file *stream) 211619370Spst{ 211719370Spst fputs_maybe_filtered (linebuffer, stream, 1); 211819370Spst} 211919370Spst 212019370Spstint 212198948Sobrienputchar_unfiltered (int c) 212219370Spst{ 212398948Sobrien char buf = c; 212498948Sobrien ui_file_write (gdb_stdout, &buf, 1); 212519370Spst return c; 212619370Spst} 212719370Spst 212898948Sobrien/* Write character C to gdb_stdout using GDB's paging mechanism and return C. 212998948Sobrien May return nonlocally. */ 213098948Sobrien 213119370Spstint 213298948Sobrienputchar_filtered (int c) 213319370Spst{ 213498948Sobrien return fputc_filtered (c, gdb_stdout); 213598948Sobrien} 213619370Spst 213798948Sobrienint 213898948Sobrienfputc_unfiltered (int c, struct ui_file *stream) 213998948Sobrien{ 214098948Sobrien char buf = c; 214198948Sobrien ui_file_write (stream, &buf, 1); 214219370Spst return c; 214319370Spst} 214419370Spst 214546283Sdfrint 214698948Sobrienfputc_filtered (int c, struct ui_file *stream) 214746283Sdfr{ 214846283Sdfr char buf[2]; 214919370Spst 215046283Sdfr buf[0] = c; 215146283Sdfr buf[1] = 0; 215246283Sdfr fputs_filtered (buf, stream); 215346283Sdfr return c; 215446283Sdfr} 215546283Sdfr 215646283Sdfr/* puts_debug is like fputs_unfiltered, except it prints special 215746283Sdfr characters in printable fashion. */ 215846283Sdfr 215946283Sdfrvoid 216098948Sobrienputs_debug (char *prefix, char *string, char *suffix) 216146283Sdfr{ 216246283Sdfr int ch; 216346283Sdfr 216446283Sdfr /* Print prefix and suffix after each line. */ 216546283Sdfr static int new_line = 1; 216646283Sdfr static int return_p = 0; 216746283Sdfr static char *prev_prefix = ""; 216846283Sdfr static char *prev_suffix = ""; 216946283Sdfr 217046283Sdfr if (*string == '\n') 217146283Sdfr return_p = 0; 217246283Sdfr 217346283Sdfr /* If the prefix is changing, print the previous suffix, a new line, 217446283Sdfr and the new prefix. */ 217598948Sobrien if ((return_p || (strcmp (prev_prefix, prefix) != 0)) && !new_line) 217646283Sdfr { 217798948Sobrien fputs_unfiltered (prev_suffix, gdb_stdlog); 217898948Sobrien fputs_unfiltered ("\n", gdb_stdlog); 217998948Sobrien fputs_unfiltered (prefix, gdb_stdlog); 218046283Sdfr } 218146283Sdfr 218246283Sdfr /* Print prefix if we printed a newline during the previous call. */ 218346283Sdfr if (new_line) 218446283Sdfr { 218546283Sdfr new_line = 0; 218698948Sobrien fputs_unfiltered (prefix, gdb_stdlog); 218746283Sdfr } 218846283Sdfr 218946283Sdfr prev_prefix = prefix; 219046283Sdfr prev_suffix = suffix; 219146283Sdfr 219246283Sdfr /* Output characters in a printable format. */ 219346283Sdfr while ((ch = *string++) != '\0') 219446283Sdfr { 219546283Sdfr switch (ch) 219698948Sobrien { 219746283Sdfr default: 219846283Sdfr if (isprint (ch)) 219998948Sobrien fputc_unfiltered (ch, gdb_stdlog); 220046283Sdfr 220146283Sdfr else 220298948Sobrien fprintf_unfiltered (gdb_stdlog, "\\x%02x", ch & 0xff); 220346283Sdfr break; 220446283Sdfr 220598948Sobrien case '\\': 220698948Sobrien fputs_unfiltered ("\\\\", gdb_stdlog); 220798948Sobrien break; 220898948Sobrien case '\b': 220998948Sobrien fputs_unfiltered ("\\b", gdb_stdlog); 221098948Sobrien break; 221198948Sobrien case '\f': 221298948Sobrien fputs_unfiltered ("\\f", gdb_stdlog); 221398948Sobrien break; 221498948Sobrien case '\n': 221598948Sobrien new_line = 1; 221698948Sobrien fputs_unfiltered ("\\n", gdb_stdlog); 221798948Sobrien break; 221898948Sobrien case '\r': 221998948Sobrien fputs_unfiltered ("\\r", gdb_stdlog); 222098948Sobrien break; 222198948Sobrien case '\t': 222298948Sobrien fputs_unfiltered ("\\t", gdb_stdlog); 222398948Sobrien break; 222498948Sobrien case '\v': 222598948Sobrien fputs_unfiltered ("\\v", gdb_stdlog); 222698948Sobrien break; 222798948Sobrien } 222846283Sdfr 222946283Sdfr return_p = ch == '\r'; 223046283Sdfr } 223146283Sdfr 223246283Sdfr /* Print suffix if we printed a newline. */ 223346283Sdfr if (new_line) 223446283Sdfr { 223598948Sobrien fputs_unfiltered (suffix, gdb_stdlog); 223698948Sobrien fputs_unfiltered ("\n", gdb_stdlog); 223746283Sdfr } 223846283Sdfr} 223946283Sdfr 224046283Sdfr 224119370Spst/* Print a variable number of ARGS using format FORMAT. If this 224219370Spst information is going to put the amount written (since the last call 224319370Spst to REINITIALIZE_MORE_FILTER or the last page break) over the page size, 224419370Spst call prompt_for_continue to get the users permision to continue. 224519370Spst 224619370Spst Unlike fprintf, this function does not return a value. 224719370Spst 224819370Spst We implement three variants, vfprintf (takes a vararg list and stream), 224919370Spst fprintf (takes a stream to write on), and printf (the usual). 225019370Spst 225119370Spst Note also that a longjmp to top level may occur in this routine 225219370Spst (since prompt_for_continue may do so) so this routine should not be 225319370Spst called when cleanups are not in place. */ 225419370Spst 225519370Spststatic void 225698948Sobrienvfprintf_maybe_filtered (struct ui_file *stream, const char *format, 225798948Sobrien va_list args, int filter) 225819370Spst{ 225919370Spst char *linebuffer; 226019370Spst struct cleanup *old_cleanups; 226119370Spst 226298948Sobrien xvasprintf (&linebuffer, format, args); 226398948Sobrien old_cleanups = make_cleanup (xfree, linebuffer); 226419370Spst fputs_maybe_filtered (linebuffer, stream, filter); 226519370Spst do_cleanups (old_cleanups); 226619370Spst} 226719370Spst 226819370Spst 226919370Spstvoid 227098948Sobrienvfprintf_filtered (struct ui_file *stream, const char *format, va_list args) 227119370Spst{ 227219370Spst vfprintf_maybe_filtered (stream, format, args, 1); 227319370Spst} 227419370Spst 227519370Spstvoid 227698948Sobrienvfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) 227719370Spst{ 227819370Spst char *linebuffer; 227919370Spst struct cleanup *old_cleanups; 228019370Spst 228198948Sobrien xvasprintf (&linebuffer, format, args); 228298948Sobrien old_cleanups = make_cleanup (xfree, linebuffer); 228319370Spst fputs_unfiltered (linebuffer, stream); 228419370Spst do_cleanups (old_cleanups); 228519370Spst} 228619370Spst 228719370Spstvoid 228898948Sobrienvprintf_filtered (const char *format, va_list args) 228919370Spst{ 229019370Spst vfprintf_maybe_filtered (gdb_stdout, format, args, 1); 229119370Spst} 229219370Spst 229319370Spstvoid 229498948Sobrienvprintf_unfiltered (const char *format, va_list args) 229519370Spst{ 229619370Spst vfprintf_unfiltered (gdb_stdout, format, args); 229719370Spst} 229819370Spst 229919370Spstvoid 2300130809Smarcelfprintf_filtered (struct ui_file *stream, const char *format, ...) 230119370Spst{ 230219370Spst va_list args; 230319370Spst va_start (args, format); 230419370Spst vfprintf_filtered (stream, format, args); 230519370Spst va_end (args); 230619370Spst} 230719370Spst 230819370Spstvoid 2309130809Smarcelfprintf_unfiltered (struct ui_file *stream, const char *format, ...) 231019370Spst{ 231119370Spst va_list args; 231219370Spst va_start (args, format); 231319370Spst vfprintf_unfiltered (stream, format, args); 231419370Spst va_end (args); 231519370Spst} 231619370Spst 231719370Spst/* Like fprintf_filtered, but prints its result indented. 231819370Spst Called as fprintfi_filtered (spaces, stream, format, ...); */ 231919370Spst 232019370Spstvoid 2321130809Smarcelfprintfi_filtered (int spaces, struct ui_file *stream, const char *format, 2322130809Smarcel ...) 232319370Spst{ 232419370Spst va_list args; 232519370Spst va_start (args, format); 232619370Spst print_spaces_filtered (spaces, stream); 232719370Spst 232819370Spst vfprintf_filtered (stream, format, args); 232919370Spst va_end (args); 233019370Spst} 233119370Spst 233219370Spst 233319370Spstvoid 2334130809Smarcelprintf_filtered (const char *format, ...) 233519370Spst{ 233619370Spst va_list args; 233719370Spst va_start (args, format); 233819370Spst vfprintf_filtered (gdb_stdout, format, args); 233919370Spst va_end (args); 234019370Spst} 234119370Spst 234219370Spst 234319370Spstvoid 2344130809Smarcelprintf_unfiltered (const char *format, ...) 234519370Spst{ 234619370Spst va_list args; 234719370Spst va_start (args, format); 234819370Spst vfprintf_unfiltered (gdb_stdout, format, args); 234919370Spst va_end (args); 235019370Spst} 235119370Spst 235219370Spst/* Like printf_filtered, but prints it's result indented. 235319370Spst Called as printfi_filtered (spaces, format, ...); */ 235419370Spst 235519370Spstvoid 2356130809Smarcelprintfi_filtered (int spaces, const char *format, ...) 235719370Spst{ 235819370Spst va_list args; 235919370Spst va_start (args, format); 236019370Spst print_spaces_filtered (spaces, gdb_stdout); 236119370Spst vfprintf_filtered (gdb_stdout, format, args); 236219370Spst va_end (args); 236319370Spst} 236419370Spst 236519370Spst/* Easy -- but watch out! 236619370Spst 236719370Spst This routine is *not* a replacement for puts()! puts() appends a newline. 236819370Spst This one doesn't, and had better not! */ 236919370Spst 237019370Spstvoid 237198948Sobrienputs_filtered (const char *string) 237219370Spst{ 237319370Spst fputs_filtered (string, gdb_stdout); 237419370Spst} 237519370Spst 237619370Spstvoid 237798948Sobrienputs_unfiltered (const char *string) 237819370Spst{ 237919370Spst fputs_unfiltered (string, gdb_stdout); 238019370Spst} 238119370Spst 238219370Spst/* Return a pointer to N spaces and a null. The pointer is good 238319370Spst until the next call to here. */ 238419370Spstchar * 238598948Sobrienn_spaces (int n) 238619370Spst{ 238798948Sobrien char *t; 238898948Sobrien static char *spaces = 0; 238998948Sobrien static int max_spaces = -1; 239019370Spst 239119370Spst if (n > max_spaces) 239219370Spst { 239319370Spst if (spaces) 239498948Sobrien xfree (spaces); 239598948Sobrien spaces = (char *) xmalloc (n + 1); 239698948Sobrien for (t = spaces + n; t != spaces;) 239719370Spst *--t = ' '; 239819370Spst spaces[n] = '\0'; 239919370Spst max_spaces = n; 240019370Spst } 240119370Spst 240219370Spst return spaces + max_spaces - n; 240319370Spst} 240419370Spst 240519370Spst/* Print N spaces. */ 240619370Spstvoid 240798948Sobrienprint_spaces_filtered (int n, struct ui_file *stream) 240819370Spst{ 240919370Spst fputs_filtered (n_spaces (n), stream); 241019370Spst} 241119370Spst 2412130809Smarcel/* C++/ObjC demangler stuff. */ 241319370Spst 241419370Spst/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language 241519370Spst LANG, using demangling args ARG_MODE, and print it filtered to STREAM. 241619370Spst If the name is not mangled, or the language for the name is unknown, or 241719370Spst demangling is off, the name is printed in its "raw" form. */ 241819370Spst 241919370Spstvoid 2420130809Smarcelfprintf_symbol_filtered (struct ui_file *stream, char *name, 2421130809Smarcel enum language lang, int arg_mode) 242219370Spst{ 242319370Spst char *demangled; 242419370Spst 242519370Spst if (name != NULL) 242619370Spst { 242719370Spst /* If user wants to see raw output, no problem. */ 242819370Spst if (!demangle) 242919370Spst { 243019370Spst fputs_filtered (name, stream); 243119370Spst } 243219370Spst else 243319370Spst { 2434130809Smarcel demangled = language_demangle (language_def (lang), name, arg_mode); 243519370Spst fputs_filtered (demangled ? demangled : name, stream); 243619370Spst if (demangled != NULL) 243719370Spst { 243898948Sobrien xfree (demangled); 243919370Spst } 244019370Spst } 244119370Spst } 244219370Spst} 244319370Spst 244419370Spst/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any 244519370Spst differences in whitespace. Returns 0 if they match, non-zero if they 244619370Spst don't (slightly different than strcmp()'s range of return values). 244798948Sobrien 244819370Spst As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". 244919370Spst This "feature" is useful when searching for matching C++ function names 245019370Spst (such as if the user types 'break FOO', where FOO is a mangled C++ 245119370Spst function). */ 245219370Spst 245319370Spstint 245498948Sobrienstrcmp_iw (const char *string1, const char *string2) 245519370Spst{ 245619370Spst while ((*string1 != '\0') && (*string2 != '\0')) 245719370Spst { 245819370Spst while (isspace (*string1)) 245919370Spst { 246019370Spst string1++; 246119370Spst } 246219370Spst while (isspace (*string2)) 246319370Spst { 246419370Spst string2++; 246519370Spst } 246619370Spst if (*string1 != *string2) 246719370Spst { 246819370Spst break; 246919370Spst } 247019370Spst if (*string1 != '\0') 247119370Spst { 247219370Spst string1++; 247319370Spst string2++; 247419370Spst } 247519370Spst } 247619370Spst return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); 247719370Spst} 2478130809Smarcel 2479130809Smarcel/* This is like strcmp except that it ignores whitespace and treats 2480130809Smarcel '(' as the first non-NULL character in terms of ordering. Like 2481130809Smarcel strcmp (and unlike strcmp_iw), it returns negative if STRING1 < 2482130809Smarcel STRING2, 0 if STRING2 = STRING2, and positive if STRING1 > STRING2 2483130809Smarcel according to that ordering. 2484130809Smarcel 2485130809Smarcel If a list is sorted according to this function and if you want to 2486130809Smarcel find names in the list that match some fixed NAME according to 2487130809Smarcel strcmp_iw(LIST_ELT, NAME), then the place to start looking is right 2488130809Smarcel where this function would put NAME. 2489130809Smarcel 2490130809Smarcel Here are some examples of why using strcmp to sort is a bad idea: 2491130809Smarcel 2492130809Smarcel Whitespace example: 2493130809Smarcel 2494130809Smarcel Say your partial symtab contains: "foo<char *>", "goo". Then, if 2495130809Smarcel we try to do a search for "foo<char*>", strcmp will locate this 2496130809Smarcel after "foo<char *>" and before "goo". Then lookup_partial_symbol 2497130809Smarcel will start looking at strings beginning with "goo", and will never 2498130809Smarcel see the correct match of "foo<char *>". 2499130809Smarcel 2500130809Smarcel Parenthesis example: 2501130809Smarcel 2502130809Smarcel In practice, this is less like to be an issue, but I'll give it a 2503130809Smarcel shot. Let's assume that '$' is a legitimate character to occur in 2504130809Smarcel symbols. (Which may well even be the case on some systems.) Then 2505130809Smarcel say that the partial symbol table contains "foo$" and "foo(int)". 2506130809Smarcel strcmp will put them in this order, since '$' < '('. Now, if the 2507130809Smarcel user searches for "foo", then strcmp will sort "foo" before "foo$". 2508130809Smarcel Then lookup_partial_symbol will notice that strcmp_iw("foo$", 2509130809Smarcel "foo") is false, so it won't proceed to the actual match of 2510130809Smarcel "foo(int)" with "foo". */ 2511130809Smarcel 2512130809Smarcelint 2513130809Smarcelstrcmp_iw_ordered (const char *string1, const char *string2) 2514130809Smarcel{ 2515130809Smarcel while ((*string1 != '\0') && (*string2 != '\0')) 2516130809Smarcel { 2517130809Smarcel while (isspace (*string1)) 2518130809Smarcel { 2519130809Smarcel string1++; 2520130809Smarcel } 2521130809Smarcel while (isspace (*string2)) 2522130809Smarcel { 2523130809Smarcel string2++; 2524130809Smarcel } 2525130809Smarcel if (*string1 != *string2) 2526130809Smarcel { 2527130809Smarcel break; 2528130809Smarcel } 2529130809Smarcel if (*string1 != '\0') 2530130809Smarcel { 2531130809Smarcel string1++; 2532130809Smarcel string2++; 2533130809Smarcel } 2534130809Smarcel } 2535130809Smarcel 2536130809Smarcel switch (*string1) 2537130809Smarcel { 2538130809Smarcel /* Characters are non-equal unless they're both '\0'; we want to 2539130809Smarcel make sure we get the comparison right according to our 2540130809Smarcel comparison in the cases where one of them is '\0' or '('. */ 2541130809Smarcel case '\0': 2542130809Smarcel if (*string2 == '\0') 2543130809Smarcel return 0; 2544130809Smarcel else 2545130809Smarcel return -1; 2546130809Smarcel case '(': 2547130809Smarcel if (*string2 == '\0') 2548130809Smarcel return 1; 2549130809Smarcel else 2550130809Smarcel return -1; 2551130809Smarcel default: 2552130809Smarcel if (*string2 == '(') 2553130809Smarcel return 1; 2554130809Smarcel else 2555130809Smarcel return *string1 - *string2; 2556130809Smarcel } 2557130809Smarcel} 2558130809Smarcel 2559130809Smarcel/* A simple comparison function with opposite semantics to strcmp. */ 2560130809Smarcel 2561130809Smarcelint 2562130809Smarcelstreq (const char *lhs, const char *rhs) 2563130809Smarcel{ 2564130809Smarcel return !strcmp (lhs, rhs); 2565130809Smarcel} 256698948Sobrien 256719370Spst 256846283Sdfr/* 256998948Sobrien ** subset_compare() 257098948Sobrien ** Answer whether string_to_compare is a full or partial match to 257198948Sobrien ** template_string. The partial match must be in sequence starting 257298948Sobrien ** at index 0. 257398948Sobrien */ 257446283Sdfrint 257598948Sobriensubset_compare (char *string_to_compare, char *template_string) 257646283Sdfr{ 257798948Sobrien int match; 2578130809Smarcel if (template_string != (char *) NULL && string_to_compare != (char *) NULL 2579130809Smarcel && strlen (string_to_compare) <= strlen (template_string)) 2580130809Smarcel match = 2581130809Smarcel (strncmp 2582130809Smarcel (template_string, string_to_compare, strlen (string_to_compare)) == 0); 258398948Sobrien else 258498948Sobrien match = 0; 258598948Sobrien return match; 258698948Sobrien} 258746283Sdfr 258846283Sdfr 258998948Sobrienstatic void pagination_on_command (char *arg, int from_tty); 259098948Sobrienstatic void 259198948Sobrienpagination_on_command (char *arg, int from_tty) 259246283Sdfr{ 259346283Sdfr pagination_enabled = 1; 259446283Sdfr} 259546283Sdfr 259698948Sobrienstatic void pagination_on_command (char *arg, int from_tty); 259798948Sobrienstatic void 259898948Sobrienpagination_off_command (char *arg, int from_tty) 259946283Sdfr{ 260046283Sdfr pagination_enabled = 0; 260146283Sdfr} 260298948Sobrien 260346283Sdfr 260419370Spstvoid 260598948Sobrieninitialize_utils (void) 260619370Spst{ 260719370Spst struct cmd_list_element *c; 260819370Spst 2609130809Smarcel c = add_set_cmd ("width", class_support, var_uinteger, &chars_per_line, 261098948Sobrien "Set number of characters gdb thinks are in a line.", 261198948Sobrien &setlist); 261219370Spst add_show_from_set (c, &showlist); 261398948Sobrien set_cmd_sfunc (c, set_width_command); 261419370Spst 2615130809Smarcel c = add_set_cmd ("height", class_support, var_uinteger, &lines_per_page, 2616130809Smarcel "Set number of lines gdb thinks are in a page.", &setlist); 2617130809Smarcel add_show_from_set (c, &showlist); 2618130809Smarcel set_cmd_sfunc (c, set_height_command); 261998948Sobrien 262046283Sdfr init_page_info (); 262119370Spst 262219370Spst add_show_from_set 262398948Sobrien (add_set_cmd ("demangle", class_support, var_boolean, 262498948Sobrien (char *) &demangle, 2625130809Smarcel "Set demangling of encoded C++/ObjC names when displaying symbols.", 2626130809Smarcel &setprintlist), &showprintlist); 262719370Spst 262819370Spst add_show_from_set 262946283Sdfr (add_set_cmd ("pagination", class_support, 263098948Sobrien var_boolean, (char *) &pagination_enabled, 2631130809Smarcel "Set state of pagination.", &setlist), &showlist); 263298948Sobrien 263346283Sdfr if (xdb_commands) 263446283Sdfr { 263598948Sobrien add_com ("am", class_support, pagination_on_command, 263698948Sobrien "Enable pagination"); 263798948Sobrien add_com ("sm", class_support, pagination_off_command, 263898948Sobrien "Disable pagination"); 263946283Sdfr } 264046283Sdfr 264146283Sdfr add_show_from_set 264298948Sobrien (add_set_cmd ("sevenbit-strings", class_support, var_boolean, 264398948Sobrien (char *) &sevenbit_strings, 264498948Sobrien "Set printing of 8-bit characters in strings as \\nnn.", 2645130809Smarcel &setprintlist), &showprintlist); 264619370Spst 264719370Spst add_show_from_set 264898948Sobrien (add_set_cmd ("asm-demangle", class_support, var_boolean, 264998948Sobrien (char *) &asm_demangle, 2650130809Smarcel "Set demangling of C++/ObjC names in disassembly listings.", 2651130809Smarcel &setprintlist), &showprintlist); 265219370Spst} 265319370Spst 265419370Spst/* Machine specific function to handle SIGWINCH signal. */ 265519370Spst 265619370Spst#ifdef SIGWINCH_HANDLER_BODY 265798948SobrienSIGWINCH_HANDLER_BODY 265819370Spst#endif 265998948Sobrien/* print routines to handle variable size regs, etc. */ 266098948Sobrien/* temporary storage using circular buffer */ 266198948Sobrien#define NUMCELLS 16 266298948Sobrien#define CELLSIZE 32 266398948Sobrienstatic char * 266498948Sobrienget_cell (void) 266598948Sobrien{ 266698948Sobrien static char buf[NUMCELLS][CELLSIZE]; 266798948Sobrien static int cell = 0; 266898948Sobrien if (++cell >= NUMCELLS) 266998948Sobrien cell = 0; 267098948Sobrien return buf[cell]; 267198948Sobrien} 267246283Sdfr 267398948Sobrienint 267498948Sobrienstrlen_paddr (void) 267598948Sobrien{ 267698948Sobrien return (TARGET_ADDR_BIT / 8 * 2); 267798948Sobrien} 267846283Sdfr 267998948Sobrienchar * 268098948Sobrienpaddr (CORE_ADDR addr) 268198948Sobrien{ 268298948Sobrien return phex (addr, TARGET_ADDR_BIT / 8); 268398948Sobrien} 268446283Sdfr 268598948Sobrienchar * 268698948Sobrienpaddr_nz (CORE_ADDR addr) 268746283Sdfr{ 268898948Sobrien return phex_nz (addr, TARGET_ADDR_BIT / 8); 268946283Sdfr} 269046283Sdfr 269198948Sobrienstatic void 269298948Sobriendecimal2str (char *paddr_str, char *sign, ULONGEST addr) 269346283Sdfr{ 269498948Sobrien /* steal code from valprint.c:print_decimal(). Should this worry 269598948Sobrien about the real size of addr as the above does? */ 269698948Sobrien unsigned long temp[3]; 269798948Sobrien int i = 0; 269898948Sobrien do 269946283Sdfr { 270098948Sobrien temp[i] = addr % (1000 * 1000 * 1000); 270198948Sobrien addr /= (1000 * 1000 * 1000); 270298948Sobrien i++; 270346283Sdfr } 270498948Sobrien while (addr != 0 && i < (sizeof (temp) / sizeof (temp[0]))); 270598948Sobrien switch (i) 270646283Sdfr { 270798948Sobrien case 1: 2708130809Smarcel sprintf (paddr_str, "%s%lu", sign, temp[0]); 270998948Sobrien break; 271098948Sobrien case 2: 2711130809Smarcel sprintf (paddr_str, "%s%lu%09lu", sign, temp[1], temp[0]); 271298948Sobrien break; 271398948Sobrien case 3: 2714130809Smarcel sprintf (paddr_str, "%s%lu%09lu%09lu", sign, temp[2], temp[1], temp[0]); 271598948Sobrien break; 271698948Sobrien default: 2717130809Smarcel internal_error (__FILE__, __LINE__, 2718130809Smarcel "failed internal consistency check"); 271946283Sdfr } 272098948Sobrien} 272146283Sdfr 272298948Sobrienchar * 272398948Sobrienpaddr_u (CORE_ADDR addr) 272498948Sobrien{ 272598948Sobrien char *paddr_str = get_cell (); 272698948Sobrien decimal2str (paddr_str, "", addr); 272798948Sobrien return paddr_str; 272846283Sdfr} 272946283Sdfr 273098948Sobrienchar * 273198948Sobrienpaddr_d (LONGEST addr) 273246283Sdfr{ 273398948Sobrien char *paddr_str = get_cell (); 273498948Sobrien if (addr < 0) 273598948Sobrien decimal2str (paddr_str, "-", -addr); 273646283Sdfr else 273798948Sobrien decimal2str (paddr_str, "", addr); 273898948Sobrien return paddr_str; 273998948Sobrien} 274046283Sdfr 274198948Sobrien/* eliminate warning from compiler on 32-bit systems */ 274298948Sobrienstatic int thirty_two = 32; 274398948Sobrien 274498948Sobrienchar * 274598948Sobrienphex (ULONGEST l, int sizeof_l) 274698948Sobrien{ 274798948Sobrien char *str; 274898948Sobrien switch (sizeof_l) 274946283Sdfr { 275098948Sobrien case 8: 275198948Sobrien str = get_cell (); 275298948Sobrien sprintf (str, "%08lx%08lx", 275398948Sobrien (unsigned long) (l >> thirty_two), 275498948Sobrien (unsigned long) (l & 0xffffffff)); 275598948Sobrien break; 275698948Sobrien case 4: 275798948Sobrien str = get_cell (); 275898948Sobrien sprintf (str, "%08lx", (unsigned long) l); 275998948Sobrien break; 276098948Sobrien case 2: 276198948Sobrien str = get_cell (); 276298948Sobrien sprintf (str, "%04x", (unsigned short) (l & 0xffff)); 276398948Sobrien break; 276498948Sobrien default: 276598948Sobrien str = phex (l, sizeof (l)); 276698948Sobrien break; 276746283Sdfr } 276898948Sobrien return str; 276946283Sdfr} 277046283Sdfr 277198948Sobrienchar * 277298948Sobrienphex_nz (ULONGEST l, int sizeof_l) 277346283Sdfr{ 277498948Sobrien char *str; 277598948Sobrien switch (sizeof_l) 277698948Sobrien { 277798948Sobrien case 8: 277846283Sdfr { 277998948Sobrien unsigned long high = (unsigned long) (l >> thirty_two); 278098948Sobrien str = get_cell (); 278198948Sobrien if (high == 0) 278298948Sobrien sprintf (str, "%lx", (unsigned long) (l & 0xffffffff)); 278398948Sobrien else 2784130809Smarcel sprintf (str, "%lx%08lx", high, (unsigned long) (l & 0xffffffff)); 278598948Sobrien break; 278646283Sdfr } 278798948Sobrien case 4: 278898948Sobrien str = get_cell (); 278998948Sobrien sprintf (str, "%lx", (unsigned long) l); 279098948Sobrien break; 279198948Sobrien case 2: 279298948Sobrien str = get_cell (); 279398948Sobrien sprintf (str, "%x", (unsigned short) (l & 0xffff)); 279498948Sobrien break; 279598948Sobrien default: 279698948Sobrien str = phex_nz (l, sizeof (l)); 279798948Sobrien break; 279846283Sdfr } 279998948Sobrien return str; 280046283Sdfr} 280146283Sdfr 280246283Sdfr 280398948Sobrien/* Convert a CORE_ADDR into a string. */ 280498948Sobrienconst char * 280598948Sobriencore_addr_to_string (const CORE_ADDR addr) 280646283Sdfr{ 280798948Sobrien char *str = get_cell (); 280898948Sobrien strcpy (str, "0x"); 280998948Sobrien strcat (str, phex (addr, sizeof (addr))); 281098948Sobrien return str; 281146283Sdfr} 281246283Sdfr 281398948Sobrienconst char * 281498948Sobriencore_addr_to_string_nz (const CORE_ADDR addr) 281546283Sdfr{ 281698948Sobrien char *str = get_cell (); 281798948Sobrien strcpy (str, "0x"); 281898948Sobrien strcat (str, phex_nz (addr, sizeof (addr))); 281998948Sobrien return str; 282046283Sdfr} 282146283Sdfr 282298948Sobrien/* Convert a string back into a CORE_ADDR. */ 282398948SobrienCORE_ADDR 282498948Sobrienstring_to_core_addr (const char *my_string) 282546283Sdfr{ 282698948Sobrien CORE_ADDR addr = 0; 282798948Sobrien if (my_string[0] == '0' && tolower (my_string[1]) == 'x') 282846283Sdfr { 282998948Sobrien /* Assume that it is in decimal. */ 283098948Sobrien int i; 283198948Sobrien for (i = 2; my_string[i] != '\0'; i++) 283246283Sdfr { 283398948Sobrien if (isdigit (my_string[i])) 283498948Sobrien addr = (my_string[i] - '0') + (addr * 16); 2835130809Smarcel else if (isxdigit (my_string[i])) 283698948Sobrien addr = (tolower (my_string[i]) - 'a' + 0xa) + (addr * 16); 283746283Sdfr else 283898948Sobrien internal_error (__FILE__, __LINE__, "invalid hex"); 283946283Sdfr } 284046283Sdfr } 284198948Sobrien else 284246283Sdfr { 284398948Sobrien /* Assume that it is in decimal. */ 284498948Sobrien int i; 284598948Sobrien for (i = 0; my_string[i] != '\0'; i++) 284646283Sdfr { 284798948Sobrien if (isdigit (my_string[i])) 284898948Sobrien addr = (my_string[i] - '0') + (addr * 10); 284946283Sdfr else 285098948Sobrien internal_error (__FILE__, __LINE__, "invalid decimal"); 285146283Sdfr } 285246283Sdfr } 285398948Sobrien return addr; 285446283Sdfr} 285598948Sobrien 285698948Sobrienchar * 285798948Sobriengdb_realpath (const char *filename) 285898948Sobrien{ 2859130809Smarcel /* Method 1: The system has a compile time upper bound on a filename 2860130809Smarcel path. Use that and realpath() to canonicalize the name. This is 2861130809Smarcel the most common case. Note that, if there isn't a compile time 2862130809Smarcel upper bound, you want to avoid realpath() at all costs. */ 286398948Sobrien#if defined(HAVE_REALPATH) 2864130809Smarcel { 286598948Sobrien# if defined (PATH_MAX) 2866130809Smarcel char buf[PATH_MAX]; 286798948Sobrien# define USE_REALPATH 286898948Sobrien# elif defined (MAXPATHLEN) 2869130809Smarcel char buf[MAXPATHLEN]; 287098948Sobrien# define USE_REALPATH 287198948Sobrien# endif 2872130809Smarcel# if defined (USE_REALPATH) 2873130809Smarcel const char *rp = realpath (filename, buf); 2874130809Smarcel if (rp == NULL) 2875130809Smarcel rp = filename; 2876130809Smarcel return xstrdup (rp); 2877130809Smarcel# endif 2878130809Smarcel } 287998948Sobrien#endif /* HAVE_REALPATH */ 288098948Sobrien 2881130809Smarcel /* Method 2: The host system (i.e., GNU) has the function 2882130809Smarcel canonicalize_file_name() which malloc's a chunk of memory and 2883130809Smarcel returns that, use that. */ 2884130809Smarcel#if defined(HAVE_CANONICALIZE_FILE_NAME) 2885130809Smarcel { 2886130809Smarcel char *rp = canonicalize_file_name (filename); 2887130809Smarcel if (rp == NULL) 2888130809Smarcel return xstrdup (filename); 2889130809Smarcel else 2890130809Smarcel return rp; 2891130809Smarcel } 2892130809Smarcel#endif 2893130809Smarcel 2894130809Smarcel /* FIXME: cagney/2002-11-13: 2895130809Smarcel 2896130809Smarcel Method 2a: Use realpath() with a NULL buffer. Some systems, due 2897130809Smarcel to the problems described in in method 3, have modified their 2898130809Smarcel realpath() implementation so that it will allocate a buffer when 2899130809Smarcel NULL is passed in. Before this can be used, though, some sort of 2900130809Smarcel configure time test would need to be added. Otherwize the code 2901130809Smarcel will likely core dump. */ 2902130809Smarcel 2903130809Smarcel /* Method 3: Now we're getting desperate! The system doesn't have a 2904130809Smarcel compile time buffer size and no alternative function. Query the 2905130809Smarcel OS, using pathconf(), for the buffer limit. Care is needed 2906130809Smarcel though, some systems do not limit PATH_MAX (return -1 for 2907130809Smarcel pathconf()) making it impossible to pass a correctly sized buffer 2908130809Smarcel to realpath() (it could always overflow). On those systems, we 2909130809Smarcel skip this. */ 2910130809Smarcel#if defined (HAVE_REALPATH) && defined (HAVE_UNISTD_H) && defined(HAVE_ALLOCA) 2911130809Smarcel { 2912130809Smarcel /* Find out the max path size. */ 2913130809Smarcel long path_max = pathconf ("/", _PC_PATH_MAX); 2914130809Smarcel if (path_max > 0) 2915130809Smarcel { 2916130809Smarcel /* PATH_MAX is bounded. */ 2917130809Smarcel char *buf = alloca (path_max); 2918130809Smarcel char *rp = realpath (filename, buf); 2919130809Smarcel return xstrdup (rp ? rp : filename); 2920130809Smarcel } 2921130809Smarcel } 2922130809Smarcel#endif 2923130809Smarcel 2924130809Smarcel /* This system is a lost cause, just dup the buffer. */ 2925130809Smarcel return xstrdup (filename); 2926130809Smarcel} 2927130809Smarcel 2928130809Smarcel/* Return a copy of FILENAME, with its directory prefix canonicalized 2929130809Smarcel by gdb_realpath. */ 2930130809Smarcel 2931130809Smarcelchar * 2932130809Smarcelxfullpath (const char *filename) 2933130809Smarcel{ 2934130809Smarcel const char *base_name = lbasename (filename); 2935130809Smarcel char *dir_name; 2936130809Smarcel char *real_path; 2937130809Smarcel char *result; 2938130809Smarcel 2939130809Smarcel /* Extract the basename of filename, and return immediately 2940130809Smarcel a copy of filename if it does not contain any directory prefix. */ 2941130809Smarcel if (base_name == filename) 294298948Sobrien return xstrdup (filename); 2943130809Smarcel 2944130809Smarcel dir_name = alloca ((size_t) (base_name - filename + 2)); 2945130809Smarcel /* Allocate enough space to store the dir_name + plus one extra 2946130809Smarcel character sometimes needed under Windows (see below), and 2947130809Smarcel then the closing \000 character */ 2948130809Smarcel strncpy (dir_name, filename, base_name - filename); 2949130809Smarcel dir_name[base_name - filename] = '\000'; 2950130809Smarcel 2951130809Smarcel#ifdef HAVE_DOS_BASED_FILE_SYSTEM 2952130809Smarcel /* We need to be careful when filename is of the form 'd:foo', which 2953130809Smarcel is equivalent of d:./foo, which is totally different from d:/foo. */ 2954130809Smarcel if (strlen (dir_name) == 2 && isalpha (dir_name[0]) && dir_name[1] == ':') 2955130809Smarcel { 2956130809Smarcel dir_name[2] = '.'; 2957130809Smarcel dir_name[3] = '\000'; 2958130809Smarcel } 2959130809Smarcel#endif 2960130809Smarcel 2961130809Smarcel /* Canonicalize the directory prefix, and build the resulting 2962130809Smarcel filename. If the dirname realpath already contains an ending 2963130809Smarcel directory separator, avoid doubling it. */ 2964130809Smarcel real_path = gdb_realpath (dir_name); 2965130809Smarcel if (IS_DIR_SEPARATOR (real_path[strlen (real_path) - 1])) 2966130809Smarcel result = concat (real_path, base_name, NULL); 296798948Sobrien else 2968130809Smarcel result = concat (real_path, SLASH_STRING, base_name, NULL); 2969130809Smarcel 2970130809Smarcel xfree (real_path); 2971130809Smarcel return result; 297298948Sobrien} 2973130809Smarcel 2974130809Smarcel 2975130809Smarcel/* This is the 32-bit CRC function used by the GNU separate debug 2976130809Smarcel facility. An executable may contain a section named 2977130809Smarcel .gnu_debuglink, which holds the name of a separate executable file 2978130809Smarcel containing its debug info, and a checksum of that file's contents, 2979130809Smarcel computed using this function. */ 2980130809Smarcelunsigned long 2981130809Smarcelgnu_debuglink_crc32 (unsigned long crc, unsigned char *buf, size_t len) 2982130809Smarcel{ 2983130809Smarcel static const unsigned long crc32_table[256] = { 2984130809Smarcel 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 2985130809Smarcel 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 2986130809Smarcel 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 2987130809Smarcel 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 2988130809Smarcel 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 2989130809Smarcel 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 2990130809Smarcel 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 2991130809Smarcel 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 2992130809Smarcel 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 2993130809Smarcel 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 2994130809Smarcel 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 2995130809Smarcel 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 2996130809Smarcel 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 2997130809Smarcel 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 2998130809Smarcel 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 2999130809Smarcel 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 3000130809Smarcel 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 3001130809Smarcel 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 3002130809Smarcel 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 3003130809Smarcel 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 3004130809Smarcel 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 3005130809Smarcel 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 3006130809Smarcel 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 3007130809Smarcel 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 3008130809Smarcel 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 3009130809Smarcel 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 3010130809Smarcel 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 3011130809Smarcel 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 3012130809Smarcel 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 3013130809Smarcel 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 3014130809Smarcel 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 3015130809Smarcel 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 3016130809Smarcel 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 3017130809Smarcel 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 3018130809Smarcel 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 3019130809Smarcel 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 3020130809Smarcel 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 3021130809Smarcel 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 3022130809Smarcel 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 3023130809Smarcel 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 3024130809Smarcel 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 3025130809Smarcel 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 3026130809Smarcel 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 3027130809Smarcel 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 3028130809Smarcel 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 3029130809Smarcel 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 3030130809Smarcel 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 3031130809Smarcel 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 3032130809Smarcel 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 3033130809Smarcel 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 3034130809Smarcel 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 3035130809Smarcel 0x2d02ef8d 3036130809Smarcel }; 3037130809Smarcel unsigned char *end; 3038130809Smarcel 3039130809Smarcel crc = ~crc & 0xffffffff; 3040130809Smarcel for (end = buf + len; buf < end; ++buf) 3041130809Smarcel crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8); 3042130809Smarcel return ~crc & 0xffffffff;; 3043130809Smarcel} 3044130809Smarcel 3045130809SmarcelULONGEST 3046130809Smarcelalign_up (ULONGEST v, int n) 3047130809Smarcel{ 3048130809Smarcel /* Check that N is really a power of two. */ 3049130809Smarcel gdb_assert (n && (n & (n-1)) == 0); 3050130809Smarcel return (v + n - 1) & -n; 3051130809Smarcel} 3052130809Smarcel 3053130809SmarcelULONGEST 3054130809Smarcelalign_down (ULONGEST v, int n) 3055130809Smarcel{ 3056130809Smarcel /* Check that N is really a power of two. */ 3057130809Smarcel gdb_assert (n && (n & (n-1)) == 0); 3058130809Smarcel return (v & -n); 3059130809Smarcel} 3060