Initial implementation of some source-level debugging stuff
[oota-llvm.git] / include / llvm / Debugger / RuntimeInfo.h
1 //===- RuntimeInfo.h - Information about running program --------*- 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 classes that capture various pieces of information about
11 // the currently executing, but stopped, program.  One instance of this object
12 // is created every time a program is stopped, and destroyed every time it
13 // starts running again.  This object's main goal is to make access to runtime
14 // information easy and efficient, by caching information as requested.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_DEBUGGER_RUNTIMEINFO_H
19 #define LLVM_DEBUGGER_RUNTIMEINFO_H
20
21 #include <vector>
22
23 namespace llvm {
24   class ProgramInfo;
25   class RuntimeInfo;
26   class InferiorProcess;
27   class GlobalVariable;
28   class SourceFileInfo;
29
30   /// StackFrame - One instance of this structure is created for each stack
31   /// frame that is active in the program.
32   ///
33   class StackFrame {
34     RuntimeInfo &RI;
35     void *FrameID;
36     const GlobalVariable *FunctionDesc;
37
38     /// LineNo, ColNo, FileInfo - This information indicates WHERE in the source
39     /// code for the program the stack frame is located.
40     unsigned LineNo, ColNo;
41     const SourceFileInfo *SourceInfo;
42   public:
43     StackFrame(RuntimeInfo &RI, void *ParentFrameID);
44     
45     StackFrame &operator=(const StackFrame &RHS) {
46       FrameID = RHS.FrameID;
47       FunctionDesc = RHS.FunctionDesc;
48       return *this;
49     }
50
51     /// getFrameID - return the low-level opaque frame ID of this stack frame.
52     ///
53     void *getFrameID() const { return FrameID; }
54
55     /// getFunctionDesc - Return the descriptor for the function that contains
56     /// this stack frame, or null if it is unknown.
57     ///
58     const GlobalVariable *getFunctionDesc();
59
60     /// getSourceLocation - Return the source location that this stack frame is
61     /// sitting at.
62     void getSourceLocation(unsigned &LineNo, unsigned &ColNo,
63                            const SourceFileInfo *&SourceInfo);
64   };
65
66
67   /// RuntimeInfo - This class collects information about the currently running
68   /// process.  It is created whenever the program stops execution for the
69   /// debugger, and destroyed whenver execution continues.
70   class RuntimeInfo {
71     /// ProgInfo - This object contains static information about the program.
72     ///
73     ProgramInfo *ProgInfo;
74
75     /// IP - This object contains information about the actual inferior process
76     /// that we are communicating with and aggregating information from.
77     const InferiorProcess &IP;
78
79     /// CallStack - This caches information about the current stack trace of the
80     /// program.  This is lazily computed as needed.
81     std::vector<StackFrame> CallStack;
82     
83     /// CurrentFrame - The user can traverse the stack frame with the
84     /// up/down/frame family of commands.  This index indicates the current
85     /// stack frame.
86     unsigned CurrentFrame;
87
88   public:
89     RuntimeInfo(ProgramInfo *PI, const InferiorProcess &ip)
90       : ProgInfo(PI), IP(ip), CurrentFrame(0) {
91       // Make sure that the top of stack has been materialized.  If this throws
92       // an exception, something is seriously wrong and the RuntimeInfo object
93       // would be unusable anyway.
94       getStackFrame(0);
95     }
96
97     ProgramInfo &getProgramInfo() { return *ProgInfo; }
98     const InferiorProcess &getInferiorProcess() const { return IP; }
99
100     //===------------------------------------------------------------------===//
101     // Methods for inspecting the call stack of the program.
102     //
103
104     /// getStackFrame - Materialize the specified stack frame and return it.  If
105     /// the specified ID is off of the bottom of the stack, throw an exception
106     /// indicating the problem.
107     StackFrame &getStackFrame(unsigned ID) {
108       if (ID >= CallStack.size())
109         materializeFrame(ID);
110       return CallStack[ID];
111     }
112
113     /// getCurrentFrame - Return the current stack frame object that the user is
114     /// inspecting.
115     StackFrame &getCurrentFrame() {
116       assert(CallStack.size() > CurrentFrame &&
117              "Must have materialized frame before making it current!");
118       return CallStack[CurrentFrame];
119     }
120
121     /// getCurrentFrameIdx - Return the current frame the user is inspecting.
122     ///
123     unsigned getCurrentFrameIdx() const { return CurrentFrame; }
124
125     /// setCurrentFrameIdx - Set the current frame index to the specified value.
126     /// Note that the specified frame must have been materialized with
127     /// getStackFrame before it can be made current.
128     void setCurrentFrameIdx(unsigned Idx) {
129       assert(Idx < CallStack.size() &&
130              "Must materialize frame before making it current!");
131       CurrentFrame = Idx;
132     }
133   private:
134     /// materializeFrame - Create and process all frames up to and including the
135     /// specified frame number.  This throws an exception if the specified frame
136     /// ID is nonexistant.
137     void materializeFrame(unsigned ID);
138   };
139 }
140
141 #endif