Skip to content

Commit

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

However, DCHECK 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 CHECK
and DCHECK.

Here is the example output.

  /home/honggyu/work/uftrace/cmds/report.c:98: add_remaining_fstack: CHECK `root' failed.
  Stack trace:
    #0 uftrace() [0x40ffed]
    #1 uftrace() [0x410542]
    #2 uftrace() [0x410827]
    #3 uftrace(command_report+0x374) [0x4118d5]
    #4 uftrace(main+0x4f7) [0x40c7c5]
    #5 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f1552007830]
    #6 uftrace(_start+0x29) [0x40a0d9]

  Please report this uftrace internal bug to https://github.com/namhyung/uftrace/issues
  Aborted (core dumped)

Signed-off-by: Honggyu Kim <[email protected]>
  • Loading branch information
honggyukim committed Jun 6, 2020
1 parent 188ad37 commit 71adec8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ LIB_LDFLAGS = $(COMMON_LDFLAGS) $(LDFLAGS_$@) $(LDFLAGS_lib) -Wl,--no-und
TEST_LDFLAGS = $(COMMON_LDFLAGS) -L$(objdir)/libtraceevent -ltraceevent

ifeq ($(DEBUG), 1)
COMMON_CFLAGS += -O0 -g -rdynamic
COMMON_CFLAGS += -O0 -g -DDEBUG_MODE=1 -rdynamic
else
COMMON_CFLAGS += -O2 -g
COMMON_CFLAGS += -O2 -g -DDEBUG_MODE=0
endif

ifeq ($(TRACE), 1)
Expand Down
20 changes: 20 additions & 0 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,24 @@ void dump_stacktrace(void **buffer, int nptrs);
dump_stacktrace(buffer, nptrs); \
} while (0)

#define CHECK(cond) \
if (unlikely(!(cond))) { \
pr_red("%s:%d: %s: CHECK `%s' failed.\n", \
__FILE__, __LINE__, __func__, #cond); \
stacktrace(); \
pr_red("Please report this uftrace internal bug to " \
"https://github.com/namhyung/uftrace/issues"); \
abort(); \
}

#define DCHECK(cond) \
if (DEBUG_MODE && unlikely(!(cond))) { \
pr_red("%s:%d: %s: DCHECK `%s' failed.\n", \
__FILE__, __LINE__, __func__, #cond); \
stacktrace(); \
pr_red("Please report this uftrace internal bug to " \
"https://github.com/namhyung/uftrace/issues"); \
abort(); \
}

#endif /* UFTRACE_UTILS_H */

0 comments on commit 71adec8

Please sign in to comment.