• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37423

pending completion
#37423

push

local

web-flow
avoid generating native code if only output ji file (#48431)

81577 of 87743 relevant lines covered (92.97%)

17744801.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.11
/base/stacktraces.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
Tools for collecting and manipulating stack traces. Mainly used for building errors.
5
"""
6
module StackTraces
7

8

9
import Base: hash, ==, show
10
import Core: CodeInfo, MethodInstance
11

12
export StackTrace, StackFrame, stacktrace
13

14
"""
15
    StackFrame
16

17
Stack information representing execution context, with the following fields:
18

19
- `func::Symbol`
20

21
  The name of the function containing the execution context.
22

23
- `linfo::Union{Core.MethodInstance, CodeInfo, Nothing}`
24

25
  The MethodInstance containing the execution context (if it could be found).
26

27
- `file::Symbol`
28

29
  The path to the file containing the execution context.
30

31
- `line::Int`
32

33
  The line number in the file containing the execution context.
34

35
- `from_c::Bool`
36

37
  True if the code is from C.
38

39
- `inlined::Bool`
40

41
  True if the code is from an inlined frame.
42

43
- `pointer::UInt64`
44

45
  Representation of the pointer to the execution context as returned by `backtrace`.
46

47
"""
48
struct StackFrame # this type should be kept platform-agnostic so that profiles can be dumped on one machine and read on another
49
    "the name of the function containing the execution context"
34,089✔
50
    func::Symbol
51
    "the path to the file containing the execution context"
52
    file::Symbol
53
    "the line number in the file containing the execution context"
54
    line::Int
55
    "the MethodInstance or CodeInfo containing the execution context (if it could be found)"
56
    linfo::Union{MethodInstance, CodeInfo, Nothing}
57
    "true if the code is from C"
58
    from_c::Bool
59
    "true if the code is from an inlined frame"
60
    inlined::Bool
61
    "representation of the pointer to the execution context as returned by `backtrace`"
62
    pointer::UInt64  # Large enough to be read losslessly on 32- and 64-bit machines.
63
end
64

65
StackFrame(func, file, line) = StackFrame(Symbol(func), Symbol(file), line,
66
                                          nothing, false, false, 0)
67

68
"""
69
    StackTrace
70

71
An alias for `Vector{StackFrame}` provided for convenience; returned by calls to
72
`stacktrace`.
73
"""
74
const StackTrace = Vector{StackFrame}
75

76
const empty_sym = Symbol("")
77
const UNKNOWN = StackFrame(empty_sym, empty_sym, -1, nothing, true, false, 0) # === lookup(C_NULL)
78

79

80
#=
81
If the StackFrame has function and line information, we consider two of them the same if
82
they share the same function/line information.
83
=#
84
function ==(a::StackFrame, b::StackFrame)
829✔
85
    return a.line == b.line && a.from_c == b.from_c && a.func == b.func && a.file == b.file && a.inlined == b.inlined # excluding linfo and pointer
42,341✔
86
end
87

88
function hash(frame::StackFrame, h::UInt)
1,249,477✔
89
    h += 0xf4fbda67fe20ce88 % UInt
1,249,477✔
90
    h = hash(frame.line, h)
1,249,477✔
91
    h = hash(frame.file, h)
1,249,477✔
92
    h = hash(frame.func, h)
1,249,477✔
93
    h = hash(frame.from_c, h)
1,249,477✔
94
    h = hash(frame.inlined, h)
1,249,477✔
95
    return h
1,249,477✔
96
end
97

98

99
"""
100
    lookup(pointer::Ptr{Cvoid}) -> Vector{StackFrame}
101

102
Given a pointer to an execution context (usually generated by a call to `backtrace`), looks
103
up stack frame context information. Returns an array of frame information for all functions
104
inlined at that point, innermost function first.
105
"""
106
Base.@constprop :none function lookup(pointer::Ptr{Cvoid})
20,544✔
107
    infos = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint), pointer, false)::Core.SimpleVector
20,544✔
108
    pointer = convert(UInt64, pointer)
20,544✔
109
    isempty(infos) && return [StackFrame(empty_sym, empty_sym, -1, nothing, true, false, pointer)] # this is equal to UNKNOWN
20,544✔
110
    res = Vector{StackFrame}(undef, length(infos))
20,544✔
111
    for i in 1:length(infos)
41,088✔
112
        info = infos[i]::Core.SimpleVector
32,582✔
113
        @assert(length(info) == 6)
32,582✔
114
        res[i] = StackFrame(info[1]::Symbol, info[2]::Symbol, info[3]::Int, info[4], info[5]::Bool, info[6]::Bool, pointer)
32,582✔
115
    end
44,620✔
116
    return res
20,544✔
117
end
118

119
const top_level_scope_sym = Symbol("top-level scope")
120

121
function lookup(ip::Union{Base.InterpreterIP,Core.Compiler.InterpreterIP})
128✔
122
    code = ip.code
128✔
123
    if code === nothing
128✔
124
        # interpreted top-level expression with no CodeInfo
125
        return [StackFrame(top_level_scope_sym, empty_sym, 0, nothing, false, false, 0)]
1✔
126
    end
127
    codeinfo = (code isa MethodInstance ? code.uninferred : code)::CodeInfo
254✔
128
    # prepare approximate code info
129
    if code isa MethodInstance && (meth = code.def; meth isa Method)
127✔
130
        func = meth.name
×
131
        file = meth.file
×
132
        line = meth.line
×
133
    else
134
        func = top_level_scope_sym
×
135
        file = empty_sym
×
136
        line = Int32(0)
×
137
    end
138
    i = max(ip.stmt+1, 1)  # ip.stmt is 0-indexed
127✔
139
    if i > length(codeinfo.codelocs) || codeinfo.codelocs[i] == 0
254✔
140
        return [StackFrame(func, file, line, code, false, false, 0)]
×
141
    end
142
    lineinfo = codeinfo.linetable[codeinfo.codelocs[i]]::Core.LineInfoNode
127✔
143
    scopes = StackFrame[]
127✔
144
    while true
286✔
145
        inlined = lineinfo.inlined_at != 0
286✔
146
        push!(scopes, StackFrame(Base.IRShow.method_name(lineinfo)::Symbol, lineinfo.file, lineinfo.line, inlined ? nothing : code, false, inlined, 0))
413✔
147
        inlined || break
286✔
148
        lineinfo = codeinfo.linetable[lineinfo.inlined_at]::Core.LineInfoNode
159✔
149
    end
159✔
150
    return scopes
127✔
151
end
152

153
"""
154
    stacktrace([trace::Vector{Ptr{Cvoid}},] [c_funcs::Bool=false]) -> StackTrace
155

156
Return a stack trace in the form of a vector of `StackFrame`s. (By default stacktrace
157
doesn't return C functions, but this can be enabled.) When called without specifying a
158
trace, `stacktrace` first calls `backtrace`.
159
"""
160
Base.@constprop :none function stacktrace(trace::Vector{<:Union{Base.InterpreterIP,Core.Compiler.InterpreterIP,Ptr{Cvoid}}}, c_funcs::Bool=false)
50✔
161
    stack = StackTrace()
84✔
162
    for ip in trace
44✔
163
        for frame in lookup(ip)
1,170✔
164
            # Skip frames that come from C calls.
165
            if c_funcs || !frame.from_c
3,195✔
166
                push!(stack, frame)
582✔
167
            end
168
        end
2,827✔
169
    end
1,212✔
170
    return stack
43✔
171
end
172

173
Base.@constprop :none function stacktrace(c_funcs::Bool=false)
174
    stack = stacktrace(backtrace(), c_funcs)
175
    # Remove frame for this function (and any functions called by this function).
176
    remove_frames!(stack, :stacktrace)
177
    # also remove all of the non-Julia functions that led up to this point (if that list is non-empty)
178
    c_funcs && deleteat!(stack, 1:(something(findfirst(frame -> !frame.from_c, stack), 1) - 1))
179
    return stack
180
end
181

182
"""
183
    remove_frames!(stack::StackTrace, name::Symbol)
184

185
Takes a `StackTrace` (a vector of `StackFrames`) and a function name (a `Symbol`) and
186
removes the `StackFrame` specified by the function name from the `StackTrace` (also removing
187
all frames above the specified function). Primarily used to remove `StackTraces` functions
188
from the `StackTrace` prior to returning it.
189
"""
190
function remove_frames!(stack::StackTrace, name::Symbol)
191
    deleteat!(stack, 1:something(findlast(frame -> frame.func == name, stack), 0))
192
    return stack
193
end
194

195
function remove_frames!(stack::StackTrace, names::Vector{Symbol})
196
    deleteat!(stack, 1:something(findlast(frame -> frame.func in names, stack), 0))
197
    return stack
198
end
199

200
"""
201
    remove_frames!(stack::StackTrace, m::Module)
202

203
Return the `StackTrace` with all `StackFrame`s from the provided `Module` removed.
204
"""
205
function remove_frames!(stack::StackTrace, m::Module)
206
    filter!(f -> !from(f, m), stack)
207
    return stack
208
end
209

210
is_top_level_frame(f::StackFrame) = f.linfo isa CodeInfo || (f.linfo === nothing && f.func === top_level_scope_sym)
11✔
211

212
function show_spec_linfo(io::IO, frame::StackFrame)
1,737✔
213
    linfo = frame.linfo
1,737✔
214
    if linfo === nothing
1,737✔
215
        if frame.func === empty_sym
601✔
216
            print(io, "ip:0x", string(frame.pointer, base=16))
×
217
        elseif frame.func === top_level_scope_sym
601✔
218
            print(io, "top-level scope")
17✔
219
        else
220
            Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
584✔
221
        end
222
    elseif linfo isa MethodInstance
1,136✔
223
        def = linfo.def
1,058✔
224
        if isa(def, Method)
1,058✔
225
            sig = linfo.specTypes
1,058✔
226
            argnames = Base.method_argnames(def)
2,116✔
227
            if def.nkw > 0
1,058✔
228
                # rearrange call kw_impl(kw_args..., func, pos_args...) to func(pos_args...)
229
                kwarg_types = Any[ fieldtype(sig, i) for i = 2:(1+def.nkw) ]
149✔
230
                uw = Base.unwrap_unionall(sig)::DataType
134✔
231
                pos_sig = Base.rewrap_unionall(Tuple{uw.parameters[(def.nkw+2):end]...}, sig)
134✔
232
                kwnames = argnames[2:(def.nkw+1)]
134✔
233
                for i = 1:length(kwnames)
268✔
234
                    str = string(kwnames[i])::String
149✔
235
                    if endswith(str, "...")
149✔
236
                        kwnames[i] = Symbol(str[1:end-3])
×
237
                    end
238
                end
164✔
239
                Base.show_tuple_as_call(io, def.name, pos_sig;
134✔
240
                                        demangle=true,
241
                                        kwargs=zip(kwnames, kwarg_types),
242
                                        argnames=argnames[def.nkw+2:end])
243
            else
244
                Base.show_tuple_as_call(io, def.name, sig; demangle=true, argnames)
924✔
245
            end
246
        else
247
            Base.show_mi(io, linfo, true)
×
248
        end
249
    elseif linfo isa CodeInfo
70✔
250
        print(io, "top-level scope")
78✔
251
    end
252
end
253

254
function show(io::IO, frame::StackFrame)
119✔
255
    show_spec_linfo(io, frame)
119✔
256
    if frame.file !== empty_sym
119✔
257
        file_info = basename(string(frame.file))
110✔
258
        print(io, " at ")
110✔
259
        print(io, file_info, ":")
110✔
260
        if frame.line >= 0
110✔
261
            print(io, frame.line)
110✔
262
        else
263
            print(io, "?")
×
264
        end
265
    end
266
    if frame.inlined
119✔
267
        print(io, " [inlined]")
45✔
268
    end
269
end
270

271
function Base.parentmodule(frame::StackFrame)
2,154✔
272
    linfo = frame.linfo
4,491✔
273
    if linfo isa MethodInstance
4,491✔
274
        def = linfo.def
2,425✔
275
        return def isa Module ? def : parentmodule(def::Method)
4,850✔
276
    else
277
        # The module is not always available (common reasons include inlined
278
        # frames and frames arising from the interpreter)
279
        nothing
2,066✔
280
    end
281
end
282

283
"""
284
    from(frame::StackFrame, filter_mod::Module) -> Bool
285

286
Return whether the `frame` is from the provided `Module`
287
"""
288
function from(frame::StackFrame, m::Module)
289
    return parentmodule(frame) === m
290
end
291

292
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc