PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-15/SWIG/Tools/WAD/Wad/vars.c

#
C | 271 lines | 202 code | 25 blank | 44 comment | 34 complexity | cd5957424cc8a1eedd15c0c870e62807 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * vars.c
  3. *
  4. * This file examines the stack trace and tries to make some sense out of
  5. * collected debugging information. This includes locating the data on
  6. * the stack and/or registers.
  7. *
  8. * This feature is detached from the debugging info collector to make
  9. * it independent of debugging formats.
  10. *
  11. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  12. *
  13. * Copyright (C) 2000. The University of Chicago.
  14. *
  15. * This library is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2.1 of the License, or (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with this library; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. * See the file COPYING for a complete copy of the LGPL.
  30. * ----------------------------------------------------------------------------- */
  31. #include "wad.h"
  32. static char cvs[] = "$Header$";
  33. /* -----------------------------------------------------------------------------
  34. * wad_build_vars()
  35. *
  36. * Build variable information for a single stack frame
  37. * ----------------------------------------------------------------------------- */
  38. void wad_build_vars(WadFrame *f) {
  39. char *stack = 0;
  40. char *nstack = 0;
  41. char *pstack = 0;
  42. WadLocal *loc;
  43. int n;
  44. stack = (char *) f->stack;
  45. if (f->next) {
  46. nstack = (char *) f->next->stack;
  47. }
  48. if (f->prev) {
  49. pstack = (char *) f->prev->stack;
  50. }
  51. for (n = 0; n < 2; n++) {
  52. if (n == 0) loc = f->debug_args;
  53. else loc = f->debug_locals;
  54. while (loc) {
  55. loc->ptr = 0;
  56. if (loc->loc == PARM_STACK) {
  57. if ((loc->stack >= 0) && (nstack)) {
  58. loc->ptr = (void *) (nstack + loc->stack);
  59. } else if (loc->stack < 0) {
  60. loc->ptr = (void *) (stack + f->stack_size + loc->stack);
  61. }
  62. loc->size = sizeof(long);
  63. }
  64. if (loc->loc == PARM_REGISTER) {
  65. /* Parameter is located in a register */
  66. #ifdef WAD_SOLARIS
  67. if ((loc->reg >= 24) && (loc->reg < 32)) {
  68. /* Value is located in the %in registers. */
  69. loc->ptr = (void *) (stack + (loc->reg - 16)*sizeof(int));
  70. loc->size = sizeof(int);
  71. } else if ((loc->reg >= 8) && (loc->reg < 16)) {
  72. /* Value is located in the %on registers */
  73. if (nstack) {
  74. loc->ptr = (void *) (stack + (loc->reg)*sizeof(int));
  75. loc->size = sizeof(int);
  76. }
  77. } else if ((loc->reg >= 16) && (loc->reg < 24)) {
  78. /* Value has been placed in the %ln registers */
  79. loc->ptr = (void *) (stack + (loc->reg - 16)*sizeof(int));
  80. loc->size = sizeof(int);
  81. }
  82. #endif
  83. }
  84. loc = loc->next;
  85. }
  86. }
  87. }
  88. /* This function creates a formatted integer given a pointer, size, and sign flag */
  89. static
  90. char *wad_format_int(char *ptr, int nbytes, int sgn) {
  91. static char fmt[128];
  92. unsigned char *s;
  93. int incr;
  94. unsigned long value = 0;
  95. int i;
  96. #ifdef WAD_LITTLE_ENDIAN
  97. s = (unsigned char *) (ptr + nbytes - 1);
  98. incr = -1;
  99. #else
  100. s = (unsigned char *) (ptr);
  101. incr = +1;
  102. #endif
  103. for (i = 0; i < nbytes; i++, s += incr) {
  104. value = (value << 8) + *s;
  105. }
  106. if (sgn) {
  107. return wad_format_signed((long) value,-1);
  108. } else {
  109. return wad_format_unsigned((unsigned long) value, -1);
  110. }
  111. return fmt;
  112. }
  113. /* Try to make a formatted version of a local */
  114. char *wad_format_var(WadLocal *l) {
  115. static char hexdigits[] = "0123456789abcdef";
  116. static char buffer[1024];
  117. double dval;
  118. float fval;
  119. buffer[0] = 0;
  120. switch(l->type) {
  121. case WAD_TYPE_INT32:
  122. wad_strcpy(buffer,wad_format_int(l->ptr,4,1));
  123. break;
  124. case WAD_TYPE_UINT32:
  125. wad_strcpy(buffer,wad_format_int(l->ptr,4,0));
  126. break;
  127. case WAD_TYPE_INT16:
  128. wad_strcpy(buffer,wad_format_int(l->ptr,2,1));
  129. break;
  130. case WAD_TYPE_UINT16:
  131. wad_strcpy(buffer,wad_format_int(l->ptr,2,0));
  132. break;
  133. case WAD_TYPE_INT8:
  134. wad_strcpy(buffer,wad_format_int(l->ptr,1,1));
  135. break;
  136. case WAD_TYPE_UINT8:
  137. wad_strcpy(buffer,wad_format_int(l->ptr,1,0));
  138. break;
  139. case WAD_TYPE_CHAR:
  140. buffer[0] = '\'';
  141. buffer[1] = *((char *) l->ptr);
  142. buffer[2] = '\'';
  143. buffer[3] = 0;
  144. break;
  145. case WAD_TYPE_FLOAT:
  146. wad_memcpy(&fval,l->ptr,sizeof(float));
  147. sprintf(buffer,"%g",fval);
  148. break;
  149. case WAD_TYPE_DOUBLE:
  150. wad_memcpy(&dval,l->ptr,sizeof(double));
  151. sprintf(buffer,"%g",dval);
  152. break;
  153. case WAD_TYPE_UNKNOWN:
  154. case WAD_TYPE_POINTER:
  155. default:
  156. /* Hmmm. Unknown data type. We'll just treat it as a word */
  157. if (l->ptr) {
  158. int incr,i;
  159. int b;
  160. int leading = 1;
  161. char *c;
  162. char *ptr;
  163. #ifdef WAD_LITTLE_ENDIAN
  164. ptr = ((char *) l->ptr) + 3;
  165. incr = -1;
  166. #else
  167. ptr = (char *) l->ptr;
  168. incr =1 ;
  169. #endif
  170. wad_strcat(buffer,"0x");
  171. c = buffer+2;
  172. for (i = 0; i < sizeof(void *); i++) {
  173. b = (int) *ptr;
  174. if (!leading || (b)) {
  175. if (!leading || (b & 0xf0))
  176. *(c++) = hexdigits[(b & 0xf0) >> 4];
  177. *(c++) = hexdigits[(b & 0xf)];
  178. leading = 0;
  179. }
  180. ptr += incr;
  181. }
  182. if (leading)
  183. *(c++) = '0';
  184. *c = 0;
  185. }
  186. }
  187. return buffer;
  188. }
  189. /* Convert a wad local variable to a long */
  190. long wad_local_as_long(WadLocal *loc) {
  191. long value = 0;
  192. int32 i32;
  193. int16 i16;
  194. int8 i8;
  195. uint32 u32;
  196. uint16 u16;
  197. uint8 u8;
  198. switch(loc->type) {
  199. case WAD_TYPE_INT32:
  200. wad_memcpy(&i32,loc->ptr,4);
  201. value = (long) i32;
  202. break;
  203. case WAD_TYPE_UINT32:
  204. wad_memcpy(&u32,loc->ptr,4);
  205. value = (long) u32;
  206. break;
  207. case WAD_TYPE_INT16:
  208. wad_memcpy(&i16,loc->ptr,2);
  209. value = (long) i16;
  210. break;
  211. case WAD_TYPE_UINT16:
  212. wad_memcpy(&u16,loc->ptr,2);
  213. value = (long) u16;
  214. break;
  215. case WAD_TYPE_INT8:
  216. case WAD_TYPE_CHAR:
  217. wad_memcpy(&i8, loc->ptr,1);
  218. value = (long) i8;
  219. break;
  220. case WAD_TYPE_UINT8:
  221. wad_memcpy(&u8, loc->ptr,1);
  222. value = (long) u8;
  223. break;
  224. default:
  225. wad_memcpy(&u32,loc->ptr,4);
  226. value = (long) u32;
  227. }
  228. return value;
  229. }
  230. /* Convert a wad local variable to a long */
  231. double wad_local_as_double(WadLocal *loc) {
  232. double value = 0;
  233. float fval;
  234. switch(loc->type) {
  235. case WAD_TYPE_DOUBLE:
  236. wad_memcpy(&value,loc->ptr,8);
  237. break;
  238. case WAD_TYPE_FLOAT:
  239. wad_memcpy(&fval,loc->ptr,4);
  240. value = (double) fval;
  241. break;
  242. default:
  243. value = 0;
  244. }
  245. return value;
  246. }