Permalink
Browse files

Merge pull request #10 from urzds/fix/top-of-stack-may-be-nil

Prevent a nil-value error when events above the recorded stack are encountered
  • Loading branch information...
2 parents 6150cfd + 2970d61 commit d9d8918a6e4272a05d09c1dccc0d2771576ad1b3 @geoffleyland committed Nov 30, 2015
Showing with 28 additions and 23 deletions.
  1. +28 −23 lua/luatrace/profile.lua
@@ -172,7 +172,7 @@ local function play_return(callee, caller)
time = time + caller.line_child_time_at_start - line.child_time
line.child_time = line.child_time + time
end
-
+
local func = callee.func
time = total_time - callee.func_start_time
time = time + callee.func_self_time_at_start - func.self_time
@@ -266,30 +266,36 @@ function profile.record(a, b, c, d)
local top = get_top()
- if top.func.line_defined > 0 and
- (line_number < top.func.line_defined or line_number > top.func.last_line_defined) then
- -- luajit sometimes forgets to tell us about returns at all, so guess that
- -- there might have been one and try again
- local above_top = stack[stack.top-1]
- if not above_top or
- (above_top.func.line_defined > 0 and
- (line_number < above_top.func.line_defined or line_number > above_top.func.last_line_defined)) then
- error_count = error_count + 1
- io.stderr:write(("ERROR (%4d, line %7d): counted execution of %g microseconds at line %d of a function defined at %s:%d-%d\n"):
- format(error_count, trace_count, time, line_number, top.func.source_file.filename, top.func.line_defined, top.func.last_line_defined))
- else
- do_return()
- top = get_top()
+ if not top then
+ error_count = error_count + 1
+ io.stderr:write(("ERROR (%4d, line %7d): recorded execution of %g microseconds at line %d in an unknown stack frame at height %d\n")
+ :format(error_count, trace_count, time, line_number, stack.top))
+ else
+ if top.func.line_defined > 0 and
+ (line_number < top.func.line_defined or line_number > top.func.last_line_defined) then
+ -- luajit sometimes forgets to tell us about returns at all, so guess that
+ -- there might have been one and try again
+ local above_top = stack[stack.top-1]
+ if not above_top or
+ (above_top.func.line_defined > 0 and
+ (line_number < above_top.func.line_defined or line_number > above_top.func.last_line_defined)) then
+ error_count = error_count + 1
+ io.stderr:write(("ERROR (%4d, line %7d): counted execution of %g microseconds at line %d of a function defined at %s:%d-%d\n"):
+ format(error_count, trace_count, time, line_number, top.func.source_file.filename, top.func.line_defined, top.func.last_line_defined))
+ else
+ do_return()
+ top = get_top()
+ end
end
- end
- local line = get_line(line_number)
- if top.current_line ~= line then
- line.hits = line.hits + 1
+ local line = get_line(line_number)
+ if top.current_line ~= line then
+ line.hits = line.hits + 1
+ end
+ line.self_time = line.self_time + time
+ top.func.self_time = top.func.self_time + time
+ top.current_line = line
end
- line.self_time = line.self_time + time
- top.func.self_time = top.func.self_time + time
- top.current_line = line
end
end
@@ -491,4 +497,3 @@ return profile
-- EOF -------------------------------------------------------------------------
-

0 comments on commit d9d8918

Please sign in to comment.