Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Target/StackFrame.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- StackFrame.cpp ----------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "lldb/Target/StackFrame.h"
10
#include "lldb/Core/Debugger.h"
11
#include "lldb/Core/Disassembler.h"
12
#include "lldb/Core/FormatEntity.h"
13
#include "lldb/Core/Mangled.h"
14
#include "lldb/Core/Module.h"
15
#include "lldb/Core/Value.h"
16
#include "lldb/Core/ValueObjectConstResult.h"
17
#include "lldb/Core/ValueObjectMemory.h"
18
#include "lldb/Core/ValueObjectVariable.h"
19
#include "lldb/Symbol/CompileUnit.h"
20
#include "lldb/Symbol/Function.h"
21
#include "lldb/Symbol/Symbol.h"
22
#include "lldb/Symbol/SymbolContextScope.h"
23
#include "lldb/Symbol/SymbolFile.h"
24
#include "lldb/Symbol/Type.h"
25
#include "lldb/Symbol/VariableList.h"
26
#include "lldb/Target/ABI.h"
27
#include "lldb/Target/ExecutionContext.h"
28
#include "lldb/Target/Process.h"
29
#include "lldb/Target/RegisterContext.h"
30
#include "lldb/Target/StackFrameRecognizer.h"
31
#include "lldb/Target/Target.h"
32
#include "lldb/Target/Thread.h"
33
#include "lldb/Utility/LLDBLog.h"
34
#include "lldb/Utility/Log.h"
35
#include "lldb/Utility/RegisterValue.h"
36
37
#include "lldb/lldb-enumerations.h"
38
39
#include <memory>
40
41
using namespace lldb;
42
using namespace lldb_private;
43
44
// The first bits in the flags are reserved for the SymbolContext::Scope bits
45
// so we know if we have tried to look up information in our internal symbol
46
// context (m_sc) already.
47
5.58M
#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextLastItem) << 1)
48
4.27M
#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
49
49.8k
#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
50
29.1k
#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
51
14.5k
#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
52
53
StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
54
                       user_id_t unwind_frame_index, addr_t cfa,
55
                       bool cfa_is_valid, addr_t pc, StackFrame::Kind kind,
56
                       bool behaves_like_zeroth_frame,
57
                       const SymbolContext *sc_ptr)
58
182k
    : m_thread_wp(thread_sp), m_frame_index(frame_idx),
59
182k
      m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
60
182k
      m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
61
182k
      m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
62
182k
      m_stack_frame_kind(kind),
63
182k
      m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
64
182k
      m_variable_list_sp(), m_variable_list_value_objects(),
65
182k
      m_recognized_frame_sp(), m_disassembly(), m_mutex() {
66
  // If we don't have a CFA value, use the frame index for our StackID so that
67
  // recursive functions properly aren't confused with one another on a history
68
  // stack.
69
182k
  if (IsHistorical() && 
!m_cfa_is_valid0
) {
70
0
    m_id.SetCFA(m_frame_index);
71
0
  }
72
73
182k
  if (sc_ptr != nullptr) {
74
64
    m_sc = *sc_ptr;
75
64
    m_flags.Set(m_sc.GetResolvedMask());
76
64
  }
77
182k
}
78
79
StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
80
                       user_id_t unwind_frame_index,
81
                       const RegisterContextSP &reg_context_sp, addr_t cfa,
82
                       addr_t pc, bool behaves_like_zeroth_frame,
83
                       const SymbolContext *sc_ptr)
84
19.6k
    : m_thread_wp(thread_sp), m_frame_index(frame_idx),
85
19.6k
      m_concrete_frame_index(unwind_frame_index),
86
19.6k
      m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
87
19.6k
      m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
88
19.6k
      m_frame_base_error(), m_cfa_is_valid(true),
89
19.6k
      m_stack_frame_kind(StackFrame::Kind::Regular),
90
19.6k
      m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
91
19.6k
      m_variable_list_sp(), m_variable_list_value_objects(),
92
19.6k
      m_recognized_frame_sp(), m_disassembly(), m_mutex() {
93
19.6k
  if (sc_ptr != nullptr) {
94
0
    m_sc = *sc_ptr;
95
0
    m_flags.Set(m_sc.GetResolvedMask());
96
0
  }
97
98
19.6k
  if (reg_context_sp && !m_sc.target_sp) {
99
19.6k
    m_sc.target_sp = reg_context_sp->CalculateTarget();
100
19.6k
    if (m_sc.target_sp)
101
19.6k
      m_flags.Set(eSymbolContextTarget);
102
19.6k
  }
103
19.6k
}
104
105
StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
106
                       user_id_t unwind_frame_index,
107
                       const RegisterContextSP &reg_context_sp, addr_t cfa,
108
                       const Address &pc_addr, bool behaves_like_zeroth_frame,
109
                       const SymbolContext *sc_ptr)
110
123
    : m_thread_wp(thread_sp), m_frame_index(frame_idx),
111
123
      m_concrete_frame_index(unwind_frame_index),
112
123
      m_reg_context_sp(reg_context_sp),
113
123
      m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
114
123
           nullptr),
115
123
      m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
116
123
      m_frame_base_error(), m_cfa_is_valid(true),
117
123
      m_stack_frame_kind(StackFrame::Kind::Regular),
118
123
      m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
119
123
      m_variable_list_sp(), m_variable_list_value_objects(),
120
123
      m_recognized_frame_sp(), m_disassembly(), m_mutex() {
121
123
  if (sc_ptr != nullptr) {
122
123
    m_sc = *sc_ptr;
123
123
    m_flags.Set(m_sc.GetResolvedMask());
124
123
  }
125
126
123
  if (!m_sc.target_sp && reg_context_sp) {
127
123
    m_sc.target_sp = reg_context_sp->CalculateTarget();
128
123
    if (m_sc.target_sp)
129
123
      m_flags.Set(eSymbolContextTarget);
130
123
  }
131
132
123
  ModuleSP pc_module_sp(pc_addr.GetModule());
133
123
  if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
134
0
    if (pc_module_sp) {
135
0
      m_sc.module_sp = pc_module_sp;
136
0
      m_flags.Set(eSymbolContextModule);
137
0
    } else {
138
0
      m_sc.module_sp.reset();
139
0
    }
140
0
  }
141
123
}
142
143
201k
StackFrame::~StackFrame() = default;
144
145
4.20M
StackID &StackFrame::GetStackID() {
146
4.20M
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
147
  // Make sure we have resolved the StackID object's symbol context scope if we
148
  // already haven't looked it up.
149
150
4.20M
  if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
151
20.8k
    if (m_id.GetSymbolContextScope()) {
152
      // We already have a symbol context scope, we just don't have our flag
153
      // bit set.
154
0
      m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
155
20.8k
    } else {
156
      // Calculate the frame block and use this for the stack ID symbol context
157
      // scope if we have one.
158
20.8k
      SymbolContextScope *scope = GetFrameBlock();
159
20.8k
      if (scope == nullptr) {
160
        // We don't have a block, so use the symbol
161
8.16k
        if (m_flags.IsClear(eSymbolContextSymbol))
162
7.61k
          GetSymbolContext(eSymbolContextSymbol);
163
164
        // It is ok if m_sc.symbol is nullptr here
165
8.16k
        scope = m_sc.symbol;
166
8.16k
      }
167
      // Set the symbol context scope (the accessor will set the
168
      // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
169
20.8k
      SetSymbolContextScope(scope);
170
20.8k
    }
171
20.8k
  }
172
4.20M
  return m_id;
173
4.20M
}
174
175
8.56k
uint32_t StackFrame::GetFrameIndex() const {
176
8.56k
  ThreadSP thread_sp = GetThread();
177
8.56k
  if (thread_sp)
178
8.56k
    return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
179
8.56k
        m_frame_index);
180
0
  else
181
0
    return m_frame_index;
182
8.56k
}
183
184
20.8k
void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
185
20.8k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
186
20.8k
  m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
187
20.8k
  m_id.SetSymbolContextScope(symbol_scope);
188
20.8k
}
189
190
902k
const Address &StackFrame::GetFrameCodeAddress() {
191
902k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
192
902k
  if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
193
902k
      
!m_frame_code_addr.IsSectionOffset()201k
) {
194
201k
    m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
195
196
    // Resolve the PC into a temporary address because if ResolveLoadAddress
197
    // fails to resolve the address, it will clear the address object...
198
201k
    ThreadSP thread_sp(GetThread());
199
201k
    if (thread_sp) {
200
201k
      TargetSP target_sp(thread_sp->CalculateTarget());
201
201k
      if (target_sp) {
202
201k
        const bool allow_section_end = true;
203
201k
        if (m_frame_code_addr.SetOpcodeLoadAddress(
204
201k
                m_frame_code_addr.GetOffset(), target_sp.get(),
205
201k
                AddressClass::eCode, allow_section_end)) {
206
201k
          ModuleSP module_sp(m_frame_code_addr.GetModule());
207
201k
          if (module_sp) {
208
201k
            m_sc.module_sp = module_sp;
209
201k
            m_flags.Set(eSymbolContextModule);
210
201k
          }
211
201k
        }
212
201k
      }
213
201k
    }
214
201k
  }
215
902k
  return m_frame_code_addr;
