Repository: llvm/llvm-project
File: lldb/source/Plugins/Process/FreeBSDKernel/RegisterContextFreeBSDKernel_arm64.cpp (C++)

Code before:
    case gpr_x28_arm64:
    case gpr_fp_arm64:
      static_assert(gpr_fp_arm64 - gpr_x0_arm64 == 29,
                    "nonconsecutive arm64 register numbers");
      value = pcb13.x[reg - gpr_x0_arm64];
      break;
    case gpr_sp_arm64:
      value = pcb13.sp;
      break;
    case gpr_pc_arm64:
      // The pc of crashing thread is stored in lr.
      value = pcb13.lr;
      break;
    default:
      return false;
    }
  }
  return true;
}

bool RegisterContextFreeBSDKernel_arm64::WriteRegister(
    const RegisterInfo *reg_info, const RegisterValue &value) {
  return false;
}

int64_t RegisterContextFreeBSDKernel_arm64::GetOsreldate() {
  ProcessSP process_sp = m_thread.GetProcess();
  if (!process_sp)
    return 0;

  Target &target = process_sp->GetTarget();

  SymbolContextList sc_list;
  target.GetImages().FindSymbolsWithNameAndType(ConstString("osreldate"),
                                                lldb::eSymbolTypeData, sc_list);

  if (sc_list.GetSize() > 0) {
    SymbolContext sc;
    sc_list.GetContextAtIndex(0, sc);

    if (sc.symbol) {
      lldb::addr_t addr = sc.symbol->GetLoadAddress(&target);
      if (addr != LLDB_INVALID_ADDRESS) {
        Status error;
        int64_t osreldate = 0;
        size_t bytes_read =
            process_sp->ReadMemory(addr, &osreldate, sizeof(osreldate), error);
        if (bytes_read == sizeof(osreldate) && error.Success()) {
          return osreldate;
        }
      }

Review comment (suggestion):
I'm looking at https://github.com/search?q=repo%3Afreebsd%2Ffreebsd-src%20osreldate&type=code and wondering if this is in fact an int64_t. It's declared as `int` in most places I see.

My concern is that the code below that reads 8 bytes might pick up some random values in the other 4 bytes. Unless I have misunderstood and the C APIs use int but the storage is in fact 64-bit.

Code after:
    case gpr_x16_arm64:
    case gpr_x17_arm64:
    case gpr_x18_arm64:
    case gpr_x19_arm64:
    case gpr_x20_arm64:
    case gpr_x21_arm64:
    case gpr_x22_arm64:
    case gpr_x23_arm64:
    case gpr_x24_arm64:
    case gpr_x25_arm64:
    case gpr_x26_arm64:
    case gpr_x27_arm64:
    case gpr_x28_arm64:
    case gpr_fp_arm64:
      static_assert(gpr_fp_arm64 - gpr_x0_arm64 == 29,
                    "nonconsecutive arm64 register numbers");
      value = pcb13.x[reg - gpr_x0_arm64];
      break;
    case gpr_sp_arm64:
      value = pcb13.sp;
      break;
    case gpr_pc_arm64:
      // The pc of crashing thread is stored in lr.
      value = pcb13.lr;
      break;
    default:
      return false;
    }
  }
  return true;
}

bool RegisterContextFreeBSDKernel_arm64::WriteRegister(
    const RegisterInfo *reg_info, const RegisterValue &value) {
  return false;
}

std::optional<int> RegisterContextFreeBSDKernel_arm64::GetOsreldate() {
  ProcessSP process_sp = m_thread.GetProcess();
  if (!process_sp)
    return std::nullopt;

  Target &target = process_sp->GetTarget();

  SymbolContextList sc_list;
  target.GetImages().FindSymbolsWithNameAndType(ConstString("osreldate"),
                                                lldb::eSymbolTypeData, sc_list);
  if (sc_list.GetSize() == 0)
    return std::nullopt;

  SymbolContext sc;