backtrace.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2013, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Asterisk backtrace generation
  20. *
  21. * This file provides backtrace generation utilities
  22. */
  23. #ifndef BACKTRACE_H_
  24. #define BACKTRACE_H_
  25. #define AST_MAX_BT_FRAMES 32
  26. #ifdef HAVE_BKTR
  27. #define ast_bt_get_addresses(bt) __ast_bt_get_addresses((bt))
  28. #define ast_bt_create() __ast_bt_create()
  29. #define ast_bt_destroy(bt) __ast_bt_destroy((bt))
  30. #define ast_bt_get_symbols(addresses, num_frames) __ast_bt_get_symbols((addresses), (num_frames))
  31. #define ast_bt_free_symbols(string_vector) __ast_bt_free_symbols((string_vector))
  32. #else
  33. #define ast_bt_get_addresses(bt) 0
  34. #define ast_bt_create() NULL
  35. #define ast_bt_destroy(bt) NULL
  36. #define ast_bt_get_symbols(addresses, num_frames) NULL
  37. #define ast_bt_free_symbols(string_vector) NULL
  38. #endif
  39. /* \brief
  40. *
  41. * A structure to hold backtrace information. This structure provides an easy means to
  42. * store backtrace information or pass backtraces to other functions.
  43. */
  44. struct ast_bt {
  45. /*! The addresses of the stack frames. This is filled in by calling the glibc backtrace() function */
  46. void *addresses[AST_MAX_BT_FRAMES];
  47. /*! The number of stack frames in the backtrace */
  48. int num_frames;
  49. /*! Tells if the ast_bt structure was dynamically allocated */
  50. unsigned int alloced:1;
  51. };
  52. #ifdef HAVE_BKTR
  53. /* \brief
  54. * Allocates memory for an ast_bt and stores addresses and symbols.
  55. *
  56. * \return Returns NULL on failure, or the allocated ast_bt on success
  57. * \since 1.6.1
  58. */
  59. struct ast_bt *__ast_bt_create(void);
  60. /* \brief
  61. * Fill an allocated ast_bt with addresses
  62. *
  63. * \retval 0 Success
  64. * \retval -1 Failure
  65. * \since 1.6.1
  66. */
  67. int __ast_bt_get_addresses(struct ast_bt *bt);
  68. /* \brief
  69. *
  70. * Free dynamically allocated portions of an ast_bt
  71. *
  72. * \retval NULL.
  73. * \since 1.6.1
  74. */
  75. void *__ast_bt_destroy(struct ast_bt *bt);
  76. /* \brief Retrieve symbols for a set of backtrace addresses
  77. *
  78. * \param addresses A list of addresses, such as the ->addresses structure element of struct ast_bt.
  79. * \param num_frames Number of addresses in the addresses list
  80. *
  81. * \retval NULL Unable to allocate memory
  82. * \return Vector of strings. Free with ast_bt_free_symbols
  83. *
  84. * \note The first frame in the addresses array will usually point to __ast_bt_create
  85. * so when printing the symbols you may wish to start at position 1 rather than 0.
  86. *
  87. * \since 1.6.2.16
  88. */
  89. struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames);
  90. /* \brief Free symbols returned from ast_bt_get_symbols
  91. *
  92. * \param symbols The symbol string vector
  93. *
  94. * \since 13.24.0
  95. */
  96. void __ast_bt_free_symbols(struct ast_vector_string *symbols);
  97. #endif /* HAVE_BKTR */
  98. #endif /* BACKTRACE_H_ */