From ccda2ada577477d13fa14c132d8c5f578c881235 Mon Sep 17 00:00:00 2001 From: Julien Gilli Date: Tue, 8 Sep 2015 16:27:44 -0700 Subject: [PATCH] joyent/mdb_v8#23: fix scopeinfo for v8 > 3.28.x --- src/mdb_v8.c | 12 ++++++---- src/mdb_v8_context.c | 53 +++++++++++++++++++++++++++++++++++++++++--- src/mdb_v8_impl.h | 1 + 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/mdb_v8.c b/src/mdb_v8.c index c3d55db..f4ca854 100644 --- a/src/mdb_v8.c +++ b/src/mdb_v8.c @@ -169,6 +169,7 @@ intptr_t V8_CONTEXT_IDX_GLOBAL; intptr_t V8_SCOPEINFO_IDX_NPARAMS; intptr_t V8_SCOPEINFO_IDX_NSTACKLOCALS; +intptr_t V8_SCOPEINFO_OFFSET_STACK_LOCALS; intptr_t V8_SCOPEINFO_IDX_NCONTEXTLOCALS; intptr_t V8_SCOPEINFO_IDX_FIRST_VARS; @@ -355,14 +356,17 @@ static v8_constant_t v8_constants[] = { V8_CONSTANT_FALLBACK(0, 0), 3 }, { &V8_SCOPEINFO_IDX_NPARAMS, "v8dbg_scopeinfo_idx_nparams", - V8_CONSTANT_FALLBACK(0, 0), 1 }, + V8_CONSTANT_FALLBACK(3, 7), 1 }, { &V8_SCOPEINFO_IDX_NSTACKLOCALS, "v8dbg_scopeinfo_idx_nstacklocals", - V8_CONSTANT_FALLBACK(0, 0), 2 }, + V8_CONSTANT_FALLBACK(3, 7), 2 }, + { &V8_SCOPEINFO_OFFSET_STACK_LOCALS, + "v8dbg_scopeinfo_offset_stack_locals", + V8_CONSTANT_ADDED_SINCE(4, 4) }, { &V8_SCOPEINFO_IDX_NCONTEXTLOCALS, "v8dbg_scopeinfo_idx_ncontextlocals", - V8_CONSTANT_FALLBACK(0, 0), 3 }, + V8_CONSTANT_FALLBACK(3, 7), 3 }, { &V8_SCOPEINFO_IDX_FIRST_VARS, "v8dbg_scopeinfo_idx_first_vars", - V8_CONSTANT_FALLBACK(0, 0), 4 }, + V8_CONSTANT_FALLBACK(3, 7), 4 }, }; static int v8_nconstants = sizeof (v8_constants) / sizeof (v8_constants[0]); diff --git a/src/mdb_v8_context.c b/src/mdb_v8_context.c index 03f025d..0478a7a 100644 --- a/src/mdb_v8_context.c +++ b/src/mdb_v8_context.c @@ -67,12 +67,13 @@ typedef struct { v8scopeinfo_vartype_t v8vti_vartype; const char *v8vti_label; intptr_t *v8vti_idx_countp; + intptr_t *v8vti_offset; } v8scopeinfo_vartype_info_t; static v8scopeinfo_vartype_info_t v8scopeinfo_vartypes[] = { { V8SV_PARAMS, "parameter", &V8_SCOPEINFO_IDX_NPARAMS }, { V8SV_STACKLOCALS, "stack local variable", - &V8_SCOPEINFO_IDX_NSTACKLOCALS }, + &V8_SCOPEINFO_IDX_NSTACKLOCALS, &V8_SCOPEINFO_OFFSET_STACK_LOCALS }, { V8SV_CONTEXTLOCALS, "context local variable", &V8_SCOPEINFO_IDX_NCONTEXTLOCALS }, }; @@ -385,13 +386,59 @@ v8scopeinfo_iter_vars(v8scopeinfo_t *sip, assert(vtip != NULL); nvars = v8scopeinfo_vartype_nvars(sip, scopevartype); + /* + * A ScopeInfo instance has two distinct parts: + * + * 1) A static part that contains information about the number of + * entries for each variable type. + * + * 2) A dynamic part of variable size that contains the actual data for + * each variable type (parameters, stack local and context local + * entries' names). + * + * V8_SCOPEINFO_IDX_FIRST_VARS is the offset from the beginning of the + * ScopeInfo layout to the start of the variable part that contains the + * actual information for each variable type, so we start by skipping to + * that offset. + */ nskip = V8_SCOPEINFO_IDX_FIRST_VARS; + + /* + * Iterate over variable types so that we can add the offset from the + * beginning of the actual data (the dynamic part) to the region of the + * dynamic part that is specific to the variable type we're interested + * in. + */ for (i = 0; i < v8scopeinfo_nvartypes; i++) { ogrp = &v8scopeinfo_vartypes[i]; - if (*(ogrp->v8vti_idx_countp) >= *(vtip->v8vti_idx_countp)) { - continue; + + /* + * In the variable/dynamic part of a ScopeInfo layout, some + * variable types have static metadata, e.g stack local entries + * have a StackLocalFirstSlot, before the actual data. Add that + * offset for each variable type, including for the one we're + * interested in. + */ + if (v8scopeinfo_vartypes[i].v8vti_offset != NULL && + *(v8scopeinfo_vartypes[i].v8vti_offset) != -1) { + nskip += *(v8scopeinfo_vartypes[i].v8vti_offset); + } + + /* + * The current variable type is the one we're interested in, + * do not add anything to the offset, we're done. + */ + if (*(ogrp->v8vti_idx_countp) == *(vtip->v8vti_idx_countp)) { + break; } + /* + * The data for the current variable type is before the one + * we're interested in in the variable part of the ScopeInfo + * layout. + * Add the number of entries for this variable type to the + * offset. + */ nskip += v8scopeinfo_vartype_nvars(sip, ogrp->v8vti_vartype); } diff --git a/src/mdb_v8_impl.h b/src/mdb_v8_impl.h index fac16e5..c4fd66a 100644 --- a/src/mdb_v8_impl.h +++ b/src/mdb_v8_impl.h @@ -62,6 +62,7 @@ extern intptr_t V8_SCOPEINFO_IDX_FIRST_VARS; extern intptr_t V8_SCOPEINFO_IDX_NCONTEXTLOCALS; extern intptr_t V8_SCOPEINFO_IDX_NPARAMS; extern intptr_t V8_SCOPEINFO_IDX_NSTACKLOCALS; +extern intptr_t V8_SCOPEINFO_OFFSET_STACK_LOCALS; extern intptr_t V8_HeapObjectTag; extern intptr_t V8_HeapObjectTagMask;