Home | History | Annotate | Line # | Download | only in libbacktrace
      1       1.1  mrg /* print.c -- Print the current backtrace.
      2  1.1.1.11  mrg    Copyright (C) 2012-2024 Free Software Foundation, Inc.
      3       1.1  mrg    Written by Ian Lance Taylor, Google.
      4       1.1  mrg 
      5       1.1  mrg Redistribution and use in source and binary forms, with or without
      6       1.1  mrg modification, are permitted provided that the following conditions are
      7       1.1  mrg met:
      8       1.1  mrg 
      9       1.1  mrg     (1) Redistributions of source code must retain the above copyright
     10   1.1.1.4  mrg     notice, this list of conditions and the following disclaimer.
     11       1.1  mrg 
     12       1.1  mrg     (2) Redistributions in binary form must reproduce the above copyright
     13       1.1  mrg     notice, this list of conditions and the following disclaimer in
     14       1.1  mrg     the documentation and/or other materials provided with the
     15   1.1.1.4  mrg     distribution.
     16   1.1.1.4  mrg 
     17       1.1  mrg     (3) The name of the author may not be used to
     18       1.1  mrg     endorse or promote products derived from this software without
     19       1.1  mrg     specific prior written permission.
     20       1.1  mrg 
     21       1.1  mrg THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22       1.1  mrg IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23       1.1  mrg WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24       1.1  mrg DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     25       1.1  mrg INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26       1.1  mrg (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     27       1.1  mrg SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28       1.1  mrg HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     29       1.1  mrg STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     30       1.1  mrg IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31       1.1  mrg POSSIBILITY OF SUCH DAMAGE.  */
     32       1.1  mrg 
     33       1.1  mrg #include "config.h"
     34       1.1  mrg 
     35       1.1  mrg #include <stdio.h>
     36       1.1  mrg #include <string.h>
     37       1.1  mrg #include <sys/types.h>
     38       1.1  mrg 
     39       1.1  mrg #include "backtrace.h"
     40       1.1  mrg #include "internal.h"
     41       1.1  mrg 
     42       1.1  mrg /* Passed to callbacks.  */
     43       1.1  mrg 
     44       1.1  mrg struct print_data
     45       1.1  mrg {
     46       1.1  mrg   struct backtrace_state *state;
     47       1.1  mrg   FILE *f;
     48       1.1  mrg };
     49       1.1  mrg 
     50       1.1  mrg /* Print one level of a backtrace.  */
     51       1.1  mrg 
     52       1.1  mrg static int
     53       1.1  mrg print_callback (void *data, uintptr_t pc, const char *filename, int lineno,
     54       1.1  mrg 		const char *function)
     55       1.1  mrg {
     56       1.1  mrg   struct print_data *pdata = (struct print_data *) data;
     57       1.1  mrg 
     58       1.1  mrg   fprintf (pdata->f, "0x%lx %s\n\t%s:%d\n",
     59       1.1  mrg 	   (unsigned long) pc,
     60       1.1  mrg 	   function == NULL ? "???" : function,
     61       1.1  mrg 	   filename == NULL ? "???" : filename,
     62       1.1  mrg 	   lineno);
     63       1.1  mrg   return 0;
     64       1.1  mrg }
     65       1.1  mrg 
     66       1.1  mrg /* Print errors to stderr.  */
     67       1.1  mrg 
     68       1.1  mrg static void
     69       1.1  mrg error_callback (void *data, const char *msg, int errnum)
     70       1.1  mrg {
     71       1.1  mrg   struct print_data *pdata = (struct print_data *) data;
     72       1.1  mrg 
     73       1.1  mrg   if (pdata->state->filename != NULL)
     74       1.1  mrg     fprintf (stderr, "%s: ", pdata->state->filename);
     75       1.1  mrg   fprintf (stderr, "libbacktrace: %s", msg);
     76       1.1  mrg   if (errnum > 0)
     77       1.1  mrg     fprintf (stderr, ": %s", strerror (errnum));
     78       1.1  mrg   fputc ('\n', stderr);
     79       1.1  mrg }
     80       1.1  mrg 
     81       1.1  mrg /* Print a backtrace.  */
     82       1.1  mrg 
     83   1.1.1.8  mrg void __attribute__((noinline))
     84       1.1  mrg backtrace_print (struct backtrace_state *state, int skip, FILE *f)
     85       1.1  mrg {
     86       1.1  mrg   struct print_data data;
     87       1.1  mrg 
     88       1.1  mrg   data.state = state;
     89       1.1  mrg   data.f = f;
     90       1.1  mrg   backtrace_full (state, skip + 1, print_callback, error_callback,
     91       1.1  mrg 		  (void *) &data);
     92       1.1  mrg }
     93