Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/7.0-staging] [MONO] Add JIT_CODE_DEBUG_INFO record functionality for Jitdump #87428

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 91 additions & 3 deletions src/mono/mono/mini/mini-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <mono/metadata/threads.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/debug-internals.h>
#include <mono/metadata/domain-internals.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/mono-config.h>
Expand Down Expand Up @@ -2023,7 +2024,7 @@ static clockid_t clock_id = CLOCK_MONOTONIC;

enum {
JIT_DUMP_MAGIC = 0x4A695444,
JIT_DUMP_VERSION = 2,
JIT_DUMP_VERSION = 1,
#if HOST_X86
ELF_MACHINE = EM_386,
#elif HOST_AMD64
Expand All @@ -2039,7 +2040,8 @@ enum {
#elif HOST_RISCV
ELF_MACHINE = EM_RISCV,
#endif
JIT_CODE_LOAD = 0
JIT_CODE_LOAD = 0,
JIT_DEBUG_INFO = 2
};
typedef struct
{
Expand Down Expand Up @@ -2070,7 +2072,22 @@ typedef struct
// Null terminated function name
// Native code
} JitCodeLoadRecord;
typedef struct
{
guint64 code_addr;
guint32 line;
guint32 discrim;
char name[];
} DebugEntry;
typedef struct
{
RecordHeader header;
guint64 code_addr;
guint64 nr_entry;
DebugEntry debug_entry[];
} JitCodeDebug;

static void add_basic_JitCodeDebug_info (JitCodeDebug *record);
static void add_file_header_info (FileHeader *header);
static void add_basic_JitCodeLoadRecord_info (JitCodeLoadRecord *record);

Expand Down Expand Up @@ -2136,7 +2153,72 @@ mono_emit_jit_dump (MonoJitInfo *jinfo, gpointer code)

record.code_index = ++code_index;

// TODO: write debugInfo and unwindInfo immediately before the JitCodeLoadRecord (while lock is held).
DebugEntry ent;
JitCodeDebug rec;
MonoDebugMethodInfo *minfo;
MonoDebugMethodJitInfo *dmji;
MonoDebugSourceLocation *loc;
int i;

memset (&rec, 0, sizeof (rec));

//populating info relating debug methods
minfo = mono_debug_lookup_method (jinfo->d.method);
dmji = mono_debug_find_method (jinfo->d.method, NULL);

add_basic_JitCodeDebug_info (&rec);
rec.code_addr = (guint64)dmji->code_start;
rec.header.total_size = sizeof (rec) + sizeof (ent) + 1;
rec.nr_entry = 1;
for (i = 0; i < dmji->num_line_numbers; ++i){

loc = mono_debug_lookup_source_location_by_il (jinfo->d.method, dmji->line_numbers[i].il_offset, NULL);

if(!loc)
continue;

if(!loc->source_file){
mono_debug_free_source_location (loc);
continue;
}

rec.header.total_size += sizeof (ent) + strlen (loc->source_file) + 1;
rec.nr_entry++;
}

fwrite (&rec, sizeof (rec), 1, perf_dump_file);


for( i = 0; i < dmji->num_line_numbers; ++i){

//get the line number using il offset
loc = mono_debug_lookup_source_location_by_il (jinfo->d.method, dmji->line_numbers[i].il_offset, NULL);

if(!loc)
continue;

if(!loc->source_file){

mono_debug_free_source_location (loc);
continue;
}

ent.code_addr = (guint64)dmji->code_start + dmji->line_numbers[i].native_offset;
ent.discrim = 0;
ent.line = (guint32)loc->row;

fwrite (&ent, sizeof(ent), 1, perf_dump_file);
fwrite (loc->source_file, strlen (loc->source_file) + 1, 1, perf_dump_file);
}


ent.code_addr = (guint64)jinfo->code_start + jinfo->code_size;
ent.discrim = 0;
ent.line = 0;
fwrite (&ent, sizeof (ent), 1, perf_dump_file);
fwrite ("", 1, 1, perf_dump_file);

// TODO: write unwindInfo immediately before the JitCodeLoadRecord (while lock is held).

record.header.timestamp = mono_clock_get_time_ns (clock_id);

Expand All @@ -2147,7 +2229,13 @@ mono_emit_jit_dump (MonoJitInfo *jinfo, gpointer code)
mono_os_mutex_unlock (&perf_dump_mutex);
}
}
static void
add_basic_JitCodeDebug_info (JitCodeDebug *record)
{
record->header.id = JIT_DEBUG_INFO;
record->header.timestamp = mono_clock_get_time_ns (clock_id);

}
static void
add_basic_JitCodeLoadRecord_info (JitCodeLoadRecord *record)
{
Expand Down