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

JuliaLang / julia / 1417

01 Sep 2025 06:55PM UTC coverage: 77.531% (+0.1%) from 77.391%
1417

push

buildkite

web-flow
Bump actions/checkout from 4.2.2 to 5.0.0 (#59453)

61684 of 79560 relevant lines covered (77.53%)

22635759.35 hits per line

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

78.66
/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"
19,011✔
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,
36✔
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
250,536✔
90
end
91

92
function hash(frame::StackFrame, h::UInt)
93
    h ⊻= 0xf4fbda67fe20ce88 % UInt
795✔
94
    h = hash(frame.line, h)
1,670,343✔
95
    h = hash(frame.file, h)
1,670,343✔
96
    h = hash(frame.func, h)
1,670,343✔
97
    h = hash(frame.from_c, h)
1,670,343✔
98
    h = hash(frame.inlined, h)
1,670,343✔
99
    return h
583✔
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})
7,391✔
110
    infos = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint), pointer, false)::Core.SimpleVector
7,391✔
111
    pointer = convert(UInt64, pointer)
7,391✔
112
    isempty(infos) && return [StackFrame(empty_sym, empty_sym, -1, nothing, true, false, pointer)] # this is equal to UNKNOWN
7,391✔
113
    res = Vector{StackFrame}(undef, length(infos))
7,391✔
114
    for i in 1:length(infos)
14,622✔
115
        info = infos[i]::Core.SimpleVector
9,830✔
116
        @assert length(info) == 6 "corrupt return from jl_lookup_code_address"
9,830✔
117
        func = info[1]::Symbol
9,830✔
118
        file = info[2]::Symbol
9,830✔
119
        linenum = info[3]::Int
9,830✔
120
        linfo = info[4]
9,830✔
121
        res[i] = StackFrame(func, file, linenum, linfo, info[5]::Bool, info[6]::Bool, pointer)
9,830✔
122
    end
12,269✔
123
    return res
7,391✔
124
end
125

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

128
function lookup(ip::Base.InterpreterIP)
424✔
129
    code = ip.code
424✔
130
    if code === nothing
424✔
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)
424✔
136
        func = meth.name
×
137
        file = meth.file
×
138
        line = meth.line
×
139
        codeinfo = meth.source
×
140
    else
141
        func = top_level_scope_sym
424✔
142
        file = empty_sym
424✔
143
        line = Int32(0)
424✔
144
        if code isa Core.CodeInstance
424✔
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)
×
151
                let meth = def.def
×
152
                    if isa(meth, Method)
×
153
                        func = meth.name
×
154
                        file = meth.file
×
155
                        line = meth.line
×
156
                    end
157
                end
158
            end
159
        else
160
            codeinfo = code::CodeInfo
424✔
161
        end
162
    end
163
    def = (code isa CodeInfo ? StackTraces : code) # Module just used as a token for top-level code
424✔
164
    pc::Int = max(ip.stmt + 1, 0) # n.b. ip.stmt is 0-indexed
424✔
165
    scopes = IRShow.LineInfoNode[]
424✔
166
    IRShow.append_scopes!(scopes, pc, codeinfo.debuginfo, def)
848✔
167
    if isempty(scopes)
424✔
168
        return [StackFrame(func, file, line, code, false, false, 0)]
×
169
    end
170
    closure = let inlined::Bool = false, def = def
424✔
171
        function closure_inner(lno)
438✔
172
            if inlined
1,480✔
173
                def = lno.method
1,056✔
174
                def isa Union{Method,Core.CodeInstance,MethodInstance} || (def = nothing)
2,112✔
175
            else
176
                def = codeinfo
424✔
177
            end
178
            sf = StackFrame(IRShow.normalize_method_name(lno.method), lno.file, lno.line, def, false, inlined, 0)
1,480✔
179
            inlined = true
1,480✔
180
            return sf
1,480✔
181
        end
