Skip to content

Commit

Permalink
make jl_lookup_code_address return CodeInstance instead of `Metho…
Browse files Browse the repository at this point in the history
…dInstance`
  • Loading branch information
aviatesk committed Apr 2, 2024
1 parent f73ffdb commit 2479c85
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
5 changes: 4 additions & 1 deletion base/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ up stack frame context information. Returns an array of frame information for al
inlined at that point, innermost function first.
"""
Base.@constprop :none function lookup(pointer::Ptr{Cvoid})
infos = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint), pointer, false)::Core.SimpleVector
infos = @ccall jl_lookup_code_address(pointer::Ptr{Cvoid}, false::Cint)::Core.SimpleVector
pointer = convert(UInt64, pointer)
isempty(infos) && return [StackFrame(empty_sym, empty_sym, -1, nothing, true, false, pointer)] # this is equal to UNKNOWN
res = Vector{StackFrame}(undef, length(infos))
Expand All @@ -117,6 +117,9 @@ Base.@constprop :none function lookup(pointer::Ptr{Cvoid})
file = info[2]::Symbol
linenum = info[3]::Int
linfo = info[4]
if linfo isa Core.CodeInstance
linfo = linfo.def
end
res[i] = StackFrame(func, file, linenum, linfo, info[5]::Bool, info[6]::Bool, pointer)
end
return res
Expand Down
2 changes: 1 addition & 1 deletion src/codegen-stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ JL_DLLEXPORT int jl_getFunctionInfo_fallback(jl_frame_t **frames, uintptr_t poin
}

JL_DLLEXPORT void jl_register_fptrs_fallback(uint64_t image_base, const struct _jl_image_fptrs_t *fptrs,
jl_method_instance_t **linfos, size_t n)
jl_code_instance_t **linfos, size_t n)
{
(void)image_base; (void)fptrs; (void)linfos; (void)n;
}
Expand Down
6 changes: 3 additions & 3 deletions src/debug-registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class JITDebugInfoRegistry
struct image_info_t {
uint64_t base;
jl_image_fptrs_t fptrs;
jl_method_instance_t **fvars_linfo;
jl_code_instance_t **fvars_linfo;
size_t fvars_n;
};

Expand Down Expand Up @@ -114,7 +114,7 @@ class JITDebugInfoRegistry
typedef rev_map<uint64_t, objfileentry_t> objfilemap_t;

objectmap_t objectmap{};
rev_map<size_t, std::pair<size_t, jl_method_instance_t *>> linfomap{};
rev_map<size_t, std::pair<size_t, jl_code_instance_t *>> linfomap{};

// Maintain a mapping of unrealized function names -> linfo objects
// so that when we see it get emitted, we can add a link back to the linfo
Expand All @@ -135,7 +135,7 @@ class JITDebugInfoRegistry
libc_frames_t libc_frames{};

void add_code_in_flight(llvm::StringRef name, jl_code_instance_t *codeinst, const llvm::DataLayout &DL) JL_NOTSAFEPOINT;
jl_method_instance_t *lookupLinfo(size_t pointer) JL_NOTSAFEPOINT;
jl_code_instance_t *lookupLinfo(size_t pointer) JL_NOTSAFEPOINT;
void registerJITObject(const llvm::object::ObjectFile &Object,
std::function<uint64_t(const llvm::StringRef &)> getLoadAddress,
std::function<void*(void*)> lookupWriteAddress);
Expand Down
18 changes: 8 additions & 10 deletions src/debuginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ void JITDebugInfoRegistry::add_code_in_flight(StringRef name, jl_code_instance_t
(**codeinst_in_flight)[mangle(name, DL)] = codeinst;
}

jl_method_instance_t *JITDebugInfoRegistry::lookupLinfo(size_t pointer)
jl_code_instance_t *JITDebugInfoRegistry::lookupLinfo(size_t pointer)
{
jl_lock_profile();
auto region = linfomap.lower_bound(pointer);
jl_method_instance_t *linfo = NULL;
jl_code_instance_t *linfo = NULL;
if (region != linfomap.end() && pointer < region->first + region->second.first)
linfo = region->second.second;
jl_unlock_profile();
Expand Down Expand Up @@ -372,19 +372,17 @@ void JITDebugInfoRegistry::registerJITObject(const object::ObjectFile &Object,
codeinst_in_flight.erase(codeinst_it);
}
}
jl_method_instance_t *mi = NULL;
if (codeinst) {
JL_GC_PROMISE_ROOTED(codeinst);
mi = codeinst->def;
// Non-opaque-closure MethodInstances are considered globally rooted
// through their methods, but for OC, we need to create a global root
// here.
if (jl_is_method(mi->def.value) && mi->def.method->is_for_opaque_closure)
mi = (jl_method_instance_t*)jl_as_global_root((jl_value_t*)mi, 1);
if (jl_is_method(codeinst->def->def.value) && codeinst->def->def.method->is_for_opaque_closure)
codeinst = (jl_code_instance_t*)jl_as_global_root((jl_value_t*)codeinst, 1);
}
jl_profile_atomic([&]() JL_NOTSAFEPOINT {
if (mi)
linfomap[Addr] = std::make_pair(Size, mi);
if (codeinst)
linfomap[Addr] = std::make_pair(Size, codeinst);
if (first) {
objectmap[SectionLoadAddr] = {&Object,
(size_t)SectionSize,
Expand Down Expand Up @@ -699,7 +697,7 @@ openDebugInfo(StringRef debuginfopath, const debug_link_info &info) JL_NOTSAFEPO
}
extern "C" JL_DLLEXPORT_CODEGEN
void jl_register_fptrs_impl(uint64_t image_base, const jl_image_fptrs_t *fptrs,
jl_method_instance_t **linfos, size_t n)
jl_code_instance_t **linfos, size_t n)
{
getJITDebugRegistry().add_image_info({(uintptr_t) image_base, *fptrs, linfos, n});
}
Expand Down Expand Up @@ -1249,7 +1247,7 @@ extern "C" JL_DLLEXPORT_CODEGEN int jl_getFunctionInfo_impl(jl_frame_t **frames_

extern "C" jl_method_instance_t *jl_gdblookuplinfo(void *p) JL_NOTSAFEPOINT
{
return getJITDebugRegistry().lookupLinfo((size_t)p);
return getJITDebugRegistry().lookupLinfo((size_t)p)->def;
}

#if defined(_OS_DARWIN_) && defined(LLVM_SHLIB)
Expand Down
4 changes: 2 additions & 2 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ typedef struct {
char *func_name;
char *file_name;
int line;
jl_method_instance_t *linfo;
jl_code_instance_t *linfo;
int fromC;
int inlined;
} jl_frame_t;
Expand Down Expand Up @@ -1751,7 +1751,7 @@ JL_DLLIMPORT void jl_get_llvm_external_fns(void *native_code, arraylist_t *gvs);
JL_DLLIMPORT void jl_get_function_id(void *native_code, jl_code_instance_t *ncode,
int32_t *func_idx, int32_t *specfunc_idx);
JL_DLLIMPORT void jl_register_fptrs(uint64_t image_base, const struct _jl_image_fptrs_t *fptrs,
jl_method_instance_t **linfos, size_t n);
jl_code_instance_t **linfos, size_t n);

JL_DLLIMPORT void jl_init_codegen(void);
JL_DLLIMPORT void jl_teardown_codegen(void) JL_NOTSAFEPOINT;
Expand Down
4 changes: 2 additions & 2 deletions src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -2162,7 +2162,7 @@ static void jl_update_all_fptrs(jl_serializer_state *s, jl_image_t *image)
uintptr_t base = (uintptr_t)&s->s->buf[0];
// These will become MethodInstance references, but they start out as a list of
// offsets into `s` for CodeInstances
jl_method_instance_t **linfos = (jl_method_instance_t**)&s->fptr_record->buf[0];
jl_code_instance_t **linfos = (jl_code_instance_t**)&s->fptr_record->buf[0];
uint32_t clone_idx = 0;
for (i = 0; i < img_fvars_max; i++) {
reloc_t offset = *(reloc_t*)&linfos[i];
Expand All @@ -2177,7 +2177,7 @@ static void jl_update_all_fptrs(jl_serializer_state *s, jl_image_t *image)
jl_code_instance_t *codeinst = (jl_code_instance_t*)(base + offset);
assert(jl_is_method(codeinst->def->def.method) && jl_atomic_load_relaxed(&codeinst->invoke) != jl_fptr_const_return);
assert(specfunc ? jl_atomic_load_relaxed(&codeinst->invoke) != NULL : jl_atomic_load_relaxed(&codeinst->invoke) == NULL);
linfos[i] = codeinst->def; // now it's a MethodInstance
linfos[i] = codeinst; // now it's a MethodInstance
void *fptr = fvars.ptrs[i];
for (; clone_idx < fvars.nclones; clone_idx++) {
uint32_t idx = fvars.clone_idxs[clone_idx] & jl_sysimg_val_mask;
Expand Down

0 comments on commit 2479c85

Please sign in to comment.