core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* Work with core dump and executable files, for GDB.
  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/dir.h>
  20. #include <sys/file.h>
  21. #include <sys/user.h>
  22. #include <sys/stat.h>
  23. #include <a.out.h>
  24. #include "initialize.h"
  25. #include "defs.h"
  26. #include "param.h"
  27. #ifdef NEW_SUN_CORE
  28. #include <sys/core.h>
  29. #endif /* NEW_SUN_CORE */
  30. #ifndef N_TXTADDR
  31. #define N_TXTADDR(hdr) 0
  32. #endif /* no N_TXTADDR */
  33. #ifndef N_DATADDR
  34. #define N_DATADDR(hdr) hdr.a_text
  35. #endif /* no N_DATADDR */
  36. START_FILE
  37. /* File names of core file and executable file. */
  38. static char *corefile;
  39. static char *execfile;
  40. /* Descriptors on which core file and executable file are open. */
  41. static int corechan;
  42. static int execchan;
  43. /* Last modification time of executable file. */
  44. int exec_mtime;
  45. /* Virtual addresses of bounds of the two areas of memory in the core file. */
  46. static CORE_ADDR data_start;
  47. static CORE_ADDR data_end;
  48. static CORE_ADDR stack_start;
  49. static CORE_ADDR stack_end;
  50. /* Virtual addresses of bounds of two areas of memory in the exec file.
  51. Note that the data area in the exec file is used only when there is no core file. */
  52. static CORE_ADDR text_start;
  53. static CORE_ADDR text_end;
  54. static CORE_ADDR exec_data_start;
  55. static CORE_ADDR exec_data_end;
  56. /* Address in executable file of start of text area data. */
  57. static int text_offset;
  58. /* Address in executable file of start of data area data. */
  59. static int exec_data_offset;
  60. /* Address in core file of start of data area data. */
  61. static int data_offset;
  62. /* Address in core file of start of stack area data. */
  63. static int stack_offset;
  64. /* a.out header saved in core file. */
  65. struct exec core_aouthdr;
  66. /* a,out header of exec file. */
  67. struct exec exec_aouthdr;
  68. static void validate_files ();
  69. unsigned int register_addr ();
  70. core_file_command (filename, from_tty)
  71. char *filename;
  72. int from_tty;
  73. {
  74. int val;
  75. extern char registers[];
  76. if (corefile)
  77. free (corefile);
  78. corefile = 0;
  79. data_start = 0;
  80. data_end = 0;
  81. stack_start = STACK_END_ADDR;
  82. stack_end = STACK_END_ADDR;
  83. if (corechan >= 0)
  84. close (corechan);
  85. corechan = -1;
  86. if (filename)
  87. {
  88. if (have_inferior_p ())
  89. error ("To look at a core file, you must kill the inferior with \"kill\".");
  90. corechan = open (filename, O_RDONLY, 0);
  91. if (corechan < 0)
  92. perror_with_name (filename);
  93. #ifdef NEW_SUN_CORE
  94. {
  95. struct core corestr;
  96. val = myread (corechan, &corestr, sizeof corestr);
  97. if (val < 0)
  98. perror_with_name (filename);
  99. if (corestr.c_magic != CORE_MAGIC)
  100. error ("\"%s\" does not appear to be a core dump file (magic 0x%x, expected 0x%x)",
  101. filename, corestr.c_magic, (int) CORE_MAGIC);
  102. else if (sizeof (struct core) != corestr.c_len)
  103. error ("\"%s\" has an invalid struct core length (%d, expected %d)",
  104. filename, corestr.c_len, (int) sizeof (struct core));
  105. data_start = exec_data_start;
  106. data_end = data_start + corestr.c_dsize;
  107. stack_start = stack_end - corestr.c_ssize;
  108. data_offset = sizeof corestr;
  109. stack_offset = sizeof corestr + corestr.c_dsize;
  110. bcopy (&corestr.c_regs, registers, 16 * 4);
  111. *(int *)&registers[REGISTER_BYTE (PS_REGNUM)] = corestr.c_regs.r_ps;
  112. *(int *)&registers[REGISTER_BYTE (PC_REGNUM)] = corestr.c_regs.r_pc;
  113. bcopy (corestr.c_fpstatus.fps_regs,
  114. &registers[REGISTER_BYTE (FP0_REGNUM)],
  115. sizeof corestr.c_fpstatus.fps_regs);
  116. bcopy (&corestr.c_fpstatus.fps_control,
  117. &registers[REGISTER_BYTE (FPC_REGNUM)],
  118. sizeof corestr.c_fpstatus - sizeof corestr.c_fpstatus.fps_regs);
  119. bcopy (&corestr.c_aouthdr, &core_aouthdr, sizeof (struct exec));
  120. printf ("Core file is from \"%s\".\n", corestr.c_cmdname);
  121. }
  122. #else /* not NEW_SUN_CORE */
  123. {
  124. struct user u;
  125. int reg_offset;
  126. /* 4.2bsd-style core dump */
  127. val = myread (corechan, &u, sizeof u);
  128. if (val < 0)
  129. perror_with_name (filename);
  130. data_start = exec_data_start;
  131. data_end = data_start + NBPG * u.u_dsize;
  132. stack_start = stack_end - NBPG * u.u_ssize;
  133. data_offset = NBPG * UPAGES;
  134. stack_offset = NBPG * (UPAGES + u.u_dsize);
  135. reg_offset = (int) u.u_ar0 - KERNEL_U_ADDR;
  136. /* Read the register values out of the core file and store
  137. them where `read_register' will find them. */
  138. {
  139. register int regno;
  140. for (regno = 0; regno < NUM_REGS; regno++)
  141. {
  142. REGISTER_TYPE buf;
  143. val = lseek (corechan, register_addr (regno, reg_offset), 0);
  144. if (val < 0)
  145. perror_with_name (filename);
  146. val = myread (corechan, &buf, sizeof buf);
  147. if (val < 0)
  148. perror_with_name (filename);
  149. supply_register (regno, buf);
  150. }
  151. }
  152. /* I don't know where to find this info.
  153. So, for now, mark it as not available. */
  154. core_aouthdr.a_magic = 0;
  155. }
  156. #endif /* not NEW_SUN_CORE */
  157. if (filename[0] == '/')
  158. corefile = savestring (filename, strlen (filename));
  159. else
  160. {
  161. char dirname[MAXPATHLEN];
  162. getwd (dirname);
  163. corefile = concat (dirname, "/", filename);
  164. }
  165. set_current_frame (read_register (FP_REGNUM));
  166. select_frame (get_current_frame (), 0);
  167. validate_files ();
  168. }
  169. else if (from_tty)
  170. printf ("No core file now.\n");
  171. }
  172. exec_file_command (filename, from_tty)
  173. char *filename;
  174. int from_tty;
  175. {
  176. int val;
  177. if (execfile)
  178. free (execfile);
  179. execfile = 0;
  180. data_start = 0;
  181. data_end -= exec_data_start;
  182. text_start = 0;
  183. text_end = 0;
  184. exec_data_start = 0;
  185. exec_data_end = 0;
  186. if (execchan >= 0)
  187. close (execchan);
  188. execchan = -1;
  189. if (filename)
  190. {
  191. execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  192. &execfile);
  193. if (execchan < 0)
  194. perror_with_name (filename);
  195. val = myread (execchan, &exec_aouthdr, sizeof exec_aouthdr);
  196. if (val < 0)
  197. perror_with_name (filename);
  198. text_start = N_TXTADDR (exec_aouthdr);
  199. text_end = text_start + exec_aouthdr.a_text;
  200. text_offset = N_TXTOFF (exec_aouthdr);
  201. exec_data_start = N_DATADDR (exec_aouthdr);
  202. exec_data_end = exec_data_start + exec_aouthdr.a_data;
  203. exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  204. data_start = exec_data_start;
  205. data_end += exec_data_start;
  206. validate_files ();
  207. }
  208. else if (from_tty)
  209. printf ("No exec file now.\n");
  210. }
  211. /* If we have both a core file and an exec file,
  212. print a warning if they don't go together.
  213. This should really check that the core file came
  214. from that exec file, but I don't know how to do it. */
  215. static void
  216. validate_files ()
  217. {
  218. struct stat st_exec;
  219. if (execfile != 0)
  220. {
  221. fstat (execchan, &st_exec);
  222. exec_mtime = st_exec.st_mtime;
  223. if (corefile != 0)
  224. {
  225. struct stat st_core;
  226. fstat (corechan, &st_core);
  227. if (core_aouthdr.a_magic != 0
  228. && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
  229. printf ("Warning: core file does not match specified executable file.\n");
  230. else if (st_exec.st_mtime > st_core.st_mtime)
  231. printf ("Warning: exec file is newer than core file.\n");
  232. }
  233. }
  234. }
  235. char *
  236. get_exec_file ()
  237. {
  238. if (execfile == 0)
  239. error ("No executable file specified.\n\
  240. Use the \"exec-file\" and \"symbol-file\" commands.");
  241. return execfile;
  242. }
  243. int
  244. have_core_file_p ()
  245. {
  246. return corefile != 0;
  247. }
  248. static void
  249. files_info ()
  250. {
  251. char *symfile;
  252. extern char *get_sym_file ();
  253. if (execfile)
  254. printf ("Executable file \"%s\".\n", execfile);
  255. else
  256. printf ("No executable file\n");
  257. if (corefile == 0)
  258. printf ("No core dump file\n");
  259. else
  260. printf ("Core dump file \"%s\".\n", corefile);
  261. if (have_inferior_p ())
  262. printf ("Using the running image of the program, rather than these files.\n");
  263. symfile = get_sym_file ();
  264. if (symfile != 0)
  265. printf ("Symbols loaded from \"%s\".\n", symfile);
  266. if (! have_inferior_p ())
  267. {
  268. if (execfile)
  269. {
  270. printf ("Text segment from 0x%x to 0x%x.\n",
  271. text_start, text_end);
  272. }
  273. if (corefile)
  274. {
  275. printf ("Data segment from 0x%x to 0x%x.\nStack segment from 0x%x to 0x%x.\n",
  276. data_start, data_end, stack_start, stack_end);
  277. }
  278. else
  279. {
  280. printf ("Data segment in executable from 0x%x to 0x%x.\n",
  281. exec_data_start, exec_data_end);
  282. }
  283. }
  284. }
  285. /* Read "memory data" from core file and/or executable file */
  286. read_memory (memaddr, myaddr, len)
  287. CORE_ADDR memaddr;
  288. char *myaddr;
  289. int len;
  290. {
  291. if (have_inferior_p ())
  292. read_inferior_memory (memaddr, myaddr, len);
  293. else
  294. xfer_core_file (memaddr, myaddr, len, 0);
  295. }
  296. /* Write LEN bytes of data starting at address MYADDR
  297. into debugged program memory at address MEMADDR.
  298. Returns zero if successful, 1 if failed because inferior is shared. */
  299. int
  300. write_memory (memaddr, myaddr, len)
  301. CORE_ADDR memaddr;
  302. char *myaddr;
  303. int len;
  304. {
  305. if (have_inferior_p ())
  306. return write_inferior_memory (memaddr, myaddr, len);
  307. else
  308. error ("Can write memory only when program being debugged is running.");
  309. }
  310. xfer_core_file (memaddr, myaddr, len)
  311. CORE_ADDR memaddr;
  312. char *myaddr;
  313. int len;
  314. {
  315. register int i;
  316. register int val;
  317. int xferchan;
  318. char **xferfile;
  319. int fileptr;
  320. while (len > 0)
  321. {
  322. xferfile = 0;
  323. xferchan = 0;
  324. /* Determine which file the next bunch of addresses reside in,
  325. and where in the file. Set the file's read/write pointer
  326. to point at the proper place for the desired address
  327. and set xferfile and xferchan for the correct file.
  328. If desired address is nonexistent, leave them zero.
  329. i is set to the number of bytes that can be handled
  330. along with the next address. */
  331. if (memaddr < text_start)
  332. {
  333. i = min (len, text_start - memaddr);
  334. }
  335. else if (memaddr >= text_end && memaddr < data_start)
  336. {
  337. i = min (len, data_start - memaddr);
  338. }
  339. else if (memaddr >= (corechan >= 0 ? data_end : exec_data_end)
  340. && memaddr < stack_start)
  341. {
  342. i = min (len, stack_start - memaddr);
  343. }
  344. else if (memaddr >= stack_end && stack_end != 0)
  345. {
  346. i = min (len, - memaddr);
  347. }
  348. /* Note that if there is no core file
  349. data_start and data_end are equal. */
  350. else if (memaddr >= data_start && memaddr < data_end)
  351. {
  352. i = min (len, data_end - memaddr);
  353. fileptr = memaddr - data_start + data_offset;
  354. xferfile = &corefile;
  355. xferchan = corechan;
  356. }
  357. /* Note that if there is no core file
  358. stack_start and stack_end are equal. */
  359. else if (memaddr >= stack_start && memaddr < stack_end)
  360. {
  361. i = min (len, stack_end - memaddr);
  362. fileptr = memaddr - stack_start + stack_offset;
  363. xferfile = &corefile;
  364. xferchan = corechan;
  365. }
  366. else if (corechan < 0
  367. && memaddr >= exec_data_start && memaddr < exec_data_end)
  368. {
  369. i = min (len, exec_data_end - memaddr);
  370. fileptr = memaddr - exec_data_start + exec_data_offset;
  371. xferfile = &execfile;
  372. xferchan = execchan;
  373. }
  374. else if (memaddr >= text_start && memaddr < text_end)
  375. {
  376. i = min (len, text_end - memaddr);
  377. fileptr = memaddr - text_start + text_offset;
  378. xferfile = &execfile;
  379. xferchan = execchan;
  380. }
  381. /* Now we know which file to use.
  382. Set up its pointer and transfer the data. */
  383. if (xferfile)
  384. {
  385. if (*xferfile == 0)
  386. if (xferfile == &execfile)
  387. error ("No program file to examine.");
  388. else
  389. error ("No core dump file or running program to examine.");
  390. val = lseek (xferchan, fileptr, 0);
  391. if (val < 0)
  392. perror_with_name (*xferfile);
  393. val = myread (xferchan, myaddr, i);
  394. if (val < 0)
  395. perror_with_name (*xferfile);
  396. }
  397. /* If this address is for nonexistent memory,
  398. read zeros if reading, or do nothing if writing. */
  399. else
  400. bzero (myaddr, i);
  401. memaddr += i;
  402. myaddr += i;
  403. len -= i;
  404. }
  405. }
  406. /* My replacement for the read system call.
  407. Takes same args as read, plus filename to use in error reports.
  408. Used like read but keeps going if read returns too soon. */
  409. myread (desc, addr, len, filename)
  410. int desc;
  411. char *addr;
  412. int len;
  413. char *filename;
  414. {
  415. register int val;
  416. int orglen = len;
  417. while (len > 0)
  418. {
  419. val = read (desc, addr, len);
  420. if (val < 0)
  421. return val;
  422. if (val == 0)
  423. return orglen - len;
  424. len -= val;
  425. addr += val;
  426. }
  427. }
  428. #ifndef NEW_SUN_CORE
  429. /* Return the address in the core dump or inferior of register REGNO.
  430. BLOCKEND is the address of the end of the user structure. */
  431. unsigned int
  432. register_addr (regno, blockend)
  433. int regno;
  434. int blockend;
  435. {
  436. int addr;
  437. if (regno < 0 || regno >= NUM_REGS)
  438. error ("Invalid register number %d.", regno);
  439. REGISTER_U_ADDR (addr, blockend, regno);
  440. return addr;
  441. }
  442. #endif /* not NEW_SUN_CORE */
  443. static
  444. initialize ()
  445. {
  446. corechan = -1;
  447. execchan = -1;
  448. corefile = 0;
  449. execfile = 0;
  450. text_start = 0;
  451. text_end = 0;
  452. data_start = 0;
  453. data_end = 0;
  454. exec_data_start = 0;
  455. exec_data_end = 0;
  456. stack_start = STACK_END_ADDR;
  457. stack_end = STACK_END_ADDR;
  458. add_com ("core-file", class_files, core_file_command,
  459. "Use FILE as core dump for examining memory and registers.\n\
  460. No arg means have no core file.");
  461. add_com ("exec-file", class_files, exec_file_command,
  462. "Use FILE as program for getting contents of pure memory.\n\
  463. If FILE cannot be found as specified, your execution directory path\n\
  464. is searched for a command of that name.\n\
  465. No arg means have no executable file.");
  466. add_info ("files", files_info, "Names of files being debugged.");
  467. }
  468. END_FILE