/librhgdb/symify.c

https://gitlab.com/OpenSourceMirror/rhide · C · 222 lines · 203 code · 17 blank · 2 comment · 51 complexity · 2f0f3bc7e2607ff20185419f38bdd00a MD5 · raw file

  1. /* Copyright (C) 1996-2000 Robert H”hne, see COPYING.RH for details */
  2. /* This file is part of RHIDE. */
  3. #include <libgdbrh.h>
  4. #include <unistd.h>
  5. #include <librhgdb.h>
  6. void
  7. symify(const char *s, char **function, char **file, int *line, int _diff)
  8. {
  9. struct minimal_symbol *msymbol = NULL;
  10. struct symtab_and_line sal;
  11. struct symtabs_and_lines sals;
  12. struct symtab *symtab;
  13. struct symbol *symbol;
  14. unsigned long core;
  15. long diff;
  16. char *tmp = alloca(100);
  17. int sl;
  18. char diff_string[100];
  19. *function = NULL;
  20. *file = NULL;
  21. sscanf(s, "%lx", &core);
  22. core += _diff;
  23. sprintf(tmp, "*%ld", core);
  24. sals = decode_line_spec(tmp, 0);
  25. if (!sals.sals[0].symtab)
  26. {
  27. msymbol = lookup_minimal_symbol_by_pc(core);
  28. if (!msymbol)
  29. {
  30. *line = 0;
  31. return;
  32. }
  33. diff = core - SYMBOL_VALUE_ADDRESS(msymbol);
  34. sl = strlen(SYMBOL_NAME(msymbol));
  35. sprintf(diff_string, "%+ld", diff);
  36. sl += strlen(diff_string);
  37. *function = (char *) malloc(sl + 1);
  38. strcpy(*function, SYMBOL_NAME(msymbol));
  39. strcat(*function, diff_string);
  40. *line = 0;
  41. return;
  42. }
  43. sal = find_pc_line(core, 1);
  44. symbol = find_pc_function(core);
  45. symtab = sal.symtab;
  46. sl = strlen(SYMBOL_SOURCE_NAME(symbol));
  47. *function = (char *) malloc(sl + 1);
  48. strcpy(*function, SYMBOL_SOURCE_NAME(symbol));
  49. sl = strlen(symtab->filename);
  50. *file = (char *) malloc(sl + 1);
  51. strcpy(*file, symtab->filename);
  52. *line = sal.line;
  53. }
  54. #ifdef TEST
  55. char *progname;
  56. static char *
  57. GetProgName()
  58. {
  59. return progname;
  60. }
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #ifdef __DJGPP__
  65. #include <pc.h>
  66. #define SC(r,c) (*(char *)(sc + (r)*ScreenCols() + (c)))
  67. #define SW(r,c) (*(sc + (r)*ScreenCols() + (c)))
  68. #endif
  69. int
  70. main(int argc, char *argv[])
  71. {
  72. char *function, *file;
  73. int lineno;
  74. #ifdef __DJGPP__
  75. int r, c;
  76. short *sc;
  77. char buf[1024];
  78. int i;
  79. #endif
  80. int diff = 0;
  81. unsigned v;
  82. FILE *ofile = 0;
  83. FILE *ifile = 0;
  84. if (argc < 2)
  85. {
  86. fprintf(stderr,
  87. "Usage: gsymify [-o <outfile>] [-i <tracefile>] [-a <adjust>] <program>\n");
  88. fprintf(stderr,
  89. "This program adds debug information to DJGPP program call frame tracebacks\n");
  90. return 1;
  91. }
  92. while (argv[1][0] == '-')
  93. {
  94. if ((strcmp(argv[1], "-a") == 0) && (argc > 3))
  95. {
  96. diff = atoi(argv[2]);
  97. argc -= 2;
  98. argv += 2;
  99. }
  100. if ((strcmp(argv[1], "-o") == 0) && (argc > 3))
  101. {
  102. ofile = fopen(argv[2], "w");
  103. if (ofile == 0)
  104. fprintf(stderr, "Error: unable to open file %s\n", argv[2]);
  105. argc -= 2;
  106. argv += 2;
  107. }
  108. if ((strcmp(argv[1], "-i") == 0) && (argc > 3))
  109. {
  110. ifile = fopen(argv[2], "r");
  111. if (ifile == 0)
  112. fprintf(stderr, "Error: unable to open file %s\n", argv[2]);
  113. argc -= 2;
  114. argv += 2;
  115. }
  116. }
  117. progname = argv[1];
  118. _GetProgName = GetProgName;
  119. InitRHGDB();
  120. if (!isatty(0) && !ifile)
  121. ifile = stdin;
  122. if (!isatty(1) && !ofile)
  123. ofile = stdout;
  124. if (ifile)
  125. {
  126. char line[1000];
  127. if (ofile == 0)
  128. ofile = stdout;
  129. while (fgets(line, 1000, ifile))
  130. {
  131. if (strncmp(line, " 0x", 4) == 0)
  132. {
  133. sscanf(line + 4, "%x", &v);
  134. symify(line + 4, &function, &file, &lineno, diff);
  135. fprintf(ofile, " 0x%08x", v);
  136. if (function)
  137. {
  138. fprintf(ofile, " %s", function);
  139. free(function);
  140. if (file)
  141. {
  142. fprintf(ofile, ", line %d of %s", lineno, file);
  143. free(file);
  144. }
  145. }
  146. fprintf(ofile, "\n");
  147. }
  148. else
  149. fputs(line, ofile);
  150. }
  151. fclose(ifile);
  152. fclose(ofile);
  153. return 0;
  154. }
  155. #ifdef __DJGPP__
  156. sc = (short *) malloc(ScreenRows() * ScreenCols() * 2);
  157. ScreenRetrieve(sc);
  158. for (r = 0; r < ScreenRows(); r++)
  159. {
  160. if (SC(r, 0) == ' ' && SC(r, 1) == ' ' && SC(r, 2) == '0'
  161. && SC(r, 3) == 'x')
  162. {
  163. buf[8] = 0;
  164. for (i = 0; i < 8; i++)
  165. buf[i] = SC(r, i + 4);
  166. sscanf(buf, "%x", &v);
  167. symify(buf, &function, &file, &lineno, diff);
  168. buf[0] = 0;
  169. if (function)
  170. {
  171. strcpy(buf, function);
  172. free(function);
  173. }
  174. if (file)
  175. {
  176. if (buf[0])
  177. strcat(buf, ", ");
  178. sprintf(buf + strlen(buf), "line %d of %s", lineno, file);
  179. free(file);
  180. }
  181. if (buf[0])
  182. for (i = 0; buf[i]; i++)
  183. SW(r, 15 + i) = 0x0f00 + buf[i];
  184. }
  185. }
  186. if (ofile)
  187. {
  188. for (r = 0; r < ScreenRows(); r++)
  189. {
  190. c = 0;
  191. for (i = 0; i < ScreenCols(); i++)
  192. if (SC(r, i) != ' ')
  193. c = i;
  194. for (i = 0; i <= c; i++)
  195. fputc(SC(r, i), ofile);
  196. fputc('\n', ofile);
  197. }
  198. fclose(ofile);
  199. }
  200. else
  201. ScreenUpdate(sc);
  202. #endif
  203. return 0;
  204. }
  205. #endif