Skip to content

Commit 4e86c60

Browse files
committed
Zero brain studio as a debugger
Now possible to debug pllua untrusted with Zero Brain Studio. At first mobdebug needs to be installed (luarocks install mobdebug) To enable extended debug info we need run this code: do $$ remote_debug_info(true) $$ language plluau Then start debugging server in Zero Brain Studio and then possible to debug line by line code like this: do $$ local mobdebug = require('mobdebug') mobdebug.start() -- same as start("localhost", 8172) print("Start") local foo = 0 for i = 1, 3 do local function bar() print("In bar") end foo = i print("Loop") bar() end print("End") $$ language plluau Note: ZBS had a bug in linux when many tabs opened for debugging if ZBS was in background, but in ZBS github master branch already fixed.
1 parent d1ad3d1 commit 4e86c60

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

Diff for: pllua.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Datum pllua_inline_handler(PG_FUNCTION_ARGS) {
7878

7979
void p_lua_mem_cxt(void){}
8080
void p_lua_master_state(void){}
81+
void p_remote_debug_info(void){}
82+
8183

8284
void push_spi_error(lua_State *L, MemoryContext oldcontext)
8385
{
@@ -137,6 +139,3 @@ int pg_to_regtype(char *typ_name)
137139
else
138140
return -1;
139141
}
140-
141-
142-

Diff for: pllua.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ void p_lua_mem_cxt(void);
1515

1616
void p_lua_master_state(void);
1717

18+
void p_remote_debug_info(void);
19+
1820
typedef struct luaP_Buffer {
1921
int size;
2022
Datum *value;

Diff for: plluaapi.c

+52-4
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,26 @@ static int luaP_register_type (lua_State *L) {
490490
return luaL_error(L, "unknown type %s", reg_name);
491491
}
492492

493+
static int luaP_remote_debug_info (lua_State *L) {
494+
BEGINLUA;
495+
if ((lua_gettop(L) != 1)||(!lua_isboolean( L, 1 ))){
496+
return luaL_error(L, "wrong arguments lentgh/type");
497+
}
498+
lua_pushlightuserdata(L, p_remote_debug_info);
499+
lua_rawget(L, LUA_REGISTRYINDEX);
500+
if (lua_isnil(L,-1)){
501+
return luaL_error(L, "trusted lua does not support debugging");
502+
}
503+
lua_pop(L, 1);
504+
505+
lua_pushlightuserdata(L, p_remote_debug_info);
506+
lua_swap(L);
507+
lua_rawset(L, LUA_REGISTRYINDEX);
508+
509+
ENDLUAV(-1);
510+
return -1;
511+
}
512+
493513
static const luaL_Reg luaP_funcs[] = {
494514
{"setshared", luaP_setshared},
495515
{"log", luaP_log},
@@ -501,6 +521,7 @@ static const luaL_Reg luaP_funcs[] = {
501521
{"register_type", luaP_register_type},
502522
{"pgfunc", get_pgfunc},
503523
{"subtransaction", use_subtransaction},
524+
{"remote_debug_info", luaP_remote_debug_info},
504525
{NULL, NULL}
505526
};
506527

@@ -529,6 +550,12 @@ lua_State *luaP_newstate (int trusted) {
529550
lua_pushlightuserdata(L, p_lua_master_state);
530551
lua_pushlightuserdata(L, (void *) L);
531552
lua_rawset(L, LUA_REGISTRYINDEX);
553+
554+
if(!trusted){
555+
lua_pushlightuserdata(L, p_remote_debug_info);
556+
lua_pushboolean(L, false);
557+
lua_rawset(L, LUA_REGISTRYINDEX);
558+
}
532559
/* core libs */
533560
if (trusted) {
534561
const luaL_Reg luaP_trusted_libs[] = {
@@ -697,10 +724,11 @@ static void luaP_newfunction (lua_State *L, int oid, HeapTuple proc,
697724
Form_pg_proc procst;
698725
bool isnull;
699726
Datum prosrc, *argname;
700-
const char *s, *fname;
727+
const char *source, *fname;
701728
text *t;
702729
luaL_Buffer b;
703730
int init = (*fi == NULL); /* not initialized? */
731+
const char *chunk_name = PLLUA_CHUNKNAME;
704732
/* read proc info */
705733
procst = (Form_pg_proc) GETSTRUCT(proc);
706734
prosrc = SysCacheGetAttr(PROCOID, proc, Anum_pg_proc_prosrc, &isnull);
@@ -762,8 +790,18 @@ static void luaP_newfunction (lua_State *L, int oid, HeapTuple proc,
762790
luaL_addlstring(&b, fname, strlen(fname));
763791
/* create function */
764792
luaL_pushresult(&b);
765-
s = lua_tostring(L, -1);
766-
if (luaL_loadbuffer(L, s, strlen(s), PLLUA_CHUNKNAME))
793+
source = lua_tostring(L, -1);
794+
795+
796+
//check if remote debug
797+
lua_pushlightuserdata(L, p_remote_debug_info);
798+
lua_rawget(L, LUA_REGISTRYINDEX);
799+
if ((!lua_isnil(L,-1))&&lua_toboolean(L, -1)){
800+
chunk_name = source;
801+
}
802+
lua_pop(L, 1);
803+
804+
if (luaL_loadbuffer(L, source, strlen(source), chunk_name))
767805
luaP_error(L, "compile");
768806
lua_remove(L, -2); /* source */
769807
if (lua_pcall(L, 0, 1, 0)) luaP_error(L, "call");
@@ -1416,7 +1454,17 @@ Datum luaP_inlinehandler (lua_State *L, const char *source) {
14161454
prev = rtds_set_current(funcxt);
14171455
PG_TRY();
14181456
{
1419-
if (luaL_loadbuffer(L, source, strlen(source), PLLUA_CHUNKNAME))
1457+
const char *chunk_name = PLLUA_CHUNKNAME;
1458+
1459+
lua_pushlightuserdata(L, p_remote_debug_info);
1460+
lua_rawget(L, LUA_REGISTRYINDEX);
1461+
1462+
if ((!lua_isnil(L,-1))&&lua_toboolean(L, -1)){
1463+
chunk_name = source;
1464+
}
1465+
lua_pop(L, 1);
1466+
1467+
if (luaL_loadbuffer(L, source, strlen(source), chunk_name))
14201468
luaP_error(L, "compile");
14211469
if (lua_pcall(L, 0, 0, 0)) {
14221470
funcxt = rtds_unref(funcxt);

Diff for: plluacommon.h

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ MemoryContext luaP_getmemctxt (lua_State *L);
7979

8080
lua_State *pllua_getmaster (lua_State *L);
8181

82+
#define lua_swap(L) lua_insert(L, -2)
83+
8284
#define MTOLUA(state) {MemoryContext ___mcxt,___m;\
8385
___mcxt = luaP_getmemctxt(state); \
8486
___m = MemoryContextSwitchTo(___mcxt)

0 commit comments

Comments
 (0)