Merge remote-tracking branch 'lethosor/memscan-funcs' into develop

develop
lethosor 2023-08-11 01:38:15 -04:00
commit 34ddf6bed7
No known key found for this signature in database
GPG Key ID: 76A269552F4F58C1
5 changed files with 82 additions and 0 deletions

@ -2732,6 +2732,11 @@ and are only documented here for completeness:
The oldval, newval or delta arguments may be used to specify additional constraints. The oldval, newval or delta arguments may be used to specify additional constraints.
Returns: *found_index*, or *nil* if end reached. Returns: *found_index*, or *nil* if end reached.
* ``dfhack.internal.cxxDemangle(mangled_name)``
Decodes a mangled C++ symbol name. Returns the demangled name on success, or
``nil, error_message`` on failure.
* ``dfhack.internal.getDir(path)`` * ``dfhack.internal.getDir(path)``
Lists files/directories in a directory. Lists files/directories in a directory.

@ -3333,6 +3333,24 @@ static int internal_diffscan(lua_State *L)
return 1; return 1;
} }
static int internal_cxxDemangle(lua_State *L)
{
std::string mangled = luaL_checkstring(L, 1);
std::string status;
std::string demangled = cxx_demangle(mangled, &status);
if (demangled.length())
{
lua_pushstring(L, demangled.c_str());
return 1;
}
else
{
lua_pushnil(L);
lua_pushstring(L, status.c_str());
return 2;
}
}
static int internal_runCommand(lua_State *L) static int internal_runCommand(lua_State *L)
{ {
color_ostream *out = NULL; color_ostream *out = NULL;
@ -3637,6 +3655,7 @@ static const luaL_Reg dfhack_internal_funcs[] = {
{ "memcmp", internal_memcmp }, { "memcmp", internal_memcmp },
{ "memscan", internal_memscan }, { "memscan", internal_memscan },
{ "diffscan", internal_diffscan }, { "diffscan", internal_diffscan },
{ "cxxDemangle", internal_cxxDemangle },
{ "getDir", filesystem_listdir }, { "getDir", filesystem_listdir },
{ "runCommand", internal_runCommand }, { "runCommand", internal_runCommand },
{ "getModifiers", internal_getModifiers }, { "getModifiers", internal_getModifiers },

@ -36,6 +36,7 @@ distribution.
#else #else
#include <sys/time.h> #include <sys/time.h>
#include <ctime> #include <ctime>
#include <cxxabi.h>
#endif #endif
#include <ctype.h> #include <ctype.h>
@ -472,3 +473,29 @@ DFHACK_EXPORT std::string DF2CONSOLE(DFHack::color_ostream &out, const std::stri
{ {
return out.is_console() ? DF2CONSOLE(in) : in; return out.is_console() ? DF2CONSOLE(in) : in;
} }
DFHACK_EXPORT std::string cxx_demangle(const std::string &mangled_name, std::string *status_out)
{
#ifdef __GNUC__
int status;
char *demangled = abi::__cxa_demangle(mangled_name.c_str(), nullptr, nullptr, &status);
std::string out;
if (demangled) {
out = demangled;
free(demangled);
}
if (status_out) {
if (status == 0) *status_out = "success";
else if (status == -1) *status_out = "memory allocation failure";
else if (status == -2) *status_out = "invalid mangled name";
else if (status == -3) *status_out = "invalid arguments";
else *status_out = "unknown error";
}
return out;
#else
if (status_out) {
*status_out = "not implemented on this platform";
}
return "";
#endif
}

@ -474,3 +474,5 @@ DFHACK_EXPORT std::string UTF2DF(const std::string &in);
DFHACK_EXPORT std::string DF2UTF(const std::string &in); DFHACK_EXPORT std::string DF2UTF(const std::string &in);
DFHACK_EXPORT std::string DF2CONSOLE(const std::string &in); DFHACK_EXPORT std::string DF2CONSOLE(const std::string &in);
DFHACK_EXPORT std::string DF2CONSOLE(DFHack::color_ostream &out, const std::string &in); DFHACK_EXPORT std::string DF2CONSOLE(DFHack::color_ostream &out, const std::string &in);
DFHACK_EXPORT std::string cxx_demangle(const std::string &mangled_name, std::string *status_out);

@ -217,6 +217,7 @@ function get_code_segment()
for i,mem in ipairs(dfhack.internal.getMemRanges()) do for i,mem in ipairs(dfhack.internal.getMemRanges()) do
if mem.read and mem.execute if mem.read and mem.execute
and (string.match(mem.name,'/dwarfort%.exe$') and (string.match(mem.name,'/dwarfort%.exe$')
or string.match(mem.name,'/dwarfort$')
or string.match(mem.name,'/Dwarf_Fortress$') or string.match(mem.name,'/Dwarf_Fortress$')
or string.match(mem.name,'Dwarf Fortress%.exe')) or string.match(mem.name,'Dwarf Fortress%.exe'))
then then
@ -533,4 +534,32 @@ function get_screen_size()
return w,h return w,h
end end
-- Global table
function read_c_string(char_ptr)
local s = ''
local i = 0
while char_ptr[i] ~= 0 do
s = s .. string.char(char_ptr[i])
i = i + 1
end
return s
end
function read_global_table(global_table)
global_table = global_table or df.global.global_table
local out = {}
local i = 0
while true do
-- use _displace() so we can read past the bounds of the array set in structures, if necessary
local entry = global_table[0]:_displace(i)
if not entry.name or not entry.address then
break
end
out[read_c_string(entry.name)] = entry
i = i + 1
end
return out
end
return _ENV return _ENV