When an error occurs executing a tool, we now throw an exception instead
[oota-llvm.git] / tools / llvm-db / CLIDebugger.h
1 //===- CLIDebugger.h - LLVM Command Line Interface Debugger -----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the CLIDebugger class, which implements a command line
11 // interface to the LLVM Debugger library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef CLIDEBUGGER_H
16 #define CLIDEBUGGER_H
17
18 #include "llvm/Debugger/Debugger.h"
19 #include <map>
20
21 namespace llvm {
22   class CLICommand;
23   class SourceFile;
24   class SourceLanguage;
25   class ProgramInfo;
26   class RuntimeInfo;
27
28   /// CLIDebugger - This class implements the command line interface for the
29   /// LLVM debugger.
30   class CLIDebugger {
31     /// Dbg - The low-level LLVM debugger object that we use to do our dirty
32     /// work.
33     Debugger Dbg;
34
35     /// CommandTable - This table contains a mapping from command names to the
36     /// CLICommand object that implements the command.
37     std::map<std::string, CLICommand*> CommandTable;
38
39     //===------------------------------------------------------------------===//
40     // Data related to the program that is currently loaded.  Note that the Dbg
41     // variable also captures some information about the loaded program.  This
42     // pointer is non-null iff Dbg.isProgramLoaded() is true.
43     //
44     ProgramInfo *TheProgramInfo;
45
46     //===------------------------------------------------------------------===//
47     // Data related to the program that is currently executing, but has stopped.
48     // Note that the Dbg variable also captures some information about the
49     // loaded program.  This pointer is non-null iff Dbg.isProgramRunning() is
50     // true.
51     //
52     RuntimeInfo *TheRuntimeInfo;
53
54     /// LastCurrentFrame - This variable holds the Frame ID of the top-level
55     /// stack frame from the last time that the program was executed.  We keep
56     /// this because we only want to print the source location when the current
57     /// function changes.
58     void *LastCurrentFrame;
59
60     //===------------------------------------------------------------------===//
61     // Data directly exposed through the debugger prompt
62     //
63     std::string Prompt;   // set prompt, show prompt
64     unsigned ListSize;    // set listsize, show listsize
65     
66     //===------------------------------------------------------------------===//
67     // Data to support user interaction
68     //
69     
70     /// CurrentFile - The current source file we are inspecting, or null if
71     /// none.
72     const SourceFile *CurrentFile;
73     unsigned LineListedStart, LineListedEnd;
74
75     /// CurrentLanguage - This contains the source language in use, if one is
76     /// explicitly set by the user.  If this is null (the default), the language
77     /// is automatically determined from the current stack frame.
78     ///
79     const SourceLanguage *CurrentLanguage;
80
81   public:
82     CLIDebugger();
83
84     /// getDebugger - Return the current LLVM debugger implementation being
85     /// used.
86     Debugger &getDebugger() { return Dbg; }
87
88     /// run - Start the debugger, returning when the user exits the debugger.
89     /// This starts the main event loop of the CLI debugger.
90     ///
91     int run();
92
93     /// addCommand - Add a command to the CommandTable, potentially displacing a
94     /// preexisting command.
95     void addCommand(const std::string &Option, CLICommand *Cmd);
96
97     /// addSourceDirectory - Add a directory to search when looking for the
98     /// source code of the program.
99     void addSourceDirectory(const std::string &Dir) {
100       // FIXME: implement
101     }
102
103     /// getCurrentLanguage - Return the current source language that the user is
104     /// playing around with.  This is aquired from the current stack frame of a
105     /// running program if one exists, but this value can be explicitly set by
106     /// the user as well.
107     const SourceLanguage &getCurrentLanguage() const;
108
109     /// getProgramInfo - Return a reference to the ProgramInfo object for the
110     /// currently loaded program.  If there is no program loaded, throw an
111     /// exception.
112     ProgramInfo &getProgramInfo() const {
113       if (TheProgramInfo == 0)
114         throw "No program is loaded.";
115       return *TheProgramInfo;
116     }
117
118     /// getRuntimeInfo - Return a reference to the current RuntimeInfo object.
119     /// If there is no program running, throw an exception.
120     RuntimeInfo &getRuntimeInfo() const {
121       if (TheRuntimeInfo == 0)
122         throw "No program is running.";
123       return *TheRuntimeInfo;
124     }
125
126   private:   // Internal implementation methods
127
128     /// getCommand - This looks up the specified command using a fuzzy match.
129     /// If the string exactly matches a command or is an unambiguous prefix of a
130     /// command, it returns the command.  Otherwise it throws an exception
131     /// indicating the possible ambiguous choices.
132     CLICommand *getCommand(const std::string &Command);
133
134     /// askYesNo - Ask the user a question, and demand a yes/no response.  If
135     /// the user says yes, return true.
136     bool askYesNo(const std::string &Message) const;
137
138     /// printProgramLocation - Given a loaded and created child process that has
139     /// stopped, print its current source location.
140     void printProgramLocation(bool PrintLocation = true);
141
142     /// eliminateRunInfo - We are about to run the program.  Forget any state
143     /// about how the program used to be stopped.
144     void eliminateRunInfo();
145
146     /// programStoppedSuccessfully - This method updates internal data
147     /// structures to reflect the fact that the program just executed a while,
148     /// and has successfully stopped.
149     void programStoppedSuccessfully();
150
151   public:   /// Builtin debugger commands, invokable by the user
152     // Program startup and shutdown options
153     void fileCommand(std::string &Options);   // file
154     void createCommand(std::string &Options); // create
155     void killCommand(std::string &Options);   // kill
156     void quitCommand(std::string &Options);   // quit
157
158     // Program execution commands
159     void runCommand(std::string &Options);    // run|r
160     void contCommand(std::string &Options);   // cont|c|fg
161     void stepCommand(std::string &Options);   // step|s [count]
162     void nextCommand(std::string &Options);   // next|n [count]
163     void finishCommand(std::string &Options); // finish
164
165     // Stack frame commands
166     void backtraceCommand(std::string &Options); // backtrace|bt [count]
167     void upCommand(std::string &Options);        // up
168     void downCommand(std::string &Options);      // down
169     void frameCommand(std::string &Options);     // frame
170
171
172     // Breakpoint related commands
173     void breakCommand(std::string &Options);  // break|b <id>
174
175     // Miscellaneous commands
176     void infoCommand(std::string &Options);   // info
177     void listCommand(std::string &Options);   // list
178     void setCommand(std::string &Options);    // set
179     void showCommand(std::string &Options);   // show
180     void helpCommand(std::string &Options);   // help
181
182   private:
183     /// startProgramRunning - If the program has been updated, reload it, then
184     /// start executing the program.
185     void startProgramRunning();
186
187     /// printSourceLine - Print the specified line of the current source file.
188     /// If the specified line is invalid (the source file could not be loaded or
189     /// the line number is out of range), don't print anything, but return true.
190     bool printSourceLine(unsigned LineNo);
191
192     /// parseLineSpec - Parses a line specifier, for use by the 'list' command.
193     /// If SourceFile is returned as a void pointer, then it was not specified.
194     /// If the line specifier is invalid, an exception is thrown.
195     void parseLineSpec(std::string &LineSpec, const SourceFile *&SourceFile,
196                        unsigned &LineNo);
197
198     /// parseProgramOptions - This method parses the Options string and loads it
199     /// as options to be passed to the program.  This is used by the run command
200     /// and by 'set args'.
201     void parseProgramOptions(std::string &Options);
202   };
203 }
204
205 #endif