reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
//===-- UnwindMacOSXFrameBackchain.cpp --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ArchSpec.h"

#include "RegisterContextMacOSXFrameBackchain.h"

#include <memory>

using namespace lldb;
using namespace lldb_private;

UnwindMacOSXFrameBackchain::UnwindMacOSXFrameBackchain(Thread &thread)
    : Unwind(thread), m_cursors() {}

uint32_t UnwindMacOSXFrameBackchain::DoGetFrameCount() {
  if (m_cursors.empty()) {
    ExecutionContext exe_ctx(m_thread.shared_from_this());
    Target *target = exe_ctx.GetTargetPtr();
    if (target) {
      const ArchSpec &target_arch = target->GetArchitecture();
      // Frame zero should always be supplied by the thread...
      exe_ctx.SetFrameSP(m_thread.GetStackFrameAtIndex(0));

      if (target_arch.GetAddressByteSize() == 8)
        GetStackFrameData_x86_64(exe_ctx);
      else
        GetStackFrameData_i386(exe_ctx);
    }
  }
  return m_cursors.size();
}

bool UnwindMacOSXFrameBackchain::DoGetFrameInfoAtIndex(
    uint32_t idx, addr_t &cfa, addr_t &pc, bool &behaves_like_zeroth_frame) {
  const uint32_t frame_count = GetFrameCount();
  if (idx < frame_count) {
    if (m_cursors[idx].pc == LLDB_INVALID_ADDRESS)
      return false;
    if (m_cursors[idx].fp == LLDB_INVALID_ADDRESS)
      return false;

    pc = m_cursors[idx].pc;
    cfa = m_cursors[idx].fp;
    behaves_like_zeroth_frame = (idx == 0);

    return true;
  }
  return false;
}

lldb::RegisterContextSP
UnwindMacOSXFrameBackchain::DoCreateRegisterContextForFrame(StackFrame *frame) {
  lldb::RegisterContextSP reg_ctx_sp;
  uint32_t concrete_idx = frame->GetConcreteFrameIndex();
  const uint32_t frame_count = GetFrameCount();
  if (concrete_idx < frame_count)
    reg_ctx_sp = std::make_shared<RegisterContextMacOSXFrameBackchain>(
        m_thread, concrete_idx, m_cursors[concrete_idx]);
  return reg_ctx_sp;
}

size_t UnwindMacOSXFrameBackchain::GetStackFrameData_i386(
    const ExecutionContext &exe_ctx) {
  m_cursors.clear();

  StackFrame *first_frame = exe_ctx.GetFramePtr();

  Process *process = exe_ctx.GetProcessPtr();
  if (process == nullptr)
    return 0;

  struct Frame_i386 {
    uint32_t fp;
    uint32_t pc;
  };

  RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
  assert(reg_ctx);

  Cursor cursor;
  cursor.pc = reg_ctx->GetPC(LLDB_INVALID_ADDRESS);
  cursor.fp = reg_ctx->GetFP(0);

  Frame_i386 frame = {static_cast<uint32_t>(cursor.fp),
                      static_cast<uint32_t>(cursor.pc)};

  m_cursors.push_back(cursor);

  const size_t k_frame_size = sizeof(frame);
  Status error;
  while (frame.fp != 0 && frame.pc != 0 && ((frame.fp & 7) == 0)) {
    // Read both the FP and PC (8 bytes)
    if (process->ReadMemory(frame.fp, &frame.fp, k_frame_size, error) !=
        k_frame_size)
      break;
    if (frame.pc >= 0x1000) {
      cursor.pc = frame.pc;
      cursor.fp = frame.fp;
      m_cursors.push_back(cursor);
    }
  }
  if (!m_cursors.empty()) {
    lldb::addr_t first_frame_pc = m_cursors.front().pc;
    if (first_frame_pc != LLDB_INVALID_ADDRESS) {
      const SymbolContextItem resolve_scope =
          eSymbolContextModule | eSymbolContextCompUnit |
          eSymbolContextFunction | eSymbolContextSymbol;

      SymbolContext first_frame_sc(
          first_frame->GetSymbolContext(resolve_scope));
      const AddressRange *addr_range_ptr = nullptr;
      AddressRange range;
      if (first_frame_sc.function)
        addr_range_ptr = &first_frame_sc.function->GetAddressRange();
      else if (first_frame_sc.symbol) {
        range.GetBaseAddress() = first_frame_sc.symbol->GetAddress();
        range.SetByteSize(first_frame_sc.symbol->GetByteSize());
        addr_range_ptr = &range;
      }

      if (addr_range_ptr) {
        if (first_frame->GetFrameCodeAddress() ==
            addr_range_ptr->GetBaseAddress()) {
          // We are at the first instruction, so we can recover the previous PC
          // by dereferencing the SP
          lldb::addr_t first_frame_sp = reg_ctx->GetSP(0);
          // Read the real second frame return address into frame.pc
          if (first_frame_sp &&
              process->ReadMemory(first_frame_sp, &frame.pc, sizeof(frame.pc),
                                  error) == sizeof(frame.pc)) {
            cursor.fp = m_cursors.front().fp;
            cursor.pc = frame.pc; // Set the new second frame PC

            // Insert the second frame
            m_cursors.insert(m_cursors.begin() + 1, cursor);

            m_cursors.front().fp = first_frame_sp;
          }
        }
      }
    }
  }
  //    uint32_t i=0;
  //    printf("      PC                 FP\n");
  //    printf("      ------------------ ------------------ \n");
  //    for (i=0; i<m_cursors.size(); ++i)
  //    {
  //        printf("[%3u] 0x%16.16" PRIx64 " 0x%16.16" PRIx64 "\n", i,
  //        m_cursors[i].pc, m_cursors[i].fp);
  //    }
  return m_cursors.size();
}

size_t UnwindMacOSXFrameBackchain::GetStackFrameData_x86_64(
    const ExecutionContext &exe_ctx) {
  m_cursors.clear();

  Process *process = exe_ctx.GetProcessPtr();
  if (process == nullptr)
    return 0;

  StackFrame *first_frame = exe_ctx.GetFramePtr();

  struct Frame_x86_64 {
    uint64_t fp;
    uint64_t pc;
  };

  RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
  assert(reg_ctx);

  Cursor cursor;
  cursor.pc = reg_ctx->GetPC(LLDB_INVALID_ADDRESS);
  cursor.fp = reg_ctx->GetFP(0);

  Frame_x86_64 frame = {cursor.fp, cursor.pc};

  m_cursors.push_back(cursor);
  Status error;
  const size_t k_frame_size = sizeof(frame);
  while (frame.fp != 0 && frame.pc != 0 && ((frame.fp & 7) == 0)) {
    // Read both the FP and PC (16 bytes)
    if (process->ReadMemory(frame.fp, &frame.fp, k_frame_size, error) !=
        k_frame_size)
      break;

    if (frame.pc >= 0x1000) {
      cursor.pc = frame.pc;
      cursor.fp = frame.fp;
      m_cursors.push_back(cursor);
    }
  }
  if (!m_cursors.empty()) {
    lldb::addr_t first_frame_pc = m_cursors.front().pc;
    if (first_frame_pc != LLDB_INVALID_ADDRESS) {
      const SymbolContextItem resolve_scope =
          eSymbolContextModule | eSymbolContextCompUnit |
          eSymbolContextFunction | eSymbolContextSymbol;

      SymbolContext first_frame_sc(
          first_frame->GetSymbolContext(resolve_scope));
      const AddressRange *addr_range_ptr = nullptr;
      AddressRange range;
      if (first_frame_sc.function)
        addr_range_ptr = &first_frame_sc.function->GetAddressRange();
      else if (first_frame_sc.symbol) {
        range.GetBaseAddress() = first_frame_sc.symbol->GetAddress();
        range.SetByteSize(first_frame_sc.symbol->GetByteSize());
        addr_range_ptr = &range;
      }

      if (addr_range_ptr) {
        if (first_frame->GetFrameCodeAddress() ==
            addr_range_ptr->GetBaseAddress()) {
          // We are at the first instruction, so we can recover the previous PC
          // by dereferencing the SP
          lldb::addr_t first_frame_sp = reg_ctx->GetSP(0);
          // Read the real second frame return address into frame.pc
          if (process->ReadMemory(first_frame_sp, &frame.pc, sizeof(frame.pc),
                                  error) == sizeof(frame.pc)) {
            cursor.fp = m_cursors.front().fp;
            cursor.pc = frame.pc; // Set the new second frame PC

            // Insert the second frame
            m_cursors.insert(m_cursors.begin() + 1, cursor);

            m_cursors.front().fp = first_frame_sp;
          }
        }
      }
    }
  }
  return m_cursors.size();
}