Skip to content

Commit 5185822

Browse files
committed
Support pre-RTTI debug symbol information
Allow to use the API on old .smx binaries too.
1 parent 6bbb94c commit 5185822

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

vm/smx-v1-image.cpp

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,71 @@ SmxV1Image::GetFileName(size_t index) const
10601060
return debug_names_ + debug_files_[index].name;
10611061
}
10621062

1063+
template <typename SymbolType, typename DimType>
1064+
uint32_t
1065+
SmxV1Image::getFunctionCount(const SymbolType* syms) const {
1066+
const uint8_t* cursor = reinterpret_cast<const uint8_t*>(syms);
1067+
const uint8_t* cursor_end = cursor + debug_symbols_section_->size;
1068+
uint32_t func_count = 0;
1069+
for (uint32_t i = 0; i < debug_info_->num_syms; i++) {
1070+
if (cursor + sizeof(SymbolType) > cursor_end)
1071+
break;
1072+
1073+
const SymbolType* sym = reinterpret_cast<const SymbolType*>(cursor);
1074+
if (sym->ident == sp::IDENT_FUNCTION)
1075+
func_count++;
1076+
1077+
if (sym->dimcount > 0)
1078+
cursor += sizeof(DimType) * sym->dimcount;
1079+
cursor += sizeof(SymbolType);
1080+
}
1081+
return func_count;
1082+
}
1083+
10631084
size_t
10641085
SmxV1Image::NumFunctions() const {
10651086
if (rtti_methods_) {
10661087
return rtti_methods_->row_count;
10671088
}
1068-
return 0;
1089+
1090+
// Count function symbols once.
1091+
static uint32_t num_debug_functions = 0;
1092+
if (num_debug_functions == 0) {
1093+
if (debug_syms_)
1094+
num_debug_functions = getFunctionCount<sp_fdbg_symbol_t, sp_fdbg_arraydim_t>(debug_syms_);
1095+
else
1096+
num_debug_functions = getFunctionCount<sp_u_fdbg_symbol_t, sp_u_fdbg_arraydim_t>(debug_syms_unpacked_);
1097+
}
1098+
return num_debug_functions;
1099+
}
1100+
1101+
template <typename SymbolType, typename DimType>
1102+
const char*
1103+
SmxV1Image::getFunctionName(const SymbolType* syms, const char** filename, uint32_t index) const {
1104+
const uint8_t* cursor = reinterpret_cast<const uint8_t*>(syms);
1105+
const uint8_t* cursor_end = cursor + debug_symbols_section_->size;
1106+
uint32_t func_count = 0;
1107+
for (uint32_t i = 0; i < debug_info_->num_syms; i++) {
1108+
if (cursor + sizeof(SymbolType) > cursor_end)
1109+
break;
1110+
1111+
const SymbolType* sym = reinterpret_cast<const SymbolType*>(cursor);
1112+
if (sym->ident == sp::IDENT_FUNCTION) {
1113+
if (func_count == index) {
1114+
if (filename)
1115+
*filename = LookupFile(sym->addr);
1116+
if (sym->name < debug_names_section_->size)
1117+
return debug_names_ + sym->name;
1118+
return nullptr;
1119+
}
1120+
func_count++;
1121+
}
1122+
1123+
if (sym->dimcount > 0)
1124+
cursor += sizeof(DimType) * sym->dimcount;
1125+
cursor += sizeof(SymbolType);
1126+
}
1127+
return nullptr;
10691128
}
10701129

10711130
const char*
@@ -1079,7 +1138,12 @@ SmxV1Image::GetFunctionName(size_t index, const char** filename) const {
10791138
*filename = LookupFile(method->pcode_start);
10801139
return names_ + method->name;
10811140
}
1082-
return nullptr;
1141+
1142+
if (debug_syms_) {
1143+
return getFunctionName<sp_fdbg_symbol_t, sp_fdbg_arraydim_t>(debug_syms_, filename, index);
1144+
} else {
1145+
return getFunctionName<sp_u_fdbg_symbol_t, sp_u_fdbg_arraydim_t>(debug_syms_unpacked_, filename, index);
1146+
}
10831147
}
10841148

10851149
template <typename SymbolType, typename DimType>

vm/smx-v1-image.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ class SmxV1Image
221221
template <typename SymbolType, typename DimType>
222222
const char* lookupFunction(const SymbolType* syms, uint32_t addr) const;
223223
template <typename SymbolType, typename DimType>
224+
uint32_t getFunctionCount(const SymbolType* syms) const;
225+
template <typename SymbolType, typename DimType>
226+
const char* getFunctionName(const SymbolType* syms, const char** filename, uint32_t index) const;
227+
template <typename SymbolType, typename DimType>
224228
bool getFunctionAddress(const SymbolType* syms, const char* function, ucell_t* funcaddr, uint32_t& index) const;
225229

226230
const smx_rtti_table_header* findRttiSection(const char* name) const {

0 commit comments

Comments
 (0)