1
0

stack_trace.inc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2012 Zeex
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the
  8. // Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #if defined STACK_TRACE_INC
  21. #endinput
  22. #endif
  23. #define STACK_TRACE_INC
  24. #tryinclude <console>
  25. #include "amx_base"
  26. #include "amx_header"
  27. #include "frame_info"
  28. stock GetFunctionFromReturnAddress(ret_addr) {
  29. new addr = ret_addr - 4;
  30. #emit load.s.alt addr
  31. #emit lctrl 0
  32. #emit add
  33. #emit move.alt
  34. #emit lctrl 1
  35. #emit sub.alt
  36. #emit stor.s.pri addr
  37. #emit lref.s.alt addr
  38. #emit lctrl 0
  39. #emit sub.alt
  40. #emit stor.s.pri addr
  41. return addr - GetAmxBaseAddress();
  42. }
  43. stock GetStackTrace(trace[], skip = 0, max = sizeof(trace)) {
  44. new frm_addr;
  45. #emit lctrl 5
  46. #emit stor.s.pri frm_addr
  47. new length = 0;
  48. while (length < max) {
  49. new ret_addr = GetFrameReturn(frm_addr);
  50. if (length >= skip) {
  51. trace[length] = ret_addr;
  52. }
  53. if (ret_addr == 0) {
  54. break;
  55. }
  56. frm_addr = GetFramePreviousFrame(frm_addr);
  57. if (frm_addr == 0) {
  58. break;
  59. }
  60. length++;
  61. }
  62. return length;
  63. }
  64. stock PrintStackTrace(trace[], max = sizeof(trace)) {
  65. print("Stack trace:");
  66. new i = 0;
  67. for (; trace[i] != 0 && i < max - 1; i++) {
  68. new name[32];
  69. new address = GetFunctionFromReturnAddress(trace[i + 1]);
  70. if (GetPublicNameFromAddress(address, name)) {
  71. printf(" %s[%08x]", name, trace[i]);
  72. } else {
  73. printf(" ??[%08x]", trace[i]);
  74. }
  75. }
  76. printf(" ??[%08x]", trace[i]);
  77. }