Browse Source

Ajduk alloc logging (--ajsheap-log)

pull/195/head
Sami Vaarala 10 years ago
parent
commit
0b49540014
  1. 20
      examples/cmdline/duk_cmdline.c
  2. 74
      examples/cmdline/duk_cmdline_ajduk.c

20
examples/cmdline/duk_cmdline.c

@ -50,6 +50,9 @@ void ajsheap_dump(void);
void ajsheap_register(duk_context *ctx);
void ajsheap_start_exec_timeout(void);
void ajsheap_clear_exec_timeout(void);
void *ajsheap_alloc_wrapped(void *udata, duk_size_t size);
void *ajsheap_realloc_wrapped(void *udata, void *ptr, duk_size_t size);
void ajsheap_free_wrapped(void *udata, void *ptr);
void *AJS_Alloc(void *udata, duk_size_t size);
void *AJS_Realloc(void *udata, void *ptr, duk_size_t size);
void AJS_Free(void *udata, void *ptr);
@ -467,12 +470,14 @@ int main(int argc, char *argv[]) {
int interactive = 0;
int memlimit_high = 1;
int alloc_provider = ALLOC_DEFAULT;
int ajsheap_log = 0;
int debugger = 0;
int i;
#ifdef DUK_CMDLINE_AJSHEAP
alloc_provider = ALLOC_AJSHEAP;
#endif
(void) ajsheap_log;
/*
* Signal handling setup
@ -514,6 +519,8 @@ int main(int argc, char *argv[]) {
alloc_provider = ALLOC_HYBRID;
} else if (strcmp(arg, "--alloc-ajsheap") == 0) {
alloc_provider = ALLOC_AJSHEAP;
} else if (strcmp(arg, "--ajsheap-log") == 0) {
ajsheap_log = 1;
} else if (strcmp(arg, "--debugger") == 0) {
debugger = 1;
} else if (strlen(arg) >= 1 && arg[0] == '-') {
@ -590,11 +597,13 @@ int main(int argc, char *argv[]) {
#ifdef DUK_CMDLINE_AJSHEAP
ajsheap_init();
ctx = duk_create_heap(AJS_Alloc,
AJS_Realloc,
AJS_Free,
(void *) 0xdeadbeef, /* heap_udata: ignored by AjsHeap, use as marker */
NULL); /* fatal_handler */
ctx = duk_create_heap(
ajsheap_log ? ajsheap_alloc_wrapped : AJS_Alloc,
ajsheap_log ? ajsheap_realloc_wrapped : AJS_Realloc,
ajsheap_log ? ajsheap_free_wrapped : AJS_Free,
(void *) 0xdeadbeef, /* heap_udata: ignored by AjsHeap, use as marker */
NULL
); /* fatal_handler */
#else
fprintf(stderr, "Warning: option --alloc-ajsheap ignored, no ajsheap allocator support\n");
fflush(stderr);
@ -754,6 +763,7 @@ int main(int argc, char *argv[]) {
#endif
#ifdef DUK_CMDLINE_AJSHEAP
" --alloc-ajsheap use ajsheap allocator (enabled by default with 'ajduk')\n"
" --ajsheap-log write alloc log to /tmp/ajduk-alloc-log.txt\n"
#endif
#ifdef DUK_CMDLINE_DEBUGGER_SUPPORT
" --debugger start example debugger\n"

74
examples/cmdline/duk_cmdline_ajduk.c

@ -139,6 +139,80 @@ void ajsheap_register(duk_context *ctx) {
duk_put_global_string(ctx, "AjsHeap");
}
/*
* Wrapped ajs_heap.c alloc functions
*
* Used to write an alloc log.
*/
static FILE *ajsheap_alloc_log = NULL;
static void ajsheap_write_alloc_log(const char *fmt, ...) {
va_list ap;
char buf[256];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
buf[sizeof(buf) - 1] = (char) 0;
va_end(ap);
if (ajsheap_alloc_log == NULL) {
ajsheap_alloc_log = fopen("/tmp/ajduk-alloc-log.txt", "wb");
if (ajsheap_alloc_log == NULL) {
fprintf(stderr, "WARNING: failed to write alloc log, ignoring\n");
fflush(stderr);
return;
}
}
(void) fwrite((const void *) buf, 1, strlen(buf), ajsheap_alloc_log);
(void) fflush(ajsheap_alloc_log);
}
void *ajsheap_alloc_wrapped(void *udata, duk_size_t size) {
void *ret = AJS_Alloc(udata, size);
if (size > 0 && ret == NULL) {
ajsheap_write_alloc_log("A FAIL %ld\n", (long) size);
} else if (ret == NULL) {
ajsheap_write_alloc_log("A NULL %ld\n", (long) size);
} else {
ajsheap_write_alloc_log("A %p %ld\n", ret, (long) size);
}
return ret;
}
void *ajsheap_realloc_wrapped(void *udata, void *ptr, duk_size_t size) {
void *ret = AJS_Realloc(udata, ptr, size);
if (size > 0 && ret == NULL) {
if (ptr == NULL) {
ajsheap_write_alloc_log("R NULL -1 FAIL %ld\n", (long) size);
} else {
ajsheap_write_alloc_log("R %p -1 FAIL %ld\n", ptr, (long) size);
}
} else if (ret == NULL) {
if (ptr == NULL) {
ajsheap_write_alloc_log("R NULL -1 NULL %ld\n", (long) size);
} else {
ajsheap_write_alloc_log("R %p -1 NULL %ld\n", ptr, (long) size);
}
} else {
if (ptr == NULL) {
ajsheap_write_alloc_log("R NULL -1 %p %ld\n", ret, (long) size);
} else {
ajsheap_write_alloc_log("R %p -1 %p %ld\n", ptr, ret, (long) size);
}
}
return ret;
}
void ajsheap_free_wrapped(void *udata, void *ptr) {
AJS_Free(udata, ptr);
if (ptr == NULL) {
} else {
ajsheap_write_alloc_log("F %p -1\n", ptr);
}
}
/*
* Example pointer compression functions.
*

Loading…
Cancel
Save