182
    end
183
    return map(closure, scopes)
424✔
184
end
185

186
"""
187
    stacktrace([trace::Vector{Ptr{Cvoid}},] [c_funcs::Bool=false])::StackTrace
188

189
Return a stack trace in the form of a vector of `StackFrame`s. (By default stacktrace
190
doesn't return C functions, but this can be enabled.) When called without specifying a
191
trace, `stacktrace` first calls `backtrace`.
192
"""
193
Base.@constprop :none function stacktrace(trace::Vector{<:Union{Base.InterpreterIP,Ptr{Cvoid}}}, c_funcs::Bool=false)
336✔
194
    stack = StackTrace()
1,835✔
195
    for ip in trace
255✔
196
        for frame in lookup(ip)
3,370✔
197
            # Skip frames that come from C calls.
198
            if c_funcs || !frame.from_c
8,932✔
199
                push!(stack, frame)
2,013✔
200
            end
201
        end
4,566✔
202
    end
3,370✔
203
    return stack
255✔
204
end
205

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

215
"""
216
    remove_frames!(stack::StackTrace, name::Symbol)
217

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

228
function remove_frames!(stack::StackTrace, names::Vector{Symbol})
×
229
    deleteat!(stack, 1:something(findlast(frame -> frame.func in names, stack), 0))
×
230
    return stack
×
231
end
232

233
"""
234
    remove_frames!(stack::StackTrace, m::Module)
235

