mips-linux-nat.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /* Native-dependent code for GNU/Linux on MIPS processors.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "command.h"
  16. #include "gdbcmd.h"
  17. #include "inferior.h"
  18. #include "mips-tdep.h"
  19. #include "target.h"
  20. #include "regcache.h"
  21. #include "linux-nat-trad.h"
  22. #include "mips-linux-tdep.h"
  23. #include "target-descriptions.h"
  24. #include "gdb_proc_service.h"
  25. #include "gregset.h"
  26. #include <sgidefs.h>
  27. #include "nat/gdb_ptrace.h"
  28. #include <asm/ptrace.h>
  29. #include "inf-ptrace.h"
  30. #include "nat/mips-linux-watch.h"
  31. #ifndef PTRACE_GET_THREAD_AREA
  32. #define PTRACE_GET_THREAD_AREA 25
  33. #endif
  34. class mips_linux_nat_target final : public linux_nat_trad_target
  35. {
  36. public:
  37. /* Add our register access methods. */
  38. void fetch_registers (struct regcache *, int) override;
  39. void store_registers (struct regcache *, int) override;
  40. void close () override;
  41. int can_use_hw_breakpoint (enum bptype, int, int) override;
  42. int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  43. struct expression *) override;
  44. int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  45. struct expression *) override;
  46. bool stopped_by_watchpoint () override;
  47. bool stopped_data_address (CORE_ADDR *) override;
  48. int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
  49. const struct target_desc *read_description () override;
  50. protected:
  51. /* Override linux_nat_trad_target methods. */
  52. CORE_ADDR register_u_offset (struct gdbarch *gdbarch,
  53. int regno, int store_p) override;
  54. /* Override linux_nat_target low methods. */
  55. void low_new_thread (struct lwp_info *lp) override;
  56. private:
  57. /* Helpers. See definitions. */
  58. void mips64_regsets_store_registers (struct regcache *regcache,
  59. int regno);
  60. void mips64_regsets_fetch_registers (struct regcache *regcache,
  61. int regno);
  62. };
  63. static mips_linux_nat_target the_mips_linux_nat_target;
  64. /* Assume that we have PTRACE_GETREGS et al. support. If we do not,
  65. we'll clear this and use PTRACE_PEEKUSER instead. */
  66. static int have_ptrace_regsets = 1;
  67. /* Map gdb internal register number to ptrace ``address''.
  68. These ``addresses'' are normally defined in <asm/ptrace.h>.
  69. ptrace does not provide a way to read (or set) MIPS_PS_REGNUM,
  70. and there's no point in reading or setting MIPS_ZERO_REGNUM.
  71. We also can not set BADVADDR, CAUSE, or FCRIR via ptrace(). */
  72. static CORE_ADDR
  73. mips_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
  74. {
  75. CORE_ADDR regaddr;
  76. if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  77. error (_("Bogon register number %d."), regno);
  78. if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
  79. regaddr = regno;
  80. else if ((regno >= mips_regnum (gdbarch)->fp0)
  81. && (regno < mips_regnum (gdbarch)->fp0 + 32))
  82. regaddr = FPR_BASE + (regno - mips_regnum (gdbarch)->fp0);
  83. else if (regno == mips_regnum (gdbarch)->pc)
  84. regaddr = PC;
  85. else if (regno == mips_regnum (gdbarch)->cause)
  86. regaddr = store? (CORE_ADDR) -1 : CAUSE;
  87. else if (regno == mips_regnum (gdbarch)->badvaddr)
  88. regaddr = store? (CORE_ADDR) -1 : BADVADDR;
  89. else if (regno == mips_regnum (gdbarch)->lo)
  90. regaddr = MMLO;
  91. else if (regno == mips_regnum (gdbarch)->hi)
  92. regaddr = MMHI;
  93. else if (regno == mips_regnum (gdbarch)->fp_control_status)
  94. regaddr = FPC_CSR;
  95. else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  96. regaddr = store? (CORE_ADDR) -1 : FPC_EIR;
  97. else if (mips_regnum (gdbarch)->dspacc != -1
  98. && regno >= mips_regnum (gdbarch)->dspacc
  99. && regno < mips_regnum (gdbarch)->dspacc + 6)
  100. regaddr = DSP_BASE + (regno - mips_regnum (gdbarch)->dspacc);
  101. else if (regno == mips_regnum (gdbarch)->dspctl)
  102. regaddr = DSP_CONTROL;
  103. else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
  104. regaddr = 0;
  105. else
  106. regaddr = (CORE_ADDR) -1;
  107. return regaddr;
  108. }
  109. static CORE_ADDR
  110. mips64_linux_register_addr (struct gdbarch *gdbarch, int regno, int store)
  111. {
  112. CORE_ADDR regaddr;
  113. if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  114. error (_("Bogon register number %d."), regno);
  115. /* On n32 we can't access 64-bit registers via PTRACE_PEEKUSR
  116. or PTRACE_POKEUSR. */
  117. if (register_size (gdbarch, regno) > sizeof (PTRACE_TYPE_RET))
  118. return (CORE_ADDR) -1;
  119. if (regno > MIPS_ZERO_REGNUM && regno < MIPS_ZERO_REGNUM + 32)
  120. regaddr = regno;
  121. else if ((regno >= mips_regnum (gdbarch)->fp0)
  122. && (regno < mips_regnum (gdbarch)->fp0 + 32))
  123. regaddr = MIPS64_FPR_BASE + (regno - gdbarch_fp0_regnum (gdbarch));
  124. else if (regno == mips_regnum (gdbarch)->pc)
  125. regaddr = MIPS64_PC;
  126. else if (regno == mips_regnum (gdbarch)->cause)
  127. regaddr = store? (CORE_ADDR) -1 : MIPS64_CAUSE;
  128. else if (regno == mips_regnum (gdbarch)->badvaddr)
  129. regaddr = store? (CORE_ADDR) -1 : MIPS64_BADVADDR;
  130. else if (regno == mips_regnum (gdbarch)->lo)
  131. regaddr = MIPS64_MMLO;
  132. else if (regno == mips_regnum (gdbarch)->hi)
  133. regaddr = MIPS64_MMHI;
  134. else if (regno == mips_regnum (gdbarch)->fp_control_status)
  135. regaddr = MIPS64_FPC_CSR;
  136. else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  137. regaddr = store? (CORE_ADDR) -1 : MIPS64_FPC_EIR;
  138. else if (mips_regnum (gdbarch)->dspacc != -1
  139. && regno >= mips_regnum (gdbarch)->dspacc
  140. && regno < mips_regnum (gdbarch)->dspacc + 6)
  141. regaddr = DSP_BASE + (regno - mips_regnum (gdbarch)->dspacc);
  142. else if (regno == mips_regnum (gdbarch)->dspctl)
  143. regaddr = DSP_CONTROL;
  144. else if (mips_linux_restart_reg_p (gdbarch) && regno == MIPS_RESTART_REGNUM)
  145. regaddr = 0;
  146. else
  147. regaddr = (CORE_ADDR) -1;
  148. return regaddr;
  149. }
  150. /* Fetch the thread-local storage pointer for libthread_db. */
  151. ps_err_e
  152. ps_get_thread_area (struct ps_prochandle *ph,
  153. lwpid_t lwpid, int idx, void **base)
  154. {
  155. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  156. return PS_ERR;
  157. /* IDX is the bias from the thread pointer to the beginning of the
  158. thread descriptor. It has to be subtracted due to implementation
  159. quirks in libthread_db. */
  160. *base = (void *) ((char *)*base - idx);
  161. return PS_OK;
  162. }
  163. /* Wrapper functions. These are only used by libthread_db. */
  164. void
  165. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  166. {
  167. if (mips_isa_regsize (regcache->arch ()) == 4)
  168. mips_supply_gregset (regcache, (const mips_elf_gregset_t *) gregsetp);
  169. else
  170. mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *) gregsetp);
  171. }
  172. void
  173. fill_gregset (const struct regcache *regcache,
  174. gdb_gregset_t *gregsetp, int regno)
  175. {
  176. if (mips_isa_regsize (regcache->arch ()) == 4)
  177. mips_fill_gregset (regcache, (mips_elf_gregset_t *) gregsetp, regno);
  178. else
  179. mips64_fill_gregset (regcache, (mips64_elf_gregset_t *) gregsetp, regno);
  180. }
  181. void
  182. supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
  183. {
  184. mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *) fpregsetp);
  185. }
  186. void
  187. fill_fpregset (const struct regcache *regcache,
  188. gdb_fpregset_t *fpregsetp, int regno)
  189. {
  190. mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *) fpregsetp, regno);
  191. }
  192. /* Fetch REGNO (or all registers if REGNO == -1) from the target
  193. using PTRACE_GETREGS et al. */
  194. void
  195. mips_linux_nat_target::mips64_regsets_fetch_registers
  196. (struct regcache *regcache, int regno)
  197. {
  198. struct gdbarch *gdbarch = regcache->arch ();
  199. int is_fp, is_dsp;
  200. int have_dsp;
  201. int regi;
  202. int tid;
  203. if (regno >= mips_regnum (gdbarch)->fp0
  204. && regno <= mips_regnum (gdbarch)->fp0 + 32)
  205. is_fp = 1;
  206. else if (regno == mips_regnum (gdbarch)->fp_control_status)
  207. is_fp = 1;
  208. else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  209. is_fp = 1;
  210. else
  211. is_fp = 0;
  212. /* DSP registers are optional and not a part of any set. */
  213. have_dsp = mips_regnum (gdbarch)->dspctl != -1;
  214. if (!have_dsp)
  215. is_dsp = 0;
  216. else if (regno >= mips_regnum (gdbarch)->dspacc
  217. && regno < mips_regnum (gdbarch)->dspacc + 6)
  218. is_dsp = 1;
  219. else if (regno == mips_regnum (gdbarch)->dspctl)
  220. is_dsp = 1;
  221. else
  222. is_dsp = 0;
  223. tid = get_ptrace_pid (regcache->ptid ());
  224. if (regno == -1 || (!is_fp && !is_dsp))
  225. {
  226. mips64_elf_gregset_t regs;
  227. if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
  228. {
  229. if (errno == EIO)
  230. {
  231. have_ptrace_regsets = 0;
  232. return;
  233. }
  234. perror_with_name (_("Couldn't get registers"));
  235. }
  236. mips64_supply_gregset (regcache,
  237. (const mips64_elf_gregset_t *) &regs);
  238. }
  239. if (regno == -1 || is_fp)
  240. {
  241. mips64_elf_fpregset_t fp_regs;
  242. if (ptrace (PTRACE_GETFPREGS, tid, 0L,
  243. (PTRACE_TYPE_ARG3) &fp_regs) == -1)
  244. {
  245. if (errno == EIO)
  246. {
  247. have_ptrace_regsets = 0;
  248. return;
  249. }
  250. perror_with_name (_("Couldn't get FP registers"));
  251. }
  252. mips64_supply_fpregset (regcache,
  253. (const mips64_elf_fpregset_t *) &fp_regs);
  254. }
  255. if (is_dsp)
  256. linux_nat_trad_target::fetch_registers (regcache, regno);
  257. else if (regno == -1 && have_dsp)
  258. {
  259. for (regi = mips_regnum (gdbarch)->dspacc;
  260. regi < mips_regnum (gdbarch)->dspacc + 6;
  261. regi++)
  262. linux_nat_trad_target::fetch_registers (regcache, regi);
  263. linux_nat_trad_target::fetch_registers (regcache,
  264. mips_regnum (gdbarch)->dspctl);
  265. }
  266. }
  267. /* Store REGNO (or all registers if REGNO == -1) to the target
  268. using PTRACE_SETREGS et al. */
  269. void
  270. mips_linux_nat_target::mips64_regsets_store_registers
  271. (struct regcache *regcache, int regno)
  272. {
  273. struct gdbarch *gdbarch = regcache->arch ();
  274. int is_fp, is_dsp;
  275. int have_dsp;
  276. int regi;
  277. int tid;
  278. if (regno >= mips_regnum (gdbarch)->fp0
  279. && regno <= mips_regnum (gdbarch)->fp0 + 32)
  280. is_fp = 1;
  281. else if (regno == mips_regnum (gdbarch)->fp_control_status)
  282. is_fp = 1;
  283. else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
  284. is_fp = 1;
  285. else
  286. is_fp = 0;
  287. /* DSP registers are optional and not a part of any set. */
  288. have_dsp = mips_regnum (gdbarch)->dspctl != -1;
  289. if (!have_dsp)
  290. is_dsp = 0;
  291. else if (regno >= mips_regnum (gdbarch)->dspacc
  292. && regno < mips_regnum (gdbarch)->dspacc + 6)
  293. is_dsp = 1;
  294. else if (regno == mips_regnum (gdbarch)->dspctl)
  295. is_dsp = 1;
  296. else
  297. is_dsp = 0;
  298. tid = get_ptrace_pid (regcache->ptid ());
  299. if (regno == -1 || (!is_fp && !is_dsp))
  300. {
  301. mips64_elf_gregset_t regs;
  302. if (ptrace (PTRACE_GETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
  303. perror_with_name (_("Couldn't get registers"));
  304. mips64_fill_gregset (regcache, &regs, regno);
  305. if (ptrace (PTRACE_SETREGS, tid, 0L, (PTRACE_TYPE_ARG3) &regs) == -1)
  306. perror_with_name (_("Couldn't set registers"));
  307. }
  308. if (regno == -1 || is_fp)
  309. {
  310. mips64_elf_fpregset_t fp_regs;
  311. if (ptrace (PTRACE_GETFPREGS, tid, 0L,
  312. (PTRACE_TYPE_ARG3) &fp_regs) == -1)
  313. perror_with_name (_("Couldn't get FP registers"));
  314. mips64_fill_fpregset (regcache, &fp_regs, regno);
  315. if (ptrace (PTRACE_SETFPREGS, tid, 0L,
  316. (PTRACE_TYPE_ARG3) &fp_regs) == -1)
  317. perror_with_name (_("Couldn't set FP registers"));
  318. }
  319. if (is_dsp)
  320. linux_nat_trad_target::store_registers (regcache, regno);
  321. else if (regno == -1 && have_dsp)
  322. {
  323. for (regi = mips_regnum (gdbarch)->dspacc;
  324. regi < mips_regnum (gdbarch)->dspacc + 6;
  325. regi++)
  326. linux_nat_trad_target::store_registers (regcache, regi);
  327. linux_nat_trad_target::store_registers (regcache,
  328. mips_regnum (gdbarch)->dspctl);
  329. }
  330. }
  331. /* Fetch REGNO (or all registers if REGNO == -1) from the target
  332. using any working method. */
  333. void
  334. mips_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  335. {
  336. /* Unless we already know that PTRACE_GETREGS does not work, try it. */
  337. if (have_ptrace_regsets)
  338. mips64_regsets_fetch_registers (regcache, regnum);
  339. /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
  340. back to PTRACE_PEEKUSER. */
  341. if (!have_ptrace_regsets)
  342. {
  343. linux_nat_trad_target::fetch_registers (regcache, regnum);
  344. /* Fill the inaccessible zero register with zero. */
  345. if (regnum == MIPS_ZERO_REGNUM || regnum == -1)
  346. regcache->raw_supply_zeroed (MIPS_ZERO_REGNUM);
  347. }
  348. }
  349. /* Store REGNO (or all registers if REGNO == -1) to the target
  350. using any working method. */
  351. void
  352. mips_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
  353. {
  354. /* Unless we already know that PTRACE_GETREGS does not work, try it. */
  355. if (have_ptrace_regsets)
  356. mips64_regsets_store_registers (regcache, regnum);
  357. /* If we know, or just found out, that PTRACE_GETREGS does not work, fall
  358. back to PTRACE_PEEKUSER. */
  359. if (!have_ptrace_regsets)
  360. linux_nat_trad_target::store_registers (regcache, regnum);
  361. }
  362. /* Return the address in the core dump or inferior of register
  363. REGNO. */
  364. CORE_ADDR
  365. mips_linux_nat_target::register_u_offset (struct gdbarch *gdbarch,
  366. int regno, int store_p)
  367. {
  368. if (mips_abi_regsize (gdbarch) == 8)
  369. return mips64_linux_register_addr (gdbarch, regno, store_p);
  370. else
  371. return mips_linux_register_addr (gdbarch, regno, store_p);
  372. }
  373. const struct target_desc *
  374. mips_linux_nat_target::read_description ()
  375. {
  376. static int have_dsp = -1;
  377. if (have_dsp < 0)
  378. {
  379. int tid = get_ptrace_pid (inferior_ptid);
  380. errno = 0;
  381. ptrace (PTRACE_PEEKUSER, tid, DSP_CONTROL, 0);
  382. switch (errno)
  383. {
  384. case 0:
  385. have_dsp = 1;
  386. break;
  387. case EIO:
  388. have_dsp = 0;
  389. break;
  390. default:
  391. perror_with_name (_("Couldn't check DSP support"));
  392. break;
  393. }
  394. }
  395. /* Report that target registers are a size we know for sure
  396. that we can get from ptrace. */
  397. if (_MIPS_SIM == _ABIO32)
  398. return have_dsp ? tdesc_mips_dsp_linux : tdesc_mips_linux;
  399. else
  400. return have_dsp ? tdesc_mips64_dsp_linux : tdesc_mips64_linux;
  401. }
  402. /* -1 if the kernel and/or CPU do not support watch registers.
  403. 1 if watch_readback is valid and we can read style, num_valid
  404. and the masks.
  405. 0 if we need to read the watch_readback. */
  406. static int watch_readback_valid;
  407. /* Cached watch register read values. */
  408. static struct pt_watch_regs watch_readback;
  409. static struct mips_watchpoint *current_watches;
  410. /* The current set of watch register values for writing the
  411. registers. */
  412. static struct pt_watch_regs watch_mirror;
  413. static void
  414. mips_show_dr (const char *func, CORE_ADDR addr,
  415. int len, enum target_hw_bp_type type)
  416. {
  417. int i;
  418. gdb_puts (func, gdb_stdlog);
  419. if (addr || len)
  420. gdb_printf (gdb_stdlog,
  421. " (addr=%s, len=%d, type=%s)",
  422. paddress (target_gdbarch (), addr), len,
  423. type == hw_write ? "data-write"
  424. : (type == hw_read ? "data-read"
  425. : (type == hw_access ? "data-read/write"
  426. : (type == hw_execute ? "instruction-execute"
  427. : "??unknown??"))));
  428. gdb_puts (":\n", gdb_stdlog);
  429. for (i = 0; i < MAX_DEBUG_REGISTER; i++)
  430. gdb_printf (gdb_stdlog, "\tDR%d: lo=%s, hi=%s\n", i,
  431. paddress (target_gdbarch (),
  432. mips_linux_watch_get_watchlo (&watch_mirror,
  433. i)),
  434. paddress (target_gdbarch (),
  435. mips_linux_watch_get_watchhi (&watch_mirror,
  436. i)));
  437. }
  438. /* Target to_can_use_hw_breakpoint implementation. Return 1 if we can
  439. handle the specified watch type. */
  440. int
  441. mips_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
  442. int cnt, int ot)
  443. {
  444. int i;
  445. uint32_t wanted_mask, irw_mask;
  446. if (!mips_linux_read_watch_registers (inferior_ptid.lwp (),
  447. &watch_readback,
  448. &watch_readback_valid, 0))
  449. return 0;
  450. switch (type)
  451. {
  452. case bp_hardware_watchpoint:
  453. wanted_mask = W_MASK;
  454. break;
  455. case bp_read_watchpoint:
  456. wanted_mask = R_MASK;
  457. break;
  458. case bp_access_watchpoint:
  459. wanted_mask = R_MASK | W_MASK;
  460. break;
  461. default:
  462. return 0;
  463. }
  464. for (i = 0;
  465. i < mips_linux_watch_get_num_valid (&watch_readback) && cnt;
  466. i++)
  467. {
  468. irw_mask = mips_linux_watch_get_irw_mask (&watch_readback, i);
  469. if ((irw_mask & wanted_mask) == wanted_mask)
  470. cnt--;
  471. }
  472. return (cnt == 0) ? 1 : 0;
  473. }
  474. /* Target to_stopped_by_watchpoint implementation. Return 1 if
  475. stopped by watchpoint. The watchhi R and W bits indicate the watch
  476. register triggered. */
  477. bool
  478. mips_linux_nat_target::stopped_by_watchpoint ()
  479. {
  480. int n;
  481. int num_valid;
  482. if (!mips_linux_read_watch_registers (inferior_ptid.lwp (),
  483. &watch_readback,
  484. &watch_readback_valid, 1))
  485. return false;
  486. num_valid = mips_linux_watch_get_num_valid (&watch_readback);
  487. for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
  488. if (mips_linux_watch_get_watchhi (&watch_readback, n) & (R_MASK | W_MASK))
  489. return true;
  490. return false;
  491. }
  492. /* Target to_stopped_data_address implementation. Set the address
  493. where the watch triggered (if known). Return 1 if the address was
  494. known. */
  495. bool
  496. mips_linux_nat_target::stopped_data_address (CORE_ADDR *paddr)
  497. {
  498. /* On mips we don't know the low order 3 bits of the data address,
  499. so we must return false. */
  500. return false;
  501. }
  502. /* Target to_region_ok_for_hw_watchpoint implementation. Return 1 if
  503. the specified region can be covered by the watch registers. */
  504. int
  505. mips_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
  506. {
  507. struct pt_watch_regs dummy_regs;
  508. int i;
  509. if (!mips_linux_read_watch_registers (inferior_ptid.lwp (),
  510. &watch_readback,
  511. &watch_readback_valid, 0))
  512. return 0;
  513. dummy_regs = watch_readback;
  514. /* Clear them out. */
  515. for (i = 0; i < mips_linux_watch_get_num_valid (&dummy_regs); i++)
  516. mips_linux_watch_set_watchlo (&dummy_regs, i, 0);
  517. return mips_linux_watch_try_one_watch (&dummy_regs, addr, len, 0);
  518. }
  519. /* Write the mirrored watch register values for each thread. */
  520. static int
  521. write_watchpoint_regs (void)
  522. {
  523. for (const lwp_info *lp : all_lwps ())
  524. {
  525. int tid = lp->ptid.lwp ();
  526. if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror, NULL) == -1)
  527. perror_with_name (_("Couldn't write debug register"));
  528. }
  529. return 0;
  530. }
  531. /* linux_nat_target::low_new_thread implementation. Write the
  532. mirrored watch register values for the new thread. */
  533. void
  534. mips_linux_nat_target::low_new_thread (struct lwp_info *lp)
  535. {
  536. long tid = lp->ptid.lwp ();
  537. if (!mips_linux_read_watch_registers (tid,
  538. &watch_readback,
  539. &watch_readback_valid, 0))
  540. return;
  541. if (ptrace (PTRACE_SET_WATCH_REGS, tid, &watch_mirror, NULL) == -1)
  542. perror_with_name (_("Couldn't write debug register"));
  543. }
  544. /* Target to_insert_watchpoint implementation. Try to insert a new
  545. watch. Return zero on success. */
  546. int
  547. mips_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
  548. enum target_hw_bp_type type,
  549. struct expression *cond)
  550. {
  551. struct pt_watch_regs regs;
  552. struct mips_watchpoint *new_watch;
  553. struct mips_watchpoint **pw;
  554. int retval;
  555. if (!mips_linux_read_watch_registers (inferior_ptid.lwp (),
  556. &watch_readback,
  557. &watch_readback_valid, 0))
  558. return -1;
  559. if (len <= 0)
  560. return -1;
  561. regs = watch_readback;
  562. /* Add the current watches. */
  563. mips_linux_watch_populate_regs (current_watches, &regs);
  564. /* Now try to add the new watch. */
  565. if (!mips_linux_watch_try_one_watch (&regs, addr, len,
  566. mips_linux_watch_type_to_irw (type)))
  567. return -1;
  568. /* It fit. Stick it on the end of the list. */
  569. new_watch = XNEW (struct mips_watchpoint);
  570. new_watch->addr = addr;
  571. new_watch->len = len;
  572. new_watch->type = type;
  573. new_watch->next = NULL;
  574. pw = &current_watches;
  575. while (*pw != NULL)
  576. pw = &(*pw)->next;
  577. *pw = new_watch;
  578. watch_mirror = regs;
  579. retval = write_watchpoint_regs ();
  580. if (show_debug_regs)
  581. mips_show_dr ("insert_watchpoint", addr, len, type);
  582. return retval;
  583. }
  584. /* Target to_remove_watchpoint implementation. Try to remove a watch.
  585. Return zero on success. */
  586. int
  587. mips_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
  588. enum target_hw_bp_type type,
  589. struct expression *cond)
  590. {
  591. int retval;
  592. int deleted_one;
  593. struct mips_watchpoint **pw;
  594. struct mips_watchpoint *w;
  595. /* Search for a known watch that matches. Then unlink and free
  596. it. */
  597. deleted_one = 0;
  598. pw = &current_watches;
  599. while ((w = *pw))
  600. {
  601. if (w->addr == addr && w->len == len && w->type == type)
  602. {
  603. *pw = w->next;
  604. xfree (w);
  605. deleted_one = 1;
  606. break;
  607. }
  608. pw = &(w->next);
  609. }
  610. if (!deleted_one)
  611. return -1; /* We don't know about it, fail doing nothing. */
  612. /* At this point watch_readback is known to be valid because we
  613. could not have added the watch without reading it. */
  614. gdb_assert (watch_readback_valid == 1);
  615. watch_mirror = watch_readback;
  616. mips_linux_watch_populate_regs (current_watches, &watch_mirror);
  617. retval = write_watchpoint_regs ();
  618. if (show_debug_regs)
  619. mips_show_dr ("remove_watchpoint", addr, len, type);
  620. return retval;
  621. }
  622. /* Target to_close implementation. Free any watches and call the
  623. super implementation. */
  624. void
  625. mips_linux_nat_target::close ()
  626. {
  627. struct mips_watchpoint *w;
  628. struct mips_watchpoint *nw;
  629. /* Clean out the current_watches list. */
  630. w = current_watches;
  631. while (w)
  632. {
  633. nw = w->next;
  634. xfree (w);
  635. w = nw;
  636. }
  637. current_watches = NULL;
  638. linux_nat_trad_target::close ();
  639. }
  640. void _initialize_mips_linux_nat ();
  641. void
  642. _initialize_mips_linux_nat ()
  643. {
  644. add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
  645. &show_debug_regs, _("\
  646. Set whether to show variables that mirror the mips debug registers."), _("\
  647. Show whether to show variables that mirror the mips debug registers."), _("\
  648. Use \"on\" to enable, \"off\" to disable.\n\
  649. If enabled, the debug registers values are shown when GDB inserts\n\
  650. or removes a hardware breakpoint or watchpoint, and when the inferior\n\
  651. triggers a breakpoint or watchpoint."),
  652. NULL,
  653. NULL,
  654. &maintenance_set_cmdlist,
  655. &maintenance_show_cmdlist);
  656. linux_target = &the_mips_linux_nat_target;
  657. add_inf_child_target (&the_mips_linux_nat_target);
  658. }