-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements in pallene-debug script
- Loading branch information
1 parent
0040ae0
commit 378f9d6
Showing
2 changed files
with
36 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,50 @@ | ||
#!/usr/bin/env lua | ||
|
||
if #arg == 0 then | ||
io.stderr:write("pallene-debug: need a file to debug. Abort!\n") | ||
os.exit(1) | ||
end | ||
local argparse = require "argparse" | ||
|
||
-- File to run. | ||
local filename = arg[1] | ||
-- Remove the filename from the argument so that | ||
-- it is identical to the arguments list if we had run | ||
-- the file explicitly. | ||
table.remove(arg, 1) | ||
|
||
-- Try opening the file. | ||
local file, err = io.open(filename, "r") | ||
if not file then | ||
io.stderr:write("pallene-debug: could not open file: "..err.."\n") | ||
os.exit(1) | ||
local opts | ||
do | ||
local p = argparse("pallene-debug", "Pallene debugger script for call-stack backtrace") | ||
|
||
p:argument("lua_script", "Lua file to debug with Pallene module") | ||
p:argument("args", "Arguments passed to Lua script") :args("*") | ||
|
||
opts = p:parse() | ||
end | ||
|
||
-- Try reading from the file. | ||
local content = file:read("*all") | ||
if not content then | ||
io.stderr:write("pallene-debug: could not read file '"..filename.."'. Abort!\n") | ||
local fn, err = loadfile(opts.lua_script) | ||
|
||
-- Was loading and parsing the file successful? | ||
if not fn then | ||
io.stderr:write("pallene-debug: "..err.."\n") | ||
os.exit(1) | ||
end | ||
-- We no longer need the file. | ||
file:close() | ||
|
||
-- Silently run the code, loading the Pallene/Pallene Tracer compatible | ||
-- modules so that we can figure out whether we have Pallene Tracer or not. | ||
local _, _ = pcall(load(content)) | ||
-- Supress I/O. | ||
local original_print = print | ||
local original_io_write = io.write | ||
|
||
print = function() end | ||
io.write = function() end | ||
|
||
-- Run the script initially so that we can figure out whether | ||
-- we have Pallene modules that is compiled with `--use-traceback` flag. | ||
local _, _ = pcall(fn) | ||
|
||
-- Check whether we have Pallene Tracer enabled. | ||
-- Restore I/O. | ||
print = original_print | ||
io.write = original_io_write | ||
|
||
-- We need the traceback fn. | ||
if not pallene_tracer_debug_traceback then | ||
io.stderr:write("pallene-debug: could not find debug traceback fn: Make sure you include Pallene modules and compile them with `--use-traceback` flag.\n") | ||
io.stderr:write("pallene-debug: could not find debug traceback fn: Make sure you include Pallene modules which are compiled with `--use-traceback` flag.\n") | ||
os.exit(1) | ||
end | ||
|
||
-- Inject the wrapper code. | ||
content = [[ | ||
function __pallene_debug_wrapper() | ||
]]..content..[[ | ||
-- Add a wrapper function. | ||
local function wrapper() | ||
fn(table.unpack(opts.args)) | ||
end | ||
|
||
xpcall(__pallene_debug_wrapper, pallene_tracer_debug_traceback) | ||
]] | ||
|
||
load(content, filename)() | ||
-- Moment of truth. | ||
xpcall(wrapper, pallene_tracer_debug_traceback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters