Skip to content

Commit

Permalink
Add functions to iterate function names to IPluginDebugInfo
Browse files Browse the repository at this point in the history
Allow to inspect the list of functions defined in a plugin as well as the corresponding source filename if available and requested.
  • Loading branch information
peace-maker committed Jan 6, 2025
1 parent 0fd19ef commit 5d8aa56
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
16 changes: 15 additions & 1 deletion include/sp_vm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/** SourcePawn Engine API Versions */
#define SOURCEPAWN_ENGINE2_API_VERSION 0x10
#define SOURCEPAWN_API_VERSION 0x0213
#define SOURCEPAWN_API_VERSION 0x0214

namespace SourceMod {
struct IdentityToken_t;
Expand Down Expand Up @@ -365,6 +365,20 @@ class IPluginDebugInfo
* @return Full file name of source file or NULL if not found.
*/
virtual const char* GetFileName(size_t index) = 0;

/**
* @brief Returns the number of functions defined in this plugin.
*/
virtual size_t NumFunctions() = 0;

/**
* @brief Returns the function name at the given index.
*
* @param index Index of the function in the list of functions.
* @param file Output pointer to store filename where the function is defined in.
* @return Name of the function or NULL if not found.
*/
virtual const char* GetFunctionName(size_t index, const char** file) = 0;
};

class ICompilation;
Expand Down
8 changes: 8 additions & 0 deletions vm/legacy-image.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class LegacyImage
virtual bool LookupLineAddress(const uint32_t line, const char* file, ucell_t* addr) const = 0;
virtual size_t NumFiles() const = 0;
virtual const char* GetFileName(size_t index) const = 0;
virtual size_t NumFunctions() const = 0;
virtual const char* GetFunctionName(size_t index, const char** filename) const = 0;
virtual bool HasRtti() const = 0;
virtual const smx_rtti_method* GetMethodRttiByOffset(uint32_t pcode_offset) const = 0;
};
Expand Down Expand Up @@ -141,6 +143,12 @@ class EmptyImage : public LegacyImage
const char* GetFileName(size_t index) const override {
return nullptr;
}
size_t NumFunctions() const override {
return 0;
}
const char* GetFunctionName(size_t index, const char** filename) const override {
return nullptr;
}
const smx_rtti_method* GetMethodRttiByOffset(uint32_t pcode_offset) const override {
return nullptr;
}
Expand Down
10 changes: 10 additions & 0 deletions vm/plugin-runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@ PluginRuntime::GetFileName(size_t index)
return image_->GetFileName(index);
}

size_t
PluginRuntime::NumFunctions() {
return image_->NumFunctions();
}

const char*
PluginRuntime::GetFunctionName(size_t index, const char** filename) {
return image_->GetFunctionName(index, filename);
}

int
PluginRuntime::LookupFunctionAddress(const char* function, const char* file, ucell_t* addr)
{
Expand Down
2 changes: 2 additions & 0 deletions vm/plugin-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class PluginRuntime
int LookupFile(ucell_t addr, const char** filename) override;
size_t NumFiles() override;
const char* GetFileName(size_t index) override;
size_t NumFunctions() override;
const char* GetFunctionName(size_t index, const char** filename) override;
int LookupFunctionAddress(const char* function, const char* file, ucell_t* addr) override;
int LookupLineAddress(const uint32_t line, const char* file, ucell_t* addr) override;
const char* GetFilename() override {
Expand Down
22 changes: 22 additions & 0 deletions vm/smx-v1-image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,28 @@ SmxV1Image::GetFileName(size_t index) const
return debug_names_ + debug_files_[index].name;
}

size_t
SmxV1Image::NumFunctions() const {
if (rtti_methods_) {
return rtti_methods_->row_count;
}
return 0;
}

const char*
SmxV1Image::GetFunctionName(size_t index, const char** filename) const {
if (rtti_methods_) {
if (index >= rtti_methods_->row_count)
return nullptr;

const smx_rtti_method* method = getRttiRow<smx_rtti_method>(rtti_methods_, index);
if (filename)
*filename = LookupFile(method->pcode_start);
return names_ + method->name;
}
return nullptr;
}

template <typename SymbolType, typename DimType>
bool
SmxV1Image::getFunctionAddress(const SymbolType* syms, const char* function, ucell_t* funcaddr, uint32_t& index) const
Expand Down
2 changes: 2 additions & 0 deletions vm/smx-v1-image.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class SmxV1Image
bool LookupLineAddress(const uint32_t line, const char* file, ucell_t* addr) const override;
size_t NumFiles() const override;
const char* GetFileName(size_t index) const override;
size_t NumFunctions() const override;
const char* GetFunctionName(size_t index, const char** filename) const override;
bool HasRtti() const override;
const smx_rtti_method* GetMethodRttiByOffset(uint32_t pcode_offset) const override;

Expand Down

0 comments on commit 5d8aa56

Please sign in to comment.