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