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

JuliaLang / julia / #37728

26 Mar 2024 03:46AM UTC coverage: 80.612% (-0.8%) from 81.423%
#37728

push

local

web-flow
Update zlib to 1.3.1 (#53841)

Released January 22, 2024

69920 of 86737 relevant lines covered (80.61%)

14456248.65 hits per line

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

78.99
/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
using Base.IRShow: normalize_method_name, append_scopes!, LineInfoNode
12

13
export StackTrace, StackFrame, stacktrace
14

15
"""
16
    StackFrame
17

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

20
- `func::Symbol`
21

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

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

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

29
- `file::Symbol`
30

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

33
- `line::Int`
34

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

37
- `from_c::Bool`
38

39
  True if the code is from C.
40

41
- `inlined::Bool`
42

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

45
- `pointer::UInt64`
46

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

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

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

71
"""
72
    StackTrace
73

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

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

82

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

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

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

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

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

127
function lookup(ip::Union{Base.InterpreterIP,Core.Compiler.InterpreterIP})
263✔
128
    code = ip.code
263✔
129
    if code === nothing
263✔
130
        # interpreted top-level expression with no CodeInfo
131
        return [StackFrame(top_level_scope_sym, empty_sym, 0, nothing, false, false, 0)]
2✔
132
    end
133
    codeinfo = (code isa MethodInstance ? code.uninferred : code)::CodeInfo
519✔
134
    # prepare approximate code info
135
    if code isa MethodInstance && (meth = code.def; meth isa Method)
261✔
136
        func = meth.name
3✔
137
        file = meth.file
3✔
138
        line = meth.line
3✔
139
    else
140
        func = top_level_scope_sym
3✔
141
        file = empty_sym
3✔
142
        line = Int32(0)
3✔
143
    end
144
    def = (code isa MethodInstance ? code : StackTraces) # Module just used as a token for top-level code
261✔
145
    pc::Int = max(ip.stmt + 1, 0) # n.b. ip.stmt is 0-indexed
261✔
146
    scopes = LineInfoNode[]
261✔
147
    append_scopes!(scopes, pc, codeinfo.debuginfo, def)
261✔
148
    if isempty(scopes)
261✔
149
        return [StackFrame(func, file, line, code, false, false, 0)]
×
150
    end
151
    inlined = false
261✔
152
    scopes = map(scopes) do lno
261✔
153
        if inlined
759✔
154
            def = lno.method
498✔
155
            def isa Union{Method,MethodInstance} || (def = nothing)
996✔
156
        else
157
            def = codeinfo
261✔
158
        end
159
        sf = StackFrame(normalize_method_name(lno.method), lno.file, lno.line, def, false, inlined, 0)
759✔
160
        inlined = true
759✔
161
        return sf
759✔
162
    end
163
    return scopes
261✔
164
end
165

166
"""
167
    stacktrace([trace::Vector{Ptr{Cvoid}},] [c_funcs::Bool=false]) -> StackTrace
168

169
Return a stack trace in the form of a vector of `StackFrame`s. (By default stacktrace
170
doesn't return C functions, but this can be enabled.) When called without specifying a
171
trace, `stacktrace` first calls `backtrace`.
172
"""
173
Base.@constprop :none function stacktrace(trace::Vector{<:Union{Base.InterpreterIP,Core.Compiler.InterpreterIP,Ptr{Cvoid}}}, c_funcs::Bool=false)
73✔
174
    stack = StackTrace()
117✔
175
    for ip in trace
55✔
176
        for frame in lookup(ip)
1,385✔
177
            # Skip frames that come from C calls.
178
            if c_funcs || !frame.from_c
3,687✔
179
                push!(stack, frame)
822✔
180
            end
181
        end
1,896✔
182
    end
1,385✔
183
    return stack
55✔
184
end
185

186
Base.@constprop :none function stacktrace(c_funcs::Bool=false)
×
187
    stack = stacktrace(backtrace(), c_funcs)
×
188
    # Remove frame for this function (and any functions called by this function).
189
    remove_frames!(stack, :stacktrace)
×
190
    # also remove all of the non-Julia functions that led up to this point (if that list is non-empty)
191
    c_funcs && deleteat!(stack, 1:(something(findfirst(frame -> !frame.from_c, stack), 1) - 1))
×
192
    return stack
×
193
end
194

195
"""
196
    remove_frames!(stack::StackTrace, name::Symbol)
197

198
Takes a `StackTrace` (a vector of `StackFrames`) and a function name (a `Symbol`) and
199
removes the `StackFrame` specified by the function name from the `StackTrace` (also removing
200
all frames above the specified function). Primarily used to remove `StackTraces` functions
201
from the `StackTrace` prior to returning it.
202
"""
203
function remove_frames!(stack::StackTrace, name::Symbol)
×
204
    deleteat!(stack, 1:something(findlast(frame -> frame.func == name, stack), 0))
×
205
    return stack
×
206
end
207

208
function remove_frames!(stack::StackTrace, names::Vector{Symbol})
×
209
    deleteat!(stack, 1:something(findlast(frame -> frame.func in names, stack), 0))
×
210
    return stack
×
211
end
212

213
"""
214
    remove_frames!(stack::StackTrace, m::Module)
215

216
Return the `StackTrace` with all `StackFrame`s from the provided `Module` removed.
217
"""
218
function remove_frames!(stack::StackTrace, m::Module)
×
219
    filter!(f -> !from(f, m), stack)
×
220
    return stack
×
221
end
222

223
is_top_level_frame(f::StackFrame) = f.linfo isa CodeInfo || (f.linfo === nothing && f.func === top_level_scope_sym)
7✔
224

225
function show_spec_linfo(io::IO, frame::StackFrame)
2,839✔
226
    linfo = frame.linfo
2,839✔
227
    if linfo === nothing
2,839✔
228
        if frame.func === empty_sym
781✔
229
            print(io, "ip:0x", string(frame.pointer, base=16))
×
230
        elseif frame.func === top_level_scope_sym
781✔
231
            print(io, "top-level scope")
19✔
232
        else
233
            Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
762✔
234
        end
235
    elseif linfo isa CodeInfo
2,058✔
236
        print(io, "top-level scope")
127✔
237
    elseif linfo isa Module
792✔
238
        Base.print_within_stacktrace(io, Base.demangle_function_name(string(frame.func)), bold=true)
×
239
    elseif linfo isa MethodInstance
1,931✔
240
        def = linfo.def
1,931✔
241
        if def isa Module
1,931✔
242
            Base.show_mi(io, linfo, #=from_stackframe=#true)
×
243
        else
244
            show_spec_sig(io, def, linfo.specTypes)
1,931✔
245
        end
246
    else
247
        m = linfo::Method
×
248
        show_spec_sig(io, m, m.sig)
×
249
    end
250
end
251

252
function show_spec_sig(io::IO, m::Method, @nospecialize(sig::Type))
1,931✔
253
    if get(io, :limit, :false)::Bool
1,633✔
254
        if !haskey(io, :displaysize)
610✔
255
            io = IOContext(io, :displaysize => displaysize(io))
136✔
256
        end
257
    end
258
    argnames = Base.method_argnames(m)
3,862✔
259
    argnames = replace(argnames, :var"#unused#" => :var"")
3,862✔
260
    if m.nkw > 0
1,931✔
261
        # rearrange call kw_impl(kw_args..., func, pos_args...) to func(pos_args...; kw_args)
262
        kwarg_types = Any[ fieldtype(sig, i) for i = 2:(1+m.nkw) ]
424✔
263
        uw = Base.unwrap_unionall(sig)::DataType
356✔
264
        pos_sig = Base.rewrap_unionall(Tuple{uw.parameters[(m.nkw+2):end]...}, sig)
356✔
265
        kwnames = argnames[2:(m.nkw+1)]
712✔
266
        for i = 1:length(kwnames)
356✔
267
            str = string(kwnames[i])::String
424✔
268
            if endswith(str, "...")
424✔
269
                kwnames[i] = Symbol(str[1:end-3])
×
270
            end
271
        end
492✔
272
        Base.show_tuple_as_call(io, m.name, pos_sig;
356✔
273
                                demangle=true,
274
                                kwargs=zip(kwnames, kwarg_types),
275
                                argnames=argnames[m.nkw+2:end])
276
    else
277
        Base.show_tuple_as_call(io, m.name, sig; demangle=true, argnames)
1,575✔
278
    end
279
end
280

281
function show(io::IO, frame::StackFrame)
170✔
282
    show_spec_linfo(io, frame)
170✔
283
    if frame.file !== empty_sym
170✔
284
        file_info = basename(string(frame.file))
163✔
285
        print(io, " at ")
163✔
286
        print(io, file_info, ":")
163✔
287
        if frame.line >= 0
163✔
288
            print(io, frame.line)
163✔
289
        else
290
            print(io, "?")
×
291
        end
292
    end
293
    if frame.inlined
170✔
294
        print(io, " [inlined]")
41✔
295
    end
296
end
297

298
function Base.parentmodule(frame::StackFrame)
299
    linfo = frame.linfo
94,967✔
300
    if linfo isa MethodInstance
94,967✔
301
        def = linfo.def
92,440✔
302
        if def isa Module
92,440✔
303
            return def
×
304
        else
305
            return (def::Method).module
92,440✔
306
        end
307
    elseif linfo isa Method
2,527✔
308
        return linfo.module
×
309
    elseif linfo isa Module
×
310
        return linfo
×
311
    else
312
        # The module is not always available (common reasons include
313
        # frames arising from the interpreter)
314
        nothing
315
    end
316
end
317

318
"""
319
    from(frame::StackFrame, filter_mod::Module) -> Bool
320

321
Return whether the `frame` is from the provided `Module`
322
"""
323
function from(frame::StackFrame, m::Module)
×
324
    return parentmodule(frame) === m
×
325
end
326

327
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

© 2025 Coveralls, Inc