remote-sim.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /* Generic remote debugging interface for simulators.
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support.
  4. Steve Chamberlain (sac@cygnus.com).
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "gdb_bfd.h"
  18. #include "inferior.h"
  19. #include "infrun.h"
  20. #include "value.h"
  21. #include <ctype.h>
  22. #include <fcntl.h>
  23. #include <signal.h>
  24. #include <setjmp.h>
  25. #include "terminal.h"
  26. #include "target.h"
  27. #include "process-stratum-target.h"
  28. #include "gdbcore.h"
  29. #include "sim/callback.h"
  30. #include "sim/sim.h"
  31. #include "command.h"
  32. #include "regcache.h"
  33. #include "sim-regno.h"
  34. #include "arch-utils.h"
  35. #include "readline/readline.h"
  36. #include "gdbthread.h"
  37. #include "gdbsupport/byte-vector.h"
  38. #include "memory-map.h"
  39. #include "remote.h"
  40. #include "gdbsupport/buildargv.h"
  41. /* Prototypes */
  42. static void init_callbacks (void);
  43. static void end_callbacks (void);
  44. static int gdb_os_write_stdout (host_callback *, const char *, int);
  45. static void gdb_os_flush_stdout (host_callback *);
  46. static int gdb_os_write_stderr (host_callback *, const char *, int);
  47. static void gdb_os_flush_stderr (host_callback *);
  48. static int gdb_os_poll_quit (host_callback *);
  49. /* gdb_printf is depreciated. */
  50. static void gdb_os_printf_filtered (host_callback *, const char *, ...);
  51. static void gdb_os_vprintf_filtered (host_callback *, const char *, va_list);
  52. static void gdb_os_evprintf_filtered (host_callback *, const char *, va_list);
  53. static void gdb_os_error (host_callback *, const char *, ...)
  54. ATTRIBUTE_NORETURN;
  55. /* Naming convention:
  56. sim_* are the interface to the simulator (see remote-sim.h).
  57. gdbsim_* are stuff which is internal to gdb. */
  58. /* Value of the next pid to allocate for an inferior. As indicated
  59. elsewhere, its initial value is somewhat arbitrary; it's critical
  60. though that it's not zero or negative. */
  61. static int next_pid;
  62. #define INITIAL_PID 42000
  63. /* Simulator-specific, per-inferior state. */
  64. struct sim_inferior_data {
  65. explicit sim_inferior_data (SIM_DESC desc)
  66. : gdbsim_desc (desc),
  67. remote_sim_ptid (next_pid, 0, next_pid)
  68. {
  69. gdb_assert (remote_sim_ptid != null_ptid);
  70. ++next_pid;
  71. }
  72. ~sim_inferior_data ();
  73. /* Flag which indicates whether or not the program has been loaded. */
  74. int program_loaded = 0;
  75. /* Simulator descriptor for this inferior. */
  76. SIM_DESC gdbsim_desc;
  77. /* This is the ptid we use for this particular simulator instance. Its
  78. value is somewhat arbitrary, as the simulator target don't have a
  79. notion of tasks or threads, but we need something non-null to place
  80. in inferior_ptid. For simulators which permit multiple instances,
  81. we also need a unique identifier to use for each inferior. */
  82. ptid_t remote_sim_ptid;
  83. /* Signal with which to resume. */
  84. enum gdb_signal resume_siggnal = GDB_SIGNAL_0;
  85. /* Flag which indicates whether resume should step or not. */
  86. int resume_step = 0;
  87. };
  88. static const target_info gdbsim_target_info = {
  89. "sim",
  90. N_("simulator"),
  91. N_("Use the compiled-in simulator.")
  92. };
  93. struct gdbsim_target final
  94. : public memory_breakpoint_target<process_stratum_target>
  95. {
  96. gdbsim_target () = default;
  97. const target_info &info () const override
  98. { return gdbsim_target_info; }
  99. void close () override;
  100. void detach (inferior *inf, int) override;
  101. void resume (ptid_t, int, enum gdb_signal) override;
  102. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  103. void fetch_registers (struct regcache *, int) override;
  104. void store_registers (struct regcache *, int) override;
  105. void prepare_to_store (struct regcache *) override;
  106. enum target_xfer_status xfer_partial (enum target_object object,
  107. const char *annex,
  108. gdb_byte *readbuf,
  109. const gdb_byte *writebuf,
  110. ULONGEST offset, ULONGEST len,
  111. ULONGEST *xfered_len) override;
  112. void files_info () override;
  113. void kill () override;
  114. void load (const char *, int) override;
  115. bool can_create_inferior () override { return true; }
  116. void create_inferior (const char *, const std::string &,
  117. char **, int) override;
  118. void mourn_inferior () override;
  119. void interrupt () override;
  120. bool thread_alive (ptid_t ptid) override;
  121. std::string pid_to_str (ptid_t) override;
  122. bool has_all_memory () override;
  123. bool has_memory () override;
  124. std::vector<mem_region> memory_map () override;
  125. private:
  126. sim_inferior_data *get_inferior_data_by_ptid (ptid_t ptid,
  127. int sim_instance_needed);
  128. void resume_one_inferior (inferior *inf, bool step, gdb_signal siggnal);
  129. void close_one_inferior (inferior *inf);
  130. };
  131. static struct gdbsim_target gdbsim_ops;
  132. static inferior_key<sim_inferior_data> sim_inferior_data_key;
  133. /* Flag indicating the "open" status of this module. It's set to 1
  134. in gdbsim_open() and 0 in gdbsim_close(). */
  135. static int gdbsim_is_open = 0;
  136. /* Argument list to pass to sim_open(). It is allocated in gdbsim_open()
  137. and deallocated in gdbsim_close(). The lifetime needs to extend beyond
  138. the call to gdbsim_open() due to the fact that other sim instances other
  139. than the first will be allocated after the gdbsim_open() call. */
  140. static char **sim_argv = NULL;
  141. /* OS-level callback functions for write, flush, etc. */
  142. static host_callback gdb_callback;
  143. static int callbacks_initialized = 0;
  144. /* Flags indicating whether or not a sim instance is needed. One of these
  145. flags should be passed to get_sim_inferior_data(). */
  146. enum {SIM_INSTANCE_NOT_NEEDED = 0, SIM_INSTANCE_NEEDED = 1};
  147. /* Obtain pointer to per-inferior simulator data, allocating it if necessary.
  148. Attempt to open the sim if SIM_INSTANCE_NEEDED is true. */
  149. static struct sim_inferior_data *
  150. get_sim_inferior_data (struct inferior *inf, int sim_instance_needed)
  151. {
  152. SIM_DESC sim_desc = NULL;
  153. struct sim_inferior_data *sim_data = sim_inferior_data_key.get (inf);
  154. /* Try to allocate a new sim instance, if needed. We do this ahead of
  155. a potential allocation of a sim_inferior_data struct in order to
  156. avoid needlessly allocating that struct in the event that the sim
  157. instance allocation fails. */
  158. if (sim_instance_needed == SIM_INSTANCE_NEEDED
  159. && (sim_data == NULL || sim_data->gdbsim_desc == NULL))
  160. {
  161. sim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback,
  162. current_program_space->exec_bfd (), sim_argv);
  163. if (sim_desc == NULL)
  164. error (_("Unable to create simulator instance for inferior %d."),
  165. inf->num);
  166. /* Check if the sim descriptor is the same as that of another
  167. inferior. */
  168. for (inferior *other_inf : all_inferiors ())
  169. {
  170. sim_inferior_data *other_sim_data
  171. = sim_inferior_data_key.get (other_inf);
  172. if (other_sim_data != NULL
  173. && other_sim_data->gdbsim_desc == sim_desc)
  174. {
  175. /* We don't close the descriptor due to the fact that it's
  176. shared with some other inferior. If we were to close it,
  177. that might needlessly muck up the other inferior. Of
  178. course, it's possible that the damage has already been
  179. done... Note that it *will* ultimately be closed during
  180. cleanup of the other inferior. */
  181. sim_desc = NULL;
  182. error (
  183. _("Inferior %d and inferior %d would have identical simulator state.\n"
  184. "(This simulator does not support the running of more than one inferior.)"),
  185. inf->num, other_inf->num);
  186. }
  187. }
  188. }
  189. if (sim_data == NULL)
  190. {
  191. sim_data = sim_inferior_data_key.emplace (inf, sim_desc);
  192. }
  193. else if (sim_desc)
  194. {
  195. /* This handles the case where sim_data was allocated prior to
  196. needing a sim instance. */
  197. sim_data->gdbsim_desc = sim_desc;
  198. }
  199. return sim_data;
  200. }
  201. /* Return pointer to per-inferior simulator data using PTID to find the
  202. inferior in question. Return NULL when no inferior is found or
  203. when ptid has a zero or negative pid component. */
  204. sim_inferior_data *
  205. gdbsim_target::get_inferior_data_by_ptid (ptid_t ptid,
  206. int sim_instance_needed)
  207. {
  208. struct inferior *inf;
  209. int pid = ptid.pid ();
  210. if (pid <= 0)
  211. return NULL;
  212. inf = find_inferior_pid (this, pid);
  213. if (inf)
  214. return get_sim_inferior_data (inf, sim_instance_needed);
  215. else
  216. return NULL;
  217. }
  218. /* Free the per-inferior simulator data. */
  219. sim_inferior_data::~sim_inferior_data ()
  220. {
  221. if (gdbsim_desc)
  222. sim_close (gdbsim_desc, 0);
  223. }
  224. static void
  225. dump_mem (const gdb_byte *buf, int len)
  226. {
  227. gdb_puts ("\t", gdb_stdlog);
  228. if (len == 8 || len == 4)
  229. {
  230. uint32_t l[2];
  231. memcpy (l, buf, len);
  232. gdb_printf (gdb_stdlog, "0x%08x", l[0]);
  233. if (len == 8)
  234. gdb_printf (gdb_stdlog, " 0x%08x", l[1]);
  235. }
  236. else
  237. {
  238. int i;
  239. for (i = 0; i < len; i++)
  240. gdb_printf (gdb_stdlog, "0x%02x ", buf[i]);
  241. }
  242. gdb_puts ("\n", gdb_stdlog);
  243. }
  244. /* Initialize gdb_callback. */
  245. static void
  246. init_callbacks (void)
  247. {
  248. if (!callbacks_initialized)
  249. {
  250. gdb_callback = default_callback;
  251. gdb_callback.init (&gdb_callback);
  252. gdb_callback.write_stdout = gdb_os_write_stdout;
  253. gdb_callback.flush_stdout = gdb_os_flush_stdout;
  254. gdb_callback.write_stderr = gdb_os_write_stderr;
  255. gdb_callback.flush_stderr = gdb_os_flush_stderr;
  256. gdb_callback.printf_filtered = gdb_os_printf_filtered;
  257. gdb_callback.vprintf_filtered = gdb_os_vprintf_filtered;
  258. gdb_callback.evprintf_filtered = gdb_os_evprintf_filtered;
  259. gdb_callback.error = gdb_os_error;
  260. gdb_callback.poll_quit = gdb_os_poll_quit;
  261. gdb_callback.magic = HOST_CALLBACK_MAGIC;
  262. callbacks_initialized = 1;
  263. }
  264. }
  265. /* Release callbacks (free resources used by them). */
  266. static void
  267. end_callbacks (void)
  268. {
  269. if (callbacks_initialized)
  270. {
  271. gdb_callback.shutdown (&gdb_callback);
  272. callbacks_initialized = 0;
  273. }
  274. }
  275. /* GDB version of os_write_stdout callback. */
  276. static int
  277. gdb_os_write_stdout (host_callback *p, const char *buf, int len)
  278. {
  279. gdb_stdtarg->write (buf, len);
  280. return len;
  281. }
  282. /* GDB version of os_flush_stdout callback. */
  283. static void
  284. gdb_os_flush_stdout (host_callback *p)
  285. {
  286. gdb_stdtarg->flush ();
  287. }
  288. /* GDB version of os_write_stderr callback. */
  289. static int
  290. gdb_os_write_stderr (host_callback *p, const char *buf, int len)
  291. {
  292. int i;
  293. char b[2];
  294. for (i = 0; i < len; i++)
  295. {
  296. b[0] = buf[i];
  297. b[1] = 0;
  298. gdb_stdtargerr->puts (b);
  299. }
  300. return len;
  301. }
  302. /* GDB version of os_flush_stderr callback. */
  303. static void
  304. gdb_os_flush_stderr (host_callback *p)
  305. {
  306. gdb_stdtargerr->flush ();
  307. }
  308. /* GDB version of gdb_printf callback. */
  309. static void ATTRIBUTE_PRINTF (2, 3)
  310. gdb_os_printf_filtered (host_callback * p, const char *format, ...)
  311. {
  312. va_list args;
  313. va_start (args, format);
  314. gdb_vprintf (gdb_stdout, format, args);
  315. va_end (args);
  316. }
  317. /* GDB version of error gdb_vprintf. */
  318. static void ATTRIBUTE_PRINTF (2, 0)
  319. gdb_os_vprintf_filtered (host_callback * p, const char *format, va_list ap)
  320. {
  321. gdb_vprintf (gdb_stdout, format, ap);
  322. }
  323. /* GDB version of error evprintf_filtered. */
  324. static void ATTRIBUTE_PRINTF (2, 0)
  325. gdb_os_evprintf_filtered (host_callback * p, const char *format, va_list ap)
  326. {
  327. gdb_vprintf (gdb_stderr, format, ap);
  328. }
  329. /* GDB version of error callback. */
  330. static void ATTRIBUTE_PRINTF (2, 3)
  331. gdb_os_error (host_callback * p, const char *format, ...)
  332. {
  333. va_list args;
  334. va_start (args, format);
  335. verror (format, args);
  336. va_end (args);
  337. }
  338. int
  339. one2one_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  340. {
  341. /* Only makes sense to supply raw registers. */
  342. gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
  343. return regnum;
  344. }
  345. void
  346. gdbsim_target::fetch_registers (struct regcache *regcache, int regno)
  347. {
  348. struct gdbarch *gdbarch = regcache->arch ();
  349. struct inferior *inf = find_inferior_ptid (this, regcache->ptid ());
  350. struct sim_inferior_data *sim_data
  351. = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
  352. if (regno == -1)
  353. {
  354. for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
  355. fetch_registers (regcache, regno);
  356. return;
  357. }
  358. switch (gdbarch_register_sim_regno (gdbarch, regno))
  359. {
  360. case LEGACY_SIM_REGNO_IGNORE:
  361. break;
  362. case SIM_REGNO_DOES_NOT_EXIST:
  363. {
  364. /* For moment treat a `does not exist' register the same way
  365. as an ``unavailable'' register. */
  366. regcache->raw_supply_zeroed (regno);
  367. break;
  368. }
  369. default:
  370. {
  371. static int warn_user = 1;
  372. int regsize = register_size (gdbarch, regno);
  373. gdb::byte_vector buf (regsize, 0);
  374. int nr_bytes;
  375. gdb_assert (regno >= 0 && regno < gdbarch_num_regs (gdbarch));
  376. nr_bytes = sim_fetch_register (sim_data->gdbsim_desc,
  377. gdbarch_register_sim_regno
  378. (gdbarch, regno),
  379. buf.data (), regsize);
  380. if (nr_bytes > 0 && nr_bytes != regsize && warn_user)
  381. {
  382. gdb_printf (gdb_stderr,
  383. "Size of register %s (%d/%d) "
  384. "incorrect (%d instead of %d))",
  385. gdbarch_register_name (gdbarch, regno),
  386. regno,
  387. gdbarch_register_sim_regno (gdbarch, regno),
  388. nr_bytes, regsize);
  389. warn_user = 0;
  390. }
  391. /* FIXME: cagney/2002-05-27: Should check `nr_bytes == 0'
  392. indicating that GDB and the SIM have different ideas about
  393. which registers are fetchable. */
  394. /* Else if (nr_bytes < 0): an old simulator, that doesn't
  395. think to return the register size. Just assume all is ok. */
  396. regcache->raw_supply (regno, buf.data ());
  397. if (remote_debug)
  398. {
  399. gdb_printf (gdb_stdlog,
  400. "gdbsim_fetch_register: %d", regno);
  401. /* FIXME: We could print something more intelligible. */
  402. dump_mem (buf.data (), regsize);
  403. }
  404. break;
  405. }
  406. }
  407. }
  408. void
  409. gdbsim_target::store_registers (struct regcache *regcache, int regno)
  410. {
  411. struct gdbarch *gdbarch = regcache->arch ();
  412. struct inferior *inf = find_inferior_ptid (this, regcache->ptid ());
  413. struct sim_inferior_data *sim_data
  414. = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
  415. if (regno == -1)
  416. {
  417. for (regno = 0; regno < gdbarch_num_regs (gdbarch); regno++)
  418. store_registers (regcache, regno);
  419. return;
  420. }
  421. else if (gdbarch_register_sim_regno (gdbarch, regno) >= 0)
  422. {
  423. int regsize = register_size (gdbarch, regno);
  424. gdb::byte_vector tmp (regsize);
  425. int nr_bytes;
  426. regcache->cooked_read (regno, tmp.data ());
  427. nr_bytes = sim_store_register (sim_data->gdbsim_desc,
  428. gdbarch_register_sim_regno
  429. (gdbarch, regno),
  430. tmp.data (), regsize);
  431. if (nr_bytes > 0 && nr_bytes != regsize)
  432. internal_error (__FILE__, __LINE__,
  433. _("Register size different to expected"));
  434. if (nr_bytes < 0)
  435. internal_error (__FILE__, __LINE__,
  436. _("Register %d not updated"), regno);
  437. if (nr_bytes == 0)
  438. warning (_("Register %s not updated"),
  439. gdbarch_register_name (gdbarch, regno));
  440. if (remote_debug)
  441. {
  442. gdb_printf (gdb_stdlog, "gdbsim_store_register: %d", regno);
  443. /* FIXME: We could print something more intelligible. */
  444. dump_mem (tmp.data (), regsize);
  445. }
  446. }
  447. }
  448. /* Kill the running program. This may involve closing any open files
  449. and releasing other resources acquired by the simulated program. */
  450. void
  451. gdbsim_target::kill ()
  452. {
  453. if (remote_debug)
  454. gdb_printf (gdb_stdlog, "gdbsim_kill\n");
  455. /* There is no need to `kill' running simulator - the simulator is
  456. not running. Mourning it is enough. */
  457. target_mourn_inferior (inferior_ptid);
  458. }
  459. /* Load an executable file into the target process. This is expected to
  460. not only bring new code into the target process, but also to update
  461. GDB's symbol tables to match. */
  462. void
  463. gdbsim_target::load (const char *args, int fromtty)
  464. {
  465. const char *prog;
  466. struct sim_inferior_data *sim_data
  467. = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
  468. if (args == NULL)
  469. error_no_arg (_("program to load"));
  470. gdb_argv argv (args);
  471. prog = tilde_expand (argv[0]);
  472. if (argv[1] != NULL)
  473. error (_("GDB sim does not yet support a load offset."));
  474. if (remote_debug)
  475. gdb_printf (gdb_stdlog, "gdbsim_load: prog \"%s\"\n", prog);
  476. /* FIXME: We will print two messages on error.
  477. Need error to either not print anything if passed NULL or need
  478. another routine that doesn't take any arguments. */
  479. if (sim_load (sim_data->gdbsim_desc, prog, NULL, fromtty) == SIM_RC_FAIL)
  480. error (_("unable to load program"));
  481. /* FIXME: If a load command should reset the targets registers then
  482. a call to sim_create_inferior() should go here. */
  483. sim_data->program_loaded = 1;
  484. }
  485. /* Start an inferior process and set inferior_ptid to its pid.
  486. EXEC_FILE is the file to run.
  487. ARGS is a string containing the arguments to the program.
  488. ENV is the environment vector to pass. Errors reported with error().
  489. On VxWorks and various standalone systems, we ignore exec_file. */
  490. /* This is called not only when we first attach, but also when the
  491. user types "run" after having attached. */
  492. void
  493. gdbsim_target::create_inferior (const char *exec_file,
  494. const std::string &allargs,
  495. char **env, int from_tty)
  496. {
  497. struct sim_inferior_data *sim_data
  498. = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
  499. int len;
  500. char *arg_buf;
  501. const char *args = allargs.c_str ();
  502. if (exec_file == 0 || current_program_space->exec_bfd () == 0)
  503. warning (_("No executable file specified."));
  504. if (!sim_data->program_loaded)
  505. warning (_("No program loaded."));
  506. if (remote_debug)
  507. gdb_printf (gdb_stdlog,
  508. "gdbsim_create_inferior: exec_file \"%s\", args \"%s\"\n",
  509. (exec_file ? exec_file : "(NULL)"),
  510. args);
  511. if (inferior_ptid == sim_data->remote_sim_ptid)
  512. kill ();
  513. remove_breakpoints ();
  514. init_wait_for_inferior ();
  515. gdb_argv built_argv;
  516. if (exec_file != NULL)
  517. {
  518. len = strlen (exec_file) + 1 + allargs.size () + 1 + /*slop */ 10;
  519. arg_buf = (char *) alloca (len);
  520. arg_buf[0] = '\0';
  521. strcat (arg_buf, exec_file);
  522. strcat (arg_buf, " ");
  523. strcat (arg_buf, args);
  524. built_argv.reset (arg_buf);
  525. }
  526. if (sim_create_inferior (sim_data->gdbsim_desc,
  527. current_program_space->exec_bfd (),
  528. built_argv.get (), env)
  529. != SIM_RC_OK)
  530. error (_("Unable to create sim inferior."));
  531. inferior_appeared (current_inferior (),
  532. sim_data->remote_sim_ptid.pid ());
  533. thread_info *thr = add_thread_silent (this, sim_data->remote_sim_ptid);
  534. switch_to_thread (thr);
  535. insert_breakpoints (); /* Needed to get correct instruction
  536. in cache. */
  537. clear_proceed_status (0);
  538. }
  539. /* The open routine takes the rest of the parameters from the command,
  540. and (if successful) pushes a new target onto the stack.
  541. Targets should supply this routine, if only to provide an error message. */
  542. /* Called when selecting the simulator. E.g. (gdb) target sim name. */
  543. static void
  544. gdbsim_target_open (const char *args, int from_tty)
  545. {
  546. int len;
  547. char *arg_buf;
  548. struct sim_inferior_data *sim_data;
  549. SIM_DESC gdbsim_desc;
  550. const char *sysroot = gdb_sysroot.c_str ();
  551. if (is_target_filename (sysroot))
  552. sysroot += strlen (TARGET_SYSROOT_PREFIX);
  553. if (remote_debug)
  554. gdb_printf (gdb_stdlog,
  555. "gdbsim_open: args \"%s\"\n", args ? args : "(null)");
  556. /* Ensure that the sim target is not on the target stack. This is
  557. necessary, because if it is on the target stack, the call to
  558. push_target below will invoke sim_close(), thus freeing various
  559. state (including a sim instance) that we allocate prior to
  560. invoking push_target(). We want to delay the push_target()
  561. operation until after we complete those operations which could
  562. error out. */
  563. if (gdbsim_is_open)
  564. current_inferior ()->unpush_target (&gdbsim_ops);
  565. len = (7 + 1 /* gdbsim */
  566. + strlen (" -E little")
  567. + strlen (" --architecture=xxxxxxxxxx")
  568. + strlen (" --sysroot=") + strlen (sysroot) +
  569. + (args ? strlen (args) : 0)
  570. + 50) /* slack */ ;
  571. arg_buf = (char *) alloca (len);
  572. strcpy (arg_buf, "gdbsim"); /* 7 */
  573. /* Specify the byte order for the target when it is explicitly
  574. specified by the user (not auto detected). */
  575. switch (selected_byte_order ())
  576. {
  577. case BFD_ENDIAN_BIG:
  578. strcat (arg_buf, " -E big");
  579. break;
  580. case BFD_ENDIAN_LITTLE:
  581. strcat (arg_buf, " -E little");
  582. break;
  583. case BFD_ENDIAN_UNKNOWN:
  584. break;
  585. }
  586. /* Specify the architecture of the target when it has been
  587. explicitly specified */
  588. if (selected_architecture_name () != NULL)
  589. {
  590. strcat (arg_buf, " --architecture=");
  591. strcat (arg_buf, selected_architecture_name ());
  592. }
  593. /* Pass along gdb's concept of the sysroot. */
  594. strcat (arg_buf, " --sysroot=");
  595. strcat (arg_buf, sysroot);
  596. /* finally, any explicit args */
  597. if (args)
  598. {
  599. strcat (arg_buf, " "); /* 1 */
  600. strcat (arg_buf, args);
  601. }
  602. gdb_argv argv (arg_buf);
  603. sim_argv = argv.release ();
  604. init_callbacks ();
  605. gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback,
  606. current_program_space->exec_bfd (), sim_argv);
  607. if (gdbsim_desc == 0)
  608. {
  609. freeargv (sim_argv);
  610. sim_argv = NULL;
  611. error (_("unable to create simulator instance"));
  612. }
  613. /* Reset the pid numberings for this batch of sim instances. */
  614. next_pid = INITIAL_PID;
  615. /* Allocate the inferior data, but do not allocate a sim instance
  616. since we've already just done that. */
  617. sim_data = get_sim_inferior_data (current_inferior (),
  618. SIM_INSTANCE_NOT_NEEDED);
  619. sim_data->gdbsim_desc = gdbsim_desc;
  620. current_inferior ()->push_target (&gdbsim_ops);
  621. gdb_printf ("Connected to the simulator.\n");
  622. /* There's nothing running after "target sim" or "load"; not until
  623. "run". */
  624. switch_to_no_thread ();
  625. gdbsim_is_open = 1;
  626. }
  627. /* Helper for gdbsim_target::close. */
  628. void
  629. gdbsim_target::close_one_inferior (inferior *inf)
  630. {
  631. struct sim_inferior_data *sim_data = sim_inferior_data_key.get (inf);
  632. if (sim_data != NULL)
  633. {
  634. ptid_t ptid = sim_data->remote_sim_ptid;
  635. sim_inferior_data_key.clear (inf);
  636. /* Having a ptid allocated and stored in remote_sim_ptid does
  637. not mean that a corresponding inferior was ever created.
  638. Thus we need to verify the existence of an inferior using the
  639. pid in question before setting inferior_ptid via
  640. switch_to_thread() or mourning the inferior. */
  641. if (find_inferior_ptid (this, ptid) != NULL)
  642. {
  643. switch_to_thread (this, ptid);
  644. generic_mourn_inferior ();
  645. }
  646. }
  647. }
  648. /* Close out all files and local state before this target loses control. */
  649. void
  650. gdbsim_target::close ()
  651. {
  652. if (remote_debug)
  653. gdb_printf (gdb_stdlog, "gdbsim_close\n");
  654. for (inferior *inf : all_inferiors (this))
  655. close_one_inferior (inf);
  656. if (sim_argv != NULL)
  657. {
  658. freeargv (sim_argv);
  659. sim_argv = NULL;
  660. }
  661. end_callbacks ();
  662. gdbsim_is_open = 0;
  663. }
  664. /* Takes a program previously attached to and detaches it.
  665. The program may resume execution (some targets do, some don't) and will
  666. no longer stop on signals, etc. We better not have left any breakpoints
  667. in the program or it'll die when it hits one. FROM_TTY says whether to be
  668. verbose or not. */
  669. /* Terminate the open connection to the remote debugger.
  670. Use this when you want to detach and do something else with your gdb. */
  671. void
  672. gdbsim_target::detach (inferior *inf, int from_tty)
  673. {
  674. if (remote_debug)
  675. gdb_printf (gdb_stdlog, "gdbsim_detach\n");
  676. inf->unpush_target (this); /* calls gdbsim_close to do the real work */
  677. if (from_tty)
  678. gdb_printf ("Ending simulator %s debugging\n", target_shortname ());
  679. }
  680. /* Resume execution of the target process. STEP says whether to single-step
  681. or to run free; SIGGNAL is the signal value (e.g. SIGINT) to be given
  682. to the target, or zero for no signal. */
  683. void
  684. gdbsim_target::resume_one_inferior (inferior *inf, bool step,
  685. gdb_signal siggnal)
  686. {
  687. struct sim_inferior_data *sim_data
  688. = get_sim_inferior_data (inf, SIM_INSTANCE_NOT_NEEDED);
  689. if (sim_data)
  690. {
  691. sim_data->resume_siggnal = siggnal;
  692. sim_data->resume_step = step;
  693. if (remote_debug)
  694. gdb_printf (gdb_stdlog,
  695. _("gdbsim_resume: pid %d, step %d, signal %d\n"),
  696. inf->pid, step, siggnal);
  697. }
  698. }
  699. void
  700. gdbsim_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
  701. {
  702. struct sim_inferior_data *sim_data
  703. = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
  704. /* We don't access any sim_data members within this function.
  705. What's of interest is whether or not the call to
  706. get_sim_inferior_data_by_ptid(), above, is able to obtain a
  707. non-NULL pointer. If it managed to obtain a non-NULL pointer, we
  708. know we have a single inferior to consider. If it's NULL, we
  709. either have multiple inferiors to resume or an error condition. */
  710. if (sim_data)
  711. resume_one_inferior (find_inferior_ptid (this, ptid), step, siggnal);
  712. else if (ptid == minus_one_ptid)
  713. {
  714. for (inferior *inf : all_inferiors (this))
  715. resume_one_inferior (inf, step, siggnal);
  716. }
  717. else
  718. error (_("The program is not being run."));
  719. }
  720. /* Notify the simulator of an asynchronous request to interrupt.
  721. The simulator shall ensure that the interrupt request is eventually
  722. delivered to the simulator. If the call is made while the
  723. simulator is not running then the interrupt request is processed when
  724. the simulator is next resumed.
  725. For simulators that do not support this operation, just abort. */
  726. void
  727. gdbsim_target::interrupt ()
  728. {
  729. for (inferior *inf : all_inferiors ())
  730. {
  731. sim_inferior_data *sim_data
  732. = get_sim_inferior_data (inf, SIM_INSTANCE_NEEDED);
  733. if (sim_data != nullptr && !sim_stop (sim_data->gdbsim_desc))
  734. quit ();
  735. }
  736. }
  737. /* GDB version of os_poll_quit callback.
  738. Taken from gdb/util.c - should be in a library. */
  739. static int
  740. gdb_os_poll_quit (host_callback *p)
  741. {
  742. if (deprecated_ui_loop_hook != NULL)
  743. deprecated_ui_loop_hook (0);
  744. if (check_quit_flag ()) /* gdb's idea of quit */
  745. return 1;
  746. return 0;
  747. }
  748. /* Wait for inferior process to do something. Return pid of child,
  749. or -1 in case of error; store status through argument pointer STATUS,
  750. just as `wait' would. */
  751. static void
  752. gdbsim_cntrl_c (int signo)
  753. {
  754. gdbsim_ops.interrupt ();
  755. }
  756. ptid_t
  757. gdbsim_target::wait (ptid_t ptid, struct target_waitstatus *status,
  758. target_wait_flags options)
  759. {
  760. struct sim_inferior_data *sim_data;
  761. static sighandler_t prev_sigint;
  762. int sigrc = 0;
  763. enum sim_stop reason = sim_running;
  764. /* This target isn't able to (yet) resume more than one inferior at a time.
  765. When ptid is minus_one_ptid, just use the current inferior. If we're
  766. given an explicit pid, we'll try to find it and use that instead. */
  767. if (ptid == minus_one_ptid)
  768. sim_data = get_sim_inferior_data (current_inferior (),
  769. SIM_INSTANCE_NEEDED);
  770. else
  771. {
  772. sim_data = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NEEDED);
  773. if (sim_data == NULL)
  774. error (_("Unable to wait for pid %d. Inferior not found."),
  775. ptid.pid ());
  776. }
  777. if (remote_debug)
  778. gdb_printf (gdb_stdlog, "gdbsim_wait\n");
  779. #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
  780. {
  781. struct sigaction sa, osa;
  782. sa.sa_handler = gdbsim_cntrl_c;
  783. sigemptyset (&sa.sa_mask);
  784. sa.sa_flags = 0;
  785. sigaction (SIGINT, &sa, &osa);
  786. prev_sigint = osa.sa_handler;
  787. }
  788. #else
  789. prev_sigint = signal (SIGINT, gdbsim_cntrl_c);
  790. #endif
  791. sim_resume (sim_data->gdbsim_desc, sim_data->resume_step,
  792. sim_data->resume_siggnal);
  793. signal (SIGINT, prev_sigint);
  794. sim_data->resume_step = 0;
  795. sim_stop_reason (sim_data->gdbsim_desc, &reason, &sigrc);
  796. switch (reason)
  797. {
  798. case sim_exited:
  799. status->set_exited (sigrc);
  800. break;
  801. case sim_stopped:
  802. switch (sigrc)
  803. {
  804. case GDB_SIGNAL_ABRT:
  805. quit ();
  806. break;
  807. case GDB_SIGNAL_INT:
  808. case GDB_SIGNAL_TRAP:
  809. default:
  810. status->set_stopped ((gdb_signal) sigrc);
  811. break;
  812. }
  813. break;
  814. case sim_signalled:
  815. status->set_signalled ((gdb_signal) sigrc);
  816. break;
  817. case sim_running:
  818. case sim_polling:
  819. /* FIXME: Is this correct? */
  820. break;
  821. }
  822. return sim_data->remote_sim_ptid;
  823. }
  824. /* Get ready to modify the registers array. On machines which store
  825. individual registers, this doesn't need to do anything. On machines
  826. which store all the registers in one fell swoop, this makes sure
  827. that registers contains all the registers from the program being
  828. debugged. */
  829. void
  830. gdbsim_target::prepare_to_store (struct regcache *regcache)
  831. {
  832. /* Do nothing, since we can store individual regs. */
  833. }
  834. /* Helper for gdbsim_xfer_partial that handles memory transfers.
  835. Arguments are like target_xfer_partial. */
  836. static enum target_xfer_status
  837. gdbsim_xfer_memory (struct target_ops *target,
  838. gdb_byte *readbuf, const gdb_byte *writebuf,
  839. ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
  840. {
  841. struct sim_inferior_data *sim_data
  842. = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
  843. int l;
  844. /* If this target doesn't have memory yet, return 0 causing the
  845. request to be passed to a lower target, hopefully an exec
  846. file. */
  847. if (!target->has_memory ())
  848. return TARGET_XFER_EOF;
  849. if (!sim_data->program_loaded)
  850. error (_("No program loaded."));
  851. /* Note that we obtained the sim_data pointer above using
  852. SIM_INSTANCE_NOT_NEEDED. We do this so that we don't needlessly
  853. allocate a sim instance prior to loading a program. If we
  854. get to this point in the code though, gdbsim_desc should be
  855. non-NULL. (Note that a sim instance is needed in order to load
  856. the program...) */
  857. gdb_assert (sim_data->gdbsim_desc != NULL);
  858. if (remote_debug)
  859. gdb_printf (gdb_stdlog,
  860. "gdbsim_xfer_memory: readbuf %s, writebuf %s, "
  861. "memaddr %s, len %s\n",
  862. host_address_to_string (readbuf),
  863. host_address_to_string (writebuf),
  864. paddress (target_gdbarch (), memaddr),
  865. pulongest (len));
  866. if (writebuf)
  867. {
  868. if (remote_debug && len > 0)
  869. dump_mem (writebuf, len);
  870. l = sim_write (sim_data->gdbsim_desc, memaddr, writebuf, len);
  871. }
  872. else
  873. {
  874. l = sim_read (sim_data->gdbsim_desc, memaddr, readbuf, len);
  875. if (remote_debug && len > 0)
  876. dump_mem (readbuf, len);
  877. }
  878. if (l > 0)
  879. {
  880. *xfered_len = (ULONGEST) l;
  881. return TARGET_XFER_OK;
  882. }
  883. else if (l == 0)
  884. return TARGET_XFER_EOF;
  885. else
  886. return TARGET_XFER_E_IO;
  887. }
  888. /* Target to_xfer_partial implementation. */
  889. enum target_xfer_status
  890. gdbsim_target::xfer_partial (enum target_object object,
  891. const char *annex, gdb_byte *readbuf,
  892. const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  893. ULONGEST *xfered_len)
  894. {
  895. switch (object)
  896. {
  897. case TARGET_OBJECT_MEMORY:
  898. return gdbsim_xfer_memory (this, readbuf, writebuf, offset, len,
  899. xfered_len);
  900. default:
  901. return TARGET_XFER_E_IO;
  902. }
  903. }
  904. void
  905. gdbsim_target::files_info ()
  906. {
  907. struct sim_inferior_data *sim_data
  908. = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
  909. const char *file = "nothing";
  910. if (current_program_space->exec_bfd ())
  911. file = bfd_get_filename (current_program_space->exec_bfd ());
  912. if (remote_debug)
  913. gdb_printf (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file);
  914. if (current_program_space->exec_bfd ())
  915. {
  916. gdb_printf ("\tAttached to %s running program %s\n",
  917. target_shortname (), file);
  918. sim_info (sim_data->gdbsim_desc, 0);
  919. }
  920. }
  921. /* Clear the simulator's notion of what the break points are. */
  922. void
  923. gdbsim_target::mourn_inferior ()
  924. {
  925. if (remote_debug)
  926. gdb_printf (gdb_stdlog, "gdbsim_mourn_inferior:\n");
  927. remove_breakpoints ();
  928. generic_mourn_inferior ();
  929. }
  930. /* Pass the command argument through to the simulator verbatim. The
  931. simulator must do any command interpretation work. */
  932. static void
  933. simulator_command (const char *args, int from_tty)
  934. {
  935. struct sim_inferior_data *sim_data;
  936. /* We use inferior_data() instead of get_sim_inferior_data() here in
  937. order to avoid attaching a sim_inferior_data struct to an
  938. inferior unnecessarily. The reason we take such care here is due
  939. to the fact that this function, simulator_command(), may be called
  940. even when the sim target is not active. If we were to use
  941. get_sim_inferior_data() here, it is possible that this call would
  942. be made either prior to gdbsim_open() or after gdbsim_close(),
  943. thus allocating memory that would not be garbage collected until
  944. the ultimate destruction of the associated inferior. */
  945. sim_data = sim_inferior_data_key.get (current_inferior ());
  946. if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
  947. {
  948. /* PREVIOUSLY: The user may give a command before the simulator
  949. is opened. [...] (??? assuming of course one wishes to
  950. continue to allow commands to be sent to unopened simulators,
  951. which isn't entirely unreasonable). */
  952. /* The simulator is a builtin abstraction of a remote target.
  953. Consistent with that model, access to the simulator, via sim
  954. commands, is restricted to the period when the channel to the
  955. simulator is open. */
  956. error (_("Not connected to the simulator target"));
  957. }
  958. sim_do_command (sim_data->gdbsim_desc, args);
  959. /* Invalidate the register cache, in case the simulator command does
  960. something funny. */
  961. registers_changed ();
  962. }
  963. static void
  964. sim_command_completer (struct cmd_list_element *ignore,
  965. completion_tracker &tracker,
  966. const char *text, const char *word)
  967. {
  968. struct sim_inferior_data *sim_data;
  969. sim_data = sim_inferior_data_key.get (current_inferior ());
  970. if (sim_data == NULL || sim_data->gdbsim_desc == NULL)
  971. return;
  972. /* sim_complete_command returns a NULL-terminated malloc'ed array of
  973. malloc'ed strings. */
  974. struct sim_completions_deleter
  975. {
  976. void operator() (char **ptr) const
  977. {
  978. for (size_t i = 0; ptr[i] != NULL; i++)
  979. xfree (ptr[i]);
  980. xfree (ptr);
  981. }
  982. };
  983. std::unique_ptr<char *[], sim_completions_deleter> sim_completions
  984. (sim_complete_command (sim_data->gdbsim_desc, text, word));
  985. if (sim_completions == NULL)
  986. return;
  987. /* Count the elements and add completions from tail to head because
  988. below we'll swap elements out of the array in case add_completion
  989. throws and the deleter deletes until it finds a NULL element. */
  990. size_t count = 0;
  991. while (sim_completions[count] != NULL)
  992. count++;
  993. for (size_t i = count; i > 0; i--)
  994. {
  995. gdb::unique_xmalloc_ptr<char> match (sim_completions[i - 1]);
  996. sim_completions[i - 1] = NULL;
  997. tracker.add_completion (std::move (match));
  998. }
  999. }
  1000. /* Check to see if a thread is still alive. */
  1001. bool
  1002. gdbsim_target::thread_alive (ptid_t ptid)
  1003. {
  1004. struct sim_inferior_data *sim_data
  1005. = get_inferior_data_by_ptid (ptid, SIM_INSTANCE_NOT_NEEDED);
  1006. if (sim_data == NULL)
  1007. return false;
  1008. if (ptid == sim_data->remote_sim_ptid)
  1009. /* The simulators' task is always alive. */
  1010. return true;
  1011. return false;
  1012. }
  1013. /* Convert a thread ID to a string. */
  1014. std::string
  1015. gdbsim_target::pid_to_str (ptid_t ptid)
  1016. {
  1017. return normal_pid_to_str (ptid);
  1018. }
  1019. /* Simulator memory may be accessed after the program has been loaded. */
  1020. bool
  1021. gdbsim_target::has_all_memory ()
  1022. {
  1023. struct sim_inferior_data *sim_data
  1024. = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
  1025. if (!sim_data->program_loaded)
  1026. return false;
  1027. return true;
  1028. }
  1029. bool
  1030. gdbsim_target::has_memory ()
  1031. {
  1032. struct sim_inferior_data *sim_data
  1033. = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NOT_NEEDED);
  1034. if (!sim_data->program_loaded)
  1035. return false;
  1036. return true;
  1037. }
  1038. /* Get memory map from the simulator. */
  1039. std::vector<mem_region>
  1040. gdbsim_target::memory_map ()
  1041. {
  1042. struct sim_inferior_data *sim_data
  1043. = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
  1044. std::vector<mem_region> result;
  1045. gdb::unique_xmalloc_ptr<char> text (sim_memory_map (sim_data->gdbsim_desc));
  1046. if (text != nullptr)
  1047. result = parse_memory_map (text.get ());
  1048. return result;
  1049. }
  1050. void _initialize_remote_sim ();
  1051. void
  1052. _initialize_remote_sim ()
  1053. {
  1054. struct cmd_list_element *c;
  1055. add_target (gdbsim_target_info, gdbsim_target_open);
  1056. c = add_com ("sim", class_obscure, simulator_command,
  1057. _("Send a command to the simulator."));
  1058. set_cmd_completer (c, sim_command_completer);
  1059. }