216
902k
}
217
218
// This can't be rewritten into a call to
219
// RegisterContext::GetPCForSymbolication because this
220
// StackFrame may have been constructed with a special pc,
221
// e.g. tail-call artificial frames.
222
587k
Address StackFrame::GetFrameCodeAddressForSymbolication() {
223
587k
  Address lookup_addr(GetFrameCodeAddress());
224
587k
  if (!lookup_addr.IsValid())
225
119
    return lookup_addr;
226
587k
  if (m_behaves_like_zeroth_frame)
227
45.0k
    return lookup_addr;
228
229
542k
  addr_t offset = lookup_addr.GetOffset();
230
542k
  if (offset > 0) {
231
542k
    lookup_addr.SetOffset(offset - 1);
232
542k
  } else {
233
    // lookup_addr is the start of a section.  We need do the math on the
234
    // actual load address and re-compute the section.  We're working with
235
    // a 'noreturn' function at the end of a section.
236
6
    TargetSP target_sp = CalculateTarget();
237
6
    if (target_sp) {
238
6
      addr_t addr_minus_one = lookup_addr.GetOpcodeLoadAddress(
239
6
                                  target_sp.get(), AddressClass::eCode) -
240
6
                              1;
241
6
      lookup_addr.SetOpcodeLoadAddress(addr_minus_one, target_sp.get());
242
6
    }
243
6
  }
244
542k
  return lookup_addr;
245
587k
}
246
247
18
bool StackFrame::ChangePC(addr_t pc) {
248
18
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
249
  // We can't change the pc value of a history stack frame - it is immutable.
250
18
  if (IsHistorical())
251
0
    return false;
252
18
  m_frame_code_addr.SetRawAddress(pc);
253
18
  m_sc.Clear(false);
254
18
  m_flags.Reset(0);
255
18
  ThreadSP thread_sp(GetThread());
256
18
  if (thread_sp)
257
18
    thread_sp->ClearStackFrames();
258
18
  return true;
259
18
}
260
261
1
const char *StackFrame::Disassemble() {
262
1
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
263
1
  if (!m_disassembly.Empty())
264
0
    return m_disassembly.GetData();
265
266
1
  ExecutionContext exe_ctx(shared_from_this());
267
1
  if (Target *target = exe_ctx.GetTargetPtr()) {
268
1
    Disassembler::Disassemble(target->GetDebugger(), target->GetArchitecture(),
269
1
                              *this, m_disassembly);
270
1
  }
271
272
1
  return m_disassembly.Empty() ? 
nullptr0
: m_disassembly.GetData();
273
1
}
274
275
36.6k
Block *StackFrame::GetFrameBlock() {
276
36.6k
  if (m_sc.block == nullptr && 
m_flags.IsClear(eSymbolContextBlock)8.25k
)
277
28
    GetSymbolContext(eSymbolContextBlock);
278
279
36.6k
  if (m_sc.block) {
280
28.3k
    Block *inline_block = m_sc.block->GetContainingInlinedBlock();
281
28.3k
    if (inline_block) {
282
      // Use the block with the inlined function info as the frame block we
283
      // want this frame to have only the variables for the inlined function
284
      // and its non-inlined block child blocks.
285
217
      return inline_block;
286
28.1k
    } else {
287
      // This block is not contained within any inlined function blocks with so
288
      // we want to use the top most function block.
289
28.1k
      return &m_sc.function->GetBlock(false);
290
28.1k
    }
291
28.3k
  }
292
8.22k
  return nullptr;
293
36.6k
}
294
295
// Get the symbol context if we already haven't done so by resolving the
296
// PC address as much as possible. This way when we pass around a
297
// StackFrame object, everyone will have as much information as possible and no
298
// one will ever have to look things up manually.
299
const SymbolContext &
300
1.44M
StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
301
1.44M
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
302
  // Copy our internal symbol context into "sc".
303
1.44M
  if ((m_flags.Get() & resolve_scope) != resolve_scope) {
304
399k
    uint32_t resolved = 0;
305
306
    // If the target was requested add that:
307
399k
    if (!m_sc.target_sp) {
308
182k
      m_sc.target_sp = CalculateTarget();
309
182k
      if (m_sc.target_sp)
310
182k
        resolved |= eSymbolContextTarget;
311
182k
    }
312
313
    // Resolve our PC to section offset if we haven't already done so and if we
314
    // don't have a module. The resolved address section will contain the
315
    // module to which it belongs
316
399k
    if (!m_sc.module_sp && 
m_flags.IsClear(201k
RESOLVED_FRAME_CODE_ADDR201k
))
317
201k
      GetFrameCodeAddress();
318
319
    // If this is not frame zero, then we need to subtract 1 from the PC value
320
    // when doing address lookups since the PC will be on the instruction
321
    // following the function call instruction...
322
399k
    Address lookup_addr(GetFrameCodeAddressForSymbolication());
323
324
399k
    if (m_sc.module_sp) {
325
      // We have something in our stack frame symbol context, lets check if we
326
      // haven't already tried to lookup one of those things. If we haven't
327
      // then we will do the query.
328
329
399k
      SymbolContextItem actual_resolve_scope = SymbolContextItem(0);
330
331
399k
      if (resolve_scope & eSymbolContextCompUnit) {
332
4.92k
        if (m_flags.IsClear(eSymbolContextCompUnit)) {
333
1.64k
          if (m_sc.comp_unit)
334
0
            resolved |= eSymbolContextCompUnit;
335
1.64k
          else
336
1.64k
            actual_resolve_scope |= eSymbolContextCompUnit;
337
1.64k
        }
338
4.92k
      }
339
340
399k
      if (resolve_scope & eSymbolContextFunction) {
341
384k
        if (m_flags.IsClear(eSymbolContextFunction)) {
342
201k
          if (m_sc.function)
343
0
            resolved |= eSymbolContextFunction;
344
201k
          else
345
201k
            actual_resolve_scope |= eSymbolContextFunction;
346
201k
        }
347
384k
      }
348
349
399k
      if (resolve_scope & eSymbolContextBlock) {
350
206k
        if (m_flags.IsClear(eSymbolContextBlock)) {
351
201k
          if (m_sc.block)
352
0
            resolved |= eSymbolContextBlock;
353
201k
          else
354
201k
            actual_resolve_scope |= eSymbolContextBlock;
355
201k
        }
356
206k
      }
357
358
399k
      if (resolve_scope & eSymbolContextSymbol) {
359
14.3k
        if (m_flags.IsClear(eSymbolContextSymbol)) {
360
12.4k
          if (m_sc.symbol)
361
0
            resolved |= eSymbolContextSymbol;
362
12.4k
          else
363
12.4k
            actual_resolve_scope |= eSymbolContextSymbol;
364
12.4k
        }
365
14.3k
      }
366
367
399k
      if (resolve_scope & eSymbolContextLineEntry) {
368
10.8k
        if (m_flags.IsClear(eSymbolContextLineEntry)) {
369
9.48k
          if (m_sc.line_entry.IsValid())
370
0
            resolved |= eSymbolContextLineEntry;
371
9.48k
          else
372
9.48k
            actual_resolve_scope |= eSymbolContextLineEntry;
373
9.48k
        }
374
10.8k
      }
375
376
399k
      if (actual_resolve_scope) {
377
        // We might be resolving less information than what is already in our
378
        // current symbol context so resolve into a temporary symbol context
379
        // "sc" so we don't clear out data we have already found in "m_sc"
380
399k
        SymbolContext sc;
381
        // Set flags that indicate what we have tried to resolve
382
399k
        resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
383
399k
            lookup_addr, actual_resolve_scope, sc);
384
        // Only replace what we didn't already have as we may have information
385
        // for an inlined function scope that won't match what a standard
386
        // lookup by address would match
387
399k
        if ((resolved & eSymbolContextCompUnit) && 
m_sc.comp_unit == nullptr372k
)
388
188k
          m_sc.comp_unit = sc.comp_unit;
389
399k
        if ((resolved & eSymbolContextFunction) && 
m_sc.function == nullptr364k
)
390
188k
          m_sc.function = sc.function;
391
399k
        if ((resolved & eSymbolContextBlock) && 
m_sc.block == nullptr188k
)
392
188k
          m_sc.block = sc.block;
393
399k
        if ((resolved & eSymbolContextSymbol) && 
m_sc.symbol == nullptr21.4k
)
394
17.9k
          m_sc.symbol = sc.symbol;
395
399k
        if ((resolved & eSymbolContextLineEntry) &&
396
399k
            
!m_sc.line_entry.IsValid()7.56k
) {
397
7.56k
          m_sc.line_entry = sc.line_entry;
398
7.56k
          m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
399
7.56k
        }
400
399k
      }
401
399k
    } else {
402
      // If we don't have a module, then we can't have the compile unit,
403
      // function, block, line entry or symbol, so we can safely call
404
      // ResolveSymbolContextForAddress with our symbol context member m_sc.
405
631
      if (m_sc.target_sp) {
406
631
        resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
407
631
            lookup_addr, resolve_scope, m_sc);
408
631
      }
409
631
    }
410
411
    // Update our internal flags so we remember what we have tried to locate so
412
    // we don't have to keep trying when more calls to this function are made.
413
    // We might have dug up more information that was requested (for example if
414
    // we were asked to only get the block, we will have gotten the compile
415
    // unit, and function) so set any additional bits that we resolved
416
399k
    m_flags.Set(resolve_scope | resolved);
417
399k
  }
418
419
  // Return the symbol context with everything that was possible to resolve
420
  // resolved.
421
1.44M
  return m_sc;
422
1.44M
}
423
424
VariableList *StackFrame::GetVariableList(bool get_file_globals,
425
12.9k
                                          Status *error_ptr) {
426
12.9k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
427
12.9k
  if (m_flags.IsClear(RESOLVED_VARIABLES)) {
428
1.57k
    m_flags.Set(RESOLVED_VARIABLES);
429
1.57k
    m_variable_list_sp = std::make_shared<VariableList>();
430
431
1.57k
    Block *frame_block = GetFrameBlock();
432
433
1.57k
    if (frame_block) {
434
1.56k
      const bool get_child_variables = true;
435
1.56k
      const bool can_create = true;
436
1.56k
      const bool stop_if_child_block_is_inlined_function = true;
437
1.56k
      frame_block->AppendBlockVariables(can_create, get_child_variables,
438
1.56k
                                        stop_if_child_block_is_inlined_function,
439
19.6k
                                        [](Variable *v) { return true; },
440
1.56k
                                        m_variable_list_sp.get());
441
1.56k
    }
442
1.57k
  }
443
444
12.9k
  if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && 
get_file_globals2.33k
) {
445
1.57k
    m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
446
447
1.57k
    if (m_flags.IsClear(eSymbolContextCompUnit))
448
5
      GetSymbolContext(eSymbolContextCompUnit);
449
450
1.57k
    if (m_sc.comp_unit) {
451
1.56k
      VariableListSP global_variable_list_sp(
452
1.56k
          m_sc.comp_unit->GetVariableList(true));
453
1.56k
      if (m_variable_list_sp)
454
1.56k
        m_variable_list_sp->AddVariables(global_variable_list_sp.get());
455
0
      else
456
0
        m_variable_list_sp = global_variable_list_sp;
457
1.56k
    }
458
1.57k
  }
459
460
12.9k
  if (error_ptr && 
m_variable_list_sp->GetSize() == 04.23k
) {
461
    // Check with the symbol file to check if there is an error for why we
462
    // don't have variables that the user might need to know about.
463
29
    GetSymbolContext(eSymbolContextEverything);
464
29
    if (m_sc.module_sp) {
465
29
      SymbolFile *sym_file = m_sc.module_sp->GetSymbolFile();
466
29
      if (sym_file)
467
29
        *error_ptr = sym_file->GetFrameVariableError(*this);
468
29
    }
469
29
  }
470
471
12.9k
  return m_variable_list_sp.get();
