• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

JuliaDebug/Infiltrator.jl: No-overhead breakpoints in Julia

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

JuliaDebug/Infiltrator.jl

开源软件地址:

https://github.com/JuliaDebug/Infiltrator.jl

开源编程语言:

Julia 100.0%

开源软件介绍:

Infiltrator.jl CI version

This packages provides a macro called @infiltrate, which sets a "breakpoint" in a local context (similar to Matlab's keyboard function and IPython's embed). The advantage of this macro over e.g. Debugger.jl is that all code is completely compiled, so the performance overhead should be negligible.

Note that you cannot access other functions in the callstack, or step into functions. If you need that functionality, use Debugger.jl, VSCode's or Juno's debugger.

Running code that ends up triggering the @infiltrate REPL mode via inline evaluation in VSCode or Juno can cause issues, so it's recommended to always use the REPL directly.

@infiltrate

@infiltrate cond = true

@infiltrate sets an infiltration point (or breakpoint).

When the infiltration point is hit, it will drop you into an interactive REPL session that lets you inspect local variables and the call stack as well as execute aribtrary statements in the context of the current functions module.

This macro also accepts an optional argument cond that must evaluate to a boolean, and then this macro will serve as a "conditinal breakpoint", which starts inspections only when its condition is true.

You can also use

if isdefined(Main, :Infiltrator)
  Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__)
end

to infiltrate package code without any post-hoc evaluation into the module (because the functional form does not require Infiltrator to be loaded at compiletime).

@exfiltrate

@exfiltrate

Assigns all local variables into global storage.

The safehouse

Exfiltrating variables (with @exfiltrate or by assignment in a @infiltrate session) happens by assigning the variable to a global storage space (backed by a module); any exfiltrated objects can be directly accessed, via Infiltrator.store or its exported aliases safehouse or exfiltrated:

julia> foo(x) = @exfiltrate
foo (generic function with 1 method)

julia> foo(3)

julia> safehouse.x # or exfiltrated.x
3

You can reset the safehouse with Infiltrator.clear_store!().

You can also assign a specific module with Infiltrator.set_store!(mod). This allows you to e.g. set the backing module to Main and therefore export the contents of the safehouse to the global namespace (although doing so is not recommended).

Example usage:

julia> function f(x)
         out = []
         for i in x
           push!(out, 2i)
           @infiltrate
         end
         out
       end
f (generic function with 1 method)

julia> f([1,2,3])
Infiltrating f(x::Vector{Int64})
  at REPL[10]:5

infil> ?
  Code entered is evaluated in the current functions module. Note that you cannot change local
  variables, but can assign to globals in a permanent store module.

  The following commands are special cased:
    - `?`: Print this help text.
    - `@trace`: Print the current stack trace.
    - `@locals`: Print local variables. `@locals x y` only prints `x` and `y`.
    - `@exfiltrate`: Save all local variables into the store. `@exfiltrate x y` saves `x` and `y`;
      this variant can also exfiltrate variables defined in the `infil>` REPL.
    - `@toggle`: Toggle infiltrating at this `@infiltrate` spot (clear all with `Infiltrator.clear_disabled!()`).
    - `@continue`: Continue to the next infiltration point or exit (shortcut: Ctrl-D).
    - `@doc symbol`: Get help for `symbol` (same as in the normal Julia REPL).
    - `@exit`: Stop infiltrating for the remainder of this session and exit (on Julia versions prior to
      1.5 this needs to be manually cleared with `Infiltrator.end_session!()`).

infil> @locals
- out::Vector{Any} = Any[2]
- i::Int64 = 1
- x::Vector{Int64} = [1, 2, 3]

infil> 0//0
ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)
Stacktrace:
 [1] __throw_rational_argerror_zero(T::Type)
   @ Base ./rational.jl:32
 [2] Rational{Int64}(num::Int64, den::Int64)
   @ Base ./rational.jl:34
 [3] Rational
   @ ./rational.jl:39 [inlined]
 [4] //(n::Int64, d::Int64)
   @ Base ./rational.jl:62
 [5] top-level scope
   @ none:1

infil> @toggle
Disabled infiltration at this infiltration point.

infil> @toggle
Enabled infiltration at this infiltration point.

infil> @continue

Infiltrating f(x::Vector{Int64})
  at REPL[10]:5

infil> intermediate = copy(out)
2-element Vector{Any}:
 2
 4

infil> @exfiltrate intermediate x
Exfiltrating 2 local variables into the safehouse.

infil> @exit

3-element Vector{Any}:
 2
 4
 6

julia> safehouse.intermediate
2-element Vector{Any}:
 2
 4

julia> @withstore begin
         x = 23
         x .* intermediate
       end
2-element Vector{Int64}:
 46
 92

Related projects




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
carstenbauer/JuliaWorkshop18: Material for an advanced julia workshop for gradua ...发布时间:2022-07-09
下一篇:
CliMA/Oceananigans.jl: 发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap