Path: blob/master/thirdparty/libbacktrace/fileline.c
10277 views
/* fileline.c -- Get file and line number information in a backtrace.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>35#include <sys/stat.h>36#include <errno.h>37#include <fcntl.h>38#include <stdlib.h>39#include <unistd.h>4041#if defined (HAVE_KERN_PROC_ARGS) || defined (HAVE_KERN_PROC)42#include <sys/sysctl.h>43#endif4445#ifdef HAVE_MACH_O_DYLD_H46#include <mach-o/dyld.h>47#endif4849#include "backtrace.h"50#include "internal.h"5152#ifndef HAVE_GETEXECNAME53#define getexecname() NULL54#endif5556#if !defined (HAVE_KERN_PROC_ARGS) && !defined (HAVE_KERN_PROC)5758#define sysctl_exec_name1(state, error_callback, data) NULL59#define sysctl_exec_name2(state, error_callback, data) NULL6061#else /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */6263static char *64sysctl_exec_name (struct backtrace_state *state,65int mib0, int mib1, int mib2, int mib3,66backtrace_error_callback error_callback, void *data)67{68int mib[4];69size_t len;70char *name;71size_t rlen;7273mib[0] = mib0;74mib[1] = mib1;75mib[2] = mib2;76mib[3] = mib3;7778if (sysctl (mib, 4, NULL, &len, NULL, 0) < 0)79return NULL;80name = (char *) backtrace_alloc (state, len, error_callback, data);81if (name == NULL)82return NULL;83rlen = len;84if (sysctl (mib, 4, name, &rlen, NULL, 0) < 0)85{86backtrace_free (state, name, len, error_callback, data);87return NULL;88}89return name;90}9192#ifdef HAVE_KERN_PROC_ARGS9394static char *95sysctl_exec_name1 (struct backtrace_state *state,96backtrace_error_callback error_callback, void *data)97{98/* This variant is used on NetBSD. */99return sysctl_exec_name (state, CTL_KERN, KERN_PROC_ARGS, -1,100KERN_PROC_PATHNAME, error_callback, data);101}102103#else104105#define sysctl_exec_name1(state, error_callback, data) NULL106107#endif108109#ifdef HAVE_KERN_PROC110111static char *112sysctl_exec_name2 (struct backtrace_state *state,113backtrace_error_callback error_callback, void *data)114{115/* This variant is used on FreeBSD. */116return sysctl_exec_name (state, CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1,117error_callback, data);118}119120#else121122#define sysctl_exec_name2(state, error_callback, data) NULL123124#endif125126#endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */127128#ifdef HAVE_MACH_O_DYLD_H129130static char *131macho_get_executable_path (struct backtrace_state *state,132backtrace_error_callback error_callback, void *data)133{134uint32_t len;135char *name;136137len = 0;138if (_NSGetExecutablePath (NULL, &len) == 0)139return NULL;140name = (char *) backtrace_alloc (state, len, error_callback, data);141if (name == NULL)142return NULL;143if (_NSGetExecutablePath (name, &len) != 0)144{145backtrace_free (state, name, len, error_callback, data);146return NULL;147}148return name;149}150151#else /* !defined (HAVE_MACH_O_DYLD_H) */152153#define macho_get_executable_path(state, error_callback, data) NULL154155#endif /* !defined (HAVE_MACH_O_DYLD_H) */156157/* Initialize the fileline information from the executable. Returns 1158on success, 0 on failure. */159160static int161fileline_initialize (struct backtrace_state *state,162backtrace_error_callback error_callback, void *data)163{164int failed;165fileline fileline_fn;166int pass;167int called_error_callback;168int descriptor;169const char *filename;170char buf[64];171172if (!state->threaded)173failed = state->fileline_initialization_failed;174else175failed = backtrace_atomic_load_int (&state->fileline_initialization_failed);176177if (failed)178{179error_callback (data, "failed to read executable information", -1);180return 0;181}182183if (!state->threaded)184fileline_fn = state->fileline_fn;185else186fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);187if (fileline_fn != NULL)188return 1;189190/* We have not initialized the information. Do it now. */191192descriptor = -1;193called_error_callback = 0;194for (pass = 0; pass < 8; ++pass)195{196int does_not_exist;197198switch (pass)199{200case 0:201filename = state->filename;202break;203case 1:204filename = getexecname ();205break;206case 2:207filename = "/proc/self/exe";208break;209case 3:210filename = "/proc/curproc/file";211break;212case 4:213snprintf (buf, sizeof (buf), "/proc/%ld/object/a.out",214(long) getpid ());215filename = buf;216break;217case 5:218filename = sysctl_exec_name1 (state, error_callback, data);219break;220case 6:221filename = sysctl_exec_name2 (state, error_callback, data);222break;223case 7:224filename = macho_get_executable_path (state, error_callback, data);225break;226default:227abort ();228}229230if (filename == NULL)231continue;232233descriptor = backtrace_open (filename, error_callback, data,234&does_not_exist);235if (descriptor < 0 && !does_not_exist)236{237called_error_callback = 1;238break;239}240if (descriptor >= 0)241break;242}243244if (descriptor < 0)245{246if (!called_error_callback)247{248if (state->filename != NULL)249error_callback (data, state->filename, ENOENT);250else251error_callback (data,252"libbacktrace could not find executable to open",2530);254}255failed = 1;256}257258if (!failed)259{260if (!backtrace_initialize (state, filename, descriptor, error_callback,261data, &fileline_fn))262failed = 1;263}264265if (failed)266{267if (!state->threaded)268state->fileline_initialization_failed = 1;269else270backtrace_atomic_store_int (&state->fileline_initialization_failed, 1);271return 0;272}273274if (!state->threaded)275state->fileline_fn = fileline_fn;276else277{278backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn);279280/* Note that if two threads initialize at once, one of the data281sets may be leaked. */282}283284return 1;285}286287/* Given a PC, find the file name, line number, and function name. */288289int290backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,291backtrace_full_callback callback,292backtrace_error_callback error_callback, void *data)293{294if (!fileline_initialize (state, error_callback, data))295return 0;296297if (state->fileline_initialization_failed)298return 0;299300return state->fileline_fn (state, pc, callback, error_callback, data);301}302303/* Given a PC, find the symbol for it, and its value. */304305int306backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,307backtrace_syminfo_callback callback,308backtrace_error_callback error_callback, void *data)309{310if (!fileline_initialize (state, error_callback, data))311return 0;312313if (state->fileline_initialization_failed)314return 0;315316state->syminfo_fn (state, pc, callback, error_callback, data);317return 1;318}319320/* A backtrace_syminfo_callback that can call into a321backtrace_full_callback, used when we have a symbol table but no322debug info. */323324void325backtrace_syminfo_to_full_callback (void *data, uintptr_t pc,326const char *symname,327uintptr_t symval ATTRIBUTE_UNUSED,328uintptr_t symsize ATTRIBUTE_UNUSED)329{330struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;331332bdata->ret = bdata->full_callback (bdata->full_data, pc, NULL, 0, symname);333}334335/* An error callback that corresponds to336backtrace_syminfo_to_full_callback. */337338void339backtrace_syminfo_to_full_error_callback (void *data, const char *msg,340int errnum)341{342struct backtrace_call_full *bdata = (struct backtrace_call_full *) data;343344bdata->full_error_callback (bdata->full_data, msg, errnum);345}346347348