Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//===-- DWARFExpression.h ---------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_DWARFExpression_h_
#define liblldb_DWARFExpression_h_

#include "lldb/Core/Address.h"
#include "lldb/Core/Disassembler.h"
#include "lldb/Core/Scalar.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-private.h"
#include <functional>

class DWARFCompileUnit;

namespace lldb_private {

//----------------------------------------------------------------------
/// @class DWARFExpression DWARFExpression.h "lldb/Expression/DWARFExpression.h"
/// @brief Encapsulates a DWARF location expression and interprets it.
///
/// DWARF location expressions are used in two ways by LLDB.  The first
/// use is to find entities specified in the debug information, since
/// their locations are specified in precisely this language.  The second
/// is to interpret expressions without having to run the target in cases
/// where the overhead from copying JIT-compiled code into the target is
/// too high or where the target cannot be run.  This class encapsulates
/// a single DWARF location expression or a location list and interprets
/// it.
//----------------------------------------------------------------------
class DWARFExpression {
public:
  enum LocationListFormat : uint8_t {
    NonLocationList,     // Not a location list
    RegularLocationList, // Location list format used in non-split dwarf files
    SplitDwarfLocationList, // Location list format used in split dwarf files
  };

  //------------------------------------------------------------------
  /// Constructor
  //------------------------------------------------------------------
  explicit DWARFExpression(DWARFCompileUnit *dwarf_cu);

  //------------------------------------------------------------------
  /// Constructor
  ///
  /// @param[in] data
  ///     A data extractor configured to read the DWARF location expression's
  ///     bytecode.
  ///
  /// @param[in] data_offset
  ///     The offset of the location expression in the extractor.
  ///
  /// @param[in] data_length
  ///     The byte length of the location expression.
  //------------------------------------------------------------------
  DWARFExpression(lldb::ModuleSP module, const DataExtractor &data,
                  DWARFCompileUnit *dwarf_cu, lldb::offset_t data_offset,
                  lldb::offset_t data_length);

  //------------------------------------------------------------------
  /// Copy constructor
  //------------------------------------------------------------------
  DWARFExpression(const DWARFExpression &rhs);

  //------------------------------------------------------------------
  /// Destructor
  //------------------------------------------------------------------
  virtual ~DWARFExpression();

  //------------------------------------------------------------------
  /// Print the description of the expression to a stream
  ///
  /// @param[in] s
  ///     The stream to print to.
  ///
  /// @param[in] level
  ///     The level of verbosity to use.
  ///
  /// @param[in] location_list_base_addr
  ///     If this is a location list based expression, this is the
  ///     address of the object that owns it. NOTE: this value is
  ///     different from the DWARF version of the location list base
  ///     address which is compile unit relative. This base address
  ///     is the address of the object that owns the location list.
  ///
  /// @param[in] abi
  ///     An optional ABI plug-in that can be used to resolve register
  ///     names.
  //------------------------------------------------------------------
  void GetDescription(Stream *s, lldb::DescriptionLevel level,
                      lldb::addr_t location_list_base_addr, ABI *abi) const;

  //------------------------------------------------------------------
  /// Return true if the location expression contains data
  //------------------------------------------------------------------
  bool IsValid() const;

  //------------------------------------------------------------------
  /// Return true if a location list was provided
  //------------------------------------------------------------------
  bool IsLocationList() const;

  //------------------------------------------------------------------
  /// Search for a load address in the location list
  ///
  /// @param[in] process
  ///     The process to use when resolving the load address
  ///
  /// @param[in] addr
  ///     The address to resolve
  ///
  /// @return
  ///     True if IsLocationList() is true and the address was found;
  ///     false otherwise.
  //------------------------------------------------------------------
  //    bool
  //    LocationListContainsLoadAddress (Process* process, const Address &addr)
  //    const;
  //
  bool LocationListContainsAddress(lldb::addr_t loclist_base_addr,
                                   lldb::addr_t addr) const;

  //------------------------------------------------------------------
  /// If a location is not a location list, return true if the location
  /// contains a DW_OP_addr () opcode in the stream that matches \a
  /// file_addr. If file_addr is LLDB_INVALID_ADDRESS, the this
  /// function will return true if the variable there is any DW_OP_addr
  /// in a location that (yet still is NOT a location list). This helps
  /// us detect if a variable is a global or static variable since
  /// there is no other indication from DWARF debug info.
  ///
  /// @param[in] op_addr_idx
  ///     The DW_OP_addr index to retrieve in case there is more than
  ///     one DW_OP_addr opcode in the location byte stream.
  ///
  /// @param[out] error
  ///     If the location stream contains unknown DW_OP opcodes or the
  ///     data is missing, \a error will be set to \b true.
  ///
  /// @return
  ///     LLDB_INVALID_ADDRESS if the location doesn't contain a
  ///     DW_OP_addr for \a op_addr_idx, otherwise a valid file address
  //------------------------------------------------------------------
  lldb::addr_t GetLocation_DW_OP_addr(uint32_t op_addr_idx, bool &error) const;

