source.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* List lines of source files for GDB, the GNU debugger.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include <stdio.h>
  18. #include <sys/param.h>
  19. #include <sys/stat.h>
  20. #include <sys/file.h>
  21. #include "defs.h"
  22. #include "initialize.h"
  23. #include "symtab.h"
  24. /* Path of directories to search for source files.
  25. Same format as the PATH environment variable's value. */
  26. static char *source_path;
  27. /* Symtab of default file for listing lines of. */
  28. struct symtab *current_source_symtab;
  29. /* Default next line to list. */
  30. int current_source_line;
  31. /* Line for "info line" to work on if no line specified. */
  32. static int line_info_default_line;
  33. /* First line number listed by last listing command. */
  34. static int first_line_listed;
  35. START_FILE
  36. static void
  37. directories_info ()
  38. {
  39. printf ("Source directories searched: %s\n", source_path);
  40. }
  41. static void
  42. init_source_path ()
  43. {
  44. register struct symtab *s;
  45. char wd[MAXPATHLEN];
  46. getwd (wd);
  47. source_path = savestring (wd, strlen (wd));
  48. /* Forget what we learned about line positions in source files;
  49. must check again now since files may be found in
  50. a different directory now. */
  51. for (s = symtab_list; s; s = s->next)
  52. if (s->line_charpos != 0)
  53. {
  54. free (s->line_charpos);
  55. s->line_charpos = 0;
  56. }
  57. }
  58. void
  59. directory_command (dirname, from_tty)
  60. char *dirname;
  61. int from_tty;
  62. {
  63. char *old = source_path;
  64. char wd[MAXPATHLEN];
  65. getwd (wd);
  66. if (dirname == 0)
  67. {
  68. if (query ("Reinitialize source path to %s? ", wd))
  69. {
  70. init_source_path ();
  71. free (old);
  72. }
  73. }
  74. else
  75. {
  76. struct stat st;
  77. register int len = strlen (dirname);
  78. register char *tem;
  79. extern char *index ();
  80. if (index (dirname, ':'))
  81. error ("Please add one directory at a time to the source path.");
  82. if (dirname[len - 1] == '/')
  83. /* Sigh. "foo/" => "foo" */
  84. dirname[--len] == '\0';
  85. while (dirname[len - 1] == '.')
  86. {
  87. if (len == 1)
  88. {
  89. /* "." => getwd () */
  90. dirname = wd;
  91. goto append;
  92. }
  93. else if (dirname[len - 2] == '/')
  94. {
  95. if (len == 2)
  96. {
  97. /* "/." => "/" */
  98. dirname[--len] = '\0';
  99. goto append;
  100. }
  101. else
  102. {
  103. /* "...foo/." => "...foo" */
  104. dirname[len -= 2] = '\0';
  105. continue;
  106. }
  107. }
  108. break;
  109. }
  110. if (dirname[0] != '/')
  111. dirname = concat (wd, "/", dirname);
  112. else
  113. dirname = savestring (dirname, len);
  114. make_cleanup (free_current_contents, &dirname);
  115. if (stat (dirname, &st) < 0)
  116. perror_with_name (dirname);
  117. if ((st.st_mode & S_IFMT) != S_IFDIR)
  118. error ("%s is not a directory.", dirname);
  119. append:
  120. len = strlen (dirname);
  121. tem = source_path;
  122. while (1)
  123. {
  124. if (!strncmp (tem, dirname, len)
  125. && (tem[len] == '\0' || tem[len] == ':'))
  126. {
  127. printf ("\"%s\" is already in the source path.\n",
  128. dirname);
  129. break;
  130. }
  131. tem = index (tem, ':');
  132. if (tem)
  133. tem++;
  134. else
  135. {
  136. source_path = concat (old, ":", dirname);
  137. free (old);
  138. break;
  139. }
  140. }
  141. if (from_tty)
  142. directories_info ();
  143. }
  144. }
  145. /* Open a file named STRING, searching path PATH (dir names sep by colons)
  146. using mode MODE and protection bits PROT in the calls to open.
  147. If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
  148. (ie pretend the first element of PATH is ".")
  149. If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
  150. the actual file opened (this string will always start with a "/"
  151. If a file is found, return the descriptor.
  152. Otherwise, return -1, with errno set for the last name we tried to open. */
  153. /* >>>> This should only allow files of certain types,
  154. >>>> eg executable, non-directory */
  155. int
  156. openp (path, try_cwd_first, string, mode, prot, filename_opened)
  157. char *path;
  158. int try_cwd_first;
  159. char *string;
  160. int mode;
  161. int prot;
  162. char **filename_opened;
  163. {
  164. register int fd;
  165. register char *filename;
  166. register char *p, *p1;
  167. register int len;
  168. /* ./foo => foo */
  169. while (string[0] == '.' && string[1] == '/')
  170. string += 2;
  171. if (try_cwd_first || string[0] == '/')
  172. {
  173. filename = string;
  174. fd = open (filename, mode, prot);
  175. if (fd >= 0 || string[0] == '/')
  176. goto done;
  177. }
  178. filename = (char *) alloca (strlen (path) + strlen (string) + 2);
  179. fd = -1;
  180. for (p = path; p; p = p1 ? p1 + 1 : 0)
  181. {
  182. p1 = (char *) index (p, ':');
  183. if (p1)
  184. len = p1 - p;
  185. else
  186. len = strlen (p);
  187. strncpy (filename, p, len);
  188. filename[len] = 0;
  189. strcat (filename, "/");
  190. strcat (filename, string);
  191. fd = open (filename, mode, prot);
  192. if (fd >= 0) break;
  193. }
  194. done:
  195. if (filename_opened)
  196. if (fd < 0)
  197. *filename_opened = (char *) 0;
  198. else if (filename[0] == '/')
  199. *filename_opened = savestring (filename, strlen (filename));
  200. else
  201. {
  202. char dirname[MAXPATHLEN];
  203. getwd (dirname);
  204. *filename_opened = concat (dirname, "/", filename);
  205. }
  206. return fd;
  207. }
  208. /* Set the source file default for the "list" command,
  209. specifying a symtab. */
  210. void
  211. select_source_symtab (s)
  212. register struct symtab *s;
  213. {
  214. /* Use the last symtab in the list
  215. which is actually the first found in the file's symbol table. */
  216. if (s)
  217. {
  218. while (s->next) s = s->next;
  219. current_source_symtab = s;
  220. current_source_line = 1;
  221. }
  222. }
  223. static void
  224. find_source_lines (s, desc)
  225. struct symtab *s;
  226. int desc;
  227. {
  228. struct stat st;
  229. register char *data, *p, *end;
  230. int nlines = 0;
  231. int lines_allocated = 1000;
  232. int *line_charpos = (int *) xmalloc (lines_allocated * sizeof (int));
  233. extern int exec_mtime;
  234. fstat (desc, &st);
  235. if (get_exec_file () != 0 && exec_mtime < st.st_mtime)
  236. printf ("Source file is more recent than executable.\n");
  237. data = (char *) alloca (st.st_size);
  238. myread (desc, data, st.st_size, s->filename);
  239. end = data + st.st_size;
  240. p = data;
  241. line_charpos[0] = 0;
  242. nlines = 1;
  243. while (p != end)
  244. {
  245. if (*p++ == '\n')
  246. {
  247. if (nlines == lines_allocated)
  248. line_charpos = (int *) xrealloc (line_charpos,
  249. sizeof (int) * (lines_allocated *= 2));
  250. line_charpos[nlines++] = p - data;
  251. }
  252. }
  253. s->nlines = nlines;
  254. s->line_charpos = (int *) xrealloc (line_charpos, nlines * sizeof (int));
  255. }
  256. /* Print source lines from the file of symtab S,
  257. starting with line number LINE and stopping before line number STOPLINE. */
  258. void
  259. print_source_lines (s, line, stopline)
  260. struct symtab *s;
  261. int line, stopline;
  262. {
  263. register int c;
  264. register int desc;
  265. register FILE *stream;
  266. int nlines = stopline - line;
  267. desc = openp (source_path, 0, s->filename, O_RDONLY, 0, (char **) 0);
  268. if (desc < 0)
  269. perror_with_name (s->filename);
  270. if (s->line_charpos == 0)
  271. find_source_lines (s, desc);
  272. if (line < 1 || line >= s->nlines)
  273. {
  274. close (desc);
  275. error ("Line number out of range; %s has %d lines.",
  276. s->filename, s->nlines);
  277. }
  278. if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
  279. {
  280. close (desc);
  281. perror_with_name (s->filename);
  282. }
  283. current_source_symtab = s;
  284. current_source_line = line;
  285. first_line_listed = line;
  286. stream = fdopen (desc, "r");
  287. clearerr (stream);
  288. while (nlines-- > 0)
  289. {
  290. c = fgetc (stream);
  291. if (c == EOF) break;
  292. line_info_default_line = current_source_line;
  293. printf ("%d\t", current_source_line++);
  294. do
  295. {
  296. if (c < 040 && c != '\t' && c != '\n')
  297. {
  298. fputc ('^', stdout);
  299. fputc (c + 0100, stdout);
  300. }
  301. else if (c == 0177)
  302. printf ("^?");
  303. else
  304. fputc (c, stdout);
  305. } while (c != '\n' && (c = fgetc (stream)) >= 0);
  306. }
  307. fclose (stream);
  308. }
  309. static void
  310. list_command (arg, from_tty)
  311. char *arg;
  312. int from_tty;
  313. {
  314. struct symtab_and_line sal, sal_end;
  315. struct symbol *sym;
  316. char *arg1;
  317. int no_end = 1;
  318. int dummy_end = 0;
  319. int dummy_beg = 0;
  320. if (symtab_list == 0)
  321. error ("Listing source lines requires symbols.");
  322. /* "l" or "l +" lists next ten lines. */
  323. if (arg == 0 || !strcmp (arg, "+"))
  324. {
  325. if (current_source_symtab == 0)
  326. error ("No source file specified yet. Do \"help list\".");
  327. print_source_lines (current_source_symtab, current_source_line,
  328. current_source_line + 10);
  329. return;
  330. }
  331. /* "l -" lists previous ten lines, the ones before the ten just listed. */
  332. if (!strcmp (arg, "-"))
  333. {
  334. if (current_source_symtab == 0)
  335. error ("No source file specified yet. Do \"help list\".");
  336. print_source_lines (current_source_symtab,
  337. max (first_line_listed - 10, 1),
  338. first_line_listed);
  339. return;
  340. }
  341. /* Now if there is only one argument, decode it in SAL
  342. and set NO_END.
  343. If there are two arguments, decode them in SAL and SAL_END
  344. and clear NO_END; however, if one of the arguments is blank,
  345. set DUMMY_BEG or DUMMY_END to record that fact. */
  346. arg1 = arg;
  347. if (*arg1 == ',')
  348. dummy_beg = 1;
  349. else
  350. sal = decode_line_1 (&arg1, 0, 0, 0);
  351. while (*arg1 == ' ' || *arg1 == '\t')
  352. arg1++;
  353. if (*arg1 == ',')
  354. {
  355. no_end = 0;
  356. arg1++;
  357. while (*arg1 == ' ' || *arg1 == '\t')
  358. arg1++;
  359. if (*arg1 == 0)
  360. dummy_end = 1;
  361. else if (dummy_beg)
  362. sal_end = decode_line_1 (&arg1, 0, 0, 0);
  363. else
  364. sal_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line);
  365. }
  366. if (*arg1)
  367. error ("Junk at end of line specification.");
  368. if (!no_end && !dummy_beg && !dummy_end
  369. && sal.symtab != sal_end.symtab)
  370. error ("Specified start and end are in different files.");
  371. if (dummy_beg && dummy_end)
  372. error ("Two empty args do not say what lines to list.");
  373. /* if line was specified by address,
  374. first print exactly which line, and which file.
  375. In this case, sal.symtab == 0 means address is outside
  376. of all known source files, not that user failed to give a filename. */
  377. if (*arg == '*')
  378. {
  379. if (sal.symtab == 0)
  380. error ("No source file for address 0x%x.", sal.pc);
  381. sym = find_pc_function (sal.pc);
  382. if (sym)
  383. printf ("0x%x is in %s (%s, line %d).\n",
  384. sal.pc, SYMBOL_NAME (sym), sal.symtab->filename, sal.line);
  385. else
  386. printf ("0x%x is in %s, line %d.\n",
  387. sal.pc, sal.symtab->filename, sal.line);
  388. }
  389. /* If this command is repeated with RET,
  390. turn it into the no-arg variant. */
  391. if (from_tty)
  392. *arg = 0;
  393. if (dummy_beg && sal_end.symtab == 0)
  394. error ("No source file specified yet. Do \"help list\".");
  395. if (dummy_beg)
  396. print_source_lines (sal_end.symtab, max (sal_end.line - 9, 1),
  397. sal_end.line + 1);
  398. else if (sal.symtab == 0)
  399. error ("No source file specified yet. Do \"help list\".");
  400. else if (no_end)
  401. print_source_lines (sal.symtab, max (sal.line - 5, 1), sal.line + 5);
  402. else
  403. print_source_lines (sal.symtab, sal.line,
  404. dummy_end ? sal.line + 10 : sal_end.line + 1);
  405. }
  406. /* Print info on range of pc's in a specified line. */
  407. static void
  408. line_info (arg, from_tty)
  409. char *arg;
  410. int from_tty;
  411. {
  412. struct symtab_and_line sal;
  413. int start_pc, end_pc;
  414. if (arg == 0)
  415. {
  416. sal.symtab = current_source_symtab;
  417. sal.line = line_info_default_line;
  418. }
  419. else
  420. {
  421. sal = decode_line_spec (arg);
  422. /* If this command is repeated with RET,
  423. turn it into the no-arg variant. */
  424. if (from_tty)
  425. *arg = 0;
  426. }
  427. if (sal.symtab == 0)
  428. error ("No source file specified.");
  429. if (sal.line > 0
  430. && find_line_pc_range (sal.symtab, sal.line, &start_pc, &end_pc))
  431. {
  432. if (start_pc == end_pc)
  433. printf ("Line %d of \"%s\" is at pc 0x%x but contains no code.\n",
  434. sal.line, sal.symtab->filename, start_pc);
  435. else
  436. printf ("Line %d of \"%s\" starts at pc 0x%x and ends at 0x%x.\n",
  437. sal.line, sal.symtab->filename, start_pc, end_pc);
  438. /* x/i should display this line's code. */
  439. set_next_address (start_pc);
  440. /* Repeating "info line" should do the following line. */
  441. line_info_default_line = sal.line + 1;
  442. }
  443. else
  444. printf ("Line number %d is out of range for \"%s\".\n",
  445. sal.line, sal.symtab->filename);
  446. }
  447. static
  448. initialize ()
  449. {
  450. current_source_symtab = 0;
  451. init_source_path ();
  452. add_com ("directory", class_files, directory_command,
  453. "Add directory DIR to end of search path for source files.\n\
  454. With no argument, reset the search path to just the working directory\n\
  455. and forget cached info on line positions in source files.");
  456. add_info ("directories", directories_info,
  457. "Current search path for finding source files.");
  458. add_info ("line", line_info,
  459. "Core addresses of the code for a source line.\n\
  460. Line can be specified as\n\
  461. LINENUM, to list around that line in current file,\n\
  462. FILE:LINENUM, to list around that line in that file,\n\
  463. FUNCTION, to list around beginning of that function,\n\
  464. FILE:FUNCTION, to distinguish among like-named static functions.\n\
  465. Default is to describe the last source line that was listed.\n\n\
  466. This sets the default address for \"x\" to the line's first instruction\n\
  467. so that \"x/i\" suffices to start examining the machine code.\n\
  468. The address is also stored as the value of \"$_\".");
  469. add_com ("list", class_files, list_command,
  470. "List specified function or line.\n\
  471. With no argument, lists ten more lines after or around previous listing.\n\
  472. \"list -\" lists the ten lines before a previous ten-line listing.\n\
  473. One argument specifies a line, and ten lines are listed around that line.\n\
  474. Two arguments with comma between specify starting and ending lines to list.\n\
  475. Lines can be specified in these ways:\n\
  476. LINENUM, to list around that line in current file,\n\
  477. FILE:LINENUM, to list around that line in that file,\n\
  478. FUNCTION, to list around beginning of that function,\n\
  479. FILE:FUNCTION, to distinguish among like-named static functions.\n\
  480. *ADDRESS, to list around the line containing that address.\n\
  481. With two args if one is empty it stands for ten lines away from the other arg.");
  482. }
  483. END_FILE