print.c 2.7 KB

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