1 //
2 // bug.h
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifdef __GNUG__
29 #pragma interface
30 #endif
31 
32 #ifndef _util_misc_bug_h
33 #define _util_misc_bug_h
34 
35 #include <util/class/class.h>
36 #include <util/state/state.h>
37 #include <util/ref/ref.h>
38 
39 namespace sc {
40 
41 /** The Debugger class describes what should be done when a catastrophic
42 error causes unexpected program termination.  It can try things such as
43 start a debugger running where the program died or it can attempt to
44 produce a stack traceback showing roughly where the program died.  These
45 attempts will not always succeed.  */
46 class Debugger: public SavableState {
47   protected:
48     char *prefix_;
49     char *exec_;
50     char *cmd_;
51     volatile int debugger_ready_;
52 
53     int debug_;
54     int traceback_;
55     int exit_on_signal_;
56     int sleep_;
57     int wait_for_debugger_;
58     int handle_sigint_;
59     int *mysigs_;
60 
61     void init();
62 
63     static Debugger *default_debugger_;
64   public:
65     Debugger(const char *exec = 0);
66 
67     /** The KeyVal constructor understands the following keywords:
68         <dl>
69         <dt><tt>debug</tt><dd> Try to start a debugger when an error occurs.
70         Doesn't work on all machines. The default is true, if possible.
71 
72         <dt><tt>traceback</tt><dd> Try to print out a traceback extracting
73         return addresses from the call stack.  Doesn't work on most
74         machines.  The default is true, if possible.
75 
76         <dt><tt>exit</tt><dd> Exit on errors.  The default is true.
77 
78         <dt><tt>wait_for_debugger</tt><dd> When starting a debugger go into
79         an infinite loop to give the debugger a chance to attach to the
80         process.  The default is true.
81 
82         <dt><tt>sleep</tt><dd> When starting a debugger wait this many
83         seconds to give the debugger a chance to attach to the process.
84         The default is 0.
85 
86         <dt><tt>handle_defaults</tt><dd> Handle a standard set of signals
87         such as SIGBUS, SIGSEGV, etc.  The default is true.
88 
89         <dt><tt>prefix</tt><dd> Gives a string that is printed before each
90         line that is printed by Debugger. The default is nothing.
91 
92         <dt><tt>cmd</tt><dd> Gives a command to be executed to start the
93         debugger.  The default varies with machine.
94 
95         </dl> */
96     Debugger(const Ref<KeyVal>&);
97 
98     Debugger(StateIn&);
99     ~Debugger();
100 
101     /** The debug member attempts to start a debugger
102         running on the current process. */
103     virtual void debug(const char *reason = 0);
104     /** The traceback member attempts a stack traceback
105      for the current process.  A symbol table must be saved for
106      the executable if any sense is to be made of the traceback.
107      Tracebacks are currently available only for a limited number
108      of architectures. */
109     virtual void traceback(const char *reason = 0);
110     /// Turn on or off debugging on a signel.  The default is on.
111     virtual void set_debug_on_signal(int);
112     /// Turn on or off traceback on a signel.  The default is on.
113     virtual void set_traceback_on_signal(int);
114     /// Turn on or off exit after a signel.  The default is on.
115     virtual void set_exit_on_signal(int);
116     /** Turn on or off running an infinite loop after the debugger is started.
117         This loop gives the debugger a chance to attack to the process.
118         The default is on. */
119     virtual void set_wait_for_debugger(int);
120 
121     /// The Debugger will be actived when sig is caught.
122     virtual void handle(int sig);
123     /// This calls handle(int) with all of the major signals.
124     virtual void handle_defaults();
125 
126     /// This sets a prefix which preceeds all messages printing by Debugger.
127     virtual void set_prefix(const char *p);
128     /// Set the prefix to the decimal represention of p followed by a ": ".
129     virtual void set_prefix(int p);
130 
131     /** Sets the command to be exectuted when debug is called.
132         The character sequence "$(EXEC)" is replaced by the executable
133         name (see set_exec), "$(PID)" is replaced by the
134         current process id, and "$(PREFIX)" is replaced by the
135         prefix. */
136     virtual void set_cmd(const char *);
137     /// Calls set_cmd with a hopefully suitable default.
138     virtual void default_cmd();
139     /** Set the name of the exectuble for the current process.
140         It is up to the programmer to set this, even if the Debugger
141         is initialized with the KeyVal constructor. */
142     virtual void set_exec(const char *);
143 
144     /// Called with signal sig is received.  This is mainly for internal use.
145     virtual void got_signal(int sig);
146 
147     /// Set the global default debugger.  The initial value is null.
148     static void set_default_debugger(const Ref<Debugger> &);
149     /// Return the global default debugger.
150     static Debugger *default_debugger();
151 
152     void save_data_state(StateOut&);
153 };
154 
155 }
156 
157 #endif
158 
159 // Local Variables:
160 // mode: c++
161 // c-file-style: "CLJ"
162 // End:
163