236
Return the `StackTrace` with all `StackFrame`s from the provided `Module` removed.
237
"""
238
function remove_frames!(stack::StackTrace, m::Module)
239
    filter!(f -> !from(f, m), stack)
3✔
240
    return stack
×
241
end
242

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

245
function frame_method_or_module(lkup::StackFrame)
246
    code = lkup.linfo
2,843✔
247
    code isa Method && return code
2,843✔
248
    code isa Module && return code
812✔
249
    mi = frame_mi(lkup)
2,855✔
250
    mi isa MethodInstance || return nothing
2,856✔
251
    return mi.def
2,830✔
252
end
253

254
function frame_mi(lkup::StackFrame)
255
    code = lkup.linfo
5,669✔
256
    code isa Core.CodeInstance && (code = code.def)
5,669✔
257
    code isa Core.ABIOverride && (code = code.def)
5,669✔
258
    code isa MethodInstance || return nothing
5,682✔
259
    return code
5,656✔
260
end
261

262
function show_spec_linfo(io::IO, frame::StackFrame)
5,615✔
263
    linfo = frame.linfo
5,615✔
264
    if linfo === nothing
5,615✔
265
        if frame.func === empty_sym
2,436✔
266
            print(io, "ip:0x", string(frame.pointer, base=16))
1✔
267
        elseif frame.func === top_level_scope_sym
2,435✔
268
            print(io, "top-level scope")
70✔
269
        else
270
            Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
2,365✔
271
        end
272
    elseif linfo isa CodeInfo
3,179✔
273
        print(io, "top-level scope")
350✔
274
    elseif linfo isa Module
2,829✔
275
        Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
×
276
    else
277
        if linfo isa Union{MethodInstance, CodeInstance}
2,829✔
278
            def = frame_method_or_module(frame)
5,658✔
279
            if def isa Module
2,829✔
280
                Base.show_mi(io, linfo::MethodInstance, #=from_stackframe=#true)
×
281
            elseif linfo isa CodeInstance && linfo.owner !== nothing
2,829✔
282
                show_custom_spec_sig(io, linfo.owner, linfo, frame)
3✔
283
            else
284
                # Equivalent to the default implementation of `show_custom_spec_sig`
285
                # for `linfo isa CodeInstance`, but saves an extra dynamic dispatch.
286
                show_spec_sig(io, def, frame_mi(frame).specTypes)
2,826✔
287
            end
288
        else
289
            m = linfo::Method
×
290
            show_spec_sig(io, m, m.sig)
×
291
        end
292
    end
293
end
294

295
# Can be extended by compiler packages to customize backtrace display of custom code instance frames
296
function show_custom_spec_sig(io::IO, @nospecialize(owner), linfo::CodeInstance, frame::StackFrame)
297
    mi = Base.get_ci_mi(linfo)
6✔
298
    return show_spec_sig(io, mi.def, mi.specTypes)
3✔
299
end
300

301
function show_spec_sig(io::IO, m::Method, @nospecialize(sig::Type))
2,829✔
302
    if get(io, :limit, :false)::Bool
9,166✔
303
        if !haskey(io, :displaysize)
2,664✔
304
            io = IOContext(io, :displaysize => displaysize(io))
352✔
305
        end
306
    end
307
    argnames = Base.method_argnames(m)
5,658✔
308
    argnames = replace(argnames, :var"#unused#" => :var"")
2,829✔
309
    if m.nkw > 0
2,829✔
310
        # rearrange call kw_impl(kw_args..., func, pos_args...) to func(pos_args...; kw_args)
311
        kwarg_types = Any[ fieldtype(sig, i) for i = 2:(1+m.nkw) ]
426✔
312
        uw = Base.unwrap_unionall(sig)::DataType
346✔
313
        pos_sig = Base.rewrap_unionall(Tuple{uw.parameters[(m.nkw+2):end]...}, sig)
346✔
314
        kwnames = argnames[2:(m.nkw+1)]
692✔
315
        for i = 1:length(kwnames)
391✔
316
            str = string(kwnames[i])::String
426✔
317
            if endswith(str, "...")
426✔
318
                kwnames[i] = Symbol(str[1:end-3])
×
319
            end
320
        end
506✔
321
        Base.show_tuple_as_call(io, m.name, pos_sig;
346✔
322
                                demangle=true,
323
                                kwargs=zip(kwnames, kwarg_types),
324
                                argnames=argnames[m.nkw+2:end])
325
    else
326
        Base.show_tuple_as_call(io, m.name, sig; demangle=true, argnames)
2,483✔
327
    end
328
end
329

330
function show(io::IO, frame::StackFrame)
322✔
331
    show_spec_linfo(io, frame)
322✔
332
    if frame.file !== empty_sym
322✔
333
        file_info = basename(string(frame.file))
313✔
334
        print(io, " at ")
313✔
335
        print(io, file_info, ":")
313✔
336
        if frame.line >= 0
313✔
337
            print(io, frame.line)
307✔
338
        else
339
            print(io, "?")
6✔
340
        end
341
    end
342
    if frame.inlined
322✔
343
        print(io, " [inlined]")
91✔
344
    end
345
end
346

347
function Base.parentmodule(frame::StackFrame)
348
    linfo = frame.linfo
11,604✔
349
    if linfo isa CodeInstance
11,604✔
350
        linfo = linfo.def
4,273✔
351
        if isa(linfo, Core.ABIOverride)
4,273✔
352
            linfo = linfo.def
×
353
        end
354
    end
355
    if linfo isa MethodInstance
11,604✔
356
        def = linfo.def
5,634✔
357
        if def isa Module
5,634✔
358
            return def
×
359
        else
360
            return (def::Method).module
5,634✔
361
        end
362
    elseif linfo isa Method
5,970✔
363
        return linfo.module
×
364
    elseif linfo isa Module
5,970✔
365
        return linfo
×
366
    else
367
        # The module is not always available (common reasons include
368
        # frames arising from the interpreter)
369
        nothing
2,286✔
370
    end
371
end
372

373
"""
374
    from(frame::StackFrame, filter_mod::Module)::Bool
375

376
Return whether the `frame` is from the provided `Module`
377
"""
378
function from(frame::StackFrame, m::Module)
×
379
    return parentmodule(frame) === m
×
380
end
381

382
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