]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Target/StackFrame.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / include / lldb / Target / StackFrame.h
1 //===-- StackFrame.h --------------------------------------------*- 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 #ifndef liblldb_StackFrame_h_
11 #define liblldb_StackFrame_h_
12
13 // C Includes
14 // C++ Includes
15 #include <memory>
16 #include <mutex>
17
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/Core/Error.h"
21 #include "lldb/Core/Flags.h"
22 #include "lldb/Core/Scalar.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Core/UserID.h"
25 #include "lldb/Core/ValueObjectList.h"
26 #include "lldb/Symbol/SymbolContext.h"
27 #include "lldb/Target/ExecutionContextScope.h"
28 #include "lldb/Target/StackID.h"
29
30 namespace lldb_private {
31
32 /// @class StackFrame StackFrame.h "lldb/Target/StackFrame.h"
33 ///
34 /// @brief This base class provides an interface to stack frames.
35 ///
36 /// StackFrames may have a Canonical Frame Address (CFA) or not.
37 /// A frame may have a plain pc value or it may have a pc value + stop_id
38 /// to indicate a specific point in the debug session so the correct section
39 /// load list is used for symbolication.
40 ///
41 /// Local variables may be available, or not.  A register context may be
42 /// available, or not.
43
44 class StackFrame : public ExecutionContextScope,
45                    public std::enable_shared_from_this<StackFrame> {
46 public:
47   enum ExpressionPathOption {
48     eExpressionPathOptionCheckPtrVsMember = (1u << 0),
49     eExpressionPathOptionsNoFragileObjcIvar = (1u << 1),
50     eExpressionPathOptionsNoSyntheticChildren = (1u << 2),
51     eExpressionPathOptionsNoSyntheticArrayRange = (1u << 3),
52     eExpressionPathOptionsAllowDirectIVarAccess = (1u << 4),
53     eExpressionPathOptionsInspectAnonymousUnions = (1u << 5)
54   };
55
56   //------------------------------------------------------------------
57   /// Construct a StackFrame object without supplying a RegisterContextSP.
58   ///
59   /// This is the one constructor that doesn't take a RegisterContext
60   /// parameter.  This ctor may be called when creating a history StackFrame;
61   /// these are used if we've collected a stack trace of pc addresses at
62   /// some point in the past.  We may only have pc values.  We may have pc
63   /// values and the stop_id when the stack trace was recorded.  We may have a
64   /// CFA, or more likely, we won't.
65   ///
66   /// @param [in] thread_sp
67   ///   The Thread that this frame belongs to.
68   ///
69   /// @param [in] frame_idx
70   ///   This StackFrame's frame index number in the Thread.  If inlined stack
71   ///   frames are being created, this may differ from the concrete_frame_idx
72   ///   which is the frame index without any inlined stack frames.
73   ///
74   /// @param [in] concrete_frame_idx
75   ///   The StackFrame's frame index number in the Thread without any inlined
76   ///   stack frames being included in the index.
77   ///
78   /// @param [in] cfa
79   ///   The Canonical Frame Address (this terminology from DWARF) for this
80   ///   stack frame.  The CFA for a stack frame does not change over the
81   ///   span of the stack frame's existence.  It is often the value of the
82   ///   caller's stack pointer before the call instruction into this frame's
83   ///   function.  It is usually not the same as the frame pointer register's
84   ///   value.
85   ///
86   /// @param [in] cfa_is_valid
87   ///   A history stack frame may not have a CFA value collected.  We want to
88   ///   distinguish between "no CFA available" and a CFA of
89   ///   LLDB_INVALID_ADDRESS.
90   ///
91   /// @param [in] pc
92   ///   The current pc value of this stack frame.
93   ///
94   /// @param [in] stop_id
95   ///   The stop_id which should be used when looking up symbols for the pc
96   ///   value,
97   ///   if appropriate.  This argument is ignored if stop_id_is_valid is false.
98   ///
99   /// @param [in] stop_id_is_valid
100   ///   If the stop_id argument provided is not needed for this StackFrame, this
101   ///   should be false.  If this is a history stack frame and we know the
102   ///   stop_id
103   ///   when the pc value was collected, that stop_id should be provided and
104   ///   this
105   ///   will be true.
106   ///
107   /// @param [in] is_history_frame
108   ///   If this is a historical stack frame -- possibly without CFA or registers
109   ///   or
110   ///   local variables -- then this should be set to true.
111   ///
112   /// @param [in] sc_ptr
113   ///   Optionally seed the StackFrame with the SymbolContext information that
114   ///   has
115   ///   already been discovered.
116   //------------------------------------------------------------------
117   StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
118              lldb::user_id_t concrete_frame_idx, lldb::addr_t cfa,
119              bool cfa_is_valid, lldb::addr_t pc, uint32_t stop_id,
120              bool stop_id_is_valid, bool is_history_frame,
121              const SymbolContext *sc_ptr);
122
123   StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
124              lldb::user_id_t concrete_frame_idx,
125              const lldb::RegisterContextSP &reg_context_sp, lldb::addr_t cfa,
126              lldb::addr_t pc, const SymbolContext *sc_ptr);
127
128   StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx,
129              lldb::user_id_t concrete_frame_idx,
130              const lldb::RegisterContextSP &reg_context_sp, lldb::addr_t cfa,
131              const Address &pc, const SymbolContext *sc_ptr);
132
133   ~StackFrame() override;
134
135   lldb::ThreadSP GetThread() const { return m_thread_wp.lock(); }
136
137   StackID &GetStackID();
138
139   //------------------------------------------------------------------
140   /// Get an Address for the current pc value in this StackFrame.
141   ///
142   /// May not be the same as the actual PC value for inlined stack frames.
143   ///
144   /// @return
145   ///   The Address object set to the current PC value.
146   //------------------------------------------------------------------
147   const Address &GetFrameCodeAddress();
148
149   //------------------------------------------------------------------
150   /// Change the pc value for a given thread.
151   ///
152   /// Change the current pc value for the frame on this thread.
153   ///
154   /// @param[in] pc
155   ///     The load address that the pc will be set to.
156   ///
157   /// @return
158   ///     true if the pc was changed.  false if this failed -- possibly
159   ///     because this frame is not a live StackFrame.
160   //------------------------------------------------------------------
161   bool ChangePC(lldb::addr_t pc);
162
163   //------------------------------------------------------------------
164   /// Provide a SymbolContext for this StackFrame's current pc value.
165   ///
166   /// The StackFrame maintains this SymbolContext and adds additional
167   /// information
168   /// to it on an as-needed basis.  This helps to avoid different functions
169   /// looking up symbolic information for a given pc value multiple times.
170   ///
171   /// @params [in] resolve_scope
172   ///   Flags from the SymbolContextItem enumerated type which specify what
173   ///   type of symbol context is needed by this caller.
174   ///
175   /// @return
176   ///   A SymbolContext reference which includes the types of information
177   ///   requested by resolve_scope, if they are available.
178   //------------------------------------------------------------------
179   const SymbolContext &GetSymbolContext(uint32_t resolve_scope);
180
181   //------------------------------------------------------------------
182   /// Return the Canonical Frame Address (DWARF term) for this frame.
183   ///
184   /// The CFA is typically the value of the stack pointer register before
185   /// the call invocation is made.  It will not change during the lifetime
186   /// of a stack frame.  It is often not the same thing as the frame pointer
187   /// register value.
188   ///
189   /// Live StackFrames will always have a CFA but other types of frames may
190   /// not be able to supply one.
191   ///
192   /// @param [out] value
193   ///   The address of the CFA for this frame, if available.
194   ///
195   /// @param [out] error_ptr
196   ///   If there is an error determining the CFA address, this may contain a
197   ///   string explaining the failure.
198   ///
199   /// @return
200   ///   Returns true if the CFA value was successfully set in value.  Some
201   ///   frames may be unable to provide this value; they will return false.
202   //------------------------------------------------------------------
203   bool GetFrameBaseValue(Scalar &value, Error *error_ptr);
204
205   //------------------------------------------------------------------
206   /// Get the DWARFExpression corresponding to the Canonical Frame Address.
207   ///
208   /// Often a register (bp), but sometimes a register + offset.
209   ///
210   /// @param [out] error_ptr
211   ///   If there is an error determining the CFA address, this may contain a
212   ///   string explaining the failure.
213   ///
214   /// @return
215   ///   Returns the corresponding DWARF expression, or NULL.
216   //------------------------------------------------------------------
217   DWARFExpression *GetFrameBaseExpression(Error *error_ptr);
218
219   //------------------------------------------------------------------
220   /// Get the current lexical scope block for this StackFrame, if possible.
221   ///
222   /// If debug information is available for this stack frame, return a
223   /// pointer to the innermost lexical Block that the frame is currently
224   /// executing.
225   ///
226   /// @return
227   ///   A pointer to the current Block.  nullptr is returned if this can
228   ///   not be provided.
229   //------------------------------------------------------------------
230   Block *GetFrameBlock();
231
232   //------------------------------------------------------------------
233   /// Get the RegisterContext for this frame, if possible.
234   ///
235   /// Returns a shared pointer to the RegisterContext for this stack frame.
236   /// Only a live StackFrame object will be able to return a RegisterContext -
237   /// callers must be prepared for an empty shared pointer being returned.
238   ///
239   /// Even a live StackFrame RegisterContext may not be able to provide all
240   /// registers.  Only the currently executing frame (frame 0) can reliably
241   /// provide every register in the register context.
242   ///
243   /// @return
244   ///   The RegisterContext shared point for this frame.
245   //------------------------------------------------------------------
246   lldb::RegisterContextSP GetRegisterContext();
247
248   const lldb::RegisterContextSP &GetRegisterContextSP() const {
249     return m_reg_context_sp;
250   }
251
252   //------------------------------------------------------------------
253   /// Retrieve the list of variables that are in scope at this StackFrame's pc.
254   ///
255   /// A frame that is not live may return an empty VariableList for a given
256   /// pc value even though variables would be available at this point if
257   /// it were a live stack frame.
258   ///
259   /// @param[in] get_file_globals
260   ///     Whether to also retrieve compilation-unit scoped variables
261   ///     that are visible to the entire compilation unit (e.g. file
262   ///     static in C, globals that are homed in this CU).
263   ///
264   /// @return
265   ///     A pointer to a list of variables.
266   //------------------------------------------------------------------
267   VariableList *GetVariableList(bool get_file_globals);
268
269   //------------------------------------------------------------------
270   /// Retrieve the list of variables that are in scope at this StackFrame's pc.
271   ///
272   /// A frame that is not live may return an empty VariableListSP for a
273   /// given pc value even though variables would be available at this point
274   /// if it were a live stack frame.
275   ///
276   /// @param[in] get_file_globals
277   ///     Whether to also retrieve compilation-unit scoped variables
278   ///     that are visible to the entire compilation unit (e.g. file
279   ///     static in C, globals that are homed in this CU).
280   ///
281   /// @return
282   ///     A pointer to a list of variables.
283   //------------------------------------------------------------------
284   lldb::VariableListSP
285   GetInScopeVariableList(bool get_file_globals,
286                          bool must_have_valid_location = false);
287
288   //------------------------------------------------------------------
289   /// Create a ValueObject for a variable name / pathname, possibly
290   /// including simple dereference/child selection syntax.
291   ///
292   /// @param[in] var_expr
293   ///     The string specifying a variable to base the VariableObject off
294   ///     of.
295   ///
296   /// @param[in] use_dynamic
297   ///     Whether the correct dynamic type of an object pointer should be
298   ///     determined before creating the object, or if the static type is
299   ///     sufficient.  One of the DynamicValueType enumerated values.
300   ///
301   /// @param[in] options
302   ///     An unsigned integer of flags, values from
303   ///     StackFrame::ExpressionPathOption
304   ///     enum.
305   /// @param[in] var_sp
306   ///     A VariableSP that will be set to the variable described in the
307   ///     var_expr path.
308   ///
309   /// @param[in] error
310   ///     Record any errors encountered while evaluating var_expr.
311   ///
312   /// @return
313   ///     A shared pointer to the ValueObject described by var_expr.
314   //------------------------------------------------------------------
315   lldb::ValueObjectSP GetValueForVariableExpressionPath(
316       llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
317       uint32_t options, lldb::VariableSP &var_sp, Error &error);
318
319   //------------------------------------------------------------------
320   /// Determine whether this StackFrame has debug information available or not
321   ///
322   /// @return
323   //    true if debug information is available for this frame (function,
324   //    compilation unit, block, etc.)
325   //------------------------------------------------------------------
326   bool HasDebugInformation();
327
328   //------------------------------------------------------------------
329   /// Return the disassembly for the instructions of this StackFrame's function
330   /// as a single C string.
331   ///
332   /// @return
333   //    C string with the assembly instructions for this function.
334   //------------------------------------------------------------------
335   const char *Disassemble();
336
337   //------------------------------------------------------------------
338   /// Print a description for this frame using the frame-format formatter
339   /// settings.
340   ///
341   /// @param [in] strm
342   ///   The Stream to print the description to.
343   ///
344   /// @param [in] frame_marker
345   ///   Optional string that will be prepended to the frame output description.
346   //------------------------------------------------------------------
347   void DumpUsingSettingsFormat(Stream *strm,
348                                const char *frame_marker = nullptr);
349
350   //------------------------------------------------------------------
351   /// Print a description for this frame using a default format.
352   ///
353   /// @param [in] strm
354   ///   The Stream to print the description to.
355   ///
356   /// @param [in] show_frame_index
357   ///   Whether to print the frame number or not.
358   ///
359   /// @param [in] show_fullpaths
360   ///   Whether to print the full source paths or just the file base name.
361   //------------------------------------------------------------------
362   void Dump(Stream *strm, bool show_frame_index, bool show_fullpaths);
363
364   //------------------------------------------------------------------
365   /// Print a description of this stack frame and/or the source context/assembly
366   /// for this stack frame.
367   ///
368   /// @param[in] strm
369   ///   The Stream to send the output to.
370   ///
371   /// @param[in] show_frame_info
372   ///   If true, print the frame info by calling DumpUsingSettingsFormat().
373   ///
374   /// @param[in] show_source
375   ///   If true, print source or disassembly as per the user's settings.
376   ///
377   /// @param[in] frame_marker
378   ///   Passed to DumpUsingSettingsFormat() for the frame info printing.
379   ///
380   /// @return
381   ///   Returns true if successful.
382   //------------------------------------------------------------------
383   bool GetStatus(Stream &strm, bool show_frame_info, bool show_source,
384                  const char *frame_marker = nullptr);
385
386   //------------------------------------------------------------------
387   /// Query whether this frame is a concrete frame on the call stack,
388   /// or if it is an inlined frame derived from the debug information
389   /// and presented by the debugger.
390   ///
391   /// @return
392   ///   true if this is an inlined frame.
393   //------------------------------------------------------------------
394   bool IsInlined();
395
396   //------------------------------------------------------------------
397   /// Query this frame to find what frame it is in this Thread's StackFrameList.
398   ///
399   /// @return
400   ///   StackFrame index 0 indicates the currently-executing function.  Inline
401   ///   frames are included in this frame index count.
402   //------------------------------------------------------------------
403   uint32_t GetFrameIndex() const;
404
405   //------------------------------------------------------------------
406   /// Query this frame to find what frame it is in this Thread's StackFrameList,
407   /// not counting inlined frames.
408   ///
409   /// @return
410   ///   StackFrame index 0 indicates the currently-executing function.  Inline
411   ///   frames are not included in this frame index count; their concrete
412   ///   frame index will be the same as the concrete frame that they are
413   ///   derived from.
414   //------------------------------------------------------------------
415   uint32_t GetConcreteFrameIndex() const { return m_concrete_frame_index; }
416
417   //------------------------------------------------------------------
418   /// Create a ValueObject for a given Variable in this StackFrame.
419   ///
420   /// @params [in] variable_sp
421   ///   The Variable to base this ValueObject on
422   ///
423   /// @params [in] use_dynamic
424   ///     Whether the correct dynamic type of the variable should be
425   ///     determined before creating the ValueObject, or if the static type
426   ///     is sufficient.  One of the DynamicValueType enumerated values.
427   ///
428   /// @return
429   //    A ValueObject for this variable.
430   //------------------------------------------------------------------
431   lldb::ValueObjectSP
432   GetValueObjectForFrameVariable(const lldb::VariableSP &variable_sp,
433                                  lldb::DynamicValueType use_dynamic);
434
435   //------------------------------------------------------------------
436   /// Add an arbitrary Variable object (e.g. one that specifics a global or
437   /// static)
438   /// to a StackFrame's list of ValueObjects.
439   ///
440   /// @params [in] variable_sp
441   ///   The Variable to base this ValueObject on
442   ///
443   /// @params [in] use_dynamic
444   ///     Whether the correct dynamic type of the variable should be
445   ///     determined before creating the ValueObject, or if the static type
446   ///     is sufficient.  One of the DynamicValueType enumerated values.
447   ///
448   /// @return
449   //    A ValueObject for this variable.
450   //------------------------------------------------------------------
451   lldb::ValueObjectSP TrackGlobalVariable(const lldb::VariableSP &variable_sp,
452                                           lldb::DynamicValueType use_dynamic);
453
454   //------------------------------------------------------------------
455   /// Query this frame to determine what the default language should be
456   /// when parsing expressions given the execution context.
457   ///
458   /// @return
459   ///   The language of the frame if known, else lldb::eLanguageTypeUnknown.
460   //------------------------------------------------------------------
461   lldb::LanguageType GetLanguage();
462
463   // similar to GetLanguage(), but is allowed to take a potentially incorrect
464   // guess
465   // if exact information is not available
466   lldb::LanguageType GuessLanguage();
467
468   //------------------------------------------------------------------
469   /// Attempt to econstruct the ValueObject for a given raw address touched by
470   /// the current instruction.  The ExpressionPath should indicate how to get
471   /// to this value using "frame variable."
472   ///
473   /// @params [in] addr
474   ///   The raw address.
475   ///
476   /// @return
477   ///   The ValueObject if found.  If valid, it has a valid ExpressionPath.
478   //------------------------------------------------------------------
479   lldb::ValueObjectSP GuessValueForAddress(lldb::addr_t addr);
480
481   //------------------------------------------------------------------
482   /// Attempt to reconstruct the ValueObject for the address contained in a
483   /// given register plus an offset.  The ExpressionPath should indicate how to
484   /// get to this value using "frame variable."
485   ///
486   /// @params [in] reg
487   ///   The name of the register.
488   ///
489   /// @params [in] offset
490   ///   The offset from the register.  Particularly important for sp...
491   ///
492   /// @return
493   ///   The ValueObject if found.  If valid, it has a valid ExpressionPath.
494   //------------------------------------------------------------------
495   lldb::ValueObjectSP GuessValueForRegisterAndOffset(ConstString reg,
496                                                      int64_t offset);
497
498   //------------------------------------------------------------------
499   // lldb::ExecutionContextScope pure virtual functions
500   //------------------------------------------------------------------
501   lldb::TargetSP CalculateTarget() override;
502
503   lldb::ProcessSP CalculateProcess() override;
504
505   lldb::ThreadSP CalculateThread() override;
506
507   lldb::StackFrameSP CalculateStackFrame() override;
508
509   void CalculateExecutionContext(ExecutionContext &exe_ctx) override;
510
511 protected:
512   friend class StackFrameList;
513
514   void SetSymbolContextScope(SymbolContextScope *symbol_scope);
515
516   void UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame);
517
518   void UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame);
519
520   bool HasCachedData() const;
521
522 private:
523   //------------------------------------------------------------------
524   // For StackFrame only
525   //------------------------------------------------------------------
526   lldb::ThreadWP m_thread_wp;
527   uint32_t m_frame_index;
528   uint32_t m_concrete_frame_index;
529   lldb::RegisterContextSP m_reg_context_sp;
530   StackID m_id;
531   Address m_frame_code_addr; // The frame code address (might not be the same as
532                              // the actual PC for inlined frames) as a
533                              // section/offset address
534   SymbolContext m_sc;
535   Flags m_flags;
536   Scalar m_frame_base;
537   Error m_frame_base_error;
538   bool m_cfa_is_valid; // Does this frame have a CFA?  Different from CFA ==
539                        // LLDB_INVALID_ADDRESS
540   uint32_t m_stop_id;
541   bool m_stop_id_is_valid; // Does this frame have a stop_id?  Use it when
542                            // referring to the m_frame_code_addr.
543   bool m_is_history_frame;
544   lldb::VariableListSP m_variable_list_sp;
545   ValueObjectList m_variable_list_value_objects; // Value objects for each
546                                                  // variable in
547                                                  // m_variable_list_sp
548   StreamString m_disassembly;
549   std::recursive_mutex m_mutex;
550
551   DISALLOW_COPY_AND_ASSIGN(StackFrame);
552 };
553
554 } // namespace lldb_private
555
556 #endif // liblldb_StackFrame_h_