472
12.9k
}
473
474
VariableListSP
475
StackFrame::GetInScopeVariableList(bool get_file_globals,
476
28.4k
                                   bool must_have_valid_location) {
477
28.4k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
478
  // We can't fetch variable information for a history stack frame.
479
28.4k
  if (IsHistorical())
480
0
    return VariableListSP();
481
482
28.4k
  VariableListSP var_list_sp(new VariableList);
483
28.4k
  GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
484
485
28.4k
  if (m_sc.block) {
486
28.4k
    const bool can_create = true;
487
28.4k
    const bool get_parent_variables = true;
488
28.4k
    const bool stop_if_block_is_inlined_function = true;
489
28.4k
    m_sc.block->AppendVariables(
490
28.4k
        can_create, get_parent_variables, stop_if_block_is_inlined_function,
491
417k
        [this, must_have_valid_location](Variable *v) {
492
417k
          return v->IsInScope(this) && (!must_have_valid_location ||
493
417k
                                        
v->LocationIsValidForFrame(this)82.0k
);
494
417k
        },
495
28.4k
        var_list_sp.get());
496
28.4k
  }
497
498
28.4k
  if (m_sc.comp_unit && 
get_file_globals28.4k
) {
499
21.6k
    VariableListSP global_variable_list_sp(
500
21.6k
        m_sc.comp_unit->GetVariableList(true));
501
21.6k
    if (global_variable_list_sp)
502
21.6k
      var_list_sp->AddVariables(global_variable_list_sp.get());
503
21.6k
  }
504
505
28.4k
  return var_list_sp;
506
28.4k
}
507
508
ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
509
    llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
510
5.80k
    VariableSP &var_sp, Status &error) {
511
5.80k
  llvm::StringRef original_var_expr = var_expr;
512
  // We can't fetch variable information for a history stack frame.
513
5.80k
  if (IsHistorical())
514
0
    return ValueObjectSP();
515
516
5.80k
  if (var_expr.empty()) {
517
0
    error.SetErrorStringWithFormat("invalid variable path '%s'",
518
0
                                   var_expr.str().c_str());
519
0
    return ValueObjectSP();
520
0
  }
521
522
5.80k
  const bool check_ptr_vs_member =
523
5.80k
      (options & eExpressionPathOptionCheckPtrVsMember) != 0;
524
5.80k
  const bool no_fragile_ivar =
525
5.80k
      (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
526
5.80k
  const bool no_synth_child =
527
5.80k
      (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
528
  // const bool no_synth_array = (options &
529
  // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
530
5.80k
  error.Clear();
531
5.80k
  bool deref = false;
532
5.80k
  bool address_of = false;
533
5.80k
  ValueObjectSP valobj_sp;
534
5.80k
  const bool get_file_globals = true;
535
  // When looking up a variable for an expression, we need only consider the
536
  // variables that are in scope.
537
5.80k
  VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
538
5.80k
  VariableList *variable_list = var_list_sp.get();
539
540
5.80k
  if (!variable_list)
541
0
    return ValueObjectSP();
542
543
  // If first character is a '*', then show pointer contents
544
5.80k
  std::string var_expr_storage;
545
5.80k
  if (var_expr[0] == '*') {
546
139
    deref = true;
547
139
    var_expr = var_expr.drop_front(); // Skip the '*'
548
5.66k
  } else if (var_expr[0] == '&') {
549
4
    address_of = true;
550
4
    var_expr = var_expr.drop_front(); // Skip the '&'
551
4
  }
552
553
5.80k
  size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
554
5.80k
  StreamString var_expr_path_strm;
555
556
5.80k
  ConstString name_const_string(var_expr.substr(0, separator_idx));
557
558
5.80k
  var_sp = variable_list->FindVariable(name_const_string, false);
559
560
5.80k
  bool synthetically_added_instance_object = false;
561
562
5.80k
  if (var_sp) {
563
5.76k
    var_expr = var_expr.drop_front(name_const_string.GetLength());
564
5.76k
  }
565
566
5.80k
  if (!var_sp && 
(options & eExpressionPathOptionsAllowDirectIVarAccess)32
) {
567
    // Check for direct ivars access which helps us with implicit access to
568
    // ivars using "this" or "self".
569
18
    GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
570
18
    llvm::StringRef instance_var_name = m_sc.GetInstanceVariableName();
571
18
    if (!instance_var_name.empty()) {
572
15
      var_sp = variable_list->FindVariable(ConstString(instance_var_name));
573
15
      if (var_sp) {
574
14
        separator_idx = 0;
575
14
        if (Type *var_type = var_sp->GetType())
576
14
          if (auto compiler_type = var_type->GetForwardCompilerType())
577
14
            if (!compiler_type.IsPointerType())
578
0
              var_expr_storage = ".";
579
580
14
        if (var_expr_storage.empty())
581
14
          var_expr_storage = "->";
582
14
        var_expr_storage += var_expr;
583
14
        var_expr = var_expr_storage;
584
14
        synthetically_added_instance_object = true;
585
14
      }
586
15
    }
587
18
  }
588
589
5.80k
  if (!var_sp && 
(options & eExpressionPathOptionsInspectAnonymousUnions)18
) {
590
    // Check if any anonymous unions are there which contain a variable with
591
    // the name we need
592
6
    for (const VariableSP &variable_sp : *variable_list) {
593
6
      if (!variable_sp)
594
0
        continue;
595
6
      if (!variable_sp->GetName().IsEmpty())
596
6
        continue;
597
598
0
      Type *var_type = variable_sp->GetType();
599
0
      if (!var_type)
600
0
        continue;
601
602
0
      if (!var_type->GetForwardCompilerType().IsAnonymousType())
603
0
        continue;
604
0
      valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
605
0
      if (!valobj_sp)
606
0
        return valobj_sp;
607
0
      valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string);
608
0
      if (valobj_sp)
609
0
        break;
610
0
    }
611
2
  }
612
613
5.80k
  if (var_sp && 
!valobj_sp5.78k
) {
614
5.78k
    valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
615
5.78k
    if (!valobj_sp)
616
0
      return valobj_sp;
617
5.78k
  }
618
5.80k
  if (!valobj_sp) {
619
18
    error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
620
18
                                   name_const_string.GetCString());
621
18
    return ValueObjectSP();
622
18
  }
623
624
  // We are dumping at least one child
625
9.19k
  
while (5.78k
!var_expr.empty()) {
626
    // Calculate the next separator index ahead of time
627
3.44k
    ValueObjectSP child_valobj_sp;
628
3.44k
    const char separator_type = var_expr[0];
629
3.44k
    bool expr_is_ptr = false;
630
3.44k
    switch (separator_type) {
631
534
    case '-':
632
534
      expr_is_ptr = true;
633
534
      if (var_expr.size() >= 2 && var_expr[1] != '>')
634
0
        return ValueObjectSP();
635
636
534
      if (no_fragile_ivar) {
637
        // Make sure we aren't trying to deref an objective
638
        // C ivar if this is not allowed
639
0
        const uint32_t pointer_type_flags =
640
0
            valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
641
0
        if ((pointer_type_flags & eTypeIsObjC) &&
642
0
            (pointer_type_flags & eTypeIsPointer)) {
643
          // This was an objective C object pointer and it was requested we
644
          // skip any fragile ivars so return nothing here
645
0
          return ValueObjectSP();
646
0
        }
647
0
      }
648
649
      // If we have a non pointer type with a sythetic value then lets check if
650
      // we have an sythetic dereference specified.
651
534
      if (!valobj_sp->IsPointerType() && 
valobj_sp->HasSyntheticValue()33
) {
652
9
        Status deref_error;
653
9
        if (valobj_sp->GetCompilerType().IsReferenceType()) {
654
0
          valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
655
0
          if (error.Fail()) {
656
0
            error.SetErrorStringWithFormatv(
657
0
                "Failed to dereference reference type: %s", deref_error);
658
0
            return ValueObjectSP();
659
0
          }
660
0
        }
661
662
9
        valobj_sp = valobj_sp->Dereference(deref_error);
663
9
        if (error.Fail()) {
664
0
          error.SetErrorStringWithFormatv(
665
0
              "Failed to dereference sythetic value: {0}", deref_error);
666
0
          return ValueObjectSP();
667
0
        }
668
        // Some synthetic plug-ins fail to set the error in Dereference
669
9
        if (!valobj_sp) {
670
0
          error.SetErrorString("Failed to dereference sythetic value");
671
0
          return ValueObjectSP();
672
0
        }
673
9
        expr_is_ptr = false;
674
9
      }
675
676
534
      var_expr = var_expr.drop_front(); // Remove the '-'
677
534
      [[fallthrough]];
678
2.05k
    case '.': {
679
2.05k
      var_expr = var_expr.drop_front(); // Remove the '.' or '>'
680
2.05k
      separator_idx = var_expr.find_first_of(".-[");
681
2.05k
      ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
682
683
2.05k
      if (check_ptr_vs_member) {
684
        // We either have a pointer type and need to verify valobj_sp is a
685
        // pointer, or we have a member of a class/union/struct being accessed
686
        // with the . syntax and need to verify we don't have a pointer.
687
2.05k
        const bool actual_is_ptr = valobj_sp->IsPointerType();
688
689
2.05k
        if (actual_is_ptr != expr_is_ptr) {
690
          // Incorrect use of "." with a pointer, or "->" with a
691
          // class/union/struct instance or reference.
692
27
          valobj_sp->GetExpressionPath(var_expr_path_strm);
693
27
          if (actual_is_ptr)
694
3
            error.SetErrorStringWithFormat(
695
3
                "\"%s\" is a pointer and . was used to attempt to access "
696
3
                "\"%s\". Did you mean \"%s->%s\"?",
697
3
                var_expr_path_strm.GetData(), child_name.GetCString(),
698
3
                var_expr_path_strm.GetData(), var_expr.str().c_str());
699
24
          else
700
24
            error.SetErrorStringWithFormat(
701
24
                "\"%s\" is not a pointer and -> was used to attempt to "
702
24
                "access \"%s\". Did you mean \"%s.%s\"?",
703
24
                var_expr_path_strm.GetData(), child_name.GetCString(),
704
24
                var_expr_path_strm.GetData(), var_expr.str().c_str());
705
27
          return ValueObjectSP();
706
27
        }
707
2.05k
      }
708
2.03k
      child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name);
709
2.03k
      if (!child_valobj_sp) {
710
7
        if (!no_synth_child) {
711
7
          child_valobj_sp = valobj_sp->GetSyntheticValue();
712
7
          if (child_valobj_sp)
713
3
            child_valobj_sp =
714
3
                child_valobj_sp->GetChildMemberWithName(child_name);
715
7
        }
716
717
7
        if (no_synth_child || !child_valobj_sp) {
718
          // No child member with name "child_name"
719
4
          if (synthetically_added_instance_object) {
720
            // We added a "this->" or "self->" to the beginning of the
721
            // expression and this is the first pointer ivar access, so just
722
            // return the normal error
723
0
            error.SetErrorStringWithFormat(
724
0
                "no variable or instance variable named '%s' found in "
725
0
                "this frame",
726
0
                name_const_string.GetCString());
727
4
          } else {
728
4
            valobj_sp->GetExpressionPath(var_expr_path_strm);
729
4
            if (child_name) {
730
4
              error.SetErrorStringWithFormat(
731
4
                  "\"%s\" is not a member of \"(%s) %s\"",
732
4
                  child_name.GetCString(),
733
4
                  valobj_sp->GetTypeName().AsCString("<invalid type>"),
734
4
                  var_expr_path_strm.GetData());
735
4
            } else {
736
0
              error.SetErrorStringWithFormat(
737
0
                  "incomplete expression path after \"%s\" in \"%s\"",
738
0
                  var_expr_path_strm.GetData(),
739
0
                  original_var_expr.str().c_str());
740
0
            }
741
4
          }
742
4
          return ValueObjectSP();
743
4
        }
744
7
      }
745
2.02k
      synthetically_added_instance_object = false;
746
      // Remove the child name from the path
747
2.02k
      var_expr = var_expr.drop_front(child_name.GetLength());
748
2.02k
      if (use_dynamic != eNoDynamicValues) {
749
1.95k
        ValueObjectSP dynamic_value_sp(
750
1.95k
            child_valobj_sp->GetDynamicValue(use_dynamic));
751
1.95k
        if (dynamic_value_sp)
752
25
          child_valobj_sp = dynamic_value_sp;
753
1.95k
      }
754
2.02k
    } break;
755
756
1.38k
    case '[': {
757
      // Array member access, or treating pointer as an array Need at least two
758
      // brackets and a number
759
1.38k
      if (var_expr.size() <= 2) {
760
0
        error.SetErrorStringWithFormat(
761
0
            "invalid square bracket encountered after \"%s\" in \"%s\"",
762
0
            var_expr_path_strm.GetData(), var_expr.str().c_str());
763
0
        return ValueObjectSP();
764
0
      }
765
766
      // Drop the open brace.
767
1.38k
      var_expr = var_expr.drop_front();
768
1.38k
      long child_index = 0;
769
770
      // If there's no closing brace, this is an invalid expression.
771
1.38k
      size_t end_pos = var_expr.find_first_of(']');
772
1.38k
      if (end_pos == llvm::StringRef::npos) {
773
0
        error.SetErrorStringWithFormat(
774
0
            "missing closing square bracket in expression \"%s\"",
775
0
            var_expr_path_strm.GetData());
776
0
        return ValueObjectSP();
777
0
      }
778
1.38k
      llvm::StringRef index_expr = var_expr.take_front(end_pos);
779
1.38k
      llvm::StringRef original_index_expr = index_expr;
780
      // Drop all of "[index_expr]"
781
1.38k
      var_expr = var_expr.drop_front(end_pos + 1);
782
783
1.38k
      if (index_expr.consumeInteger(0, child_index)) {
784
        // If there was no integer anywhere in the index expression, this is
785
        // erroneous expression.
786
0
        error.SetErrorStringWithFormat("invalid index expression \"%s\"",
787
0
                                       index_expr.str().c_str());
788
0
        return ValueObjectSP();
789
0
      }
790
791
1.38k
      if (index_expr.empty()) {
792
        // The entire index expression was a single integer.
793
794
1.38k
        if (valobj_sp->GetCompilerType().IsPointerToScalarType() && 
deref0
) {
795
          // what we have is *ptr[low]. the most similar C++ syntax is to deref
796
          // ptr and extract bit low out of it. reading array item low would be
797
          // done by saying ptr[low], without a deref * sign
798
0
          Status error;
799
0
          ValueObjectSP temp(valobj_sp->Dereference(error));
800
0
          if (error.Fail()) {
801
0
            valobj_sp->GetExpressionPath(var_expr_path_strm);
802
0
            error.SetErrorStringWithFormat(
803
0
                "could not dereference \"(%s) %s\"",
804
0
                valobj_sp->GetTypeName().AsCString("<invalid type>"),
805
0
                var_expr_path_strm.GetData());
806
0
            return ValueObjectSP();
807
0
          }
808
0
          valobj_sp = temp;
809
0
          deref = false;
810
1.38k
        } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
811
1.38k
                   
deref134
) {
812
          // what we have is *arr[low]. the most similar C++ syntax is to get
813
          // arr[0] (an operation that is equivalent to deref-ing arr) and
814
          // extract bit low out of it. reading array item low would be done by
815
          // saying arr[low], without a deref * sign
816
0
          Status error;
817
0
          ValueObjectSP temp(valobj_sp->GetChildAtIndex(0));
818
0
          if (error.Fail()) {
819
0
            valobj_sp->GetExpressionPath(var_expr_path_strm);
820
0
            error.SetErrorStringWithFormat(
821
0
                "could not get item 0 for \"(%s) %s\"",
822
0
                valobj_sp->GetTypeName().AsCString("<invalid type>"),
823
0
                var_expr_path_strm.GetData());
824
0
            return ValueObjectSP();
825
0
          }
826
0
          valobj_sp = temp;
827
0
          deref = false;
828
0
        }
829
830
1.38k
        bool is_incomplete_array = false;
831
1.38k
        if (valobj_sp->IsPointerType()) {
832
43
          bool is_objc_pointer = true;
833
834
43
          if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
835
43
              eLanguageTypeObjC)
836
41
            is_objc_pointer = false;
837
2
          else if (!valobj_sp->GetCompilerType().IsPointerType())
838
0
            is_objc_pointer = false;
839
840
43
          if (no_synth_child && 
is_objc_pointer0
) {
841
0
            error.SetErrorStringWithFormat(
842
0
                "\"(%s) %s\" is an Objective-C pointer, and cannot be "
843
0
                "subscripted",
844
0
                valobj_sp->GetTypeName().AsCString("<invalid type>"),
845
0
                var_expr_path_strm.GetData());
846
847
0
            return ValueObjectSP();
848
43
          } else if (is_objc_pointer) {
849
            // dereferencing ObjC variables is not valid.. so let's try and
850
            // recur to synthetic children
851
2
            ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
852
2
            if (!synthetic                 /* no synthetic */
853
2
                || synthetic == valobj_sp) /* synthetic is the same as
854
                                              the original object */
855
0
            {
856
0
              valobj_sp->GetExpressionPath(var_expr_path_strm);
857
0
              error.SetErrorStringWithFormat(
858
0
                  "\"(%s) %s\" is not an array type",
859
0
                  valobj_sp->GetTypeName().AsCString("<invalid type>"),
860
0
                  var_expr_path_strm.GetData());
861
2
            } else if (
862
2
                static_cast<uint32_t>(child_index) >=
863
2
                synthetic
864
2
                    ->GetNumChildren() /* synthetic does not have that many values */) {
865
0
              valobj_sp->GetExpressionPath(var_expr_path_strm);
866
0
              error.SetErrorStringWithFormat(
867
0
                  "array index %ld is not valid for \"(%s) %s\"", child_index,
868
0
                  valobj_sp->GetTypeName().AsCString("<invalid type>"),
869
0
                  var_expr_path_strm.GetData());
870
2
            } else {
871
2
              child_valobj_sp = synthetic->GetChildAtIndex(child_index);
872
2
              if (!child_valobj_sp) {
873
0
                valobj_sp->GetExpressionPath(var_expr_path_strm);
874
0
                error.SetErrorStringWithFormat(
875
0
                    "array index %ld is not valid for \"(%s) %s\"", child_index,
876
0
                    valobj_sp->GetTypeName().AsCString("<invalid type>"),
877
0
                    var_expr_path_strm.GetData());
878
0
              }
879
2
            }
880
41
          } else {
881
41
            child_valobj_sp =
882
41
                valobj_sp->GetSyntheticArrayMember(child_index, true);
883
41
            if (!child_valobj_sp) {
884
0
              valobj_sp->GetExpressionPath(var_expr_path_strm);
885
0
              error.SetErrorStringWithFormat(
886
0
                  "failed to use pointer as array for index %ld for "
887
0
                  "\"(%s) %s\"",
888
0
                  child_index,
889
0
                  valobj_sp->GetTypeName().AsCString("<invalid type>"),
890
0
                  var_expr_path_strm.GetData());
891
0
            }
892
41
          }
893
1.34k
        } else if (valobj_sp->GetCompilerType().IsArrayType(
894
1.34k
                       nullptr, nullptr, &is_incomplete_array)) {
895
          // Pass false to dynamic_value here so we can tell the difference
896
          // between no dynamic value and no member of this type...
897
467
          child_valobj_sp = valobj_sp->GetChildAtIndex(child_index);
898
467
          if (!child_valobj_sp && 
(4
is_incomplete_array4
||
!no_synth_child0
))
899
4
            child_valobj_sp =
900
4
                valobj_sp->GetSyntheticArrayMember(child_index, true);
901
902
467
          if (!child_valobj_sp) {
903
0
            valobj_sp->GetExpressionPath(var_expr_path_strm);
904
0
            error.SetErrorStringWithFormat(
905
0
                "array index %ld is not valid for \"(%s) %s\"", child_index,
906
0
                valobj_sp->GetTypeName().AsCString("<invalid type>"),
907
0
                var_expr_path_strm.GetData());
908
0
          }
909
878
        } else if (valobj_sp->GetCompilerType().IsScalarType()) {
910
          // this is a bitfield asking to display just one bit
911
0
          child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
912
0
              child_index, child_index, true);
913
0
          if (!child_valobj_sp) {
914
0
            valobj_sp->GetExpressionPath(var_expr_path_strm);
915
0
            error.SetErrorStringWithFormat(
916
0
                "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
917
0
                child_index, child_index,
918
0
                valobj_sp->GetTypeName().AsCString("<invalid type>"),
919
0
                var_expr_path_strm.GetData());
920
0
          }
921
878
        } else {
922
878
          ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
923
878
          if (no_synth_child /* synthetic is forbidden */ ||
924
878
              !synthetic                 /* no synthetic */
925
878
              || synthetic == valobj_sp) /* synthetic is the same as the
926
                                            original object */
927
0
          {
928
0
            valobj_sp->GetExpressionPath(var_expr_path_strm);
929
0
            error.SetErrorStringWithFormat(
930
0
                "\"(%s) %s\" is not an array type",
931
0
                valobj_sp->GetTypeName().AsCString("<invalid type>"),
932
0
                var_expr_path_strm.GetData());
933
878
          } else if (
934
878
              static_cast<uint32_t>(child_index) >=
935
878
              synthetic
936
878
                  ->GetNumChildren() /* synthetic does not have that many values */) {
937
0
            valobj_sp->GetExpressionPath(var_expr_path_strm);
938
0
            error.SetErrorStringWithFormat(
939
0
                "array index %ld is not valid for \"(%s) %s\"", child_index,
940
0
                valobj_sp->GetTypeName().AsCString("<invalid type>"),
941
0
                var_expr_path_strm.GetData());
942
878
          } else {
943
878
            child_valobj_sp = synthetic->GetChildAtIndex(child_index);
944
878
            if (!child_valobj_sp) {
945
0
              valobj_sp->GetExpressionPath(var_expr_path_strm);
946
0
              error.SetErrorStringWithFormat(
947
0
                  "array index %ld is not valid for \"(%s) %s\"", child_index,
948
0
                  valobj_sp->GetTypeName().AsCString("<invalid type>"),
949
0
                  var_expr_path_strm.GetData());
950
0
            }
951
878
          }
952
878
        }
953
954
1.38k
        if (!child_valobj_sp) {
955
          // Invalid array index...
956
0
          return ValueObjectSP();
957
0
        }
958
959
1.38k
        if (use_dynamic != eNoDynamicValues) {
960
494
          ValueObjectSP dynamic_value_sp(
961
494
              child_valobj_sp->GetDynamicValue(use_dynamic));
962
494
          if (dynamic_value_sp)
963
0
            child_valobj_sp = dynamic_value_sp;
964
494
        }
965
        // Break out early from the switch since we were able to find the child
966
        // member
967
1.38k
        break;
968
1.38k
      }
