Adding optional stack traces on potential GC misses detected by conservative tracing.

This commit is contained in:
Alex Shinn 2014-01-30 22:18:23 +09:00
parent b39fa4d060
commit 74162906f3
4 changed files with 38 additions and 0 deletions

13
gc.c
View file

@ -223,6 +223,18 @@ int stack_references_pointer_p (sexp ctx, sexp x) {
return 0;
}
#if SEXP_USE_TRACK_ALLOC_BACKTRACE
void sexp_print_gc_trace(sexp ctx, sexp p) {
int i;
char **debug_text = backtrace_symbols(p->backtrace, SEXP_BACKTRACE_SIZE);
for (i=0; i < SEXP_BACKTRACE_SIZE; i++)
fprintf(stderr, SEXP_BANNER(" : %s"), debug_text[i]);
free(debug_text);
}
#else
#define sexp_print_gc_trace(ctx, p)
#endif
void sexp_conservative_mark (sexp ctx) {
sexp_heap h = sexp_context_heap(ctx);
sexp p, end;
@ -247,6 +259,7 @@ void sexp_conservative_mark (sexp ctx) {
if (p && sexp_pointerp(p)) {
fprintf(stderr, SEXP_BANNER("MISS: %p [%d]: %s"), p,
sexp_pointer_tag(p), sexp_pointer_source(p));
sexp_print_gc_trace(ctx, p);
fflush(stderr);
}
#endif

View file

@ -87,6 +87,10 @@
/* uncomment this to track what C source line each object is allocated from */
/* #define SEXP_USE_TRACK_ALLOC_SOURCE 1 */
/* uncomment this to take a short backtrace of where each object is */
/* allocated from */
/* #define SEXP_USE_TRACK_ALLOC_BACKTRACE 1 */
/* uncomment this to add additional native gc checks to verify a magic header */
/* #define SEXP_USE_HEADER_MAGIC 1 */
@ -372,6 +376,14 @@
#define SEXP_USE_TRACK_ALLOC_SOURCE SEXP_USE_DEBUG_GC > 2
#endif
#ifndef SEXP_USE_TRACK_ALLOC_BACKTRACE
#define SEXP_USE_TRACK_ALLOC_BACKTRACE SEXP_USE_TRACK_ALLOC_SOURCE
#endif
#ifndef SEXP_BACKTRACE_SIZE
#define SEXP_BACKTRACE_SIZE 3
#endif
#ifndef SEXP_USE_HEADER_MAGIC
#define SEXP_USE_HEADER_MAGIC 0
#endif

View file

@ -75,6 +75,10 @@ typedef unsigned long size_t;
#endif
#endif
#if SEXP_USE_TRACK_ALLOC_BACKTRACE
#include <execinfo.h>
#endif
#include <ctype.h>
#include <stdio.h>
@ -294,6 +298,7 @@ struct sexp_struct {
unsigned int syntacticp:1;
#if SEXP_USE_TRACK_ALLOC_SOURCE
const char* source;
void* backtrace[SEXP_BACKTRACE_SIZE];
#endif
#if SEXP_USE_HEADER_MAGIC
unsigned int magic;

8
sexp.c
View file

@ -62,11 +62,19 @@ sexp sexp_push_op(sexp ctx, sexp* loc, sexp x) {
#endif
sexp sexp_alloc_tagged_aux(sexp ctx, size_t size, sexp_uint_t tag sexp_current_source_param) {
#if SEXP_USE_TRACK_ALLOC_BACKTRACE
int i;
void* trace[SEXP_BACKTRACE_SIZE + 1];
#endif
sexp res = (sexp) sexp_alloc(ctx, size);
if (res && ! sexp_exceptionp(res)) {
sexp_pointer_tag(res) = tag;
#if SEXP_USE_TRACK_ALLOC_SOURCE
sexp_pointer_source(res) = source;
#if SEXP_USE_TRACK_ALLOC_BACKTRACE
backtrace(trace, SEXP_BACKTRACE_SIZE + 1);
for (i=0; i<SEXP_BACKTRACE_SIZE; i++) res->backtrace[i] = trace[i+1];
#endif
#endif
#if SEXP_USE_HEADER_MAGIC
sexp_pointer_magic(res) = SEXP_POINTER_MAGIC;