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

JuliaLang / julia / 1371

11 Dec 2025 01:05PM UTC coverage: 69.814% (+0.02%) from 69.79%
1371

push

buildkite

web-flow
set VERSION to 1.12.3 (#60346)

53418 of 76515 relevant lines covered (69.81%)

11397578.99 hits per line

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

76.1
/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

11
using Core: CodeInfo, MethodInstance, CodeInstance
12
using Base.IRShow
13

14
export StackTrace, StackFrame, stacktrace
15

16
"""
17
    StackFrame
18

19
Stack information representing execution context, with the following fields:
20

21
- `func::Symbol`
22

23
  The name of the function containing the execution context.
24

25
- `linfo::Union{Method, Core.MethodInstance, Core.CodeInstance, Core.CodeInfo, Nothing}`
26

27
  The Method, MethodInstance, CodeInstance, or CodeInfo containing the execution context (if it could be found), \
28
     or nothing (for example, if the inlining was a result of macro expansion).
29

30
- `file::Symbol`
31

32
  The path to the file containing the execution context.
33

34
- `line::Int`
35

36
  The line number in the file containing the execution context.
37

38
- `from_c::Bool`
39

40
  True if the code is from C.
41

42
- `inlined::Bool`
43

44
  True if the code is from an inlined frame.
45

46
- `pointer::UInt64`
47

48
  Representation of the pointer to the execution context as returned by `backtrace`.
49

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

69
StackFrame(func, file, line) = StackFrame(Symbol(func), Symbol(file), line,
12✔
70
                                          nothing, false, false, 0)
71

72
"""
73
    StackTrace
74

75
An alias for `Vector{StackFrame}` provided for convenience; returned by calls to
76
`stacktrace`.
77
"""
78
const StackTrace = Vector{StackFrame}
79

80
const empty_sym = Symbol("")
81
const UNKNOWN = StackFrame(empty_sym, empty_sym, -1, nothing, true, false, 0) # === lookup(C_NULL)
82

83

84
#=
85
If the StackFrame has function and line information, we consider two of them the same if
86
they share the same function/line information.
87
=#
88
function ==(a::StackFrame, b::StackFrame)
89
    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
52,377✔
90
end
91

92
function hash(frame::StackFrame, h::UInt)
93
    h += 0xf4fbda67fe20ce88 % UInt
×
94
    h = hash(frame.line, h)
96,089✔
95
    h = hash(frame.file, h)
96,089✔
96
    h = hash(frame.func, h)
96,089✔
97
    h = hash(frame.from_c, h)
96,089✔
98
    h = hash(frame.inlined, h)
96,089✔
99
    return h
×
100
end
101

102
"""
103
    lookup(pointer::Ptr{Cvoid}) -> Vector{StackFrame}
104

105
Given a pointer to an execution context (usually generated by a call to `backtrace`), looks
106
up stack frame context information. Returns an array of frame information for all functions
107
inlined at that point, innermost function first.
108
"""
109
Base.@constprop :none function lookup(pointer::Ptr{Cvoid})
319✔
110
    infos = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint), pointer, false)::Core.SimpleVector
319✔
111
    pointer = convert(UInt64, pointer)
319✔
112
    isempty(infos) && return [StackFrame(empty_sym, empty_sym, -1, nothing, true, false, pointer)] # this is equal to UNKNOWN
319✔
113
    res = Vector{StackFrame}(undef, length(infos))
319✔
114
    for i in 1:length(infos)
319✔
115
        info = infos[i]::Core.SimpleVector
430✔
116
        @assert length(info) == 6 "corrupt return from jl_lookup_code_address"
430✔
117
        func = info[1]::Symbol
430✔
118
        file = info[2]::Symbol
430✔
119
        linenum = info[3]::Int
430✔
120
        linfo = info[4]
430✔
121
        res[i] = StackFrame(func, file, linenum, linfo, info[5]::Bool, info[6]::Bool, pointer)
430✔
122
    end
541✔
123
    return res
319✔
124
end
125

126
const top_level_scope_sym = Symbol("top-level scope")
127

128
function lookup(ip::Base.InterpreterIP)
13✔
129
    code = ip.code
13✔
130
    if code === nothing
13✔
131
        # interpreted top-level expression with no CodeInfo
132
        return [StackFrame(top_level_scope_sym, empty_sym, 0, nothing, false, false, 0)]
×
133
    end
134
    # prepare approximate code info
135
    if code isa MethodInstance && (meth = code.def; meth isa Method)
13✔
136
        func = meth.name
×
137
        file = meth.file
×
138
        line = meth.line
×
139
        codeinfo = meth.source
×
140
    else
141
        func = top_level_scope_sym
13✔
142
        file = empty_sym
11✔
143
        line = Int32(0)
11✔
144
        if code isa Core.CodeInstance
13✔
145
            codeinfo = code.inferred::CodeInfo
×
146
            def = code.def
×
147
            if isa(def, Core.ABIOverride)
×
148
                def = def.def
×
149
            end
150
            if isa(def, MethodInstance) && isa(def.def, Method)
×
151
                meth = def.def
×
152
                func = meth.name
×
153
                file = meth.file
×
154
                line = meth.line
×
155
            end
156
        else
157
            codeinfo = code::CodeInfo
13✔
158
        end
159
    end
160
    def = (code isa CodeInfo ? StackTraces : code) # Module just used as a token for top-level code
13✔
161
    pc::Int = max(ip.stmt + 1, 0) # n.b. ip.stmt is 0-indexed
13✔
162
    scopes = IRShow.LineInfoNode[]
13✔
163
    IRShow.append_scopes!(scopes, pc, codeinfo.debuginfo, def)
13✔
164
    if isempty(scopes)
13✔
165
        return [StackFrame(func, file, line, code, false, false, 0)]
×
166
    end
167
    inlined = false
13✔
168
    scopes = map(scopes) do lno
13✔
169
        if inlined
37✔
170
            def = lno.method
24✔
171
            def isa Union{Method,Core.CodeInstance,MethodInstance} || (def = nothing)
48✔
172
        else
173
            def = codeinfo
13✔
174
        end
175
        sf = StackFrame(IRShow.normalize_method_name(lno.method), lno.file, lno.line, def, false, inlined, 0)
37✔
176
        inlined = true
37✔
177
        return sf
37✔
178
    end
179
    return scopes
13✔
180
end
181

182
"""
183
    stacktrace([trace::Vector{Ptr{Cvoid}},] [c_funcs::Bool=false]) -> StackTrace
184

185
Return a stack trace in the form of a vector of `StackFrame`s. (By default stacktrace
186
doesn't return C functions, but this can be enabled.) When called without specifying a
187
trace, `stacktrace` first calls `backtrace`.
188
"""
189
Base.@constprop :none function stacktrace(trace::Vector{<:Union{Base.InterpreterIP,Ptr{Cvoid}}}, c_funcs::Bool=false)
65✔
190
    stack = StackTrace()
227✔
191
    for ip in trace
11✔
192
        for frame in lookup(ip)
248✔
193
            # Skip frames that come from C calls.
194
            if c_funcs || !frame.from_c
387✔
195
                push!(stack, frame)
309✔
196
            end
197
        end
341✔
198
    end
248✔
199
    return stack
11✔
200
end
201

202
Base.@constprop :none function stacktrace(c_funcs::Bool=false)
203
    stack = stacktrace(backtrace(), c_funcs)
12✔
204
    # Remove frame for this function (and any functions called by this function).
205
    remove_frames!(stack, :stacktrace)
206
    # also remove all of the non-Julia functions that led up to this point (if that list is non-empty)
207
    c_funcs && deleteat!(stack, 1:(something(findfirst(frame -> !frame.from_c, stack), 1) - 1))
208
    return stack
209
end
210

211
"""
212
    remove_frames!(stack::StackTrace, name::Symbol)
213

214
Takes a `StackTrace` (a vector of `StackFrames`) and a function name (a `Symbol`) and
215
removes the `StackFrame` specified by the function name from the `StackTrace` (also removing
216
all frames above the specified function). Primarily used to remove `StackTraces` functions
217
from the `StackTrace` prior to returning it.
218
"""
219
function remove_frames!(stack::StackTrace, name::Symbol)
×
220
    deleteat!(stack, 1:something(findlast(frame -> frame.func == name, stack), 0))
×
221
    return stack
×
222
end
223

224
function remove_frames!(stack::StackTrace, names::Vector{Symbol})
×
225
    deleteat!(stack, 1:something(findlast(frame -> frame.func in names, stack), 0))
×
226
    return stack
×
227
end
228

229
"""
230
    remove_frames!(stack::StackTrace, m::Module)
231

232
Return the `StackTrace` with all `StackFrame`s from the provided `Module` removed.
233
"""
234
function remove_frames!(stack::StackTrace, m::Module)
235
    filter!(f -> !from(f, m), stack)
2✔
236
    return stack
×
237
end
238

239
is_top_level_frame(f::StackFrame) = f.linfo isa CodeInfo || (f.linfo === nothing && f.func === top_level_scope_sym)
×
240

241
function frame_method_or_module(lkup::StackFrame)
242
    code = lkup.linfo
1,550✔
243
    code isa Method && return code
1,550✔
244
    code isa Module && return code
×
245
    mi = frame_mi(lkup)
1,550✔
246
    mi isa MethodInstance || return nothing
1,550✔
247
    return mi.def
1,550✔
248
end
249

250
function frame_mi(lkup::StackFrame)
251
    code = lkup.linfo
3,052✔
252
    code isa Core.CodeInstance && (code = code.def)
3,052✔
253
    code isa Core.ABIOverride && (code = code.def)
3,052✔
254
    code isa MethodInstance || return nothing
3,052✔
255
    return code
3,052✔
256
end
257

258
function show_spec_linfo(io::IO, frame::StackFrame)
3,520✔
259
    linfo = frame.linfo
3,520✔
260
    if linfo === nothing
3,520✔
261
        if frame.func === empty_sym
1,773✔
262
            print(io, "ip:0x", string(frame.pointer, base=16))
1✔
263
        elseif frame.func === top_level_scope_sym
1,772✔
264
            print(io, "top-level scope")
60✔
265
        else
266
            Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
1,712✔
267
        end
268
    elseif linfo isa CodeInfo
1,747✔
269
        print(io, "top-level scope")
245✔
270
    elseif linfo isa Module
1,502✔
271
        Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
×
272
    else
273
        if linfo isa Union{MethodInstance, CodeInstance}
1,502✔
274
            def = frame_method_or_module(frame)
3,004✔
275
            if def isa Module
1,502✔
276
                Base.show_mi(io, linfo, #=from_stackframe=#true)
×
277
            else
278
                show_spec_sig(io, def, frame_mi(frame).specTypes)
1,502✔
279
            end
280
        else
281
            m = linfo::Method
×
282
            show_spec_sig(io, m, m.sig)
×
283
        end
284
    end
285
end
286

287
function show_spec_sig(io::IO, m::Method, @nospecialize(sig::Type))
1,502✔
288
    if get(io, :limit, :false)::Bool
3,645✔
289
        if !haskey(io, :displaysize)
2,253✔
290
            io = IOContext(io, :displaysize => displaysize(io))
445✔
291
        end
292
    end
293
    argnames = Base.method_argnames(m)
3,004✔
294
    argnames = replace(argnames, :var"#unused#" => :var"")
1,502✔
295
    if m.nkw > 0
1,502✔
296
        # rearrange call kw_impl(kw_args..., func, pos_args...) to func(pos_args...; kw_args)
297
        kwarg_types = Any[ fieldtype(sig, i) for i = 2:(1+m.nkw) ]
254✔
298
        uw = Base.unwrap_unionall(sig)::DataType
182✔
299
        pos_sig = Base.rewrap_unionall(Tuple{uw.parameters[(m.nkw+2):end]...}, sig)
182✔
300
        kwnames = argnames[2:(m.nkw+1)]
364✔
301
        for i = 1:length(kwnames)
182✔
302
            str = string(kwnames[i])::String
254✔
303
            if endswith(str, "...")
254✔
304
                kwnames[i] = Symbol(str[1:end-3])
×
305
            end
306
        end
326✔
307
        Base.show_tuple_as_call(io, m.name, pos_sig;
182✔
308
                                demangle=true,
309
                                kwargs=zip(kwnames, kwarg_types),
310
                                argnames=argnames[m.nkw+2:end])
311
    else
312
        Base.show_tuple_as_call(io, m.name, sig; demangle=true, argnames)
1,320✔
313
    end
314
end
315

316
function show(io::IO, frame::StackFrame)
378✔
317
    show_spec_linfo(io, frame)
378✔
318
    if frame.file !== empty_sym
378✔
319
        file_info = basename(string(frame.file))
365✔
320
        print(io, " at ")
365✔
321
        print(io, file_info, ":")
365✔
322
        if frame.line >= 0
365✔
323
            print(io, frame.line)
356✔
324
        else
325
            print(io, "?")
9✔
326
        end
327
    end
328
    if frame.inlined
378✔
329
        print(io, " [inlined]")
105✔
330
    end
331
end
332

333
function Base.parentmodule(frame::StackFrame)
334
    linfo = frame.linfo
6,358✔
335
    if linfo isa CodeInstance
6,358✔
336
        linfo = linfo.def
1,739✔
337
        if isa(linfo, Core.ABIOverride)
1,739✔
338
            linfo = linfo.def
×
339
        end
340
    end
341
    if linfo isa MethodInstance
6,358✔
342
        def = linfo.def
2,760✔
343
        if def isa Module
2,760✔
344
            return def
×
345
        else
346
            return (def::Method).module
2,760✔
347
        end
348
    elseif linfo isa Method
3,598✔
349
        return linfo.module
×
350
    elseif linfo isa Module
3,598✔
351
        return linfo
×
352
    else
353
        # The module is not always available (common reasons include
354
        # frames arising from the interpreter)
355
        nothing
×
356
    end
357
end
358

359
"""
360
    from(frame::StackFrame, filter_mod::Module) -> Bool
361

362
Return whether the `frame` is from the provided `Module`
363
"""
364
function from(frame::StackFrame, m::Module)
×
365
    return parentmodule(frame) === m
×
366
end
367

368
end  # module StackTraces
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