969
970
      // this is most probably a BitField, let's take a look
971
0
      if (index_expr.front() != '-') {
972
0
        error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
973
0
                                       original_index_expr.str().c_str());
974
0
        return ValueObjectSP();
975
0
      }
976
977
0
      index_expr = index_expr.drop_front();
978
0
      long final_index = 0;
979
0
      if (index_expr.getAsInteger(0, final_index)) {
980
0
        error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
981
0
                                       original_index_expr.str().c_str());
982
0
        return ValueObjectSP();
983
0
      }
984
985
      // if the format given is [high-low], swap range
986
0
      if (child_index > final_index) {
987
0
        long temp = child_index;
988
0
        child_index = final_index;
989
0
        final_index = temp;
990
0
      }
991
992
0
      if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
993
        // what we have is *ptr[low-high]. the most similar C++ syntax is to
994
        // deref ptr and extract bits low thru high out of it. reading array
995
        // items low thru high would be done by saying ptr[low-high], without a
996
        // deref * sign
997
0
        Status error;
998
0
        ValueObjectSP temp(valobj_sp->Dereference(error));
999
0
        if (error.Fail()) {
1000
0
          valobj_sp->GetExpressionPath(var_expr_path_strm);
1001
0
          error.SetErrorStringWithFormat(
1002
0
              "could not dereference \"(%s) %s\"",
1003
0
              valobj_sp->GetTypeName().AsCString("<invalid type>"),
1004
0
              var_expr_path_strm.GetData());
1005
0
          return ValueObjectSP();
1006
0
        }
1007
0
        valobj_sp = temp;
1008
0
        deref = false;
1009
0
      } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
1010
        // what we have is *arr[low-high]. the most similar C++ syntax is to
1011
        // get arr[0] (an operation that is equivalent to deref-ing arr) and
1012
        // extract bits low thru high out of it. reading array items low thru
1013
        // high would be done by saying arr[low-high], without a deref * sign
1014
0
        Status error;
1015
0
        ValueObjectSP temp(valobj_sp->GetChildAtIndex(0));
1016
0
        if (error.Fail()) {
1017
0
          valobj_sp->GetExpressionPath(var_expr_path_strm);
1018
0
          error.SetErrorStringWithFormat(
1019
0
              "could not get item 0 for \"(%s) %s\"",
1020
0
              valobj_sp->GetTypeName().AsCString("<invalid type>"),
1021
0
              var_expr_path_strm.GetData());
1022
0
          return ValueObjectSP();
1023
0
        }
1024
0
        valobj_sp = temp;
1025
0
        deref = false;
1026
0
      }
1027
1028
0
      child_valobj_sp =
1029
0
          valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1030
0
      if (!child_valobj_sp) {
1031
0
        valobj_sp->GetExpressionPath(var_expr_path_strm);
1032
0
        error.SetErrorStringWithFormat(
1033
0
            "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1034
0
            final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1035
0
            var_expr_path_strm.GetData());
1036
0
      }
1037
1038
0
      if (!child_valobj_sp) {
1039
        // Invalid bitfield range...
1040
0
        return ValueObjectSP();
1041
0
      }
1042
1043
0
      if (use_dynamic != eNoDynamicValues) {
1044
0
        ValueObjectSP dynamic_value_sp(
1045
0
            child_valobj_sp->GetDynamicValue(use_dynamic));
1046
0
        if (dynamic_value_sp)
1047
0
          child_valobj_sp = dynamic_value_sp;
1048
0
      }
1049
      // Break out early from the switch since we were able to find the child
1050
      // member
1051
0
      break;
1052
0
    }
1053
1
    default:
1054
      // Failure...
1055
1
      {
1056
1
        valobj_sp->GetExpressionPath(var_expr_path_strm);
1057
1
        error.SetErrorStringWithFormat(
1058
1
            "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1059
1
            separator_type, var_expr_path_strm.GetData(),
1060
1
            var_expr.str().c_str());
1061
1062
1
        return ValueObjectSP();
1063
0
      }
1064
3.44k
    }
1065
1066
3.41k
    if (child_valobj_sp)
1067
3.41k
      valobj_sp = child_valobj_sp;
1068
3.41k
  }
1069
5.75k
  if (valobj_sp) {
1070
5.75k
    if (deref) {
1071
139
      ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1072
139
      valobj_sp = deref_valobj_sp;
1073
5.61k
    } else if (address_of) {
1074
4
      ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1075
4
      valobj_sp = address_of_valobj_sp;
1076
4
    }
1077
5.75k
  }
1078
5.75k
  return valobj_sp;
1079
5.78k
}
1080
1081
13.0k
bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
1082
13.0k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1083
13.0k
  if (!m_cfa_is_valid) {
1084
0
    m_frame_base_error.SetErrorString(
1085
0
        "No frame base available for this historical stack frame.");
1086
0
    return false;
1087
0
  }
1088
1089
13.0k
  if (m_flags.IsClear(GOT_FRAME_BASE)) {
1090
3.58k
    if (m_sc.function) {
1091
3.58k
      m_frame_base.Clear();
1092
3.58k
      m_frame_base_error.Clear();
1093
1094
3.58k
      m_flags.Set(GOT_FRAME_BASE);
1095
3.58k
      ExecutionContext exe_ctx(shared_from_this());
1096
3.58k
      Value expr_value;
1097
3.58k
      addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1098
3.58k
      if (!m_sc.function->GetFrameBaseExpression().IsAlwaysValidSingleExpr())
1099
0
        loclist_base_addr =
1100
0
            m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1101
0
                exe_ctx.GetTargetPtr());
1102
1103
3.58k
      if (!m_sc.function->GetFrameBaseExpression().Evaluate(
1104
3.58k
              &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
1105
3.58k
              expr_value, &m_frame_base_error)) {
1106
        // We should really have an error if evaluate returns, but in case we
1107
        // don't, lets set the error to something at least.
1108
14
        if (m_frame_base_error.Success())
1109
0
          m_frame_base_error.SetErrorString(
1110
0
              "Evaluation of the frame base expression failed.");
1111
3.57k
      } else {
1112
3.57k
        m_frame_base = expr_value.ResolveValue(&exe_ctx);
1113
3.57k
      }
1114
3.58k
    } else {
1115
0
      m_frame_base_error.SetErrorString("No function in symbol context.");
1116
0
    }
1117
3.58k
  }
1118
1119
13.0k
  if (m_frame_base_error.Success())
1120
12.9k
    frame_base = m_frame_base;
1121
1122
13.0k
  if (error_ptr)
1123
13.0k
    *error_ptr = m_frame_base_error;
1124
13.0k
  return m_frame_base_error.Success();
1125
13.0k
}
1126
1127
336
DWARFExpressionList *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
1128
336
  if (!m_sc.function) {
1129
0
    if (error_ptr) {
1130
0
      error_ptr->SetErrorString("No function in symbol context.");
1131
0
    }
1132
0
    return nullptr;
1133
0
  }
1134
1135
336
  return &m_sc.function->GetFrameBaseExpression();