  bool Update_DW_OP_addr(lldb::addr_t file_addr);

  bool ContainsThreadLocalStorage() const;

  bool LinkThreadLocalStorage(
      lldb::ModuleSP new_module_sp,
      std::function<lldb::addr_t(lldb::addr_t file_addr)> const
          &link_address_callback);

  //------------------------------------------------------------------
  /// Make the expression parser read its location information from a
  /// given data source.  Does not change the offset and length
  ///
  /// @param[in] data
  ///     A data extractor configured to read the DWARF location expression's
  ///     bytecode.
  //------------------------------------------------------------------
  void SetOpcodeData(const DataExtractor &data);

  //------------------------------------------------------------------
  /// Make the expression parser read its location information from a
  /// given data source
  ///
  /// @param[in] module_sp
  ///     The module that defines the DWARF expression.
  ///
  /// @param[in] data
  ///     A data extractor configured to read the DWARF location expression's
  ///     bytecode.
  ///
  /// @param[in] data_offset
  ///     The offset of the location expression in the extractor.
  ///
  /// @param[in] data_length
  ///     The byte length of the location expression.
  //------------------------------------------------------------------
  void SetOpcodeData(lldb::ModuleSP module_sp, const DataExtractor &data,
                     lldb::offset_t data_offset, lldb::offset_t data_length);

  //------------------------------------------------------------------
  /// Copy the DWARF location expression into a local buffer.
  ///
  /// It is a good idea to copy the data so we don't keep the entire
  /// object file worth of data around just for a few bytes of location
  /// expression. LLDB typically will mmap the entire contents of debug
  /// information files, and if we use SetOpcodeData, it will get a
  /// shared reference to all of this data for the and cause the object
  /// file to have to stay around. Even worse, a very very large ".a"
  /// that contains one or more .o files could end up being referenced.
  /// Location lists are typically small so even though we are copying
  /// the data, it shouldn't amount to that much for the variables we
  /// end up parsing.
  ///
  /// @param[in] module_sp
  ///     The module that defines the DWARF expression.
  ///
  /// @param[in] data
  ///     A data extractor configured to read and copy the DWARF
  ///     location expression's bytecode.
  ///
  /// @param[in] data_offset
  ///     The offset of the location expression in the extractor.
  ///
  /// @param[in] data_length
  ///     The byte length of the location expression.
  //------------------------------------------------------------------
  void CopyOpcodeData(lldb::ModuleSP module_sp, const DataExtractor &data,
                      lldb::offset_t data_offset, lldb::offset_t data_length);

  void CopyOpcodeData(const void *data, lldb::offset_t data_length,
                      lldb::ByteOrder byte_order, uint8_t addr_byte_size);

  void CopyOpcodeData(uint64_t const_value,
                      lldb::offset_t const_value_byte_size,
                      uint8_t addr_byte_size);

  //------------------------------------------------------------------
  /// Tells the expression that it refers to a location list.
  ///
  /// @param[in] slide
  ///     This value should be a slide that is applied to any values
  ///     in the location list data so the values become zero based
  ///     offsets into the object that owns the location list. We need
  ///     to make location lists relative to the objects that own them
  ///     so we can relink addresses on the fly.
  //------------------------------------------------------------------
  void SetLocationListSlide(lldb::addr_t slide);

  //------------------------------------------------------------------
  /// Return the call-frame-info style register kind
  //------------------------------------------------------------------
  int GetRegisterKind();

  //------------------------------------------------------------------
  /// Set the call-frame-info style register kind
  ///
  /// @param[in] reg_kind
  ///     The register kind.
  //------------------------------------------------------------------
  void SetRegisterKind(lldb::RegisterKind reg_kind);

  //------------------------------------------------------------------
  /// Wrapper for the static evaluate function that accepts an
  /// ExecutionContextScope instead of an ExecutionContext and uses
  /// member variables to populate many operands
  //------------------------------------------------------------------
  bool Evaluate(ExecutionContextScope *exe_scope,
                lldb::addr_t loclist_base_load_addr,
                const Value *initial_value_ptr, const Value *object_address_ptr,
                Value &result, Status *error_ptr) const;

  //------------------------------------------------------------------
  /// Wrapper for the static evaluate function that uses member
  /// variables to populate many operands
  //------------------------------------------------------------------
  bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
                lldb::addr_t loclist_base_load_addr,
                const Value *initial_value_ptr, const Value *object_address_ptr,
                Value &result, Status *error_ptr) const;

  //------------------------------------------------------------------
  /// Evaluate a DWARF location expression in a particular context
  ///
  /// @param[in] exe_ctx
  ///     The execution context in which to evaluate the location
  ///     expression.  The location expression may access the target's
  ///     memory, especially if it comes from the expression parser.
  ///
  /// @param[in] opcode_ctx
  ///     The module which defined the expression.
  ///
  /// @param[in] opcodes
  ///     This is a static method so the opcodes need to be provided
  ///     explicitly.
  ///
  /// @param[in] expr_locals
  ///     If the location expression was produced by the expression parser,
  ///     the list of local variables referenced by the DWARF expression.
  ///     This list should already have been populated during parsing;
  ///     the DWARF expression refers to variables by index.  Can be NULL if
  ///     the location expression uses no locals.
  ///
  /// @param[in] decl_map
  ///     If the location expression was produced by the expression parser,
  ///     the list of external variables referenced by the location
  ///     expression.  Can be NULL if the location expression uses no
  ///     external variables.
  ///
  ///  @param[in] reg_ctx
  ///     An optional parameter which provides a RegisterContext for use
  ///     when evaluating the expression (i.e. for fetching register values).
  ///     Normally this will come from the ExecutionContext's StackFrame but
  ///     in the case where an expression needs to be evaluated while building
  ///     the stack frame list, this short-cut is available.
  ///
  /// @param[in] offset
  ///     The offset of the location expression in the data extractor.
  ///
  /// @param[in] length
  ///     The length in bytes of the location expression.
  ///
  /// @param[in] reg_set
  ///     The call-frame-info style register kind.
  ///
  /// @param[in] initial_value_ptr
  ///     A value to put on top of the interpreter stack before evaluating
  ///     the expression, if the expression is parametrized.  Can be NULL.
  ///
  /// @param[in] result
  ///     A value into which the result of evaluating the expression is
  ///     to be placed.
  ///
  /// @param[in] error_ptr
  ///     If non-NULL, used to report errors in expression evaluation.
  ///
  /// @return
  ///     True on success; false otherwise.  If error_ptr is non-NULL,
  ///     details of the failure are provided through it.
  //------------------------------------------------------------------
  static bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
                       lldb::ModuleSP opcode_ctx, const DataExtractor &opcodes,
                       DWARFCompileUnit *dwarf_cu, const lldb::offset_t offset,
                       const lldb::offset_t length,
                       const lldb::RegisterKind reg_set,
                       const Value *initial_value_ptr,
                       const Value *object_address_ptr, Value &result,
                       Status *error_ptr);

  bool GetExpressionData(DataExtractor &data) const {
    data = m_data;
    return data.GetByteSize() > 0;
  }

  bool DumpLocationForAddress(Stream *s, lldb::DescriptionLevel level,
                              lldb::addr_t loclist_base_load_addr,
                              lldb::addr_t address, ABI *abi);

  static size_t LocationListSize(const DWARFCompileUnit *dwarf_cu,
                                 const DataExtractor &debug_loc_data,
                                 lldb::offset_t offset);

  static bool PrintDWARFExpression(Stream &s, const DataExtractor &data,
                                   int address_size, int dwarf_ref_size,
                                   bool location_expression);

  static void PrintDWARFLocationList(Stream &s, const DWARFCompileUnit *cu,
                                     const DataExtractor &debug_loc_data,
                                     lldb::offset_t offset);

  bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op);

protected:
  //------------------------------------------------------------------
  /// Pretty-prints the location expression to a stream
  ///
  /// @param[in] stream
  ///     The stream to use for pretty-printing.
  ///
  /// @param[in] offset
  ///     The offset into the data buffer of the opcodes to be printed.
  ///
  /// @param[in] length
  ///     The length in bytes of the opcodes to be printed.
  ///
  /// @param[in] level
  ///     The level of detail to use in pretty-printing.
  ///
  /// @param[in] abi
  ///     An optional ABI plug-in that can be used to resolve register
  ///     names.
  //------------------------------------------------------------------
  void DumpLocation(Stream *s, lldb::offset_t offset, lldb::offset_t length,
                    lldb::DescriptionLevel level, ABI *abi) const;

  bool GetLocation(lldb::addr_t base_addr, lldb::addr_t pc,
                   lldb::offset_t &offset, lldb::offset_t &len);

  static bool AddressRangeForLocationListEntry(
      const DWARFCompileUnit *dwarf_cu, const DataExtractor &debug_loc_data,
      lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc);

  bool GetOpAndEndOffsets(StackFrame &frame, lldb::offset_t &op_offset,
                          lldb::offset_t &end_offset);

  //------------------------------------------------------------------
  /// Classes that inherit from DWARFExpression can see and modify these
  //------------------------------------------------------------------

  lldb::ModuleWP m_module_wp; ///< Module which defined this expression.
  DataExtractor m_data; ///< A data extractor capable of reading opcode bytes
  DWARFCompileUnit *m_dwarf_cu; ///< The DWARF compile unit this expression
                                ///belongs to. It is used
  ///< to evaluate values indexing into the .debug_addr section (e.g.
  ///< DW_OP_GNU_addr_index, DW_OP_GNU_const_index)
  lldb::RegisterKind
      m_reg_kind; ///< One of the defines that starts with LLDB_REGKIND_
  lldb::addr_t m_loclist_slide; ///< A value used to slide the location list
                                ///offsets so that
  ///< they are relative to the object that owns the location list
  ///< (the function for frame base and variable location lists)
};

} // namespace lldb_private

#endif // liblldb_DWARFExpression_h_