2012-03-02 17:38:43 +01:00
|
|
|
/* go-callers.c -- get callers for Go.
|
|
|
|
|
|
|
|
Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file. */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2012-09-28 16:48:30 +02:00
|
|
|
#include "backtrace.h"
|
2012-03-02 17:38:43 +01:00
|
|
|
|
|
|
|
#include "runtime.h"
|
2012-11-01 04:02:13 +01:00
|
|
|
#include "array.h"
|
2012-03-02 17:38:43 +01:00
|
|
|
|
2013-12-01 02:40:16 +01:00
|
|
|
/* This is set to non-zero when calling backtrace_full. This is used
|
|
|
|
to avoid getting hanging on a recursive lock in dl_iterate_phdr on
|
|
|
|
older versions of glibc when a SIGPROF signal arrives while
|
|
|
|
collecting a backtrace. */
|
|
|
|
|
|
|
|
uint32 runtime_in_callers;
|
|
|
|
|
2012-09-28 16:48:30 +02:00
|
|
|
/* Argument passed to callback function. */
|
2012-03-02 17:38:43 +01:00
|
|
|
|
|
|
|
struct callers_data
|
|
|
|
{
|
2013-01-30 23:24:40 +01:00
|
|
|
Location *locbuf;
|
2013-01-31 20:44:24 +01:00
|
|
|
int skip;
|
2012-03-02 17:38:43 +01:00
|
|
|
int index;
|
|
|
|
int max;
|
2014-07-19 23:36:26 +02:00
|
|
|
int keep_thunks;
|
2012-03-02 17:38:43 +01:00
|
|
|
};
|
|
|
|
|
2013-01-30 23:24:40 +01:00
|
|
|
/* Callback function for backtrace_full. Just collect the locations.
|
|
|
|
Return zero to continue, non-zero to stop. */
|
2012-09-28 16:48:30 +02:00
|
|
|
|
|
|
|
static int
|
2013-01-30 23:24:40 +01:00
|
|
|
callback (void *data, uintptr_t pc, const char *filename, int lineno,
|
|
|
|
const char *function)
|
2012-09-28 16:48:30 +02:00
|
|
|
{
|
|
|
|
struct callers_data *arg = (struct callers_data *) data;
|
2013-01-30 23:24:40 +01:00
|
|
|
Location *loc;
|
|
|
|
|
|
|
|
/* Skip split stack functions. */
|
|
|
|
if (function != NULL)
|
|
|
|
{
|
2013-02-01 00:12:42 +01:00
|
|
|
const char *p;
|
2013-01-30 23:24:40 +01:00
|
|
|
|
2013-02-01 00:12:42 +01:00
|
|
|
p = function;
|
2013-01-30 23:24:40 +01:00
|
|
|
if (__builtin_strncmp (p, "___", 3) == 0)
|
|
|
|
++p;
|
|
|
|
if (__builtin_strncmp (p, "__morestack_", 12) == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2013-02-01 00:12:42 +01:00
|
|
|
else if (filename != NULL)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = strrchr (filename, '/');
|
|
|
|
if (p == NULL)
|
|
|
|
p = filename;
|
2013-02-01 06:49:14 +01:00
|
|
|
if (__builtin_strncmp (p, "/morestack.S", 12) == 0)
|
2013-02-01 00:12:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:24:40 +01:00
|
|
|
|
2013-10-09 02:02:14 +02:00
|
|
|
/* Skip thunks and recover functions. There is no equivalent to
|
|
|
|
these functions in the gc toolchain, so returning them here means
|
|
|
|
significantly different results for runtime.Caller(N). */
|
2014-07-19 23:36:26 +02:00
|
|
|
if (function != NULL && !arg->keep_thunks)
|
2013-10-09 02:02:14 +02:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = __builtin_strchr (function, '.');
|
|
|
|
if (p != NULL && __builtin_strncmp (p + 1, "$thunk", 6) == 0)
|
|
|
|
return 0;
|
|
|
|
p = __builtin_strrchr (function, '$');
|
|
|
|
if (p != NULL && __builtin_strcmp(p, "$recover") == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-31 20:44:24 +01:00
|
|
|
if (arg->skip > 0)
|
|
|
|
{
|
|
|
|
--arg->skip;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:24:40 +01:00
|
|
|
loc = &arg->locbuf[arg->index];
|
2015-04-17 21:29:28 +02:00
|
|
|
|
|
|
|
/* On the call to backtrace_full the pc value was most likely
|
|
|
|
decremented if there was a normal call, since the pc referred to
|
|
|
|
the instruction where the call returned and not the call itself.
|
|
|
|
This was done so that the line number referred to the call
|
|
|
|
instruction. To make sure the actual pc from the call stack is
|
|
|
|
used, it is incremented here.
|
|
|
|
|
|
|
|
In the case of a signal, the pc was not decremented by
|
|
|
|
backtrace_full but still incremented here. That doesn't really
|
|
|
|
hurt anything since the line number is right and the pc refers to
|
|
|
|
the same instruction. */
|
|
|
|
|
|
|
|
loc->pc = pc + 1;
|
2013-01-31 17:41:15 +01:00
|
|
|
|
|
|
|
/* The libbacktrace library says that these strings might disappear,
|
|
|
|
but with the current implementation they won't. We can't easily
|
|
|
|
allocate memory here, so for now assume that we can save a
|
|
|
|
pointer to the strings. */
|
|
|
|
loc->filename = runtime_gostringnocopy ((const byte *) filename);
|
|
|
|
loc->function = runtime_gostringnocopy ((const byte *) function);
|
|
|
|
|
2013-01-30 23:24:40 +01:00
|
|
|
loc->lineno = lineno;
|
2012-09-28 16:48:30 +02:00
|
|
|
++arg->index;
|
2014-06-13 15:56:14 +02:00
|
|
|
|
|
|
|
/* There is no point to tracing past certain runtime functions.
|
|
|
|
Stopping the backtrace here can avoid problems on systems that
|
|
|
|
don't provide proper unwind information for makecontext, such as
|
|
|
|
Solaris (http://gcc.gnu.org/PR52583 comment #21). */
|
|
|
|
if (function != NULL)
|
|
|
|
{
|
|
|
|
if (__builtin_strcmp (function, "makecontext") == 0)
|
|
|
|
return 1;
|
|
|
|
if (filename != NULL)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = strrchr (filename, '/');
|
|
|
|
if (p == NULL)
|
|
|
|
p = filename;
|
|
|
|
if (__builtin_strcmp (p, "/proc.c") == 0)
|
|
|
|
{
|
|
|
|
if (__builtin_strcmp (function, "kickoff") == 0
|
|
|
|
|| __builtin_strcmp (function, "runtime_mstart") == 0
|
|
|
|
|| __builtin_strcmp (function, "runtime_main") == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 16:48:30 +02:00
|
|
|
return arg->index >= arg->max;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error callback. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
error_callback (void *data __attribute__ ((unused)),
|
|
|
|
const char *msg, int errnum)
|
2012-03-02 17:38:43 +01:00
|
|
|
{
|
2015-01-23 20:49:53 +01:00
|
|
|
if (errnum == -1)
|
|
|
|
{
|
|
|
|
/* No debug info available. Carry on as best we can. */
|
|
|
|
return;
|
|
|
|
}
|
2012-09-28 16:48:30 +02:00
|
|
|
if (errnum != 0)
|
|
|
|
runtime_printf ("%s errno %d\n", msg, errnum);
|
|
|
|
runtime_throw (msg);
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
2012-09-28 16:48:30 +02:00
|
|
|
/* Gather caller PC's. */
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
int32
|
2014-07-19 23:36:26 +02:00
|
|
|
runtime_callers (int32 skip, Location *locbuf, int32 m, bool keep_thunks)
|
2012-03-02 17:38:43 +01:00
|
|
|
{
|
2012-09-28 16:48:30 +02:00
|
|
|
struct callers_data data;
|
|
|
|
|
2013-01-30 23:24:40 +01:00
|
|
|
data.locbuf = locbuf;
|
2013-01-31 20:44:24 +01:00
|
|
|
data.skip = skip + 1;
|
2012-09-28 16:48:30 +02:00
|
|
|
data.index = 0;
|
|
|
|
data.max = m;
|
2014-07-19 23:36:26 +02:00
|
|
|
data.keep_thunks = keep_thunks;
|
2013-12-01 02:40:16 +01:00
|
|
|
runtime_xadd (&runtime_in_callers, 1);
|
2013-01-31 20:44:24 +01:00
|
|
|
backtrace_full (__go_get_backtrace_state (), 0, callback, error_callback,
|
|
|
|
&data);
|
2013-12-01 02:40:16 +01:00
|
|
|
runtime_xadd (&runtime_in_callers, -1);
|
2012-09-28 16:48:30 +02:00
|
|
|
return data.index;
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
2012-03-05 18:07:37 +01:00
|
|
|
|
|
|
|
int Callers (int, struct __go_open_array)
|
2013-01-24 20:44:23 +01:00
|
|
|
__asm__ (GOSYM_PREFIX "runtime.Callers");
|
2012-03-05 18:07:37 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
Callers (int skip, struct __go_open_array pc)
|
|
|
|
{
|
2013-01-30 23:24:40 +01:00
|
|
|
Location *locbuf;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
locbuf = (Location *) runtime_mal (pc.__count * sizeof (Location));
|
|
|
|
|
2012-05-22 23:52:56 +02:00
|
|
|
/* In the Go 1 release runtime.Callers has an off-by-one error,
|
|
|
|
which we can not correct because it would break backward
|
2013-02-02 16:11:48 +01:00
|
|
|
compatibility. Normally we would add 1 to SKIP here, but we
|
|
|
|
don't so that we are compatible. */
|
2014-07-19 23:36:26 +02:00
|
|
|
ret = runtime_callers (skip, locbuf, pc.__count, false);
|
2013-01-30 23:24:40 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ret; i++)
|
|
|
|
((uintptr *) pc.__values)[i] = locbuf[i].pc;
|
|
|
|
|
|
|
|
return ret;
|
2012-03-05 18:07:37 +01:00
|
|
|
}
|