1136
336
}
1137
1138
213k
RegisterContextSP StackFrame::GetRegisterContext() {
1139
213k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1140
213k
  if (!m_reg_context_sp) {
1141
181k
    ThreadSP thread_sp(GetThread());
1142
181k
    if (thread_sp)
1143
181k
      m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1144
181k
  }
1145
213k
  return m_reg_context_sp;
1146
213k
}
1147
1148
73.4k
bool StackFrame::HasDebugInformation() {
1149
73.4k
  GetSymbolContext(eSymbolContextLineEntry);
1150
73.4k
  return m_sc.line_entry.IsValid();
1151
73.4k
}
1152
1153
ValueObjectSP
1154
StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1155
8.60k
                                           DynamicValueType use_dynamic) {
1156
8.60k
  ValueObjectSP valobj_sp;
1157
8.60k
  { // Scope for stack frame mutex.  We need to drop this mutex before we figure
1158
    // out the dynamic value.  That will require converting the StackID in the
1159
    // VO back to a StackFrame, which will in turn require locking the
1160
    // StackFrameList.  If we still hold the StackFrame mutex, we could suffer
1161
    // lock inversion against the pattern of getting the StackFrameList and
1162
    // then the stack frame, which is fairly common.
1163
8.60k
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
1164
8.60k
    if (IsHistorical()) {
1165
0
      return valobj_sp;
1166
0
    }
1167
8.60k
    VariableList *var_list = GetVariableList(true, nullptr);
1168
8.60k
    if (var_list) {
1169
      // Make sure the variable is a frame variable
1170
8.60k
      const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1171
8.60k
      const uint32_t num_variables = var_list->GetSize();
1172
8.60k
      if (var_idx < num_variables) {
1173
8.60k
        valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1174
8.60k
        if (!valobj_sp) {
1175
4.72k
          if (m_variable_list_value_objects.GetSize() < num_variables)
1176
1.56k
            m_variable_list_value_objects.Resize(num_variables);
1177
4.72k
          valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1178
4.72k
          m_variable_list_value_objects.SetValueObjectAtIndex(var_idx,
1179
4.72k
                                                              valobj_sp);
1180
4.72k
        }
1181
8.60k
      }
1182
8.60k
    }
1183
8.60k
  } // End of StackFrame mutex scope.
1184
8.60k
  if (use_dynamic != eNoDynamicValues && 
valobj_sp4.37k
) {
1185
4.37k
    ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1186
4.37k
    if (dynamic_sp)
1187
272
      return dynamic_sp;
1188
4.37k
  }
1189
8.33k
  return valobj_sp;
1190
8.60k
}
1191
1192
14.0k
bool StackFrame::IsInlined() {
1193
14.0k
  if (m_sc.block == nullptr)
1194
5.19k
    GetSymbolContext(eSymbolContextBlock);
1195
14.0k
  if (m_sc.block)
1196
8.81k
    return m_sc.block->GetContainingInlinedBlock() != nullptr;
1197
5.19k
  return false;
1198
14.0k
}
1199
1200
224k
bool StackFrame::IsHistorical() const {
1201
224k
  return m_stack_frame_kind == StackFrame::Kind::History;
1202
224k
}
1203
1204
4.71k
bool StackFrame::IsArtificial() const {
1205
4.71k
  return m_stack_frame_kind == StackFrame::Kind::Artificial;
1206
4.71k
}
1207
1208
7.63k
lldb::LanguageType StackFrame::GetLanguage() {
1209
7.63k
  CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1210
7.63k
  if (cu)
1211
7.56k
    return cu->GetLanguage();
1212
65
  return lldb::eLanguageTypeUnknown;
1213
7.63k
}
1214
1215
1.66k
lldb::LanguageType StackFrame::GuessLanguage() {
1216
1.66k
  LanguageType lang_type = GetLanguage();
1217
1218
1.66k
  if (lang_type == eLanguageTypeUnknown) {
1219
5
    SymbolContext sc = GetSymbolContext(eSymbolContextFunction
1220
5
                                        | eSymbolContextSymbol);
1221
5
    if (sc.function) {
1222
0
      lang_type = sc.function->GetMangled().GuessLanguage();
1223
0
    }
1224
5
    else if (sc.symbol)
1225
5
    {
1226
5
      lang_type = sc.symbol->GetMangled().GuessLanguage();
1227
5
    }
1228
5
  }
1229
1230
1.66k
  return lang_type;
1231
1.66k
}
1232
1233
namespace {
1234
std::pair<const Instruction::Operand *, int64_t>
1235
GetBaseExplainingValue(const Instruction::Operand &operand,
1236
102
                       RegisterContext &register_context, lldb::addr_t value) {
1237
102
  switch (operand.m_type) {
1238
0
  case Instruction::Operand::Type::Dereference:
1239
0
  case Instruction::Operand::Type::Immediate:
1240
0
  case Instruction::Operand::Type::Invalid:
1241
0
  case Instruction::Operand::Type::Product:
1242
    // These are not currently interesting
1243
0
    return std::make_pair(nullptr, 0);
1244
24
  case Instruction::Operand::Type::Sum: {
1245
24
    const Instruction::Operand *immediate_child = nullptr;
1246
24
    const Instruction::Operand *variable_child = nullptr;
1247
24
    if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1248
24
      immediate_child = &operand.m_children[0];
1249
24
      variable_child = &operand.m_children[1];
1250
24
    } else 
if (0
operand.m_children[1].m_type ==
1251
0
               Instruction::Operand::Type::Immediate) {
1252
0
      immediate_child = &operand.m_children[1];
1253
0
      variable_child = &operand.m_children[0];
1254
0
    }
1255
24
    if (!immediate_child) {
1256
0
      return std::make_pair(nullptr, 0);
1257
0
    }
1258
24
    lldb::addr_t adjusted_value = value;
1259
24
    if (immediate_child->m_negative) {
1260
0
      adjusted_value += immediate_child->m_immediate;
1261
24
    } else {
1262
24
      adjusted_value -= immediate_child->m_immediate;
1263
24
    }
1264
24
    std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1265
24
        GetBaseExplainingValue(*variable_child, register_context,
1266
24
                               adjusted_value);
1267
24
    if (!base_and_offset.first) {
1268
0
      return std::make_pair(nullptr, 0);
1269
0
    }
1270
24
    if (immediate_child->m_negative) {
1271
0
      base_and_offset.second -= immediate_child->m_immediate;
1272
24
    } else {
1273
24
      base_and_offset.second += immediate_child->m_immediate;
1274
24
    }
1275
24
    return base_and_offset;
1276
24
  }
1277
78
  case Instruction::Operand::Type::Register: {
1278
78
    const RegisterInfo *info =
1279
78
        register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1280
78
    if (!info) {
1281
0
      return std::make_pair(nullptr, 0);
1282
0
    }
1283
78
    RegisterValue reg_value;
1284
78
    if (!register_context.ReadRegister(info, reg_value)) {
1285
0
      return std::make_pair(nullptr, 0);
1286
0
    }
1287
78
    if (reg_value.GetAsUInt64() == value) {
1288
78
      return std::make_pair(&operand, 0);
1289
78
    } else {
1290
0
      return std::make_pair(nullptr, 0);
1291
0
    }
1292
78
  }
1293
102
  }
1294
0
  return std::make_pair(nullptr, 0);
1295
102
}
1296
1297
std::pair<const Instruction::Operand *, int64_t>
1298
GetBaseExplainingDereference(const Instruction::Operand &operand,
1299
                             RegisterContext &register_context,
1300
95
                             lldb::addr_t addr) {
1301
95
  if (operand.m_type == Instruction::Operand::Type::Dereference) {
1302
78
    return GetBaseExplainingValue(operand.m_children[0], register_context,
1303
78
                                  addr);
1304
78
  }
1305
17
  return std::make_pair(nullptr, 0);
1306
95
}
1307
}
1308
1309
78
lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1310
78
  TargetSP target_sp = CalculateTarget();
1311
1312
78
  const ArchSpec &target_arch = target_sp->GetArchitecture();
1313
1314
78
  AddressRange pc_range;
1315
78
  pc_range.GetBaseAddress() = GetFrameCodeAddress();
1316
78
  pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1317
1318
78
  const char *plugin_name = nullptr;
1319
78
  const char *flavor = nullptr;
1320
78
  const bool force_live_memory = true;
1321
1322
78
  DisassemblerSP disassembler_sp =
1323
78
      Disassembler::DisassembleRange(target_arch, plugin_name, flavor,
1324
78
                                     *target_sp, pc_range, force_live_memory);
1325
1326
78
  if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1327
0
    return ValueObjectSP();
1328
0
  }
1329
1330
78
  InstructionSP instruction_sp =
1331
78
      disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1332
1333
78
  llvm::SmallVector<Instruction::Operand, 3> operands;
1334
1335
78
  if (!instruction_sp->ParseOperands(operands)) {
1336
0
    return ValueObjectSP();
1337
0
  }
1338
1339
78
  RegisterContextSP register_context_sp = GetRegisterContext();
1340
1341
78
  if (!register_context_sp) {
1342
0
    return ValueObjectSP();
1343
0
  }
1344
1345
95
  
for (const Instruction::Operand &operand : operands)78
{
1346
95
    std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1347
95
        GetBaseExplainingDereference(operand, *register_context_sp, addr);
1348
1349
95
    if (!base_and_offset.first) {
1350
17
      continue;
1351
17
    }
1352
1353
78
    switch (base_and_offset.first->m_type) {
1354
0
    case Instruction::Operand::Type::Immediate: {
1355
0
      lldb_private::Address addr;
1356
0
      if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1357
0
                                            base_and_offset.second,
1358
0
                                        addr)) {
1359
0
        auto c_type_system_or_err =
1360
0
            target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
1361
0
        if (auto err = c_type_system_or_err.takeError()) {
1362
0
          LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), std::move(err),
1363
0
                         "Unable to guess value for given address: {0}");
1364
0
          return ValueObjectSP();
1365
0
        } else {
1366
0
          auto ts = *c_type_system_or_err;
1367
0
          if (!ts)
1368
0
            return {};
1369
0
          CompilerType void_ptr_type =
1370
0
              ts->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1371
0
                  .GetPointerType();
1372
0
          return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
1373
0
        }
1374
0
      } else {
1375
0
        return ValueObjectSP();
1376
0
      }
1377
0
      break;
1378
0
    }
1379
78
    case Instruction::Operand::Type::Register: {
1380
78
      return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1381
78
                                            base_and_offset.second);
1382
0
    }
1383
0
    default:
1384
0
      return ValueObjectSP();
1385
78
    }
1386
78
  }
1387
1388
0
  return ValueObjectSP();
1389
78
}
1390
1391
namespace {
1392
ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1393
134
                                int64_t offset) {
1394
134
  if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1395
0
    return ValueObjectSP();
1396
0
  }
1397
1398
134
  if (parent->IsPointerOrReferenceType()) {
1399
12
    return parent;
1400
12
  }
1401
1402
170
  
for (int ci = 0, ce = parent->GetNumChildren(); 122
ci != ce;
++ci48
) {
1403
92
    ValueObjectSP child_sp = parent->GetChildAtIndex(ci);
1404
1405
92
    if (!child_sp) {
1406
0
      return ValueObjectSP();
1407
0
    }
1408
1409
92
    int64_t child_offset = child_sp->GetByteOffset();
1410
92
    int64_t child_size = child_sp->GetByteSize().value_or(0);
1411
1412
92
    if (offset >= child_offset && 
offset < (child_offset + child_size)80
) {
1413
44
      return GetValueForOffset(frame, child_sp, offset - child_offset);
1414
44
    }
1415
92
  }
1416
1417
78
  if (offset == 0) {
1418
78
    return parent;
1419
78
  } else {
1420
0
    return ValueObjectSP();
1421
0
  }
1422
78
}
1423
1424
ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1425
                                             ValueObjectSP &base,
1426
90
                                             int64_t offset) {
1427
  // base is a pointer to something
1428
  // offset is the thing to add to the pointer We return the most sensible
1429
  // ValueObject for the result of *(base+offset)
1430
1431
90
  if (!base->IsPointerOrReferenceType()) {
1432
0
    return ValueObjectSP();
1433
0
  }
1434
1435
90
  Status error;
1436
90
  ValueObjectSP pointee = base->Dereference(error);
1437
1438
90
  if (!pointee) {
1439
0
    return ValueObjectSP();
1440
0
  }
1441
1442
90
  if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
1443
4
    int64_t index = offset / pointee->GetByteSize().value_or(1);
1444
4
    offset = offset % pointee->GetByteSize().value_or(1);
1445
4
    const bool can_create = true;
1446
4
    pointee = base->GetSyntheticArrayMember(index, can_create);
1447
4
  }
1448
1449
90
  if (!pointee || error.Fail()) {
1450
0
    return ValueObjectSP();
1451
0
  }
1452
1453
90
  return GetValueForOffset(frame, pointee, offset);
1454
90
}
1455
1456
/// Attempt to reconstruct the ValueObject for the address contained in a
1457
/// given register plus an offset.
1458
///
1459
/// \param [in] frame
1460
///   The current stack frame.
1461
///
1462
/// \param [in] reg
1463
///   The register.
1464
///
1465
/// \param [in] offset
1466
///   The offset from the register.
1467
///
1468
/// \param [in] disassembler
1469
///   A disassembler containing instructions valid up to the current PC.
1470
///
1471
/// \param [in] variables
1472
///   The variable list from the current frame,
1473
///
1474
/// \param [in] pc
1475
///   The program counter for the instruction considered the 'user'.
1476
///
1477
/// \return
1478
///   A string describing the base for the ExpressionPath.  This could be a
1479
///     variable, a register value, an argument, or a function return value.
1480
///   The ValueObject if found.  If valid, it has a valid ExpressionPath.
1481
lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1482
                                   int64_t offset, Disassembler &disassembler,
1483
164
                                   VariableList &variables, const Address &pc) {
1484
  // Example of operation for Intel:
1485
  //
1486
  // +14: movq   -0x8(%rbp), %rdi
1487
  // +18: movq   0x8(%rdi), %rdi
1488
  // +22: addl   0x4(%rdi), %eax
1489
  //
1490
  // f, a pointer to a struct, is known to be at -0x8(%rbp).
1491
  //
1492
  // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1493
  // +18 that assigns to rdi, and calls itself recursively for that dereference
1494
  //   DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1495
  //   +14 that assigns to rdi, and calls itself recursively for that
1496
  //   dereference
1497
  //     DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1498
  //     variable list.
1499
  //     Returns a ValueObject for f.  (That's what was stored at rbp-8 at +14)
1500
  //   Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1501
  //   at +18)
1502
  // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1503
  // rdi+4 at +22)
1504
1505
  // First, check the variable list to see if anything is at the specified
1506
  // location.
1507
1508
164
  using namespace OperandMatchers;
1509
1510
164
  const RegisterInfo *reg_info =
1511
164
      frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1512
164
  if (!reg_info) {
1513
0
    return ValueObjectSP();
1514
0
  }
1515
1516
164
  Instruction::Operand op =
1517
164
      offset ? Instruction::Operand::BuildDereference(
1518
110
                   Instruction::Operand::BuildSum(
1519
110
                       Instruction::Operand::BuildRegister(reg),
1520
110
                       Instruction::Operand::BuildImmediate(offset)))
1521
164
             : Instruction::Operand::BuildDereference(
1522
54
                   Instruction::Operand::BuildRegister(reg));
1523
1524
336
  for (VariableSP var_sp : variables) {
1525
336
    if (var_sp->LocationExpressionList().MatchesOperand(frame, op))
1526
74
      return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1527
336
  }
1528
1529
90
  const uint32_t current_inst =
1530
90
      disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1531
90
  if (current_inst == UINT32_MAX) {
1532
0
    return ValueObjectSP();
1533
0
  }
1534
1535
98
  
for (uint32_t ii = current_inst - 1; 90
ii != (uint32_t)-1;
--ii8
) {
1536
    // This is not an exact algorithm, and it sacrifices accuracy for
1537
    // generality.  Recognizing "mov" and "ld" instructions –– and which
1538
    // are their source and destination operands -- is something the
1539
    // disassembler should do for us.
1540
98
    InstructionSP instruction_sp =
1541
98
        disassembler.GetInstructionList().GetInstructionAtIndex(ii);
1542
1543
98
    if (instruction_sp->IsCall()) {
1544
4
      ABISP abi_sp = frame.CalculateProcess()->GetABI();
1545
4
      if (!abi_sp) {
1546
0
        continue;
1547
0
      }
1548
1549
4
      const char *return_register_name;
1550
4
      if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1551
0
        continue;
1552
0
      }
1553
1554
4
      const RegisterInfo *return_register_info =
1555
4
          frame.GetRegisterContext()->GetRegisterInfoByName(
1556
4
              return_register_name);
1557
4
      if (!return_register_info) {
1558
0
        continue;
1559
0
      }
1560
1561
4
      int64_t offset = 0;
1562
1563
4
      if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1564
4
                        MatchRegOp(*return_register_info))(op) &&
1565
4
          !MatchUnaryOp(
1566
4
              MatchOpType(Instruction::Operand::Type::Dereference),
1567
4
              MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1568
4
                            MatchRegOp(*return_register_info),
1569
4
                            FetchImmOp(offset)))(op)) {
1570
0
        continue;
1571
0
      }
1572
1573
4
      llvm::SmallVector<Instruction::Operand, 1> operands;
1574
4
      if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1575
0
        continue;
1576
0
      }
1577
1578
4
      switch (operands[0].m_type) {
1579
0
      default:
1580
0
        break;
1581
4
      case Instruction::Operand::Type::Immediate: {
1582
4
        SymbolContext sc;
1583
4
        Address load_address;
1584
4
        if (!frame.CalculateTarget()->ResolveLoadAddress(
1585
4
                operands[0].m_immediate, load_address)) {
1586
0
          break;
1587
0
        }
1588
4
        frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1589
4
            load_address, eSymbolContextFunction, sc);
1590
4
        if (!sc.function) {
1591
0
          break;
1592
0
        }
1593
4
        CompilerType function_type = sc.function->GetCompilerType();
1594
4
        if (!function_type.IsFunctionType()) {
1595
0
          break;
1596
0
        }
1597
4
        CompilerType return_type = function_type.GetFunctionReturnType();
1598
4
        RegisterValue return_value;
1599
4
        if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
1600
4
                                                      return_value)) {
1601
0
          break;
1602
0
        }
1603
4
        std::string name_str(
1604
4
            sc.function->GetName().AsCString("<unknown function>"));
1605
4
        name_str.append("()");
1606
4
        Address return_value_address(return_value.GetAsUInt64());
1607
4
        ValueObjectSP return_value_sp = ValueObjectMemory::Create(
1608
4
            &frame, name_str, return_value_address, return_type);
1609
4
        return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1610
4
      }
1611
4
      }
1612
1613
0
      continue;
1614
4
    }
1615
1616
94
    llvm::SmallVector<Instruction::Operand, 2> operands;
1617
94
    if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1618
0
      continue;
1619
0
    }
1620
1621
94
    Instruction::Operand *origin_operand = nullptr;
1622
188
    auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1623
188
      return MatchRegOp(*reg_info)(op) && 
op.m_clobbered90
;
1624
188
    };
1625
1626
94
    if (clobbered_reg_matcher(operands[0])) {
1627
0
      origin_operand = &operands[1];
1628
0
    }
1629
94
    else if (clobbered_reg_matcher(operands[1])) {
1630
86
      origin_operand = &operands[0];
1631
86
    }
1632
8
    else {
1633
8
      continue;
1634
8
    }
1635
1636
    // We have an origin operand.  Can we track its value down?
1637
86
    ValueObjectSP source_path;
1638
86
    ConstString origin_register;
1639
86
    int64_t origin_offset = 0;
1640
1641
86
    if (FetchRegOp(origin_register)(*origin_operand)) {
1642
0
      source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1643
0
                                   variables, instruction_sp->GetAddress());
1644
86
    } else if (MatchUnaryOp(
1645
86
                   MatchOpType(Instruction::Operand::Type::Dereference),
1646
86
                   FetchRegOp(origin_register))(*origin_operand) ||
1647
86
               MatchUnaryOp(
1648
86
                   MatchOpType(Instruction::Operand::Type::Dereference),
1649
86
                   MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1650
86
                                 FetchRegOp(origin_register),
1651
86
                                 FetchImmOp(origin_offset)))(*origin_operand)) {
1652
86
      source_path =
1653
86
          DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
1654
86
                         variables, instruction_sp->GetAddress());
1655
86
      if (!source_path) {
1656
0
        continue;
1657
0
      }
1658
86
      source_path =
1659
86
          GetValueForDereferincingOffset(frame, source_path, offset);
1660
86
    }
1661
1662
86
    if (source_path) {
1663
86
      return source_path;
1664
86
    }
1665
86
  }
1666
1667
0
  return ValueObjectSP();
1668
90
}
1669
}
1670
1671
lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1672
78
                                                               int64_t offset) {
1673
78
  TargetSP target_sp = CalculateTarget();
1674
1675
78
  const ArchSpec &target_arch = target_sp->GetArchitecture();
1676
1677
78
  Block *frame_block = GetFrameBlock();
1678
1679
78
  if (!frame_block) {
1680
0
    return ValueObjectSP();
1681
0
  }
1682
1683
78
  Function *function = frame_block->CalculateSymbolContextFunction();
1684
78
  if (!function) {
1685
0
    return ValueObjectSP();
1686
0
  }
1687
1688
78
  AddressRange pc_range = function->GetAddressRange();
1689
1690
78
  if (GetFrameCodeAddress().GetFileAddress() <
1691
78
          pc_range.GetBaseAddress().GetFileAddress() ||
1692
78
      GetFrameCodeAddress().GetFileAddress() -
1693
78
              pc_range.GetBaseAddress().GetFileAddress() >=
1694
78
          pc_range.GetByteSize()) {
1695
0
    return ValueObjectSP();
1696
0
  }
1697
1698
78
  const char *plugin_name = nullptr;
1699
78
  const char *flavor = nullptr;
1700
78
  const bool force_live_memory = true;
1701
78
  DisassemblerSP disassembler_sp =
1702
78
      Disassembler::DisassembleRange(target_arch, plugin_name, flavor,
1703
78
                                     *target_sp, pc_range, force_live_memory);
1704
1705
78
  if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1706
0
    return ValueObjectSP();
1707
0
  }
1708
1709
78
  const bool get_file_globals = false;
1710
78
  VariableList *variables = GetVariableList(get_file_globals, nullptr);
1711
1712
78
  if (!variables) {
1713
0
    return ValueObjectSP();
1714
0
  }
1715
1716
78
  return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1717
78
                        GetFrameCodeAddress());
1718
78
}
1719
1720
3.47k
lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
1721
3.47k
  ValueObjectSP value_sp;
1722
1723
3.47k
  if (!name)
1724
0
    return value_sp;
1725
1726
3.47k
  TargetSP target_sp = CalculateTarget();
1727
3.47k
  ProcessSP process_sp = CalculateProcess();
1728
1729
3.47k
  if (!target_sp && 
!process_sp0
)
1730
0
    return value_sp;
1731
1732
3.47k
  VariableList variable_list;
1733
3.47k
  VariableSP var_sp;
1734
3.47k
  SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
1735
1736
3.47k
  if (sc.block) {
1737
3.47k
    const bool can_create = true;
1738
3.47k
    const bool get_parent_variables = true;
1739
3.47k
    const bool stop_if_block_is_inlined_function = true;
1740
1741
3.47k
    if (sc.block->AppendVariables(
1742
3.47k
            can_create, get_parent_variables, stop_if_block_is_inlined_function,
1743
15.5k
            [this](Variable *v) { return v->IsInScope(this); },
1744
3.47k
            &variable_list)) {
1745
3.19k
      var_sp = variable_list.FindVariable(name);
1746
3.19k
    }
1747
1748
3.47k
    if (var_sp)
1749
993
      value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1750
3.47k
  }
1751
1752
3.47k
  return value_sp;
1753
3.47k
}
1754
1755
703k
TargetSP StackFrame::CalculateTarget() {
1756
703k
  TargetSP target_sp;
1757
703k
  ThreadSP thread_sp(GetThread());
1758
703k
  if (thread_sp) {
1759
703k
    ProcessSP process_sp(thread_sp->CalculateProcess());
1760
703k
    if (process_sp)
1761
703k
      target_sp = process_sp->CalculateTarget();
1762
703k
  }
1763
703k
  return target_sp;
1764
703k
}
1765
1766
4.28k
ProcessSP StackFrame::CalculateProcess() {
1767
4.28k
  ProcessSP process_sp;
1768
4.28k
  ThreadSP thread_sp(GetThread());
1769
4.28k
  if (thread_sp)
1770
4.28k
    process_sp = thread_sp->CalculateProcess();
1771
4.28k
  return process_sp;
1772
4.28k
}
1773
1774
88.9k
ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1775
1776
9.35k
StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1777
1778
67.0k
void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1779
67.0k
  exe_ctx.SetContext(shared_from_this());
1780
67.0k
}
1781
1782
void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
1783
4.40k
                                         const char *frame_marker) {
1784
4.40k
  if (strm == nullptr)
1785
0
    return;
1786
1787
4.40k
  GetSymbolContext(eSymbolContextEverything);
1788
4.40k
  ExecutionContext exe_ctx(shared_from_this());
1789
4.40k
  StreamString s;
1790
1791
4.40k
  if (frame_marker)
1792
1.22k
    s.PutCString(frame_marker);
1793
1794
4.40k
  const FormatEntity::Entry *frame_format = nullptr;
1795
4.40k
  Target *target = exe_ctx.GetTargetPtr();
1796
4.40k
  if (target) {
1797
4.40k
    if (show_unique) {
1798
0
      frame_format = target->GetDebugger().GetFrameFormatUnique();
1799
4.40k
    } else {
1800
4.40k
      frame_format = target->GetDebugger().GetFrameFormat();
1801
4.40k
    }
1802
4.40k
  }
1803
4.40k
  if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1804
4.40k
                                           nullptr, nullptr, false, false)) {
1805
4.39k
    strm->PutCString(s.GetString());
1806
4.39k
  } else {
1807
5
    Dump(strm, true, false);
1808
5
    strm->EOL();
1809
5
  }
1810
4.40k
}
1811
1812
void StackFrame::Dump(Stream *strm, bool show_frame_index,
1813
11
                      bool show_fullpaths) {
1814
11
  if (strm == nullptr)
1815
0
    return;
1816
1817
11
  if (show_frame_index)
1818
5
    strm->Printf("frame #%u: ", m_frame_index);
1819
11
  ExecutionContext exe_ctx(shared_from_this());
1820
11
  Target *target = exe_ctx.GetTargetPtr();
1821
11
  strm->Printf("0x%0*" PRIx64 " ",
1822
11
               target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1823
11
                      : 
160
,
1824
11
               GetFrameCodeAddress().GetLoadAddress(target));
1825
11
  GetSymbolContext(eSymbolContextEverything);
1826
11
  const bool show_module = true;
1827
11
  const bool show_inline = true;
1828
11
  const bool show_function_arguments = true;
1829
11
  const bool show_function_name = true;
1830
11
  m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1831
11
                       GetFrameCodeAddress(), show_fullpaths, show_module,
1832
11
                       show_inline, show_function_arguments,
1833
11
                       show_function_name);
1834
11
}
1835
1836
0
void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1837
0
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1838
0
  assert(GetStackID() ==
1839
0
         prev_frame.GetStackID()); // TODO: remove this after some testing
1840
0
  m_variable_list_sp = prev_frame.m_variable_list_sp;
1841
0
  m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
1842
0
  if (!m_disassembly.GetString().empty()) {
1843
0
    m_disassembly.Clear();
1844
0
    m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1845
0
  }
1846
0
}
1847
1848
4.09k
void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1849
4.09k
  std::lock_guard<std::recursive_mutex> guard(m_mutex);
1850
4.09k
  assert(GetStackID() ==
1851
4.09k
         curr_frame.GetStackID());     // TODO: remove this after some testing
1852
4.09k
  m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1853
4.09k
  assert(GetThread() == curr_frame.GetThread());
1854
4.09k
  m_frame_index = curr_frame.m_frame_index;
1855
4.09k
  m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1856
4.09k
  m_reg_context_sp = curr_frame.m_reg_context_sp;
1857
4.09k
  m_frame_code_addr = curr_frame.m_frame_code_addr;
1858
4.09k
  m_behaves_like_zeroth_frame = curr_frame.m_behaves_like_zeroth_frame;
1859
4.09k
  assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1860
4.09k
         m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1861
4.09k
  assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1862
4.09k
         m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1863
4.09k
  assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1864
4.09k
         m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1865
4.09k
  assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1866
4.09k
         m_sc.function == curr_frame.m_sc.function);
1867
4.09k
  m_sc = curr_frame.m_sc;
1868
4.09k
  m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1869
4.09k
  m_flags.Set(m_sc.GetResolvedMask());
1870
4.09k
  m_frame_base.Clear();
1871
4.09k
  m_frame_base_error.Clear();
1872
4.09k
}
1873
1874
0
bool StackFrame::HasCachedData() const {
1875
0
  if (m_variable_list_sp)
1876
0
    return true;
1877
0
  if (m_variable_list_value_objects.GetSize() > 0)
1878
0
    return true;
1879
0
  if (!m_disassembly.GetString().empty())
1880
0
    return true;
1881
0
  return false;
1882
0
}
1883
1884
bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1885
3.52k
                           bool show_unique, const char *frame_marker) {
1886
3.52k
  if (show_frame_info) {
1887
3.52k
    strm.Indent();
1888
3.52k
    DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
1889
3.52k
  }
1890
1891
3.52k
  if (show_source) {
1892
1.60k
    ExecutionContext exe_ctx(shared_from_this());
1893
1.60k
    bool have_source = false, have_debuginfo = false;
1894
1.60k
    Debugger::StopDisassemblyType disasm_display =
1895
1.60k
        Debugger::eStopDisassemblyTypeNever;
1896
1.60k
    Target *target = exe_ctx.GetTargetPtr();
1897
1.60k
    if (target) {
1898
1.60k
      Debugger &debugger = target->GetDebugger();
1899
1.60k
      const uint32_t source_lines_before =
1900
1.60k
          debugger.GetStopSourceLineCount(true);
1901
1.60k
      const uint32_t source_lines_after =
1902
1.60k
          debugger.GetStopSourceLineCount(false);
1903
1.60k
      disasm_display = debugger.GetStopDisassemblyDisplay();
1904
1905
1.60k
      GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1906
1.60k
      if (m_sc.comp_unit && 
m_sc.line_entry.IsValid()1.48k
) {
1907
1.48k
        have_debuginfo = true;
1908
1.48k
        if (source_lines_before > 0 || 
source_lines_after > 02
) {
1909
1.48k
          uint32_t start_line = m_sc.line_entry.line;
1910
1.48k
          if (!start_line && 
m_sc.function1
) {
1911
1
            FileSpec source_file;
1912
1
            m_sc.function->GetStartLineSourceInfo(source_file, start_line);
1913
1
          }
1914
1915
1.48k
          size_t num_lines =
1916
1.48k
              target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1917
1.48k
                  m_sc.line_entry.file, start_line, m_sc.line_entry.column,
1918
1.48k
                  source_lines_before, source_lines_after, "->", &strm);
1919
1.48k
          if (num_lines != 0)
1920
1.48k
            have_source = true;
1921
          // TODO: Give here a one time warning if source file is missing.
1922
1.48k
          if (!m_sc.line_entry.line) {
1923
1
            ConstString fn_name = m_sc.GetFunctionName();
1924
1925
1
            if (!fn_name.IsEmpty())
1926
1
              strm.Printf(
1927
1
                  "Note: this address is compiler-generated code in function "
1928
1
                  "%s that has no source code associated with it.",
1929
1
                  fn_name.AsCString());
1930
0
            else
1931
0
              strm.Printf("Note: this address is compiler-generated code that "
1932
0
                          "has no source code associated with it.");
1933
1
            strm.EOL();
1934
1
          }
1935
1.48k
        }
1936
1.48k
      }
1937
1.60k
      switch (disasm_display) {
1938
0
      case Debugger::eStopDisassemblyTypeNever:
1939
0
        break;
1940
1941
1.60k
      case Debugger::eStopDisassemblyTypeNoDebugInfo:
1942
1.60k
        if (have_debuginfo)
1943
1.48k
          break;
1944
1.60k
        
[[fallthrough]];116
1945
1946
116
      case Debugger::eStopDisassemblyTypeNoSource:
1947
116
        if (have_source)
1948
0
          break;
1949
116
        [[fallthrough]];
1950
1951
116
      case Debugger::eStopDisassemblyTypeAlways:
1952
116
        if (target) {
1953
116
          const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1954
116
          if (disasm_lines > 0) {
1955
116
            const ArchSpec &target_arch = target->GetArchitecture();
1956
116
            const char *plugin_name = nullptr;
1957
116
            const char *flavor = nullptr;
1958
116
            const bool mixed_source_and_assembly = false;
1959
116
            Disassembler::Disassemble(
1960
116
                target->GetDebugger(), target_arch, plugin_name, flavor,
1961
116
                exe_ctx, GetFrameCodeAddress(),
1962
116
                {Disassembler::Limit::Instructions, disasm_lines},
1963
116
                mixed_source_and_assembly, 0,
1964
116
                Disassembler::eOptionMarkPCAddress, strm);
1965
116
          }
1966
116
        }
1967
116
        break;
1968
1.60k
      }
1969
1.60k
    }
1970
1.60k
  }
1971
3.52k
  return true;
1972
3.52k
}
1973
1974
8.59k
RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
1975
8.59k
  if (!m_recognized_frame_sp) {
1976
8.56k
    m_recognized_frame_sp = GetThread()
1977
8.56k
                                ->GetProcess()
1978
8.56k
                                ->GetTarget()
1979
8.56k
                                .GetFrameRecognizerManager()
1980
8.56k
                                .RecognizeFrame(CalculateStackFrame());
1981
8.56k
  }
1982
8.59k
  return m_recognized_frame_sp;
1983
8.59k
}