Last active
February 12, 2021 22:40
-
-
Save cscherrer/1f792f1cbd4a46b74aa52cf3b54d7bf0 to your computer and use it in GitHub Desktop.
Showing a callstack with IRTools
This file contains hidden or 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
| using IRTools: @dynamo, IR, xcall, arguments, insertafter!, recurse!, self | |
| mutable struct CallStack | |
| count::Int | |
| end | |
| inc!(d::CallStack) = d.count += 1 | |
| dec!(d::CallStack) = d.count -= 1 | |
| function hook0(callstack::CallStack, f, args...) | |
| m = parentmodule(f, typeof.(args)) | |
| print(" "^callstack.count) | |
| print(m,".",f, "(") | |
| join(stdout, args, ", ") | |
| println(")") | |
| end | |
| function hook1(callstack::CallStack, f, args...) | |
| # You could do more here if you like | |
| return | |
| end | |
| @dynamo function (c::CallStack)(m...) | |
| ir = IR(m...) | |
| ir == nothing && return | |
| recurse!(ir) | |
| pushfirst!(ir, xcall(inc!, self)) | |
| pushfirst!(ir, xcall(hook0, self, arguments(ir)...)) | |
| push!(ir, xcall(dec!, self)) | |
| push!(ir, xcall(hook1, self, arguments(ir)...)) | |
| return ir | |
| end | |
| showstack = CallStack(0) | |
| # EXAMPLE | |
| # julia> showstack(*, 2, 3.0) | |
| # Base.*(2, 3.0) | |
| # Base.promote(2, 3.0) | |
| # Base._promote(2, 3.0) | |
| # Base.promote_type(Int64, Float64) | |
| # Base.promote_rule(Int64, Float64) | |
| # Base.promote_rule(Float64, Int64) | |
| # Base.promote_result(Int64, Float64, Union{}, Float64) | |
| # Base.promote_type(Union{}, Float64) | |
| # Base.convert(Float64, 2) | |
| # Base.Float64(2) | |
| # Base.convert(Float64, 3.0) | |
| # Base.indexed_iterate((2.0, 3.0), 1) | |
| # Base.indexed_iterate((2.0, 3.0), 1, 1) | |
| # Base.+(1, 1) | |
| # Base.indexed_iterate((2.0, 3.0), 2, 2) | |
| # Base.+(2, 1) | |
| # Base.not_sametype((2, 3.0), (2.0, 3.0)) | |
| # 6.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment