Path: blob/master/thirdparty/libbacktrace/backtrace.c
9896 views
/* backtrace.c -- Entry point for stack backtrace library.1Copyright (C) 2012-2021 Free Software Foundation, Inc.2Written by Ian Lance Taylor, Google.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78(1) Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011(2) Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516(3) The name of the author may not be used to17endorse or promote products derived from this software without18specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30POSSIBILITY OF SUCH DAMAGE. */3132#include "config.h"3334#include <sys/types.h>3536#include "unwind.h"37#include "backtrace.h"38#include "internal.h"3940/* The main backtrace_full routine. */4142/* Data passed through _Unwind_Backtrace. */4344struct backtrace_data45{46/* Number of frames to skip. */47int skip;48/* Library state. */49struct backtrace_state *state;50/* Callback routine. */51backtrace_full_callback callback;52/* Error callback routine. */53backtrace_error_callback error_callback;54/* Data to pass to callback routines. */55void *data;56/* Value to return from backtrace_full. */57int ret;58/* Whether there is any memory available. */59int can_alloc;60};6162/* Unwind library callback routine. This is passed to63_Unwind_Backtrace. */6465static _Unwind_Reason_Code66unwind (struct _Unwind_Context *context, void *vdata)67{68struct backtrace_data *bdata = (struct backtrace_data *) vdata;69uintptr_t pc;70int ip_before_insn = 0;7172#ifdef HAVE_GETIPINFO73pc = _Unwind_GetIPInfo (context, &ip_before_insn);74#else75pc = _Unwind_GetIP (context);76#endif7778if (bdata->skip > 0)79{80--bdata->skip;81return _URC_NO_REASON;82}8384if (!ip_before_insn)85--pc;8687if (!bdata->can_alloc)88bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL);89else90bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback,91bdata->error_callback, bdata->data);92if (bdata->ret != 0)93return _URC_END_OF_STACK;9495return _URC_NO_REASON;96}9798/* Get a stack backtrace. */99100int __attribute__((noinline))101backtrace_full (struct backtrace_state *state, int skip,102backtrace_full_callback callback,103backtrace_error_callback error_callback, void *data)104{105struct backtrace_data bdata;106void *p;107108bdata.skip = skip + 1;109bdata.state = state;110bdata.callback = callback;111bdata.error_callback = error_callback;112bdata.data = data;113bdata.ret = 0;114115/* If we can't allocate any memory at all, don't try to produce116file/line information. */117p = backtrace_alloc (state, 4096, NULL, NULL);118if (p == NULL)119bdata.can_alloc = 0;120else121{122backtrace_free (state, p, 4096, NULL, NULL);123bdata.can_alloc = 1;124}125126_Unwind_Backtrace (unwind, &bdata);127return bdata.ret;128}129130131