Skip to content

Instantly share code, notes, and snippets.

@staticfloat
Created June 7, 2014 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staticfloat/93d7050a08ff7bb52373 to your computer and use it in GitHub Desktop.
Save staticfloat/93d7050a08ff7bb52373 to your computer and use it in GitHub Desktop.
Code to read the `jl_compileropts` structure in julia at runtime
#!/usr/bin/env julia
immutable jl_compileropts
build_path::ASCIIString
cpu_target::ASCIIString
code_coverage::Int8
check_bounds::Int8
int_literals::Int32
end
function load_compileropts()
opts_addr = cglobal(:jl_compileropts)
build_path = unsafe_load(convert(Ptr{Ptr{Uint8}}, opts_addr))
if build_path != C_NULL
build_path = bytestring(build_path)
else
build_path = ""
end
opts_addr += sizeof(Ptr{Uint8})
cpu_target = unsafe_load(convert(Ptr{Ptr{Uint8}}, opts_addr))
if cpu_target != C_NULL
cpu_target = bytestring(cpu_target)
else
cpu_target = ""
end
opts_addr += sizeof(Ptr{Uint8})
code_coverage = unsafe_load(convert(Ptr{Int8}, opts_addr))
opts_addr += sizeof(Int8)
check_bounds = unsafe_load(convert(Ptr{Int8}, opts_addr))
opts_addr += sizeof(Int8)
int_literals = unsafe_load(convert(Ptr{Int32}, opts_addr))
opts_addr += sizeof(Int32)
return jl_compileropts(build_path, cpu_target, code_coverage, check_bounds, int_literals)
end
println("cpu_target: $(load_compileropts().cpu_target)")
@vtjnash
Copy link

vtjnash commented Aug 21, 2014

Unless you declared this struct as __attribute__((packed)) in C, opts_addr has the wrong offset for int_literals. In general, it is better to declare the entire struct in Julia and load it in one go:

immutable jl_compileropts
    build_path::Ptr{Uint8}
    cpu_target::Ptr{Uint8}
    code_coverage::Int8
    check_bounds::Int8
    int_literals::Int32
end
function load_compileropts()
    return unsafe_load(cglobal(:jl_compileropts, jl_compileropts))
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment