Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(diagnostics): do not drop samples when stack capture fails

Martin Vogel51621051a040
diff --git a/src/foundation/mem.c b/src/foundation/mem.c
index 43cfb7bf9..56975ca1f 100644
--- a/src/foundation/mem.c
+++ b/src/foundation/mem.c
@@ -883,7 +883,8 @@ void cbm_mem_census_log(const char *tag) {
(void)snprintf(prof_live_kb, sizeof(prof_live_kb), "%zu", profile.live_bytes / CBM_SZ_1K);
(void)snprintf(prof_sites, sizeof(prof_sites), "%zu", profile.sites);
(void)snprintf(prof_lost, sizeof(prof_lost), "%zu",
- profile.site_table_full + profile.pointer_table_full);
+ profile.site_table_full + profile.pointer_table_full +
+ profile.capture_failed);
char profile_path[CBM_SZ_1K];
if (cbm_safe_getenv("CBM_MEM_PROFILE_OUT", profile_path, sizeof(profile_path), NULL) != NULL) {
(void)cbm_mem_profile_dump(profile_path, tag ? tag : "?");
diff --git a/src/foundation/mem_profile.c b/src/foundation/mem_profile.c
index aed51a27c..9c8e4e003 100644
--- a/src/foundation/mem_profile.c
+++ b/src/foundation/mem_profile.c
@@ -114,7 +114,15 @@ static uint64_t profile_hash(void *const *frames, int count) {
static int profile_capture(void **frames) {
#ifdef _WIN32
- return (int)CaptureStackBackTrace(PROFILE_SKIP_FRAMES, PROFILE_FRAMES, frames, NULL);
+ int captured = (int)CaptureStackBackTrace(PROFILE_SKIP_FRAMES, PROFILE_FRAMES, frames, NULL);
+ if (captured > 0) {
+ return captured;
+ }
+ /* Fall back to the immediate return address rather than dropping the
+ * sample: one frame still separates call sites, and a coarse attribution
+ * beats an empty profile that looks like "nothing allocates". */
+ frames[0] = __builtin_return_address(0);
+ return frames[0] != NULL ? 1 : 0;
#else
void *raw[PROFILE_FRAMES + PROFILE_SKIP_FRAMES];
int got = backtrace(raw, PROFILE_FRAMES + PROFILE_SKIP_FRAMES);
@@ -172,6 +180,9 @@ void cbm_mem_profile_alloc(void *block, size_t size) {
void *frames[PROFILE_FRAMES];
int count = profile_capture(frames);
if (count <= 0) {
+ cbm_mutex_lock(&g_profile_mutex);
+ g_totals.capture_failed++;
+ cbm_mutex_unlock(&g_profile_mutex);
g_profile_reentrant = false;
return;
}
diff --git a/src/foundation/mem_profile.h b/src/foundation/mem_profile.h
index cbe1fca93..466b9d895 100644
--- a/src/foundation/mem_profile.h
+++ b/src/foundation/mem_profile.h
@@ -42,6 +42,10 @@ typedef struct {
size_t untracked_frees; /* frees whose pointer was not in the table */
size_t site_table_full; /* records lost: site table exhausted */
size_t pointer_table_full; /* records lost: pointer table exhausted */
+ /* Stack capture returned nothing. On Windows ARM64 CaptureStackBackTrace
+ * can legitimately fail, and dropping those samples silently makes a
+ * working profiler look like an empty one. */
+ size_t capture_failed;
} cbm_mem_profile_totals_t;
void cbm_mem_profile_totals(cbm_mem_profile_totals_t *out);