Skip to content

Commit 54d4f62

Browse files
authored
[android][debugger] Implement support to debug after hot reload. (#55722)
* Implement support to debug after hot reload. * Remove unused variable.
1 parent 617fce4 commit 54d4f62

File tree

7 files changed

+109
-54
lines changed

7 files changed

+109
-54
lines changed

src/mono/mono/component/debugger-agent.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8449,7 +8449,7 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
84498449
ERROR_DECL (error);
84508450
MonoDebugMethodInfo *minfo;
84518451
char *source_file;
8452-
int i, j, n_il_offsets;
8452+
int i, j, n_il_offsets, n_il_offsets_original;
84538453
int *source_files;
84548454
GPtrArray *source_file_list;
84558455
MonoSymSeqPoint *sym_seq_points;
@@ -8472,7 +8472,8 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
84728472
break;
84738473
}
84748474

8475-
mono_debug_get_seq_points (minfo, &source_file, &source_file_list, &source_files, &sym_seq_points, &n_il_offsets);
8475+
mono_debug_get_seq_points (minfo, &source_file, &source_file_list, &source_files, NULL, &n_il_offsets_original);
8476+
mono_debug_get_seq_points (minfo, NULL, NULL, NULL, &sym_seq_points, &n_il_offsets);
84768477
buffer_add_int (buf, header->code_size);
84778478
if (CHECK_PROTOCOL_VERSION (2, 13)) {
84788479
buffer_add_int (buf, source_file_list->len);
@@ -8494,14 +8495,17 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
84948495
const char *srcfile = "";
84958496

84968497
if (source_files [i] != -1) {
8497-
MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, source_files [i]);
8498+
int idx = i;
8499+
if (i >= n_il_offsets_original)
8500+
idx = 0;
8501+
MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, source_files [idx]);
84988502
srcfile = sinfo->source_file;
84998503
}
85008504
PRINT_DEBUG_MSG (10, "IL%x -> %s:%d %d %d %d\n", sp->il_offset, srcfile, sp->line, sp->column, sp->end_line, sp->end_column);
85018505
buffer_add_int (buf, sp->il_offset);
85028506
buffer_add_int (buf, sp->line);
85038507
if (CHECK_PROTOCOL_VERSION (2, 13))
8504-
buffer_add_int (buf, source_files [i]);
8508+
buffer_add_int (buf, i >= n_il_offsets_original ? source_files [0] : source_files [i]);
85058509
if (CHECK_PROTOCOL_VERSION (2, 19))
85068510
buffer_add_int (buf, sp->column);
85078511
if (CHECK_PROTOCOL_VERSION (2, 32)) {

src/mono/mono/component/hot_reload.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "mono/utils/mono-lazy-init.h"
2020
#include "mono/utils/mono-logger-internals.h"
2121
#include "mono/utils/mono-path.h"
22+
#include "mono/metadata/mono-debug.h"
2223

2324

2425
#include <mono/component/hot_reload.h>
@@ -321,13 +322,21 @@ baseline_info_lookup (MonoImage *base_image)
321322
static DeltaInfo*
322323
delta_info_init (MonoImage *image_dmeta, MonoImage *image_base, BaselineInfo *base_info, uint32_t generation);
323324

325+
static void
326+
free_ppdb_entry (gpointer key, gpointer val, gpointer user_data)
327+
{
328+
g_free (val);
329+
}
330+
324331
static void
325332
delta_info_destroy (DeltaInfo *dinfo)
326333
{
327334
if (dinfo->method_table_update)
328335
g_hash_table_destroy (dinfo->method_table_update);
329-
if (dinfo->method_ppdb_table_update)
336+
if (dinfo->method_ppdb_table_update) {
337+
g_hash_table_foreach (dinfo->method_ppdb_table_update, free_ppdb_entry, NULL);
330338
g_hash_table_destroy (dinfo->method_ppdb_table_update);
339+
}
331340
g_free (dinfo);
332341
}
333342

@@ -1182,7 +1191,7 @@ apply_enclog_pass1 (MonoImage *image_base, MonoImage *image_dmeta, gconstpointer
11821191
}
11831192

11841193
static void
1185-
set_update_method (MonoImage *image_base, BaselineInfo *base_info, uint32_t generation, MonoImage *image_dmeta, DeltaInfo *delta_info, uint32_t token_index, const char* il_address, const char* pdb_address)
1194+
set_update_method (MonoImage *image_base, BaselineInfo *base_info, uint32_t generation, MonoImage *image_dmeta, DeltaInfo *delta_info, uint32_t token_index, const char* il_address, MonoDebugInformationEnc* pdb_address)
11861195
{
11871196
mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_METADATA_UPDATE, "setting method 0x%08x in g=%d IL=%p", token_index, generation, (void*)il_address);
11881197
/* FIXME: this is a race if other threads are doing a lookup. */
@@ -1191,7 +1200,7 @@ set_update_method (MonoImage *image_base, BaselineInfo *base_info, uint32_t gene
11911200
g_hash_table_insert (delta_info->method_ppdb_table_update, GUINT_TO_POINTER (token_index), (gpointer) pdb_address);
11921201
}
11931202

1194-
static const char *
1203+
static MonoDebugInformationEnc *
11951204
hot_reload_get_method_debug_information (MonoImage *image_dppdb, int idx)
11961205
{
11971206
if (!image_dppdb)
@@ -1204,21 +1213,15 @@ hot_reload_get_method_debug_information (MonoImage *image_dppdb, int idx)
12041213
mono_metadata_decode_row (table_encmap, i, cols, MONO_ENCMAP_SIZE);
12051214
int map_token = cols [MONO_ENCMAP_TOKEN];
12061215
int token_table = mono_metadata_token_table (map_token);
1207-
if (token_table != MONO_TABLE_METHODBODY)
1208-
continue;
1209-
int token_index = mono_metadata_token_index (map_token);
1210-
if (token_index == idx)
1211-
{
1212-
guint32 cols [MONO_METHODBODY_SIZE];
1213-
MonoTableInfo *methodbody_table = &image_dppdb->tables [MONO_TABLE_METHODBODY];
1214-
mono_metadata_decode_row (methodbody_table, i, cols, MONO_METHODBODY_SIZE);
1215-
if (!cols [MONO_METHODBODY_SEQ_POINTS])
1216-
return NULL;
1217-
1218-
const char *ptr = mono_metadata_blob_heap (image_dppdb, cols [MONO_METHODBODY_SEQ_POINTS]);
1219-
return ptr;
1216+
if (token_table == MONO_TABLE_METHODBODY) {
1217+
int token_index = mono_metadata_token_index (map_token);
1218+
if (token_index == idx) {
1219+
MonoDebugInformationEnc *encDebugInfo = g_new0 (MonoDebugInformationEnc, 1);
1220+
encDebugInfo->idx = i;
1221+
encDebugInfo->image = image_dppdb;
1222+
return encDebugInfo;
1223+
}
12201224
}
1221-
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_METADATA_UPDATE, "pdb encmap i=%d: token=0x%08x (table=%s)", i, map_token, mono_meta_table_name (token_table));
12221225
}
12231226
return NULL;
12241227
}
@@ -1304,7 +1307,7 @@ apply_enclog_pass2 (MonoImage *image_base, BaselineInfo *base_info, uint32_t gen
13041307
int rva = mono_metadata_decode_row_col (&image_dmeta->tables [MONO_TABLE_METHOD], mapped_token - 1, MONO_METHOD_RVA);
13051308
if (rva < dil_length) {
13061309
char *il_address = ((char *) dil_data) + rva;
1307-
const char *method_debug_information = hot_reload_get_method_debug_information (image_dppdb, token_index);
1310+
MonoDebugInformationEnc *method_debug_information = hot_reload_get_method_debug_information (image_dppdb, token_index);
13081311
set_update_method (image_base, base_info, generation, image_dmeta, delta_info, token_index, il_address, method_debug_information);
13091312
} else {
13101313
/* rva points probably into image_base IL stream. can this ever happen? */
@@ -1385,7 +1388,7 @@ hot_reload_apply_changes (MonoImage *image_base, gconstpointer dmeta_bytes, uint
13851388
MonoImage *image_dpdb = NULL;
13861389
if (dpdb_length > 0)
13871390
{
1388-
MonoImage *image_dpdb = image_open_dmeta_from_data (image_base, generation, dpdb_bytes_orig, dpdb_length);
1391+
image_dpdb = image_open_dmeta_from_data (image_base, generation, dpdb_bytes_orig, dpdb_length);
13891392
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_METADATA_UPDATE, "pdb image string size: 0x%08x", image_dpdb->heap_strings.size);
13901393
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_METADATA_UPDATE, "pdb image user string size: 0x%08x", image_dpdb->heap_us.size);
13911394
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_METADATA_UPDATE, "pdb image blob heap addr: %p", image_dpdb->heap_blob.data);

src/mono/mono/metadata/debug-mono-ppdb.c

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -543,19 +543,29 @@ mono_ppdb_get_seq_points_internal (const char* ptr, MonoSymSeqPoint **seq_points
543543

544544
if (n_seq_points) {
545545
*n_seq_points = sps->len;
546-
g_assert (seq_points);
547-
*seq_points = g_new (MonoSymSeqPoint, sps->len);
548-
memcpy (*seq_points, sps->data, sps->len * sizeof (MonoSymSeqPoint));
546+
if (seq_points) {
547+
*seq_points = g_new (MonoSymSeqPoint, sps->len);
548+
memcpy (*seq_points, sps->data, sps->len * sizeof (MonoSymSeqPoint));
549+
}
549550
}
550551
int sps_len = sps->len;
551552
g_array_free (sps, TRUE);
552553
return sps_len;
553554
}
554555

555-
void
556-
mono_ppdb_get_seq_points_enc (const char* ptr, MonoSymSeqPoint **seq_points, int *n_seq_points)
556+
gboolean
557+
mono_ppdb_get_seq_points_enc (MonoImage *image, int idx, MonoSymSeqPoint **seq_points, int *n_seq_points)
557558
{
559+
guint32 cols [MONO_METHODBODY_SIZE];
560+
MonoTableInfo *tables = image->tables;
561+
MonoTableInfo *methodbody_table = &tables [MONO_TABLE_METHODBODY];
562+
mono_metadata_decode_row (methodbody_table, idx, cols, MONO_METHODBODY_SIZE);
563+
if (!cols [MONO_METHODBODY_SEQ_POINTS])
564+
return FALSE;
565+
566+
const char *ptr = mono_metadata_blob_heap (image, cols [MONO_METHODBODY_SEQ_POINTS]);
558567
mono_ppdb_get_seq_points_internal (ptr, seq_points, n_seq_points, 0, NULL, NULL, NULL, NULL, NULL, NULL, FALSE);
568+
return TRUE;
559569
}
560570

561571
void
@@ -619,27 +629,16 @@ mono_ppdb_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrAr
619629

620630
}
621631

