debug.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 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. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Igor van den Hoven 2004 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. #define MAX_STACK_SIZE 51
  27. #define MAX_DEBUG_SIZE 400
  28. char debug_stack[MAX_STACK_SIZE][MAX_DEBUG_SIZE];
  29. short debug_index;
  30. int push_call(char *f, ...)
  31. {
  32. va_list ap;
  33. if (debug_index < MAX_STACK_SIZE)
  34. {
  35. va_start(ap, f);
  36. vsnprintf(debug_stack[debug_index], MAX_DEBUG_SIZE - 1, f, ap);
  37. va_end(ap);
  38. }
  39. if (++debug_index == 10000)
  40. {
  41. dump_stack();
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. void pop_call(void)
  47. {
  48. if (debug_index > 0)
  49. {
  50. debug_index--;
  51. }
  52. else
  53. {
  54. tintin_printf2(gtd->ses, "pop_call: index is zero.");
  55. dump_full_stack();
  56. }
  57. }
  58. void dump_stack(void)
  59. {
  60. unsigned char i;
  61. if (gtd && gtd->ses)
  62. {
  63. for (i = 0 ; i < debug_index && i < MAX_STACK_SIZE ; i++)
  64. {
  65. 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]);
  66. }
  67. }
  68. else
  69. {
  70. for (i = 0 ; i < debug_index && i < MAX_STACK_SIZE ; i++)
  71. {
  72. 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]);
  73. }
  74. }
  75. }
  76. void dump_stack_fatal(void)
  77. {
  78. unsigned char i;
  79. for (i = 0 ; i < debug_index && i < MAX_STACK_SIZE ; i++)
  80. {
  81. printf("\e[1;32mDEBUG_STACK[\e[1;31m%03d\e[1;32m] = \e[1;31m%s\e[0m\n", i, debug_stack[i]);
  82. }
  83. }
  84. void dump_full_stack(void)
  85. {
  86. unsigned char i;
  87. tintin_header(gtd->ses, " FULL DEBUG STACK ");
  88. for (i = 0 ; i < MAX_STACK_SIZE ; i++)
  89. {
  90. if (*debug_stack[i])
  91. {
  92. tintin_printf2(gtd->ses, "\e[1;31mDEBUG_STACK[%03d] = %s", i, debug_stack[i]);
  93. }
  94. }
  95. tintin_header(gtd->ses, "");
  96. }