Skip to content

Instantly share code, notes, and snippets.

@justincormack
Created April 17, 2013 22:37
Show Gist options
  • Save justincormack/5408343 to your computer and use it in GitHub Desktop.
Save justincormack/5408343 to your computer and use it in GitHub Desktop.
How to get SIGINT to give a backtrace using ljsyscall
local S = require "syscall"
local t = S.t
local c = S.c
local ffi = require "ffi"
local ip
if ffi.arch == "x86" then ip = c.REG.EIP
elseif ffi.arch == "x64" then ip = c.REG.RIP
else error "unsupported architecture" end
local g = ffi.cast("void (*)(void)", function() error("sigint") end)
local f = t.sa_sigaction(function(s, info, ucontext)
ucontext.uc_mcontext.gregs[ip] = ffi.cast("intptr_t", ffi.cast("void *", g)) -- set instruction pointer to g
end)
assert(S.sigaction("int", {sigaction = f}))
-- example code to get interesting stack trace
function bb(x)
assert(S.kill(S.getpid(), "int"))
return x + 1
end
function aa(x)
local c = 2 * bb(x + 1)
print("not here")
return c
end
aa(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment