Skip to content

Commit

Permalink
Show fuzzy-stack-hash for crashes in debug build.
Browse files Browse the repository at this point in the history
  • Loading branch information
NikLeberg committed Dec 12, 2024
1 parent 72b5e72 commit 989e244
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
17 changes: 17 additions & 0 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#endif

#define MAX_TRACE_DEPTH 25
#define MAX_HASH_DEPTH 10

struct debug_info {
A(debug_frame_t*) frames;
Expand Down Expand Up @@ -926,6 +927,22 @@ const debug_frame_t *debug_get_frame(debug_info_t *di, unsigned n)
return AGET(di->frames, n);
}

hash_state_t debug_hash(debug_info_t *di)
{
hash_state_t hash = HASH_INIT;
int hash_depth = 0;

const int nframes = debug_count_frames(di);
for (int n = 1; n < nframes; n++) {
const debug_frame_t *f = debug_get_frame(di, n);
if (f->kind == FRAME_PROG && f->symbol != NULL
&& hash_depth++ < MAX_HASH_DEPTH)
hash_update(&hash, f->symbol, strlen(f->symbol));
}

return hash;
}

void debug_add_unwinder(void *start, size_t len, debug_unwind_fn_t fn,
void *context)
{
Expand Down
1 change: 1 addition & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ debug_info_t *debug_capture(void);
void debug_free(debug_info_t *di);
unsigned debug_count_frames(debug_info_t *di);
const debug_frame_t *debug_get_frame(debug_info_t *di, unsigned n);
hash_state_t debug_hash(debug_info_t *di);

void debug_add_unwinder(void *start, size_t len, debug_unwind_fn_t fn,
void *context);
Expand Down
17 changes: 0 additions & 17 deletions src/ident.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#include <ctype.h>
#include <limits.h>

#define HASH_INIT 5381;
typedef uint32_t hash_state_t;

#define INITIAL_SIZE 1024
#define REPROBE_LIMIT 20
#define MOVED_TAG 1
Expand Down Expand Up @@ -68,20 +65,6 @@ typedef struct {
static ident_tab_t *table = NULL;
static ident_tab_t *resizing = NULL;

static inline int hash_update(hash_state_t *state, const char *key, int nchars)
{
// DJB2 hash function from here:
// http://www.cse.yorku.ca/~oz/hash.html

hash_state_t hash = *state;
const char *p = key;
for (; p < key + nchars && *p; p++)
hash = ((hash << 5) + hash) + *p;

*state = hash;
return p - key;
}

static ident_t ident_alloc(size_t len, hash_state_t hash)
{
ident_t id = xmalloc_flex(sizeof(struct _ident), len + 1, sizeof(char));
Expand Down
18 changes: 18 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,10 @@ void show_stacktrace(void)

}

#ifdef DEBUG
color_fprintf(stderr, "\n$!red$Crash hash: %08x$$\n", debug_hash(di));
#endif

debug_free(di);

#if defined __linux__ && !defined HAVE_LIBDW && !defined HAVE_LIBDWARF
Expand Down Expand Up @@ -2491,3 +2495,17 @@ void pool_stats(mem_pool_t *mp, size_t *alloc, size_t *npages)
*alloc += p->alloc - sizeof(pool_page_t);
}
}

int hash_update(hash_state_t *state, const char *key, int nchars)
{
// DJB2 hash function from here:
// http://www.cse.yorku.ca/~oz/hash.html

hash_state_t hash = *state;
const char *p = key;
for (; p < key + nchars && *p; p++)
hash = ((hash << 5) + hash) + *p;

*state = hash;
return p - key;
}
5 changes: 5 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,9 @@ void list_clear(ptr_list_t *l);
__tmp; \
})

#define HASH_INIT 5381;
typedef uint32_t hash_state_t;

int hash_update(hash_state_t *state, const char *key, int nchars);

#endif // _UTIL_H

0 comments on commit 989e244

Please sign in to comment.