Skip to content

Commit

Permalink
Merge remote-tracking branch 'lethosor/memscan-funcs' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lethosor committed Aug 11, 2023
2 parents 7526556 + ea43d6c commit 34ddf6b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,11 @@ and are only documented here for completeness:
The oldval, newval or delta arguments may be used to specify additional constraints.
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)``

Lists files/directories in a directory.
Expand Down
19 changes: 19 additions & 0 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,24 @@ static int internal_diffscan(lua_State *L)
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)
{
color_ostream *out = NULL;
Expand Down Expand Up @@ -3637,6 +3655,7 @@ static const luaL_Reg dfhack_internal_funcs[] = {
{ "memcmp", internal_memcmp },
{ "memscan", internal_memscan },
{ "diffscan", internal_diffscan },
{ "cxxDemangle", internal_cxxDemangle },
{ "getDir", filesystem_listdir },
{ "runCommand", internal_runCommand },
{ "getModifiers", internal_getModifiers },
Expand Down
27 changes: 27 additions & 0 deletions library/MiscUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ distribution.
#else
#include <sys/time.h>
#include <ctime>
#include <cxxabi.h>
#endif

#include <ctype.h>
Expand Down Expand Up @@ -472,3 +473,29 @@ DFHACK_EXPORT std::string DF2CONSOLE(DFHack::color_ostream &out, const std::stri
{
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
}
2 changes: 2 additions & 0 deletions library/include/MiscUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 DF2CONSOLE(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);
29 changes: 29 additions & 0 deletions library/lua/memscan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ function get_code_segment()
for i,mem in ipairs(dfhack.internal.getMemRanges()) do
if mem.read and mem.execute
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%.exe'))
then
Expand Down Expand Up @@ -533,4 +534,32 @@ function get_screen_size()
return w,h
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

0 comments on commit 34ddf6b

Please sign in to comment.