memattr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /* Memory attributes support, for GDB.
  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 "memattr.h"
  18. #include "target.h"
  19. #include "target-dcache.h"
  20. #include "value.h"
  21. #include "language.h"
  22. #include "breakpoint.h"
  23. #include "cli/cli-utils.h"
  24. #include <algorithm>
  25. #include "gdbarch.h"
  26. static std::vector<mem_region> user_mem_region_list, target_mem_region_list;
  27. static std::vector<mem_region> *mem_region_list = &target_mem_region_list;
  28. static int mem_number = 0;
  29. /* If this flag is set, the memory region list should be automatically
  30. updated from the target. If it is clear, the list is user-controlled
  31. and should be left alone. */
  32. static bool
  33. mem_use_target ()
  34. {
  35. return mem_region_list == &target_mem_region_list;
  36. }
  37. /* If this flag is set, we have tried to fetch the target memory regions
  38. since the last time it was invalidated. If that list is still
  39. empty, then the target can't supply memory regions. */
  40. static bool target_mem_regions_valid;
  41. /* If this flag is set, gdb will assume that memory ranges not
  42. specified by the memory map have type MEM_NONE, and will
  43. emit errors on all accesses to that memory. */
  44. static bool inaccessible_by_default = true;
  45. static void
  46. show_inaccessible_by_default (struct ui_file *file, int from_tty,
  47. struct cmd_list_element *c,
  48. const char *value)
  49. {
  50. if (inaccessible_by_default)
  51. gdb_printf (file, _("Unknown memory addresses will "
  52. "be treated as inaccessible.\n"));
  53. else
  54. gdb_printf (file, _("Unknown memory addresses "
  55. "will be treated as RAM.\n"));
  56. }
  57. /* This function should be called before any command which would
  58. modify the memory region list. It will handle switching from
  59. a target-provided list to a local list, if necessary. */
  60. static void
  61. require_user_regions (int from_tty)
  62. {
  63. /* If we're already using a user-provided list, nothing to do. */
  64. if (!mem_use_target ())
  65. return;
  66. /* Switch to a user-provided list (possibly a copy of the current
  67. one). */
  68. mem_region_list = &user_mem_region_list;
  69. /* If we don't have a target-provided region list yet, then
  70. no need to warn. */
  71. if (target_mem_region_list.empty ())
  72. return;
  73. /* Otherwise, let the user know how to get back. */
  74. if (from_tty)
  75. warning (_("Switching to manual control of memory regions; use "
  76. "\"mem auto\" to fetch regions from the target again."));
  77. /* And create a new list (copy of the target-supplied regions) for the user
  78. to modify. */
  79. user_mem_region_list = target_mem_region_list;
  80. }
  81. /* This function should be called before any command which would
  82. read the memory region list, other than those which call
  83. require_user_regions. It will handle fetching the
  84. target-provided list, if necessary. */
  85. static void
  86. require_target_regions (void)
  87. {
  88. if (mem_use_target () && !target_mem_regions_valid)
  89. {
  90. target_mem_regions_valid = true;
  91. target_mem_region_list = target_memory_map ();
  92. }
  93. }
  94. /* Create a new user-defined memory region. */
  95. static void
  96. create_user_mem_region (CORE_ADDR lo, CORE_ADDR hi,
  97. const mem_attrib &attrib)
  98. {
  99. /* lo == hi is a useless empty region. */
  100. if (lo >= hi && hi != 0)
  101. {
  102. gdb_printf (_("invalid memory region: low >= high\n"));
  103. return;
  104. }
  105. mem_region newobj (lo, hi, attrib);
  106. auto it = std::lower_bound (user_mem_region_list.begin (),
  107. user_mem_region_list.end (),
  108. newobj);
  109. int ix = std::distance (user_mem_region_list.begin (), it);
  110. /* Check for an overlapping memory region. We only need to check
  111. in the vincinity - at most one before and one after the
  112. insertion point. */
  113. for (int i = ix - 1; i < ix + 1; i++)
  114. {
  115. if (i < 0)
  116. continue;
  117. if (i >= user_mem_region_list.size ())
  118. continue;
  119. mem_region &n = user_mem_region_list[i];
  120. if ((lo >= n.lo && (lo < n.hi || n.hi == 0))
  121. || (hi > n.lo && (hi <= n.hi || n.hi == 0))
  122. || (lo <= n.lo && ((hi >= n.hi && n.hi != 0) || hi == 0)))
  123. {
  124. gdb_printf (_("overlapping memory region\n"));
  125. return;
  126. }
  127. }
  128. newobj.number = ++mem_number;
  129. user_mem_region_list.insert (it, newobj);
  130. }
  131. /* Look up the memory region corresponding to ADDR. */
  132. struct mem_region *
  133. lookup_mem_region (CORE_ADDR addr)
  134. {
  135. static struct mem_region region (0, 0);
  136. CORE_ADDR lo;
  137. CORE_ADDR hi;
  138. require_target_regions ();
  139. /* First we initialize LO and HI so that they describe the entire
  140. memory space. As we process the memory region chain, they are
  141. redefined to describe the minimal region containing ADDR. LO
  142. and HI are used in the case where no memory region is defined
  143. that contains ADDR. If a memory region is disabled, it is
  144. treated as if it does not exist. The initial values for LO
  145. and HI represent the bottom and top of memory. */
  146. lo = 0;
  147. hi = 0;
  148. /* Either find memory range containing ADDR, or set LO and HI
  149. to the nearest boundaries of an existing memory range.
  150. If we ever want to support a huge list of memory regions, this
  151. check should be replaced with a binary search (probably using
  152. VEC_lower_bound). */
  153. for (mem_region &m : *mem_region_list)
  154. {
  155. if (m.enabled_p == 1)
  156. {
  157. /* If the address is in the memory region, return that
  158. memory range. */
  159. if (addr >= m.lo && (addr < m.hi || m.hi == 0))
  160. return &m;
  161. /* This (correctly) won't match if m->hi == 0, representing
  162. the top of the address space, because CORE_ADDR is unsigned;
  163. no value of LO is less than zero. */
  164. if (addr >= m.hi && lo < m.hi)
  165. lo = m.hi;
  166. /* This will never set HI to zero; if we're here and ADDR
  167. is at or below M, and the region starts at zero, then ADDR
  168. would have been in the region. */
  169. if (addr <= m.lo && (hi == 0 || hi > m.lo))
  170. hi = m.lo;
  171. }
  172. }
  173. /* Because no region was found, we must cons up one based on what
  174. was learned above. */
  175. region.lo = lo;
  176. region.hi = hi;
  177. /* When no memory map is defined at all, we always return
  178. 'default_mem_attrib', so that we do not make all memory
  179. inaccessible for targets that don't provide a memory map. */
  180. if (inaccessible_by_default && !mem_region_list->empty ())
  181. region.attrib = mem_attrib::unknown ();
  182. else
  183. region.attrib = mem_attrib ();
  184. return &region;
  185. }
  186. /* Invalidate any memory regions fetched from the target. */
  187. void
  188. invalidate_target_mem_regions (void)
  189. {
  190. if (!target_mem_regions_valid)
  191. return;
  192. target_mem_regions_valid = false;
  193. target_mem_region_list.clear ();
  194. }
  195. /* Clear user-defined memory region list. */
  196. static void
  197. user_mem_clear (void)
  198. {
  199. user_mem_region_list.clear ();
  200. }
  201. static void
  202. mem_command (const char *args, int from_tty)
  203. {
  204. CORE_ADDR lo, hi;
  205. if (!args)
  206. error_no_arg (_("No mem"));
  207. /* For "mem auto", switch back to using a target provided list. */
  208. if (strcmp (args, "auto") == 0)
  209. {
  210. if (mem_use_target ())
  211. return;
  212. user_mem_clear ();
  213. mem_region_list = &target_mem_region_list;
  214. return;
  215. }
  216. require_user_regions (from_tty);
  217. std::string tok = extract_arg (&args);
  218. if (tok == "")
  219. error (_("no lo address"));
  220. lo = parse_and_eval_address (tok.c_str ());
  221. tok = extract_arg (&args);
  222. if (tok == "")
  223. error (_("no hi address"));
  224. hi = parse_and_eval_address (tok.c_str ());
  225. mem_attrib attrib;
  226. while ((tok = extract_arg (&args)) != "")
  227. {
  228. if (tok == "rw")
  229. attrib.mode = MEM_RW;
  230. else if (tok == "ro")
  231. attrib.mode = MEM_RO;
  232. else if (tok == "wo")
  233. attrib.mode = MEM_WO;
  234. else if (tok == "8")
  235. attrib.width = MEM_WIDTH_8;
  236. else if (tok == "16")
  237. {
  238. if ((lo % 2 != 0) || (hi % 2 != 0))
  239. error (_("region bounds not 16 bit aligned"));
  240. attrib.width = MEM_WIDTH_16;
  241. }
  242. else if (tok == "32")
  243. {
  244. if ((lo % 4 != 0) || (hi % 4 != 0))
  245. error (_("region bounds not 32 bit aligned"));
  246. attrib.width = MEM_WIDTH_32;
  247. }
  248. else if (tok == "64")
  249. {
  250. if ((lo % 8 != 0) || (hi % 8 != 0))
  251. error (_("region bounds not 64 bit aligned"));
  252. attrib.width = MEM_WIDTH_64;
  253. }
  254. #if 0
  255. else if (tok == "hwbreak")
  256. attrib.hwbreak = 1;
  257. else if (tok == "swbreak")
  258. attrib.hwbreak = 0;
  259. #endif
  260. else if (tok == "cache")
  261. attrib.cache = 1;
  262. else if (tok == "nocache")
  263. attrib.cache = 0;
  264. #if 0
  265. else if (tok == "verify")
  266. attrib.verify = 1;
  267. else if (tok == "noverify")
  268. attrib.verify = 0;
  269. #endif
  270. else
  271. error (_("unknown attribute: %s"), tok.c_str ());
  272. }
  273. create_user_mem_region (lo, hi, attrib);
  274. }
  275. static void
  276. info_mem_command (const char *args, int from_tty)
  277. {
  278. if (mem_use_target ())
  279. gdb_printf (_("Using memory regions provided by the target.\n"));
  280. else
  281. gdb_printf (_("Using user-defined memory regions.\n"));
  282. require_target_regions ();
  283. if (mem_region_list->empty ())
  284. {
  285. gdb_printf (_("There are no memory regions defined.\n"));
  286. return;
  287. }
  288. gdb_printf ("Num ");
  289. gdb_printf ("Enb ");
  290. gdb_printf ("Low Addr ");
  291. if (gdbarch_addr_bit (target_gdbarch ()) > 32)
  292. gdb_printf (" ");
  293. gdb_printf ("High Addr ");
  294. if (gdbarch_addr_bit (target_gdbarch ()) > 32)
  295. gdb_printf (" ");
  296. gdb_printf ("Attrs ");
  297. gdb_printf ("\n");
  298. for (const mem_region &m : *mem_region_list)
  299. {
  300. const char *tmp;
  301. gdb_printf ("%-3d %-3c\t",
  302. m.number,
  303. m.enabled_p ? 'y' : 'n');
  304. if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
  305. tmp = hex_string_custom (m.lo, 8);
  306. else
  307. tmp = hex_string_custom (m.lo, 16);
  308. gdb_printf ("%s ", tmp);
  309. if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
  310. {
  311. if (m.hi == 0)
  312. tmp = "0x100000000";
  313. else
  314. tmp = hex_string_custom (m.hi, 8);
  315. }
  316. else
  317. {
  318. if (m.hi == 0)
  319. tmp = "0x10000000000000000";
  320. else
  321. tmp = hex_string_custom (m.hi, 16);
  322. }
  323. gdb_printf ("%s ", tmp);
  324. /* Print a token for each attribute.
  325. * FIXME: Should we output a comma after each token? It may
  326. * make it easier for users to read, but we'd lose the ability
  327. * to cut-and-paste the list of attributes when defining a new
  328. * region. Perhaps that is not important.
  329. *
  330. * FIXME: If more attributes are added to GDB, the output may
  331. * become cluttered and difficult for users to read. At that
  332. * time, we may want to consider printing tokens only if they
  333. * are different from the default attribute. */
  334. switch (m.attrib.mode)
  335. {
  336. case MEM_RW:
  337. gdb_printf ("rw ");
  338. break;
  339. case MEM_RO:
  340. gdb_printf ("ro ");
  341. break;
  342. case MEM_WO:
  343. gdb_printf ("wo ");
  344. break;
  345. case MEM_FLASH:
  346. gdb_printf ("flash blocksize 0x%x ", m.attrib.blocksize);
  347. break;
  348. }
  349. switch (m.attrib.width)
  350. {
  351. case MEM_WIDTH_8:
  352. gdb_printf ("8 ");
  353. break;
  354. case MEM_WIDTH_16:
  355. gdb_printf ("16 ");
  356. break;
  357. case MEM_WIDTH_32:
  358. gdb_printf ("32 ");
  359. break;
  360. case MEM_WIDTH_64:
  361. gdb_printf ("64 ");
  362. break;
  363. case MEM_WIDTH_UNSPECIFIED:
  364. break;
  365. }
  366. #if 0
  367. if (attrib->hwbreak)
  368. gdb_printf ("hwbreak");
  369. else
  370. gdb_printf ("swbreak");
  371. #endif
  372. if (m.attrib.cache)
  373. gdb_printf ("cache ");
  374. else
  375. gdb_printf ("nocache ");
  376. #if 0
  377. if (attrib->verify)
  378. gdb_printf ("verify ");
  379. else
  380. gdb_printf ("noverify ");
  381. #endif
  382. gdb_printf ("\n");
  383. }
  384. }
  385. /* Enable the memory region number NUM. */
  386. static void
  387. mem_enable (int num)
  388. {
  389. for (mem_region &m : *mem_region_list)
  390. if (m.number == num)
  391. {
  392. m.enabled_p = 1;
  393. return;
  394. }
  395. gdb_printf (_("No memory region number %d.\n"), num);
  396. }
  397. static void
  398. enable_mem_command (const char *args, int from_tty)
  399. {
  400. require_user_regions (from_tty);
  401. target_dcache_invalidate ();
  402. if (args == NULL || *args == '\0')
  403. { /* Enable all mem regions. */
  404. for (mem_region &m : *mem_region_list)
  405. m.enabled_p = 1;
  406. }
  407. else
  408. {
  409. number_or_range_parser parser (args);
  410. while (!parser.finished ())
  411. {
  412. int num = parser.get_number ();
  413. mem_enable (num);
  414. }
  415. }
  416. }
  417. /* Disable the memory region number NUM. */
  418. static void
  419. mem_disable (int num)
  420. {
  421. for (mem_region &m : *mem_region_list)
  422. if (m.number == num)
  423. {
  424. m.enabled_p = 0;
  425. return;
  426. }
  427. gdb_printf (_("No memory region number %d.\n"), num);
  428. }
  429. static void
  430. disable_mem_command (const char *args, int from_tty)
  431. {
  432. require_user_regions (from_tty);
  433. target_dcache_invalidate ();
  434. if (args == NULL || *args == '\0')
  435. {
  436. for (mem_region &m : *mem_region_list)
  437. m.enabled_p = false;
  438. }
  439. else
  440. {
  441. number_or_range_parser parser (args);
  442. while (!parser.finished ())
  443. {
  444. int num = parser.get_number ();
  445. mem_disable (num);
  446. }
  447. }
  448. }
  449. /* Delete the memory region number NUM. */
  450. static void
  451. mem_delete (int num)
  452. {
  453. if (!mem_region_list)
  454. {
  455. gdb_printf (_("No memory region number %d.\n"), num);
  456. return;
  457. }
  458. auto it = std::remove_if (mem_region_list->begin (), mem_region_list->end (),
  459. [num] (const mem_region &m)
  460. {
  461. return m.number == num;
  462. });
  463. if (it != mem_region_list->end ())
  464. mem_region_list->erase (it);
  465. else
  466. gdb_printf (_("No memory region number %d.\n"), num);
  467. }
  468. static void
  469. delete_mem_command (const char *args, int from_tty)
  470. {
  471. require_user_regions (from_tty);
  472. target_dcache_invalidate ();
  473. if (args == NULL || *args == '\0')
  474. {
  475. if (query (_("Delete all memory regions? ")))
  476. user_mem_clear ();
  477. dont_repeat ();
  478. return;
  479. }
  480. number_or_range_parser parser (args);
  481. while (!parser.finished ())
  482. {
  483. int num = parser.get_number ();
  484. mem_delete (num);
  485. }
  486. dont_repeat ();
  487. }
  488. static struct cmd_list_element *mem_set_cmdlist;
  489. static struct cmd_list_element *mem_show_cmdlist;
  490. void _initialize_mem ();
  491. void
  492. _initialize_mem ()
  493. {
  494. add_com ("mem", class_vars, mem_command, _("\
  495. Define attributes for memory region or reset memory region handling to "
  496. "target-based.\n\
  497. Usage: mem auto\n\
  498. mem LOW HIGH [MODE WIDTH CACHE],\n\
  499. where MODE may be rw (read/write), ro (read-only) or wo (write-only),\n\
  500. WIDTH may be 8, 16, 32, or 64, and\n\
  501. CACHE may be cache or nocache"));
  502. add_cmd ("mem", class_vars, enable_mem_command, _("\
  503. Enable memory region.\n\
  504. Arguments are the IDs of the memory regions to enable.\n\
  505. Usage: enable mem [ID]...\n\
  506. Do \"info mem\" to see current list of IDs."), &enablelist);
  507. add_cmd ("mem", class_vars, disable_mem_command, _("\
  508. Disable memory region.\n\
  509. Arguments are the IDs of the memory regions to disable.\n\
  510. Usage: disable mem [ID]...\n\
  511. Do \"info mem\" to see current list of IDs."), &disablelist);
  512. add_cmd ("mem", class_vars, delete_mem_command, _("\
  513. Delete memory region.\n\
  514. Arguments are the IDs of the memory regions to delete.\n\
  515. Usage: delete mem [ID]...\n\
  516. Do \"info mem\" to see current list of IDs."), &deletelist);
  517. add_info ("mem", info_mem_command,
  518. _("Memory region attributes."));
  519. add_setshow_prefix_cmd ("mem", class_vars,
  520. _("Memory regions settings."),
  521. _("Memory regions settings."),
  522. &mem_set_cmdlist, &mem_show_cmdlist,
  523. &setlist, &showlist);
  524. add_setshow_boolean_cmd ("inaccessible-by-default", no_class,
  525. &inaccessible_by_default, _("\
  526. Set handling of unknown memory regions."), _("\
  527. Show handling of unknown memory regions."), _("\
  528. If on, and some memory map is defined, debugger will emit errors on\n\
  529. accesses to memory not defined in the memory map. If off, accesses to all\n\
  530. memory addresses will be allowed."),
  531. NULL,
  532. show_inaccessible_by_default,
  533. &mem_set_cmdlist,
  534. &mem_show_cmdlist);
  535. }