/* print.c -- Print the current 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 <stdio.h>35#include <string.h>36#include <sys/types.h>3738#include "backtrace.h"39#include "internal.h"4041/* Passed to callbacks. */4243struct print_data44{45struct backtrace_state *state;46FILE *f;47};4849/* Print one level of a backtrace. */5051static int52print_callback (void *data, uintptr_t pc, const char *filename, int lineno,53const char *function)54{55struct print_data *pdata = (struct print_data *) data;5657fprintf (pdata->f, "0x%lx %s\n\t%s:%d\n",58(unsigned long) pc,59function == NULL ? "???" : function,60filename == NULL ? "???" : filename,61lineno);62return 0;63}6465/* Print errors to stderr. */6667static void68error_callback (void *data, const char *msg, int errnum)69{70struct print_data *pdata = (struct print_data *) data;7172if (pdata->state->filename != NULL)73fprintf (stderr, "%s: ", pdata->state->filename);74fprintf (stderr, "libbacktrace: %s", msg);75if (errnum > 0)76fprintf (stderr, ": %s", strerror (errnum));77fputc ('\n', stderr);78}7980/* Print a backtrace. */8182void __attribute__((noinline))83backtrace_print (struct backtrace_state *state, int skip, FILE *f)84{85struct print_data data;8687data.state = state;88data.f = f;89backtrace_full (state, skip + 1, print_callback, error_callback,90(void *) &data);91}929394