stack.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583
  1. /* Print and select stack 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 "value.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "expression.h"
  19. #include "language.h"
  20. #include "frame.h"
  21. #include "gdbcmd.h"
  22. #include "gdbcore.h"
  23. #include "target.h"
  24. #include "source.h"
  25. #include "breakpoint.h"
  26. #include "demangle.h"
  27. #include "inferior.h"
  28. #include "annotate.h"
  29. #include "ui-out.h"
  30. #include "block.h"
  31. #include "stack.h"
  32. #include "dictionary.h"
  33. #include "reggroups.h"
  34. #include "regcache.h"
  35. #include "solib.h"
  36. #include "valprint.h"
  37. #include "gdbthread.h"
  38. #include "cp-support.h"
  39. #include "disasm.h"
  40. #include "inline-frame.h"
  41. #include "linespec.h"
  42. #include "cli/cli-utils.h"
  43. #include "objfiles.h"
  44. #include "annotate.h"
  45. #include "symfile.h"
  46. #include "extension.h"
  47. #include "observable.h"
  48. #include "gdbsupport/def-vector.h"
  49. #include "cli/cli-option.h"
  50. #include "cli/cli-style.h"
  51. #include "gdbsupport/buildargv.h"
  52. /* The possible choices of "set print frame-arguments", and the value
  53. of this setting. */
  54. const char print_frame_arguments_all[] = "all";
  55. const char print_frame_arguments_scalars[] = "scalars";
  56. const char print_frame_arguments_none[] = "none";
  57. const char print_frame_arguments_presence[] = "presence";
  58. static const char *const print_frame_arguments_choices[] =
  59. {
  60. print_frame_arguments_all,
  61. print_frame_arguments_scalars,
  62. print_frame_arguments_none,
  63. print_frame_arguments_presence,
  64. NULL
  65. };
  66. /* The possible choices of "set print frame-info", and the value
  67. of this setting. */
  68. const char print_frame_info_auto[] = "auto";
  69. const char print_frame_info_source_line[] = "source-line";
  70. const char print_frame_info_location[] = "location";
  71. const char print_frame_info_source_and_location[] = "source-and-location";
  72. const char print_frame_info_location_and_address[] = "location-and-address";
  73. const char print_frame_info_short_location[] = "short-location";
  74. static const char *const print_frame_info_choices[] =
  75. {
  76. print_frame_info_auto,
  77. print_frame_info_source_line,
  78. print_frame_info_location,
  79. print_frame_info_source_and_location,
  80. print_frame_info_location_and_address,
  81. print_frame_info_short_location,
  82. NULL
  83. };
  84. /* print_frame_info_print_what[i] maps a choice to the corresponding
  85. print_what enum. */
  86. static const gdb::optional<enum print_what> print_frame_info_print_what[] =
  87. {{}, /* Empty value for "auto". */
  88. SRC_LINE, LOCATION, SRC_AND_LOC, LOC_AND_ADDRESS, SHORT_LOCATION};
  89. /* The possible choices of "set print entry-values", and the value
  90. of this setting. */
  91. const char print_entry_values_no[] = "no";
  92. const char print_entry_values_only[] = "only";
  93. const char print_entry_values_preferred[] = "preferred";
  94. const char print_entry_values_if_needed[] = "if-needed";
  95. const char print_entry_values_both[] = "both";
  96. const char print_entry_values_compact[] = "compact";
  97. const char print_entry_values_default[] = "default";
  98. static const char *const print_entry_values_choices[] =
  99. {
  100. print_entry_values_no,
  101. print_entry_values_only,
  102. print_entry_values_preferred,
  103. print_entry_values_if_needed,
  104. print_entry_values_both,
  105. print_entry_values_compact,
  106. print_entry_values_default,
  107. NULL
  108. };
  109. /* See frame.h. */
  110. frame_print_options user_frame_print_options;
  111. /* Option definitions for some frame-related "set print ..."
  112. settings. */
  113. using boolean_option_def
  114. = gdb::option::boolean_option_def<frame_print_options>;
  115. using enum_option_def
  116. = gdb::option::enum_option_def<frame_print_options>;
  117. static const gdb::option::option_def frame_print_option_defs[] = {
  118. enum_option_def {
  119. "entry-values",
  120. print_entry_values_choices,
  121. [] (frame_print_options *opt) { return &opt->print_entry_values; },
  122. NULL, /* show_cmd_cb */
  123. N_("Set printing of function arguments at function entry."),
  124. N_("Show printing of function arguments at function entry."),
  125. N_("GDB can sometimes determine the values of function arguments at entry,\n\
  126. in addition to their current values. This option tells GDB whether\n\
  127. to print the current value, the value at entry (marked as val@entry),\n\
  128. or both. Note that one or both of these values may be <optimized out>."),
  129. },
  130. enum_option_def {
  131. "frame-arguments",
  132. print_frame_arguments_choices,
  133. [] (frame_print_options *opt) { return &opt->print_frame_arguments; },
  134. NULL, /* show_cmd_cb */
  135. N_("Set printing of non-scalar frame arguments."),
  136. N_("Show printing of non-scalar frame arguments."),
  137. NULL /* help_doc */
  138. },
  139. boolean_option_def {
  140. "raw-frame-arguments",
  141. [] (frame_print_options *opt) { return &opt->print_raw_frame_arguments; },
  142. NULL, /* show_cmd_cb */
  143. N_("Set whether to print frame arguments in raw form."),
  144. N_("Show whether to print frame arguments in raw form."),
  145. N_("If set, frame arguments are printed in raw form, bypassing any\n\
  146. pretty-printers for that value.")
  147. },
  148. enum_option_def {
  149. "frame-info",
  150. print_frame_info_choices,
  151. [] (frame_print_options *opt) { return &opt->print_frame_info; },
  152. NULL, /* show_cmd_cb */
  153. N_("Set printing of frame information."),
  154. N_("Show printing of frame information."),
  155. NULL /* help_doc */
  156. }
  157. };
  158. /* Options for the "backtrace" command. */
  159. struct backtrace_cmd_options
  160. {
  161. bool full = false;
  162. bool no_filters = false;
  163. bool hide = false;
  164. };
  165. using bt_flag_option_def
  166. = gdb::option::flag_option_def<backtrace_cmd_options>;
  167. static const gdb::option::option_def backtrace_command_option_defs[] = {
  168. bt_flag_option_def {
  169. "full",
  170. [] (backtrace_cmd_options *opt) { return &opt->full; },
  171. N_("Print values of local variables.")
  172. },
  173. bt_flag_option_def {
  174. "no-filters",
  175. [] (backtrace_cmd_options *opt) { return &opt->no_filters; },
  176. N_("Prohibit frame filters from executing on a backtrace."),
  177. },
  178. bt_flag_option_def {
  179. "hide",
  180. [] (backtrace_cmd_options *opt) { return &opt->hide; },
  181. N_("Causes Python frame filter elided frames to not be printed."),
  182. },
  183. };
  184. /* Prototypes for local functions. */
  185. static void print_frame_local_vars (struct frame_info *frame,
  186. bool quiet,
  187. const char *regexp, const char *t_regexp,
  188. int num_tabs, struct ui_file *stream);
  189. static void print_frame (const frame_print_options &opts,
  190. frame_info *frame, int print_level,
  191. enum print_what print_what, int print_args,
  192. struct symtab_and_line sal);
  193. static struct frame_info *find_frame_for_function (const char *);
  194. static struct frame_info *find_frame_for_address (CORE_ADDR);
  195. /* Zero means do things normally; we are interacting directly with the
  196. user. One means print the full filename and linenumber when a
  197. frame is printed, and do so in a format emacs18/emacs19.22 can
  198. parse. Two means print similar annotations, but in many more
  199. cases and in a slightly different syntax. */
  200. int annotation_level = 0;
  201. /* Class used to manage tracking the last symtab we displayed. */
  202. class last_displayed_symtab_info_type
  203. {
  204. public:
  205. /* True if the cached information is valid. */
  206. bool is_valid () const
  207. { return m_valid; }
  208. /* Return the cached program_space. If the cache is invalid nullptr is
  209. returned. */
  210. struct program_space *pspace () const
  211. { return m_pspace; }
  212. /* Return the cached CORE_ADDR address. If the cache is invalid 0 is
  213. returned. */
  214. CORE_ADDR address () const
  215. { return m_address; }
  216. /* Return the cached symtab. If the cache is invalid nullptr is
  217. returned. */
  218. struct symtab *symtab () const
  219. { return m_symtab; }
  220. /* Return the cached line number. If the cache is invalid 0 is
  221. returned. */
  222. int line () const
  223. { return m_line; }
  224. /* Invalidate the cache, reset all the members to their default value. */
  225. void invalidate ()
  226. {
  227. m_valid = false;
  228. m_pspace = nullptr;
  229. m_address = 0;
  230. m_symtab = nullptr;
  231. m_line = 0;
  232. }
  233. /* Store a new set of values in the cache. */
  234. void set (struct program_space *pspace, CORE_ADDR address,
  235. struct symtab *symtab, int line)
  236. {
  237. gdb_assert (pspace != nullptr);
  238. m_valid = true;
  239. m_pspace = pspace;
  240. m_address = address;
  241. m_symtab = symtab;
  242. m_line = line;
  243. }
  244. private:
  245. /* True when the cache is valid. */
  246. bool m_valid = false;
  247. /* The last program space displayed. */
  248. struct program_space *m_pspace = nullptr;
  249. /* The last address displayed. */
  250. CORE_ADDR m_address = 0;
  251. /* The last symtab displayed. */
  252. struct symtab *m_symtab = nullptr;
  253. /* The last line number displayed. */
  254. int m_line = 0;
  255. };
  256. /* An actual instance of the cache, holds information about the last symtab
  257. displayed. */
  258. static last_displayed_symtab_info_type last_displayed_symtab_info;
  259. /* See stack.h. */
  260. bool
  261. frame_show_address (struct frame_info *frame,
  262. struct symtab_and_line sal)
  263. {
  264. /* If there is a line number, but no PC, then there is no location
  265. information associated with this sal. The only way that should
  266. happen is for the call sites of inlined functions (SAL comes from
  267. find_frame_sal). Otherwise, we would have some PC range if the
  268. SAL came from a line table. */
  269. if (sal.line != 0 && sal.pc == 0 && sal.end == 0)
  270. {
  271. if (get_next_frame (frame) == NULL)
  272. gdb_assert (inline_skipped_frames (inferior_thread ()) > 0);
  273. else
  274. gdb_assert (get_frame_type (get_next_frame (frame)) == INLINE_FRAME);
  275. return false;
  276. }
  277. return get_frame_pc (frame) != sal.pc || !sal.is_stmt;
  278. }
  279. /* See frame.h. */
  280. void
  281. print_stack_frame_to_uiout (struct ui_out *uiout, struct frame_info *frame,
  282. int print_level, enum print_what print_what,
  283. int set_current_sal)
  284. {
  285. scoped_restore save_uiout = make_scoped_restore (&current_uiout, uiout);
  286. print_stack_frame (frame, print_level, print_what, set_current_sal);
  287. }
  288. /* Show or print a stack frame FRAME briefly. The output is formatted
  289. according to PRINT_LEVEL and PRINT_WHAT printing the frame's
  290. relative level, function name, argument list, and file name and
  291. line number. If the frame's PC is not at the beginning of the
  292. source line, the actual PC is printed at the beginning. */
  293. void
  294. print_stack_frame (struct frame_info *frame, int print_level,
  295. enum print_what print_what,
  296. int set_current_sal)
  297. {
  298. /* For mi, always print location and address. */
  299. if (current_uiout->is_mi_like_p ())
  300. print_what = LOC_AND_ADDRESS;
  301. try
  302. {
  303. print_frame_info (user_frame_print_options,
  304. frame, print_level, print_what, 1 /* print_args */,
  305. set_current_sal);
  306. if (set_current_sal)
  307. set_current_sal_from_frame (frame);
  308. }
  309. catch (const gdb_exception_error &e)
  310. {
  311. }
  312. }
  313. /* Print nameless arguments of frame FRAME on STREAM, where START is
  314. the offset of the first nameless argument, and NUM is the number of
  315. nameless arguments to print. FIRST is nonzero if this is the first
  316. argument (not just the first nameless argument). */
  317. static void
  318. print_frame_nameless_args (struct frame_info *frame, long start, int num,
  319. int first, struct ui_file *stream)
  320. {
  321. struct gdbarch *gdbarch = get_frame_arch (frame);
  322. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  323. int i;
  324. CORE_ADDR argsaddr;
  325. long arg_value;
  326. for (i = 0; i < num; i++)
  327. {
  328. QUIT;
  329. argsaddr = get_frame_args_address (frame);
  330. if (!argsaddr)
  331. return;
  332. arg_value = read_memory_integer (argsaddr + start,
  333. sizeof (int), byte_order);
  334. if (!first)
  335. gdb_printf (stream, ", ");
  336. gdb_printf (stream, "%ld", arg_value);
  337. first = 0;
  338. start += sizeof (int);
  339. }
  340. }
  341. /* Print single argument of inferior function. ARG must be already
  342. read in.
  343. Errors are printed as if they would be the parameter value. Use zeroed ARG
  344. iff it should not be printed according to user settings. */
  345. static void
  346. print_frame_arg (const frame_print_options &fp_opts,
  347. const struct frame_arg *arg)
  348. {
  349. struct ui_out *uiout = current_uiout;
  350. string_file stb;
  351. gdb_assert (!arg->val || !arg->error);
  352. gdb_assert (arg->entry_kind == print_entry_values_no
  353. || arg->entry_kind == print_entry_values_only
  354. || (!uiout->is_mi_like_p ()
  355. && arg->entry_kind == print_entry_values_compact));
  356. annotate_arg_emitter arg_emitter;
  357. ui_out_emit_tuple tuple_emitter (uiout, NULL);
  358. gdb_puts (arg->sym->print_name (), &stb);
  359. if (arg->entry_kind == print_entry_values_compact)
  360. {
  361. /* It is OK to provide invalid MI-like stream as with
  362. PRINT_ENTRY_VALUE_COMPACT we never use MI. */
  363. stb.puts ("=");
  364. gdb_puts (arg->sym->print_name (), &stb);
  365. }
  366. if (arg->entry_kind == print_entry_values_only
  367. || arg->entry_kind == print_entry_values_compact)
  368. stb.puts ("@entry");
  369. uiout->field_stream ("name", stb, variable_name_style.style ());
  370. annotate_arg_name_end ();
  371. uiout->text ("=");
  372. ui_file_style style;
  373. if (!arg->val && !arg->error)
  374. uiout->text ("...");
  375. else
  376. {
  377. if (arg->error)
  378. {
  379. stb.printf (_("<error reading variable: %s>"), arg->error.get ());
  380. style = metadata_style.style ();
  381. }
  382. else
  383. {
  384. try
  385. {
  386. const struct language_defn *language;
  387. struct value_print_options vp_opts;
  388. /* Avoid value_print because it will deref ref parameters. We
  389. just want to print their addresses. Print ??? for args whose
  390. address we do not know. We pass 2 as "recurse" to val_print
  391. because our standard indentation here is 4 spaces, and
  392. val_print indents 2 for each recurse. */
  393. annotate_arg_value (value_type (arg->val));
  394. /* Use the appropriate language to display our symbol, unless the
  395. user forced the language to a specific language. */
  396. if (language_mode == language_mode_auto)
  397. language = language_def (arg->sym->language ());
  398. else
  399. language = current_language;
  400. get_no_prettyformat_print_options (&vp_opts);
  401. vp_opts.deref_ref = 1;
  402. vp_opts.raw = fp_opts.print_raw_frame_arguments;
  403. /* True in "summary" mode, false otherwise. */
  404. vp_opts.summary
  405. = fp_opts.print_frame_arguments == print_frame_arguments_scalars;
  406. common_val_print_checked (arg->val, &stb, 2, &vp_opts, language);
  407. }
  408. catch (const gdb_exception_error &except)
  409. {
  410. stb.printf (_("<error reading variable: %s>"),
  411. except.what ());
  412. style = metadata_style.style ();
  413. }
  414. }
  415. }
  416. uiout->field_stream ("value", stb, style);
  417. }
  418. /* Read in inferior function local SYM at FRAME into ARGP. Caller is
  419. responsible for xfree of ARGP->ERROR. This function never throws an
  420. exception. */
  421. void
  422. read_frame_local (struct symbol *sym, struct frame_info *frame,
  423. struct frame_arg *argp)
  424. {
  425. argp->sym = sym;
  426. argp->val = NULL;
  427. argp->error = NULL;
  428. try
  429. {
  430. argp->val = read_var_value (sym, NULL, frame);
  431. }
  432. catch (const gdb_exception_error &except)
  433. {
  434. argp->error.reset (xstrdup (except.what ()));
  435. }
  436. }
  437. /* Read in inferior function parameter SYM at FRAME into ARGP. This
  438. function never throws an exception. */
  439. void
  440. read_frame_arg (const frame_print_options &fp_opts,
  441. symbol *sym, frame_info *frame,
  442. struct frame_arg *argp, struct frame_arg *entryargp)
  443. {
  444. struct value *val = NULL, *entryval = NULL;
  445. char *val_error = NULL, *entryval_error = NULL;
  446. int val_equal = 0;
  447. if (fp_opts.print_entry_values != print_entry_values_only
  448. && fp_opts.print_entry_values != print_entry_values_preferred)
  449. {
  450. try
  451. {
  452. val = read_var_value (sym, NULL, frame);
  453. }
  454. catch (const gdb_exception_error &except)
  455. {
  456. val_error = (char *) alloca (except.message->size () + 1);
  457. strcpy (val_error, except.what ());
  458. }
  459. }
  460. if (SYMBOL_COMPUTED_OPS (sym) != NULL
  461. && SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry != NULL
  462. && fp_opts.print_entry_values != print_entry_values_no
  463. && (fp_opts.print_entry_values != print_entry_values_if_needed
  464. || !val || value_optimized_out (val)))
  465. {
  466. try
  467. {
  468. const struct symbol_computed_ops *ops;
  469. ops = SYMBOL_COMPUTED_OPS (sym);
  470. entryval = ops->read_variable_at_entry (sym, frame);
  471. }
  472. catch (const gdb_exception_error &except)
  473. {
  474. if (except.error != NO_ENTRY_VALUE_ERROR)
  475. {
  476. entryval_error = (char *) alloca (except.message->size () + 1);
  477. strcpy (entryval_error, except.what ());
  478. }
  479. }
  480. if (entryval != NULL && value_optimized_out (entryval))
  481. entryval = NULL;
  482. if (fp_opts.print_entry_values == print_entry_values_compact
  483. || fp_opts.print_entry_values == print_entry_values_default)
  484. {
  485. /* For MI do not try to use print_entry_values_compact for ARGP. */
  486. if (val && entryval && !current_uiout->is_mi_like_p ())
  487. {
  488. struct type *type = value_type (val);
  489. if (value_lazy (val))
  490. value_fetch_lazy (val);
  491. if (value_lazy (entryval))
  492. value_fetch_lazy (entryval);
  493. if (value_contents_eq (val, 0, entryval, 0, TYPE_LENGTH (type)))
  494. {
  495. /* Initialize it just to avoid a GCC false warning. */
  496. struct value *val_deref = NULL, *entryval_deref;
  497. /* DW_AT_call_value does match with the current
  498. value. If it is a reference still try to verify if
  499. dereferenced DW_AT_call_data_value does not differ. */
  500. try
  501. {
  502. struct type *type_deref;
  503. val_deref = coerce_ref (val);
  504. if (value_lazy (val_deref))
  505. value_fetch_lazy (val_deref);
  506. type_deref = value_type (val_deref);
  507. entryval_deref = coerce_ref (entryval);
  508. if (value_lazy (entryval_deref))
  509. value_fetch_lazy (entryval_deref);
  510. /* If the reference addresses match but dereferenced
  511. content does not match print them. */
  512. if (val != val_deref
  513. && value_contents_eq (val_deref, 0,
  514. entryval_deref, 0,
  515. TYPE_LENGTH (type_deref)))
  516. val_equal = 1;
  517. }
  518. catch (const gdb_exception_error &except)
  519. {
  520. /* If the dereferenced content could not be
  521. fetched do not display anything. */
  522. if (except.error == NO_ENTRY_VALUE_ERROR)
  523. val_equal = 1;
  524. else if (except.message != NULL)
  525. {
  526. entryval_error
  527. = (char *) alloca (except.message->size () + 1);
  528. strcpy (entryval_error, except.what ());
  529. }
  530. }
  531. /* Value was not a reference; and its content matches. */
  532. if (val == val_deref)
  533. val_equal = 1;
  534. if (val_equal)
  535. entryval = NULL;
  536. }
  537. }
  538. /* Try to remove possibly duplicate error message for ENTRYARGP even
  539. in MI mode. */
  540. if (val_error && entryval_error
  541. && strcmp (val_error, entryval_error) == 0)
  542. {
  543. entryval_error = NULL;
  544. /* Do not se VAL_EQUAL as the same error message may be shown for
  545. the entry value even if no entry values are present in the
  546. inferior. */
  547. }
  548. }
  549. }
  550. if (entryval == NULL)
  551. {
  552. if (fp_opts.print_entry_values == print_entry_values_preferred)
  553. {
  554. gdb_assert (val == NULL);
  555. try
  556. {
  557. val = read_var_value (sym, NULL, frame);
  558. }
  559. catch (const gdb_exception_error &except)
  560. {
  561. val_error = (char *) alloca (except.message->size () + 1);
  562. strcpy (val_error, except.what ());
  563. }
  564. }
  565. if (fp_opts.print_entry_values == print_entry_values_only
  566. || fp_opts.print_entry_values == print_entry_values_both
  567. || (fp_opts.print_entry_values == print_entry_values_preferred
  568. && (!val || value_optimized_out (val))))
  569. {
  570. entryval = allocate_optimized_out_value (sym->type ());
  571. entryval_error = NULL;
  572. }
  573. }
  574. if ((fp_opts.print_entry_values == print_entry_values_compact
  575. || fp_opts.print_entry_values == print_entry_values_if_needed
  576. || fp_opts.print_entry_values == print_entry_values_preferred)
  577. && (!val || value_optimized_out (val)) && entryval != NULL)
  578. {
  579. val = NULL;
  580. val_error = NULL;
  581. }
  582. argp->sym = sym;
  583. argp->val = val;
  584. argp->error.reset (val_error ? xstrdup (val_error) : NULL);
  585. if (!val && !val_error)
  586. argp->entry_kind = print_entry_values_only;
  587. else if ((fp_opts.print_entry_values == print_entry_values_compact
  588. || fp_opts.print_entry_values == print_entry_values_default)
  589. && val_equal)
  590. {
  591. argp->entry_kind = print_entry_values_compact;
  592. gdb_assert (!current_uiout->is_mi_like_p ());
  593. }
  594. else
  595. argp->entry_kind = print_entry_values_no;
  596. entryargp->sym = sym;
  597. entryargp->val = entryval;
  598. entryargp->error.reset (entryval_error ? xstrdup (entryval_error) : NULL);
  599. if (!entryval && !entryval_error)
  600. entryargp->entry_kind = print_entry_values_no;
  601. else
  602. entryargp->entry_kind = print_entry_values_only;
  603. }
  604. /* Print the arguments of frame FRAME on STREAM, given the function
  605. FUNC running in that frame (as a symbol), where NUM is the number
  606. of arguments according to the stack frame (or -1 if the number of
  607. arguments is unknown). */
  608. /* Note that currently the "number of arguments according to the
  609. stack frame" is only known on VAX where i refers to the "number of
  610. ints of arguments according to the stack frame". */
  611. static void
  612. print_frame_args (const frame_print_options &fp_opts,
  613. struct symbol *func, struct frame_info *frame,
  614. int num, struct ui_file *stream)
  615. {
  616. struct ui_out *uiout = current_uiout;
  617. int first = 1;
  618. /* Offset of next stack argument beyond the one we have seen that is
  619. at the highest offset, or -1 if we haven't come to a stack
  620. argument yet. */
  621. long highest_offset = -1;
  622. /* Number of ints of arguments that we have printed so far. */
  623. int args_printed = 0;
  624. /* True if we should print arg names. If false, we only indicate
  625. the presence of arguments by printing ellipsis. */
  626. bool print_names
  627. = fp_opts.print_frame_arguments != print_frame_arguments_presence;
  628. /* True if we should print arguments, false otherwise. */
  629. bool print_args
  630. = (print_names
  631. && fp_opts.print_frame_arguments != print_frame_arguments_none);
  632. /* Temporarily change the selected frame to the given FRAME.
  633. This allows routines that rely on the selected frame instead
  634. of being given a frame as parameter to use the correct frame. */
  635. scoped_restore_selected_frame restore_selected_frame;
  636. select_frame (frame);
  637. if (func)
  638. {
  639. const struct block *b = SYMBOL_BLOCK_VALUE (func);
  640. struct block_iterator iter;
  641. struct symbol *sym;
  642. ALL_BLOCK_SYMBOLS (b, iter, sym)
  643. {
  644. struct frame_arg arg, entryarg;
  645. QUIT;
  646. /* Keep track of the highest stack argument offset seen, and
  647. skip over any kinds of symbols we don't care about. */
  648. if (!sym->is_argument ())
  649. continue;
  650. if (!print_names)
  651. {
  652. uiout->text ("...");
  653. first = 0;
  654. break;
  655. }
  656. switch (sym->aclass ())
  657. {
  658. case LOC_ARG:
  659. case LOC_REF_ARG:
  660. {
  661. long current_offset = SYMBOL_VALUE (sym);
  662. int arg_size = TYPE_LENGTH (sym->type ());
  663. /* Compute address of next argument by adding the size of
  664. this argument and rounding to an int boundary. */
  665. current_offset =
  666. ((current_offset + arg_size + sizeof (int) - 1)
  667. & ~(sizeof (int) - 1));
  668. /* If this is the highest offset seen yet, set
  669. highest_offset. */
  670. if (highest_offset == -1
  671. || (current_offset > highest_offset))
  672. highest_offset = current_offset;
  673. /* Add the number of ints we're about to print to
  674. args_printed. */
  675. args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
  676. }
  677. /* We care about types of symbols, but don't need to
  678. keep track of stack offsets in them. */
  679. case LOC_REGISTER:
  680. case LOC_REGPARM_ADDR:
  681. case LOC_COMPUTED:
  682. case LOC_OPTIMIZED_OUT:
  683. default:
  684. break;
  685. }
  686. /* We have to look up the symbol because arguments can have
  687. two entries (one a parameter, one a local) and the one we
  688. want is the local, which lookup_symbol will find for us.
  689. This includes gcc1 (not gcc2) on SPARC when passing a
  690. small structure and gcc2 when the argument type is float
  691. and it is passed as a double and converted to float by
  692. the prologue (in the latter case the type of the LOC_ARG
  693. symbol is double and the type of the LOC_LOCAL symbol is
  694. float). */
  695. /* But if the parameter name is null, don't try it. Null
  696. parameter names occur on the RS/6000, for traceback
  697. tables. FIXME, should we even print them? */
  698. if (*sym->linkage_name ())
  699. {
  700. struct symbol *nsym;
  701. nsym = lookup_symbol_search_name (sym->search_name (),
  702. b, VAR_DOMAIN).symbol;
  703. gdb_assert (nsym != NULL);
  704. if (nsym->aclass () == LOC_REGISTER
  705. && !nsym->is_argument ())
  706. {
  707. /* There is a LOC_ARG/LOC_REGISTER pair. This means
  708. that it was passed on the stack and loaded into a
  709. register, or passed in a register and stored in a
  710. stack slot. GDB 3.x used the LOC_ARG; GDB
  711. 4.0-4.11 used the LOC_REGISTER.
  712. Reasons for using the LOC_ARG:
  713. (1) Because find_saved_registers may be slow for
  714. remote debugging.
  715. (2) Because registers are often re-used and stack
  716. slots rarely (never?) are. Therefore using
  717. the stack slot is much less likely to print
  718. garbage.
  719. Reasons why we might want to use the LOC_REGISTER:
  720. (1) So that the backtrace prints the same value
  721. as "print foo". I see no compelling reason
  722. why this needs to be the case; having the
  723. backtrace print the value which was passed
  724. in, and "print foo" print the value as
  725. modified within the called function, makes
  726. perfect sense to me.
  727. Additional note: It might be nice if "info args"
  728. displayed both values.
  729. One more note: There is a case with SPARC
  730. structure passing where we need to use the
  731. LOC_REGISTER, but this is dealt with by creating
  732. a single LOC_REGPARM in symbol reading. */
  733. /* Leave sym (the LOC_ARG) alone. */
  734. ;
  735. }
  736. else
  737. sym = nsym;
  738. }
  739. /* Print the current arg. */
  740. if (!first)
  741. uiout->text (", ");
  742. uiout->wrap_hint (4);
  743. if (!print_args)
  744. {
  745. arg.sym = sym;
  746. arg.entry_kind = print_entry_values_no;
  747. entryarg.sym = sym;
  748. entryarg.entry_kind = print_entry_values_no;
  749. }
  750. else
  751. read_frame_arg (fp_opts, sym, frame, &arg, &entryarg);
  752. if (arg.entry_kind != print_entry_values_only)
  753. print_frame_arg (fp_opts, &arg);
  754. if (entryarg.entry_kind != print_entry_values_no)
  755. {
  756. if (arg.entry_kind != print_entry_values_only)
  757. {
  758. uiout->text (", ");
  759. uiout->wrap_hint (4);
  760. }
  761. print_frame_arg (fp_opts, &entryarg);
  762. }
  763. first = 0;
  764. }
  765. }
  766. /* Don't print nameless args in situations where we don't know
  767. enough about the stack to find them. */
  768. if (num != -1)
  769. {
  770. long start;
  771. if (highest_offset == -1)
  772. start = gdbarch_frame_args_skip (get_frame_arch (frame));
  773. else
  774. start = highest_offset;
  775. if (!print_names && !first && num > 0)
  776. uiout->text ("...");
  777. else
  778. print_frame_nameless_args (frame, start, num - args_printed,
  779. first, stream);
  780. }
  781. }
  782. /* Set the current source and line to the location given by frame
  783. FRAME, if possible. When CENTER is true, adjust so the relevant
  784. line is in the center of the next 'list'. */
  785. void
  786. set_current_sal_from_frame (struct frame_info *frame)
  787. {
  788. symtab_and_line sal = find_frame_sal (frame);
  789. if (sal.symtab != NULL)
  790. set_current_source_symtab_and_line (sal);
  791. }
  792. /* If ON, GDB will display disassembly of the next source line when
  793. execution of the program being debugged stops.
  794. If AUTO (which is the default), or there's no line info to determine
  795. the source line of the next instruction, display disassembly of next
  796. instruction instead. */
  797. static enum auto_boolean disassemble_next_line;
  798. static void
  799. show_disassemble_next_line (struct ui_file *file, int from_tty,
  800. struct cmd_list_element *c,
  801. const char *value)
  802. {
  803. gdb_printf (file,
  804. _("Debugger's willingness to use "
  805. "disassemble-next-line is %s.\n"),
  806. value);
  807. }
  808. /* Use TRY_CATCH to catch the exception from the gdb_disassembly
  809. because it will be broken by filter sometime. */
  810. static void
  811. do_gdb_disassembly (struct gdbarch *gdbarch,
  812. int how_many, CORE_ADDR low, CORE_ADDR high)
  813. {
  814. try
  815. {
  816. gdb_disassembly (gdbarch, current_uiout,
  817. DISASSEMBLY_RAW_INSN, how_many,
  818. low, high);
  819. }
  820. catch (const gdb_exception_error &exception)
  821. {
  822. /* If an exception was thrown while doing the disassembly, print
  823. the error message, to give the user a clue of what happened. */
  824. exception_print (gdb_stderr, exception);
  825. }
  826. }
  827. /* Converts the PRINT_FRAME_INFO choice to an optional enum print_what.
  828. Value not present indicates to the caller to use default values
  829. specific to the command being executed. */
  830. static gdb::optional<enum print_what>
  831. print_frame_info_to_print_what (const char *print_frame_info)
  832. {
  833. for (int i = 0; print_frame_info_choices[i] != NULL; i++)
  834. if (print_frame_info == print_frame_info_choices[i])
  835. return print_frame_info_print_what[i];
  836. internal_error (__FILE__, __LINE__,
  837. "Unexpected print frame-info value `%s'.",
  838. print_frame_info);
  839. }
  840. /* Print the PC from FRAME, plus any flags, to UIOUT. */
  841. static void
  842. print_pc (struct ui_out *uiout, struct gdbarch *gdbarch, frame_info *frame,
  843. CORE_ADDR pc)
  844. {
  845. uiout->field_core_addr ("addr", gdbarch, pc);
  846. std::string flags = gdbarch_get_pc_address_flags (gdbarch, frame, pc);
  847. if (!flags.empty ())
  848. {
  849. uiout->text (" [");
  850. uiout->field_string ("addr_flags", flags);
  851. uiout->text ("]");
  852. }
  853. }
  854. /* See stack.h. */
  855. void
  856. get_user_print_what_frame_info (gdb::optional<enum print_what> *what)
  857. {
  858. *what
  859. = print_frame_info_to_print_what
  860. (user_frame_print_options.print_frame_info);
  861. }
  862. /* Print information about frame FRAME. The output is format according
  863. to PRINT_LEVEL and PRINT_WHAT and PRINT_ARGS. For the meaning of
  864. PRINT_WHAT, see enum print_what comments in frame.h.
  865. Note that PRINT_WHAT is overridden if FP_OPTS.print_frame_info
  866. != print_frame_info_auto.
  867. Used in "where" output, and to emit breakpoint or step
  868. messages. */
  869. void
  870. print_frame_info (const frame_print_options &fp_opts,
  871. frame_info *frame, int print_level,
  872. enum print_what print_what, int print_args,
  873. int set_current_sal)
  874. {
  875. struct gdbarch *gdbarch = get_frame_arch (frame);
  876. int source_print;
  877. int location_print;
  878. struct ui_out *uiout = current_uiout;
  879. if (!current_uiout->is_mi_like_p ()
  880. && fp_opts.print_frame_info != print_frame_info_auto)
  881. {
  882. /* Use the specific frame information desired by the user. */
  883. print_what = *print_frame_info_to_print_what (fp_opts.print_frame_info);
  884. }
  885. if (get_frame_type (frame) == DUMMY_FRAME
  886. || get_frame_type (frame) == SIGTRAMP_FRAME
  887. || get_frame_type (frame) == ARCH_FRAME)
  888. {
  889. ui_out_emit_tuple tuple_emitter (uiout, "frame");
  890. annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
  891. gdbarch, get_frame_pc (frame));
  892. /* Do this regardless of SOURCE because we don't have any source
  893. to list for this frame. */
  894. if (print_level)
  895. {
  896. uiout->text ("#");
  897. uiout->field_fmt_signed (2, ui_left, "level",
  898. frame_relative_level (frame));
  899. }
  900. if (uiout->is_mi_like_p ())
  901. {
  902. annotate_frame_address ();
  903. print_pc (uiout, gdbarch, frame, get_frame_pc (frame));
  904. annotate_frame_address_end ();
  905. }
  906. if (get_frame_type (frame) == DUMMY_FRAME)
  907. {
  908. annotate_function_call ();
  909. uiout->field_string ("func", "<function called from gdb>",
  910. metadata_style.style ());
  911. }
  912. else if (get_frame_type (frame) == SIGTRAMP_FRAME)
  913. {
  914. annotate_signal_handler_caller ();
  915. uiout->field_string ("func", "<signal handler called>",
  916. metadata_style.style ());
  917. }
  918. else if (get_frame_type (frame) == ARCH_FRAME)
  919. {
  920. uiout->field_string ("func", "<cross-architecture call>",
  921. metadata_style.style ());
  922. }
  923. uiout->text ("\n");
  924. annotate_frame_end ();
  925. /* If disassemble-next-line is set to auto or on output the next
  926. instruction. */
  927. if (disassemble_next_line == AUTO_BOOLEAN_AUTO
  928. || disassemble_next_line == AUTO_BOOLEAN_TRUE)
  929. do_gdb_disassembly (get_frame_arch (frame), 1,
  930. get_frame_pc (frame), get_frame_pc (frame) + 1);
  931. return;
  932. }
  933. /* If FRAME is not the innermost frame, that normally means that
  934. FRAME->pc points to *after* the call instruction, and we want to
  935. get the line containing the call, never the next line. But if
  936. the next frame is a SIGTRAMP_FRAME or a DUMMY_FRAME, then the
  937. next frame was not entered as the result of a call, and we want
  938. to get the line containing FRAME->pc. */
  939. symtab_and_line sal = find_frame_sal (frame);
  940. location_print = (print_what == LOCATION
  941. || print_what == SRC_AND_LOC
  942. || print_what == LOC_AND_ADDRESS
  943. || print_what == SHORT_LOCATION);
  944. if (location_print || !sal.symtab)
  945. print_frame (fp_opts, frame, print_level, print_what, print_args, sal);
  946. source_print = (print_what == SRC_LINE || print_what == SRC_AND_LOC);
  947. /* If disassemble-next-line is set to auto or on and doesn't have
  948. the line debug messages for $pc, output the next instruction. */
  949. if ((disassemble_next_line == AUTO_BOOLEAN_AUTO
  950. || disassemble_next_line == AUTO_BOOLEAN_TRUE)
  951. && source_print && !sal.symtab)
  952. do_gdb_disassembly (get_frame_arch (frame), 1,
  953. get_frame_pc (frame), get_frame_pc (frame) + 1);
  954. if (source_print && sal.symtab)
  955. {
  956. int mid_statement = ((print_what == SRC_LINE)
  957. && frame_show_address (frame, sal));
  958. if (annotation_level > 0
  959. && annotate_source_line (sal.symtab, sal.line, mid_statement,
  960. get_frame_pc (frame)))
  961. {
  962. /* The call to ANNOTATE_SOURCE_LINE already printed the
  963. annotation for this source line, so we avoid the two cases
  964. below and do not print the actual source line. The
  965. documentation for annotations makes it clear that the source
  966. line annotation is printed __instead__ of printing the source
  967. line, not as well as.
  968. However, if we fail to print the source line, which usually
  969. means either the source file is missing, or the requested
  970. line is out of range of the file, then we don't print the
  971. source annotation, and will pass through the "normal" print
  972. source line code below, the expectation is that this code
  973. will print an appropriate error. */
  974. }
  975. else if (deprecated_print_frame_info_listing_hook)
  976. deprecated_print_frame_info_listing_hook (sal.symtab, sal.line,
  977. sal.line + 1, 0);
  978. else
  979. {
  980. struct value_print_options opts;
  981. get_user_print_options (&opts);
  982. /* We used to do this earlier, but that is clearly
  983. wrong. This function is used by many different
  984. parts of gdb, including normal_stop in infrun.c,
  985. which uses this to print out the current PC
  986. when we stepi/nexti into the middle of a source
  987. line. Only the command line really wants this
  988. behavior. Other UIs probably would like the
  989. ability to decide for themselves if it is desired. */
  990. if (opts.addressprint && mid_statement)
  991. {
  992. print_pc (uiout, gdbarch, frame, get_frame_pc (frame));
  993. uiout->text ("\t");
  994. }
  995. print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
  996. }
  997. /* If disassemble-next-line is set to on and there is line debug
  998. messages, output assembly codes for next line. */
  999. if (disassemble_next_line == AUTO_BOOLEAN_TRUE)
  1000. do_gdb_disassembly (get_frame_arch (frame), -1, sal.pc, sal.end);
  1001. }
  1002. if (set_current_sal)
  1003. {
  1004. CORE_ADDR pc;
  1005. if (get_frame_pc_if_available (frame, &pc))
  1006. last_displayed_symtab_info.set (sal.pspace, pc, sal.symtab, sal.line);
  1007. else
  1008. last_displayed_symtab_info.invalidate ();
  1009. }
  1010. annotate_frame_end ();
  1011. gdb_flush (gdb_stdout);
  1012. }
  1013. /* See stack.h. */
  1014. void
  1015. clear_last_displayed_sal (void)
  1016. {
  1017. last_displayed_symtab_info.invalidate ();
  1018. }
  1019. /* See stack.h. */
  1020. bool
  1021. last_displayed_sal_is_valid (void)
  1022. {
  1023. return last_displayed_symtab_info.is_valid ();
  1024. }
  1025. /* See stack.h. */
  1026. struct program_space *
  1027. get_last_displayed_pspace (void)
  1028. {
  1029. return last_displayed_symtab_info.pspace ();
  1030. }
  1031. /* See stack.h. */
  1032. CORE_ADDR
  1033. get_last_displayed_addr (void)
  1034. {
  1035. return last_displayed_symtab_info.address ();
  1036. }
  1037. /* See stack.h. */
  1038. struct symtab*
  1039. get_last_displayed_symtab (void)
  1040. {
  1041. return last_displayed_symtab_info.symtab ();
  1042. }
  1043. /* See stack.h. */
  1044. int
  1045. get_last_displayed_line (void)
  1046. {
  1047. return last_displayed_symtab_info.line ();
  1048. }
  1049. /* See stack.h. */
  1050. symtab_and_line
  1051. get_last_displayed_sal ()
  1052. {
  1053. symtab_and_line sal;
  1054. if (last_displayed_symtab_info.is_valid ())
  1055. {
  1056. sal.pspace = last_displayed_symtab_info.pspace ();
  1057. sal.pc = last_displayed_symtab_info.address ();
  1058. sal.symtab = last_displayed_symtab_info.symtab ();
  1059. sal.line = last_displayed_symtab_info.line ();
  1060. }
  1061. return sal;
  1062. }
  1063. /* Attempt to obtain the name, FUNLANG and optionally FUNCP of the function
  1064. corresponding to FRAME. */
  1065. gdb::unique_xmalloc_ptr<char>
  1066. find_frame_funname (struct frame_info *frame, enum language *funlang,
  1067. struct symbol **funcp)
  1068. {
  1069. struct symbol *func;
  1070. gdb::unique_xmalloc_ptr<char> funname;
  1071. *funlang = language_unknown;
  1072. if (funcp)
  1073. *funcp = NULL;
  1074. func = get_frame_function (frame);
  1075. if (func)
  1076. {
  1077. const char *print_name = func->print_name ();
  1078. *funlang = func->language ();
  1079. if (funcp)
  1080. *funcp = func;
  1081. if (*funlang == language_cplus)
  1082. {
  1083. /* It seems appropriate to use print_name() here,
  1084. to display the demangled name that we already have
  1085. stored in the symbol table, but we stored a version
  1086. with DMGL_PARAMS turned on, and here we don't want to
  1087. display parameters. So remove the parameters. */
  1088. funname = cp_remove_params (print_name);
  1089. }
  1090. /* If we didn't hit the C++ case above, set *funname
  1091. here. */
  1092. if (funname == NULL)
  1093. funname.reset (xstrdup (print_name));
  1094. }
  1095. else
  1096. {
  1097. struct bound_minimal_symbol msymbol;
  1098. CORE_ADDR pc;
  1099. if (!get_frame_address_in_block_if_available (frame, &pc))
  1100. return funname;
  1101. msymbol = lookup_minimal_symbol_by_pc (pc);
  1102. if (msymbol.minsym != NULL)
  1103. {
  1104. funname.reset (xstrdup (msymbol.minsym->print_name ()));
  1105. *funlang = msymbol.minsym->language ();
  1106. }
  1107. }
  1108. return funname;
  1109. }
  1110. static void
  1111. print_frame (const frame_print_options &fp_opts,
  1112. frame_info *frame, int print_level,
  1113. enum print_what print_what, int print_args,
  1114. struct symtab_and_line sal)
  1115. {
  1116. struct gdbarch *gdbarch = get_frame_arch (frame);
  1117. struct ui_out *uiout = current_uiout;
  1118. enum language funlang = language_unknown;
  1119. struct value_print_options opts;
  1120. struct symbol *func;
  1121. CORE_ADDR pc = 0;
  1122. int pc_p;
  1123. pc_p = get_frame_pc_if_available (frame, &pc);
  1124. gdb::unique_xmalloc_ptr<char> funname
  1125. = find_frame_funname (frame, &funlang, &func);
  1126. annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
  1127. gdbarch, pc);
  1128. {
  1129. ui_out_emit_tuple tuple_emitter (uiout, "frame");
  1130. if (print_level)
  1131. {
  1132. uiout->text ("#");
  1133. uiout->field_fmt_signed (2, ui_left, "level",
  1134. frame_relative_level (frame));
  1135. }
  1136. get_user_print_options (&opts);
  1137. if (opts.addressprint)
  1138. if (!sal.symtab
  1139. || frame_show_address (frame, sal)
  1140. || print_what == LOC_AND_ADDRESS)
  1141. {
  1142. annotate_frame_address ();
  1143. if (pc_p)
  1144. print_pc (uiout, gdbarch, frame, pc);
  1145. else
  1146. uiout->field_string ("addr", "<unavailable>",
  1147. metadata_style.style ());
  1148. annotate_frame_address_end ();
  1149. uiout->text (" in ");
  1150. }
  1151. annotate_frame_function_name ();
  1152. string_file stb;
  1153. gdb_puts (funname ? funname.get () : "??", &stb);
  1154. uiout->field_stream ("func", stb, function_name_style.style ());
  1155. uiout->wrap_hint (3);
  1156. annotate_frame_args ();
  1157. uiout->text (" (");
  1158. if (print_args)
  1159. {
  1160. int numargs;
  1161. if (gdbarch_frame_num_args_p (gdbarch))
  1162. {
  1163. numargs = gdbarch_frame_num_args (gdbarch, frame);
  1164. gdb_assert (numargs >= 0);
  1165. }
  1166. else
  1167. numargs = -1;
  1168. {
  1169. ui_out_emit_list list_emitter (uiout, "args");
  1170. try
  1171. {
  1172. print_frame_args (fp_opts, func, frame, numargs, gdb_stdout);
  1173. }
  1174. catch (const gdb_exception_error &e)
  1175. {
  1176. }
  1177. /* FIXME: ARGS must be a list. If one argument is a string it
  1178. will have " that will not be properly escaped. */
  1179. }
  1180. QUIT;
  1181. }
  1182. uiout->text (")");
  1183. if (print_what != SHORT_LOCATION && sal.symtab)
  1184. {
  1185. const char *filename_display;
  1186. filename_display = symtab_to_filename_for_display (sal.symtab);
  1187. annotate_frame_source_begin ();
  1188. uiout->wrap_hint (3);
  1189. uiout->text (" at ");
  1190. annotate_frame_source_file ();
  1191. uiout->field_string ("file", filename_display,
  1192. file_name_style.style ());
  1193. if (uiout->is_mi_like_p ())
  1194. {
  1195. const char *fullname = symtab_to_fullname (sal.symtab);
  1196. uiout->field_string ("fullname", fullname);
  1197. }
  1198. annotate_frame_source_file_end ();
  1199. uiout->text (":");
  1200. annotate_frame_source_line ();
  1201. uiout->field_signed ("line", sal.line);
  1202. annotate_frame_source_end ();
  1203. }
  1204. if (print_what != SHORT_LOCATION
  1205. && pc_p && (funname == NULL || sal.symtab == NULL))
  1206. {
  1207. char *lib = solib_name_from_address (get_frame_program_space (frame),
  1208. get_frame_pc (frame));
  1209. if (lib)
  1210. {
  1211. annotate_frame_where ();
  1212. uiout->wrap_hint (2);
  1213. uiout->text (" from ");
  1214. uiout->field_string ("from", lib, file_name_style.style ());
  1215. }
  1216. }
  1217. if (uiout->is_mi_like_p ())
  1218. uiout->field_string ("arch",
  1219. (gdbarch_bfd_arch_info (gdbarch))->printable_name);
  1220. }
  1221. uiout->text ("\n");
  1222. }
  1223. /* Completion function for "frame function", "info frame function", and
  1224. "select-frame function" commands. */
  1225. static void
  1226. frame_selection_by_function_completer (struct cmd_list_element *ignore,
  1227. completion_tracker &tracker,
  1228. const char *text, const char *word)
  1229. {
  1230. /* This is used to complete function names within a stack. It would be
  1231. nice if we only offered functions that were actually in the stack.
  1232. However, this would mean unwinding the stack to completion, which
  1233. could take too long, or on a corrupted stack, possibly not end.
  1234. Instead, we offer all symbol names as a safer choice. */
  1235. collect_symbol_completion_matches (tracker,
  1236. complete_symbol_mode::EXPRESSION,
  1237. symbol_name_match_type::EXPRESSION,
  1238. text, word);
  1239. }
  1240. /* Core of all the "info frame" sub-commands. Print information about a
  1241. frame FI. If SELECTED_FRAME_P is true then the user didn't provide a
  1242. frame specification, they just entered 'info frame'. If the user did
  1243. provide a frame specification (for example 'info frame 0', 'info frame
  1244. level 1') then SELECTED_FRAME_P will be false. */
  1245. static void
  1246. info_frame_command_core (struct frame_info *fi, bool selected_frame_p)
  1247. {
  1248. struct symbol *func;
  1249. struct symtab *s;
  1250. struct frame_info *calling_frame_info;
  1251. int numregs;
  1252. const char *funname = 0;
  1253. enum language funlang = language_unknown;
  1254. const char *pc_regname;
  1255. struct gdbarch *gdbarch;
  1256. CORE_ADDR frame_pc;
  1257. int frame_pc_p;
  1258. /* Initialize it to avoid "may be used uninitialized" warning. */
  1259. CORE_ADDR caller_pc = 0;
  1260. int caller_pc_p = 0;
  1261. gdbarch = get_frame_arch (fi);
  1262. /* Name of the value returned by get_frame_pc(). Per comments, "pc"
  1263. is not a good name. */
  1264. if (gdbarch_pc_regnum (gdbarch) >= 0)
  1265. /* OK, this is weird. The gdbarch_pc_regnum hardware register's value can
  1266. easily not match that of the internal value returned by
  1267. get_frame_pc(). */
  1268. pc_regname = gdbarch_register_name (gdbarch, gdbarch_pc_regnum (gdbarch));
  1269. else
  1270. /* But then, this is weird to. Even without gdbarch_pc_regnum, an
  1271. architectures will often have a hardware register called "pc",
  1272. and that register's value, again, can easily not match
  1273. get_frame_pc(). */
  1274. pc_regname = "pc";
  1275. frame_pc_p = get_frame_pc_if_available (fi, &frame_pc);
  1276. func = get_frame_function (fi);
  1277. symtab_and_line sal = find_frame_sal (fi);
  1278. s = sal.symtab;
  1279. gdb::unique_xmalloc_ptr<char> func_only;
  1280. if (func)
  1281. {
  1282. funname = func->print_name ();
  1283. funlang = func->language ();
  1284. if (funlang == language_cplus)
  1285. {
  1286. /* It seems appropriate to use print_name() here,
  1287. to display the demangled name that we already have
  1288. stored in the symbol table, but we stored a version
  1289. with DMGL_PARAMS turned on, and here we don't want to
  1290. display parameters. So remove the parameters. */
  1291. func_only = cp_remove_params (funname);
  1292. if (func_only)
  1293. funname = func_only.get ();
  1294. }
  1295. }
  1296. else if (frame_pc_p)
  1297. {
  1298. struct bound_minimal_symbol msymbol;
  1299. msymbol = lookup_minimal_symbol_by_pc (frame_pc);
  1300. if (msymbol.minsym != NULL)
  1301. {
  1302. funname = msymbol.minsym->print_name ();
  1303. funlang = msymbol.minsym->language ();
  1304. }
  1305. }
  1306. calling_frame_info = get_prev_frame (fi);
  1307. if (selected_frame_p && frame_relative_level (fi) >= 0)
  1308. {
  1309. gdb_printf (_("Stack level %d, frame at "),
  1310. frame_relative_level (fi));
  1311. }
  1312. else
  1313. {
  1314. gdb_printf (_("Stack frame at "));
  1315. }
  1316. gdb_puts (paddress (gdbarch, get_frame_base (fi)));
  1317. gdb_printf (":\n");
  1318. gdb_printf (" %s = ", pc_regname);
  1319. if (frame_pc_p)
  1320. gdb_puts (paddress (gdbarch, get_frame_pc (fi)));
  1321. else
  1322. fputs_styled ("<unavailable>", metadata_style.style (), gdb_stdout);
  1323. gdb_stdout->wrap_here (3);
  1324. if (funname)
  1325. {
  1326. gdb_printf (" in ");
  1327. gdb_puts (funname);
  1328. }
  1329. gdb_stdout->wrap_here (3);
  1330. if (sal.symtab)
  1331. gdb_printf
  1332. (" (%ps:%d)",
  1333. styled_string (file_name_style.style (),
  1334. symtab_to_filename_for_display (sal.symtab)),
  1335. sal.line);
  1336. gdb_puts ("; ");
  1337. gdb_stdout->wrap_here (4);
  1338. gdb_printf ("saved %s = ", pc_regname);
  1339. if (!frame_id_p (frame_unwind_caller_id (fi)))
  1340. val_print_not_saved (gdb_stdout);
  1341. else
  1342. {
  1343. try
  1344. {
  1345. caller_pc = frame_unwind_caller_pc (fi);
  1346. caller_pc_p = 1;
  1347. }
  1348. catch (const gdb_exception_error &ex)
  1349. {
  1350. switch (ex.error)
  1351. {
  1352. case NOT_AVAILABLE_ERROR:
  1353. val_print_unavailable (gdb_stdout);
  1354. break;
  1355. case OPTIMIZED_OUT_ERROR:
  1356. val_print_not_saved (gdb_stdout);
  1357. break;
  1358. default:
  1359. fprintf_styled (gdb_stdout, metadata_style.style (),
  1360. _("<error: %s>"),
  1361. ex.what ());
  1362. break;
  1363. }
  1364. }
  1365. }
  1366. if (caller_pc_p)
  1367. gdb_puts (paddress (gdbarch, caller_pc));
  1368. gdb_printf ("\n");
  1369. if (calling_frame_info == NULL)
  1370. {
  1371. enum unwind_stop_reason reason;
  1372. reason = get_frame_unwind_stop_reason (fi);
  1373. if (reason != UNWIND_NO_REASON)
  1374. gdb_printf (_(" Outermost frame: %s\n"),
  1375. frame_stop_reason_string (fi));
  1376. }
  1377. else if (get_frame_type (fi) == TAILCALL_FRAME)
  1378. gdb_puts (" tail call frame");
  1379. else if (get_frame_type (fi) == INLINE_FRAME)
  1380. gdb_printf (" inlined into frame %d",
  1381. frame_relative_level (get_prev_frame (fi)));
  1382. else
  1383. {
  1384. gdb_printf (" called by frame at ");
  1385. gdb_puts (paddress (gdbarch, get_frame_base (calling_frame_info)));
  1386. }
  1387. if (get_next_frame (fi) && calling_frame_info)
  1388. gdb_puts (",");
  1389. gdb_stdout->wrap_here (3);
  1390. if (get_next_frame (fi))
  1391. {
  1392. gdb_printf (" caller of frame at ");
  1393. gdb_puts (paddress (gdbarch, get_frame_base (get_next_frame (fi))));
  1394. }
  1395. if (get_next_frame (fi) || calling_frame_info)
  1396. gdb_puts ("\n");
  1397. if (s)
  1398. gdb_printf (" source language %s.\n",
  1399. language_str (s->language ()));
  1400. {
  1401. /* Address of the argument list for this frame, or 0. */
  1402. CORE_ADDR arg_list = get_frame_args_address (fi);
  1403. /* Number of args for this frame, or -1 if unknown. */
  1404. int numargs;
  1405. if (arg_list == 0)
  1406. gdb_printf (" Arglist at unknown address.\n");
  1407. else
  1408. {
  1409. gdb_printf (" Arglist at ");
  1410. gdb_puts (paddress (gdbarch, arg_list));
  1411. gdb_printf (",");
  1412. if (!gdbarch_frame_num_args_p (gdbarch))
  1413. {
  1414. numargs = -1;
  1415. gdb_puts (" args: ");
  1416. }
  1417. else
  1418. {
  1419. numargs = gdbarch_frame_num_args (gdbarch, fi);
  1420. gdb_assert (numargs >= 0);
  1421. if (numargs == 0)
  1422. gdb_puts (" no args.");
  1423. else if (numargs == 1)
  1424. gdb_puts (" 1 arg: ");
  1425. else
  1426. gdb_printf (" %d args: ", numargs);
  1427. }
  1428. print_frame_args (user_frame_print_options,
  1429. func, fi, numargs, gdb_stdout);
  1430. gdb_puts ("\n");
  1431. }
  1432. }
  1433. {
  1434. /* Address of the local variables for this frame, or 0. */
  1435. CORE_ADDR arg_list = get_frame_locals_address (fi);
  1436. if (arg_list == 0)
  1437. gdb_printf (" Locals at unknown address,");
  1438. else
  1439. {
  1440. gdb_printf (" Locals at ");
  1441. gdb_puts (paddress (gdbarch, arg_list));
  1442. gdb_printf (",");
  1443. }
  1444. }
  1445. /* Print as much information as possible on the location of all the
  1446. registers. */
  1447. {
  1448. int count;
  1449. int i;
  1450. int need_nl = 1;
  1451. int sp_regnum = gdbarch_sp_regnum (gdbarch);
  1452. /* The sp is special; what's displayed isn't the save address, but
  1453. the value of the previous frame's sp. This is a legacy thing,
  1454. at one stage the frame cached the previous frame's SP instead
  1455. of its address, hence it was easiest to just display the cached
  1456. value. */
  1457. if (sp_regnum >= 0)
  1458. {
  1459. struct value *value = frame_unwind_register_value (fi, sp_regnum);
  1460. gdb_assert (value != NULL);
  1461. if (!value_optimized_out (value) && value_entirely_available (value))
  1462. {
  1463. if (VALUE_LVAL (value) == not_lval)
  1464. {
  1465. CORE_ADDR sp;
  1466. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1467. int sp_size = register_size (gdbarch, sp_regnum);
  1468. sp = extract_unsigned_integer
  1469. (value_contents_all (value).data (), sp_size, byte_order);
  1470. gdb_printf (" Previous frame's sp is ");
  1471. gdb_puts (paddress (gdbarch, sp));
  1472. gdb_printf ("\n");
  1473. }
  1474. else if (VALUE_LVAL (value) == lval_memory)
  1475. {
  1476. gdb_printf (" Previous frame's sp at ");
  1477. gdb_puts (paddress (gdbarch, value_address (value)));
  1478. gdb_printf ("\n");
  1479. }
  1480. else if (VALUE_LVAL (value) == lval_register)
  1481. {
  1482. gdb_printf (" Previous frame's sp in %s\n",
  1483. gdbarch_register_name (gdbarch,
  1484. VALUE_REGNUM (value)));
  1485. }
  1486. release_value (value);
  1487. need_nl = 0;
  1488. }
  1489. /* else keep quiet. */
  1490. }
  1491. count = 0;
  1492. numregs = gdbarch_num_cooked_regs (gdbarch);
  1493. for (i = 0; i < numregs; i++)
  1494. if (i != sp_regnum
  1495. && gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
  1496. {
  1497. enum lval_type lval;
  1498. int optimized;
  1499. int unavailable;
  1500. CORE_ADDR addr;
  1501. int realnum;
  1502. /* Find out the location of the saved register without
  1503. fetching the corresponding value. */
  1504. frame_register_unwind (fi, i, &optimized, &unavailable,
  1505. &lval, &addr, &realnum, NULL);
  1506. /* For moment, only display registers that were saved on the
  1507. stack. */
  1508. if (!optimized && !unavailable && lval == lval_memory)
  1509. {
  1510. if (count == 0)
  1511. gdb_puts (" Saved registers:\n ");
  1512. else
  1513. gdb_puts (",");
  1514. gdb_stdout->wrap_here (1);
  1515. gdb_printf (" %s at ",
  1516. gdbarch_register_name (gdbarch, i));
  1517. gdb_puts (paddress (gdbarch, addr));
  1518. count++;
  1519. }
  1520. }
  1521. if (count || need_nl)
  1522. gdb_puts ("\n");
  1523. }
  1524. }
  1525. /* Return the innermost frame at level LEVEL. */
  1526. static struct frame_info *
  1527. leading_innermost_frame (int level)
  1528. {
  1529. struct frame_info *leading;
  1530. leading = get_current_frame ();
  1531. gdb_assert (level >= 0);
  1532. while (leading != nullptr && level)
  1533. {
  1534. QUIT;
  1535. leading = get_prev_frame (leading);
  1536. level--;
  1537. }
  1538. return leading;
  1539. }
  1540. /* Return the starting frame needed to handle COUNT outermost frames. */
  1541. static struct frame_info *
  1542. trailing_outermost_frame (int count)
  1543. {
  1544. struct frame_info *current;
  1545. struct frame_info *trailing;
  1546. trailing = get_current_frame ();
  1547. gdb_assert (count > 0);
  1548. current = trailing;
  1549. while (current != nullptr && count--)
  1550. {
  1551. QUIT;
  1552. current = get_prev_frame (current);
  1553. }
  1554. /* Will stop when CURRENT reaches the top of the stack.
  1555. TRAILING will be COUNT below it. */
  1556. while (current != nullptr)
  1557. {
  1558. QUIT;
  1559. trailing = get_prev_frame (trailing);
  1560. current = get_prev_frame (current);
  1561. }
  1562. return trailing;
  1563. }
  1564. /* The core of all the "select-frame" sub-commands. Just wraps a call to
  1565. SELECT_FRAME. */
  1566. static void
  1567. select_frame_command_core (struct frame_info *fi, bool ignored)
  1568. {
  1569. frame_info *prev_frame = get_selected_frame ();
  1570. select_frame (fi);
  1571. if (get_selected_frame () != prev_frame)
  1572. gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
  1573. }
  1574. /* The core of all the "frame" sub-commands. Select frame FI, and if this
  1575. means we change frame send out a change notification (otherwise, just
  1576. reprint the current frame summary). */
  1577. static void
  1578. frame_command_core (struct frame_info *fi, bool ignored)
  1579. {
  1580. frame_info *prev_frame = get_selected_frame ();
  1581. select_frame (fi);
  1582. if (get_selected_frame () != prev_frame)
  1583. gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
  1584. else
  1585. print_selected_thread_frame (current_uiout, USER_SELECTED_FRAME);
  1586. }
  1587. /* The three commands 'frame', 'select-frame', and 'info frame' all have a
  1588. common set of sub-commands that allow a specific frame to be selected.
  1589. All of the sub-command functions are static methods within this class
  1590. template which is then instantiated below. The template parameter is a
  1591. callback used to implement the functionality of the base command
  1592. ('frame', 'select-frame', or 'info frame').
  1593. In the template parameter FI is the frame being selected. The
  1594. SELECTED_FRAME_P flag is true if the frame being selected was done by
  1595. default, which happens when the user uses the base command with no
  1596. arguments. For example the commands 'info frame', 'select-frame',
  1597. 'frame' will all cause SELECTED_FRAME_P to be true. In all other cases
  1598. SELECTED_FRAME_P is false. */
  1599. template <void (*FPTR) (struct frame_info *fi, bool selected_frame_p)>
  1600. class frame_command_helper
  1601. {
  1602. public:
  1603. /* The "frame level" family of commands. The ARG is an integer that is
  1604. the frame's level in the stack. */
  1605. static void
  1606. level (const char *arg, int from_tty)
  1607. {
  1608. int level = value_as_long (parse_and_eval (arg));
  1609. struct frame_info *fid
  1610. = find_relative_frame (get_current_frame (), &level);
  1611. if (level != 0)
  1612. error (_("No frame at level %s."), arg);
  1613. FPTR (fid, false);
  1614. }
  1615. /* The "frame address" family of commands. ARG is a stack-pointer
  1616. address for an existing frame. This command does not allow new
  1617. frames to be created. */
  1618. static void
  1619. address (const char *arg, int from_tty)
  1620. {
  1621. CORE_ADDR addr = value_as_address (parse_and_eval (arg));
  1622. struct frame_info *fid = find_frame_for_address (addr);
  1623. if (fid == NULL)
  1624. error (_("No frame at address %s."), arg);
  1625. FPTR (fid, false);
  1626. }
  1627. /* The "frame view" family of commands. ARG is one or two addresses and
  1628. is used to view a frame that might be outside the current backtrace.
  1629. The addresses are stack-pointer address, and (optional) pc-address. */
  1630. static void
  1631. view (const char *args, int from_tty)
  1632. {
  1633. struct frame_info *fid;
  1634. if (args == NULL)
  1635. error (_("Missing address argument to view a frame"));
  1636. gdb_argv argv (args);
  1637. if (argv.count () == 2)
  1638. {
  1639. CORE_ADDR addr[2];
  1640. addr [0] = value_as_address (parse_and_eval (argv[0]));
  1641. addr [1] = value_as_address (parse_and_eval (argv[1]));
  1642. fid = create_new_frame (addr[0], addr[1]);
  1643. }
  1644. else
  1645. {
  1646. CORE_ADDR addr = value_as_address (parse_and_eval (argv[0]));
  1647. fid = create_new_frame (addr, false);
  1648. }
  1649. FPTR (fid, false);
  1650. }
  1651. /* The "frame function" family of commands. ARG is the name of a
  1652. function within the stack, the first function (searching from frame
  1653. 0) with that name will be selected. */
  1654. static void
  1655. function (const char *arg, int from_tty)
  1656. {
  1657. if (arg == NULL)
  1658. error (_("Missing function name argument"));
  1659. struct frame_info *fid = find_frame_for_function (arg);
  1660. if (fid == NULL)
  1661. error (_("No frame for function \"%s\"."), arg);
  1662. FPTR (fid, false);
  1663. }
  1664. /* The "frame" base command, that is, when no sub-command is specified.
  1665. If one argument is provided then we assume that this is a frame's
  1666. level as historically, this was the supported command syntax that was
  1667. used most often.
  1668. If no argument is provided, then the current frame is selected. */
  1669. static void
  1670. base_command (const char *arg, int from_tty)
  1671. {
  1672. if (arg == NULL)
  1673. FPTR (get_selected_frame (_("No stack.")), true);
  1674. else
  1675. level (arg, from_tty);
  1676. }
  1677. };
  1678. /* Instantiate three FRAME_COMMAND_HELPER instances to implement the
  1679. sub-commands for 'info frame', 'frame', and 'select-frame' commands. */
  1680. static frame_command_helper <info_frame_command_core> info_frame_cmd;
  1681. static frame_command_helper <frame_command_core> frame_cmd;
  1682. static frame_command_helper <select_frame_command_core> select_frame_cmd;
  1683. /* Print briefly all stack frames or just the innermost COUNT_EXP
  1684. frames. */
  1685. static void
  1686. backtrace_command_1 (const frame_print_options &fp_opts,
  1687. const backtrace_cmd_options &bt_opts,
  1688. const char *count_exp, int from_tty)
  1689. {
  1690. struct frame_info *fi;
  1691. int count;
  1692. int py_start = 0, py_end = 0;
  1693. enum ext_lang_bt_status result = EXT_LANG_BT_ERROR;
  1694. if (!target_has_stack ())
  1695. error (_("No stack."));
  1696. if (count_exp)
  1697. {
  1698. count = parse_and_eval_long (count_exp);
  1699. if (count < 0)
  1700. py_start = count;
  1701. else
  1702. {
  1703. py_start = 0;
  1704. /* The argument to apply_ext_lang_frame_filter is the number
  1705. of the final frame to print, and frames start at 0. */
  1706. py_end = count - 1;
  1707. }
  1708. }
  1709. else
  1710. {
  1711. py_end = -1;
  1712. count = -1;
  1713. }
  1714. frame_filter_flags flags = 0;
  1715. if (bt_opts.full)
  1716. flags |= PRINT_LOCALS;
  1717. if (bt_opts.hide)
  1718. flags |= PRINT_HIDE;
  1719. if (!bt_opts.no_filters)
  1720. {
  1721. enum ext_lang_frame_args arg_type;
  1722. flags |= PRINT_LEVEL | PRINT_FRAME_INFO | PRINT_ARGS;
  1723. if (from_tty)
  1724. flags |= PRINT_MORE_FRAMES;
  1725. if (fp_opts.print_frame_arguments == print_frame_arguments_scalars)
  1726. arg_type = CLI_SCALAR_VALUES;
  1727. else if (fp_opts.print_frame_arguments == print_frame_arguments_all)
  1728. arg_type = CLI_ALL_VALUES;
  1729. else if (fp_opts.print_frame_arguments == print_frame_arguments_presence)
  1730. arg_type = CLI_PRESENCE;
  1731. else if (fp_opts.print_frame_arguments == print_frame_arguments_none)
  1732. arg_type = NO_VALUES;
  1733. else
  1734. gdb_assert (0);
  1735. result = apply_ext_lang_frame_filter (get_current_frame (), flags,
  1736. arg_type, current_uiout,
  1737. py_start, py_end);
  1738. }
  1739. /* Run the inbuilt backtrace if there are no filters registered, or
  1740. "-no-filters" has been specified from the command. */
  1741. if (bt_opts.no_filters || result == EXT_LANG_BT_NO_FILTERS)
  1742. {
  1743. struct frame_info *trailing;
  1744. /* The following code must do two things. First, it must set the
  1745. variable TRAILING to the frame from which we should start
  1746. printing. Second, it must set the variable count to the number
  1747. of frames which we should print, or -1 if all of them. */
  1748. if (count_exp != NULL && count < 0)
  1749. {
  1750. trailing = trailing_outermost_frame (-count);
  1751. count = -1;
  1752. }
  1753. else
  1754. trailing = get_current_frame ();
  1755. for (fi = trailing; fi && count--; fi = get_prev_frame (fi))
  1756. {
  1757. QUIT;
  1758. /* Don't use print_stack_frame; if an error() occurs it probably
  1759. means further attempts to backtrace would fail (on the other
  1760. hand, perhaps the code does or could be fixed to make sure
  1761. the frame->prev field gets set to NULL in that case). */
  1762. print_frame_info (fp_opts, fi, 1, LOCATION, 1, 0);
  1763. if ((flags & PRINT_LOCALS) != 0)
  1764. {
  1765. struct frame_id frame_id = get_frame_id (fi);
  1766. print_frame_local_vars (fi, false, NULL, NULL, 1, gdb_stdout);
  1767. /* print_frame_local_vars invalidates FI. */
  1768. fi = frame_find_by_id (frame_id);
  1769. if (fi == NULL)
  1770. {
  1771. trailing = NULL;
  1772. warning (_("Unable to restore previously selected frame."));
  1773. break;
  1774. }
  1775. }
  1776. /* Save the last frame to check for error conditions. */
  1777. trailing = fi;
  1778. }
  1779. /* If we've stopped before the end, mention that. */
  1780. if (fi && from_tty)
  1781. gdb_printf (_("(More stack frames follow...)\n"));
  1782. /* If we've run out of frames, and the reason appears to be an error
  1783. condition, print it. */
  1784. if (fi == NULL && trailing != NULL)
  1785. {
  1786. enum unwind_stop_reason reason;
  1787. reason = get_frame_unwind_stop_reason (trailing);
  1788. if (reason >= UNWIND_FIRST_ERROR)
  1789. gdb_printf (_("Backtrace stopped: %s\n"),
  1790. frame_stop_reason_string (trailing));
  1791. }
  1792. }
  1793. }
  1794. /* Create an option_def_group array grouping all the "backtrace"
  1795. options, with FP_OPTS, BT_CMD_OPT, SET_BT_OPTS as contexts. */
  1796. static inline std::array<gdb::option::option_def_group, 3>
  1797. make_backtrace_options_def_group (frame_print_options *fp_opts,
  1798. backtrace_cmd_options *bt_cmd_opts,
  1799. set_backtrace_options *set_bt_opts)
  1800. {
  1801. return {{
  1802. { {frame_print_option_defs}, fp_opts },
  1803. { {set_backtrace_option_defs}, set_bt_opts },
  1804. { {backtrace_command_option_defs}, bt_cmd_opts }
  1805. }};
  1806. }
  1807. /* Parse the backtrace command's qualifiers. Returns ARG advanced
  1808. past the qualifiers, if any. BT_CMD_OPTS, if not null, is used to
  1809. store the parsed qualifiers. */
  1810. static const char *
  1811. parse_backtrace_qualifiers (const char *arg,
  1812. backtrace_cmd_options *bt_cmd_opts = nullptr)
  1813. {
  1814. while (true)
  1815. {
  1816. const char *save_arg = arg;
  1817. std::string this_arg = extract_arg (&arg);
  1818. if (this_arg.empty ())
  1819. return arg;
  1820. if (subset_compare (this_arg.c_str (), "no-filters"))
  1821. {
  1822. if (bt_cmd_opts != nullptr)
  1823. bt_cmd_opts->no_filters = true;
  1824. }
  1825. else if (subset_compare (this_arg.c_str (), "full"))
  1826. {
  1827. if (bt_cmd_opts != nullptr)
  1828. bt_cmd_opts->full = true;
  1829. }
  1830. else if (subset_compare (this_arg.c_str (), "hide"))
  1831. {
  1832. if (bt_cmd_opts != nullptr)
  1833. bt_cmd_opts->hide = true;
  1834. }
  1835. else
  1836. {
  1837. /* Not a recognized qualifier, so stop. */
  1838. return save_arg;
  1839. }
  1840. }
  1841. }
  1842. static void
  1843. backtrace_command (const char *arg, int from_tty)
  1844. {
  1845. frame_print_options fp_opts = user_frame_print_options;
  1846. backtrace_cmd_options bt_cmd_opts;
  1847. set_backtrace_options set_bt_opts = user_set_backtrace_options;
  1848. auto grp
  1849. = make_backtrace_options_def_group (&fp_opts, &bt_cmd_opts, &set_bt_opts);
  1850. gdb::option::process_options
  1851. (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  1852. /* Parse non-'-'-prefixed qualifiers, for backwards
  1853. compatibility. */
  1854. if (arg != NULL)
  1855. {
  1856. arg = parse_backtrace_qualifiers (arg, &bt_cmd_opts);
  1857. if (*arg == '\0')
  1858. arg = NULL;
  1859. }
  1860. /* These options are handled quite deep in the unwind machinery, so
  1861. we get to pass them down by swapping globals. */
  1862. scoped_restore restore_set_backtrace_options
  1863. = make_scoped_restore (&user_set_backtrace_options, set_bt_opts);
  1864. backtrace_command_1 (fp_opts, bt_cmd_opts, arg, from_tty);
  1865. }
  1866. /* Completer for the "backtrace" command. */
  1867. static void
  1868. backtrace_command_completer (struct cmd_list_element *ignore,
  1869. completion_tracker &tracker,
  1870. const char *text, const char */*word*/)
  1871. {
  1872. const auto group
  1873. = make_backtrace_options_def_group (nullptr, nullptr, nullptr);
  1874. if (gdb::option::complete_options
  1875. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
  1876. return;
  1877. if (*text != '\0')
  1878. {
  1879. const char *p = skip_to_space (text);
  1880. if (*p == '\0')
  1881. {
  1882. static const char *const backtrace_cmd_qualifier_choices[] = {
  1883. "full", "no-filters", "hide", nullptr,
  1884. };
  1885. complete_on_enum (tracker, backtrace_cmd_qualifier_choices,
  1886. text, text);
  1887. if (tracker.have_completions ())
  1888. return;
  1889. }
  1890. else
  1891. {
  1892. const char *cmd = parse_backtrace_qualifiers (text);
  1893. tracker.advance_custom_word_point_by (cmd - text);
  1894. text = cmd;
  1895. }
  1896. }
  1897. const char *word = advance_to_expression_complete_word_point (tracker, text);
  1898. expression_completer (ignore, tracker, text, word);
  1899. }
  1900. /* Iterate over the local variables of a block B, calling CB. */
  1901. static void
  1902. iterate_over_block_locals (const struct block *b,
  1903. iterate_over_block_arg_local_vars_cb cb)
  1904. {
  1905. struct block_iterator iter;
  1906. struct symbol *sym;
  1907. ALL_BLOCK_SYMBOLS (b, iter, sym)
  1908. {
  1909. switch (sym->aclass ())
  1910. {
  1911. case LOC_CONST:
  1912. case LOC_LOCAL:
  1913. case LOC_REGISTER:
  1914. case LOC_STATIC:
  1915. case LOC_COMPUTED:
  1916. case LOC_OPTIMIZED_OUT:
  1917. if (sym->is_argument ())
  1918. break;
  1919. if (sym->domain () == COMMON_BLOCK_DOMAIN)
  1920. break;
  1921. cb (sym->print_name (), sym);
  1922. break;
  1923. default:
  1924. /* Ignore symbols which are not locals. */
  1925. break;
  1926. }
  1927. }
  1928. }
  1929. /* Iterate over all the local variables in block B, including all its
  1930. superblocks, stopping when the top-level block is reached. */
  1931. void
  1932. iterate_over_block_local_vars (const struct block *block,
  1933. iterate_over_block_arg_local_vars_cb cb)
  1934. {
  1935. while (block)
  1936. {
  1937. iterate_over_block_locals (block, cb);
  1938. /* After handling the function's top-level block, stop. Don't
  1939. continue to its superblock, the block of per-file
  1940. symbols. */
  1941. if (BLOCK_FUNCTION (block))
  1942. break;
  1943. block = BLOCK_SUPERBLOCK (block);
  1944. }
  1945. }
  1946. /* Data to be passed around in the calls to the locals and args
  1947. iterators. */
  1948. struct print_variable_and_value_data
  1949. {
  1950. gdb::optional<compiled_regex> preg;
  1951. gdb::optional<compiled_regex> treg;
  1952. struct frame_id frame_id;
  1953. int num_tabs;
  1954. struct ui_file *stream;
  1955. int values_printed;
  1956. void operator() (const char *print_name, struct symbol *sym);
  1957. };
  1958. /* The callback for the locals and args iterators. */
  1959. void
  1960. print_variable_and_value_data::operator() (const char *print_name,
  1961. struct symbol *sym)
  1962. {
  1963. struct frame_info *frame;
  1964. if (preg.has_value ()
  1965. && preg->exec (sym->natural_name (), 0, NULL, 0) != 0)
  1966. return;
  1967. if (treg.has_value ()
  1968. && !treg_matches_sym_type_name (*treg, sym))
  1969. return;
  1970. if (language_def (sym->language ())->symbol_printing_suppressed (sym))
  1971. return;
  1972. frame = frame_find_by_id (frame_id);
  1973. if (frame == NULL)
  1974. {
  1975. warning (_("Unable to restore previously selected frame."));
  1976. return;
  1977. }
  1978. print_variable_and_value (print_name, sym, frame, stream, num_tabs);
  1979. /* print_variable_and_value invalidates FRAME. */
  1980. frame = NULL;
  1981. values_printed = 1;
  1982. }
  1983. /* Prepares the regular expression REG from REGEXP.
  1984. If REGEXP is NULL, it results in an empty regular expression. */
  1985. static void
  1986. prepare_reg (const char *regexp, gdb::optional<compiled_regex> *reg)
  1987. {
  1988. if (regexp != NULL)
  1989. {
  1990. int cflags = REG_NOSUB | (case_sensitivity == case_sensitive_off
  1991. ? REG_ICASE : 0);
  1992. reg->emplace (regexp, cflags, _("Invalid regexp"));
  1993. }
  1994. else
  1995. reg->reset ();
  1996. }
  1997. /* Print all variables from the innermost up to the function block of FRAME.
  1998. Print them with values to STREAM indented by NUM_TABS.
  1999. If REGEXP is not NULL, only print local variables whose name
  2000. matches REGEXP.
  2001. If T_REGEXP is not NULL, only print local variables whose type
  2002. matches T_REGEXP.
  2003. If no local variables have been printed and !QUIET, prints a message
  2004. explaining why no local variables could be printed.
  2005. This function will invalidate FRAME. */
  2006. static void
  2007. print_frame_local_vars (struct frame_info *frame,
  2008. bool quiet,
  2009. const char *regexp, const char *t_regexp,
  2010. int num_tabs, struct ui_file *stream)
  2011. {
  2012. struct print_variable_and_value_data cb_data;
  2013. const struct block *block;
  2014. CORE_ADDR pc;
  2015. if (!get_frame_pc_if_available (frame, &pc))
  2016. {
  2017. if (!quiet)
  2018. gdb_printf (stream,
  2019. _("PC unavailable, cannot determine locals.\n"));
  2020. return;
  2021. }
  2022. block = get_frame_block (frame, 0);
  2023. if (block == 0)
  2024. {
  2025. if (!quiet)
  2026. gdb_printf (stream, "No symbol table info available.\n");
  2027. return;
  2028. }
  2029. prepare_reg (regexp, &cb_data.preg);
  2030. prepare_reg (t_regexp, &cb_data.treg);
  2031. cb_data.frame_id = get_frame_id (frame);
  2032. cb_data.num_tabs = 4 * num_tabs;
  2033. cb_data.stream = stream;
  2034. cb_data.values_printed = 0;
  2035. /* Temporarily change the selected frame to the given FRAME.
  2036. This allows routines that rely on the selected frame instead
  2037. of being given a frame as parameter to use the correct frame. */
  2038. scoped_restore_selected_frame restore_selected_frame;
  2039. select_frame (frame);
  2040. iterate_over_block_local_vars (block, cb_data);
  2041. if (!cb_data.values_printed && !quiet)
  2042. {
  2043. if (regexp == NULL && t_regexp == NULL)
  2044. gdb_printf (stream, _("No locals.\n"));
  2045. else
  2046. gdb_printf (stream, _("No matching locals.\n"));
  2047. }
  2048. }
  2049. /* Structure to hold the values of the options used by the 'info
  2050. variables' command and other similar commands. These correspond to the
  2051. -q and -t options. */
  2052. struct info_print_options
  2053. {
  2054. bool quiet = false;
  2055. std::string type_regexp;
  2056. };
  2057. /* The options used by the 'info locals' and 'info args' commands. */
  2058. static const gdb::option::option_def info_print_options_defs[] = {
  2059. gdb::option::boolean_option_def<info_print_options> {
  2060. "q",
  2061. [] (info_print_options *opt) { return &opt->quiet; },
  2062. nullptr, /* show_cmd_cb */
  2063. nullptr /* set_doc */
  2064. },
  2065. gdb::option::string_option_def<info_print_options> {
  2066. "t",
  2067. [] (info_print_options *opt) { return &opt->type_regexp; },
  2068. nullptr, /* show_cmd_cb */
  2069. nullptr /* set_doc */
  2070. }
  2071. };
  2072. /* Returns the option group used by 'info locals' and 'info args'
  2073. commands. */
  2074. static gdb::option::option_def_group
  2075. make_info_print_options_def_group (info_print_options *opts)
  2076. {
  2077. return {{info_print_options_defs}, opts};
  2078. }
  2079. /* Command completer for 'info locals' and 'info args'. */
  2080. static void
  2081. info_print_command_completer (struct cmd_list_element *ignore,
  2082. completion_tracker &tracker,
  2083. const char *text, const char * /* word */)
  2084. {
  2085. const auto group
  2086. = make_info_print_options_def_group (nullptr);
  2087. if (gdb::option::complete_options
  2088. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
  2089. return;
  2090. const char *word = advance_to_expression_complete_word_point (tracker, text);
  2091. symbol_completer (ignore, tracker, text, word);
  2092. }
  2093. /* Implement the 'info locals' command. */
  2094. void
  2095. info_locals_command (const char *args, int from_tty)
  2096. {
  2097. info_print_options opts;
  2098. auto grp = make_info_print_options_def_group (&opts);
  2099. gdb::option::process_options
  2100. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  2101. if (args != nullptr && *args == '\0')
  2102. args = nullptr;
  2103. print_frame_local_vars
  2104. (get_selected_frame (_("No frame selected.")),
  2105. opts.quiet, args,
  2106. opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
  2107. 0, gdb_stdout);
  2108. }
  2109. /* Iterate over all the argument variables in block B. */
  2110. void
  2111. iterate_over_block_arg_vars (const struct block *b,
  2112. iterate_over_block_arg_local_vars_cb cb)
  2113. {
  2114. struct block_iterator iter;
  2115. struct symbol *sym, *sym2;
  2116. ALL_BLOCK_SYMBOLS (b, iter, sym)
  2117. {
  2118. /* Don't worry about things which aren't arguments. */
  2119. if (sym->is_argument ())
  2120. {
  2121. /* We have to look up the symbol because arguments can have
  2122. two entries (one a parameter, one a local) and the one we
  2123. want is the local, which lookup_symbol will find for us.
  2124. This includes gcc1 (not gcc2) on the sparc when passing a
  2125. small structure and gcc2 when the argument type is float
  2126. and it is passed as a double and converted to float by
  2127. the prologue (in the latter case the type of the LOC_ARG
  2128. symbol is double and the type of the LOC_LOCAL symbol is
  2129. float). There are also LOC_ARG/LOC_REGISTER pairs which
  2130. are not combined in symbol-reading. */
  2131. sym2 = lookup_symbol_search_name (sym->search_name (),
  2132. b, VAR_DOMAIN).symbol;
  2133. cb (sym->print_name (), sym2);
  2134. }
  2135. }
  2136. }
  2137. /* Print all argument variables of the function of FRAME.
  2138. Print them with values to STREAM.
  2139. If REGEXP is not NULL, only print argument variables whose name
  2140. matches REGEXP.
  2141. If T_REGEXP is not NULL, only print argument variables whose type
  2142. matches T_REGEXP.
  2143. If no argument variables have been printed and !QUIET, prints a message
  2144. explaining why no argument variables could be printed.
  2145. This function will invalidate FRAME. */
  2146. static void
  2147. print_frame_arg_vars (struct frame_info *frame,
  2148. bool quiet,
  2149. const char *regexp, const char *t_regexp,
  2150. struct ui_file *stream)
  2151. {
  2152. struct print_variable_and_value_data cb_data;
  2153. struct symbol *func;
  2154. CORE_ADDR pc;
  2155. gdb::optional<compiled_regex> preg;
  2156. gdb::optional<compiled_regex> treg;
  2157. if (!get_frame_pc_if_available (frame, &pc))
  2158. {
  2159. if (!quiet)
  2160. gdb_printf (stream,
  2161. _("PC unavailable, cannot determine args.\n"));
  2162. return;
  2163. }
  2164. func = get_frame_function (frame);
  2165. if (func == NULL)
  2166. {
  2167. if (!quiet)
  2168. gdb_printf (stream, _("No symbol table info available.\n"));
  2169. return;
  2170. }
  2171. prepare_reg (regexp, &cb_data.preg);
  2172. prepare_reg (t_regexp, &cb_data.treg);
  2173. cb_data.frame_id = get_frame_id (frame);
  2174. cb_data.num_tabs = 0;
  2175. cb_data.stream = stream;
  2176. cb_data.values_printed = 0;
  2177. iterate_over_block_arg_vars (SYMBOL_BLOCK_VALUE (func), cb_data);
  2178. /* do_print_variable_and_value invalidates FRAME. */
  2179. frame = NULL;
  2180. if (!cb_data.values_printed && !quiet)
  2181. {
  2182. if (regexp == NULL && t_regexp == NULL)
  2183. gdb_printf (stream, _("No arguments.\n"));
  2184. else
  2185. gdb_printf (stream, _("No matching arguments.\n"));
  2186. }
  2187. }
  2188. /* Implement the 'info args' command. */
  2189. void
  2190. info_args_command (const char *args, int from_tty)
  2191. {
  2192. info_print_options opts;
  2193. auto grp = make_info_print_options_def_group (&opts);
  2194. gdb::option::process_options
  2195. (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
  2196. if (args != nullptr && *args == '\0')
  2197. args = nullptr;
  2198. print_frame_arg_vars
  2199. (get_selected_frame (_("No frame selected.")),
  2200. opts.quiet, args,
  2201. opts.type_regexp.empty () ? nullptr : opts.type_regexp.c_str (),
  2202. gdb_stdout);
  2203. }
  2204. /* Return the symbol-block in which the selected frame is executing.
  2205. Can return zero under various legitimate circumstances.
  2206. If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
  2207. code address within the block returned. We use this to decide
  2208. which macros are in scope. */
  2209. const struct block *
  2210. get_selected_block (CORE_ADDR *addr_in_block)
  2211. {
  2212. if (!has_stack_frames ())
  2213. return 0;
  2214. return get_frame_block (get_selected_frame (NULL), addr_in_block);
  2215. }
  2216. /* Find a frame a certain number of levels away from FRAME.
  2217. LEVEL_OFFSET_PTR points to an int containing the number of levels.
  2218. Positive means go to earlier frames (up); negative, the reverse.
  2219. The int that contains the number of levels is counted toward
  2220. zero as the frames for those levels are found.
  2221. If the top or bottom frame is reached, that frame is returned,
  2222. but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
  2223. how much farther the original request asked to go. */
  2224. struct frame_info *
  2225. find_relative_frame (struct frame_info *frame, int *level_offset_ptr)
  2226. {
  2227. /* Going up is simple: just call get_prev_frame enough times or
  2228. until the initial frame is reached. */
  2229. while (*level_offset_ptr > 0)
  2230. {
  2231. struct frame_info *prev = get_prev_frame (frame);
  2232. if (!prev)
  2233. break;
  2234. (*level_offset_ptr)--;
  2235. frame = prev;
  2236. }
  2237. /* Going down is just as simple. */
  2238. while (*level_offset_ptr < 0)
  2239. {
  2240. struct frame_info *next = get_next_frame (frame);
  2241. if (!next)
  2242. break;
  2243. (*level_offset_ptr)++;
  2244. frame = next;
  2245. }
  2246. return frame;
  2247. }
  2248. /* Select the frame up one or COUNT_EXP stack levels from the
  2249. previously selected frame, and print it briefly. */
  2250. static void
  2251. up_silently_base (const char *count_exp)
  2252. {
  2253. struct frame_info *frame;
  2254. int count = 1;
  2255. if (count_exp)
  2256. count = parse_and_eval_long (count_exp);
  2257. frame = find_relative_frame (get_selected_frame ("No stack."), &count);
  2258. if (count != 0 && count_exp == NULL)
  2259. error (_("Initial frame selected; you cannot go up."));
  2260. select_frame (frame);
  2261. }
  2262. static void
  2263. up_silently_command (const char *count_exp, int from_tty)
  2264. {
  2265. up_silently_base (count_exp);
  2266. }
  2267. static void
  2268. up_command (const char *count_exp, int from_tty)
  2269. {
  2270. up_silently_base (count_exp);
  2271. gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
  2272. }
  2273. /* Select the frame down one or COUNT_EXP stack levels from the previously
  2274. selected frame, and print it briefly. */
  2275. static void
  2276. down_silently_base (const char *count_exp)
  2277. {
  2278. struct frame_info *frame;
  2279. int count = -1;
  2280. if (count_exp)
  2281. count = -parse_and_eval_long (count_exp);
  2282. frame = find_relative_frame (get_selected_frame ("No stack."), &count);
  2283. if (count != 0 && count_exp == NULL)
  2284. {
  2285. /* We only do this if COUNT_EXP is not specified. That way
  2286. "down" means to really go down (and let me know if that is
  2287. impossible), but "down 9999" can be used to mean go all the
  2288. way down without getting an error. */
  2289. error (_("Bottom (innermost) frame selected; you cannot go down."));
  2290. }
  2291. select_frame (frame);
  2292. }
  2293. static void
  2294. down_silently_command (const char *count_exp, int from_tty)
  2295. {
  2296. down_silently_base (count_exp);
  2297. }
  2298. static void
  2299. down_command (const char *count_exp, int from_tty)
  2300. {
  2301. down_silently_base (count_exp);
  2302. gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
  2303. }
  2304. void
  2305. return_command (const char *retval_exp, int from_tty)
  2306. {
  2307. /* Initialize it just to avoid a GCC false warning. */
  2308. enum return_value_convention rv_conv = RETURN_VALUE_STRUCT_CONVENTION;
  2309. struct frame_info *thisframe;
  2310. struct gdbarch *gdbarch;
  2311. struct symbol *thisfun;
  2312. struct value *return_value = NULL;
  2313. struct value *function = NULL;
  2314. std::string query_prefix;
  2315. thisframe = get_selected_frame ("No selected frame.");
  2316. thisfun = get_frame_function (thisframe);
  2317. gdbarch = get_frame_arch (thisframe);
  2318. if (get_frame_type (get_current_frame ()) == INLINE_FRAME)
  2319. error (_("Can not force return from an inlined function."));
  2320. /* Compute the return value. If the computation triggers an error,
  2321. let it bail. If the return type can't be handled, set
  2322. RETURN_VALUE to NULL, and QUERY_PREFIX to an informational
  2323. message. */
  2324. if (retval_exp)
  2325. {
  2326. expression_up retval_expr = parse_expression (retval_exp);
  2327. struct type *return_type = NULL;
  2328. /* Compute the return value. Should the computation fail, this
  2329. call throws an error. */
  2330. return_value = evaluate_expression (retval_expr.get ());
  2331. /* Cast return value to the return type of the function. Should
  2332. the cast fail, this call throws an error. */
  2333. if (thisfun != NULL)
  2334. return_type = TYPE_TARGET_TYPE (thisfun->type ());
  2335. if (return_type == NULL)
  2336. {
  2337. if (retval_expr->first_opcode () != UNOP_CAST
  2338. && retval_expr->first_opcode () != UNOP_CAST_TYPE)
  2339. error (_("Return value type not available for selected "
  2340. "stack frame.\n"
  2341. "Please use an explicit cast of the value to return."));
  2342. return_type = value_type (return_value);
  2343. }
  2344. return_type = check_typedef (return_type);
  2345. return_value = value_cast (return_type, return_value);
  2346. /* Make sure the value is fully evaluated. It may live in the
  2347. stack frame we're about to pop. */
  2348. if (value_lazy (return_value))
  2349. value_fetch_lazy (return_value);
  2350. if (thisfun != NULL)
  2351. function = read_var_value (thisfun, NULL, thisframe);
  2352. rv_conv = RETURN_VALUE_REGISTER_CONVENTION;
  2353. if (return_type->code () == TYPE_CODE_VOID)
  2354. /* If the return-type is "void", don't try to find the
  2355. return-value's location. However, do still evaluate the
  2356. return expression so that, even when the expression result
  2357. is discarded, side effects such as "return i++" still
  2358. occur. */
  2359. return_value = NULL;
  2360. else if (thisfun != NULL)
  2361. {
  2362. if (is_nocall_function (check_typedef (value_type (function))))
  2363. {
  2364. query_prefix =
  2365. string_printf ("Function '%s' does not follow the target "
  2366. "calling convention.\n"
  2367. "If you continue, setting the return value "
  2368. "will probably lead to unpredictable "
  2369. "behaviors.\n",
  2370. thisfun->print_name ());
  2371. }
  2372. rv_conv = struct_return_convention (gdbarch, function, return_type);
  2373. if (rv_conv == RETURN_VALUE_STRUCT_CONVENTION
  2374. || rv_conv == RETURN_VALUE_ABI_RETURNS_ADDRESS)
  2375. {
  2376. query_prefix = "The location at which to store the "
  2377. "function's return value is unknown.\n"
  2378. "If you continue, the return value "
  2379. "that you specified will be ignored.\n";
  2380. return_value = NULL;
  2381. }
  2382. }
  2383. }
  2384. /* Does an interactive user really want to do this? Include
  2385. information, such as how well GDB can handle the return value, in
  2386. the query message. */
  2387. if (from_tty)
  2388. {
  2389. int confirmed;
  2390. if (thisfun == NULL)
  2391. confirmed = query (_("%sMake selected stack frame return now? "),
  2392. query_prefix.c_str ());
  2393. else
  2394. {
  2395. if (TYPE_NO_RETURN (thisfun->type ()))
  2396. warning (_("Function does not return normally to caller."));
  2397. confirmed = query (_("%sMake %s return now? "),
  2398. query_prefix.c_str (),
  2399. thisfun->print_name ());
  2400. }
  2401. if (!confirmed)
  2402. error (_("Not confirmed"));
  2403. }
  2404. /* Discard the selected frame and all frames inner-to it. */
  2405. frame_pop (get_selected_frame (NULL));
  2406. /* Store RETURN_VALUE in the just-returned register set. */
  2407. if (return_value != NULL)
  2408. {
  2409. struct type *return_type = value_type (return_value);
  2410. struct gdbarch *cache_arch = get_current_regcache ()->arch ();
  2411. gdb_assert (rv_conv != RETURN_VALUE_STRUCT_CONVENTION
  2412. && rv_conv != RETURN_VALUE_ABI_RETURNS_ADDRESS);
  2413. gdbarch_return_value (cache_arch, function, return_type,
  2414. get_current_regcache (), NULL /*read*/,
  2415. value_contents (return_value).data () /*write*/);
  2416. }
  2417. /* If we are at the end of a call dummy now, pop the dummy frame
  2418. too. */
  2419. if (get_frame_type (get_current_frame ()) == DUMMY_FRAME)
  2420. frame_pop (get_current_frame ());
  2421. select_frame (get_current_frame ());
  2422. /* If interactive, print the frame that is now current. */
  2423. if (from_tty)
  2424. print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  2425. }
  2426. /* Find the most inner frame in the current stack for a function called
  2427. FUNCTION_NAME. If no matching frame is found return NULL. */
  2428. static struct frame_info *
  2429. find_frame_for_function (const char *function_name)
  2430. {
  2431. /* Used to hold the lower and upper addresses for each of the
  2432. SYMTAB_AND_LINEs found for functions matching FUNCTION_NAME. */
  2433. struct function_bounds
  2434. {
  2435. CORE_ADDR low, high;
  2436. };
  2437. struct frame_info *frame;
  2438. bool found = false;
  2439. int level = 1;
  2440. gdb_assert (function_name != NULL);
  2441. frame = get_current_frame ();
  2442. std::vector<symtab_and_line> sals
  2443. = decode_line_with_current_source (function_name,
  2444. DECODE_LINE_FUNFIRSTLINE);
  2445. gdb::def_vector<function_bounds> func_bounds (sals.size ());
  2446. for (size_t i = 0; i < sals.size (); i++)
  2447. {
  2448. if (sals[i].pspace != current_program_space)
  2449. func_bounds[i].low = func_bounds[i].high = 0;
  2450. else if (sals[i].pc == 0
  2451. || find_pc_partial_function (sals[i].pc, NULL,
  2452. &func_bounds[i].low,
  2453. &func_bounds[i].high) == 0)
  2454. func_bounds[i].low = func_bounds[i].high = 0;
  2455. }
  2456. do
  2457. {
  2458. for (size_t i = 0; (i < sals.size () && !found); i++)
  2459. found = (get_frame_pc (frame) >= func_bounds[i].low
  2460. && get_frame_pc (frame) < func_bounds[i].high);
  2461. if (!found)
  2462. {
  2463. level = 1;
  2464. frame = find_relative_frame (frame, &level);
  2465. }
  2466. }
  2467. while (!found && level == 0);
  2468. if (!found)
  2469. frame = NULL;
  2470. return frame;
  2471. }
  2472. /* The qcs command line flags for the "frame apply" commands. Keep
  2473. this in sync with the "thread apply" commands. */
  2474. using qcs_flag_option_def
  2475. = gdb::option::flag_option_def<qcs_flags>;
  2476. static const gdb::option::option_def fr_qcs_flags_option_defs[] = {
  2477. qcs_flag_option_def {
  2478. "q", [] (qcs_flags *opt) { return &opt->quiet; },
  2479. N_("Disables printing the frame location information."),
  2480. },
  2481. qcs_flag_option_def {
  2482. "c", [] (qcs_flags *opt) { return &opt->cont; },
  2483. N_("Print any error raised by COMMAND and continue."),
  2484. },
  2485. qcs_flag_option_def {
  2486. "s", [] (qcs_flags *opt) { return &opt->silent; },
  2487. N_("Silently ignore any errors or empty output produced by COMMAND."),
  2488. },
  2489. };
  2490. /* Create an option_def_group array for all the "frame apply" options,
  2491. with FLAGS and SET_BT_OPTS as context. */
  2492. static inline std::array<gdb::option::option_def_group, 2>
  2493. make_frame_apply_options_def_group (qcs_flags *flags,
  2494. set_backtrace_options *set_bt_opts)
  2495. {
  2496. return {{
  2497. { {fr_qcs_flags_option_defs}, flags },
  2498. { {set_backtrace_option_defs}, set_bt_opts },
  2499. }};
  2500. }
  2501. /* Apply a GDB command to all stack frames, or a set of identified frames,
  2502. or innermost COUNT frames.
  2503. With a negative COUNT, apply command on outermost -COUNT frames.
  2504. frame apply 3 info frame Apply 'info frame' to frames 0, 1, 2
  2505. frame apply -3 info frame Apply 'info frame' to outermost 3 frames.
  2506. frame apply all x/i $pc Apply 'x/i $pc' cmd to all frames.
  2507. frame apply all -s p local_var_no_idea_in_which_frame
  2508. If a frame has a local variable called
  2509. local_var_no_idea_in_which_frame, print frame
  2510. and value of local_var_no_idea_in_which_frame.
  2511. frame apply all -s -q p local_var_no_idea_in_which_frame
  2512. Same as before, but only print the variable value.
  2513. frame apply level 2-5 0 4-7 -s p i = i + 1
  2514. Adds 1 to the variable i in the specified frames.
  2515. Note that i will be incremented twice in
  2516. frames 4 and 5. */
  2517. /* Apply a GDB command to COUNT stack frames, starting at TRAILING.
  2518. CMD starts with 0 or more qcs flags followed by the GDB command to apply.
  2519. COUNT -1 means all frames starting at TRAILING. WHICH_COMMAND is used
  2520. for error messages. */
  2521. static void
  2522. frame_apply_command_count (const char *which_command,
  2523. const char *cmd, int from_tty,
  2524. struct frame_info *trailing, int count)
  2525. {
  2526. qcs_flags flags;
  2527. set_backtrace_options set_bt_opts = user_set_backtrace_options;
  2528. auto group = make_frame_apply_options_def_group (&flags, &set_bt_opts);
  2529. gdb::option::process_options
  2530. (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
  2531. validate_flags_qcs (which_command, &flags);
  2532. if (cmd == NULL || *cmd == '\0')
  2533. error (_("Please specify a command to apply on the selected frames"));
  2534. /* The below will restore the current inferior/thread/frame.
  2535. Usually, only the frame is effectively to be restored.
  2536. But in case CMD switches of inferior/thread, better restore
  2537. these also. */
  2538. scoped_restore_current_thread restore_thread;
  2539. /* These options are handled quite deep in the unwind machinery, so
  2540. we get to pass them down by swapping globals. */
  2541. scoped_restore restore_set_backtrace_options
  2542. = make_scoped_restore (&user_set_backtrace_options, set_bt_opts);
  2543. for (frame_info *fi = trailing; fi && count--; fi = get_prev_frame (fi))
  2544. {
  2545. QUIT;
  2546. select_frame (fi);
  2547. try
  2548. {
  2549. std::string cmd_result;
  2550. {
  2551. /* In case CMD switches of inferior/thread/frame, the below
  2552. restores the inferior/thread/frame. FI can then be
  2553. set to the selected frame. */
  2554. scoped_restore_current_thread restore_fi_current_frame;
  2555. execute_command_to_string
  2556. (cmd_result, cmd, from_tty, gdb_stdout->term_out ());
  2557. }
  2558. fi = get_selected_frame (_("frame apply "
  2559. "unable to get selected frame."));
  2560. if (!flags.silent || cmd_result.length () > 0)
  2561. {
  2562. if (!flags.quiet)
  2563. print_stack_frame (fi, 1, LOCATION, 0);
  2564. gdb_printf ("%s", cmd_result.c_str ());
  2565. }
  2566. }
  2567. catch (const gdb_exception_error &ex)
  2568. {
  2569. fi = get_selected_frame (_("frame apply "
  2570. "unable to get selected frame."));
  2571. if (!flags.silent)
  2572. {
  2573. if (!flags.quiet)
  2574. print_stack_frame (fi, 1, LOCATION, 0);
  2575. if (flags.cont)
  2576. gdb_printf ("%s\n", ex.what ());
  2577. else
  2578. throw;
  2579. }
  2580. }
  2581. }
  2582. }
  2583. /* Completer for the "frame apply ..." commands. */
  2584. static void
  2585. frame_apply_completer (completion_tracker &tracker, const char *text)
  2586. {
  2587. const auto group = make_frame_apply_options_def_group (nullptr, nullptr);
  2588. if (gdb::option::complete_options
  2589. (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
  2590. return;
  2591. complete_nested_command_line (tracker, text);
  2592. }
  2593. /* Completer for the "frame apply" commands. */
  2594. static void
  2595. frame_apply_level_cmd_completer (struct cmd_list_element *ignore,
  2596. completion_tracker &tracker,
  2597. const char *text, const char */*word*/)
  2598. {
  2599. /* Do this explicitly because there's an early return below. */
  2600. tracker.set_use_custom_word_point (true);
  2601. number_or_range_parser levels (text);
  2602. /* Skip the LEVEL list to find the options and command args. */
  2603. try
  2604. {
  2605. while (!levels.finished ())
  2606. {
  2607. /* Call for effect. */
  2608. levels.get_number ();
  2609. if (levels.in_range ())
  2610. levels.skip_range ();
  2611. }
  2612. }
  2613. catch (const gdb_exception_error &ex)
  2614. {
  2615. /* get_number throws if it parses a negative number, for
  2616. example. But a seemingly negative number may be the start of
  2617. an option instead. */
  2618. }
  2619. const char *cmd = levels.cur_tok ();
  2620. if (cmd == text)
  2621. {
  2622. /* No level list yet. */
  2623. return;
  2624. }
  2625. /* Check if we're past a valid LEVEL already. */
  2626. if (levels.finished ()
  2627. && cmd > text && !isspace (cmd[-1]))
  2628. return;
  2629. /* We're past LEVELs, advance word point. */
  2630. tracker.advance_custom_word_point_by (cmd - text);
  2631. text = cmd;
  2632. frame_apply_completer (tracker, text);
  2633. }
  2634. /* Completer for the "frame apply all" command. */
  2635. void
  2636. frame_apply_all_cmd_completer (struct cmd_list_element *ignore,
  2637. completion_tracker &tracker,
  2638. const char *text, const char */*word*/)
  2639. {
  2640. frame_apply_completer (tracker, text);
  2641. }
  2642. /* Completer for the "frame apply COUNT" command. */
  2643. static void
  2644. frame_apply_cmd_completer (struct cmd_list_element *ignore,
  2645. completion_tracker &tracker,
  2646. const char *text, const char */*word*/)
  2647. {
  2648. const char *cmd = text;
  2649. int count = get_number_trailer (&cmd, 0);
  2650. if (count == 0)
  2651. return;
  2652. /* Check if we're past a valid COUNT already. */
  2653. if (cmd > text && !isspace (cmd[-1]))
  2654. return;
  2655. /* We're past COUNT, advance word point. */
  2656. tracker.advance_custom_word_point_by (cmd - text);
  2657. text = cmd;
  2658. frame_apply_completer (tracker, text);
  2659. }
  2660. /* Implementation of the "frame apply level" command. */
  2661. static void
  2662. frame_apply_level_command (const char *cmd, int from_tty)
  2663. {
  2664. if (!target_has_stack ())
  2665. error (_("No stack."));
  2666. bool level_found = false;
  2667. const char *levels_str = cmd;
  2668. number_or_range_parser levels (levels_str);
  2669. /* Skip the LEVEL list to find the flags and command args. */
  2670. while (!levels.finished ())
  2671. {
  2672. /* Call for effect. */
  2673. levels.get_number ();
  2674. level_found = true;
  2675. if (levels.in_range ())
  2676. levels.skip_range ();
  2677. }
  2678. if (!level_found)
  2679. error (_("Missing or invalid LEVEL... argument"));
  2680. cmd = levels.cur_tok ();
  2681. /* Redo the LEVELS parsing, but applying COMMAND. */
  2682. levels.init (levels_str);
  2683. while (!levels.finished ())
  2684. {
  2685. const int level_beg = levels.get_number ();
  2686. int n_frames;
  2687. if (levels.in_range ())
  2688. {
  2689. n_frames = levels.end_value () - level_beg + 1;
  2690. levels.skip_range ();
  2691. }
  2692. else
  2693. n_frames = 1;
  2694. frame_apply_command_count ("frame apply level", cmd, from_tty,
  2695. leading_innermost_frame (level_beg), n_frames);
  2696. }
  2697. }
  2698. /* Implementation of the "frame apply all" command. */
  2699. static void
  2700. frame_apply_all_command (const char *cmd, int from_tty)
  2701. {
  2702. if (!target_has_stack ())
  2703. error (_("No stack."));
  2704. frame_apply_command_count ("frame apply all", cmd, from_tty,
  2705. get_current_frame (), INT_MAX);
  2706. }
  2707. /* Implementation of the "frame apply" command. */
  2708. static void
  2709. frame_apply_command (const char* cmd, int from_tty)
  2710. {
  2711. int count;
  2712. struct frame_info *trailing;
  2713. if (!target_has_stack ())
  2714. error (_("No stack."));
  2715. if (cmd == NULL)
  2716. error (_("Missing COUNT argument."));
  2717. count = get_number_trailer (&cmd, 0);
  2718. if (count == 0)
  2719. error (_("Invalid COUNT argument."));
  2720. if (count < 0)
  2721. {
  2722. trailing = trailing_outermost_frame (-count);
  2723. count = -1;
  2724. }
  2725. else
  2726. trailing = get_current_frame ();
  2727. frame_apply_command_count ("frame apply", cmd, from_tty,
  2728. trailing, count);
  2729. }
  2730. /* Implementation of the "faas" command. */
  2731. static void
  2732. faas_command (const char *cmd, int from_tty)
  2733. {
  2734. if (cmd == NULL || *cmd == '\0')
  2735. error (_("Please specify a command to apply on all frames"));
  2736. std::string expanded = std::string ("frame apply all -s ") + cmd;
  2737. execute_command (expanded.c_str (), from_tty);
  2738. }
  2739. /* Find inner-mode frame with frame address ADDRESS. Return NULL if no
  2740. matching frame can be found. */
  2741. static struct frame_info *
  2742. find_frame_for_address (CORE_ADDR address)
  2743. {
  2744. struct frame_id id;
  2745. struct frame_info *fid;
  2746. id = frame_id_build_wild (address);
  2747. /* If (s)he specifies the frame with an address, he deserves
  2748. what (s)he gets. Still, give the highest one that matches.
  2749. (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
  2750. know). */
  2751. for (fid = get_current_frame ();
  2752. fid != NULL;
  2753. fid = get_prev_frame (fid))
  2754. {
  2755. if (frame_id_eq (id, get_frame_id (fid)))
  2756. {
  2757. struct frame_info *prev_frame;
  2758. while (1)
  2759. {
  2760. prev_frame = get_prev_frame (fid);
  2761. if (!prev_frame
  2762. || !frame_id_eq (id, get_frame_id (prev_frame)))
  2763. break;
  2764. fid = prev_frame;
  2765. }
  2766. return fid;
  2767. }
  2768. }
  2769. return NULL;
  2770. }
  2771. /* Commands with a prefix of `frame apply'. */
  2772. static struct cmd_list_element *frame_apply_cmd_list = NULL;
  2773. /* Commands with a prefix of `frame'. */
  2774. static struct cmd_list_element *frame_cmd_list = NULL;
  2775. /* Commands with a prefix of `select frame'. */
  2776. static struct cmd_list_element *select_frame_cmd_list = NULL;
  2777. /* Commands with a prefix of `info frame'. */
  2778. static struct cmd_list_element *info_frame_cmd_list = NULL;
  2779. void _initialize_stack ();
  2780. void
  2781. _initialize_stack ()
  2782. {
  2783. struct cmd_list_element *cmd;
  2784. add_com ("return", class_stack, return_command, _("\
  2785. Make selected stack frame return to its caller.\n\
  2786. Control remains in the debugger, but when you continue\n\
  2787. execution will resume in the frame above the one now selected.\n\
  2788. If an argument is given, it is an expression for the value to return."));
  2789. add_com ("up", class_stack, up_command, _("\
  2790. Select and print stack frame that called this one.\n\
  2791. An argument says how many frames up to go."));
  2792. add_com ("up-silently", class_support, up_silently_command, _("\
  2793. Same as the `up' command, but does not print anything.\n\
  2794. This is useful in command scripts."));
  2795. cmd_list_element *down_cmd
  2796. = add_com ("down", class_stack, down_command, _("\
  2797. Select and print stack frame called by this one.\n\
  2798. An argument says how many frames down to go."));
  2799. add_com_alias ("do", down_cmd, class_stack, 1);
  2800. add_com_alias ("dow", down_cmd, class_stack, 1);
  2801. add_com ("down-silently", class_support, down_silently_command, _("\
  2802. Same as the `down' command, but does not print anything.\n\
  2803. This is useful in command scripts."));
  2804. cmd_list_element *frame_cmd_el
  2805. = add_prefix_cmd ("frame", class_stack,
  2806. &frame_cmd.base_command, _("\
  2807. Select and print a stack frame.\n\
  2808. With no argument, print the selected stack frame. (See also \"info frame\").\n\
  2809. A single numerical argument specifies the frame to select."),
  2810. &frame_cmd_list, 1, &cmdlist);
  2811. add_com_alias ("f", frame_cmd_el, class_stack, 1);
  2812. #define FRAME_APPLY_OPTION_HELP "\
  2813. Prints the frame location information followed by COMMAND output.\n\
  2814. \n\
  2815. By default, an error raised during the execution of COMMAND\n\
  2816. aborts \"frame apply\".\n\
  2817. \n\
  2818. Options:\n\
  2819. %OPTIONS%"
  2820. const auto frame_apply_opts
  2821. = make_frame_apply_options_def_group (nullptr, nullptr);
  2822. static std::string frame_apply_cmd_help = gdb::option::build_help (_("\
  2823. Apply a command to a number of frames.\n\
  2824. Usage: frame apply COUNT [OPTION]... COMMAND\n\
  2825. With a negative COUNT argument, applies the command on outermost -COUNT frames.\n"
  2826. FRAME_APPLY_OPTION_HELP),
  2827. frame_apply_opts);
  2828. cmd = add_prefix_cmd ("apply", class_stack, frame_apply_command,
  2829. frame_apply_cmd_help.c_str (),
  2830. &frame_apply_cmd_list, 1,
  2831. &frame_cmd_list);
  2832. set_cmd_completer_handle_brkchars (cmd, frame_apply_cmd_completer);
  2833. static std::string frame_apply_all_cmd_help = gdb::option::build_help (_("\
  2834. Apply a command to all frames.\n\
  2835. \n\
  2836. Usage: frame apply all [OPTION]... COMMAND\n"
  2837. FRAME_APPLY_OPTION_HELP),
  2838. frame_apply_opts);
  2839. cmd = add_cmd ("all", class_stack, frame_apply_all_command,
  2840. frame_apply_all_cmd_help.c_str (),
  2841. &frame_apply_cmd_list);
  2842. set_cmd_completer_handle_brkchars (cmd, frame_apply_all_cmd_completer);
  2843. static std::string frame_apply_level_cmd_help = gdb::option::build_help (_("\
  2844. Apply a command to a list of frames.\n\
  2845. \n\
  2846. Usage: frame apply level LEVEL... [OPTION]... COMMAND\n\
  2847. LEVEL is a space-separated list of levels of frames to apply COMMAND on.\n"
  2848. FRAME_APPLY_OPTION_HELP),
  2849. frame_apply_opts);
  2850. cmd = add_cmd ("level", class_stack, frame_apply_level_command,
  2851. frame_apply_level_cmd_help.c_str (),
  2852. &frame_apply_cmd_list);
  2853. set_cmd_completer_handle_brkchars (cmd, frame_apply_level_cmd_completer);
  2854. cmd = add_com ("faas", class_stack, faas_command, _("\
  2855. Apply a command to all frames (ignoring errors and empty output).\n\
  2856. Usage: faas [OPTION]... COMMAND\n\
  2857. shortcut for 'frame apply all -s [OPTION]... COMMAND'\n\
  2858. See \"help frame apply all\" for available options."));
  2859. set_cmd_completer_handle_brkchars (cmd, frame_apply_all_cmd_completer);
  2860. add_cmd ("address", class_stack, &frame_cmd.address,
  2861. _("\
  2862. Select and print a stack frame by stack address.\n\
  2863. \n\
  2864. Usage: frame address STACK-ADDRESS"),
  2865. &frame_cmd_list);
  2866. add_cmd ("view", class_stack, &frame_cmd.view,
  2867. _("\
  2868. View a stack frame that might be outside the current backtrace.\n\
  2869. \n\
  2870. Usage: frame view STACK-ADDRESS\n\
  2871. frame view STACK-ADDRESS PC-ADDRESS"),
  2872. &frame_cmd_list);
  2873. cmd = add_cmd ("function", class_stack, &frame_cmd.function,
  2874. _("\
  2875. Select and print a stack frame by function name.\n\
  2876. \n\
  2877. Usage: frame function NAME\n\
  2878. \n\
  2879. The innermost frame that visited function NAME is selected."),
  2880. &frame_cmd_list);
  2881. set_cmd_completer (cmd, frame_selection_by_function_completer);
  2882. add_cmd ("level", class_stack, &frame_cmd.level,
  2883. _("\
  2884. Select and print a stack frame by level.\n\
  2885. \n\
  2886. Usage: frame level LEVEL"),
  2887. &frame_cmd_list);
  2888. cmd = add_prefix_cmd_suppress_notification ("select-frame", class_stack,
  2889. &select_frame_cmd.base_command, _("\
  2890. Select a stack frame without printing anything.\n\
  2891. A single numerical argument specifies the frame to select."),
  2892. &select_frame_cmd_list, 1, &cmdlist,
  2893. &cli_suppress_notification.user_selected_context);
  2894. add_cmd_suppress_notification ("address", class_stack,
  2895. &select_frame_cmd.address, _("\
  2896. Select a stack frame by stack address.\n\
  2897. \n\
  2898. Usage: select-frame address STACK-ADDRESS"),
  2899. &select_frame_cmd_list,
  2900. &cli_suppress_notification.user_selected_context);
  2901. add_cmd_suppress_notification ("view", class_stack,
  2902. &select_frame_cmd.view, _("\
  2903. Select a stack frame that might be outside the current backtrace.\n\
  2904. \n\
  2905. Usage: select-frame view STACK-ADDRESS\n\
  2906. select-frame view STACK-ADDRESS PC-ADDRESS"),
  2907. &select_frame_cmd_list,
  2908. &cli_suppress_notification.user_selected_context);
  2909. cmd = add_cmd_suppress_notification ("function", class_stack,
  2910. &select_frame_cmd.function, _("\
  2911. Select a stack frame by function name.\n\
  2912. \n\
  2913. Usage: select-frame function NAME"),
  2914. &select_frame_cmd_list,
  2915. &cli_suppress_notification.user_selected_context);
  2916. set_cmd_completer (cmd, frame_selection_by_function_completer);
  2917. add_cmd_suppress_notification ("level", class_stack,
  2918. &select_frame_cmd.level, _("\
  2919. Select a stack frame by level.\n\
  2920. \n\
  2921. Usage: select-frame level LEVEL"),
  2922. &select_frame_cmd_list,
  2923. &cli_suppress_notification.user_selected_context);
  2924. const auto backtrace_opts
  2925. = make_backtrace_options_def_group (nullptr, nullptr, nullptr);
  2926. static std::string backtrace_help
  2927. = gdb::option::build_help (_("\
  2928. Print backtrace of all stack frames, or innermost COUNT frames.\n\
  2929. Usage: backtrace [OPTION]... [QUALIFIER]... [COUNT | -COUNT]\n\
  2930. \n\
  2931. Options:\n\
  2932. %OPTIONS%\n\
  2933. \n\
  2934. For backward compatibility, the following qualifiers are supported:\n\
  2935. \n\
  2936. full - same as -full option.\n\
  2937. no-filters - same as -no-filters option.\n\
  2938. hide - same as -hide.\n\
  2939. \n\
  2940. With a negative COUNT, print outermost -COUNT frames."),
  2941. backtrace_opts);
  2942. cmd_list_element *backtrace_cmd
  2943. = add_com ("backtrace", class_stack, backtrace_command,
  2944. backtrace_help.c_str ());
  2945. set_cmd_completer_handle_brkchars (backtrace_cmd, backtrace_command_completer);
  2946. add_com_alias ("bt", backtrace_cmd, class_stack, 0);
  2947. add_com_alias ("where", backtrace_cmd, class_stack, 0);
  2948. cmd_list_element *info_stack_cmd
  2949. = add_info ("stack", backtrace_command,
  2950. _("Backtrace of the stack, or innermost COUNT frames."));
  2951. add_info_alias ("s", info_stack_cmd, 1);
  2952. cmd_list_element *info_frame_cmd_el
  2953. = add_prefix_cmd ("frame", class_info, &info_frame_cmd.base_command,
  2954. _("All about the selected stack frame.\n\
  2955. With no arguments, displays information about the currently selected stack\n\
  2956. frame. Alternatively a frame specification may be provided (See \"frame\")\n\
  2957. the information is then printed about the specified frame."),
  2958. &info_frame_cmd_list, 1, &infolist);
  2959. add_info_alias ("f", info_frame_cmd_el, 1);
  2960. add_cmd ("address", class_stack, &info_frame_cmd.address,
  2961. _("\
  2962. Print information about a stack frame selected by stack address.\n\
  2963. \n\
  2964. Usage: info frame address STACK-ADDRESS"),
  2965. &info_frame_cmd_list);
  2966. add_cmd ("view", class_stack, &info_frame_cmd.view,
  2967. _("\
  2968. Print information about a stack frame outside the current backtrace.\n\
  2969. \n\
  2970. Usage: info frame view STACK-ADDRESS\n\
  2971. info frame view STACK-ADDRESS PC-ADDRESS"),
  2972. &info_frame_cmd_list);
  2973. cmd = add_cmd ("function", class_stack, &info_frame_cmd.function,
  2974. _("\
  2975. Print information about a stack frame selected by function name.\n\
  2976. \n\
  2977. Usage: info frame function NAME"),
  2978. &info_frame_cmd_list);
  2979. set_cmd_completer (cmd, frame_selection_by_function_completer);
  2980. add_cmd ("level", class_stack, &info_frame_cmd.level,
  2981. _("\
  2982. Print information about a stack frame selected by level.\n\
  2983. \n\
  2984. Usage: info frame level LEVEL"),
  2985. &info_frame_cmd_list);
  2986. cmd = add_info ("locals", info_locals_command,
  2987. info_print_args_help (_("\
  2988. All local variables of current stack frame or those matching REGEXPs.\n\
  2989. Usage: info locals [-q] [-t TYPEREGEXP] [NAMEREGEXP]\n\
  2990. Prints the local variables of the current stack frame.\n"),
  2991. _("local variables"),
  2992. false));
  2993. set_cmd_completer_handle_brkchars (cmd, info_print_command_completer);
  2994. cmd = add_info ("args", info_args_command,
  2995. info_print_args_help (_("\
  2996. All argument variables of current stack frame or those matching REGEXPs.\n\
  2997. Usage: info args [-q] [-t TYPEREGEXP] [NAMEREGEXP]\n\
  2998. Prints the argument variables of the current stack frame.\n"),
  2999. _("argument variables"),
  3000. false));
  3001. set_cmd_completer_handle_brkchars (cmd, info_print_command_completer);
  3002. /* Install "set print raw frame-arguments", a deprecated spelling of
  3003. "set print raw-frame-arguments". */
  3004. set_show_commands set_show_frame_args
  3005. = add_setshow_boolean_cmd
  3006. ("frame-arguments", no_class,
  3007. &user_frame_print_options.print_raw_frame_arguments,
  3008. _("\
  3009. Set whether to print frame arguments in raw form."), _("\
  3010. Show whether to print frame arguments in raw form."), _("\
  3011. If set, frame arguments are printed in raw form, bypassing any\n\
  3012. pretty-printers for that value."),
  3013. NULL, NULL,
  3014. &setprintrawlist, &showprintrawlist);
  3015. deprecate_cmd (set_show_frame_args.set, "set print raw-frame-arguments");
  3016. add_setshow_auto_boolean_cmd ("disassemble-next-line", class_stack,
  3017. &disassemble_next_line, _("\
  3018. Set whether to disassemble next source line or insn when execution stops."),
  3019. _("\
  3020. Show whether to disassemble next source line or insn when execution stops."),
  3021. _("\
  3022. If ON, GDB will display disassembly of the next source line, in addition\n\
  3023. to displaying the source line itself. If the next source line cannot\n\
  3024. be displayed (e.g., source is unavailable or there's no line info), GDB\n\
  3025. will display disassembly of next instruction instead of showing the\n\
  3026. source line.\n\
  3027. If AUTO, display disassembly of next instruction only if the source line\n\
  3028. cannot be displayed.\n\
  3029. If OFF (which is the default), never display the disassembly of the next\n\
  3030. source line."),
  3031. NULL,
  3032. show_disassemble_next_line,
  3033. &setlist, &showlist);
  3034. disassemble_next_line = AUTO_BOOLEAN_FALSE;
  3035. gdb::option::add_setshow_cmds_for_options
  3036. (class_stack, &user_frame_print_options,
  3037. frame_print_option_defs, &setprintlist, &showprintlist);
  3038. }