debug.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2004 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #define MAX_STACK_SIZE 100
  26. #define MAX_DEBUG_SIZE 400
  27. char debug_stack[MAX_STACK_SIZE][MAX_DEBUG_SIZE];
  28. int debug_index = 0;
  29. int push_call(char *f, ...)
  30. {
  31. va_list ap;
  32. if (debug_index < MAX_STACK_SIZE)
  33. {
  34. va_start(ap, f);
  35. vsnprintf(debug_stack[debug_index], MAX_DEBUG_SIZE - 1, f, ap);
  36. va_end(ap);
  37. }
  38. if (++debug_index == 10000)
  39. {
  40. dump_stack();
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. void pop_call(void)
  46. {
  47. if (debug_index > 0)
  48. {
  49. debug_index--;
  50. }
  51. else
  52. {
  53. tintin_printf2(gtd->ses, "pop_call: index is zero.");
  54. dump_full_stack();
  55. }
  56. }
  57. void dump_stack(void)
  58. {
  59. unsigned char i;
  60. if (gtd && gtd->ses)
  61. {
  62. for (i = 0 ; i < debug_index && i < MAX_STACK_SIZE ; i++)
  63. {
  64. tintin_printf2(gtd->ses, "\e[1;32mDEBUG_STACK[\e[1;31m%03d\e[1;32m] = \e[1;31m%s\e[0m", i, debug_stack[i]);
  65. }
  66. }
  67. else
  68. {
  69. for (i = 0 ; i < debug_index && i < MAX_STACK_SIZE ; i++)
  70. {
  71. tintin_printf2(gtd->ses, "\e[1;32mDEBUG_STACK[\e[1;31m%03d\e[1;32m] = \e[1;31m%s\e[0m\n", i, debug_stack[i]);
  72. }
  73. }
  74. }
  75. void dump_stack_fatal(void)
  76. {
  77. unsigned char i;
  78. for (i = 0 ; i < debug_index && i < MAX_STACK_SIZE ; i++)
  79. {
  80. print_stdout("\e[1;32mDEBUG_STACK[\e[1;31m%03d\e[1;32m] = \e[1;31m%s\e[0m\n", i, debug_stack[i]);
  81. }
  82. }
  83. void dump_full_stack(void)
  84. {
  85. unsigned char i;
  86. tintin_header(gtd->ses, " FULL DEBUG STACK ");
  87. for (i = 0 ; i < MAX_STACK_SIZE ; i++)
  88. {
  89. if (*debug_stack[i])
  90. {
  91. tintin_printf2(gtd->ses, "\e[1;31mDEBUG_STACK[%03d] = %s", i, debug_stack[i]);
  92. }
  93. }
  94. tintin_header(gtd->ses, "");
  95. }