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

JuliaLang / julia / #37638

01 Oct 2023 06:36PM UTC coverage: 87.461% (-0.1%) from 87.584%
#37638

push

local

web-flow
Update libc.jl compatability note (#51535)

Update libc.jl compatability note to julia 1.11

73897 of 84491 relevant lines covered (87.46%)

11729457.42 hits per line

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

81.73
/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, Method, Module, Core.CodeInfo, Nothing}`
24

25
  The MethodInstance or CodeInfo containing the execution context (if it could be found), \
26
     or Module (for macro expansions)"
27

28
- `file::Symbol`
29

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

32
- `line::Int`
33

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

36
- `from_c::Bool`
37

38
  True if the code is from C.
39

40
- `inlined::Bool`
41

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

44
- `pointer::UInt64`
45

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

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

67
StackFrame(func, file, line) = StackFrame(Symbol(func), Symbol(file), line,
×
68
                                          nothing, false, false, 0)
69

70
"""
71
    StackTrace
72

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

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

81

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

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

100
get_inlinetable(::Any) = nothing
17,307✔
101
function get_inlinetable(mi::MethodInstance)
182,981✔
102
    isdefined(mi, :def) && mi.def isa Method && isdefined(mi, :cache) && isdefined(mi.cache, :inferred) &&
353,653✔
103
        mi.cache.inferred !== nothing || return nothing
104
    linetable = ccall(:jl_uncompress_ir, Any, (Any, Any, Any), mi.def, mi.cache, mi.cache.inferred).linetable
12,309✔
105
    return filter!(x -> x.inlined_at > 0, linetable)
1,098,943✔
106
end
107

108
get_method_instance_roots(::Any) = nothing
17,307✔
109
function get_method_instance_roots(mi::Union{Method, MethodInstance})
170,699✔
110
    m = mi isa MethodInstance ? mi.def : mi
170,699✔
111
    m isa Method && isdefined(m, :roots) || return nothing
170,699✔
112
    return filter(x -> x isa MethodInstance, m.roots)
1,253,464✔
113
end
114

115
function lookup_inline_frame_info(func::Symbol, file::Symbol, linenum::Int, inlinetable::Vector{Core.LineInfoNode})
1,486✔
116
    #REPL frames and some base files lack this prefix while others have it; should fix?
117
    filestripped = Symbol(lstrip(string(file), ('.', '\\', '/')))
1,486✔
118
    linfo = nothing
×
119
    #=
120
    Some matching entries contain the MethodInstance directly.
121
    Other matching entries contain only a Method or Symbol (function name); such entries
122
    are located after the entry with the MethodInstance, so backtracking is required.
123
    If backtracking fails, the Method or Module is stored for return, but we continue
124
    the search in case a MethodInstance is found later.
125
    TODO: If a backtrack has failed, do we need to backtrack again later if another Method
126
    or Symbol match is found? Or can a limit on the subsequent backtracks be placed?
127
    =#
128
    for (i, line) in enumerate(inlinetable)
2,972✔
129
        Base.IRShow.method_name(line) === func && line.file ∈ (file, filestripped) && line.line == linenum || continue
1,958,866✔
130
        if line.method isa MethodInstance
7,078✔
131
            linfo = line.method
×
132
            break
×
133
        elseif line.method isa Method || line.method isa Symbol
14,156✔
134
            linfo = line.method isa Method ? line.method : line.module
14,156✔
135
            # backtrack to find the matching MethodInstance, if possible
136
            for j in (i - 1):-1:1
13,951✔
137
                nextline = inlinetable[j]
9,857✔
138
                nextline.inlined_at == line.inlined_at && Base.IRShow.method_name(line) === Base.IRShow.method_name(nextline) && line.file === nextline.file || break
16,670✔
139
                if nextline.method isa MethodInstance
3,044✔
140
                    linfo = nextline.method
×
141
                    break
×
142
                end
143
            end
3,044✔
144
        end
145
    end
1,958,866✔
146
    return linfo
1,486✔
147
end
148

149
function lookup_inline_frame_info(func::Symbol, file::Symbol, miroots::Vector{Any})
7,663✔
150
    # REPL frames and some base files lack this prefix while others have it; should fix?
151
    filestripped = Symbol(lstrip(string(file), ('.', '\\', '/')))
7,663✔
152
    matches = filter(miroots) do x
7,663✔
153
        x.def isa Method || return false
69,906✔
154
        m = x.def::Method
69,906✔
155
        return m.name == func && m.file ∈ (file, filestripped)
69,906✔
156
    end
157
    if length(matches) > 1
7,663✔
158
        # ambiguous, check if method is same and return that instead
159
        all_matched = true
×
160
        for m in matches
84✔
161
            all_matched = m.def.line == matches[1].def.line &&
411✔
162
                m.def.module == matches[1].def.module
163
            all_matched || break
411✔
164
        end
411✔
165
        if all_matched
84✔
166
            return matches[1].def
42✔
167
        end
168
        # all else fails, return module if they match, or give up
169
        all_matched = true
×
170
        for m in matches
42✔
171
            all_matched = m.def.module == matches[1].def.module
104✔
172
            all_matched || break
104✔
173
        end
146✔
174
        return all_matched ? matches[1].def.module : nothing
42✔
175
    elseif length(matches) == 1
7,579✔
176
        return matches[1]
53✔
177
    end
178
    return nothing
7,526✔
179
end
180

181
"""
182
    lookup(pointer::Ptr{Cvoid}) -> Vector{StackFrame}
183

184
Given a pointer to an execution context (usually generated by a call to `backtrace`), looks
185
up stack frame context information. Returns an array of frame information for all functions
186
inlined at that point, innermost function first.
187
"""
188
Base.@constprop :none function lookup(pointer::Ptr{Cvoid})
200,371✔
189
    infos = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint), pointer, false)::Core.SimpleVector
200,371✔
190
    pointer = convert(UInt64, pointer)
200,371✔
191
    isempty(infos) && return [StackFrame(empty_sym, empty_sym, -1, nothing, true, false, pointer)] # this is equal to UNKNOWN
200,371✔
192
    parent_linfo = infos[end][4]
200,371✔
193
    inlinetable = get_inlinetable(parent_linfo)
217,761✔
194
    miroots = inlinetable === nothing ? get_method_instance_roots(parent_linfo) : nothing # fallback if linetable missing
200,371✔
195
    res = Vector{StackFrame}(undef, length(infos))
200,371✔
196
    for i in reverse(1:length(infos))
400,742✔
197
        info = infos[i]::Core.SimpleVector
218,576✔
198
        @assert(length(info) == 6)
218,576✔
199
        func = info[1]::Symbol
218,576✔
200
        file = info[2]::Symbol
218,576✔
201
        linenum = info[3]::Int
218,576✔
202
        linfo = info[4]
218,576✔
203
        if i < length(infos)
218,576✔
204
            if inlinetable !== nothing
18,205✔
205
                linfo = lookup_inline_frame_info(func, file, linenum, inlinetable)
1,486✔
206
            elseif miroots !== nothing
16,719✔
207
                linfo = lookup_inline_frame_info(func, file, miroots)
7,663✔
208
            end
209
        end
210
        res[i] = StackFrame(func, file, linenum, linfo, info[5]::Bool, info[6]::Bool, pointer)
218,576✔
211
    end
236,781✔
212
    return res
200,371✔
213
end
214

215
const top_level_scope_sym = Symbol("top-level scope")
216

217
function lookup(ip::Union{Base.InterpreterIP,Core.Compiler.InterpreterIP})
253✔
218
    code = ip.code
253✔
219
    if code === nothing
253✔
220
        # interpreted top-level expression with no CodeInfo
221
        return [StackFrame(top_level_scope_sym, empty_sym, 0, nothing, false, false, 0)]
1✔
222
    end
223
    codeinfo = (code isa MethodInstance ? code.uninferred : code)::CodeInfo
504✔
224
    # prepare approximate code info
225
    if code isa MethodInstance && (meth = code.def; meth isa Method)
252✔
226
        func = meth.name
×
227
        file = meth.file
×
228
        line = meth.line
×
229
    else
230
        func = top_level_scope_sym
×
231
        file = empty_sym
×
232
        line = Int32(0)
×
233
    end
234
    i = max(ip.stmt+1, 1)  # ip.stmt is 0-indexed
252✔
235
    if i > length(codeinfo.codelocs) || codeinfo.codelocs[i] == 0
504✔
236
        return [StackFrame(func, file, line, code, false, false, 0)]
×
237
    end
238
    lineinfo = codeinfo.linetable[codeinfo.codelocs[i]]::Core.LineInfoNode
252✔
239
    scopes = StackFrame[]
252✔
240
    while true
697✔
241
        inlined = lineinfo.inlined_at != 0
697✔
242
        push!(scopes, StackFrame(Base.IRShow.method_name(lineinfo)::Symbol, lineinfo.file, lineinfo.line, inlined ? nothing : code, false, inlined, 0))
949✔
243
        inlined || break
697✔
244
        lineinfo = codeinfo.linetable[lineinfo.inlined_at]::Core.LineInfoNode
445✔
245
    end
445✔
246
    return scopes
252✔
247
end
248

249
"""
250
    stacktrace([trace::Vector{Ptr{Cvoid}},] [c_funcs::Bool=false]) -> StackTrace
251

252
Return a stack trace in the form of a vector of `StackFrame`s. (By default stacktrace
253
doesn't return C functions, but this can be enabled.) When called without specifying a
254
trace, `stacktrace` first calls `backtrace`.
255
"""
256
Base.@constprop :none function stacktrace(trace::Vector{<:Union{Base.InterpreterIP,Core.Compiler.InterpreterIP,Ptr{Cvoid}}}, c_funcs::Bool=false)
112✔
257
    stack = StackTrace()
121✔
258
    for ip in trace
58✔
259
        for frame in lookup(ip)
1,679✔
260
            # Skip frames that come from C calls.
261
            if c_funcs || !frame.from_c
4,761✔
262
                push!(stack, frame)
847✔
263
            end
264
        end
4,125✔
265
    end
1,735✔
266
    return stack
57✔
267
end
268

269
Base.@constprop :none function stacktrace(c_funcs::Bool=false)
×
270
    stack = stacktrace(backtrace(), c_funcs)
×
271
    # Remove frame for this function (and any functions called by this function).
272
    remove_frames!(stack, :stacktrace)
×
273
    # also remove all of the non-Julia functions that led up to this point (if that list is non-empty)
274
    c_funcs && deleteat!(stack, 1:(something(findfirst(frame -> !frame.from_c, stack), 1) - 1))
×
275
    return stack
×
276
end
277

278
"""
279
    remove_frames!(stack::StackTrace, name::Symbol)
280

281
Takes a `StackTrace` (a vector of `StackFrames`) and a function name (a `Symbol`) and
282
removes the `StackFrame` specified by the function name from the `StackTrace` (also removing
283
all frames above the specified function). Primarily used to remove `StackTraces` functions
284
from the `StackTrace` prior to returning it.
285
"""
286
function remove_frames!(stack::StackTrace, name::Symbol)
×
287
    deleteat!(stack, 1:something(findlast(frame -> frame.func == name, stack), 0))
×
288
    return stack
×
289
end
290

291
function remove_frames!(stack::StackTrace, names::Vector{Symbol})
×
292
    deleteat!(stack, 1:something(findlast(frame -> frame.func in names, stack), 0))
×
293
    return stack
×
294
end
295

296
"""
297
    remove_frames!(stack::StackTrace, m::Module)
298

299
Return the `StackTrace` with all `StackFrame`s from the provided `Module` removed.
300
"""
301
function remove_frames!(stack::StackTrace, m::Module)
×
302
    filter!(f -> !from(f, m), stack)
×
303
    return stack
×
304
end
305

306
is_top_level_frame(f::StackFrame) = f.linfo isa CodeInfo || (f.linfo === nothing && f.func === top_level_scope_sym)
12✔
307

308
function show_spec_linfo(io::IO, frame::StackFrame)
2,347✔
309
    linfo = frame.linfo
2,347✔
310
    if linfo === nothing
2,347✔
311
        if frame.func === empty_sym
735✔
312
            print(io, "ip:0x", string(frame.pointer, base=16))
×
313
        elseif frame.func === top_level_scope_sym
735✔
314
            print(io, "top-level scope")
20✔
315
        else
316
            Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
715✔
317
        end
318
    elseif linfo isa CodeInfo
1,612✔
319
        print(io, "top-level scope")
125✔
320
    elseif linfo isa Module
1,487✔
321
        Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
143✔
322
    elseif linfo isa MethodInstance
1,344✔
323
        def = linfo.def
1,343✔
324
        if def isa Module
1,343✔
325
            Base.show_mi(io, linfo, #=from_stackframe=#true)
×
326
        else
327
            show_spec_sig(io, def, linfo.specTypes)
1,343✔
328
        end
329
    else
330
        m = linfo::Method
1✔
331
        show_spec_sig(io, m, m.sig)
1✔
332
    end
333
end
334

335
function show_spec_sig(io::IO, m::Method, @nospecialize(sig::Type))
1,344✔
336
    if get(io, :limit, :false)::Bool
1,506✔
337
        if !haskey(io, :displaysize)
494✔
338
            io = IOContext(io, :displaysize => displaysize(io))
108✔
339
        end
340
    end
341
    argnames = Base.method_argnames(m)
2,688✔
342
    argnames = replace(argnames, :var"#unused#" => :var"")
1,344✔
343
    if m.nkw > 0
1,344✔
344
        # rearrange call kw_impl(kw_args..., func, pos_args...) to func(pos_args...; kw_args)
345
        kwarg_types = Any[ fieldtype(sig, i) for i = 2:(1+m.nkw) ]
333✔
346
        uw = Base.unwrap_unionall(sig)::DataType
307✔
347
        pos_sig = Base.rewrap_unionall(Tuple{uw.parameters[(m.nkw+2):end]...}, sig)
307✔
348
        kwnames = argnames[2:(m.nkw+1)]
307✔
349
        for i = 1:length(kwnames)
614✔
350
            str = string(kwnames[i])::String
333✔
351
            if endswith(str, "...")
333✔
352
                kwnames[i] = Symbol(str[1:end-3])
×
353
            end
354
        end
359✔
355
        Base.show_tuple_as_call(io, m.name, pos_sig;
307✔
356
                                demangle=true,
357
                                kwargs=zip(kwnames, kwarg_types),
358
                                argnames=argnames[m.nkw+2:end])
359
    else
360
        Base.show_tuple_as_call(io, m.name, sig; demangle=true, argnames)
1,037✔
361
    end
362
end
363

364
function show(io::IO, frame::StackFrame)
208✔
365
    show_spec_linfo(io, frame)
208✔
366
    if frame.file !== empty_sym
208✔
367
        file_info = basename(string(frame.file))
201✔
368
        print(io, " at ")
201✔
369
        print(io, file_info, ":")
201✔
370
        if frame.line >= 0
201✔
371
            print(io, frame.line)
201✔
372
        else
373
            print(io, "?")
×
374
        end
375
    end
376
    if frame.inlined
208✔
377
        print(io, " [inlined]")
59✔
378
    end
379
end
380

381
function Base.parentmodule(frame::StackFrame)
3,032✔
382
    linfo = frame.linfo
95,400✔
383
    if linfo isa MethodInstance
95,400✔
384
        def = linfo.def
92,736✔
385
        if def isa Module
92,736✔
386
            return def
×
387
        else
388
            return (def::Method).module
92,736✔
389
        end
390
    elseif linfo isa Method
2,664✔
391
        return linfo.module
3✔
392
    elseif linfo isa Module
2,661✔
393
        return linfo
271✔
394
    else
395
        # The module is not always available (common reasons include
396
        # frames arising from the interpreter)
397
        nothing
2,390✔
398
    end
399
end
400

401
"""
402
    from(frame::StackFrame, filter_mod::Module) -> Bool
403

404
Return whether the `frame` is from the provided `Module`
405
"""
406
function from(frame::StackFrame, m::Module)
×
407
    return parentmodule(frame) === m
×
408
end
409

410
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