Skip to content

Commit 69eaabf

Browse files
committed
better stats, better err handling
1 parent 40cf8a3 commit 69eaabf

7 files changed

Lines changed: 40 additions & 30 deletions

File tree

Lib/test/test_external_inspection.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,6 @@ def test_get_stats(self):
37773777
"alias_remap_failures",
37783778
"alias_validation_fails",
37793779
"alias_evictions",
3780-
"alias_identity_mismatches",
37813780
"alias_probe_checks",
37823781
"alias_probe_recycles",
37833782
"batched_read_success_rate",

Modules/_remote_debugging/_remote_debugging.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ typedef struct {
310310
uint64_t alias_remap_failures; // macOS remap/protect failures
311311
uint64_t alias_validation_fails; // macOS alias snapshot validation failures
312312
uint64_t alias_evictions; // macOS alias-cache LRU evictions
313-
uint64_t alias_identity_mismatches; // macOS target identity mismatches
314313
uint64_t alias_probe_checks; // macOS alias object identity probes
315314
uint64_t alias_probe_recycles; // macOS alias recycled-page detections
316315
} UnwinderStats;
@@ -695,6 +694,10 @@ extern int _Py_RemoteDebug_ValidateThreadStateSnapshot(
695694
uintptr_t tstate_addr,
696695
uintptr_t current_interpreter
697696
);
697+
/* Returns 0 if bytes were served from an aliased page snapshot (callers
698+
* should validate), 1 if no snapshot was involved (live syscall read or
699+
* zero-length no-op; skip validation), -1 on error with an exception
700+
* set. */
698701
extern int _Py_RemoteDebug_AliasedRead(
699702
RemoteUnwinderObject *unwinder,
700703
uintptr_t remote_addr,

Modules/_remote_debugging/alias_read.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,27 +259,36 @@ _Py_RemoteDebug_AliasedRead(RemoteUnwinderObject *unwinder,
259259
{
260260
AliasReadCache *cache = &unwinder->alias_cache;
261261
if (len == 0) {
262-
return 0;
262+
return 1;
263263
}
264264
if (cache->disabled) {
265-
return _Py_RemoteDebug_ReadRemoteMemory(
266-
&unwinder->handle, remote_addr, len, dst);
265+
if (_Py_RemoteDebug_ReadRemoteMemory(
266+
&unwinder->handle, remote_addr, len, dst) < 0) {
267+
return -1;
268+
}
269+
return 1;
267270
}
268271

269272
size_t page_size = (size_t)unwinder->handle.page_size;
270273
uintptr_t page_base = remote_addr & ~(uintptr_t)(page_size - 1);
271274
size_t offset = (size_t)(remote_addr - page_base);
272275
if (offset >= page_size || len > page_size - offset) {
273-
return _Py_RemoteDebug_ReadRemoteMemory(
274-
&unwinder->handle, remote_addr, len, dst);
276+
if (_Py_RemoteDebug_ReadRemoteMemory(
277+
&unwinder->handle, remote_addr, len, dst) < 0) {
278+
return -1;
279+
}
280+
return 1;
275281
}
276282

277283
AliasPageEntry *entry = alias_find_entry(unwinder, page_base);
278284
if (entry != NULL) {
279285
int probe = alias_maybe_probe_entry(unwinder, entry);
280286
if (probe < 0) {
281-
return _Py_RemoteDebug_ReadRemoteMemory(
282-
&unwinder->handle, remote_addr, len, dst);
287+
if (_Py_RemoteDebug_ReadRemoteMemory(
288+
&unwinder->handle, remote_addr, len, dst) < 0) {
289+
return -1;
290+
}
291+
return 1;
283292
}
284293
if (probe == 0) {
285294
PyErr_SetString(PyExc_RuntimeError,
@@ -296,8 +305,11 @@ _Py_RemoteDebug_AliasedRead(RemoteUnwinderObject *unwinder,
296305

297306
STATS_INC(unwinder, alias_misses);
298307
if (alias_remap_page(unwinder, page_base, &entry) < 0) {
299-
return _Py_RemoteDebug_ReadRemoteMemory(
300-
&unwinder->handle, remote_addr, len, dst);
308+
if (_Py_RemoteDebug_ReadRemoteMemory(
309+
&unwinder->handle, remote_addr, len, dst) < 0) {
310+
return -1;
311+
}
312+
return 1;
301313
}
302314
memcpy(dst, (const char *)entry->local_page_base + offset, len);
303315
return 0;

Modules/_remote_debugging/clinic/module.c.h

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_remote_debugging/frames.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,22 @@ parse_frame_object_aliased(
316316
uintptr_t *previous_frame)
317317
{
318318
char frame[SIZEOF_INTERP_FRAME];
319-
if (_Py_RemoteDebug_AliasedRead(
320-
unwinder, address, SIZEOF_INTERP_FRAME,
321-
frame) < 0) {
319+
int rc = _Py_RemoteDebug_AliasedRead(
320+
unwinder, address, SIZEOF_INTERP_FRAME, frame);
321+
if (rc < 0) {
322322
set_exception_cause(unwinder, PyExc_RuntimeError,
323323
"Failed to read interpreter frame");
324324
return -1;
325325
}
326-
STATS_INC(unwinder, memory_reads);
327-
STATS_ADD(unwinder, memory_bytes_read, SIZEOF_INTERP_FRAME);
328326

329-
if (!validate_frame_snapshot(unwinder, frame, expected_parent)) {
327+
if (rc == 0 && !validate_frame_snapshot(unwinder, frame, expected_parent)) {
330328
STATS_INC(unwinder, alias_validation_fails);
331329
return parse_frame_object(unwinder, result, address,
332330
address_of_code_object, previous_frame);
333331
}
334332

333+
STATS_INC(unwinder, memory_reads);
334+
STATS_ADD(unwinder, memory_bytes_read, SIZEOF_INTERP_FRAME);
335335
return parse_frame_buffer(unwinder, result, frame,
336336
address_of_code_object, previous_frame);
337337
}

Modules/_remote_debugging/module.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,6 @@ RemoteUnwinder was created with stats=True.
11831183
- alias_validation_fails: macOS alias snapshot validation
11841184
failures
11851185
- alias_evictions: macOS alias-cache LRU evictions
1186-
- alias_identity_mismatches: macOS target identity
1187-
mismatches
11881186
- alias_probe_checks: macOS alias object identity probes
11891187
- alias_probe_recycles: macOS alias recycled-page detections
11901188
- frame_cache_hit_rate: Percentage of samples that hit the
@@ -1202,7 +1200,7 @@ RemoteUnwinder was created with stats=True.
12021200

12031201
static PyObject *
12041202
_remote_debugging_RemoteUnwinder_get_stats_impl(RemoteUnwinderObject *self)
1205-
/*[clinic end generated code: output=21e36477122be2a0 input=d9bce4858ca7cbc7]*/
1203+
/*[clinic end generated code: output=21e36477122be2a0 input=913e10ed7cabd40d]*/
12061204
{
12071205
if (!self->collect_stats) {
12081206
PyErr_SetString(PyExc_RuntimeError,
@@ -1247,7 +1245,6 @@ _remote_debugging_RemoteUnwinder_get_stats_impl(RemoteUnwinderObject *self)
12471245
ADD_STAT(alias_remap_failures);
12481246
ADD_STAT(alias_validation_fails);
12491247
ADD_STAT(alias_evictions);
1250-
ADD_STAT(alias_identity_mismatches);
12511248
ADD_STAT(alias_probe_checks);
12521249
ADD_STAT(alias_probe_recycles);
12531250

Modules/_remote_debugging/threads.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,15 @@ read_thread_state_and_maybe_frame(
303303
*frame_read = 0;
304304
#if defined(__APPLE__) && TARGET_OS_OSX
305305
if (unwinder->cache_frames) {
306-
if (_Py_RemoteDebug_AliasedRead(
307-
unwinder,
308-
tstate_addr,
309-
tstate_size,
310-
tstate_buffer) < 0) {
306+
int rc = _Py_RemoteDebug_AliasedRead(
307+
unwinder,
308+
tstate_addr,
309+
tstate_size,
310+
tstate_buffer);
311+
if (rc < 0) {
311312
return -1;
312313
}
313-
if (!_Py_RemoteDebug_ValidateThreadStateSnapshot(
314+
if (rc == 0 && !_Py_RemoteDebug_ValidateThreadStateSnapshot(
314315
unwinder, tstate_buffer, tstate_addr,
315316
current_interpreter)) {
316317
STATS_INC(unwinder, alias_validation_fails);

0 commit comments

Comments
 (0)