regcache.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. /* Cache and manage the values of registers for GDB, the GNU debugger.
  2. Copyright (C) 1986-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 "inferior.h"
  16. #include "gdbthread.h"
  17. #include "target.h"
  18. #include "test-target.h"
  19. #include "scoped-mock-context.h"
  20. #include "gdbarch.h"
  21. #include "gdbcmd.h"
  22. #include "regcache.h"
  23. #include "reggroups.h"
  24. #include "observable.h"
  25. #include "regset.h"
  26. #include <unordered_map>
  27. #include "cli/cli-cmds.h"
  28. /*
  29. * DATA STRUCTURE
  30. *
  31. * Here is the actual register cache.
  32. */
  33. /* Per-architecture object describing the layout of a register cache.
  34. Computed once when the architecture is created. */
  35. static struct gdbarch_data *regcache_descr_handle;
  36. struct regcache_descr
  37. {
  38. /* The architecture this descriptor belongs to. */
  39. struct gdbarch *gdbarch;
  40. /* The raw register cache. Each raw (or hard) register is supplied
  41. by the target interface. The raw cache should not contain
  42. redundant information - if the PC is constructed from two
  43. registers then those registers and not the PC lives in the raw
  44. cache. */
  45. long sizeof_raw_registers;
  46. /* The cooked register space. Each cooked register in the range
  47. [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
  48. register. The remaining [NR_RAW_REGISTERS
  49. .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
  50. both raw registers and memory by the architecture methods
  51. gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
  52. int nr_cooked_registers;
  53. long sizeof_cooked_registers;
  54. /* Offset and size (in 8 bit bytes), of each register in the
  55. register cache. All registers (including those in the range
  56. [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
  57. offset. */
  58. long *register_offset;
  59. long *sizeof_register;
  60. /* Cached table containing the type of each register. */
  61. struct type **register_type;
  62. };
  63. static void *
  64. init_regcache_descr (struct gdbarch *gdbarch)
  65. {
  66. int i;
  67. struct regcache_descr *descr;
  68. gdb_assert (gdbarch != NULL);
  69. /* Create an initial, zero filled, table. */
  70. descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
  71. descr->gdbarch = gdbarch;
  72. /* Total size of the register space. The raw registers are mapped
  73. directly onto the raw register cache while the pseudo's are
  74. either mapped onto raw-registers or memory. */
  75. descr->nr_cooked_registers = gdbarch_num_cooked_regs (gdbarch);
  76. /* Fill in a table of register types. */
  77. descr->register_type
  78. = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
  79. struct type *);
  80. for (i = 0; i < descr->nr_cooked_registers; i++)
  81. descr->register_type[i] = gdbarch_register_type (gdbarch, i);
  82. /* Construct a strictly RAW register cache. Don't allow pseudo's
  83. into the register cache. */
  84. /* Lay out the register cache.
  85. NOTE: cagney/2002-05-22: Only register_type () is used when
  86. constructing the register cache. It is assumed that the
  87. register's raw size, virtual size and type length are all the
  88. same. */
  89. {
  90. long offset = 0;
  91. descr->sizeof_register
  92. = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
  93. descr->register_offset
  94. = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
  95. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  96. {
  97. descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
  98. descr->register_offset[i] = offset;
  99. offset += descr->sizeof_register[i];
  100. }
  101. /* Set the real size of the raw register cache buffer. */
  102. descr->sizeof_raw_registers = offset;
  103. for (; i < descr->nr_cooked_registers; i++)
  104. {
  105. descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
  106. descr->register_offset[i] = offset;
  107. offset += descr->sizeof_register[i];
  108. }
  109. /* Set the real size of the readonly register cache buffer. */
  110. descr->sizeof_cooked_registers = offset;
  111. }
  112. return descr;
  113. }
  114. static struct regcache_descr *
  115. regcache_descr (struct gdbarch *gdbarch)
  116. {
  117. return (struct regcache_descr *) gdbarch_data (gdbarch,
  118. regcache_descr_handle);
  119. }
  120. /* Utility functions returning useful register attributes stored in
  121. the regcache descr. */
  122. struct type *
  123. register_type (struct gdbarch *gdbarch, int regnum)
  124. {
  125. struct regcache_descr *descr = regcache_descr (gdbarch);
  126. gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
  127. return descr->register_type[regnum];
  128. }
  129. /* Utility functions returning useful register attributes stored in
  130. the regcache descr. */
  131. int
  132. register_size (struct gdbarch *gdbarch, int regnum)
  133. {
  134. struct regcache_descr *descr = regcache_descr (gdbarch);
  135. int size;
  136. gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch));
  137. size = descr->sizeof_register[regnum];
  138. return size;
  139. }
  140. /* See gdbsupport/common-regcache.h. */
  141. int
  142. regcache_register_size (const struct regcache *regcache, int n)
  143. {
  144. return register_size (regcache->arch (), n);
  145. }
  146. reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
  147. : m_has_pseudo (has_pseudo)
  148. {
  149. gdb_assert (gdbarch != NULL);
  150. m_descr = regcache_descr (gdbarch);
  151. /* We don't zero-initialize the M_REGISTERS array, as the bytes it contains
  152. aren't meaningful as long as the corresponding register status is not
  153. REG_VALID. */
  154. if (has_pseudo)
  155. {
  156. m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers]);
  157. m_register_status.reset
  158. (new register_status[m_descr->nr_cooked_registers] ());
  159. }
  160. else
  161. {
  162. m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers]);
  163. m_register_status.reset
  164. (new register_status[gdbarch_num_regs (gdbarch)] ());
  165. }
  166. }
  167. regcache::regcache (process_stratum_target *target, gdbarch *gdbarch,
  168. const address_space *aspace_)
  169. /* The register buffers. A read/write register cache can only hold
  170. [0 .. gdbarch_num_regs). */
  171. : detached_regcache (gdbarch, false), m_aspace (aspace_), m_target (target)
  172. {
  173. m_ptid = minus_one_ptid;
  174. }
  175. readonly_detached_regcache::readonly_detached_regcache (regcache &src)
  176. : readonly_detached_regcache (src.arch (),
  177. [&src] (int regnum, gdb_byte *buf)
  178. {
  179. return src.cooked_read (regnum, buf);
  180. })
  181. {
  182. }
  183. gdbarch *
  184. reg_buffer::arch () const
  185. {
  186. return m_descr->gdbarch;
  187. }
  188. /* Return a pointer to register REGNUM's buffer cache. */
  189. gdb_byte *
  190. reg_buffer::register_buffer (int regnum) const
  191. {
  192. return m_registers.get () + m_descr->register_offset[regnum];
  193. }
  194. void
  195. reg_buffer::save (register_read_ftype cooked_read)
  196. {
  197. struct gdbarch *gdbarch = m_descr->gdbarch;
  198. int regnum;
  199. /* It should have pseudo registers. */
  200. gdb_assert (m_has_pseudo);
  201. /* Clear the dest. */
  202. memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
  203. memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
  204. /* Copy over any registers (identified by their membership in the
  205. save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
  206. gdbarch_num_pseudo_regs) range is checked since some architectures need
  207. to save/restore `cooked' registers that live in memory. */
  208. for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
  209. {
  210. if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
  211. {
  212. gdb_byte *dst_buf = register_buffer (regnum);
  213. enum register_status status = cooked_read (regnum, dst_buf);
  214. gdb_assert (status != REG_UNKNOWN);
  215. if (status != REG_VALID)
  216. memset (dst_buf, 0, register_size (gdbarch, regnum));
  217. m_register_status[regnum] = status;
  218. }
  219. }
  220. }
  221. void
  222. regcache::restore (readonly_detached_regcache *src)
  223. {
  224. struct gdbarch *gdbarch = m_descr->gdbarch;
  225. int regnum;
  226. gdb_assert (src != NULL);
  227. gdb_assert (src->m_has_pseudo);
  228. gdb_assert (gdbarch == src->arch ());
  229. /* Copy over any registers, being careful to only restore those that
  230. were both saved and need to be restored. The full [0 .. gdbarch_num_regs
  231. + gdbarch_num_pseudo_regs) range is checked since some architectures need
  232. to save/restore `cooked' registers that live in memory. */
  233. for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
  234. {
  235. if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
  236. {
  237. if (src->m_register_status[regnum] == REG_VALID)
  238. cooked_write (regnum, src->register_buffer (regnum));
  239. }
  240. }
  241. }
  242. /* See gdbsupport/common-regcache.h. */
  243. enum register_status
  244. reg_buffer::get_register_status (int regnum) const
  245. {
  246. assert_regnum (regnum);
  247. return m_register_status[regnum];
  248. }
  249. void
  250. reg_buffer::invalidate (int regnum)
  251. {
  252. assert_regnum (regnum);
  253. m_register_status[regnum] = REG_UNKNOWN;
  254. }
  255. void
  256. reg_buffer::assert_regnum (int regnum) const
  257. {
  258. gdb_assert (regnum >= 0);
  259. if (m_has_pseudo)
  260. gdb_assert (regnum < m_descr->nr_cooked_registers);
  261. else
  262. gdb_assert (regnum < gdbarch_num_regs (arch ()));
  263. }
  264. /* Type to map a ptid to a list of regcaches (one thread may have multiple
  265. regcaches, associated to different gdbarches). */
  266. using ptid_regcache_map
  267. = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
  268. /* Type holding regcaches for a given pid. */
  269. using pid_ptid_regcache_map = std::unordered_map<int, ptid_regcache_map>;
  270. /* Type holding regcaches for a given target. */
  271. using target_pid_ptid_regcache_map
  272. = std::unordered_map<process_stratum_target *, pid_ptid_regcache_map>;
  273. /* Global structure containing the existing regcaches. */
  274. /* NOTE: this is a write-through cache. There is no "dirty" bit for
  275. recording if the register values have been changed (eg. by the
  276. user). Therefore all registers must be written back to the
  277. target when appropriate. */
  278. static target_pid_ptid_regcache_map regcaches;
  279. struct regcache *
  280. get_thread_arch_aspace_regcache (process_stratum_target *target,
  281. ptid_t ptid, gdbarch *arch,
  282. struct address_space *aspace)
  283. {
  284. gdb_assert (target != nullptr);
  285. /* Find the map for this target. */
  286. pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[target];
  287. /* Find the map for this pid. */
  288. ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()];
  289. /* Check first if a regcache for this arch already exists. */
  290. auto range = ptid_regc_map.equal_range (ptid);
  291. for (auto it = range.first; it != range.second; ++it)
  292. {
  293. if (it->second->arch () == arch)
  294. return it->second.get ();
  295. }
  296. /* It does not exist, create it. */
  297. regcache *new_regcache = new regcache (target, arch, aspace);
  298. new_regcache->set_ptid (ptid);
  299. /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
  300. constructor explictly instead of implicitly. */
  301. ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache)));
  302. return new_regcache;
  303. }
  304. struct regcache *
  305. get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
  306. struct gdbarch *gdbarch)
  307. {
  308. scoped_restore_current_inferior restore_current_inferior;
  309. set_current_inferior (find_inferior_ptid (target, ptid));
  310. address_space *aspace = target_thread_address_space (ptid);
  311. return get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
  312. }
  313. static process_stratum_target *current_thread_target;
  314. static ptid_t current_thread_ptid;
  315. static struct gdbarch *current_thread_arch;
  316. struct regcache *
  317. get_thread_regcache (process_stratum_target *target, ptid_t ptid)
  318. {
  319. if (!current_thread_arch
  320. || target != current_thread_target
  321. || current_thread_ptid != ptid)
  322. {
  323. gdb_assert (ptid != null_ptid);
  324. current_thread_ptid = ptid;
  325. current_thread_target = target;
  326. scoped_restore_current_inferior restore_current_inferior;
  327. set_current_inferior (find_inferior_ptid (target, ptid));
  328. current_thread_arch = target_thread_architecture (ptid);
  329. }
  330. return get_thread_arch_regcache (target, ptid, current_thread_arch);
  331. }
  332. /* See regcache.h. */
  333. struct regcache *
  334. get_thread_regcache (thread_info *thread)
  335. {
  336. return get_thread_regcache (thread->inf->process_target (),
  337. thread->ptid);
  338. }
  339. struct regcache *
  340. get_current_regcache (void)
  341. {
  342. return get_thread_regcache (inferior_thread ());
  343. }
  344. /* See gdbsupport/common-regcache.h. */
  345. struct regcache *
  346. get_thread_regcache_for_ptid (ptid_t ptid)
  347. {
  348. /* This function doesn't take a process_stratum_target parameter
  349. because it's a gdbsupport/ routine implemented by both gdb and
  350. gdbserver. It always refers to a ptid of the current target. */
  351. process_stratum_target *proc_target = current_inferior ()->process_target ();
  352. return get_thread_regcache (proc_target, ptid);
  353. }
  354. /* Observer for the target_changed event. */
  355. static void
  356. regcache_observer_target_changed (struct target_ops *target)
  357. {
  358. registers_changed ();
  359. }
  360. /* Update regcaches related to OLD_PTID to now use NEW_PTID. */
  361. static void
  362. regcache_thread_ptid_changed (process_stratum_target *target,
  363. ptid_t old_ptid, ptid_t new_ptid)
  364. {
  365. /* Look up map for target. */
  366. auto pid_ptid_regc_map_it = regcaches.find (target);
  367. if (pid_ptid_regc_map_it == regcaches.end ())
  368. return;
  369. /* Look up map for pid. */
  370. pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
  371. auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ());
  372. if (ptid_regc_map_it == pid_ptid_regc_map.end ())
  373. return;
  374. /* Update all regcaches belonging to old_ptid. */
  375. ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
  376. auto range = ptid_regc_map.equal_range (old_ptid);
  377. for (auto it = range.first; it != range.second;)
  378. {
  379. regcache_up rc = std::move (it->second);
  380. rc->set_ptid (new_ptid);
  381. /* Remove old before inserting new, to avoid rehashing,
  382. which would invalidate iterators. */
  383. it = ptid_regc_map.erase (it);
  384. ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc)));
  385. }
  386. }
  387. /* Low level examining and depositing of registers.
  388. The caller is responsible for making sure that the inferior is
  389. stopped before calling the fetching routines, or it will get
  390. garbage. (a change from GDB version 3, in which the caller got the
  391. value from the last stop). */
  392. /* REGISTERS_CHANGED ()
  393. Indicate that registers may have changed, so invalidate the cache. */
  394. void
  395. registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
  396. {
  397. if (target == nullptr)
  398. {
  399. /* Since there can be ptid clashes between targets, it's not valid to
  400. pass a ptid without saying to which target it belongs. */
  401. gdb_assert (ptid == minus_one_ptid);
  402. /* Delete all the regcaches of all targets. */
  403. regcaches.clear ();
  404. }
  405. else if (ptid.is_pid ())
  406. {
  407. /* Non-NULL target and pid ptid, delete all regcaches belonging
  408. to this (TARGET, PID). */
  409. /* Look up map for target. */
  410. auto pid_ptid_regc_map_it = regcaches.find (target);
  411. if (pid_ptid_regc_map_it != regcaches.end ())
  412. {
  413. pid_ptid_regcache_map &pid_ptid_regc_map
  414. = pid_ptid_regc_map_it->second;
  415. pid_ptid_regc_map.erase (ptid.pid ());
  416. }
  417. }
  418. else if (ptid != minus_one_ptid)
  419. {
  420. /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
  421. to this (TARGET, PTID). */
  422. /* Look up map for target. */
  423. auto pid_ptid_regc_map_it = regcaches.find (target);
  424. if (pid_ptid_regc_map_it != regcaches.end ())
  425. {
  426. pid_ptid_regcache_map &pid_ptid_regc_map
  427. = pid_ptid_regc_map_it->second;
  428. /* Look up map for pid. */
  429. auto ptid_regc_map_it
  430. = pid_ptid_regc_map.find (ptid.pid ());
  431. if (ptid_regc_map_it != pid_ptid_regc_map.end ())
  432. {
  433. ptid_regcache_map &ptid_regc_map
  434. = ptid_regc_map_it->second;
  435. ptid_regc_map.erase (ptid);
  436. }
  437. }
  438. }
  439. else
  440. {
  441. /* Non-NULL target and minus_one_ptid, delete all regcaches
  442. associated to this target. */
  443. regcaches.erase (target);
  444. }
  445. if ((target == nullptr || current_thread_target == target)
  446. && current_thread_ptid.matches (ptid))
  447. {
  448. current_thread_target = NULL;
  449. current_thread_ptid = null_ptid;
  450. current_thread_arch = NULL;
  451. }
  452. if ((target == nullptr || current_inferior ()->process_target () == target)
  453. && inferior_ptid.matches (ptid))
  454. {
  455. /* We just deleted the regcache of the current thread. Need to
  456. forget about any frames we have cached, too. */
  457. reinit_frame_cache ();
  458. }
  459. }
  460. /* See regcache.h. */
  461. void
  462. registers_changed_thread (thread_info *thread)
  463. {
  464. registers_changed_ptid (thread->inf->process_target (), thread->ptid);
  465. }
  466. void
  467. registers_changed (void)
  468. {
  469. registers_changed_ptid (nullptr, minus_one_ptid);
  470. }
  471. void
  472. regcache::raw_update (int regnum)
  473. {
  474. assert_regnum (regnum);
  475. /* Make certain that the register cache is up-to-date with respect
  476. to the current thread. This switching shouldn't be necessary
  477. only there is still only one target side register cache. Sigh!
  478. On the bright side, at least there is a regcache object. */
  479. if (get_register_status (regnum) == REG_UNKNOWN)
  480. {
  481. target_fetch_registers (this, regnum);
  482. /* A number of targets can't access the whole set of raw
  483. registers (because the debug API provides no means to get at
  484. them). */
  485. if (m_register_status[regnum] == REG_UNKNOWN)
  486. m_register_status[regnum] = REG_UNAVAILABLE;
  487. }
  488. }
  489. enum register_status
  490. readable_regcache::raw_read (int regnum, gdb_byte *buf)
  491. {
  492. gdb_assert (buf != NULL);
  493. raw_update (regnum);
  494. if (m_register_status[regnum] != REG_VALID)
  495. memset (buf, 0, m_descr->sizeof_register[regnum]);
  496. else
  497. memcpy (buf, register_buffer (regnum),
  498. m_descr->sizeof_register[regnum]);
  499. return m_register_status[regnum];
  500. }
  501. enum register_status
  502. regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
  503. {
  504. gdb_assert (regcache != NULL);
  505. return regcache->raw_read (regnum, val);
  506. }
  507. template<typename T, typename>
  508. enum register_status
  509. readable_regcache::raw_read (int regnum, T *val)
  510. {
  511. assert_regnum (regnum);
  512. size_t len = m_descr->sizeof_register[regnum];
  513. gdb_byte *buf = (gdb_byte *) alloca (len);
  514. register_status status = raw_read (regnum, buf);
  515. if (status == REG_VALID)
  516. *val = extract_integer<T> ({buf, len},
  517. gdbarch_byte_order (m_descr->gdbarch));
  518. else
  519. *val = 0;
  520. return status;
  521. }
  522. enum register_status
  523. regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
  524. ULONGEST *val)
  525. {
  526. gdb_assert (regcache != NULL);
  527. return regcache->raw_read (regnum, val);
  528. }
  529. void
  530. regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
  531. {
  532. gdb_assert (regcache != NULL);
  533. regcache->raw_write (regnum, val);
  534. }
  535. template<typename T, typename>
  536. void
  537. regcache::raw_write (int regnum, T val)
  538. {
  539. gdb_byte *buf;
  540. assert_regnum (regnum);
  541. buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
  542. store_integer (buf, m_descr->sizeof_register[regnum],
  543. gdbarch_byte_order (m_descr->gdbarch), val);
  544. raw_write (regnum, buf);
  545. }
  546. void
  547. regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
  548. ULONGEST val)
  549. {
  550. gdb_assert (regcache != NULL);
  551. regcache->raw_write (regnum, val);
  552. }
  553. LONGEST
  554. regcache_raw_get_signed (struct regcache *regcache, int regnum)
  555. {
  556. LONGEST value;
  557. enum register_status status;
  558. status = regcache_raw_read_signed (regcache, regnum, &value);
  559. if (status == REG_UNAVAILABLE)
  560. throw_error (NOT_AVAILABLE_ERROR,
  561. _("Register %d is not available"), regnum);
  562. return value;
  563. }
  564. enum register_status
  565. readable_regcache::cooked_read (int regnum, gdb_byte *buf)
  566. {
  567. gdb_assert (regnum >= 0);
  568. gdb_assert (regnum < m_descr->nr_cooked_registers);
  569. if (regnum < num_raw_registers ())
  570. return raw_read (regnum, buf);
  571. else if (m_has_pseudo
  572. && m_register_status[regnum] != REG_UNKNOWN)
  573. {
  574. if (m_register_status[regnum] == REG_VALID)
  575. memcpy (buf, register_buffer (regnum),
  576. m_descr->sizeof_register[regnum]);
  577. else
  578. memset (buf, 0, m_descr->sizeof_register[regnum]);
  579. return m_register_status[regnum];
  580. }
  581. else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
  582. {
  583. struct value *mark, *computed;
  584. enum register_status result = REG_VALID;
  585. mark = value_mark ();
  586. computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
  587. this, regnum);
  588. if (value_entirely_available (computed))
  589. memcpy (buf, value_contents_raw (computed).data (),
  590. m_descr->sizeof_register[regnum]);
  591. else
  592. {
  593. memset (buf, 0, m_descr->sizeof_register[regnum]);
  594. result = REG_UNAVAILABLE;
  595. }
  596. value_free_to_mark (mark);
  597. return result;
  598. }
  599. else
  600. return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
  601. regnum, buf);
  602. }
  603. struct value *
  604. readable_regcache::cooked_read_value (int regnum)
  605. {
  606. gdb_assert (regnum >= 0);
  607. gdb_assert (regnum < m_descr->nr_cooked_registers);
  608. if (regnum < num_raw_registers ()
  609. || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
  610. || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
  611. {
  612. struct value *result;
  613. result = allocate_value (register_type (m_descr->gdbarch, regnum));
  614. VALUE_LVAL (result) = lval_register;
  615. VALUE_REGNUM (result) = regnum;
  616. /* It is more efficient in general to do this delegation in this
  617. direction than in the other one, even though the value-based
  618. API is preferred. */
  619. if (cooked_read (regnum,
  620. value_contents_raw (result).data ()) == REG_UNAVAILABLE)
  621. mark_value_bytes_unavailable (result, 0,
  622. TYPE_LENGTH (value_type (result)));
  623. return result;
  624. }
  625. else
  626. return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
  627. this, regnum);
  628. }
  629. enum register_status
  630. regcache_cooked_read_signed (struct regcache *regcache, int regnum,
  631. LONGEST *val)
  632. {
  633. gdb_assert (regcache != NULL);
  634. return regcache->cooked_read (regnum, val);
  635. }
  636. template<typename T, typename>
  637. enum register_status
  638. readable_regcache::cooked_read (int regnum, T *val)
  639. {
  640. gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
  641. size_t len = m_descr->sizeof_register[regnum];
  642. gdb_byte *buf = (gdb_byte *) alloca (len);
  643. register_status status = cooked_read (regnum, buf);
  644. if (status == REG_VALID)
  645. *val = extract_integer<T> ({buf, len},
  646. gdbarch_byte_order (m_descr->gdbarch));
  647. else
  648. *val = 0;
  649. return status;
  650. }
  651. enum register_status
  652. regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
  653. ULONGEST *val)
  654. {
  655. gdb_assert (regcache != NULL);
  656. return regcache->cooked_read (regnum, val);
  657. }
  658. void
  659. regcache_cooked_write_signed (struct regcache *regcache, int regnum,
  660. LONGEST val)
  661. {
  662. gdb_assert (regcache != NULL);
  663. regcache->cooked_write (regnum, val);
  664. }
  665. template<typename T, typename>
  666. void
  667. regcache::cooked_write (int regnum, T val)
  668. {
  669. gdb_byte *buf;
  670. gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
  671. buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
  672. store_integer (buf, m_descr->sizeof_register[regnum],
  673. gdbarch_byte_order (m_descr->gdbarch), val);
  674. cooked_write (regnum, buf);
  675. }
  676. void
  677. regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
  678. ULONGEST val)
  679. {
  680. gdb_assert (regcache != NULL);
  681. regcache->cooked_write (regnum, val);
  682. }
  683. void
  684. regcache::raw_write (int regnum, const gdb_byte *buf)
  685. {
  686. gdb_assert (buf != NULL);
  687. assert_regnum (regnum);
  688. /* On the sparc, writing %g0 is a no-op, so we don't even want to
  689. change the registers array if something writes to this register. */
  690. if (gdbarch_cannot_store_register (arch (), regnum))
  691. return;
  692. /* If we have a valid copy of the register, and new value == old
  693. value, then don't bother doing the actual store. */
  694. if (get_register_status (regnum) == REG_VALID
  695. && (memcmp (register_buffer (regnum), buf,
  696. m_descr->sizeof_register[regnum]) == 0))
  697. return;
  698. target_prepare_to_store (this);
  699. raw_supply (regnum, buf);
  700. /* Invalidate the register after it is written, in case of a
  701. failure. */
  702. auto invalidator
  703. = make_scope_exit ([&] { this->invalidate (regnum); });
  704. target_store_registers (this, regnum);
  705. /* The target did not throw an error so we can discard invalidating
  706. the register. */
  707. invalidator.release ();
  708. }
  709. void
  710. regcache::cooked_write (int regnum, const gdb_byte *buf)
  711. {
  712. gdb_assert (regnum >= 0);
  713. gdb_assert (regnum < m_descr->nr_cooked_registers);
  714. if (regnum < num_raw_registers ())
  715. raw_write (regnum, buf);
  716. else
  717. gdbarch_pseudo_register_write (m_descr->gdbarch, this,
  718. regnum, buf);
  719. }
  720. /* See regcache.h. */
  721. enum register_status
  722. readable_regcache::read_part (int regnum, int offset, int len,
  723. gdb_byte *out, bool is_raw)
  724. {
  725. int reg_size = register_size (arch (), regnum);
  726. gdb_assert (out != NULL);
  727. gdb_assert (offset >= 0 && offset <= reg_size);
  728. gdb_assert (len >= 0 && offset + len <= reg_size);
  729. if (offset == 0 && len == 0)
  730. {
  731. /* Nothing to do. */
  732. return REG_VALID;
  733. }
  734. if (offset == 0 && len == reg_size)
  735. {
  736. /* Read the full register. */
  737. return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
  738. }
  739. enum register_status status;
  740. gdb_byte *reg = (gdb_byte *) alloca (reg_size);
  741. /* Read full register to buffer. */
  742. status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
  743. if (status != REG_VALID)
  744. return status;
  745. /* Copy out. */
  746. memcpy (out, reg + offset, len);
  747. return REG_VALID;
  748. }
  749. /* See regcache.h. */
  750. void
  751. reg_buffer::raw_collect_part (int regnum, int offset, int len,
  752. gdb_byte *out) const
  753. {
  754. int reg_size = register_size (arch (), regnum);
  755. gdb_assert (out != nullptr);
  756. gdb_assert (offset >= 0 && offset <= reg_size);
  757. gdb_assert (len >= 0 && offset + len <= reg_size);
  758. if (offset == 0 && len == 0)
  759. {
  760. /* Nothing to do. */
  761. return;
  762. }
  763. if (offset == 0 && len == reg_size)
  764. {
  765. /* Collect the full register. */
  766. return raw_collect (regnum, out);
  767. }
  768. /* Read to buffer, then write out. */
  769. gdb_byte *reg = (gdb_byte *) alloca (reg_size);
  770. raw_collect (regnum, reg);
  771. memcpy (out, reg + offset, len);
  772. }
  773. /* See regcache.h. */
  774. enum register_status
  775. regcache::write_part (int regnum, int offset, int len,
  776. const gdb_byte *in, bool is_raw)
  777. {
  778. int reg_size = register_size (arch (), regnum);
  779. gdb_assert (in != NULL);
  780. gdb_assert (offset >= 0 && offset <= reg_size);
  781. gdb_assert (len >= 0 && offset + len <= reg_size);
  782. if (offset == 0 && len == 0)
  783. {
  784. /* Nothing to do. */
  785. return REG_VALID;
  786. }
  787. if (offset == 0 && len == reg_size)
  788. {
  789. /* Write the full register. */
  790. (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
  791. return REG_VALID;
  792. }
  793. enum register_status status;
  794. gdb_byte *reg = (gdb_byte *) alloca (reg_size);
  795. /* Read existing register to buffer. */
  796. status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
  797. if (status != REG_VALID)
  798. return status;
  799. /* Update buffer, then write back to regcache. */
  800. memcpy (reg + offset, in, len);
  801. is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
  802. return REG_VALID;
  803. }
  804. /* See regcache.h. */
  805. void
  806. reg_buffer::raw_supply_part (int regnum, int offset, int len,
  807. const gdb_byte *in)
  808. {
  809. int reg_size = register_size (arch (), regnum);
  810. gdb_assert (in != nullptr);
  811. gdb_assert (offset >= 0 && offset <= reg_size);
  812. gdb_assert (len >= 0 && offset + len <= reg_size);
  813. if (offset == 0 && len == 0)
  814. {
  815. /* Nothing to do. */
  816. return;
  817. }
  818. if (offset == 0 && len == reg_size)
  819. {
  820. /* Supply the full register. */
  821. return raw_supply (regnum, in);
  822. }
  823. gdb_byte *reg = (gdb_byte *) alloca (reg_size);
  824. /* Read existing value to buffer. */
  825. raw_collect (regnum, reg);
  826. /* Write to buffer, then write out. */
  827. memcpy (reg + offset, in, len);
  828. raw_supply (regnum, reg);
  829. }
  830. enum register_status
  831. readable_regcache::raw_read_part (int regnum, int offset, int len,
  832. gdb_byte *buf)
  833. {
  834. assert_regnum (regnum);
  835. return read_part (regnum, offset, len, buf, true);
  836. }
  837. /* See regcache.h. */
  838. void
  839. regcache::raw_write_part (int regnum, int offset, int len,
  840. const gdb_byte *buf)
  841. {
  842. assert_regnum (regnum);
  843. write_part (regnum, offset, len, buf, true);
  844. }
  845. /* See regcache.h. */
  846. enum register_status
  847. readable_regcache::cooked_read_part (int regnum, int offset, int len,
  848. gdb_byte *buf)
  849. {
  850. gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
  851. return read_part (regnum, offset, len, buf, false);
  852. }
  853. /* See regcache.h. */
  854. void
  855. regcache::cooked_write_part (int regnum, int offset, int len,
  856. const gdb_byte *buf)
  857. {
  858. gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
  859. write_part (regnum, offset, len, buf, false);
  860. }
  861. /* See gdbsupport/common-regcache.h. */
  862. void
  863. reg_buffer::raw_supply (int regnum, const void *buf)
  864. {
  865. void *regbuf;
  866. size_t size;
  867. assert_regnum (regnum);
  868. regbuf = register_buffer (regnum);
  869. size = m_descr->sizeof_register[regnum];
  870. if (buf)
  871. {
  872. memcpy (regbuf, buf, size);
  873. m_register_status[regnum] = REG_VALID;
  874. }
  875. else
  876. {
  877. /* This memset not strictly necessary, but better than garbage
  878. in case the register value manages to escape somewhere (due
  879. to a bug, no less). */
  880. memset (regbuf, 0, size);
  881. m_register_status[regnum] = REG_UNAVAILABLE;
  882. }
  883. }
  884. /* See regcache.h. */
  885. void
  886. reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
  887. int addr_len, bool is_signed)
  888. {
  889. enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
  890. gdb_byte *regbuf;
  891. size_t regsize;
  892. assert_regnum (regnum);
  893. regbuf = register_buffer (regnum);
  894. regsize = m_descr->sizeof_register[regnum];
  895. copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
  896. byte_order);
  897. m_register_status[regnum] = REG_VALID;
  898. }
  899. /* See regcache.h. */
  900. void
  901. reg_buffer::raw_supply_zeroed (int regnum)
  902. {
  903. void *regbuf;
  904. size_t size;
  905. assert_regnum (regnum);
  906. regbuf = register_buffer (regnum);
  907. size = m_descr->sizeof_register[regnum];
  908. memset (regbuf, 0, size);
  909. m_register_status[regnum] = REG_VALID;
  910. }
  911. /* See gdbsupport/common-regcache.h. */
  912. void
  913. reg_buffer::raw_collect (int regnum, void *buf) const
  914. {
  915. const void *regbuf;
  916. size_t size;
  917. gdb_assert (buf != NULL);
  918. assert_regnum (regnum);
  919. regbuf = register_buffer (regnum);
  920. size = m_descr->sizeof_register[regnum];
  921. memcpy (buf, regbuf, size);
  922. }
  923. /* See regcache.h. */
  924. void
  925. reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
  926. bool is_signed) const
  927. {
  928. enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
  929. const gdb_byte *regbuf;
  930. size_t regsize;
  931. assert_regnum (regnum);
  932. regbuf = register_buffer (regnum);
  933. regsize = m_descr->sizeof_register[regnum];
  934. copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
  935. byte_order);
  936. }
  937. /* See regcache.h. */
  938. void
  939. regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
  940. const gdb_byte *in_buf, gdb_byte *out_buf,
  941. int slot_size, int offs) const
  942. {
  943. struct gdbarch *gdbarch = arch ();
  944. int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
  945. /* Use part versions and reg_size to prevent possible buffer overflows when
  946. accessing the regcache. */
  947. if (out_buf != nullptr)
  948. {
  949. raw_collect_part (regnum, 0, reg_size, out_buf + offs);
  950. /* Ensure any additional space is cleared. */
  951. if (slot_size > reg_size)
  952. memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
  953. }
  954. else if (in_buf != nullptr)
  955. {
  956. /* Zero-extend the register value if the slot is smaller than the register. */
  957. if (slot_size < register_size (gdbarch, regnum))
  958. out_regcache->raw_supply_zeroed (regnum);
  959. out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
  960. }
  961. else
  962. {
  963. /* Invalidate the register. */
  964. out_regcache->raw_supply (regnum, nullptr);
  965. }
  966. }
  967. /* See regcache.h. */
  968. void
  969. regcache::transfer_regset (const struct regset *regset,
  970. struct regcache *out_regcache,
  971. int regnum, const gdb_byte *in_buf,
  972. gdb_byte *out_buf, size_t size) const
  973. {
  974. const struct regcache_map_entry *map;
  975. int offs = 0, count;
  976. for (map = (const struct regcache_map_entry *) regset->regmap;
  977. (count = map->count) != 0;
  978. map++)
  979. {
  980. int regno = map->regno;
  981. int slot_size = map->size;
  982. if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
  983. slot_size = m_descr->sizeof_register[regno];
  984. if (regno == REGCACHE_MAP_SKIP
  985. || (regnum != -1
  986. && (regnum < regno || regnum >= regno + count)))
  987. offs += count * slot_size;
  988. else if (regnum == -1)
  989. for (; count--; regno++, offs += slot_size)
  990. {
  991. if (offs + slot_size > size)
  992. break;
  993. transfer_regset_register (out_regcache, regno, in_buf, out_buf,
  994. slot_size, offs);
  995. }
  996. else
  997. {
  998. /* Transfer a single register and return. */
  999. offs += (regnum - regno) * slot_size;
  1000. if (offs + slot_size > size)
  1001. return;
  1002. transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
  1003. slot_size, offs);
  1004. return;
  1005. }
  1006. }
  1007. }
  1008. /* Supply register REGNUM from BUF to REGCACHE, using the register map
  1009. in REGSET. If REGNUM is -1, do this for all registers in REGSET.
  1010. If BUF is NULL, set the register(s) to "unavailable" status. */
  1011. void
  1012. regcache_supply_regset (const struct regset *regset,
  1013. struct regcache *regcache,
  1014. int regnum, const void *buf, size_t size)
  1015. {
  1016. regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
  1017. }
  1018. void
  1019. regcache::supply_regset (const struct regset *regset,
  1020. int regnum, const void *buf, size_t size)
  1021. {
  1022. transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
  1023. }
  1024. /* Collect register REGNUM from REGCACHE to BUF, using the register
  1025. map in REGSET. If REGNUM is -1, do this for all registers in
  1026. REGSET. */
  1027. void
  1028. regcache_collect_regset (const struct regset *regset,
  1029. const struct regcache *regcache,
  1030. int regnum, void *buf, size_t size)
  1031. {
  1032. regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
  1033. }
  1034. void
  1035. regcache::collect_regset (const struct regset *regset,
  1036. int regnum, void *buf, size_t size) const
  1037. {
  1038. transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
  1039. }
  1040. /* See regcache.h */
  1041. bool
  1042. regcache_map_supplies (const struct regcache_map_entry *map, int regnum,
  1043. struct gdbarch *gdbarch, size_t size)
  1044. {
  1045. int offs = 0, count;
  1046. for (; (count = map->count) != 0; map++)
  1047. {
  1048. int regno = map->regno;
  1049. int slot_size = map->size;
  1050. if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
  1051. slot_size = register_size (gdbarch, regno);
  1052. if (regno != REGCACHE_MAP_SKIP && regnum >= regno
  1053. && regnum < regno + count)
  1054. return offs + (regnum - regno + 1) * slot_size <= size;
  1055. offs += count * slot_size;
  1056. if (offs >= size)
  1057. return false;
  1058. }
  1059. return false;
  1060. }
  1061. /* See gdbsupport/common-regcache.h. */
  1062. bool
  1063. reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
  1064. {
  1065. gdb_assert (buf != NULL);
  1066. assert_regnum (regnum);
  1067. const char *regbuf = (const char *) register_buffer (regnum);
  1068. size_t size = m_descr->sizeof_register[regnum];
  1069. gdb_assert (size >= offset);
  1070. return (memcmp (buf, regbuf + offset, size - offset) == 0);
  1071. }
  1072. /* Special handling for register PC. */
  1073. CORE_ADDR
  1074. regcache_read_pc (struct regcache *regcache)
  1075. {
  1076. struct gdbarch *gdbarch = regcache->arch ();
  1077. CORE_ADDR pc_val;
  1078. if (gdbarch_read_pc_p (gdbarch))
  1079. pc_val = gdbarch_read_pc (gdbarch, regcache);
  1080. /* Else use per-frame method on get_current_frame. */
  1081. else if (gdbarch_pc_regnum (gdbarch) >= 0)
  1082. {
  1083. ULONGEST raw_val;
  1084. if (regcache_cooked_read_unsigned (regcache,
  1085. gdbarch_pc_regnum (gdbarch),
  1086. &raw_val) == REG_UNAVAILABLE)
  1087. throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
  1088. pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
  1089. }
  1090. else
  1091. internal_error (__FILE__, __LINE__,
  1092. _("regcache_read_pc: Unable to find PC"));
  1093. return pc_val;
  1094. }
  1095. /* See gdbsupport/common-regcache.h. */
  1096. CORE_ADDR
  1097. regcache_read_pc_protected (regcache *regcache)
  1098. {
  1099. CORE_ADDR pc;
  1100. try
  1101. {
  1102. pc = regcache_read_pc (regcache);
  1103. }
  1104. catch (const gdb_exception_error &ex)
  1105. {
  1106. pc = 0;
  1107. }
  1108. return pc;
  1109. }
  1110. void
  1111. regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
  1112. {
  1113. struct gdbarch *gdbarch = regcache->arch ();
  1114. if (gdbarch_write_pc_p (gdbarch))
  1115. gdbarch_write_pc (gdbarch, regcache, pc);
  1116. else if (gdbarch_pc_regnum (gdbarch) >= 0)
  1117. regcache_cooked_write_unsigned (regcache,
  1118. gdbarch_pc_regnum (gdbarch), pc);
  1119. else
  1120. internal_error (__FILE__, __LINE__,
  1121. _("regcache_write_pc: Unable to update PC"));
  1122. /* Writing the PC (for instance, from "load") invalidates the
  1123. current frame. */
  1124. reinit_frame_cache ();
  1125. }
  1126. int
  1127. reg_buffer::num_raw_registers () const
  1128. {
  1129. return gdbarch_num_regs (arch ());
  1130. }
  1131. void
  1132. regcache::debug_print_register (const char *func, int regno)
  1133. {
  1134. struct gdbarch *gdbarch = arch ();
  1135. gdb_printf (gdb_stdlog, "%s ", func);
  1136. if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
  1137. && gdbarch_register_name (gdbarch, regno) != NULL
  1138. && gdbarch_register_name (gdbarch, regno)[0] != '\0')
  1139. gdb_printf (gdb_stdlog, "(%s)",
  1140. gdbarch_register_name (gdbarch, regno));
  1141. else
  1142. gdb_printf (gdb_stdlog, "(%d)", regno);
  1143. if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
  1144. {
  1145. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1146. int size = register_size (gdbarch, regno);
  1147. gdb_byte *buf = register_buffer (regno);
  1148. gdb_printf (gdb_stdlog, " = ");
  1149. for (int i = 0; i < size; i++)
  1150. {
  1151. gdb_printf (gdb_stdlog, "%02x", buf[i]);
  1152. }
  1153. if (size <= sizeof (LONGEST))
  1154. {
  1155. ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
  1156. gdb_printf (gdb_stdlog, " %s %s",
  1157. core_addr_to_string_nz (val), plongest (val));
  1158. }
  1159. }
  1160. gdb_printf (gdb_stdlog, "\n");
  1161. }
  1162. /* Implement 'maint flush register-cache' command. */
  1163. static void
  1164. reg_flush_command (const char *command, int from_tty)
  1165. {
  1166. /* Force-flush the register cache. */
  1167. registers_changed ();
  1168. if (from_tty)
  1169. gdb_printf (_("Register cache flushed.\n"));
  1170. }
  1171. void
  1172. register_dump::dump (ui_file *file)
  1173. {
  1174. auto descr = regcache_descr (m_gdbarch);
  1175. int regnum;
  1176. int footnote_nr = 0;
  1177. int footnote_register_offset = 0;
  1178. int footnote_register_type_name_null = 0;
  1179. long register_offset = 0;
  1180. gdb_assert (descr->nr_cooked_registers
  1181. == gdbarch_num_cooked_regs (m_gdbarch));
  1182. for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
  1183. {
  1184. /* Name. */
  1185. if (regnum < 0)
  1186. gdb_printf (file, " %-10s", "Name");
  1187. else
  1188. {
  1189. const char *p = gdbarch_register_name (m_gdbarch, regnum);
  1190. if (p == NULL)
  1191. p = "";
  1192. else if (p[0] == '\0')
  1193. p = "''";
  1194. gdb_printf (file, " %-10s", p);
  1195. }
  1196. /* Number. */
  1197. if (regnum < 0)
  1198. gdb_printf (file, " %4s", "Nr");
  1199. else
  1200. gdb_printf (file, " %4d", regnum);
  1201. /* Relative number. */
  1202. if (regnum < 0)
  1203. gdb_printf (file, " %4s", "Rel");
  1204. else if (regnum < gdbarch_num_regs (m_gdbarch))
  1205. gdb_printf (file, " %4d", regnum);
  1206. else
  1207. gdb_printf (file, " %4d",
  1208. (regnum - gdbarch_num_regs (m_gdbarch)));
  1209. /* Offset. */
  1210. if (regnum < 0)
  1211. gdb_printf (file, " %6s ", "Offset");
  1212. else
  1213. {
  1214. gdb_printf (file, " %6ld",
  1215. descr->register_offset[regnum]);
  1216. if (register_offset != descr->register_offset[regnum]
  1217. || (regnum > 0
  1218. && (descr->register_offset[regnum]
  1219. != (descr->register_offset[regnum - 1]
  1220. + descr->sizeof_register[regnum - 1])))
  1221. )
  1222. {
  1223. if (!footnote_register_offset)
  1224. footnote_register_offset = ++footnote_nr;
  1225. gdb_printf (file, "*%d", footnote_register_offset);
  1226. }
  1227. else
  1228. gdb_printf (file, " ");
  1229. register_offset = (descr->register_offset[regnum]
  1230. + descr->sizeof_register[regnum]);
  1231. }
  1232. /* Size. */
  1233. if (regnum < 0)
  1234. gdb_printf (file, " %5s ", "Size");
  1235. else
  1236. gdb_printf (file, " %5ld", descr->sizeof_register[regnum]);
  1237. /* Type. */
  1238. {
  1239. const char *t;
  1240. std::string name_holder;
  1241. if (regnum < 0)
  1242. t = "Type";
  1243. else
  1244. {
  1245. static const char blt[] = "builtin_type";
  1246. t = register_type (m_gdbarch, regnum)->name ();
  1247. if (t == NULL)
  1248. {
  1249. if (!footnote_register_type_name_null)
  1250. footnote_register_type_name_null = ++footnote_nr;
  1251. name_holder = string_printf ("*%d",
  1252. footnote_register_type_name_null);
  1253. t = name_holder.c_str ();
  1254. }
  1255. /* Chop a leading builtin_type. */
  1256. if (startswith (t, blt))
  1257. t += strlen (blt);
  1258. }
  1259. gdb_printf (file, " %-15s", t);
  1260. }
  1261. /* Leading space always present. */
  1262. gdb_printf (file, " ");
  1263. dump_reg (file, regnum);
  1264. gdb_printf (file, "\n");
  1265. }
  1266. if (footnote_register_offset)
  1267. gdb_printf (file, "*%d: Inconsistent register offsets.\n",
  1268. footnote_register_offset);
  1269. if (footnote_register_type_name_null)
  1270. gdb_printf (file,
  1271. "*%d: Register type's name NULL.\n",
  1272. footnote_register_type_name_null);
  1273. }
  1274. #if GDB_SELF_TEST
  1275. #include "gdbsupport/selftest.h"
  1276. #include "selftest-arch.h"
  1277. #include "target-float.h"
  1278. namespace selftests {
  1279. static size_t
  1280. regcaches_size ()
  1281. {
  1282. size_t size = 0;
  1283. for (auto pid_ptid_regc_map_it = regcaches.cbegin ();
  1284. pid_ptid_regc_map_it != regcaches.cend ();
  1285. ++pid_ptid_regc_map_it)
  1286. {
  1287. const pid_ptid_regcache_map &pid_ptid_regc_map
  1288. = pid_ptid_regc_map_it->second;
  1289. for (auto ptid_regc_map_it = pid_ptid_regc_map.cbegin ();
  1290. ptid_regc_map_it != pid_ptid_regc_map.cend ();
  1291. ++ptid_regc_map_it)
  1292. {
  1293. const ptid_regcache_map &ptid_regc_map
  1294. = ptid_regc_map_it->second;
  1295. size += ptid_regc_map.size ();
  1296. }
  1297. }
  1298. return size;
  1299. }
  1300. /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
  1301. static int
  1302. regcache_count (process_stratum_target *target, ptid_t ptid)
  1303. {
  1304. /* Look up map for target. */
  1305. auto pid_ptid_regc_map_it = regcaches.find (target);
  1306. if (pid_ptid_regc_map_it != regcaches.end ())
  1307. {
  1308. pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
  1309. /* Look map for pid. */
  1310. auto ptid_regc_map_it = pid_ptid_regc_map.find (ptid.pid ());
  1311. if (ptid_regc_map_it != pid_ptid_regc_map.end ())
  1312. {
  1313. ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
  1314. auto range = ptid_regc_map.equal_range (ptid);
  1315. return std::distance (range.first, range.second);
  1316. }
  1317. }
  1318. return 0;
  1319. };
  1320. /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
  1321. static void
  1322. get_thread_arch_aspace_regcache_and_check (process_stratum_target *target,
  1323. ptid_t ptid)
  1324. {
  1325. /* We currently only test with a single gdbarch. Any gdbarch will do, so use
  1326. the current inferior's gdbarch. Also use the current inferior's address
  1327. space. */
  1328. gdbarch *arch = current_inferior ()->gdbarch;
  1329. address_space *aspace = current_inferior ()->aspace;
  1330. regcache *regcache
  1331. = get_thread_arch_aspace_regcache (target, ptid, arch, aspace);
  1332. SELF_CHECK (regcache != NULL);
  1333. SELF_CHECK (regcache->target () == target);
  1334. SELF_CHECK (regcache->ptid () == ptid);
  1335. SELF_CHECK (regcache->arch () == arch);
  1336. SELF_CHECK (regcache->aspace () == aspace);
  1337. }
  1338. /* The data that the regcaches selftests must hold onto for the duration of the
  1339. test. */
  1340. struct regcache_test_data
  1341. {
  1342. regcache_test_data ()
  1343. {
  1344. /* Ensure the regcaches container is empty at the start. */
  1345. registers_changed ();
  1346. }
  1347. ~regcache_test_data ()
  1348. {
  1349. /* Make sure to leave the global regcaches container empty. */
  1350. registers_changed ();
  1351. }
  1352. test_target_ops test_target1;
  1353. test_target_ops test_target2;
  1354. };
  1355. using regcache_test_data_up = std::unique_ptr<regcache_test_data>;
  1356. /* Set up a few regcaches from two different targets, for use in
  1357. regcache-management tests.
  1358. Return a pointer, because the `regcache_test_data` type is not moveable. */
  1359. static regcache_test_data_up
  1360. populate_regcaches_for_test ()
  1361. {
  1362. regcache_test_data_up data (new regcache_test_data);
  1363. size_t expected_regcache_size = 0;
  1364. SELF_CHECK (regcaches_size () == 0);
  1365. /* Populate the regcache container with a few regcaches for the two test
  1366. targets. */
  1367. for (int pid : { 1, 2 })
  1368. {
  1369. for (long lwp : { 1, 2, 3 })
  1370. {
  1371. get_thread_arch_aspace_regcache_and_check
  1372. (&data->test_target1, ptid_t (pid, lwp));
  1373. expected_regcache_size++;
  1374. SELF_CHECK (regcaches_size () == expected_regcache_size);
  1375. get_thread_arch_aspace_regcache_and_check
  1376. (&data->test_target2, ptid_t (pid, lwp));
  1377. expected_regcache_size++;
  1378. SELF_CHECK (regcaches_size () == expected_regcache_size);
  1379. }
  1380. }
  1381. return data;
  1382. }
  1383. static void
  1384. get_thread_arch_aspace_regcache_test ()
  1385. {
  1386. /* populate_regcaches_for_test already tests most of the
  1387. get_thread_arch_aspace_regcache functionality. */
  1388. regcache_test_data_up data = populate_regcaches_for_test ();
  1389. size_t regcaches_size_before = regcaches_size ();
  1390. /* Test that getting an existing regcache doesn't create a new one. */
  1391. get_thread_arch_aspace_regcache_and_check (&data->test_target1, ptid_t (2, 2));
  1392. SELF_CHECK (regcaches_size () == regcaches_size_before);
  1393. }
  1394. /* Test marking all regcaches of all targets as changed. */
  1395. static void
  1396. registers_changed_ptid_all_test ()
  1397. {
  1398. regcache_test_data_up data = populate_regcaches_for_test ();
  1399. registers_changed_ptid (nullptr, minus_one_ptid);
  1400. SELF_CHECK (regcaches_size () == 0);
  1401. }
  1402. /* Test marking regcaches of a specific target as changed. */
  1403. static void
  1404. registers_changed_ptid_target_test ()
  1405. {
  1406. regcache_test_data_up data = populate_regcaches_for_test ();
  1407. registers_changed_ptid (&data->test_target1, minus_one_ptid);
  1408. SELF_CHECK (regcaches_size () == 6);
  1409. /* Check that we deleted the regcache for the right target. */
  1410. SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
  1411. SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
  1412. }
  1413. /* Test marking regcaches of a specific (target, pid) as changed. */
  1414. static void
  1415. registers_changed_ptid_target_pid_test ()
  1416. {
  1417. regcache_test_data_up data = populate_regcaches_for_test ();
  1418. registers_changed_ptid (&data->test_target1, ptid_t (2));
  1419. SELF_CHECK (regcaches_size () == 9);
  1420. /* Regcaches from target1 should not exist, while regcaches from target2
  1421. should exist. */
  1422. SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
  1423. SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
  1424. }
  1425. /* Test marking regcaches of a specific (target, ptid) as changed. */
  1426. static void
  1427. registers_changed_ptid_target_ptid_test ()
  1428. {
  1429. regcache_test_data_up data = populate_regcaches_for_test ();
  1430. registers_changed_ptid (&data->test_target1, ptid_t (2, 2));
  1431. SELF_CHECK (regcaches_size () == 11);
  1432. /* Check that we deleted the regcache for the right target. */
  1433. SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
  1434. SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
  1435. }
  1436. class target_ops_no_register : public test_target_ops
  1437. {
  1438. public:
  1439. target_ops_no_register ()
  1440. : test_target_ops {}
  1441. {}
  1442. void reset ()
  1443. {
  1444. fetch_registers_called = 0;
  1445. store_registers_called = 0;
  1446. xfer_partial_called = 0;
  1447. }
  1448. void fetch_registers (regcache *regs, int regno) override;
  1449. void store_registers (regcache *regs, int regno) override;
  1450. enum target_xfer_status xfer_partial (enum target_object object,
  1451. const char *annex, gdb_byte *readbuf,
  1452. const gdb_byte *writebuf,
  1453. ULONGEST offset, ULONGEST len,
  1454. ULONGEST *xfered_len) override;
  1455. unsigned int fetch_registers_called = 0;
  1456. unsigned int store_registers_called = 0;
  1457. unsigned int xfer_partial_called = 0;
  1458. };
  1459. void
  1460. target_ops_no_register::fetch_registers (regcache *regs, int regno)
  1461. {
  1462. /* Mark register available. */
  1463. regs->raw_supply_zeroed (regno);
  1464. this->fetch_registers_called++;
  1465. }
  1466. void
  1467. target_ops_no_register::store_registers (regcache *regs, int regno)
  1468. {
  1469. this->store_registers_called++;
  1470. }
  1471. enum target_xfer_status
  1472. target_ops_no_register::xfer_partial (enum target_object object,
  1473. const char *annex, gdb_byte *readbuf,
  1474. const gdb_byte *writebuf,
  1475. ULONGEST offset, ULONGEST len,
  1476. ULONGEST *xfered_len)
  1477. {
  1478. this->xfer_partial_called++;
  1479. *xfered_len = len;
  1480. return TARGET_XFER_OK;
  1481. }
  1482. class readwrite_regcache : public regcache
  1483. {
  1484. public:
  1485. readwrite_regcache (process_stratum_target *target,
  1486. struct gdbarch *gdbarch)
  1487. : regcache (target, gdbarch, nullptr)
  1488. {}
  1489. };
  1490. /* Test regcache::cooked_read gets registers from raw registers and
  1491. memory instead of target to_{fetch,store}_registers. */
  1492. static void
  1493. cooked_read_test (struct gdbarch *gdbarch)
  1494. {
  1495. scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
  1496. /* Test that read one raw register from regcache_no_target will go
  1497. to the target layer. */
  1498. /* Find a raw register which size isn't zero. */
  1499. int nonzero_regnum;
  1500. for (nonzero_regnum = 0;
  1501. nonzero_regnum < gdbarch_num_regs (gdbarch);
  1502. nonzero_regnum++)
  1503. {
  1504. if (register_size (gdbarch, nonzero_regnum) != 0)
  1505. break;
  1506. }
  1507. readwrite_regcache readwrite (&mockctx.mock_target, gdbarch);
  1508. gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
  1509. readwrite.raw_read (nonzero_regnum, buf.data ());
  1510. /* raw_read calls target_fetch_registers. */
  1511. SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
  1512. mockctx.mock_target.reset ();
  1513. /* Mark all raw registers valid, so the following raw registers
  1514. accesses won't go to target. */
  1515. for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
  1516. readwrite.raw_update (i);
  1517. mockctx.mock_target.reset ();
  1518. /* Then, read all raw and pseudo registers, and don't expect calling
  1519. to_{fetch,store}_registers. */
  1520. for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
  1521. {
  1522. if (register_size (gdbarch, regnum) == 0)
  1523. continue;
  1524. gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
  1525. SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
  1526. inner_buf.data ()));
  1527. SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
  1528. SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
  1529. SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
  1530. mockctx.mock_target.reset ();
  1531. }
  1532. readonly_detached_regcache readonly (readwrite);
  1533. /* GDB may go to target layer to fetch all registers and memory for
  1534. readonly regcache. */
  1535. mockctx.mock_target.reset ();
  1536. for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
  1537. {
  1538. if (register_size (gdbarch, regnum) == 0)
  1539. continue;
  1540. gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
  1541. enum register_status status = readonly.cooked_read (regnum,
  1542. inner_buf.data ());
  1543. if (regnum < gdbarch_num_regs (gdbarch))
  1544. {
  1545. auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  1546. if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
  1547. || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
  1548. || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
  1549. || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
  1550. || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
  1551. || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
  1552. || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
  1553. || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
  1554. {
  1555. /* Raw registers. If raw registers are not in save_reggroup,
  1556. their status are unknown. */
  1557. if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
  1558. SELF_CHECK (status == REG_VALID);
  1559. else
  1560. SELF_CHECK (status == REG_UNKNOWN);
  1561. }
  1562. else
  1563. SELF_CHECK (status == REG_VALID);
  1564. }
  1565. else
  1566. {
  1567. if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
  1568. SELF_CHECK (status == REG_VALID);
  1569. else
  1570. {
  1571. /* If pseudo registers are not in save_reggroup, some of
  1572. them can be computed from saved raw registers, but some
  1573. of them are unknown. */
  1574. auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  1575. if (bfd_arch == bfd_arch_frv
  1576. || bfd_arch == bfd_arch_m32c
  1577. || bfd_arch == bfd_arch_mep
  1578. || bfd_arch == bfd_arch_sh)
  1579. SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
  1580. else if (bfd_arch == bfd_arch_mips
  1581. || bfd_arch == bfd_arch_h8300)
  1582. SELF_CHECK (status == REG_UNKNOWN);
  1583. else
  1584. SELF_CHECK (status == REG_VALID);
  1585. }
  1586. }
  1587. SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
  1588. SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
  1589. SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
  1590. mockctx.mock_target.reset ();
  1591. }
  1592. }
  1593. /* Test regcache::cooked_write by writing some expected contents to
  1594. registers, and checking that contents read from registers and the
  1595. expected contents are the same. */
  1596. static void
  1597. cooked_write_test (struct gdbarch *gdbarch)
  1598. {
  1599. /* Error out if debugging something, because we're going to push the
  1600. test target, which would pop any existing target. */
  1601. if (current_inferior ()->top_target ()->stratum () >= process_stratum)
  1602. error (_("target already pushed"));
  1603. /* Create a mock environment. A process_stratum target pushed. */
  1604. target_ops_no_register mock_target;
  1605. /* Push the process_stratum target so we can mock accessing
  1606. registers. */
  1607. current_inferior ()->push_target (&mock_target);
  1608. /* Pop it again on exit (return/exception). */
  1609. struct on_exit
  1610. {
  1611. ~on_exit ()
  1612. {
  1613. pop_all_targets_at_and_above (process_stratum);
  1614. }
  1615. } pop_targets;
  1616. readwrite_regcache readwrite (&mock_target, gdbarch);
  1617. const int num_regs = gdbarch_num_cooked_regs (gdbarch);
  1618. for (auto regnum = 0; regnum < num_regs; regnum++)
  1619. {
  1620. if (register_size (gdbarch, regnum) == 0
  1621. || gdbarch_cannot_store_register (gdbarch, regnum))
  1622. continue;
  1623. auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  1624. if (bfd_arch == bfd_arch_sparc
  1625. /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
  1626. SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
  1627. && gdbarch_ptr_bit (gdbarch) == 64
  1628. && (regnum >= gdbarch_num_regs (gdbarch)
  1629. && regnum <= gdbarch_num_regs (gdbarch) + 4))
  1630. continue;
  1631. std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
  1632. std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
  1633. const auto type = register_type (gdbarch, regnum);
  1634. if (type->code () == TYPE_CODE_FLT
  1635. || type->code () == TYPE_CODE_DECFLOAT)
  1636. {
  1637. /* Generate valid float format. */
  1638. target_float_from_string (expected.data (), type, "1.25");
  1639. }
  1640. else if (type->code () == TYPE_CODE_INT
  1641. || type->code () == TYPE_CODE_ARRAY
  1642. || type->code () == TYPE_CODE_PTR
  1643. || type->code () == TYPE_CODE_UNION
  1644. || type->code () == TYPE_CODE_STRUCT)
  1645. {
  1646. if (bfd_arch == bfd_arch_ia64
  1647. || (regnum >= gdbarch_num_regs (gdbarch)
  1648. && (bfd_arch == bfd_arch_xtensa
  1649. || bfd_arch == bfd_arch_bfin
  1650. || bfd_arch == bfd_arch_m32c
  1651. /* m68hc11 pseudo registers are in memory. */
  1652. || bfd_arch == bfd_arch_m68hc11
  1653. || bfd_arch == bfd_arch_m68hc12
  1654. || bfd_arch == bfd_arch_s390))
  1655. || (bfd_arch == bfd_arch_frv
  1656. /* FRV pseudo registers except iacc0. */
  1657. && regnum > gdbarch_num_regs (gdbarch)))
  1658. {
  1659. /* Skip setting the expected values for some architecture
  1660. registers. */
  1661. }
  1662. else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
  1663. {
  1664. /* RL78_PC_REGNUM */
  1665. for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
  1666. expected[j] = j;
  1667. }
  1668. else
  1669. {
  1670. for (auto j = 0; j < register_size (gdbarch, regnum); j++)
  1671. expected[j] = j;
  1672. }
  1673. }
  1674. else if (type->code () == TYPE_CODE_FLAGS)
  1675. {
  1676. /* No idea how to test flags. */
  1677. continue;
  1678. }
  1679. else
  1680. {
  1681. /* If we don't know how to create the expected value for the
  1682. this type, make it fail. */
  1683. SELF_CHECK (0);
  1684. }
  1685. readwrite.cooked_write (regnum, expected.data ());
  1686. SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
  1687. SELF_CHECK (expected == buf);
  1688. }
  1689. }
  1690. /* Verify that when two threads with the same ptid exist (from two different
  1691. targets) and one of them changes ptid, we only update the appropriate
  1692. regcaches. */
  1693. static void
  1694. regcache_thread_ptid_changed ()
  1695. {
  1696. /* This test relies on the global regcache list to initially be empty. */
  1697. registers_changed ();
  1698. /* Any arch will do. */
  1699. gdbarch *arch = current_inferior ()->gdbarch;
  1700. /* Prepare two targets with one thread each, with the same ptid. */
  1701. scoped_mock_context<test_target_ops> target1 (arch);
  1702. scoped_mock_context<test_target_ops> target2 (arch);
  1703. ptid_t old_ptid (111, 222);
  1704. ptid_t new_ptid (111, 333);
  1705. target1.mock_inferior.pid = old_ptid.pid ();
  1706. target1.mock_thread.ptid = old_ptid;
  1707. target1.mock_inferior.ptid_thread_map.clear ();
  1708. target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread;
  1709. target2.mock_inferior.pid = old_ptid.pid ();
  1710. target2.mock_thread.ptid = old_ptid;
  1711. target2.mock_inferior.ptid_thread_map.clear ();
  1712. target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread;
  1713. gdb_assert (regcaches.empty ());
  1714. /* Populate the regcaches container. */
  1715. get_thread_arch_aspace_regcache (&target1.mock_target, old_ptid, arch,
  1716. nullptr);
  1717. get_thread_arch_aspace_regcache (&target2.mock_target, old_ptid, arch,
  1718. nullptr);
  1719. gdb_assert (regcaches.size () == 2);
  1720. gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
  1721. gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
  1722. gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
  1723. gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
  1724. thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
  1725. gdb_assert (regcaches.size () == 2);
  1726. gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
  1727. gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
  1728. gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
  1729. gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
  1730. /* Leave the regcache list empty. */
  1731. registers_changed ();
  1732. gdb_assert (regcaches.empty ());
  1733. }
  1734. } // namespace selftests
  1735. #endif /* GDB_SELF_TEST */
  1736. void _initialize_regcache ();
  1737. void
  1738. _initialize_regcache ()
  1739. {
  1740. struct cmd_list_element *c;
  1741. regcache_descr_handle
  1742. = gdbarch_data_register_post_init (init_regcache_descr);
  1743. gdb::observers::target_changed.attach (regcache_observer_target_changed,
  1744. "regcache");
  1745. gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed,
  1746. "regcache");
  1747. cmd_list_element *maintenance_flush_register_cache_cmd
  1748. = add_cmd ("register-cache", class_maintenance, reg_flush_command,
  1749. _("Force gdb to flush its register and frame cache."),
  1750. &maintenanceflushlist);
  1751. c = add_com_alias ("flushregs", maintenance_flush_register_cache_cmd,
  1752. class_maintenance, 0);
  1753. deprecate_cmd (c, "maintenance flush register-cache");
  1754. #if GDB_SELF_TEST
  1755. selftests::register_test ("get_thread_arch_aspace_regcache",
  1756. selftests::get_thread_arch_aspace_regcache_test);
  1757. selftests::register_test ("registers_changed_ptid_all",
  1758. selftests::registers_changed_ptid_all_test);
  1759. selftests::register_test ("registers_changed_ptid_target",
  1760. selftests::registers_changed_ptid_target_test);
  1761. selftests::register_test ("registers_changed_ptid_target_pid",
  1762. selftests::registers_changed_ptid_target_pid_test);
  1763. selftests::register_test ("registers_changed_ptid_target_ptid",
  1764. selftests::registers_changed_ptid_target_ptid_test);
  1765. selftests::register_test_foreach_arch ("regcache::cooked_read_test",
  1766. selftests::cooked_read_test);
  1767. selftests::register_test_foreach_arch ("regcache::cooked_write_test",
  1768. selftests::cooked_write_test);
  1769. selftests::register_test ("regcache_thread_ptid_changed",
  1770. selftests::regcache_thread_ptid_changed);
  1771. #endif
  1772. }