Skip to content

Commit 5d8aa56

Browse files
committed
Add functions to iterate function names to IPluginDebugInfo
Allow to inspect the list of functions defined in a plugin as well as the corresponding source filename if available and requested.
1 parent 0fd19ef commit 5d8aa56

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

include/sp_vm_api.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/** SourcePawn Engine API Versions */
2626
#define SOURCEPAWN_ENGINE2_API_VERSION 0x10
27-
#define SOURCEPAWN_API_VERSION 0x0213
27+
#define SOURCEPAWN_API_VERSION 0x0214
2828

2929
namespace SourceMod {
3030
struct IdentityToken_t;
@@ -365,6 +365,20 @@ class IPluginDebugInfo
365365
* @return Full file name of source file or NULL if not found.
366366
*/
367367
virtual const char* GetFileName(size_t index) = 0;
368+
369+
/**
370+
* @brief Returns the number of functions defined in this plugin.
371+
*/
372+
virtual size_t NumFunctions() = 0;
373+
374+
/**
375+
* @brief Returns the function name at the given index.
376+
*
377+
* @param index Index of the function in the list of functions.
378+
* @param file Output pointer to store filename where the function is defined in.
379+
* @return Name of the function or NULL if not found.
380+
*/
381+
virtual const char* GetFunctionName(size_t index, const char** file) = 0;
368382
};
369383

370384
class ICompilation;

vm/legacy-image.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class LegacyImage
5959
virtual bool LookupLineAddress(const uint32_t line, const char* file, ucell_t* addr) const = 0;
6060
virtual size_t NumFiles() const = 0;
6161
virtual const char* GetFileName(size_t index) const = 0;
62+
virtual size_t NumFunctions() const = 0;
63+
virtual const char* GetFunctionName(size_t index, const char** filename) const = 0;
6264
virtual bool HasRtti() const = 0;
6365
virtual const smx_rtti_method* GetMethodRttiByOffset(uint32_t pcode_offset) const = 0;
6466
};
@@ -141,6 +143,12 @@ class EmptyImage : public LegacyImage
141143
const char* GetFileName(size_t index) const override {
142144
return nullptr;
143145
}
146+
size_t NumFunctions() const override {
147+
return 0;
148+
}
149+
const char* GetFunctionName(size_t index, const char** filename) const override {
150+
return nullptr;
151+
}
144152
const smx_rtti_method* GetMethodRttiByOffset(uint32_t pcode_offset) const override {
145153
return nullptr;
146154
}

vm/plugin-runtime.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,16 @@ PluginRuntime::GetFileName(size_t index)
598598
return image_->GetFileName(index);
599599
}
600600

601+
size_t
602+
PluginRuntime::NumFunctions() {
603+
return image_->NumFunctions();
604+
}
605+
606+
const char*
607+
PluginRuntime::GetFunctionName(size_t index, const char** filename) {
608+
return image_->GetFunctionName(index, filename);
609+
}
610+
601611
int
602612
PluginRuntime::LookupFunctionAddress(const char* function, const char* file, ucell_t* addr)
603613
{

vm/plugin-runtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class PluginRuntime
8484
int LookupFile(ucell_t addr, const char** filename) override;
8585
size_t NumFiles() override;
8686
const char* GetFileName(size_t index) override;
87+
size_t NumFunctions() override;
88+
const char* GetFunctionName(size_t index, const char** filename) override;
8789
int LookupFunctionAddress(const char* function, const char* file, ucell_t* addr) override;
8890
int LookupLineAddress(const uint32_t line, const char* file, ucell_t* addr) override;
8991
const char* GetFilename() override {

vm/smx-v1-image.cpp

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

1063+
size_t
1064+
SmxV1Image::NumFunctions() const {
1065+
if (rtti_methods_) {
1066+
return rtti_methods_->row_count;
1067+
}
1068+
return 0;
1069+
}
1070+
1071+
const char*
1072+
SmxV1Image::GetFunctionName(size_t index, const char** filename) const {
1073+
if (rtti_methods_) {
1074+
if (index >= rtti_methods_->row_count)
1075+
return nullptr;
1076+
1077+
const smx_rtti_method* method = getRttiRow<smx_rtti_method>(rtti_methods_, index);
1078+
if (filename)
1079+
*filename = LookupFile(method->pcode_start);
1080+
return names_ + method->name;
1081+
}
1082+
return nullptr;
1083+
}
1084+
10631085
template <typename SymbolType, typename DimType>
10641086
bool
10651087
SmxV1Image::getFunctionAddress(const SymbolType* syms, const char* function, ucell_t* funcaddr, uint32_t& index) const

vm/smx-v1-image.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class SmxV1Image
6969
bool LookupLineAddress(const uint32_t line, const char* file, ucell_t* addr) const override;
7070
size_t NumFiles() const override;
7171
const char* GetFileName(size_t index) const override;
72+
size_t NumFunctions() const override;
73+
const char* GetFunctionName(size_t index, const char** filename) const override;
7274
bool HasRtti() const override;
7375
const smx_rtti_method* GetMethodRttiByOffset(uint32_t pcode_offset) const override;
7476

0 commit comments

Comments
 (0)