display.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * File display.c - display handling for Wine internal debugger.
  3. *
  4. * Copyright (C) 1997, Eric Youngdale.
  5. * Copyright (C) 2003, Michal Miroslaw
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "debugger.h"
  24. /* needs to be power of 2, search for MARK to see why :) */
  25. #define DISPTAB_DELTA 8
  26. struct display
  27. {
  28. struct expr* exp;
  29. int count;
  30. char format;
  31. char enabled;
  32. char func_buffer[sizeof(SYMBOL_INFO) + 256];
  33. SYMBOL_INFO* func;
  34. };
  35. static struct display *displaypoints = NULL;
  36. static unsigned int maxdisplays = 0, ndisplays = 0;
  37. static inline BOOL cmp_symbol(const SYMBOL_INFO* si1, const SYMBOL_INFO* si2)
  38. {
  39. /* FIXME: !memcmp(si1, si2, sizeof(SYMBOL_INFO) + si1->NameLen)
  40. * is wrong because sizeof(SYMBOL_INFO) can be aligned on 4-byte boundary
  41. * Note: we also need to zero out the structures before calling
  42. * stack_get_frame, so that un-touched fields by stack_get_frame
  43. * get the same value!!
  44. */
  45. return !memcmp(si1, si2, FIELD_OFFSET(SYMBOL_INFO, Name)) &&
  46. !memcmp(si1->Name, si2->Name, si1->NameLen);
  47. }
  48. BOOL display_add(struct expr *exp, int count, char format)
  49. {
  50. unsigned i;
  51. BOOL local_binding = FALSE;
  52. for (i = 0; i < ndisplays; i++)
  53. if (displaypoints[i].exp == NULL)
  54. break;
  55. if (i == maxdisplays)
  56. {
  57. /* no space left - expand */
  58. maxdisplays += DISPTAB_DELTA;
  59. displaypoints = dbg_heap_realloc(displaypoints,
  60. maxdisplays * sizeof(*displaypoints));
  61. }
  62. if (i == ndisplays) ndisplays++;
  63. displaypoints[i].exp = expr_clone(exp, &local_binding);
  64. displaypoints[i].count = count;
  65. displaypoints[i].format = format;
  66. displaypoints[i].enabled = TRUE;
  67. if (local_binding)
  68. {
  69. displaypoints[i].func = (SYMBOL_INFO*)displaypoints[i].func_buffer;
  70. memset(displaypoints[i].func, 0, sizeof(SYMBOL_INFO));
  71. displaypoints[i].func->SizeOfStruct = sizeof(SYMBOL_INFO);
  72. displaypoints[i].func->MaxNameLen = sizeof(displaypoints[i].func_buffer) -
  73. sizeof(*displaypoints[i].func);
  74. if (!stack_get_current_symbol(displaypoints[i].func))
  75. {
  76. expr_free(displaypoints[i].exp);
  77. displaypoints[i].exp = NULL;
  78. return FALSE;
  79. }
  80. }
  81. else displaypoints[i].func = NULL;
  82. return TRUE;
  83. }
  84. BOOL display_info(void)
  85. {
  86. unsigned i;
  87. char buffer[sizeof(SYMBOL_INFO) + 256];
  88. SYMBOL_INFO* func;
  89. const char* info;
  90. func = (SYMBOL_INFO*)buffer;
  91. memset(func, 0, sizeof(SYMBOL_INFO));
  92. func->SizeOfStruct = sizeof(SYMBOL_INFO);
  93. func->MaxNameLen = sizeof(buffer) - sizeof(*func);
  94. if (!stack_get_current_symbol(func)) return FALSE;
  95. for (i = 0; i < ndisplays; i++)
  96. {
  97. if (displaypoints[i].exp == NULL) continue;
  98. dbg_printf("%d: ", i + 1);
  99. expr_print(displaypoints[i].exp);
  100. if (displaypoints[i].enabled)
  101. {
  102. if (displaypoints[i].func && !cmp_symbol(displaypoints[i].func, func))
  103. info = " (out of scope)";
  104. else
  105. info = "";
  106. }
  107. else
  108. info = " (disabled)";
  109. if (displaypoints[i].func)
  110. dbg_printf(" in %s", displaypoints[i].func->Name);
  111. dbg_printf("%s\n", info);
  112. }
  113. return TRUE;
  114. }
  115. static void print_one_display(int i)
  116. {
  117. struct dbg_lvalue lvalue;
  118. if (displaypoints[i].enabled)
  119. {
  120. lvalue = expr_eval(displaypoints[i].exp);
  121. if (lvalue.type.id == dbg_itype_none)
  122. {
  123. dbg_printf("Unable to evaluate expression ");
  124. expr_print(displaypoints[i].exp);
  125. dbg_printf("\nDisabling display %d ...\n", i + 1);
  126. displaypoints[i].enabled = FALSE;
  127. return;
  128. }
  129. }
  130. dbg_printf("%d: ", i + 1);
  131. expr_print(displaypoints[i].exp);
  132. dbg_printf(" = ");
  133. if (!displaypoints[i].enabled)
  134. dbg_printf("(disabled)\n");
  135. else
  136. if (displaypoints[i].format == 'i')
  137. memory_examine(&lvalue, displaypoints[i].count, displaypoints[i].format);
  138. else
  139. print_value(&lvalue, displaypoints[i].format, 0);
  140. }
  141. BOOL display_print(void)
  142. {
  143. unsigned i;
  144. char buffer[sizeof(SYMBOL_INFO) + 256];
  145. SYMBOL_INFO* func;
  146. func = (SYMBOL_INFO*)buffer;
  147. memset(func, 0, sizeof(SYMBOL_INFO));
  148. func->SizeOfStruct = sizeof(SYMBOL_INFO);
  149. func->MaxNameLen = sizeof(buffer) - sizeof(*func);
  150. if (!stack_get_current_symbol(func)) return FALSE;
  151. for (i = 0; i < ndisplays; i++)
  152. {
  153. if (displaypoints[i].exp == NULL || !displaypoints[i].enabled)
  154. continue;
  155. if (displaypoints[i].func && !cmp_symbol(displaypoints[i].func, func))
  156. continue;
  157. print_one_display(i);
  158. }
  159. return TRUE;
  160. }
  161. BOOL display_delete(int displaynum)
  162. {
  163. if (displaynum > ndisplays || displaynum == 0 || displaynum < -1 ||
  164. displaypoints[displaynum - 1].exp == NULL)
  165. {
  166. dbg_printf("Invalid display number\n");
  167. return TRUE;
  168. }
  169. if (displaynum == -1)
  170. {
  171. unsigned i;
  172. for (i = 0; i < ndisplays; i++)
  173. {
  174. if (displaypoints[i].exp != NULL)
  175. {
  176. expr_free(displaypoints[i].exp);
  177. displaypoints[i].exp = NULL;
  178. }
  179. }
  180. maxdisplays = DISPTAB_DELTA;
  181. displaypoints = dbg_heap_realloc(displaypoints,
  182. (maxdisplays = DISPTAB_DELTA) * sizeof(*displaypoints));
  183. ndisplays = 0;
  184. }
  185. else if (displaypoints[--displaynum].exp != NULL)
  186. {
  187. expr_free(displaypoints[displaynum].exp);
  188. displaypoints[displaynum].exp = NULL;
  189. while (displaynum == ndisplays - 1 && displaypoints[displaynum].exp == NULL)
  190. {
  191. --ndisplays;
  192. --displaynum;
  193. }
  194. if (maxdisplays - ndisplays >= 2 * DISPTAB_DELTA)
  195. {
  196. /* MARK */
  197. maxdisplays = (ndisplays + DISPTAB_DELTA - 1) & ~(DISPTAB_DELTA - 1);
  198. displaypoints = dbg_heap_realloc(displaypoints,
  199. maxdisplays * sizeof(*displaypoints));
  200. }
  201. }
  202. return TRUE;
  203. }
  204. BOOL display_enable(int displaynum, int enable)
  205. {
  206. char buffer[sizeof(SYMBOL_INFO) + 256];
  207. SYMBOL_INFO* func;
  208. func = (SYMBOL_INFO*)buffer;
  209. memset(func, 0, sizeof(SYMBOL_INFO));
  210. func->SizeOfStruct = sizeof(SYMBOL_INFO);
  211. func->MaxNameLen = sizeof(buffer) - sizeof(*func);
  212. if (!stack_get_current_symbol(func)) return FALSE;
  213. --displaynum;
  214. if (displaynum >= ndisplays || displaynum < 0 ||
  215. displaypoints[displaynum].exp == NULL)
  216. {
  217. dbg_printf("Invalid display number\n");
  218. return TRUE;
  219. }
  220. displaypoints[displaynum].enabled = enable;
  221. if (!displaypoints[displaynum].func ||
  222. cmp_symbol(displaypoints[displaynum].func, func))
  223. {
  224. print_one_display(displaynum);
  225. }
  226. return TRUE;
  227. }