622-
MonoDebugLocalsInfo*
623-
mono_ppdb_lookup_locals (MonoDebugMethodInfo *minfo)
632+
static MonoDebugLocalsInfo*
633+
mono_ppdb_lookup_locals_internal (MonoImage *image, int method_idx, gboolean is_enc)
624634
{
625-
MonoPPDBFile *ppdb = minfo->handle->ppdb;
626-
MonoImage *image = ppdb->image;
635+
MonoDebugLocalsInfo *res;
627636
MonoTableInfo *tables = image->tables;
628-
MonoMethod *method = minfo->method;
637+
629638
guint32 cols [MONO_LOCALSCOPE_SIZE];
630639
guint32 locals_cols [MONO_LOCALVARIABLE_SIZE];
631-
int i, lindex, sindex, method_idx, start_scope_idx, scope_idx, locals_idx, locals_end_idx, nscopes;
632-
MonoDebugLocalsInfo *res;
633-
MonoMethodSignature *sig;
634-
635-
if (!method->token)
636-
return NULL;
637-
638-
sig = mono_method_signature_internal (method);
639-
if (!sig)
640-
return NULL;
641-
642-
method_idx = mono_metadata_token_index (method->token);
640+
641+
int i, lindex, sindex, locals_idx, locals_end_idx, nscopes, start_scope_idx, scope_idx;
643642

644643
start_scope_idx = mono_metadata_localscope_from_methoddef (image, method_idx);
645644

@@ -725,6 +724,34 @@ mono_ppdb_lookup_locals (MonoDebugMethodInfo *minfo)
725724
return res;
726725
}
727726

727+
MonoDebugLocalsInfo*
728+
mono_ppdb_lookup_locals_enc (MonoImage *image, int method_idx)
729+
{
730+
return mono_ppdb_lookup_locals_internal (image, method_idx + 1, TRUE);
731+
}
732+
733+
MonoDebugLocalsInfo*
734+
mono_ppdb_lookup_locals (MonoDebugMethodInfo *minfo)
735+
{
736+
MonoPPDBFile *ppdb = minfo->handle->ppdb;
737+
MonoImage *image = ppdb->image;
738+
MonoMethod *method = minfo->method;
739+
int method_idx;
740+
MonoMethodSignature *sig;
741+
742+
if (!method->token)
743+
return NULL;
744+
745+
sig = mono_method_signature_internal (method);
746+
if (!sig)
747+
return NULL;
748+
749+
method_idx = mono_metadata_token_index (method->token);
750+
751+
752+
return mono_ppdb_lookup_locals_internal (image, method_idx, FALSE);
753+
}
754+
728755
/*
729756
* We use this to pass context information to the row locator
730757
*/

src/mono/mono/metadata/debug-mono-ppdb.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ mono_ppdb_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset);
3232
void
3333
mono_ppdb_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points);
3434

35-
void
36-
mono_ppdb_get_seq_points_enc (const char* ptr, MonoSymSeqPoint **seq_points, int *n_seq_points);
35+
gboolean
36+
mono_ppdb_get_seq_points_enc (MonoImage *image, int idx, MonoSymSeqPoint **seq_points, int *n_seq_points);
3737

3838
MonoDebugLocalsInfo*
3939
mono_ppdb_lookup_locals (MonoDebugMethodInfo *minfo);
4040

41+
MonoDebugLocalsInfo*
42+
mono_ppdb_lookup_locals_enc (MonoImage *image, int method_idx);
43+
4144
MonoDebugMethodAsyncInfo*
4245
mono_ppdb_lookup_method_async_debug_info (MonoDebugMethodInfo *minfo);
4346

src/mono/mono/metadata/mono-debug.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,17 @@ mono_debug_lookup_locals (MonoMethod *method, mono_bool ignore_pdb)
860860
{
861861
MonoDebugMethodInfo *minfo;
862862
MonoDebugLocalsInfo *res;
863+
864+
MonoImage* img = m_class_get_image (method->klass);
865+
if (img->has_updates) {
866+
int idx = mono_metadata_token_index (method->token);
867+
MonoDebugInformationEnc *mdie = (MonoDebugInformationEnc *) mono_metadata_update_get_updated_method_ppdb (img, idx);
868+
if (mdie != NULL) {
869+
res = mono_ppdb_lookup_locals_enc (mdie->image, mdie->idx);
870+
if (res != NULL)
871+
return res;
872+
}
873+
}
863874

864875
if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
865876
return NULL;
@@ -1117,12 +1128,12 @@ void
11171128
mono_debug_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points)
11181129
{
11191130
MonoImage* img = m_class_get_image (minfo->method->klass);
1120-
if (img->has_updates) {
1131+
if (img->has_updates && !source_file_list) {
11211132
int idx = mono_metadata_token_index (minfo->method->token);
1122-
gpointer ptr = mono_metadata_update_get_updated_method_ppdb (img, idx);
1123-
if (ptr != NULL) {
1124-
mono_ppdb_get_seq_points_enc (ptr, seq_points, n_seq_points);
1125-
return;
1133+
MonoDebugInformationEnc *mdie = (MonoDebugInformationEnc *) mono_metadata_update_get_updated_method_ppdb (img, idx);
1134+
if (mdie != NULL) {
1135+
if (mono_ppdb_get_seq_points_enc (mdie->image, mdie->idx, seq_points, n_seq_points))
1136+
return;
11261137
}
11271138
}
11281139
if (minfo->handle->ppdb)

src/mono/mono/metadata/mono-debug.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef struct _MonoDebugMethodInfo MonoDebugMethodInfo;
3333
typedef struct _MonoDebugLocalsInfo MonoDebugLocalsInfo;
3434
typedef struct _MonoDebugMethodAsyncInfo MonoDebugMethodAsyncInfo;
3535
typedef struct _MonoDebugSourceLocation MonoDebugSourceLocation;
36+
typedef struct _MonoDebugInformationEnc MonoDebugInformationEnc;
3637

3738
typedef struct _MonoDebugList MonoDebugList;
3839

@@ -110,6 +111,12 @@ struct _MonoDebugSourceLocation {
110111
uint32_t il_offset;
111112
};
112113

114+
115+
struct _MonoDebugInformationEnc {
116+
MonoImage *image;
117+
int idx;
118+
};
119+
113120
MONO_API mono_bool mono_debug_enabled (void);
114121

115122
/*

src/mono/wasm/debugger/DebuggerTestSuite/BreakpointTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ public async Task DebugHotReloadMethodAddBreakpoint()
336336

337337
var locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
338338
CheckNumber(locals, "a", 10);
339-
pause_location = await SendCommandAndCheck(JObject.FromObject(new { }), "Debugger.resume", "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 29, 12, "StaticMethod3");
339+
pause_location = await SendCommandAndCheck(JObject.FromObject(new { }), "Debugger.resume", "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 30, 12, "StaticMethod3");
340340
locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
341341
CheckNumber(locals, "b", 15);
342342

343-
pause_location = await SendCommandAndCheck(JObject.FromObject(new { }), "Debugger.resume", "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 29, 12, "StaticMethod3");
343+
pause_location = await SendCommandAndCheck(JObject.FromObject(new { }), "Debugger.resume", "dotnet://ApplyUpdateReferencedAssembly.dll/MethodBody1.cs", 30, 12, "StaticMethod3");
344344
locals = await GetProperties(pause_location["callFrames"][0]["callFrameId"].Value<string>());
345345
CheckBool(locals, "c", true);
346346
}

0 commit comments

Comments
 (0)