Skip to content

Commit

Permalink
Merge pull request #614 from pallene-lang/stack-trace-adaption
Browse files Browse the repository at this point in the history
Adaptation of `pallene-tracer` in Pallene
  • Loading branch information
singul4ri7y authored Aug 8, 2024
2 parents 46881a9 + d1617da commit 516b397
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 459 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ jobs:
make
sudo make install
- name: Install Pallene Tracer
run: |
git clone https://github.com/pallene-lang/pallene-tracer
cd pallene-tracer
sudo make install
luarocks --local make
- name: Build
run: luarocks --local make

Expand All @@ -87,3 +94,4 @@ jobs:
run: |
eval "$(luarocks path)"
busted -o gtest -v ./spec
6 changes: 6 additions & 0 deletions pallene-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ dependencies = {
"lua == 5.4",
"lpeg >= 1.0",
"argparse >= 0.7.0",
"pallene-tracer >= 0.5.0a"
}
external_dependencies = {
PTRACER = {
header = "ptracer.h",
}
}
build = {
type = "builtin",
Expand Down
2 changes: 1 addition & 1 deletion run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FLAGS=(--verbose --no-keep-going)

# To speed things up, we tell the C compiler to skip optimizations. (It's OK, the CI still uses -O2)
# Also, add some compiler flags to verify standard compliance.
export CFLAGS='-O0 -std=c99 -Wall -Werror -Wundef -Wpedantic -Wno-unused'
export CFLAGS='-O0 -std=c99 -Wall -Werror -Wundef -Wno-unused'

if [ "$#" -eq 0 ]; then
if command -v parallel >/dev/null; then
Expand Down
3 changes: 3 additions & 0 deletions src/pallene/c_compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local c_compiler = {}

local CC = os.getenv("CC") or "cc"
local CFLAGS = os.getenv("CFLAGS") or "-O2"
local PTLIBDIR = os.getenv("PTLIBDIR") or "/usr/local/lib"

local function get_uname()
local ok, err, uname = util.outputs_of_execute("uname -s")
Expand Down Expand Up @@ -61,6 +62,8 @@ function c_compiler.compile_o_to_so(in_filename, out_filename)
CFLAGS_SHARED,
"-o", util.shell_quote(out_filename),
util.shell_quote(in_filename),
"-lptracer",
"-Wl,-rpath="..PTLIBDIR,
})
end

Expand Down
79 changes: 28 additions & 51 deletions src/pallene/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ end
-- these cases instead of invoking the metatable operations, which may impair program optimization
-- even if they are never called.
local function check_no_metatable(self, src, loc)
local setline = ""
if self.flags.use_traceback then
setline = string.format("PALLENE_SETLINE(%d);", loc.line)
end
local setline = string.format("PALLENE_SETLINE(%d);", loc.line)

return (util.render([[
if ($src->metatable) {
Expand Down Expand Up @@ -254,10 +251,7 @@ function Coder:get_stack_slot(typ, dst, slot, loc, description_fmt, ...)
assert(not typ.is_upvalue_box)
local extra_args = table.pack(...)

local setline = ""
if self.flags.use_traceback then
setline = string.format("PALLENE_SETLINE(%d);", loc.line)
end
local setline = string.format("PALLENE_SETLINE(%d);", loc.line)

check_tag = util.render([[
if (l_unlikely(!$test)) {
Expand Down Expand Up @@ -461,18 +455,12 @@ function Coder:pallene_entry_point_definition(f_id)
local max_frame_size = self.gc[func].max_frame_size
local slots_needed = max_frame_size + self.max_lua_call_stack_usage[func]

local setline = ""
local frameexit = ""
if self.flags.use_traceback then
table.insert(prologue, util.render([[
PALLENE_C_FRAMEENTER(L, "$name");
]], {
name = func.name
}));

setline = string.format("PALLENE_SETLINE(%d);", func.loc and func.loc.line or 0)
frameexit = "PALLENE_FRAMEEXIT();"
end
table.insert(prologue, util.render([[
PALLENE_C_FRAMEENTER("$name");
]], {
name = func.name
}));
local setline = string.format("PALLENE_SETLINE(%d);", func.loc and func.loc.line or 0)

if slots_needed > 0 then
table.insert(prologue, util.render([[
Expand Down Expand Up @@ -527,7 +515,7 @@ function Coder:pallene_entry_point_definition(f_id)
${prologue}
/**/
${body}
${frameexit}
PALLENE_FRAMEEXIT();
${ret_mult}
${ret_stat}
}
Expand All @@ -537,7 +525,6 @@ function Coder:pallene_entry_point_definition(f_id)
prologue = table.concat(prologue, "\n"),
body = body,
ret_mult = ret_mult,
frameexit = frameexit,
ret_stat = ret_stat,
}))
end
Expand Down Expand Up @@ -615,22 +602,18 @@ function Coder:lua_entry_point_definition(f_id)
Udata *K = uvalue(&func->upvalue[0]);
]]

local frameenter = ""
local frameexit = ""
local nargs = #arg_types
-- We will be having our call-stack finalizer object on top of our stack when debugging mode is
-- enabled
local cargs = nargs
if self.flags.use_traceback then
frameenter = util.render([[
PALLENE_LUA_FRAMEENTER(L, $fun_name);
]], {
fun_name = self:lua_entry_point_name(f_id),
})
-- It's as simple as popping the finalizer.
frameexit = "lua_pop(L, 1);"

-- We will be having our finalizer on top of our stack.
cargs = cargs + 1
end
local frameenter = util.render([[
PALLENE_LUA_FRAMEENTER($fun_name);
]], {
fun_name = self:lua_entry_point_name(f_id),
})

local arity_check = util.render([[
int nargs = lua_gettop(L);
Expand Down Expand Up @@ -694,7 +677,6 @@ function Coder:lua_entry_point_definition(f_id)
/**/
${ret_decls}
${call_pallene}
${lua_fexit}
${push_results}
return $nresults;
}
Expand All @@ -708,7 +690,6 @@ function Coder:lua_entry_point_definition(f_id)
ret_decls = table.concat(ret_decls, "\n"),
call_pallene = call_pallene,
push_results = table.concat(push_results, "\n"),
lua_fexit = frameexit,
nresults = C.integer(#ret_types)
}))
end
Expand All @@ -728,11 +709,9 @@ define_union("Constant", {

function Coder:init_upvalues()

-- If we are using tracebacks
if self.flags.use_traceback then
table.insert(self.constants, coder.Constant.DebugUserdata)
table.insert(self.constants, coder.Constant.DebugMetatable)
end
-- Debug traceback constants
table.insert(self.constants, coder.Constant.DebugUserdata)
table.insert(self.constants, coder.Constant.DebugMetatable)

-- Metatables
for _, typ in ipairs(self.module.record_types) do
Expand Down Expand Up @@ -1490,11 +1469,8 @@ gen_cmd["CallStatic"] = function(self, args)
end

table.insert(parts, self:update_stack_top(args.position))

if self.flags.use_traceback then
table.insert(parts, string.format("PALLENE_SETLINE(%d);\n",
args.func.loc and args.func.loc.line or 0))
end
table.insert(parts, string.format("PALLENE_SETLINE(%d);\n",
args.func.loc and args.func.loc.line or 0))

table.insert(parts, self:call_pallene_function(dsts, f_id, cclosure, xs, nil))
table.insert(parts, self:restorestack())
Expand Down Expand Up @@ -1531,12 +1507,9 @@ gen_cmd["CallDyn"] = function(self, args)
}))
end

local setline = ""
if self.flags.use_traceback then
setline = util.render([[ PALLENE_SETLINE($line); ]], {
line = C.integer(args.func.loc and args.func.loc.line or 0)
})
end
local setline = util.render([[ PALLENE_SETLINE($line); ]], {
line = C.integer(args.func.loc and args.func.loc.line or 0)
})

return util.render([[
${update_stack_top}
Expand Down Expand Up @@ -1808,6 +1781,10 @@ function Coder:generate_module_header()
table.insert(out, "/* This file was generated by the Pallene compiler. Do not edit by hand */")
table.insert(out, "")
table.insert(out, string.format("#define PALLENE_SOURCE_FILE %s", C.string(self.filename)))
if self.flags.use_traceback then
table.insert(out, "/* Enable Pallene Tracer debugging. */")
table.insert(out, "#define PT_DEBUG")
end
table.insert(out, "")

table.insert(out, "/* ------------------------ */")
Expand Down
2 changes: 1 addition & 1 deletion src/pallene/pallenec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ do
)

-- No Pallene tracebacks
p:flag("--use-traceback", "Use function traceback for debugging")
p:flag("--use-traceback", "Enable call-stack tracing")

p:option("-O", "Optimization level")
:args(1):convert(tonumber)
Expand Down
Loading

0 comments on commit 516b397

Please sign in to comment.