Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backtrace improvements for utilities #4116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions bin/varnishtest/vtc_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "vtc_log.h"

#include "vtim.h"
#include "vbt.h"

static pthread_mutex_t vtclog_mtx;
static char *vtclog_buf;
Expand Down Expand Up @@ -296,20 +297,30 @@ static void v_noreturn_
vtc_log_VAS_Fail(const char *func, const char *file, int line,
const char *cond, enum vas_e why)
{
char buf[4096] = "";
struct vtclog *vl;
int e = errno;

(void)why;

if (VBT_dump(sizeof buf, buf) < 0) {
bprintf(buf, "Failed to print backtrace: %d (%s)\n",
errno, strerror(errno));
}

vl = pthread_getspecific(log_key);
if (vl == NULL || vl->act) {
fprintf(stderr,
"Assert error in %s(), %s line %d:\n"
" Condition(%s) not true. (errno=%d %s)\n",
func, file, line, cond, e, strerror(e));
" Condition(%s) not true. (errno=%d %s)\n"
"%s\n",
func, file, line, cond, e, strerror(e), buf);
} else
vtc_fatal(vl, "Assert error in %s(), %s line %d:"
" Condition(%s) not true."
" Errno=%d %s", func, file, line, cond, e, strerror(e));
" Errno=%d %s\n"
"%s\n",
func, file, line, cond, e, strerror(e), buf);
abort();
}

Expand Down
1 change: 1 addition & 0 deletions include/vbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
struct vsb;

void VBT_format(struct vsb *);
int VBT_dump(size_t len, char [len]);
69 changes: 43 additions & 26 deletions lib/libvarnish/vas.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <string.h>

#include "vdef.h"
#include "vbt.h"

#include "vas.h"

Expand All @@ -57,37 +58,53 @@ VAS_errtxt(int e)

vas_f *VAS_Fail_Func v_noreturn_;

void v_noreturn_
VAS_Fail(const char *func, const char *file, int line,
static void
vas_default(const char *func, const char *file, int line,
const char *cond, enum vas_e kind)
{
int err = errno;
char buf[4096];

if (VAS_Fail_Func != NULL) {
VAS_Fail_Func(func, file, line, cond, kind);
if (kind == VAS_MISSING) {
fprintf(stderr,
"Missing error handling code in %s(), %s line %d:\n"
" Condition(%s) not true.\n",
func, file, line, cond);
} else if (kind == VAS_INCOMPLETE) {
fprintf(stderr,
"Incomplete code in %s(), %s line %d:\n",
func, file, line);
} else if (kind == VAS_WRONG) {
fprintf(stderr,
"Wrong turn in %s(), %s line %d: %s\n",
func, file, line, cond);
} else {
if (kind == VAS_MISSING) {
fprintf(stderr,
"Missing error handling code in %s(), %s line %d:\n"
" Condition(%s) not true.\n",
func, file, line, cond);
} else if (kind == VAS_INCOMPLETE) {
fprintf(stderr,
"Incomplete code in %s(), %s line %d:\n",
func, file, line);
} else if (kind == VAS_WRONG) {
fprintf(stderr,
"Wrong turn in %s(), %s line %d: %s\n",
func, file, line, cond);
} else {
fprintf(stderr,
"Assert error in %s(), %s line %d:\n"
" Condition(%s) not true.\n",
func, file, line, cond);
}
if (err)
fprintf(stderr,
" errno = %d (%s)\n", err, strerror(err));
fprintf(stderr,
"Assert error in %s(), %s line %d:\n"
" Condition(%s) not true.\n",
func, file, line, cond);
}
if (err) {
fprintf(stderr,
" errno = %d (%s)\n", err, strerror(err));
}

if (VBT_dump(sizeof buf, buf) < 0) {
bprintf(buf, "Failed to print backtrace: %d (%s)\n",
errno, strerror(errno));
}

fprintf(stderr, "%s", buf);
}

void v_noreturn_
VAS_Fail(const char *func, const char *file, int line,
const char *cond, enum vas_e kind)
{

if (VAS_Fail_Func != NULL)
VAS_Fail_Func(func, file, line, cond, kind);
else
vas_default(func, file, line, cond, kind);
abort();
}
16 changes: 16 additions & 0 deletions lib/libvarnish/vbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,19 @@ VBT_format(struct vsb *vsb)
vbt_execinfo(vsb);
#endif
}

int
VBT_dump(size_t len, char buf[len])
{
struct vsb vsb[1];

if (buf == NULL || VSB_init(vsb, buf, len) == NULL)
return (-1);

VSB_printf(vsb, "Backtrace:\n");
VSB_indent(vsb, 2);
VBT_format(vsb);
VSB_indent(vsb, -2);

return (0);
}