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