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

JuliaLang / julia / 1576

09 Feb 2026 02:05PM UTC coverage: 75.058% (-1.7%) from 76.711%
1576

push

buildkite

web-flow
Supporting sticky (generational) immix (#57327)

Adding support for sticky immix. Needs to be merged after:

- https://github.com/JuliaLang/julia/pull/57237
- https://github.com/JuliaLang/julia/pull/57176
- https://github.com/JuliaLang/julia/pull/57252
- https://github.com/JuliaLang/julia/pull/57769

---------

Co-authored-by: Luis Eduardo de Souza Amorim <eduardo@bear.moma>

57362 of 76424 relevant lines covered (75.06%)

7438320.97 hits per line

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

79.29
/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"
6,728✔
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
11,118✔
90
end
91

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

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

128
function lookup(ip::Base.InterpreterIP)
153✔
129
    code = ip.code
153✔
130
    if code === nothing
153✔
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)
153✔
136
        func = meth.name
×
137
        file = meth.file
×
138
        line = meth.line
×
139
        codeinfo = meth.source
×
140
    else
141
        func = top_level_scope_sym
153✔
142
        file = empty_sym
153✔
143
        line = Int32(0)
153✔
144
        if code isa Core.CodeInstance
153✔
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
153✔
161
        end
162
    end
163
    def = (code isa CodeInfo ? StackTraces : code) # Module just used as a token for top-level code
153✔
164
    pc::Int = max(ip.stmt + 1, 0) # n.b. ip.stmt is 0-indexed
153✔
165
    scopes = IRShow.LineInfoNode[]
153✔
166
    IRShow.append_scopes!(scopes, pc, codeinfo.debuginfo, def)
306✔
167
    if isempty(scopes)
153✔
168
        return [StackFrame(func, file, line, code, false, false, 0)]
×
169
    end
170
    res = Vector{StackFrame}(undef, length(scopes))
153✔
171
    inlined = false
153✔
172
    def_local = def
153✔
173
    for i in eachindex(scopes)
301✔
174
        lno = scopes[i]
542✔
175
        if inlined
542✔
176
            def_local = lno.method
389✔
177
            def_local isa Union{Method,Core.CodeInstance,MethodInstance} || (def_local = nothing)
389✔
178
        else
179
            def_local = codeinfo
153✔
180
        end
181
        res[i] = StackFrame(IRShow.normalize_method_name(lno.method), lno.file, lno.line,
542✔
182
            def_local, false, inlined, 0)
183
        inlined = true
542✔
184
    end
931✔
185
    return res
153✔
186
end
187

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

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

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

217
"""
218
    remove_frames!(stack::StackTrace, name::Symbol)
219

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

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

235
"""
236
    remove_frames!(stack::StackTrace, m::Module)
237

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

245
is_top_level_frame(f::StackFrame) = f.linfo isa CodeInfo || (f.linfo === nothing && f.func === top_level_scope_sym)
6✔
246

247
function frame_method_or_module(lkup::StackFrame)
248
    code = lkup.linfo
752✔
249
    code isa Method && return code
752✔
250
    code isa Module && return code
156✔
251
    mi = frame_mi(lkup)
756✔
252
    mi isa MethodInstance || return nothing
758✔
253
    return mi.def
746✔
254
end
255

256
function frame_mi(lkup::StackFrame)
257
    code = lkup.linfo
1,495✔
258
    code isa Core.CodeInstance && (code = code.def)
1,495✔
259
    code isa Core.ABIOverride && (code = code.def)
1,495✔
260
    code isa MethodInstance || return nothing
1,501✔
261
    return code
1,489✔
262
end
263

264
function show_spec_linfo(io::IO, frame::StackFrame)
1,645✔
265
    linfo = frame.linfo
1,645✔
266
    if linfo === nothing
1,645✔
267
        if frame.func === empty_sym
787✔
268
            print(io, "ip:0x", string(frame.pointer, base=16))
1✔
269
        elseif frame.func === top_level_scope_sym
786✔
270
            print(io, "top-level scope")
23✔
271
        else
272
            Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
763✔
273
        end
274
    elseif linfo isa CodeInfo
858✔
275
        print(io, "top-level scope")
114✔
276
    elseif linfo isa Module
744✔
277
        Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
×
278
    else
279
        if linfo isa Union{MethodInstance, CodeInstance}
744✔
280
            def = frame_method_or_module(frame)
1,488✔
281
            if def isa Module
744✔
282
                Base.show_mi(io, linfo::MethodInstance, #=from_stackframe=#true)
×
283
            elseif linfo isa CodeInstance && linfo.owner !== nothing
744✔
284
                show_custom_spec_sig(io, linfo.owner, linfo, frame)
1✔
285
            else
286
                # Equivalent to the default implementation of `show_custom_spec_sig`
287
                # for `linfo isa CodeInstance`, but saves an extra dynamic dispatch.
288
                mi = frame_mi(frame)::MethodInstance
743✔
289
                show_spec_sig(io, def::Method, mi.specTypes)
743✔
290
            end
291
        else
292
            m = linfo::Method
×
293
            show_spec_sig(io, m, m.sig)
×
294
        end
295
    end
296
end
297

298
# Can be extended by compiler packages to customize backtrace display of custom code instance frames
299
function show_custom_spec_sig(io::IO, @nospecialize(owner), linfo::CodeInstance, frame::StackFrame)
300
    mi = Base.get_ci_mi(linfo)
2✔
301
    m = mi.def::Method # the case ::Module is handled in show_spec_linfo
1✔
302
    return show_spec_sig(io, m, mi.specTypes)
1✔
303
end
304

305
function show_spec_sig(io::IO, m::Method, @nospecialize(sig::Type))
744✔
306
    if get(io, :limit, :false)::Bool
2,517✔
307
        if !haskey(io, :displaysize)
743✔
308
            io = IOContext(io, :displaysize => displaysize(io))
87✔
309
        end
310
    end
311
    argnames = Base.method_argnames(m)
1,488✔
312
    argnames = replace(argnames, :var"#unused#" => :var"")
744✔
313
    if m.nkw > 0
744✔
314
        # rearrange call kw_impl(kw_args..., func, pos_args...) to func(pos_args...; kw_args)
315
        kwarg_types = Any[ fieldtype(sig, i) for i = 2:(1+m.nkw) ]
122✔
316
        uw = Base.unwrap_unionall(sig)::DataType
91✔
317
        pos_sig = Base.rewrap_unionall(Tuple{uw.parameters[(m.nkw+2):end]...}, sig)
91✔
318
        kwnames = argnames[2:(m.nkw+1)]
182✔
319
        for i = 1:length(kwnames)
101✔
320
            str = string(kwnames[i])::String
122✔
321
            if endswith(str, "...")
122✔
322
                kwnames[i] = Symbol(str[1:end-3])
×
323
            end
324
        end
153✔
325
        Base.show_tuple_as_call(io, m.name, pos_sig;
91✔
326
                                demangle=true,
327
                                kwargs=zip(kwnames, kwarg_types),
328
                                argnames=argnames[m.nkw+2:end])
329
    else
330
        Base.show_tuple_as_call(io, m.name, sig; demangle=true, argnames)
653✔
331
    end
332
end
333

334
function show(io::IO, frame::StackFrame)
161✔
335
    show_spec_linfo(io, frame)
161✔
336
    if frame.file !== empty_sym
161✔
337
        file_info = basename(string(frame.file))
156✔
338
        print(io, " at ")
156✔
339
        print(io, file_info, ":")
156✔
340
        if frame.line >= 0
156✔
341
            print(io, frame.line)
153✔
342
        else
343
            print(io, "?")
3✔
344
        end
345
    end
346
    if frame.inlined
161✔
347
        print(io, " [inlined]")
42✔
348
    end
349
end
350

351
function Base.parentmodule(frame::StackFrame)
352
    linfo = frame.linfo
3,501✔
353
    if linfo isa CodeInstance
3,501✔
354
        linfo = linfo.def
1,274✔
355
        if isa(linfo, Core.ABIOverride)
1,274✔
356
            linfo = linfo.def
×
357
        end
358
    end
359
    if linfo isa MethodInstance
3,501✔
360
        def = linfo.def
1,633✔
361
        if def isa Module
1,633✔
362
            return def
×
363
        else
364
            return (def::Method).module
1,633✔
365
        end
366
    elseif linfo isa Method
1,868✔
367
        return linfo.module
×
368
    elseif linfo isa Module
1,868✔
369
        return linfo
×
370
    else
371
        # The module is not always available (common reasons include
372
        # frames arising from the interpreter)
373
        nothing
724✔
374
    end
375
end
376

377
"""
378
    from(frame::StackFrame, filter_mod::Module)::Bool
379

380
Return whether the `frame` is from the provided `Module`
381
"""
382
function from(frame::StackFrame, m::Module)
×
383
    return parentmodule(frame) === m
×
384
end
385

386
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