Skip to content

Commit

Permalink
utils: Introduce ASSERT and DASSERT debugging features
Browse files Browse the repository at this point in the history
ASSERT is same as assert, but dumps stacktrace before stopped.

However, DASSERT is only compiled when DEBUG=1 is on in debug mode build.
Otherwise, it's simply ignored and doesn't make any overhead.

It would be better if we add more assertion like statements with ASSERT
and DASSERT.

When one of the assertion is failed, it prints the stack trace and
raises SIGTRAP signal so that it can be handled by a debugger, otherwise
it will just stop execution anyway.

Here is the example output.

  /data_sdb/honggyu/work/uftrace/git/uftrace/utils/fstack.c:2025: fstack_update_stack_count: ASSERT `!task' failed.
  Stack trace:
    #1  0x0000004559f2 fstack_update_stack_count + 0x66
    #2  0x000000456187 __fstack_consume + 0x48f
    #3  0x000000456c68 __read_rstack + 0x74e
    #4  0x000000456cc1 read_rstack + 0x28
    #5  0x000000417386 command_replay + 0x28e
    #6  0x000000407bf9 main + 0x4c3
    #7  0x7fd2a7bee840 __libc_start_main + 0xf0
    #8  0x0000004054e9 _start + 0x29
    #9  0x000000000000  + 0x29

  Please report this bug to https://github.com/namhyung/uftrace/issues.

  Trace/breakpoint trap

Signed-off-by: Honggyu Kim <[email protected]>
  • Loading branch information
honggyukim committed Jul 11, 2021
1 parent 76027ad commit 100fe93
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libmcount/mcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ static void segv_handler(int sig, siginfo_t *si, void *ctx)
rstack--;
}

pr_red("\nPlease report this bug to https://github.com/namhyung/uftrace/issues.\n\n");
pr_out("\n");
pr_red(BUG_REPORT_MSG);

out:
sigaction(sig, &old_sigact[(sig == SIGSEGV)], NULL);
Expand Down
21 changes: 21 additions & 0 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <signal.h>

#include "compiler.h"

Expand Down Expand Up @@ -44,6 +45,8 @@
#define NSEC_PER_SEC 1000000000
#define NSEC_PER_MSEC 1000000

#define BUG_REPORT_MSG "Please report this bug to https://github.com/namhyung/uftrace/issues.\n\n"

extern int debug;
extern FILE *logfp;
extern FILE *outfp;
Expand Down Expand Up @@ -369,4 +372,22 @@ char *absolute_dirname(const char *path, char *resolved_path);

void stacktrace(void);

#define ASSERT(cond) \
if (unlikely(!(cond))) { \
pr_red("%s:%d: %s: ASSERT `%s' failed.\n", \
__FILE__, __LINE__, __func__, #cond); \
stacktrace(); \
pr_red(BUG_REPORT_MSG); \
raise(SIGTRAP); \
}

#define DASSERT(cond) \
if (DEBUG_MODE && unlikely(!(cond))) { \
pr_red("%s:%d: %s: DEBUG ASSERT `%s' failed.\n", \
__FILE__, __LINE__, __func__, #cond); \
stacktrace(); \
pr_red(BUG_REPORT_MSG); \
raise(SIGTRAP); \
}

#endif /* UFTRACE_UTILS_H */

0 comments on commit 100fe93

Please sign in to comment.