Commit c33f40a7 authored by Tollef Fog Heen's avatar Tollef Fog Heen

Merge 4058-4065: Add a stack backtrace to the panic message.

Note that to use this for full effect, varnishd should be installed
unstripped.

r4058:
Also remember compat execinfo.h file.

r4059:
Tell autocrap about compat/execinfo.h

r4060:
Attempt to add a stack backtrace to the panic message.

r4061:
Spit out more information on panic

r4062:
Autocrap the dladdr() functions existence.

r4063:
More autocrappery.

r4064:
Comment out unneeded function.

r4065:
Pass a void* to Symbol_Lookup() to avoid stdint requirement on
common.h



git-svn-id: http://www.varnish-cache.org/svn/branches/2.0@4256 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent e370644a
......@@ -37,6 +37,11 @@
#include <stdlib.h>
#include <unistd.h>
#ifndef HAVE_EXECINFO_H
#include "compat/execinfo.h"
#else
#include <execinfo.h>
#endif
#include "cache.h"
#include "cache_backend.h"
#include "vcl.h"
......@@ -131,21 +136,21 @@ pan_storage(const struct storage *st)
/*--------------------------------------------------------------------*/
static void
pan_http(const struct http *h)
pan_http(const char *id, const struct http *h, int indent)
{
int i;
vsb_printf(vsp, " http = {\n");
pan_ws(h->ws, 6);
if (h->nhd > HTTP_HDR_FIRST) {
vsb_printf(vsp, " hd = {\n");
for (i = HTTP_HDR_FIRST; i < h->nhd; ++i)
vsb_printf(vsp, " \"%.*s\",\n",
(int)(h->hd[i].e - h->hd[i].b),
h->hd[i].b);
vsb_printf(vsp, " },\n");
vsb_printf(vsp, "%*shttp[%s] = {\n", indent, "", id);
vsb_printf(vsp, "%*sws = %p[%s]\n", indent + 2, "",
h->ws, h->ws ? h->ws->id : "");
for (i = 0; i < h->nhd; ++i) {
if (h->hd[i].b == NULL && h->hd[i].e == NULL)
continue;
vsb_printf(vsp, "%*s\"%.*s\",\n", indent + 4, "",
(int)(h->hd[i].e - h->hd[i].b),
h->hd[i].b);
}
vsb_printf(vsp, " },\n");
vsb_printf(vsp, "%*s},\n", indent, "");
}
......@@ -159,7 +164,7 @@ pan_object(const struct object *o)
vsb_printf(vsp, " obj = %p {\n", o);
vsb_printf(vsp, " refcnt = %u, xid = %u,\n", o->refcnt, o->xid);
pan_ws(o->ws_o, 4);
pan_http(o->http);
pan_http("obj", o->http, 4);
vsb_printf(vsp, " len = %u,\n", o->len);
vsb_printf(vsp, " store = {\n");
VTAILQ_FOREACH(st, &o->store, list)
......@@ -190,8 +195,7 @@ static void
pan_wrk(const struct worker *wrk)
{
vsb_printf(vsp, " worker = %p {\n", wrk);
vsb_printf(vsp, " },\n");
vsb_printf(vsp, " worker = %p\n", wrk);
}
/*--------------------------------------------------------------------*/
......@@ -229,7 +233,11 @@ pan_sess(const struct sess *sp)
" err_code = %d, err_reason = %s,\n", sp->err_code,
sp->err_reason ? sp->err_reason : "(null)");
vsb_printf(vsp, " restarts = %d, esis = %d\n",
sp->restarts, sp->esis);
pan_ws(sp->ws, 2);
pan_http("req", sp->http, 2);
if (sp->wrk != NULL)
pan_wrk(sp->wrk);
......@@ -248,6 +256,28 @@ pan_sess(const struct sess *sp)
/*--------------------------------------------------------------------*/
static void
pan_backtrace(void)
{
void *array[10];
size_t size;
size_t i;
size = backtrace (array, 10);
vsb_printf(vsp, "Backtrace:\n");
for (i = 0; i < size; i++) {
vsb_printf (vsp, " ");
if (Symbol_Lookup(vsp, array[i]) < 0) {
char **strings;
strings = backtrace_symbols(&array[i], 1);
vsb_printf(vsp, "%p: %s", array[i], strings[0]);
}
vsb_printf (vsp, "\n");
}
}
/*--------------------------------------------------------------------*/
static void
pan_ic(const char *func, const char *file, int line, const char *cond,
int err, int xxx)
......@@ -264,7 +294,7 @@ pan_ic(const char *func, const char *file, int line, const char *cond,
break;
case 2:
vsb_printf(vsp,
"Panic from VCL:\n%s\n", cond);
"Panic from VCL:\n %s\n", cond);
break;
case 1:
vsb_printf(vsp,
......@@ -276,16 +306,19 @@ pan_ic(const char *func, const char *file, int line, const char *cond,
case 0:
vsb_printf(vsp,
"Assert error in %s(), %s line %d:\n"
" Condition(%s) not true.",
" Condition(%s) not true.\n",
func, file, line, cond);
break;
}
if (err)
vsb_printf(vsp, " errno = %d (%s)", err, strerror(err));
vsb_printf(vsp, "errno = %d (%s)\n", err, strerror(err));
q = THR_GetName();
if (q != NULL)
vsb_printf(vsp, " thread = (%s)", q);
vsb_printf(vsp, "thread = (%s)\n", q);
pan_backtrace();
if (!(params->diag_bitmap & 0x2000)) {
sp = THR_GetSession();
if (sp != NULL)
......
......@@ -42,6 +42,10 @@ void VSL_Panic(int *len, char **ptr);
void VSL_MgtInit(const char *fn, unsigned size);
extern struct varnish_stats *VSL_stats;
/* varnishd.c */
struct vsb;
int Symbol_Lookup(struct vsb *vsb, void *ptr);
#define TRUST_ME(ptr) ((void*)(uintptr_t)(ptr))
/* Really belongs in mgt.h, but storage_file chokes on both */
......
......@@ -414,6 +414,92 @@ cli_check(const struct cli *cli)
exit (2);
}
/*--------------------------------------------------------------------
* All praise POSIX! Thanks to our glorious standards there are no
* standard way to get a back-trace of the stack, and even if we hack
* that together from spit and pieces of string, there is no way no
* standard way to translate a pointer to a symbol, which returns anything
* usable. (See for instance FreeBSD PR-134391).
*
* Attempt to run nm(1) on our binary during startup, hoping it will
* give us a usable list of symbols.
*/
struct symbols {
uintptr_t a;
char *n;
VTAILQ_ENTRY(symbols) list;
};
static VTAILQ_HEAD(,symbols) symbols = VTAILQ_HEAD_INITIALIZER(symbols);
int
Symbol_Lookup(struct vsb *vsb, void *ptr)
{
struct symbols *s, *s0;
uintptr_t pp;
pp = (uintptr_t)ptr;
s0 = NULL;
VTAILQ_FOREACH(s, &symbols, list) {
if (s->a > pp)
continue;
if (s0 != NULL && s->a < s0->a)
continue;
s0 = s;
}
if (s0 == NULL)
return (-1);
vsb_printf(vsb, "%p", ptr);
if (s0 != NULL)
vsb_printf(vsb, ": %s+%jx", s0->n, (uintmax_t)pp - s0->a);
return (0);
}
static void
Symbol_hack(const char *a0)
{
char buf[BUFSIZ], *p, *e;
FILE *fi;
uintptr_t a;
struct symbols *s;
strcpy(buf, "nm -an ");
strcat(buf, a0);
fi = popen(buf, "r");
if (fi != NULL) {
while (fgets(buf, sizeof buf, fi)) {
if (buf[0] == ' ')
continue;
p = NULL;
a = strtoul(buf, &p, 16);
if (p == NULL)
continue;
if (a == 0)
continue;
if (*p++ != ' ')
continue;
p++;
if (*p++ != ' ')
continue;
if (*p <= ' ')
continue;
e = strchr(p, '\0');
AN(e);
while (e > p && isspace(e[-1]))
e--;
*e = '\0';
s = malloc(sizeof *s + strlen(p) + 1);
AN(s);
s->a = a;
s->n = (void*)(s + 1);
strcpy(s->n, p);
VTAILQ_INSERT_TAIL(&symbols, s, list);
}
pclose(fi);
}
}
/*--------------------------------------------------------------------*/
int
......@@ -443,6 +529,11 @@ main(int argc, char * const *argv)
setbuf(stdout, NULL);
setbuf(stderr, NULL);
Symbol_hack(argv[0]);
/* for ASSERT_MGT() */
mgt_pid = getpid();
/*
* Run in UTC timezone, on the off-chance that this operating
* system does not have a timegm() function, and translates
......
......@@ -103,6 +103,7 @@ AC_TYPE_SIZE_T
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([strerror])
AC_FUNC_STRERROR_R
AC_CHECK_FUNCS([dladdr])
AC_CHECK_FUNCS([socket])
AC_CHECK_FUNCS([strptime])
AC_CHECK_FUNCS([fmtcheck])
......
......@@ -14,6 +14,7 @@ nobase_noinst_HEADERS = \
cli_priv.h \
compat/asprintf.h \
compat/daemon.h \
compat/execinfo.h \
compat/setproctitle.h \
compat/srandomdev.h \
compat/strlcat.h \
......
/*
* Copyright (c) 2003 Maxim Sobolev <sobomax@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: execinfo.h,v 1.2 2004/07/19 05:20:29 sobomax Exp $
*/
#ifndef COMPAT_EXECINFO_H_INCLUDED
#define COMPAT_EXECINFO_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
int backtrace(void **, int);
char ** backtrace_symbols(void *const *, int);
void backtrace_symbols_fd(void *const *, int, int);
#ifdef __cplusplus
}
#endif
#endif /* COMPAT_EXECINFO_H_INCLUDED */
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment