frame.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163
  1. /* Cache and manage frames 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 "frame.h"
  16. #include "target.h"
  17. #include "value.h"
  18. #include "inferior.h" /* for inferior_ptid */
  19. #include "regcache.h"
  20. #include "user-regs.h"
  21. #include "gdbsupport/gdb_obstack.h"
  22. #include "dummy-frame.h"
  23. #include "sentinel-frame.h"
  24. #include "gdbcore.h"
  25. #include "annotate.h"
  26. #include "language.h"
  27. #include "frame-unwind.h"
  28. #include "frame-base.h"
  29. #include "command.h"
  30. #include "gdbcmd.h"
  31. #include "observable.h"
  32. #include "objfiles.h"
  33. #include "gdbthread.h"
  34. #include "block.h"
  35. #include "inline-frame.h"
  36. #include "tracepoint.h"
  37. #include "hashtab.h"
  38. #include "valprint.h"
  39. #include "cli/cli-option.h"
  40. /* The sentinel frame terminates the innermost end of the frame chain.
  41. If unwound, it returns the information needed to construct an
  42. innermost frame.
  43. The current frame, which is the innermost frame, can be found at
  44. sentinel_frame->prev. */
  45. static struct frame_info *sentinel_frame;
  46. /* Number of calls to reinit_frame_cache. */
  47. static unsigned int frame_cache_generation = 0;
  48. /* See frame.h. */
  49. unsigned int
  50. get_frame_cache_generation ()
  51. {
  52. return frame_cache_generation;
  53. }
  54. /* The values behind the global "set backtrace ..." settings. */
  55. set_backtrace_options user_set_backtrace_options;
  56. static struct frame_info *get_prev_frame_raw (struct frame_info *this_frame);
  57. static const char *frame_stop_reason_symbol_string (enum unwind_stop_reason reason);
  58. /* Status of some values cached in the frame_info object. */
  59. enum cached_copy_status
  60. {
  61. /* Value is unknown. */
  62. CC_UNKNOWN,
  63. /* We have a value. */
  64. CC_VALUE,
  65. /* Value was not saved. */
  66. CC_NOT_SAVED,
  67. /* Value is unavailable. */
  68. CC_UNAVAILABLE
  69. };
  70. enum class frame_id_status
  71. {
  72. /* Frame id is not computed. */
  73. NOT_COMPUTED = 0,
  74. /* Frame id is being computed (compute_frame_id is active). */
  75. COMPUTING,
  76. /* Frame id has been computed. */
  77. COMPUTED,
  78. };
  79. /* We keep a cache of stack frames, each of which is a "struct
  80. frame_info". The innermost one gets allocated (in
  81. wait_for_inferior) each time the inferior stops; sentinel_frame
  82. points to it. Additional frames get allocated (in get_prev_frame)
  83. as needed, and are chained through the next and prev fields. Any
  84. time that the frame cache becomes invalid (most notably when we
  85. execute something, but also if we change how we interpret the
  86. frames (e.g. "set heuristic-fence-post" in mips-tdep.c, or anything
  87. which reads new symbols)), we should call reinit_frame_cache. */
  88. struct frame_info
  89. {
  90. /* Return a string representation of this frame. */
  91. std::string to_string () const;
  92. /* Level of this frame. The inner-most (youngest) frame is at level
  93. 0. As you move towards the outer-most (oldest) frame, the level
  94. increases. This is a cached value. It could just as easily be
  95. computed by counting back from the selected frame to the inner
  96. most frame. */
  97. /* NOTE: cagney/2002-04-05: Perhaps a level of ``-1'' should be
  98. reserved to indicate a bogus frame - one that has been created
  99. just to keep GDB happy (GDB always needs a frame). For the
  100. moment leave this as speculation. */
  101. int level;
  102. /* The frame's program space. */
  103. struct program_space *pspace;
  104. /* The frame's address space. */
  105. const address_space *aspace;
  106. /* The frame's low-level unwinder and corresponding cache. The
  107. low-level unwinder is responsible for unwinding register values
  108. for the previous frame. The low-level unwind methods are
  109. selected based on the presence, or otherwise, of register unwind
  110. information such as CFI. */
  111. void *prologue_cache;
  112. const struct frame_unwind *unwind;
  113. /* Cached copy of the previous frame's architecture. */
  114. struct
  115. {
  116. bool p;
  117. struct gdbarch *arch;
  118. } prev_arch;
  119. /* Cached copy of the previous frame's resume address. */
  120. struct {
  121. cached_copy_status status;
  122. /* Did VALUE require unmasking when being read. */
  123. bool masked;
  124. CORE_ADDR value;
  125. } prev_pc;
  126. /* Cached copy of the previous frame's function address. */
  127. struct
  128. {
  129. CORE_ADDR addr;
  130. cached_copy_status status;
  131. } prev_func;
  132. /* This frame's ID. */
  133. struct
  134. {
  135. frame_id_status p;
  136. struct frame_id value;
  137. } this_id;
  138. /* The frame's high-level base methods, and corresponding cache.
  139. The high level base methods are selected based on the frame's
  140. debug info. */
  141. const struct frame_base *base;
  142. void *base_cache;
  143. /* Pointers to the next (down, inner, younger) and previous (up,
  144. outer, older) frame_info's in the frame cache. */
  145. struct frame_info *next; /* down, inner, younger */
  146. bool prev_p;
  147. struct frame_info *prev; /* up, outer, older */
  148. /* The reason why we could not set PREV, or UNWIND_NO_REASON if we
  149. could. Only valid when PREV_P is set. */
  150. enum unwind_stop_reason stop_reason;
  151. /* A frame specific string describing the STOP_REASON in more detail.
  152. Only valid when PREV_P is set, but even then may still be NULL. */
  153. const char *stop_string;
  154. };
  155. /* See frame.h. */
  156. void
  157. set_frame_previous_pc_masked (struct frame_info *frame)
  158. {
  159. frame->prev_pc.masked = true;
  160. }
  161. /* See frame.h. */
  162. bool
  163. get_frame_pc_masked (const struct frame_info *frame)
  164. {
  165. gdb_assert (frame->next != nullptr);
  166. gdb_assert (frame->next->prev_pc.status == CC_VALUE);
  167. return frame->next->prev_pc.masked;
  168. }
  169. /* A frame stash used to speed up frame lookups. Create a hash table
  170. to stash frames previously accessed from the frame cache for
  171. quicker subsequent retrieval. The hash table is emptied whenever
  172. the frame cache is invalidated. */
  173. static htab_t frame_stash;
  174. /* Internal function to calculate a hash from the frame_id addresses,
  175. using as many valid addresses as possible. Frames below level 0
  176. are not stored in the hash table. */
  177. static hashval_t
  178. frame_addr_hash (const void *ap)
  179. {
  180. const struct frame_info *frame = (const struct frame_info *) ap;
  181. const struct frame_id f_id = frame->this_id.value;
  182. hashval_t hash = 0;
  183. gdb_assert (f_id.stack_status != FID_STACK_INVALID
  184. || f_id.code_addr_p
  185. || f_id.special_addr_p);
  186. if (f_id.stack_status == FID_STACK_VALID)
  187. hash = iterative_hash (&f_id.stack_addr,
  188. sizeof (f_id.stack_addr), hash);
  189. if (f_id.code_addr_p)
  190. hash = iterative_hash (&f_id.code_addr,
  191. sizeof (f_id.code_addr), hash);
  192. if (f_id.special_addr_p)
  193. hash = iterative_hash (&f_id.special_addr,
  194. sizeof (f_id.special_addr), hash);
  195. return hash;
  196. }
  197. /* Internal equality function for the hash table. This function
  198. defers equality operations to frame_id_eq. */
  199. static int
  200. frame_addr_hash_eq (const void *a, const void *b)
  201. {
  202. const struct frame_info *f_entry = (const struct frame_info *) a;
  203. const struct frame_info *f_element = (const struct frame_info *) b;
  204. return frame_id_eq (f_entry->this_id.value,
  205. f_element->this_id.value);
  206. }
  207. /* Internal function to create the frame_stash hash table. 100 seems
  208. to be a good compromise to start the hash table at. */
  209. static void
  210. frame_stash_create (void)
  211. {
  212. frame_stash = htab_create (100,
  213. frame_addr_hash,
  214. frame_addr_hash_eq,
  215. NULL);
  216. }
  217. /* Internal function to add a frame to the frame_stash hash table.
  218. Returns false if a frame with the same ID was already stashed, true
  219. otherwise. */
  220. static bool
  221. frame_stash_add (frame_info *frame)
  222. {
  223. /* Do not try to stash the sentinel frame. */
  224. gdb_assert (frame->level >= 0);
  225. frame_info **slot = (struct frame_info **) htab_find_slot (frame_stash,
  226. frame, INSERT);
  227. /* If we already have a frame in the stack with the same id, we
  228. either have a stack cycle (corrupted stack?), or some bug
  229. elsewhere in GDB. In any case, ignore the duplicate and return
  230. an indication to the caller. */
  231. if (*slot != nullptr)
  232. return false;
  233. *slot = frame;
  234. return true;
  235. }
  236. /* Internal function to search the frame stash for an entry with the
  237. given frame ID. If found, return that frame. Otherwise return
  238. NULL. */
  239. static struct frame_info *
  240. frame_stash_find (struct frame_id id)
  241. {
  242. struct frame_info dummy;
  243. struct frame_info *frame;
  244. dummy.this_id.value = id;
  245. frame = (struct frame_info *) htab_find (frame_stash, &dummy);
  246. return frame;
  247. }
  248. /* Internal function to invalidate the frame stash by removing all
  249. entries in it. This only occurs when the frame cache is
  250. invalidated. */
  251. static void
  252. frame_stash_invalidate (void)
  253. {
  254. htab_empty (frame_stash);
  255. }
  256. /* See frame.h */
  257. scoped_restore_selected_frame::scoped_restore_selected_frame ()
  258. {
  259. m_lang = current_language->la_language;
  260. save_selected_frame (&m_fid, &m_level);
  261. }
  262. /* See frame.h */
  263. scoped_restore_selected_frame::~scoped_restore_selected_frame ()
  264. {
  265. restore_selected_frame (m_fid, m_level);
  266. set_language (m_lang);
  267. }
  268. /* Flag to control debugging. */
  269. bool frame_debug;
  270. static void
  271. show_frame_debug (struct ui_file *file, int from_tty,
  272. struct cmd_list_element *c, const char *value)
  273. {
  274. gdb_printf (file, _("Frame debugging is %s.\n"), value);
  275. }
  276. /* Implementation of "show backtrace past-main". */
  277. static void
  278. show_backtrace_past_main (struct ui_file *file, int from_tty,
  279. struct cmd_list_element *c, const char *value)
  280. {
  281. gdb_printf (file,
  282. _("Whether backtraces should "
  283. "continue past \"main\" is %s.\n"),
  284. value);
  285. }
  286. /* Implementation of "show backtrace past-entry". */
  287. static void
  288. show_backtrace_past_entry (struct ui_file *file, int from_tty,
  289. struct cmd_list_element *c, const char *value)
  290. {
  291. gdb_printf (file, _("Whether backtraces should continue past the "
  292. "entry point of a program is %s.\n"),
  293. value);
  294. }
  295. /* Implementation of "show backtrace limit". */
  296. static void
  297. show_backtrace_limit (struct ui_file *file, int from_tty,
  298. struct cmd_list_element *c, const char *value)
  299. {
  300. gdb_printf (file,
  301. _("An upper bound on the number "
  302. "of backtrace levels is %s.\n"),
  303. value);
  304. }
  305. /* See frame.h. */
  306. std::string
  307. frame_id::to_string () const
  308. {
  309. const struct frame_id &id = *this;
  310. std::string res = "{";
  311. if (id.stack_status == FID_STACK_INVALID)
  312. res += "!stack";
  313. else if (id.stack_status == FID_STACK_UNAVAILABLE)
  314. res += "stack=<unavailable>";
  315. else if (id.stack_status == FID_STACK_SENTINEL)
  316. res += "stack=<sentinel>";
  317. else if (id.stack_status == FID_STACK_OUTER)
  318. res += "stack=<outer>";
  319. else
  320. res += std::string ("stack=") + hex_string (id.stack_addr);
  321. /* Helper function to format 'N=A' if P is true, otherwise '!N'. */
  322. auto field_to_string = [] (const char *n, bool p, CORE_ADDR a) -> std::string
  323. {
  324. if (p)
  325. return std::string (n) + "=" + core_addr_to_string (a);
  326. else
  327. return std::string ("!") + std::string (n);
  328. };
  329. res += (std::string (",")
  330. + field_to_string ("code", id.code_addr_p, id.code_addr)
  331. + std::string (",")
  332. + field_to_string ("special", id.special_addr_p, id.special_addr));
  333. if (id.artificial_depth)
  334. res += ",artificial=" + std::to_string (id.artificial_depth);
  335. res += "}";
  336. return res;
  337. }
  338. /* Return a string representation of TYPE. */
  339. static const char *
  340. frame_type_str (frame_type type)
  341. {
  342. switch (type)
  343. {
  344. case NORMAL_FRAME:
  345. return "NORMAL_FRAME";
  346. case DUMMY_FRAME:
  347. return "DUMMY_FRAME";
  348. case INLINE_FRAME:
  349. return "INLINE_FRAME";
  350. case TAILCALL_FRAME:
  351. return "TAILCALL_FRAME";
  352. case SIGTRAMP_FRAME:
  353. return "SIGTRAMP_FRAME";
  354. case ARCH_FRAME:
  355. return "ARCH_FRAME";
  356. case SENTINEL_FRAME:
  357. return "SENTINEL_FRAME";
  358. default:
  359. return "<unknown type>";
  360. };
  361. }
  362. /* See struct frame_info. */
  363. std::string
  364. frame_info::to_string () const
  365. {
  366. const frame_info *fi = this;
  367. std::string res;
  368. res += string_printf ("{level=%d,", fi->level);
  369. if (fi->unwind != NULL)
  370. res += string_printf ("type=%s,", frame_type_str (fi->unwind->type));
  371. else
  372. res += "type=<unknown>,";
  373. if (fi->unwind != NULL)
  374. res += string_printf ("unwinder=\"%s\",", fi->unwind->name);
  375. else
  376. res += "unwinder=<unknown>,";
  377. if (fi->next == NULL || fi->next->prev_pc.status == CC_UNKNOWN)
  378. res += "pc=<unknown>,";
  379. else if (fi->next->prev_pc.status == CC_VALUE)
  380. res += string_printf ("pc=%s%s,", hex_string (fi->next->prev_pc.value),
  381. fi->next->prev_pc.masked ? "[PAC]" : "");
  382. else if (fi->next->prev_pc.status == CC_NOT_SAVED)
  383. res += "pc=<not saved>,";
  384. else if (fi->next->prev_pc.status == CC_UNAVAILABLE)
  385. res += "pc=<unavailable>,";
  386. if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
  387. res += "id=<not computed>,";
  388. else if (fi->this_id.p == frame_id_status::COMPUTING)
  389. res += "id=<computing>,";
  390. else
  391. res += string_printf ("id=%s,", fi->this_id.value.to_string ().c_str ());
  392. if (fi->next != NULL && fi->next->prev_func.status == CC_VALUE)
  393. res += string_printf ("func=%s", hex_string (fi->next->prev_func.addr));
  394. else
  395. res += "func=<unknown>";
  396. res += "}";
  397. return res;
  398. }
  399. /* Given FRAME, return the enclosing frame as found in real frames read-in from
  400. inferior memory. Skip any previous frames which were made up by GDB.
  401. Return FRAME if FRAME is a non-artificial frame.
  402. Return NULL if FRAME is the start of an artificial-only chain. */
  403. static struct frame_info *
  404. skip_artificial_frames (struct frame_info *frame)
  405. {
  406. /* Note we use get_prev_frame_always, and not get_prev_frame. The
  407. latter will truncate the frame chain, leading to this function
  408. unintentionally returning a null_frame_id (e.g., when the user
  409. sets a backtrace limit).
  410. Note that for record targets we may get a frame chain that consists
  411. of artificial frames only. */
  412. while (get_frame_type (frame) == INLINE_FRAME
  413. || get_frame_type (frame) == TAILCALL_FRAME)
  414. {
  415. frame = get_prev_frame_always (frame);
  416. if (frame == NULL)
  417. break;
  418. }
  419. return frame;
  420. }
  421. struct frame_info *
  422. skip_unwritable_frames (struct frame_info *frame)
  423. {
  424. while (gdbarch_code_of_frame_writable (get_frame_arch (frame), frame) == 0)
  425. {
  426. frame = get_prev_frame (frame);
  427. if (frame == NULL)
  428. break;
  429. }
  430. return frame;
  431. }
  432. /* See frame.h. */
  433. struct frame_info *
  434. skip_tailcall_frames (struct frame_info *frame)
  435. {
  436. while (get_frame_type (frame) == TAILCALL_FRAME)
  437. {
  438. /* Note that for record targets we may get a frame chain that consists of
  439. tailcall frames only. */
  440. frame = get_prev_frame (frame);
  441. if (frame == NULL)
  442. break;
  443. }
  444. return frame;
  445. }
  446. /* Compute the frame's uniq ID that can be used to, later, re-find the
  447. frame. */
  448. static void
  449. compute_frame_id (struct frame_info *fi)
  450. {
  451. FRAME_SCOPED_DEBUG_ENTER_EXIT;
  452. gdb_assert (fi->this_id.p == frame_id_status::NOT_COMPUTED);
  453. unsigned int entry_generation = get_frame_cache_generation ();
  454. try
  455. {
  456. /* Mark this frame's id as "being computed. */
  457. fi->this_id.p = frame_id_status::COMPUTING;
  458. frame_debug_printf ("fi=%d", fi->level);
  459. /* Find the unwinder. */
  460. if (fi->unwind == NULL)
  461. frame_unwind_find_by_frame (fi, &fi->prologue_cache);
  462. /* Find THIS frame's ID. */
  463. /* Default to outermost if no ID is found. */
  464. fi->this_id.value = outer_frame_id;
  465. fi->unwind->this_id (fi, &fi->prologue_cache, &fi->this_id.value);
  466. gdb_assert (frame_id_p (fi->this_id.value));
  467. /* Mark this frame's id as "computed". */
  468. fi->this_id.p = frame_id_status::COMPUTED;
  469. frame_debug_printf (" -> %s", fi->this_id.value.to_string ().c_str ());
  470. }
  471. catch (const gdb_exception &ex)
  472. {
  473. /* On error, revert the frame id status to not computed. If the frame
  474. cache generation changed, the frame object doesn't exist anymore, so
  475. don't touch it. */
  476. if (get_frame_cache_generation () == entry_generation)
  477. fi->this_id.p = frame_id_status::NOT_COMPUTED;
  478. throw;
  479. }
  480. }
  481. /* Return a frame uniq ID that can be used to, later, re-find the
  482. frame. */
  483. struct frame_id
  484. get_frame_id (struct frame_info *fi)
  485. {
  486. if (fi == NULL)
  487. return null_frame_id;
  488. /* It's always invalid to try to get a frame's id while it is being
  489. computed. */
  490. gdb_assert (fi->this_id.p != frame_id_status::COMPUTING);
  491. if (fi->this_id.p == frame_id_status::NOT_COMPUTED)
  492. {
  493. /* If we haven't computed the frame id yet, then it must be that
  494. this is the current frame. Compute it now, and stash the
  495. result. The IDs of other frames are computed as soon as
  496. they're created, in order to detect cycles. See
  497. get_prev_frame_if_no_cycle. */
  498. gdb_assert (fi->level == 0);
  499. /* Compute. */
  500. compute_frame_id (fi);
  501. /* Since this is the first frame in the chain, this should
  502. always succeed. */
  503. bool stashed = frame_stash_add (fi);
  504. gdb_assert (stashed);
  505. }
  506. return fi->this_id.value;
  507. }
  508. struct frame_id
  509. get_stack_frame_id (struct frame_info *next_frame)
  510. {
  511. return get_frame_id (skip_artificial_frames (next_frame));
  512. }
  513. struct frame_id
  514. frame_unwind_caller_id (struct frame_info *next_frame)
  515. {
  516. struct frame_info *this_frame;
  517. /* Use get_prev_frame_always, and not get_prev_frame. The latter
  518. will truncate the frame chain, leading to this function
  519. unintentionally returning a null_frame_id (e.g., when a caller
  520. requests the frame ID of "main()"s caller. */
  521. next_frame = skip_artificial_frames (next_frame);
  522. if (next_frame == NULL)
  523. return null_frame_id;
  524. this_frame = get_prev_frame_always (next_frame);
  525. if (this_frame)
  526. return get_frame_id (skip_artificial_frames (this_frame));
  527. else
  528. return null_frame_id;
  529. }
  530. const struct frame_id null_frame_id = { 0 }; /* All zeros. */
  531. const struct frame_id sentinel_frame_id = { 0, 0, 0, FID_STACK_SENTINEL, 0, 1, 0 };
  532. const struct frame_id outer_frame_id = { 0, 0, 0, FID_STACK_OUTER, 0, 1, 0 };
  533. struct frame_id
  534. frame_id_build_special (CORE_ADDR stack_addr, CORE_ADDR code_addr,
  535. CORE_ADDR special_addr)
  536. {
  537. struct frame_id id = null_frame_id;
  538. id.stack_addr = stack_addr;
  539. id.stack_status = FID_STACK_VALID;
  540. id.code_addr = code_addr;
  541. id.code_addr_p = true;
  542. id.special_addr = special_addr;
  543. id.special_addr_p = true;
  544. return id;
  545. }
  546. /* See frame.h. */
  547. struct frame_id
  548. frame_id_build_unavailable_stack (CORE_ADDR code_addr)
  549. {
  550. struct frame_id id = null_frame_id;
  551. id.stack_status = FID_STACK_UNAVAILABLE;
  552. id.code_addr = code_addr;
  553. id.code_addr_p = true;
  554. return id;
  555. }
  556. /* See frame.h. */
  557. struct frame_id
  558. frame_id_build_unavailable_stack_special (CORE_ADDR code_addr,
  559. CORE_ADDR special_addr)
  560. {
  561. struct frame_id id = null_frame_id;
  562. id.stack_status = FID_STACK_UNAVAILABLE;
  563. id.code_addr = code_addr;
  564. id.code_addr_p = true;
  565. id.special_addr = special_addr;
  566. id.special_addr_p = true;
  567. return id;
  568. }
  569. struct frame_id
  570. frame_id_build (CORE_ADDR stack_addr, CORE_ADDR code_addr)
  571. {
  572. struct frame_id id = null_frame_id;
  573. id.stack_addr = stack_addr;
  574. id.stack_status = FID_STACK_VALID;
  575. id.code_addr = code_addr;
  576. id.code_addr_p = true;
  577. return id;
  578. }
  579. struct frame_id
  580. frame_id_build_wild (CORE_ADDR stack_addr)
  581. {
  582. struct frame_id id = null_frame_id;
  583. id.stack_addr = stack_addr;
  584. id.stack_status = FID_STACK_VALID;
  585. return id;
  586. }
  587. bool
  588. frame_id_p (frame_id l)
  589. {
  590. /* The frame is valid iff it has a valid stack address. */
  591. bool p = l.stack_status != FID_STACK_INVALID;
  592. frame_debug_printf ("l=%s -> %d", l.to_string ().c_str (), p);
  593. return p;
  594. }
  595. bool
  596. frame_id_artificial_p (frame_id l)
  597. {
  598. if (!frame_id_p (l))
  599. return false;
  600. return l.artificial_depth != 0;
  601. }
  602. bool
  603. frame_id_eq (frame_id l, frame_id r)
  604. {
  605. bool eq;
  606. if (l.stack_status == FID_STACK_INVALID
  607. || r.stack_status == FID_STACK_INVALID)
  608. /* Like a NaN, if either ID is invalid, the result is false.
  609. Note that a frame ID is invalid iff it is the null frame ID. */
  610. eq = false;
  611. else if (l.stack_status != r.stack_status || l.stack_addr != r.stack_addr)
  612. /* If .stack addresses are different, the frames are different. */
  613. eq = false;
  614. else if (l.code_addr_p && r.code_addr_p && l.code_addr != r.code_addr)
  615. /* An invalid code addr is a wild card. If .code addresses are
  616. different, the frames are different. */
  617. eq = false;
  618. else if (l.special_addr_p && r.special_addr_p
  619. && l.special_addr != r.special_addr)
  620. /* An invalid special addr is a wild card (or unused). Otherwise
  621. if special addresses are different, the frames are different. */
  622. eq = false;
  623. else if (l.artificial_depth != r.artificial_depth)
  624. /* If artificial depths are different, the frames must be different. */
  625. eq = false;
  626. else
  627. /* Frames are equal. */
  628. eq = true;
  629. frame_debug_printf ("l=%s, r=%s -> %d",
  630. l.to_string ().c_str (), r.to_string ().c_str (), eq);
  631. return eq;
  632. }
  633. /* Safety net to check whether frame ID L should be inner to
  634. frame ID R, according to their stack addresses.
  635. This method cannot be used to compare arbitrary frames, as the
  636. ranges of valid stack addresses may be discontiguous (e.g. due
  637. to sigaltstack).
  638. However, it can be used as safety net to discover invalid frame
  639. IDs in certain circumstances. Assuming that NEXT is the immediate
  640. inner frame to THIS and that NEXT and THIS are both NORMAL frames:
  641. * The stack address of NEXT must be inner-than-or-equal to the stack
  642. address of THIS.
  643. Therefore, if frame_id_inner (THIS, NEXT) holds, some unwind
  644. error has occurred.
  645. * If NEXT and THIS have different stack addresses, no other frame
  646. in the frame chain may have a stack address in between.
  647. Therefore, if frame_id_inner (TEST, THIS) holds, but
  648. frame_id_inner (TEST, NEXT) does not hold, TEST cannot refer
  649. to a valid frame in the frame chain.
  650. The sanity checks above cannot be performed when a SIGTRAMP frame
  651. is involved, because signal handlers might be executed on a different
  652. stack than the stack used by the routine that caused the signal
  653. to be raised. This can happen for instance when a thread exceeds
  654. its maximum stack size. In this case, certain compilers implement
  655. a stack overflow strategy that cause the handler to be run on a
  656. different stack. */
  657. static bool
  658. frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
  659. {
  660. bool inner;
  661. if (l.stack_status != FID_STACK_VALID || r.stack_status != FID_STACK_VALID)
  662. /* Like NaN, any operation involving an invalid ID always fails.
  663. Likewise if either ID has an unavailable stack address. */
  664. inner = false;
  665. else if (l.artificial_depth > r.artificial_depth
  666. && l.stack_addr == r.stack_addr
  667. && l.code_addr_p == r.code_addr_p
  668. && l.special_addr_p == r.special_addr_p
  669. && l.special_addr == r.special_addr)
  670. {
  671. /* Same function, different inlined functions. */
  672. const struct block *lb, *rb;
  673. gdb_assert (l.code_addr_p && r.code_addr_p);
  674. lb = block_for_pc (l.code_addr);
  675. rb = block_for_pc (r.code_addr);
  676. if (lb == NULL || rb == NULL)
  677. /* Something's gone wrong. */
  678. inner = false;
  679. else
  680. /* This will return true if LB and RB are the same block, or
  681. if the block with the smaller depth lexically encloses the
  682. block with the greater depth. */
  683. inner = contained_in (lb, rb);
  684. }
  685. else
  686. /* Only return non-zero when strictly inner than. Note that, per
  687. comment in "frame.h", there is some fuzz here. Frameless
  688. functions are not strictly inner than (same .stack but
  689. different .code and/or .special address). */
  690. inner = gdbarch_inner_than (gdbarch, l.stack_addr, r.stack_addr);
  691. frame_debug_printf ("is l=%s inner than r=%s? %d",
  692. l.to_string ().c_str (), r.to_string ().c_str (),
  693. inner);
  694. return inner;
  695. }
  696. struct frame_info *
  697. frame_find_by_id (struct frame_id id)
  698. {
  699. struct frame_info *frame, *prev_frame;
  700. /* ZERO denotes the null frame, let the caller decide what to do
  701. about it. Should it instead return get_current_frame()? */
  702. if (!frame_id_p (id))
  703. return NULL;
  704. /* Check for the sentinel frame. */
  705. if (frame_id_eq (id, sentinel_frame_id))
  706. return sentinel_frame;
  707. /* Try using the frame stash first. Finding it there removes the need
  708. to perform the search by looping over all frames, which can be very
  709. CPU-intensive if the number of frames is very high (the loop is O(n)
  710. and get_prev_frame performs a series of checks that are relatively
  711. expensive). This optimization is particularly useful when this function
  712. is called from another function (such as value_fetch_lazy, case
  713. VALUE_LVAL (val) == lval_register) which already loops over all frames,
  714. making the overall behavior O(n^2). */
  715. frame = frame_stash_find (id);
  716. if (frame)
  717. return frame;
  718. for (frame = get_current_frame (); ; frame = prev_frame)
  719. {
  720. struct frame_id self = get_frame_id (frame);
  721. if (frame_id_eq (id, self))
  722. /* An exact match. */
  723. return frame;
  724. prev_frame = get_prev_frame (frame);
  725. if (!prev_frame)
  726. return NULL;
  727. /* As a safety net to avoid unnecessary backtracing while trying
  728. to find an invalid ID, we check for a common situation where
  729. we can detect from comparing stack addresses that no other
  730. frame in the current frame chain can have this ID. See the
  731. comment at frame_id_inner for details. */
  732. if (get_frame_type (frame) == NORMAL_FRAME
  733. && !frame_id_inner (get_frame_arch (frame), id, self)
  734. && frame_id_inner (get_frame_arch (prev_frame), id,
  735. get_frame_id (prev_frame)))
  736. return NULL;
  737. }
  738. return NULL;
  739. }
  740. static CORE_ADDR
  741. frame_unwind_pc (struct frame_info *this_frame)
  742. {
  743. if (this_frame->prev_pc.status == CC_UNKNOWN)
  744. {
  745. struct gdbarch *prev_gdbarch;
  746. CORE_ADDR pc = 0;
  747. bool pc_p = false;
  748. /* The right way. The `pure' way. The one true way. This
  749. method depends solely on the register-unwind code to
  750. determine the value of registers in THIS frame, and hence
  751. the value of this frame's PC (resume address). A typical
  752. implementation is no more than:
  753. frame_unwind_register (this_frame, ISA_PC_REGNUM, buf);
  754. return extract_unsigned_integer (buf, size of ISA_PC_REGNUM);
  755. Note: this method is very heavily dependent on a correct
  756. register-unwind implementation, it pays to fix that
  757. method first; this method is frame type agnostic, since
  758. it only deals with register values, it works with any
  759. frame. This is all in stark contrast to the old
  760. FRAME_SAVED_PC which would try to directly handle all the
  761. different ways that a PC could be unwound. */
  762. prev_gdbarch = frame_unwind_arch (this_frame);
  763. try
  764. {
  765. pc = gdbarch_unwind_pc (prev_gdbarch, this_frame);
  766. pc_p = true;
  767. }
  768. catch (const gdb_exception_error &ex)
  769. {
  770. if (ex.error == NOT_AVAILABLE_ERROR)
  771. {
  772. this_frame->prev_pc.status = CC_UNAVAILABLE;
  773. frame_debug_printf ("this_frame=%d -> <unavailable>",
  774. this_frame->level);
  775. }
  776. else if (ex.error == OPTIMIZED_OUT_ERROR)
  777. {
  778. this_frame->prev_pc.status = CC_NOT_SAVED;
  779. frame_debug_printf ("this_frame=%d -> <not saved>",
  780. this_frame->level);
  781. }
  782. else
  783. throw;
  784. }
  785. if (pc_p)
  786. {
  787. this_frame->prev_pc.value = pc;
  788. this_frame->prev_pc.status = CC_VALUE;
  789. frame_debug_printf ("this_frame=%d -> %s",
  790. this_frame->level,
  791. hex_string (this_frame->prev_pc.value));
  792. }
  793. }
  794. if (this_frame->prev_pc.status == CC_VALUE)
  795. return this_frame->prev_pc.value;
  796. else if (this_frame->prev_pc.status == CC_UNAVAILABLE)
  797. throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
  798. else if (this_frame->prev_pc.status == CC_NOT_SAVED)
  799. throw_error (OPTIMIZED_OUT_ERROR, _("PC not saved"));
  800. else
  801. internal_error (__FILE__, __LINE__,
  802. "unexpected prev_pc status: %d",
  803. (int) this_frame->prev_pc.status);
  804. }
  805. CORE_ADDR
  806. frame_unwind_caller_pc (struct frame_info *this_frame)
  807. {
  808. this_frame = skip_artificial_frames (this_frame);
  809. /* We must have a non-artificial frame. The caller is supposed to check
  810. the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
  811. in this case. */
  812. gdb_assert (this_frame != NULL);
  813. return frame_unwind_pc (this_frame);
  814. }
  815. bool
  816. get_frame_func_if_available (frame_info *this_frame, CORE_ADDR *pc)
  817. {
  818. struct frame_info *next_frame = this_frame->next;
  819. if (next_frame->prev_func.status == CC_UNKNOWN)
  820. {
  821. CORE_ADDR addr_in_block;
  822. /* Make certain that this, and not the adjacent, function is
  823. found. */
  824. if (!get_frame_address_in_block_if_available (this_frame, &addr_in_block))
  825. {
  826. next_frame->prev_func.status = CC_UNAVAILABLE;
  827. frame_debug_printf ("this_frame=%d -> unavailable",
  828. this_frame->level);
  829. }
  830. else
  831. {
  832. next_frame->prev_func.status = CC_VALUE;
  833. next_frame->prev_func.addr = get_pc_function_start (addr_in_block);
  834. frame_debug_printf ("this_frame=%d -> %s",
  835. this_frame->level,
  836. hex_string (next_frame->prev_func.addr));
  837. }
  838. }
  839. if (next_frame->prev_func.status == CC_UNAVAILABLE)
  840. {
  841. *pc = -1;
  842. return false;
  843. }
  844. else
  845. {
  846. gdb_assert (next_frame->prev_func.status == CC_VALUE);
  847. *pc = next_frame->prev_func.addr;
  848. return true;
  849. }
  850. }
  851. CORE_ADDR
  852. get_frame_func (struct frame_info *this_frame)
  853. {
  854. CORE_ADDR pc;
  855. if (!get_frame_func_if_available (this_frame, &pc))
  856. throw_error (NOT_AVAILABLE_ERROR, _("PC not available"));
  857. return pc;
  858. }
  859. std::unique_ptr<readonly_detached_regcache>
  860. frame_save_as_regcache (struct frame_info *this_frame)
  861. {
  862. auto cooked_read = [this_frame] (int regnum, gdb_byte *buf)
  863. {
  864. if (!deprecated_frame_register_read (this_frame, regnum, buf))
  865. return REG_UNAVAILABLE;
  866. else
  867. return REG_VALID;
  868. };
  869. std::unique_ptr<readonly_detached_regcache> regcache
  870. (new readonly_detached_regcache (get_frame_arch (this_frame), cooked_read));
  871. return regcache;
  872. }
  873. void
  874. frame_pop (struct frame_info *this_frame)
  875. {
  876. struct frame_info *prev_frame;
  877. if (get_frame_type (this_frame) == DUMMY_FRAME)
  878. {
  879. /* Popping a dummy frame involves restoring more than just registers.
  880. dummy_frame_pop does all the work. */
  881. dummy_frame_pop (get_frame_id (this_frame), inferior_thread ());
  882. return;
  883. }
  884. /* Ensure that we have a frame to pop to. */
  885. prev_frame = get_prev_frame_always (this_frame);
  886. if (!prev_frame)
  887. error (_("Cannot pop the initial frame."));
  888. /* Ignore TAILCALL_FRAME type frames, they were executed already before
  889. entering THISFRAME. */
  890. prev_frame = skip_tailcall_frames (prev_frame);
  891. if (prev_frame == NULL)
  892. error (_("Cannot find the caller frame."));
  893. /* Make a copy of all the register values unwound from this frame.
  894. Save them in a scratch buffer so that there isn't a race between
  895. trying to extract the old values from the current regcache while
  896. at the same time writing new values into that same cache. */
  897. std::unique_ptr<readonly_detached_regcache> scratch
  898. = frame_save_as_regcache (prev_frame);
  899. /* FIXME: cagney/2003-03-16: It should be possible to tell the
  900. target's register cache that it is about to be hit with a burst
  901. register transfer and that the sequence of register writes should
  902. be batched. The pair target_prepare_to_store() and
  903. target_store_registers() kind of suggest this functionality.
  904. Unfortunately, they don't implement it. Their lack of a formal
  905. definition can lead to targets writing back bogus values
  906. (arguably a bug in the target code mind). */
  907. /* Now copy those saved registers into the current regcache. */
  908. get_current_regcache ()->restore (scratch.get ());
  909. /* We've made right mess of GDB's local state, just discard
  910. everything. */
  911. reinit_frame_cache ();
  912. }
  913. void
  914. frame_register_unwind (frame_info *next_frame, int regnum,
  915. int *optimizedp, int *unavailablep,
  916. enum lval_type *lvalp, CORE_ADDR *addrp,
  917. int *realnump, gdb_byte *bufferp)
  918. {
  919. struct value *value;
  920. /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
  921. that the value proper does not need to be fetched. */
  922. gdb_assert (optimizedp != NULL);
  923. gdb_assert (lvalp != NULL);
  924. gdb_assert (addrp != NULL);
  925. gdb_assert (realnump != NULL);
  926. /* gdb_assert (bufferp != NULL); */
  927. value = frame_unwind_register_value (next_frame, regnum);
  928. gdb_assert (value != NULL);
  929. *optimizedp = value_optimized_out (value);
  930. *unavailablep = !value_entirely_available (value);
  931. *lvalp = VALUE_LVAL (value);
  932. *addrp = value_address (value);
  933. if (*lvalp == lval_register)
  934. *realnump = VALUE_REGNUM (value);
  935. else
  936. *realnump = -1;
  937. if (bufferp)
  938. {
  939. if (!*optimizedp && !*unavailablep)
  940. memcpy (bufferp, value_contents_all (value).data (),
  941. TYPE_LENGTH (value_type (value)));
  942. else
  943. memset (bufferp, 0, TYPE_LENGTH (value_type (value)));
  944. }
  945. /* Dispose of the new value. This prevents watchpoints from
  946. trying to watch the saved frame pointer. */
  947. release_value (value);
  948. }
  949. void
  950. frame_register (struct frame_info *frame, int regnum,
  951. int *optimizedp, int *unavailablep, enum lval_type *lvalp,
  952. CORE_ADDR *addrp, int *realnump, gdb_byte *bufferp)
  953. {
  954. /* Require all but BUFFERP to be valid. A NULL BUFFERP indicates
  955. that the value proper does not need to be fetched. */
  956. gdb_assert (optimizedp != NULL);
  957. gdb_assert (lvalp != NULL);
  958. gdb_assert (addrp != NULL);
  959. gdb_assert (realnump != NULL);
  960. /* gdb_assert (bufferp != NULL); */
  961. /* Obtain the register value by unwinding the register from the next
  962. (more inner frame). */
  963. gdb_assert (frame != NULL && frame->next != NULL);
  964. frame_register_unwind (frame->next, regnum, optimizedp, unavailablep,
  965. lvalp, addrp, realnump, bufferp);
  966. }
  967. void
  968. frame_unwind_register (frame_info *next_frame, int regnum, gdb_byte *buf)
  969. {
  970. int optimized;
  971. int unavailable;
  972. CORE_ADDR addr;
  973. int realnum;
  974. enum lval_type lval;
  975. frame_register_unwind (next_frame, regnum, &optimized, &unavailable,
  976. &lval, &addr, &realnum, buf);
  977. if (optimized)
  978. throw_error (OPTIMIZED_OUT_ERROR,
  979. _("Register %d was not saved"), regnum);
  980. if (unavailable)
  981. throw_error (NOT_AVAILABLE_ERROR,
  982. _("Register %d is not available"), regnum);
  983. }
  984. void
  985. get_frame_register (struct frame_info *frame,
  986. int regnum, gdb_byte *buf)
  987. {
  988. frame_unwind_register (frame->next, regnum, buf);
  989. }
  990. struct value *
  991. frame_unwind_register_value (frame_info *next_frame, int regnum)
  992. {
  993. FRAME_SCOPED_DEBUG_ENTER_EXIT;
  994. gdb_assert (next_frame != NULL);
  995. gdbarch *gdbarch = frame_unwind_arch (next_frame);
  996. frame_debug_printf ("frame=%d, regnum=%d(%s)",
  997. next_frame->level, regnum,
  998. user_reg_map_regnum_to_name (gdbarch, regnum));
  999. /* Find the unwinder. */
  1000. if (next_frame->unwind == NULL)
  1001. frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
  1002. /* Ask this frame to unwind its register. */
  1003. value *value = next_frame->unwind->prev_register (next_frame,
  1004. &next_frame->prologue_cache,
  1005. regnum);
  1006. if (frame_debug)
  1007. {
  1008. string_file debug_file;
  1009. gdb_printf (&debug_file, " ->");
  1010. if (value_optimized_out (value))
  1011. {
  1012. gdb_printf (&debug_file, " ");
  1013. val_print_not_saved (&debug_file);
  1014. }
  1015. else
  1016. {
  1017. if (VALUE_LVAL (value) == lval_register)
  1018. gdb_printf (&debug_file, " register=%d",
  1019. VALUE_REGNUM (value));
  1020. else if (VALUE_LVAL (value) == lval_memory)
  1021. gdb_printf (&debug_file, " address=%s",
  1022. paddress (gdbarch,
  1023. value_address (value)));
  1024. else
  1025. gdb_printf (&debug_file, " computed");
  1026. if (value_lazy (value))
  1027. gdb_printf (&debug_file, " lazy");
  1028. else
  1029. {
  1030. int i;
  1031. gdb::array_view<const gdb_byte> buf = value_contents (value);
  1032. gdb_printf (&debug_file, " bytes=");
  1033. gdb_printf (&debug_file, "[");
  1034. for (i = 0; i < register_size (gdbarch, regnum); i++)
  1035. gdb_printf (&debug_file, "%02x", buf[i]);
  1036. gdb_printf (&debug_file, "]");
  1037. }
  1038. }
  1039. frame_debug_printf ("%s", debug_file.c_str ());
  1040. }
  1041. return value;
  1042. }
  1043. struct value *
  1044. get_frame_register_value (struct frame_info *frame, int regnum)
  1045. {
  1046. return frame_unwind_register_value (frame->next, regnum);
  1047. }
  1048. LONGEST
  1049. frame_unwind_register_signed (frame_info *next_frame, int regnum)
  1050. {
  1051. struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
  1052. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1053. struct value *value = frame_unwind_register_value (next_frame, regnum);
  1054. gdb_assert (value != NULL);
  1055. if (value_optimized_out (value))
  1056. {
  1057. throw_error (OPTIMIZED_OUT_ERROR,
  1058. _("Register %d was not saved"), regnum);
  1059. }
  1060. if (!value_entirely_available (value))
  1061. {
  1062. throw_error (NOT_AVAILABLE_ERROR,
  1063. _("Register %d is not available"), regnum);
  1064. }
  1065. LONGEST r = extract_signed_integer (value_contents_all (value), byte_order);
  1066. release_value (value);
  1067. return r;
  1068. }
  1069. LONGEST
  1070. get_frame_register_signed (struct frame_info *frame, int regnum)
  1071. {
  1072. return frame_unwind_register_signed (frame->next, regnum);
  1073. }
  1074. ULONGEST
  1075. frame_unwind_register_unsigned (frame_info *next_frame, int regnum)
  1076. {
  1077. struct gdbarch *gdbarch = frame_unwind_arch (next_frame);
  1078. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1079. int size = register_size (gdbarch, regnum);
  1080. struct value *value = frame_unwind_register_value (next_frame, regnum);
  1081. gdb_assert (value != NULL);
  1082. if (value_optimized_out (value))
  1083. {
  1084. throw_error (OPTIMIZED_OUT_ERROR,
  1085. _("Register %d was not saved"), regnum);
  1086. }
  1087. if (!value_entirely_available (value))
  1088. {
  1089. throw_error (NOT_AVAILABLE_ERROR,
  1090. _("Register %d is not available"), regnum);
  1091. }
  1092. ULONGEST r = extract_unsigned_integer (value_contents_all (value).data (),
  1093. size, byte_order);
  1094. release_value (value);
  1095. return r;
  1096. }
  1097. ULONGEST
  1098. get_frame_register_unsigned (struct frame_info *frame, int regnum)
  1099. {
  1100. return frame_unwind_register_unsigned (frame->next, regnum);
  1101. }
  1102. bool
  1103. read_frame_register_unsigned (frame_info *frame, int regnum,
  1104. ULONGEST *val)
  1105. {
  1106. struct value *regval = get_frame_register_value (frame, regnum);
  1107. if (!value_optimized_out (regval)
  1108. && value_entirely_available (regval))
  1109. {
  1110. struct gdbarch *gdbarch = get_frame_arch (frame);
  1111. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1112. int size = register_size (gdbarch, VALUE_REGNUM (regval));
  1113. *val = extract_unsigned_integer (value_contents (regval).data (), size,
  1114. byte_order);
  1115. return true;
  1116. }
  1117. return false;
  1118. }
  1119. void
  1120. put_frame_register (struct frame_info *frame, int regnum,
  1121. const gdb_byte *buf)
  1122. {
  1123. struct gdbarch *gdbarch = get_frame_arch (frame);
  1124. int realnum;
  1125. int optim;
  1126. int unavail;
  1127. enum lval_type lval;
  1128. CORE_ADDR addr;
  1129. frame_register (frame, regnum, &optim, &unavail,
  1130. &lval, &addr, &realnum, NULL);
  1131. if (optim)
  1132. error (_("Attempt to assign to a register that was not saved."));
  1133. switch (lval)
  1134. {
  1135. case lval_memory:
  1136. {
  1137. write_memory (addr, buf, register_size (gdbarch, regnum));
  1138. break;
  1139. }
  1140. case lval_register:
  1141. get_current_regcache ()->cooked_write (realnum, buf);
  1142. break;
  1143. default:
  1144. error (_("Attempt to assign to an unmodifiable value."));
  1145. }
  1146. }
  1147. /* This function is deprecated. Use get_frame_register_value instead,
  1148. which provides more accurate information.
  1149. Find and return the value of REGNUM for the specified stack frame.
  1150. The number of bytes copied is REGISTER_SIZE (REGNUM).
  1151. Returns 0 if the register value could not be found. */
  1152. bool
  1153. deprecated_frame_register_read (frame_info *frame, int regnum,
  1154. gdb_byte *myaddr)
  1155. {
  1156. int optimized;
  1157. int unavailable;
  1158. enum lval_type lval;
  1159. CORE_ADDR addr;
  1160. int realnum;
  1161. frame_register (frame, regnum, &optimized, &unavailable,
  1162. &lval, &addr, &realnum, myaddr);
  1163. return !optimized && !unavailable;
  1164. }
  1165. bool
  1166. get_frame_register_bytes (frame_info *frame, int regnum,
  1167. CORE_ADDR offset,
  1168. gdb::array_view<gdb_byte> buffer,
  1169. int *optimizedp, int *unavailablep)
  1170. {
  1171. struct gdbarch *gdbarch = get_frame_arch (frame);
  1172. int i;
  1173. int maxsize;
  1174. int numregs;
  1175. /* Skip registers wholly inside of OFFSET. */
  1176. while (offset >= register_size (gdbarch, regnum))
  1177. {
  1178. offset -= register_size (gdbarch, regnum);
  1179. regnum++;
  1180. }
  1181. /* Ensure that we will not read beyond the end of the register file.
  1182. This can only ever happen if the debug information is bad. */
  1183. maxsize = -offset;
  1184. numregs = gdbarch_num_cooked_regs (gdbarch);
  1185. for (i = regnum; i < numregs; i++)
  1186. {
  1187. int thissize = register_size (gdbarch, i);
  1188. if (thissize == 0)
  1189. break; /* This register is not available on this architecture. */
  1190. maxsize += thissize;
  1191. }
  1192. int len = buffer.size ();
  1193. if (len > maxsize)
  1194. error (_("Bad debug information detected: "
  1195. "Attempt to read %d bytes from registers."), len);
  1196. /* Copy the data. */
  1197. while (len > 0)
  1198. {
  1199. int curr_len = register_size (gdbarch, regnum) - offset;
  1200. if (curr_len > len)
  1201. curr_len = len;
  1202. gdb_byte *myaddr = buffer.data ();
  1203. if (curr_len == register_size (gdbarch, regnum))
  1204. {
  1205. enum lval_type lval;
  1206. CORE_ADDR addr;
  1207. int realnum;
  1208. frame_register (frame, regnum, optimizedp, unavailablep,
  1209. &lval, &addr, &realnum, myaddr);
  1210. if (*optimizedp || *unavailablep)
  1211. return false;
  1212. }
  1213. else
  1214. {
  1215. struct value *value = frame_unwind_register_value (frame->next,
  1216. regnum);
  1217. gdb_assert (value != NULL);
  1218. *optimizedp = value_optimized_out (value);
  1219. *unavailablep = !value_entirely_available (value);
  1220. if (*optimizedp || *unavailablep)
  1221. {
  1222. release_value (value);
  1223. return false;
  1224. }
  1225. memcpy (myaddr, value_contents_all (value).data () + offset,
  1226. curr_len);
  1227. release_value (value);
  1228. }
  1229. myaddr += curr_len;
  1230. len -= curr_len;
  1231. offset = 0;
  1232. regnum++;
  1233. }
  1234. *optimizedp = 0;
  1235. *unavailablep = 0;
  1236. return true;
  1237. }
  1238. void
  1239. put_frame_register_bytes (struct frame_info *frame, int regnum,
  1240. CORE_ADDR offset,
  1241. gdb::array_view<const gdb_byte> buffer)
  1242. {
  1243. struct gdbarch *gdbarch = get_frame_arch (frame);
  1244. /* Skip registers wholly inside of OFFSET. */
  1245. while (offset >= register_size (gdbarch, regnum))
  1246. {
  1247. offset -= register_size (gdbarch, regnum);
  1248. regnum++;
  1249. }
  1250. int len = buffer.size ();
  1251. /* Copy the data. */
  1252. while (len > 0)
  1253. {
  1254. int curr_len = register_size (gdbarch, regnum) - offset;
  1255. if (curr_len > len)
  1256. curr_len = len;
  1257. const gdb_byte *myaddr = buffer.data ();
  1258. if (curr_len == register_size (gdbarch, regnum))
  1259. {
  1260. put_frame_register (frame, regnum, myaddr);
  1261. }
  1262. else
  1263. {
  1264. struct value *value = frame_unwind_register_value (frame->next,
  1265. regnum);
  1266. gdb_assert (value != NULL);
  1267. memcpy ((char *) value_contents_writeable (value).data () + offset,
  1268. myaddr, curr_len);
  1269. put_frame_register (frame, regnum,
  1270. value_contents_raw (value).data ());
  1271. release_value (value);
  1272. }
  1273. myaddr += curr_len;
  1274. len -= curr_len;
  1275. offset = 0;
  1276. regnum++;
  1277. }
  1278. }
  1279. /* Create a sentinel frame. */
  1280. static struct frame_info *
  1281. create_sentinel_frame (struct program_space *pspace, struct regcache *regcache)
  1282. {
  1283. struct frame_info *frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
  1284. frame->level = -1;
  1285. frame->pspace = pspace;
  1286. frame->aspace = regcache->aspace ();
  1287. /* Explicitly initialize the sentinel frame's cache. Provide it
  1288. with the underlying regcache. In the future additional
  1289. information, such as the frame's thread will be added. */
  1290. frame->prologue_cache = sentinel_frame_cache (regcache);
  1291. /* For the moment there is only one sentinel frame implementation. */
  1292. frame->unwind = &sentinel_frame_unwind;
  1293. /* Link this frame back to itself. The frame is self referential
  1294. (the unwound PC is the same as the pc), so make it so. */
  1295. frame->next = frame;
  1296. /* The sentinel frame has a special ID. */
  1297. frame->this_id.p = frame_id_status::COMPUTED;
  1298. frame->this_id.value = sentinel_frame_id;
  1299. frame_debug_printf (" -> %s", frame->to_string ().c_str ());
  1300. return frame;
  1301. }
  1302. /* Cache for frame addresses already read by gdb. Valid only while
  1303. inferior is stopped. Control variables for the frame cache should
  1304. be local to this module. */
  1305. static struct obstack frame_cache_obstack;
  1306. void *
  1307. frame_obstack_zalloc (unsigned long size)
  1308. {
  1309. void *data = obstack_alloc (&frame_cache_obstack, size);
  1310. memset (data, 0, size);
  1311. return data;
  1312. }
  1313. static struct frame_info *get_prev_frame_always_1 (struct frame_info *this_frame);
  1314. struct frame_info *
  1315. get_current_frame (void)
  1316. {
  1317. struct frame_info *current_frame;
  1318. /* First check, and report, the lack of registers. Having GDB
  1319. report "No stack!" or "No memory" when the target doesn't even
  1320. have registers is very confusing. Besides, "printcmd.exp"
  1321. explicitly checks that ``print $pc'' with no registers prints "No
  1322. registers". */
  1323. if (!target_has_registers ())
  1324. error (_("No registers."));
  1325. if (!target_has_stack ())
  1326. error (_("No stack."));
  1327. if (!target_has_memory ())
  1328. error (_("No memory."));
  1329. /* Traceframes are effectively a substitute for the live inferior. */
  1330. if (get_traceframe_number () < 0)
  1331. validate_registers_access ();
  1332. if (sentinel_frame == NULL)
  1333. sentinel_frame =
  1334. create_sentinel_frame (current_program_space, get_current_regcache ());
  1335. /* Set the current frame before computing the frame id, to avoid
  1336. recursion inside compute_frame_id, in case the frame's
  1337. unwinder decides to do a symbol lookup (which depends on the
  1338. selected frame's block).
  1339. This call must always succeed. In particular, nothing inside
  1340. get_prev_frame_always_1 should try to unwind from the
  1341. sentinel frame, because that could fail/throw, and we always
  1342. want to leave with the current frame created and linked in --
  1343. we should never end up with the sentinel frame as outermost
  1344. frame. */
  1345. current_frame = get_prev_frame_always_1 (sentinel_frame);
  1346. gdb_assert (current_frame != NULL);
  1347. return current_frame;
  1348. }
  1349. /* The "selected" stack frame is used by default for local and arg
  1350. access.
  1351. The "single source of truth" for the selected frame is the
  1352. SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL pair.
  1353. Frame IDs can be saved/restored across reinitializing the frame
  1354. cache, while frame_info pointers can't (frame_info objects are
  1355. invalidated). If we know the corresponding frame_info object, it
  1356. is cached in SELECTED_FRAME.
  1357. If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
  1358. and the target has stack and is stopped, the selected frame is the
  1359. current (innermost) frame. This means that SELECTED_FRAME_LEVEL is
  1360. never 0 and SELECTED_FRAME_ID is never the ID of the innermost
  1361. frame.
  1362. If SELECTED_FRAME_ID / SELECTED_FRAME_LEVEL are null_frame_id / -1,
  1363. and the target has no stack or is executing, then there's no
  1364. selected frame. */
  1365. static frame_id selected_frame_id = null_frame_id;
  1366. static int selected_frame_level = -1;
  1367. /* The cached frame_info object pointing to the selected frame.
  1368. Looked up on demand by get_selected_frame. */
  1369. static struct frame_info *selected_frame;
  1370. /* See frame.h. */
  1371. void
  1372. save_selected_frame (frame_id *frame_id, int *frame_level)
  1373. noexcept
  1374. {
  1375. *frame_id = selected_frame_id;
  1376. *frame_level = selected_frame_level;
  1377. }
  1378. /* See frame.h. */
  1379. void
  1380. restore_selected_frame (frame_id frame_id, int frame_level)
  1381. noexcept
  1382. {
  1383. /* save_selected_frame never returns level == 0, so we shouldn't see
  1384. it here either. */
  1385. gdb_assert (frame_level != 0);
  1386. /* FRAME_ID can be null_frame_id only IFF frame_level is -1. */
  1387. gdb_assert ((frame_level == -1 && !frame_id_p (frame_id))
  1388. || (frame_level != -1 && frame_id_p (frame_id)));
  1389. selected_frame_id = frame_id;
  1390. selected_frame_level = frame_level;
  1391. /* Will be looked up later by get_selected_frame. */
  1392. selected_frame = nullptr;
  1393. }
  1394. /* See frame.h. */
  1395. void
  1396. lookup_selected_frame (struct frame_id a_frame_id, int frame_level)
  1397. {
  1398. struct frame_info *frame = NULL;
  1399. int count;
  1400. /* This either means there was no selected frame, or the selected
  1401. frame was the current frame. In either case, select the current
  1402. frame. */
  1403. if (frame_level == -1)
  1404. {
  1405. select_frame (get_current_frame ());
  1406. return;
  1407. }
  1408. /* select_frame never saves 0 in SELECTED_FRAME_LEVEL, so we
  1409. shouldn't see it here. */
  1410. gdb_assert (frame_level > 0);
  1411. /* Restore by level first, check if the frame id is the same as
  1412. expected. If that fails, try restoring by frame id. If that
  1413. fails, nothing to do, just warn the user. */
  1414. count = frame_level;
  1415. frame = find_relative_frame (get_current_frame (), &count);
  1416. if (count == 0
  1417. && frame != NULL
  1418. /* The frame ids must match - either both valid or both
  1419. outer_frame_id. The latter case is not failsafe, but since
  1420. it's highly unlikely the search by level finds the wrong
  1421. frame, it's 99.9(9)% of the time (for all practical purposes)
  1422. safe. */
  1423. && frame_id_eq (get_frame_id (frame), a_frame_id))
  1424. {
  1425. /* Cool, all is fine. */
  1426. select_frame (frame);
  1427. return;
  1428. }
  1429. frame = frame_find_by_id (a_frame_id);
  1430. if (frame != NULL)
  1431. {
  1432. /* Cool, refound it. */
  1433. select_frame (frame);
  1434. return;
  1435. }
  1436. /* Nothing else to do, the frame layout really changed. Select the
  1437. innermost stack frame. */
  1438. select_frame (get_current_frame ());
  1439. /* Warn the user. */
  1440. if (frame_level > 0 && !current_uiout->is_mi_like_p ())
  1441. {
  1442. warning (_("Couldn't restore frame #%d in "
  1443. "current thread. Bottom (innermost) frame selected:"),
  1444. frame_level);
  1445. /* For MI, we should probably have a notification about current
  1446. frame change. But this error is not very likely, so don't
  1447. bother for now. */
  1448. print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  1449. }
  1450. }
  1451. bool
  1452. has_stack_frames ()
  1453. {
  1454. if (!target_has_registers () || !target_has_stack ()
  1455. || !target_has_memory ())
  1456. return false;
  1457. /* Traceframes are effectively a substitute for the live inferior. */
  1458. if (get_traceframe_number () < 0)
  1459. {
  1460. /* No current inferior, no frame. */
  1461. if (inferior_ptid == null_ptid)
  1462. return false;
  1463. thread_info *tp = inferior_thread ();
  1464. /* Don't try to read from a dead thread. */
  1465. if (tp->state == THREAD_EXITED)
  1466. return false;
  1467. /* ... or from a spinning thread. */
  1468. if (tp->executing ())
  1469. return false;
  1470. }
  1471. return true;
  1472. }
  1473. /* See frame.h. */
  1474. struct frame_info *
  1475. get_selected_frame (const char *message)
  1476. {
  1477. if (selected_frame == NULL)
  1478. {
  1479. if (message != NULL && !has_stack_frames ())
  1480. error (("%s"), message);
  1481. lookup_selected_frame (selected_frame_id, selected_frame_level);
  1482. }
  1483. /* There is always a frame. */
  1484. gdb_assert (selected_frame != NULL);
  1485. return selected_frame;
  1486. }
  1487. /* This is a variant of get_selected_frame() which can be called when
  1488. the inferior does not have a frame; in that case it will return
  1489. NULL instead of calling error(). */
  1490. struct frame_info *
  1491. deprecated_safe_get_selected_frame (void)
  1492. {
  1493. if (!has_stack_frames ())
  1494. return NULL;
  1495. return get_selected_frame (NULL);
  1496. }
  1497. /* Select frame FI (or NULL - to invalidate the selected frame). */
  1498. void
  1499. select_frame (struct frame_info *fi)
  1500. {
  1501. selected_frame = fi;
  1502. selected_frame_level = frame_relative_level (fi);
  1503. if (selected_frame_level == 0)
  1504. {
  1505. /* Treat the current frame especially -- we want to always
  1506. save/restore it without warning, even if the frame ID changes
  1507. (see lookup_selected_frame). E.g.:
  1508. // The current frame is selected, the target had just stopped.
  1509. {
  1510. scoped_restore_selected_frame restore_frame;
  1511. some_operation_that_changes_the_stack ();
  1512. }
  1513. // scoped_restore_selected_frame's dtor runs, but the
  1514. // original frame_id can't be found. No matter whether it
  1515. // is found or not, we still end up with the now-current
  1516. // frame selected. Warning in lookup_selected_frame in this
  1517. // case seems pointless.
  1518. Also get_frame_id may access the target's registers/memory,
  1519. and thus skipping get_frame_id optimizes the common case.
  1520. Saving the selected frame this way makes get_selected_frame
  1521. and restore_current_frame return/re-select whatever frame is
  1522. the innermost (current) then. */
  1523. selected_frame_level = -1;
  1524. selected_frame_id = null_frame_id;
  1525. }
  1526. else
  1527. selected_frame_id = get_frame_id (fi);
  1528. /* NOTE: cagney/2002-05-04: FI can be NULL. This occurs when the
  1529. frame is being invalidated. */
  1530. /* FIXME: kseitz/2002-08-28: It would be nice to call
  1531. selected_frame_level_changed_event() right here, but due to limitations
  1532. in the current interfaces, we would end up flooding UIs with events
  1533. because select_frame() is used extensively internally.
  1534. Once we have frame-parameterized frame (and frame-related) commands,
  1535. the event notification can be moved here, since this function will only
  1536. be called when the user's selected frame is being changed. */
  1537. /* Ensure that symbols for this frame are read in. Also, determine the
  1538. source language of this frame, and switch to it if desired. */
  1539. if (fi)
  1540. {
  1541. CORE_ADDR pc;
  1542. /* We retrieve the frame's symtab by using the frame PC.
  1543. However we cannot use the frame PC as-is, because it usually
  1544. points to the instruction following the "call", which is
  1545. sometimes the first instruction of another function. So we
  1546. rely on get_frame_address_in_block() which provides us with a
  1547. PC which is guaranteed to be inside the frame's code
  1548. block. */
  1549. if (get_frame_address_in_block_if_available (fi, &pc))
  1550. {
  1551. struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
  1552. if (cust != NULL
  1553. && compunit_language (cust) != current_language->la_language
  1554. && compunit_language (cust) != language_unknown
  1555. && language_mode == language_mode_auto)
  1556. set_language (compunit_language (cust));
  1557. }
  1558. }
  1559. }
  1560. /* Create an arbitrary (i.e. address specified by user) or innermost frame.
  1561. Always returns a non-NULL value. */
  1562. struct frame_info *
  1563. create_new_frame (CORE_ADDR addr, CORE_ADDR pc)
  1564. {
  1565. struct frame_info *fi;
  1566. frame_debug_printf ("addr=%s, pc=%s", hex_string (addr), hex_string (pc));
  1567. fi = FRAME_OBSTACK_ZALLOC (struct frame_info);
  1568. fi->next = create_sentinel_frame (current_program_space,
  1569. get_current_regcache ());
  1570. /* Set/update this frame's cached PC value, found in the next frame.
  1571. Do this before looking for this frame's unwinder. A sniffer is
  1572. very likely to read this, and the corresponding unwinder is
  1573. entitled to rely that the PC doesn't magically change. */
  1574. fi->next->prev_pc.value = pc;
  1575. fi->next->prev_pc.status = CC_VALUE;
  1576. /* We currently assume that frame chain's can't cross spaces. */
  1577. fi->pspace = fi->next->pspace;
  1578. fi->aspace = fi->next->aspace;
  1579. /* Select/initialize both the unwind function and the frame's type
  1580. based on the PC. */
  1581. frame_unwind_find_by_frame (fi, &fi->prologue_cache);
  1582. fi->this_id.p = frame_id_status::COMPUTED;
  1583. fi->this_id.value = frame_id_build (addr, pc);
  1584. frame_debug_printf (" -> %s", fi->to_string ().c_str ());
  1585. return fi;
  1586. }
  1587. /* Return the frame that THIS_FRAME calls (NULL if THIS_FRAME is the
  1588. innermost frame). Be careful to not fall off the bottom of the
  1589. frame chain and onto the sentinel frame. */
  1590. struct frame_info *
  1591. get_next_frame (struct frame_info *this_frame)
  1592. {
  1593. if (this_frame->level > 0)
  1594. return this_frame->next;
  1595. else
  1596. return NULL;
  1597. }
  1598. /* Return the frame that THIS_FRAME calls. If THIS_FRAME is the
  1599. innermost (i.e. current) frame, return the sentinel frame. Thus,
  1600. unlike get_next_frame(), NULL will never be returned. */
  1601. struct frame_info *
  1602. get_next_frame_sentinel_okay (struct frame_info *this_frame)
  1603. {
  1604. gdb_assert (this_frame != NULL);
  1605. /* Note that, due to the manner in which the sentinel frame is
  1606. constructed, this_frame->next still works even when this_frame
  1607. is the sentinel frame. But we disallow it here anyway because
  1608. calling get_next_frame_sentinel_okay() on the sentinel frame
  1609. is likely a coding error. */
  1610. gdb_assert (this_frame != sentinel_frame);
  1611. return this_frame->next;
  1612. }
  1613. /* Observer for the target_changed event. */
  1614. static void
  1615. frame_observer_target_changed (struct target_ops *target)
  1616. {
  1617. reinit_frame_cache ();
  1618. }
  1619. /* Flush the entire frame cache. */
  1620. void
  1621. reinit_frame_cache (void)
  1622. {
  1623. struct frame_info *fi;
  1624. ++frame_cache_generation;
  1625. /* Tear down all frame caches. */
  1626. for (fi = sentinel_frame; fi != NULL; fi = fi->prev)
  1627. {
  1628. if (fi->prologue_cache && fi->unwind->dealloc_cache)
  1629. fi->unwind->dealloc_cache (fi, fi->prologue_cache);
  1630. if (fi->base_cache && fi->base->unwind->dealloc_cache)
  1631. fi->base->unwind->dealloc_cache (fi, fi->base_cache);
  1632. }
  1633. /* Since we can't really be sure what the first object allocated was. */
  1634. obstack_free (&frame_cache_obstack, 0);
  1635. obstack_init (&frame_cache_obstack);
  1636. if (sentinel_frame != NULL)
  1637. annotate_frames_invalid ();
  1638. sentinel_frame = NULL; /* Invalidate cache */
  1639. select_frame (NULL);
  1640. frame_stash_invalidate ();
  1641. frame_debug_printf ("generation=%d", frame_cache_generation);
  1642. }
  1643. /* Find where a register is saved (in memory or another register).
  1644. The result of frame_register_unwind is just where it is saved
  1645. relative to this particular frame. */
  1646. static void
  1647. frame_register_unwind_location (struct frame_info *this_frame, int regnum,
  1648. int *optimizedp, enum lval_type *lvalp,
  1649. CORE_ADDR *addrp, int *realnump)
  1650. {
  1651. gdb_assert (this_frame == NULL || this_frame->level >= 0);
  1652. while (this_frame != NULL)
  1653. {
  1654. int unavailable;
  1655. frame_register_unwind (this_frame, regnum, optimizedp, &unavailable,
  1656. lvalp, addrp, realnump, NULL);
  1657. if (*optimizedp)
  1658. break;
  1659. if (*lvalp != lval_register)
  1660. break;
  1661. regnum = *realnump;
  1662. this_frame = get_next_frame (this_frame);
  1663. }
  1664. }
  1665. /* Get the previous raw frame, and check that it is not identical to
  1666. same other frame frame already in the chain. If it is, there is
  1667. most likely a stack cycle, so we discard it, and mark THIS_FRAME as
  1668. outermost, with UNWIND_SAME_ID stop reason. Unlike the other
  1669. validity tests, that compare THIS_FRAME and the next frame, we do
  1670. this right after creating the previous frame, to avoid ever ending
  1671. up with two frames with the same id in the frame chain.
  1672. There is however, one case where this cycle detection is not desirable,
  1673. when asking for the previous frame of an inline frame, in this case, if
  1674. the previous frame is a duplicate and we return nullptr then we will be
  1675. unable to calculate the frame_id of the inline frame, this in turn
  1676. causes inline_frame_this_id() to fail. So for inline frames (and only
  1677. for inline frames), the previous frame will always be returned, even when it
  1678. has a duplicate frame_id. We're not worried about cycles in the frame
  1679. chain as, if the previous frame returned here has a duplicate frame_id,
  1680. then the frame_id of the inline frame, calculated based off the frame_id
  1681. of the previous frame, should also be a duplicate. */
  1682. static struct frame_info *
  1683. get_prev_frame_maybe_check_cycle (struct frame_info *this_frame)
  1684. {
  1685. struct frame_info *prev_frame = get_prev_frame_raw (this_frame);
  1686. /* Don't compute the frame id of the current frame yet. Unwinding
  1687. the sentinel frame can fail (e.g., if the thread is gone and we
  1688. can't thus read its registers). If we let the cycle detection
  1689. code below try to compute a frame ID, then an error thrown from
  1690. within the frame ID computation would result in the sentinel
  1691. frame as outermost frame, which is bogus. Instead, we'll compute
  1692. the current frame's ID lazily in get_frame_id. Note that there's
  1693. no point in doing cycle detection when there's only one frame, so
  1694. nothing is lost here. */
  1695. if (prev_frame->level == 0)
  1696. return prev_frame;
  1697. unsigned int entry_generation = get_frame_cache_generation ();
  1698. try
  1699. {
  1700. compute_frame_id (prev_frame);
  1701. bool cycle_detection_p = get_frame_type (this_frame) != INLINE_FRAME;
  1702. /* This assert checks GDB's state with respect to calculating the
  1703. frame-id of THIS_FRAME, in the case where THIS_FRAME is an inline
  1704. frame.
  1705. If THIS_FRAME is frame #0, and is an inline frame, then we put off
  1706. calculating the frame_id until we specifically make a call to
  1707. get_frame_id(). As a result we can enter this function in two
  1708. possible states. If GDB asked for the previous frame of frame #0
  1709. then THIS_FRAME will be frame #0 (an inline frame), and the
  1710. frame_id will be in the NOT_COMPUTED state. However, if GDB asked
  1711. for the frame_id of frame #0, then, as getting the frame_id of an
  1712. inline frame requires us to get the frame_id of the previous
  1713. frame, we will still end up in here, and the frame_id status will
  1714. be COMPUTING.
  1715. If, instead, THIS_FRAME is at a level greater than #0 then things
  1716. are simpler. For these frames we immediately compute the frame_id
  1717. when the frame is initially created, and so, for those frames, we
  1718. will always enter this function with the frame_id status of
  1719. COMPUTING. */
  1720. gdb_assert (cycle_detection_p
  1721. || (this_frame->level > 0
  1722. && (this_frame->this_id.p
  1723. == frame_id_status::COMPUTING))
  1724. || (this_frame->level == 0
  1725. && (this_frame->this_id.p
  1726. != frame_id_status::COMPUTED)));
  1727. /* We must do the CYCLE_DETECTION_P check after attempting to add
  1728. PREV_FRAME into the cache; if PREV_FRAME is unique then we do want
  1729. it in the cache, but if it is a duplicate and CYCLE_DETECTION_P is
  1730. false, then we don't want to unlink it. */
  1731. if (!frame_stash_add (prev_frame) && cycle_detection_p)
  1732. {
  1733. /* Another frame with the same id was already in the stash. We just
  1734. detected a cycle. */
  1735. frame_debug_printf (" -> nullptr // this frame has same ID");
  1736. this_frame->stop_reason = UNWIND_SAME_ID;
  1737. /* Unlink. */
  1738. prev_frame->next = NULL;
  1739. this_frame->prev = NULL;
  1740. prev_frame = NULL;
  1741. }
  1742. }
  1743. catch (const gdb_exception &ex)
  1744. {
  1745. if (get_frame_cache_generation () == entry_generation)
  1746. {
  1747. prev_frame->next = NULL;
  1748. this_frame->prev = NULL;
  1749. }
  1750. throw;
  1751. }
  1752. return prev_frame;
  1753. }
  1754. /* Helper function for get_prev_frame_always, this is called inside a
  1755. TRY_CATCH block. Return the frame that called THIS_FRAME or NULL if
  1756. there is no such frame. This may throw an exception. */
  1757. static struct frame_info *
  1758. get_prev_frame_always_1 (struct frame_info *this_frame)
  1759. {
  1760. FRAME_SCOPED_DEBUG_ENTER_EXIT;
  1761. gdb_assert (this_frame != NULL);
  1762. if (frame_debug)
  1763. {
  1764. if (this_frame != NULL)
  1765. frame_debug_printf ("this_frame=%d", this_frame->level);
  1766. else
  1767. frame_debug_printf ("this_frame=nullptr");
  1768. }
  1769. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1770. /* Only try to do the unwind once. */
  1771. if (this_frame->prev_p)
  1772. {
  1773. if (this_frame->prev != nullptr)
  1774. frame_debug_printf (" -> %s // cached",
  1775. this_frame->prev->to_string ().c_str ());
  1776. else
  1777. frame_debug_printf
  1778. (" -> nullptr // %s // cached",
  1779. frame_stop_reason_symbol_string (this_frame->stop_reason));
  1780. return this_frame->prev;
  1781. }
  1782. /* If the frame unwinder hasn't been selected yet, we must do so
  1783. before setting prev_p; otherwise the check for misbehaved
  1784. sniffers will think that this frame's sniffer tried to unwind
  1785. further (see frame_cleanup_after_sniffer). */
  1786. if (this_frame->unwind == NULL)
  1787. frame_unwind_find_by_frame (this_frame, &this_frame->prologue_cache);
  1788. this_frame->prev_p = true;
  1789. this_frame->stop_reason = UNWIND_NO_REASON;
  1790. /* If we are unwinding from an inline frame, all of the below tests
  1791. were already performed when we unwound from the next non-inline
  1792. frame. We must skip them, since we can not get THIS_FRAME's ID
  1793. until we have unwound all the way down to the previous non-inline
  1794. frame. */
  1795. if (get_frame_type (this_frame) == INLINE_FRAME)
  1796. return get_prev_frame_maybe_check_cycle (this_frame);
  1797. /* If this_frame is the current frame, then compute and stash its
  1798. frame id prior to fetching and computing the frame id of the
  1799. previous frame. Otherwise, the cycle detection code in
  1800. get_prev_frame_if_no_cycle() will not work correctly. When
  1801. get_frame_id() is called later on, an assertion error will be
  1802. triggered in the event of a cycle between the current frame and
  1803. its previous frame.
  1804. Note we do this after the INLINE_FRAME check above. That is
  1805. because the inline frame's frame id computation needs to fetch
  1806. the frame id of its previous real stack frame. I.e., we need to
  1807. avoid recursion in that case. This is OK since we're sure the
  1808. inline frame won't create a cycle with the real stack frame. See
  1809. inline_frame_this_id. */
  1810. if (this_frame->level == 0)
  1811. get_frame_id (this_frame);
  1812. /* Check that this frame is unwindable. If it isn't, don't try to
  1813. unwind to the prev frame. */
  1814. this_frame->stop_reason
  1815. = this_frame->unwind->stop_reason (this_frame,
  1816. &this_frame->prologue_cache);
  1817. if (this_frame->stop_reason != UNWIND_NO_REASON)
  1818. {
  1819. frame_debug_printf
  1820. (" -> nullptr // %s",
  1821. frame_stop_reason_symbol_string (this_frame->stop_reason));
  1822. return NULL;
  1823. }
  1824. /* Check that this frame's ID isn't inner to (younger, below, next)
  1825. the next frame. This happens when a frame unwind goes backwards.
  1826. This check is valid only if this frame and the next frame are NORMAL.
  1827. See the comment at frame_id_inner for details. */
  1828. if (get_frame_type (this_frame) == NORMAL_FRAME
  1829. && this_frame->next->unwind->type == NORMAL_FRAME
  1830. && frame_id_inner (get_frame_arch (this_frame->next),
  1831. get_frame_id (this_frame),
  1832. get_frame_id (this_frame->next)))
  1833. {
  1834. CORE_ADDR this_pc_in_block;
  1835. struct minimal_symbol *morestack_msym;
  1836. const char *morestack_name = NULL;
  1837. /* gcc -fsplit-stack __morestack can continue the stack anywhere. */
  1838. this_pc_in_block = get_frame_address_in_block (this_frame);
  1839. morestack_msym = lookup_minimal_symbol_by_pc (this_pc_in_block).minsym;
  1840. if (morestack_msym)
  1841. morestack_name = morestack_msym->linkage_name ();
  1842. if (!morestack_name || strcmp (morestack_name, "__morestack") != 0)
  1843. {
  1844. frame_debug_printf (" -> nullptr // this frame ID is inner");
  1845. this_frame->stop_reason = UNWIND_INNER_ID;
  1846. return NULL;
  1847. }
  1848. }
  1849. /* Check that this and the next frame do not unwind the PC register
  1850. to the same memory location. If they do, then even though they
  1851. have different frame IDs, the new frame will be bogus; two
  1852. functions can't share a register save slot for the PC. This can
  1853. happen when the prologue analyzer finds a stack adjustment, but
  1854. no PC save.
  1855. This check does assume that the "PC register" is roughly a
  1856. traditional PC, even if the gdbarch_unwind_pc method adjusts
  1857. it (we do not rely on the value, only on the unwound PC being
  1858. dependent on this value). A potential improvement would be
  1859. to have the frame prev_pc method and the gdbarch unwind_pc
  1860. method set the same lval and location information as
  1861. frame_register_unwind. */
  1862. if (this_frame->level > 0
  1863. && gdbarch_pc_regnum (gdbarch) >= 0
  1864. && get_frame_type (this_frame) == NORMAL_FRAME
  1865. && (get_frame_type (this_frame->next) == NORMAL_FRAME
  1866. || get_frame_type (this_frame->next) == INLINE_FRAME))
  1867. {
  1868. int optimized, realnum, nrealnum;
  1869. enum lval_type lval, nlval;
  1870. CORE_ADDR addr, naddr;
  1871. frame_register_unwind_location (this_frame,
  1872. gdbarch_pc_regnum (gdbarch),
  1873. &optimized, &lval, &addr, &realnum);
  1874. frame_register_unwind_location (get_next_frame (this_frame),
  1875. gdbarch_pc_regnum (gdbarch),
  1876. &optimized, &nlval, &naddr, &nrealnum);
  1877. if ((lval == lval_memory && lval == nlval && addr == naddr)
  1878. || (lval == lval_register && lval == nlval && realnum == nrealnum))
  1879. {
  1880. frame_debug_printf (" -> nullptr // no saved PC");
  1881. this_frame->stop_reason = UNWIND_NO_SAVED_PC;
  1882. this_frame->prev = NULL;
  1883. return NULL;
  1884. }
  1885. }
  1886. return get_prev_frame_maybe_check_cycle (this_frame);
  1887. }
  1888. /* Return a "struct frame_info" corresponding to the frame that called
  1889. THIS_FRAME. Returns NULL if there is no such frame.
  1890. Unlike get_prev_frame, this function always tries to unwind the
  1891. frame. */
  1892. struct frame_info *
  1893. get_prev_frame_always (struct frame_info *this_frame)
  1894. {
  1895. struct frame_info *prev_frame = NULL;
  1896. try
  1897. {
  1898. prev_frame = get_prev_frame_always_1 (this_frame);
  1899. }
  1900. catch (const gdb_exception_error &ex)
  1901. {
  1902. if (ex.error == MEMORY_ERROR)
  1903. {
  1904. this_frame->stop_reason = UNWIND_MEMORY_ERROR;
  1905. if (ex.message != NULL)
  1906. {
  1907. char *stop_string;
  1908. size_t size;
  1909. /* The error needs to live as long as the frame does.
  1910. Allocate using stack local STOP_STRING then assign the
  1911. pointer to the frame, this allows the STOP_STRING on the
  1912. frame to be of type 'const char *'. */
  1913. size = ex.message->size () + 1;
  1914. stop_string = (char *) frame_obstack_zalloc (size);
  1915. memcpy (stop_string, ex.what (), size);
  1916. this_frame->stop_string = stop_string;
  1917. }
  1918. prev_frame = NULL;
  1919. }
  1920. else
  1921. throw;
  1922. }
  1923. return prev_frame;
  1924. }
  1925. /* Construct a new "struct frame_info" and link it previous to
  1926. this_frame. */
  1927. static struct frame_info *
  1928. get_prev_frame_raw (struct frame_info *this_frame)
  1929. {
  1930. struct frame_info *prev_frame;
  1931. /* Allocate the new frame but do not wire it in to the frame chain.
  1932. Some (bad) code in INIT_FRAME_EXTRA_INFO tries to look along
  1933. frame->next to pull some fancy tricks (of course such code is, by
  1934. definition, recursive). Try to prevent it.
  1935. There is no reason to worry about memory leaks, should the
  1936. remainder of the function fail. The allocated memory will be
  1937. quickly reclaimed when the frame cache is flushed, and the `we've
  1938. been here before' check above will stop repeated memory
  1939. allocation calls. */
  1940. prev_frame = FRAME_OBSTACK_ZALLOC (struct frame_info);
  1941. prev_frame->level = this_frame->level + 1;
  1942. /* For now, assume we don't have frame chains crossing address
  1943. spaces. */
  1944. prev_frame->pspace = this_frame->pspace;
  1945. prev_frame->aspace = this_frame->aspace;
  1946. /* Don't yet compute ->unwind (and hence ->type). It is computed
  1947. on-demand in get_frame_type, frame_register_unwind, and
  1948. get_frame_id. */
  1949. /* Don't yet compute the frame's ID. It is computed on-demand by
  1950. get_frame_id(). */
  1951. /* The unwound frame ID is validate at the start of this function,
  1952. as part of the logic to decide if that frame should be further
  1953. unwound, and not here while the prev frame is being created.
  1954. Doing this makes it possible for the user to examine a frame that
  1955. has an invalid frame ID.
  1956. Some very old VAX code noted: [...] For the sake of argument,
  1957. suppose that the stack is somewhat trashed (which is one reason
  1958. that "info frame" exists). So, return 0 (indicating we don't
  1959. know the address of the arglist) if we don't know what frame this
  1960. frame calls. */
  1961. /* Link it in. */
  1962. this_frame->prev = prev_frame;
  1963. prev_frame->next = this_frame;
  1964. frame_debug_printf (" -> %s", prev_frame->to_string ().c_str ());
  1965. return prev_frame;
  1966. }
  1967. /* Debug routine to print a NULL frame being returned. */
  1968. static void
  1969. frame_debug_got_null_frame (struct frame_info *this_frame,
  1970. const char *reason)
  1971. {
  1972. if (frame_debug)
  1973. {
  1974. if (this_frame != NULL)
  1975. frame_debug_printf ("this_frame=%d -> %s", this_frame->level, reason);
  1976. else
  1977. frame_debug_printf ("this_frame=nullptr -> %s", reason);
  1978. }
  1979. }
  1980. /* Is this (non-sentinel) frame in the "main"() function? */
  1981. static bool
  1982. inside_main_func (frame_info *this_frame)
  1983. {
  1984. if (current_program_space->symfile_object_file == nullptr)
  1985. return false;
  1986. CORE_ADDR sym_addr;
  1987. const char *name = main_name ();
  1988. bound_minimal_symbol msymbol
  1989. = lookup_minimal_symbol (name, NULL,
  1990. current_program_space->symfile_object_file);
  1991. if (msymbol.minsym == nullptr)
  1992. {
  1993. /* In some language (for example Fortran) there will be no minimal
  1994. symbol with the name of the main function. In this case we should
  1995. search the full symbols to see if we can find a match. */
  1996. struct block_symbol bs = lookup_symbol (name, NULL, VAR_DOMAIN, 0);
  1997. if (bs.symbol == nullptr)
  1998. return false;
  1999. const struct block *block = SYMBOL_BLOCK_VALUE (bs.symbol);
  2000. gdb_assert (block != nullptr);
  2001. sym_addr = BLOCK_START (block);
  2002. }
  2003. else
  2004. sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  2005. /* Convert any function descriptor addresses into the actual function
  2006. code address. */
  2007. sym_addr = gdbarch_convert_from_func_ptr_addr
  2008. (get_frame_arch (this_frame), sym_addr, current_inferior ()->top_target ());
  2009. return sym_addr == get_frame_func (this_frame);
  2010. }
  2011. /* Test whether THIS_FRAME is inside the process entry point function. */
  2012. static bool
  2013. inside_entry_func (frame_info *this_frame)
  2014. {
  2015. CORE_ADDR entry_point;
  2016. if (!entry_point_address_query (&entry_point))
  2017. return false;
  2018. return get_frame_func (this_frame) == entry_point;
  2019. }
  2020. /* Return a structure containing various interesting information about
  2021. the frame that called THIS_FRAME. Returns NULL if there is entier
  2022. no such frame or the frame fails any of a set of target-independent
  2023. condition that should terminate the frame chain (e.g., as unwinding
  2024. past main()).
  2025. This function should not contain target-dependent tests, such as
  2026. checking whether the program-counter is zero. */
  2027. struct frame_info *
  2028. get_prev_frame (struct frame_info *this_frame)
  2029. {
  2030. FRAME_SCOPED_DEBUG_ENTER_EXIT;
  2031. CORE_ADDR frame_pc;
  2032. int frame_pc_p;
  2033. /* There is always a frame. If this assertion fails, suspect that
  2034. something should be calling get_selected_frame() or
  2035. get_current_frame(). */
  2036. gdb_assert (this_frame != NULL);
  2037. frame_pc_p = get_frame_pc_if_available (this_frame, &frame_pc);
  2038. /* tausq/2004-12-07: Dummy frames are skipped because it doesn't make much
  2039. sense to stop unwinding at a dummy frame. One place where a dummy
  2040. frame may have an address "inside_main_func" is on HPUX. On HPUX, the
  2041. pcsqh register (space register for the instruction at the head of the
  2042. instruction queue) cannot be written directly; the only way to set it
  2043. is to branch to code that is in the target space. In order to implement
  2044. frame dummies on HPUX, the called function is made to jump back to where
  2045. the inferior was when the user function was called. If gdb was inside
  2046. the main function when we created the dummy frame, the dummy frame will
  2047. point inside the main function. */
  2048. if (this_frame->level >= 0
  2049. && get_frame_type (this_frame) == NORMAL_FRAME
  2050. && !user_set_backtrace_options.backtrace_past_main
  2051. && frame_pc_p
  2052. && inside_main_func (this_frame))
  2053. /* Don't unwind past main(). Note, this is done _before_ the
  2054. frame has been marked as previously unwound. That way if the
  2055. user later decides to enable unwinds past main(), that will
  2056. automatically happen. */
  2057. {
  2058. frame_debug_got_null_frame (this_frame, "inside main func");
  2059. return NULL;
  2060. }
  2061. /* If the user's backtrace limit has been exceeded, stop. We must
  2062. add two to the current level; one of those accounts for backtrace_limit
  2063. being 1-based and the level being 0-based, and the other accounts for
  2064. the level of the new frame instead of the level of the current
  2065. frame. */
  2066. if (this_frame->level + 2 > user_set_backtrace_options.backtrace_limit)
  2067. {
  2068. frame_debug_got_null_frame (this_frame, "backtrace limit exceeded");
  2069. return NULL;
  2070. }
  2071. /* If we're already inside the entry function for the main objfile,
  2072. then it isn't valid. Don't apply this test to a dummy frame -
  2073. dummy frame PCs typically land in the entry func. Don't apply
  2074. this test to the sentinel frame. Sentinel frames should always
  2075. be allowed to unwind. */
  2076. /* NOTE: cagney/2003-07-07: Fixed a bug in inside_main_func() -
  2077. wasn't checking for "main" in the minimal symbols. With that
  2078. fixed asm-source tests now stop in "main" instead of halting the
  2079. backtrace in weird and wonderful ways somewhere inside the entry
  2080. file. Suspect that tests for inside the entry file/func were
  2081. added to work around that (now fixed) case. */
  2082. /* NOTE: cagney/2003-07-15: danielj (if I'm reading it right)
  2083. suggested having the inside_entry_func test use the
  2084. inside_main_func() msymbol trick (along with entry_point_address()
  2085. I guess) to determine the address range of the start function.
  2086. That should provide a far better stopper than the current
  2087. heuristics. */
  2088. /* NOTE: tausq/2004-10-09: this is needed if, for example, the compiler
  2089. applied tail-call optimizations to main so that a function called
  2090. from main returns directly to the caller of main. Since we don't
  2091. stop at main, we should at least stop at the entry point of the
  2092. application. */
  2093. if (this_frame->level >= 0
  2094. && get_frame_type (this_frame) == NORMAL_FRAME
  2095. && !user_set_backtrace_options.backtrace_past_entry
  2096. && frame_pc_p
  2097. && inside_entry_func (this_frame))
  2098. {
  2099. frame_debug_got_null_frame (this_frame, "inside entry func");
  2100. return NULL;
  2101. }
  2102. /* Assume that the only way to get a zero PC is through something
  2103. like a SIGSEGV or a dummy frame, and hence that NORMAL frames
  2104. will never unwind a zero PC. */
  2105. if (this_frame->level > 0
  2106. && (get_frame_type (this_frame) == NORMAL_FRAME
  2107. || get_frame_type (this_frame) == INLINE_FRAME)
  2108. && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME
  2109. && frame_pc_p && frame_pc == 0)
  2110. {
  2111. frame_debug_got_null_frame (this_frame, "zero PC");
  2112. return NULL;
  2113. }
  2114. return get_prev_frame_always (this_frame);
  2115. }
  2116. CORE_ADDR
  2117. get_frame_pc (struct frame_info *frame)
  2118. {
  2119. gdb_assert (frame->next != NULL);
  2120. return frame_unwind_pc (frame->next);
  2121. }
  2122. bool
  2123. get_frame_pc_if_available (frame_info *frame, CORE_ADDR *pc)
  2124. {
  2125. gdb_assert (frame->next != NULL);
  2126. try
  2127. {
  2128. *pc = frame_unwind_pc (frame->next);
  2129. }
  2130. catch (const gdb_exception_error &ex)
  2131. {
  2132. if (ex.error == NOT_AVAILABLE_ERROR)
  2133. return false;
  2134. else
  2135. throw;
  2136. }
  2137. return true;
  2138. }
  2139. /* Return an address that falls within THIS_FRAME's code block. */
  2140. CORE_ADDR
  2141. get_frame_address_in_block (struct frame_info *this_frame)
  2142. {
  2143. /* A draft address. */
  2144. CORE_ADDR pc = get_frame_pc (this_frame);
  2145. struct frame_info *next_frame = this_frame->next;
  2146. /* Calling get_frame_pc returns the resume address for THIS_FRAME.
  2147. Normally the resume address is inside the body of the function
  2148. associated with THIS_FRAME, but there is a special case: when
  2149. calling a function which the compiler knows will never return
  2150. (for instance abort), the call may be the very last instruction
  2151. in the calling function. The resume address will point after the
  2152. call and may be at the beginning of a different function
  2153. entirely.
  2154. If THIS_FRAME is a signal frame or dummy frame, then we should
  2155. not adjust the unwound PC. For a dummy frame, GDB pushed the
  2156. resume address manually onto the stack. For a signal frame, the
  2157. OS may have pushed the resume address manually and invoked the
  2158. handler (e.g. GNU/Linux), or invoked the trampoline which called
  2159. the signal handler - but in either case the signal handler is
  2160. expected to return to the trampoline. So in both of these
  2161. cases we know that the resume address is executable and
  2162. related. So we only need to adjust the PC if THIS_FRAME
  2163. is a normal function.
  2164. If the program has been interrupted while THIS_FRAME is current,
  2165. then clearly the resume address is inside the associated
  2166. function. There are three kinds of interruption: debugger stop
  2167. (next frame will be SENTINEL_FRAME), operating system
  2168. signal or exception (next frame will be SIGTRAMP_FRAME),
  2169. or debugger-induced function call (next frame will be
  2170. DUMMY_FRAME). So we only need to adjust the PC if
  2171. NEXT_FRAME is a normal function.
  2172. We check the type of NEXT_FRAME first, since it is already
  2173. known; frame type is determined by the unwinder, and since
  2174. we have THIS_FRAME we've already selected an unwinder for
  2175. NEXT_FRAME.
  2176. If the next frame is inlined, we need to keep going until we find
  2177. the real function - for instance, if a signal handler is invoked
  2178. while in an inlined function, then the code address of the
  2179. "calling" normal function should not be adjusted either. */
  2180. while (get_frame_type (next_frame) == INLINE_FRAME)
  2181. next_frame = next_frame->next;
  2182. if ((get_frame_type (next_frame) == NORMAL_FRAME
  2183. || get_frame_type (next_frame) == TAILCALL_FRAME)
  2184. && (get_frame_type (this_frame) == NORMAL_FRAME
  2185. || get_frame_type (this_frame) == TAILCALL_FRAME
  2186. || get_frame_type (this_frame) == INLINE_FRAME))
  2187. return pc - 1;
  2188. return pc;
  2189. }
  2190. bool
  2191. get_frame_address_in_block_if_available (frame_info *this_frame,
  2192. CORE_ADDR *pc)
  2193. {
  2194. try
  2195. {
  2196. *pc = get_frame_address_in_block (this_frame);
  2197. }
  2198. catch (const gdb_exception_error &ex)
  2199. {
  2200. if (ex.error == NOT_AVAILABLE_ERROR)
  2201. return false;
  2202. throw;
  2203. }
  2204. return true;
  2205. }
  2206. symtab_and_line
  2207. find_frame_sal (frame_info *frame)
  2208. {
  2209. struct frame_info *next_frame;
  2210. int notcurrent;
  2211. CORE_ADDR pc;
  2212. if (frame_inlined_callees (frame) > 0)
  2213. {
  2214. struct symbol *sym;
  2215. /* If the current frame has some inlined callees, and we have a next
  2216. frame, then that frame must be an inlined frame. In this case
  2217. this frame's sal is the "call site" of the next frame's inlined
  2218. function, which can not be inferred from get_frame_pc. */
  2219. next_frame = get_next_frame (frame);
  2220. if (next_frame)
  2221. sym = get_frame_function (next_frame);
  2222. else
  2223. sym = inline_skipped_symbol (inferior_thread ());
  2224. /* If frame is inline, it certainly has symbols. */
  2225. gdb_assert (sym);
  2226. symtab_and_line sal;
  2227. if (sym->line () != 0)
  2228. {
  2229. sal.symtab = symbol_symtab (sym);
  2230. sal.line = sym->line ();
  2231. }
  2232. else
  2233. /* If the symbol does not have a location, we don't know where
  2234. the call site is. Do not pretend to. This is jarring, but
  2235. we can't do much better. */
  2236. sal.pc = get_frame_pc (frame);
  2237. sal.pspace = get_frame_program_space (frame);
  2238. return sal;
  2239. }
  2240. /* If FRAME is not the innermost frame, that normally means that
  2241. FRAME->pc points at the return instruction (which is *after* the
  2242. call instruction), and we want to get the line containing the
  2243. call (because the call is where the user thinks the program is).
  2244. However, if the next frame is either a SIGTRAMP_FRAME or a
  2245. DUMMY_FRAME, then the next frame will contain a saved interrupt
  2246. PC and such a PC indicates the current (rather than next)
  2247. instruction/line, consequently, for such cases, want to get the
  2248. line containing fi->pc. */
  2249. if (!get_frame_pc_if_available (frame, &pc))
  2250. return {};
  2251. notcurrent = (pc != get_frame_address_in_block (frame));
  2252. return find_pc_line (pc, notcurrent);
  2253. }
  2254. /* Per "frame.h", return the ``address'' of the frame. Code should
  2255. really be using get_frame_id(). */
  2256. CORE_ADDR
  2257. get_frame_base (struct frame_info *fi)
  2258. {
  2259. return get_frame_id (fi).stack_addr;
  2260. }
  2261. /* High-level offsets into the frame. Used by the debug info. */
  2262. CORE_ADDR
  2263. get_frame_base_address (struct frame_info *fi)
  2264. {
  2265. if (get_frame_type (fi) != NORMAL_FRAME)
  2266. return 0;
  2267. if (fi->base == NULL)
  2268. fi->base = frame_base_find_by_frame (fi);
  2269. /* Sneaky: If the low-level unwind and high-level base code share a
  2270. common unwinder, let them share the prologue cache. */
  2271. if (fi->base->unwind == fi->unwind)
  2272. return fi->base->this_base (fi, &fi->prologue_cache);
  2273. return fi->base->this_base (fi, &fi->base_cache);
  2274. }
  2275. CORE_ADDR
  2276. get_frame_locals_address (struct frame_info *fi)
  2277. {
  2278. if (get_frame_type (fi) != NORMAL_FRAME)
  2279. return 0;
  2280. /* If there isn't a frame address method, find it. */
  2281. if (fi->base == NULL)
  2282. fi->base = frame_base_find_by_frame (fi);
  2283. /* Sneaky: If the low-level unwind and high-level base code share a
  2284. common unwinder, let them share the prologue cache. */
  2285. if (fi->base->unwind == fi->unwind)
  2286. return fi->base->this_locals (fi, &fi->prologue_cache);
  2287. return fi->base->this_locals (fi, &fi->base_cache);
  2288. }
  2289. CORE_ADDR
  2290. get_frame_args_address (struct frame_info *fi)
  2291. {
  2292. if (get_frame_type (fi) != NORMAL_FRAME)
  2293. return 0;
  2294. /* If there isn't a frame address method, find it. */
  2295. if (fi->base == NULL)
  2296. fi->base = frame_base_find_by_frame (fi);
  2297. /* Sneaky: If the low-level unwind and high-level base code share a
  2298. common unwinder, let them share the prologue cache. */
  2299. if (fi->base->unwind == fi->unwind)
  2300. return fi->base->this_args (fi, &fi->prologue_cache);
  2301. return fi->base->this_args (fi, &fi->base_cache);
  2302. }
  2303. /* Return true if the frame unwinder for frame FI is UNWINDER; false
  2304. otherwise. */
  2305. bool
  2306. frame_unwinder_is (frame_info *fi, const frame_unwind *unwinder)
  2307. {
  2308. if (fi->unwind == nullptr)
  2309. frame_unwind_find_by_frame (fi, &fi->prologue_cache);
  2310. return fi->unwind == unwinder;
  2311. }
  2312. /* Level of the selected frame: 0 for innermost, 1 for its caller, ...
  2313. or -1 for a NULL frame. */
  2314. int
  2315. frame_relative_level (struct frame_info *fi)
  2316. {
  2317. if (fi == NULL)
  2318. return -1;
  2319. else
  2320. return fi->level;
  2321. }
  2322. enum frame_type
  2323. get_frame_type (struct frame_info *frame)
  2324. {
  2325. if (frame->unwind == NULL)
  2326. /* Initialize the frame's unwinder because that's what
  2327. provides the frame's type. */
  2328. frame_unwind_find_by_frame (frame, &frame->prologue_cache);
  2329. return frame->unwind->type;
  2330. }
  2331. struct program_space *
  2332. get_frame_program_space (struct frame_info *frame)
  2333. {
  2334. return frame->pspace;
  2335. }
  2336. struct program_space *
  2337. frame_unwind_program_space (struct frame_info *this_frame)
  2338. {
  2339. gdb_assert (this_frame);
  2340. /* This is really a placeholder to keep the API consistent --- we
  2341. assume for now that we don't have frame chains crossing
  2342. spaces. */
  2343. return this_frame->pspace;
  2344. }
  2345. const address_space *
  2346. get_frame_address_space (struct frame_info *frame)
  2347. {
  2348. return frame->aspace;
  2349. }
  2350. /* Memory access methods. */
  2351. void
  2352. get_frame_memory (struct frame_info *this_frame, CORE_ADDR addr,
  2353. gdb::array_view<gdb_byte> buffer)
  2354. {
  2355. read_memory (addr, buffer.data (), buffer.size ());
  2356. }
  2357. LONGEST
  2358. get_frame_memory_signed (struct frame_info *this_frame, CORE_ADDR addr,
  2359. int len)
  2360. {
  2361. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2362. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2363. return read_memory_integer (addr, len, byte_order);
  2364. }
  2365. ULONGEST
  2366. get_frame_memory_unsigned (struct frame_info *this_frame, CORE_ADDR addr,
  2367. int len)
  2368. {
  2369. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2370. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2371. return read_memory_unsigned_integer (addr, len, byte_order);
  2372. }
  2373. bool
  2374. safe_frame_unwind_memory (struct frame_info *this_frame,
  2375. CORE_ADDR addr, gdb::array_view<gdb_byte> buffer)
  2376. {
  2377. /* NOTE: target_read_memory returns zero on success! */
  2378. return target_read_memory (addr, buffer.data (), buffer.size ()) == 0;
  2379. }
  2380. /* Architecture methods. */
  2381. struct gdbarch *
  2382. get_frame_arch (struct frame_info *this_frame)
  2383. {
  2384. return frame_unwind_arch (this_frame->next);
  2385. }
  2386. struct gdbarch *
  2387. frame_unwind_arch (struct frame_info *next_frame)
  2388. {
  2389. if (!next_frame->prev_arch.p)
  2390. {
  2391. struct gdbarch *arch;
  2392. if (next_frame->unwind == NULL)
  2393. frame_unwind_find_by_frame (next_frame, &next_frame->prologue_cache);
  2394. if (next_frame->unwind->prev_arch != NULL)
  2395. arch = next_frame->unwind->prev_arch (next_frame,
  2396. &next_frame->prologue_cache);
  2397. else
  2398. arch = get_frame_arch (next_frame);
  2399. next_frame->prev_arch.arch = arch;
  2400. next_frame->prev_arch.p = true;
  2401. frame_debug_printf ("next_frame=%d -> %s",
  2402. next_frame->level,
  2403. gdbarch_bfd_arch_info (arch)->printable_name);
  2404. }
  2405. return next_frame->prev_arch.arch;
  2406. }
  2407. struct gdbarch *
  2408. frame_unwind_caller_arch (struct frame_info *next_frame)
  2409. {
  2410. next_frame = skip_artificial_frames (next_frame);
  2411. /* We must have a non-artificial frame. The caller is supposed to check
  2412. the result of frame_unwind_caller_id (), which returns NULL_FRAME_ID
  2413. in this case. */
  2414. gdb_assert (next_frame != NULL);
  2415. return frame_unwind_arch (next_frame);
  2416. }
  2417. /* Gets the language of FRAME. */
  2418. enum language
  2419. get_frame_language (struct frame_info *frame)
  2420. {
  2421. CORE_ADDR pc = 0;
  2422. bool pc_p = false;
  2423. gdb_assert (frame!= NULL);
  2424. /* We determine the current frame language by looking up its
  2425. associated symtab. To retrieve this symtab, we use the frame
  2426. PC. However we cannot use the frame PC as is, because it
  2427. usually points to the instruction following the "call", which
  2428. is sometimes the first instruction of another function. So
  2429. we rely on get_frame_address_in_block(), it provides us with
  2430. a PC that is guaranteed to be inside the frame's code
  2431. block. */
  2432. try
  2433. {
  2434. pc = get_frame_address_in_block (frame);
  2435. pc_p = true;
  2436. }
  2437. catch (const gdb_exception_error &ex)
  2438. {
  2439. if (ex.error != NOT_AVAILABLE_ERROR)
  2440. throw;
  2441. }
  2442. if (pc_p)
  2443. {
  2444. struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
  2445. if (cust != NULL)
  2446. return compunit_language (cust);
  2447. }
  2448. return language_unknown;
  2449. }
  2450. /* Stack pointer methods. */
  2451. CORE_ADDR
  2452. get_frame_sp (struct frame_info *this_frame)
  2453. {
  2454. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2455. /* NOTE drow/2008-06-28: gdbarch_unwind_sp could be converted to
  2456. operate on THIS_FRAME now. */
  2457. return gdbarch_unwind_sp (gdbarch, this_frame->next);
  2458. }
  2459. /* Return the reason why we can't unwind past FRAME. */
  2460. enum unwind_stop_reason
  2461. get_frame_unwind_stop_reason (struct frame_info *frame)
  2462. {
  2463. /* Fill-in STOP_REASON. */
  2464. get_prev_frame_always (frame);
  2465. gdb_assert (frame->prev_p);
  2466. return frame->stop_reason;
  2467. }
  2468. /* Return a string explaining REASON. */
  2469. const char *
  2470. unwind_stop_reason_to_string (enum unwind_stop_reason reason)
  2471. {
  2472. switch (reason)
  2473. {
  2474. #define SET(name, description) \
  2475. case name: return _(description);
  2476. #include "unwind_stop_reasons.def"
  2477. #undef SET
  2478. default:
  2479. internal_error (__FILE__, __LINE__,
  2480. "Invalid frame stop reason");
  2481. }
  2482. }
  2483. const char *
  2484. frame_stop_reason_string (struct frame_info *fi)
  2485. {
  2486. gdb_assert (fi->prev_p);
  2487. gdb_assert (fi->prev == NULL);
  2488. /* Return the specific string if we have one. */
  2489. if (fi->stop_string != NULL)
  2490. return fi->stop_string;
  2491. /* Return the generic string if we have nothing better. */
  2492. return unwind_stop_reason_to_string (fi->stop_reason);
  2493. }
  2494. /* Return the enum symbol name of REASON as a string, to use in debug
  2495. output. */
  2496. static const char *
  2497. frame_stop_reason_symbol_string (enum unwind_stop_reason reason)
  2498. {
  2499. switch (reason)
  2500. {
  2501. #define SET(name, description) \
  2502. case name: return #name;
  2503. #include "unwind_stop_reasons.def"
  2504. #undef SET
  2505. default:
  2506. internal_error (__FILE__, __LINE__,
  2507. "Invalid frame stop reason");
  2508. }
  2509. }
  2510. /* Clean up after a failed (wrong unwinder) attempt to unwind past
  2511. FRAME. */
  2512. void
  2513. frame_cleanup_after_sniffer (struct frame_info *frame)
  2514. {
  2515. /* The sniffer should not allocate a prologue cache if it did not
  2516. match this frame. */
  2517. gdb_assert (frame->prologue_cache == NULL);
  2518. /* No sniffer should extend the frame chain; sniff based on what is
  2519. already certain. */
  2520. gdb_assert (!frame->prev_p);
  2521. /* The sniffer should not check the frame's ID; that's circular. */
  2522. gdb_assert (frame->this_id.p != frame_id_status::COMPUTED);
  2523. /* Clear cached fields dependent on the unwinder.
  2524. The previous PC is independent of the unwinder, but the previous
  2525. function is not (see get_frame_address_in_block). */
  2526. frame->prev_func.status = CC_UNKNOWN;
  2527. frame->prev_func.addr = 0;
  2528. /* Discard the unwinder last, so that we can easily find it if an assertion
  2529. in this function triggers. */
  2530. frame->unwind = NULL;
  2531. }
  2532. /* Set FRAME's unwinder temporarily, so that we can call a sniffer.
  2533. If sniffing fails, the caller should be sure to call
  2534. frame_cleanup_after_sniffer. */
  2535. void
  2536. frame_prepare_for_sniffer (struct frame_info *frame,
  2537. const struct frame_unwind *unwind)
  2538. {
  2539. gdb_assert (frame->unwind == NULL);
  2540. frame->unwind = unwind;
  2541. }
  2542. static struct cmd_list_element *set_backtrace_cmdlist;
  2543. static struct cmd_list_element *show_backtrace_cmdlist;
  2544. /* Definition of the "set backtrace" settings that are exposed as
  2545. "backtrace" command options. */
  2546. using boolean_option_def
  2547. = gdb::option::boolean_option_def<set_backtrace_options>;
  2548. const gdb::option::option_def set_backtrace_option_defs[] = {
  2549. boolean_option_def {
  2550. "past-main",
  2551. [] (set_backtrace_options *opt) { return &opt->backtrace_past_main; },
  2552. show_backtrace_past_main, /* show_cmd_cb */
  2553. N_("Set whether backtraces should continue past \"main\"."),
  2554. N_("Show whether backtraces should continue past \"main\"."),
  2555. N_("Normally the caller of \"main\" is not of interest, so GDB will terminate\n\
  2556. the backtrace at \"main\". Set this if you need to see the rest\n\
  2557. of the stack trace."),
  2558. },
  2559. boolean_option_def {
  2560. "past-entry",
  2561. [] (set_backtrace_options *opt) { return &opt->backtrace_past_entry; },
  2562. show_backtrace_past_entry, /* show_cmd_cb */
  2563. N_("Set whether backtraces should continue past the entry point of a program."),
  2564. N_("Show whether backtraces should continue past the entry point of a program."),
  2565. N_("Normally there are no callers beyond the entry point of a program, so GDB\n\
  2566. will terminate the backtrace there. Set this if you need to see\n\
  2567. the rest of the stack trace."),
  2568. },
  2569. };
  2570. void _initialize_frame ();
  2571. void
  2572. _initialize_frame ()
  2573. {
  2574. obstack_init (&frame_cache_obstack);
  2575. frame_stash_create ();
  2576. gdb::observers::target_changed.attach (frame_observer_target_changed,
  2577. "frame");
  2578. add_setshow_prefix_cmd ("backtrace", class_maintenance,
  2579. _("\
  2580. Set backtrace specific variables.\n\
  2581. Configure backtrace variables such as the backtrace limit"),
  2582. _("\
  2583. Show backtrace specific variables.\n\
  2584. Show backtrace variables such as the backtrace limit."),
  2585. &set_backtrace_cmdlist, &show_backtrace_cmdlist,
  2586. &setlist, &showlist);
  2587. add_setshow_uinteger_cmd ("limit", class_obscure,
  2588. &user_set_backtrace_options.backtrace_limit, _("\
  2589. Set an upper bound on the number of backtrace levels."), _("\
  2590. Show the upper bound on the number of backtrace levels."), _("\
  2591. No more than the specified number of frames can be displayed or examined.\n\
  2592. Literal \"unlimited\" or zero means no limit."),
  2593. NULL,
  2594. show_backtrace_limit,
  2595. &set_backtrace_cmdlist,
  2596. &show_backtrace_cmdlist);
  2597. gdb::option::add_setshow_cmds_for_options
  2598. (class_stack, &user_set_backtrace_options,
  2599. set_backtrace_option_defs, &set_backtrace_cmdlist, &show_backtrace_cmdlist);
  2600. /* Debug this files internals. */
  2601. add_setshow_boolean_cmd ("frame", class_maintenance, &frame_debug, _("\
  2602. Set frame debugging."), _("\
  2603. Show frame debugging."), _("\
  2604. When non-zero, frame specific internal debugging is enabled."),
  2605. NULL,
  2606. show_frame_debug,
  2607. &setdebuglist, &showdebuglist);
  2608. }