xref: /netbsd-src/external/gpl3/gcc/dist/libbacktrace/print.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
148fb7bfaSmrg /* print.c -- Print the current backtrace.
2*b1e83836Smrg    Copyright (C) 2012-2022 Free Software Foundation, Inc.
348fb7bfaSmrg    Written by Ian Lance Taylor, Google.
448fb7bfaSmrg 
548fb7bfaSmrg Redistribution and use in source and binary forms, with or without
648fb7bfaSmrg modification, are permitted provided that the following conditions are
748fb7bfaSmrg met:
848fb7bfaSmrg 
948fb7bfaSmrg     (1) Redistributions of source code must retain the above copyright
1048fb7bfaSmrg     notice, this list of conditions and the following disclaimer.
1148fb7bfaSmrg 
1248fb7bfaSmrg     (2) Redistributions in binary form must reproduce the above copyright
1348fb7bfaSmrg     notice, this list of conditions and the following disclaimer in
1448fb7bfaSmrg     the documentation and/or other materials provided with the
1548fb7bfaSmrg     distribution.
1648fb7bfaSmrg 
1748fb7bfaSmrg     (3) The name of the author may not be used to
1848fb7bfaSmrg     endorse or promote products derived from this software without
1948fb7bfaSmrg     specific prior written permission.
2048fb7bfaSmrg 
2148fb7bfaSmrg THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2248fb7bfaSmrg IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2348fb7bfaSmrg WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2448fb7bfaSmrg DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2548fb7bfaSmrg INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2648fb7bfaSmrg (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2748fb7bfaSmrg SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2848fb7bfaSmrg HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2948fb7bfaSmrg STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3048fb7bfaSmrg IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3148fb7bfaSmrg POSSIBILITY OF SUCH DAMAGE.  */
3248fb7bfaSmrg 
3348fb7bfaSmrg #include "config.h"
3448fb7bfaSmrg 
3548fb7bfaSmrg #include <stdio.h>
3648fb7bfaSmrg #include <string.h>
3748fb7bfaSmrg #include <sys/types.h>
3848fb7bfaSmrg 
3948fb7bfaSmrg #include "backtrace.h"
4048fb7bfaSmrg #include "internal.h"
4148fb7bfaSmrg 
4248fb7bfaSmrg /* Passed to callbacks.  */
4348fb7bfaSmrg 
4448fb7bfaSmrg struct print_data
4548fb7bfaSmrg {
4648fb7bfaSmrg   struct backtrace_state *state;
4748fb7bfaSmrg   FILE *f;
4848fb7bfaSmrg };
4948fb7bfaSmrg 
5048fb7bfaSmrg /* Print one level of a backtrace.  */
5148fb7bfaSmrg 
5248fb7bfaSmrg static int
print_callback(void * data,uintptr_t pc,const char * filename,int lineno,const char * function)5348fb7bfaSmrg print_callback (void *data, uintptr_t pc, const char *filename, int lineno,
5448fb7bfaSmrg 		const char *function)
5548fb7bfaSmrg {
5648fb7bfaSmrg   struct print_data *pdata = (struct print_data *) data;
5748fb7bfaSmrg 
5848fb7bfaSmrg   fprintf (pdata->f, "0x%lx %s\n\t%s:%d\n",
5948fb7bfaSmrg 	   (unsigned long) pc,
6048fb7bfaSmrg 	   function == NULL ? "???" : function,
6148fb7bfaSmrg 	   filename == NULL ? "???" : filename,
6248fb7bfaSmrg 	   lineno);
6348fb7bfaSmrg   return 0;
6448fb7bfaSmrg }
6548fb7bfaSmrg 
6648fb7bfaSmrg /* Print errors to stderr.  */
6748fb7bfaSmrg 
6848fb7bfaSmrg static void
error_callback(void * data,const char * msg,int errnum)6948fb7bfaSmrg error_callback (void *data, const char *msg, int errnum)
7048fb7bfaSmrg {
7148fb7bfaSmrg   struct print_data *pdata = (struct print_data *) data;
7248fb7bfaSmrg 
7348fb7bfaSmrg   if (pdata->state->filename != NULL)
7448fb7bfaSmrg     fprintf (stderr, "%s: ", pdata->state->filename);
7548fb7bfaSmrg   fprintf (stderr, "libbacktrace: %s", msg);
7648fb7bfaSmrg   if (errnum > 0)
7748fb7bfaSmrg     fprintf (stderr, ": %s", strerror (errnum));
7848fb7bfaSmrg   fputc ('\n', stderr);
7948fb7bfaSmrg }
8048fb7bfaSmrg 
8148fb7bfaSmrg /* Print a backtrace.  */
8248fb7bfaSmrg 
83181254a7Smrg void __attribute__((noinline))
backtrace_print(struct backtrace_state * state,int skip,FILE * f)8448fb7bfaSmrg backtrace_print (struct backtrace_state *state, int skip, FILE *f)
8548fb7bfaSmrg {
8648fb7bfaSmrg   struct print_data data;
8748fb7bfaSmrg 
8848fb7bfaSmrg   data.state = state;
8948fb7bfaSmrg   data.f = f;
9048fb7bfaSmrg   backtrace_full (state, skip + 1, print_callback, error_callback,
9148fb7bfaSmrg 		  (void *) &data);
9248fb7bfaSmrg }
93