printcmd.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447
  1. /* Print values for GNU debugger GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "value.h"
  19. #include "language.h"
  20. #include "c-lang.h"
  21. #include "expression.h"
  22. #include "gdbcore.h"
  23. #include "gdbcmd.h"
  24. #include "target.h"
  25. #include "breakpoint.h"
  26. #include "demangle.h"
  27. #include "gdb-demangle.h"
  28. #include "valprint.h"
  29. #include "annotate.h"
  30. #include "symfile.h" /* for overlay functions */
  31. #include "objfiles.h" /* ditto */
  32. #include "completer.h" /* for completion functions */
  33. #include "ui-out.h"
  34. #include "block.h"
  35. #include "disasm.h"
  36. #include "target-float.h"
  37. #include "observable.h"
  38. #include "solist.h"
  39. #include "parser-defs.h"
  40. #include "charset.h"
  41. #include "arch-utils.h"
  42. #include "cli/cli-utils.h"
  43. #include "cli/cli-option.h"
  44. #include "cli/cli-script.h"
  45. #include "cli/cli-style.h"
  46. #include "gdbsupport/format.h"
  47. #include "source.h"
  48. #include "gdbsupport/byte-vector.h"
  49. #include "gdbsupport/gdb_optional.h"
  50. #include "safe-ctype.h"
  51. #include "gdbsupport/rsp-low.h"
  52. /* Chain containing all defined memory-tag subcommands. */
  53. static struct cmd_list_element *memory_tag_list;
  54. /* Last specified output format. */
  55. static char last_format = 0;
  56. /* Last specified examination size. 'b', 'h', 'w' or `q'. */
  57. static char last_size = 'w';
  58. /* Last specified count for the 'x' command. */
  59. static int last_count;
  60. /* Last specified tag-printing option. */
  61. static bool last_print_tags = false;
  62. /* Default address to examine next, and associated architecture. */
  63. static struct gdbarch *next_gdbarch;
  64. static CORE_ADDR next_address;
  65. /* Number of delay instructions following current disassembled insn. */
  66. static int branch_delay_insns;
  67. /* Last address examined. */
  68. static CORE_ADDR last_examine_address;
  69. /* Contents of last address examined.
  70. This is not valid past the end of the `x' command! */
  71. static value_ref_ptr last_examine_value;
  72. /* Largest offset between a symbolic value and an address, that will be
  73. printed as `0x1234 <symbol+offset>'. */
  74. static unsigned int max_symbolic_offset = UINT_MAX;
  75. static void
  76. show_max_symbolic_offset (struct ui_file *file, int from_tty,
  77. struct cmd_list_element *c, const char *value)
  78. {
  79. gdb_printf (file,
  80. _("The largest offset that will be "
  81. "printed in <symbol+1234> form is %s.\n"),
  82. value);
  83. }
  84. /* Append the source filename and linenumber of the symbol when
  85. printing a symbolic value as `<symbol at filename:linenum>' if set. */
  86. static bool print_symbol_filename = false;
  87. static void
  88. show_print_symbol_filename (struct ui_file *file, int from_tty,
  89. struct cmd_list_element *c, const char *value)
  90. {
  91. gdb_printf (file, _("Printing of source filename and "
  92. "line number with <symbol> is %s.\n"),
  93. value);
  94. }
  95. /* Number of auto-display expression currently being displayed.
  96. So that we can disable it if we get a signal within it.
  97. -1 when not doing one. */
  98. static int current_display_number;
  99. /* Last allocated display number. */
  100. static int display_number;
  101. struct display
  102. {
  103. display (const char *exp_string_, expression_up &&exp_,
  104. const struct format_data &format_, struct program_space *pspace_,
  105. const struct block *block_)
  106. : exp_string (exp_string_),
  107. exp (std::move (exp_)),
  108. number (++display_number),
  109. format (format_),
  110. pspace (pspace_),
  111. block (block_),
  112. enabled_p (true)
  113. {
  114. }
  115. /* The expression as the user typed it. */
  116. std::string exp_string;
  117. /* Expression to be evaluated and displayed. */
  118. expression_up exp;
  119. /* Item number of this auto-display item. */
  120. int number;
  121. /* Display format specified. */
  122. struct format_data format;
  123. /* Program space associated with `block'. */
  124. struct program_space *pspace;
  125. /* Innermost block required by this expression when evaluated. */
  126. const struct block *block;
  127. /* Status of this display (enabled or disabled). */
  128. bool enabled_p;
  129. };
  130. /* Expressions whose values should be displayed automatically each
  131. time the program stops. */
  132. static std::vector<std::unique_ptr<struct display>> all_displays;
  133. /* Prototypes for local functions. */
  134. static void do_one_display (struct display *);
  135. /* Decode a format specification. *STRING_PTR should point to it.
  136. OFORMAT and OSIZE are used as defaults for the format and size
  137. if none are given in the format specification.
  138. If OSIZE is zero, then the size field of the returned value
  139. should be set only if a size is explicitly specified by the
  140. user.
  141. The structure returned describes all the data
  142. found in the specification. In addition, *STRING_PTR is advanced
  143. past the specification and past all whitespace following it. */
  144. static struct format_data
  145. decode_format (const char **string_ptr, int oformat, int osize)
  146. {
  147. struct format_data val;
  148. const char *p = *string_ptr;
  149. val.format = '?';
  150. val.size = '?';
  151. val.count = 1;
  152. val.raw = 0;
  153. val.print_tags = false;
  154. if (*p == '-')
  155. {
  156. val.count = -1;
  157. p++;
  158. }
  159. if (*p >= '0' && *p <= '9')
  160. val.count *= atoi (p);
  161. while (*p >= '0' && *p <= '9')
  162. p++;
  163. /* Now process size or format letters that follow. */
  164. while (1)
  165. {
  166. if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
  167. val.size = *p++;
  168. else if (*p == 'r')
  169. {
  170. val.raw = 1;
  171. p++;
  172. }
  173. else if (*p == 'm')
  174. {
  175. val.print_tags = true;
  176. p++;
  177. }
  178. else if (*p >= 'a' && *p <= 'z')
  179. val.format = *p++;
  180. else
  181. break;
  182. }
  183. *string_ptr = skip_spaces (p);
  184. /* Set defaults for format and size if not specified. */
  185. if (val.format == '?')
  186. {
  187. if (val.size == '?')
  188. {
  189. /* Neither has been specified. */
  190. val.format = oformat;
  191. val.size = osize;
  192. }
  193. else
  194. /* If a size is specified, any format makes a reasonable
  195. default except 'i'. */
  196. val.format = oformat == 'i' ? 'x' : oformat;
  197. }
  198. else if (val.size == '?')
  199. switch (val.format)
  200. {
  201. case 'a':
  202. /* Pick the appropriate size for an address. This is deferred
  203. until do_examine when we know the actual architecture to use.
  204. A special size value of 'a' is used to indicate this case. */
  205. val.size = osize ? 'a' : osize;
  206. break;
  207. case 'f':
  208. /* Floating point has to be word or giantword. */
  209. if (osize == 'w' || osize == 'g')
  210. val.size = osize;
  211. else
  212. /* Default it to giantword if the last used size is not
  213. appropriate. */
  214. val.size = osize ? 'g' : osize;
  215. break;
  216. case 'c':
  217. /* Characters default to one byte. */
  218. val.size = osize ? 'b' : osize;
  219. break;
  220. case 's':
  221. /* Display strings with byte size chars unless explicitly
  222. specified. */
  223. val.size = '\0';
  224. break;
  225. default:
  226. /* The default is the size most recently specified. */
  227. val.size = osize;
  228. }
  229. return val;
  230. }
  231. /* Print value VAL on stream according to OPTIONS.
  232. Do not end with a newline.
  233. SIZE is the letter for the size of datum being printed.
  234. This is used to pad hex numbers so they line up. SIZE is 0
  235. for print / output and set for examine. */
  236. static void
  237. print_formatted (struct value *val, int size,
  238. const struct value_print_options *options,
  239. struct ui_file *stream)
  240. {
  241. struct type *type = check_typedef (value_type (val));
  242. int len = TYPE_LENGTH (type);
  243. if (VALUE_LVAL (val) == lval_memory)
  244. next_address = value_address (val) + len;
  245. if (size)
  246. {
  247. switch (options->format)
  248. {
  249. case 's':
  250. {
  251. struct type *elttype = value_type (val);
  252. next_address = (value_address (val)
  253. + val_print_string (elttype, NULL,
  254. value_address (val), -1,
  255. stream, options) * len);
  256. }
  257. return;
  258. case 'i':
  259. /* We often wrap here if there are long symbolic names. */
  260. stream->wrap_here (4);
  261. next_address = (value_address (val)
  262. + gdb_print_insn (type->arch (),
  263. value_address (val), stream,
  264. &branch_delay_insns));
  265. return;
  266. }
  267. }
  268. if (options->format == 0 || options->format == 's'
  269. || type->code () == TYPE_CODE_VOID
  270. || type->code () == TYPE_CODE_REF
  271. || type->code () == TYPE_CODE_ARRAY
  272. || type->code () == TYPE_CODE_STRING
  273. || type->code () == TYPE_CODE_STRUCT
  274. || type->code () == TYPE_CODE_UNION
  275. || type->code () == TYPE_CODE_NAMESPACE)
  276. value_print (val, stream, options);
  277. else
  278. /* User specified format, so don't look to the type to tell us
  279. what to do. */
  280. value_print_scalar_formatted (val, options, size, stream);
  281. }
  282. /* Return builtin floating point type of same length as TYPE.
  283. If no such type is found, return TYPE itself. */
  284. static struct type *
  285. float_type_from_length (struct type *type)
  286. {
  287. struct gdbarch *gdbarch = type->arch ();
  288. const struct builtin_type *builtin = builtin_type (gdbarch);
  289. if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_float))
  290. type = builtin->builtin_float;
  291. else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_double))
  292. type = builtin->builtin_double;
  293. else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_long_double))
  294. type = builtin->builtin_long_double;
  295. return type;
  296. }
  297. /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
  298. according to OPTIONS and SIZE on STREAM. Formats s and i are not
  299. supported at this level. */
  300. void
  301. print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
  302. const struct value_print_options *options,
  303. int size, struct ui_file *stream)
  304. {
  305. struct gdbarch *gdbarch = type->arch ();
  306. unsigned int len = TYPE_LENGTH (type);
  307. enum bfd_endian byte_order = type_byte_order (type);
  308. /* String printing should go through val_print_scalar_formatted. */
  309. gdb_assert (options->format != 's');
  310. /* If the value is a pointer, and pointers and addresses are not the
  311. same, then at this point, the value's length (in target bytes) is
  312. gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
  313. if (type->code () == TYPE_CODE_PTR)
  314. len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
  315. /* If we are printing it as unsigned, truncate it in case it is actually
  316. a negative signed value (e.g. "print/u (short)-1" should print 65535
  317. (if shorts are 16 bits) instead of 4294967295). */
  318. if (options->format != 'c'
  319. && (options->format != 'd' || type->is_unsigned ()))
  320. {
  321. if (len < TYPE_LENGTH (type) && byte_order == BFD_ENDIAN_BIG)
  322. valaddr += TYPE_LENGTH (type) - len;
  323. }
  324. /* Allow LEN == 0, and in this case, don't assume that VALADDR is
  325. valid. */
  326. const gdb_byte zero = 0;
  327. if (len == 0)
  328. {
  329. len = 1;
  330. valaddr = &zero;
  331. }
  332. if (size != 0 && (options->format == 'x' || options->format == 't'))
  333. {
  334. /* Truncate to fit. */
  335. unsigned newlen;
  336. switch (size)
  337. {
  338. case 'b':
  339. newlen = 1;
  340. break;
  341. case 'h':
  342. newlen = 2;
  343. break;
  344. case 'w':
  345. newlen = 4;
  346. break;
  347. case 'g':
  348. newlen = 8;
  349. break;
  350. default:
  351. error (_("Undefined output size \"%c\"."), size);
  352. }
  353. if (newlen < len && byte_order == BFD_ENDIAN_BIG)
  354. valaddr += len - newlen;
  355. len = newlen;
  356. }
  357. /* Biased range types and sub-word scalar types must be handled
  358. here; the value is correctly computed by unpack_long. */
  359. gdb::byte_vector converted_bytes;
  360. /* Some cases below will unpack the value again. In the biased
  361. range case, we want to avoid this, so we store the unpacked value
  362. here for possible use later. */
  363. gdb::optional<LONGEST> val_long;
  364. if ((is_fixed_point_type (type)
  365. && (options->format == 'o'
  366. || options->format == 'x'
  367. || options->format == 't'
  368. || options->format == 'z'
  369. || options->format == 'd'
  370. || options->format == 'u'))
  371. || (type->code () == TYPE_CODE_RANGE && type->bounds ()->bias != 0)
  372. || type->bit_size_differs_p ())
  373. {
  374. val_long.emplace (unpack_long (type, valaddr));
  375. converted_bytes.resize (TYPE_LENGTH (type));
  376. store_signed_integer (converted_bytes.data (), TYPE_LENGTH (type),
  377. byte_order, *val_long);
  378. valaddr = converted_bytes.data ();
  379. }
  380. /* Printing a non-float type as 'f' will interpret the data as if it were
  381. of a floating-point type of the same length, if that exists. Otherwise,
  382. the data is printed as integer. */
  383. char format = options->format;
  384. if (format == 'f' && type->code () != TYPE_CODE_FLT)
  385. {
  386. type = float_type_from_length (type);
  387. if (type->code () != TYPE_CODE_FLT)
  388. format = 0;
  389. }
  390. switch (format)
  391. {
  392. case 'o':
  393. print_octal_chars (stream, valaddr, len, byte_order);
  394. break;
  395. case 'd':
  396. print_decimal_chars (stream, valaddr, len, true, byte_order);
  397. break;
  398. case 'u':
  399. print_decimal_chars (stream, valaddr, len, false, byte_order);
  400. break;
  401. case 0:
  402. if (type->code () != TYPE_CODE_FLT)
  403. {
  404. print_decimal_chars (stream, valaddr, len, !type->is_unsigned (),
  405. byte_order);
  406. break;
  407. }
  408. /* FALLTHROUGH */
  409. case 'f':
  410. print_floating (valaddr, type, stream);
  411. break;
  412. case 't':
  413. print_binary_chars (stream, valaddr, len, byte_order, size > 0);
  414. break;
  415. case 'x':
  416. print_hex_chars (stream, valaddr, len, byte_order, size > 0);
  417. break;
  418. case 'z':
  419. print_hex_chars (stream, valaddr, len, byte_order, true);
  420. break;
  421. case 'c':
  422. {
  423. struct value_print_options opts = *options;
  424. if (!val_long.has_value ())
  425. val_long.emplace (unpack_long (type, valaddr));
  426. opts.format = 0;
  427. if (type->is_unsigned ())
  428. type = builtin_type (gdbarch)->builtin_true_unsigned_char;
  429. else
  430. type = builtin_type (gdbarch)->builtin_true_char;
  431. value_print (value_from_longest (type, *val_long), stream, &opts);
  432. }
  433. break;
  434. case 'a':
  435. {
  436. if (!val_long.has_value ())
  437. val_long.emplace (unpack_long (type, valaddr));
  438. print_address (gdbarch, *val_long, stream);
  439. }
  440. break;
  441. default:
  442. error (_("Undefined output format \"%c\"."), format);
  443. }
  444. }
  445. /* Specify default address for `x' command.
  446. The `info lines' command uses this. */
  447. void
  448. set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
  449. {
  450. struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  451. next_gdbarch = gdbarch;
  452. next_address = addr;
  453. /* Make address available to the user as $_. */
  454. set_internalvar (lookup_internalvar ("_"),
  455. value_from_pointer (ptr_type, addr));
  456. }
  457. /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
  458. after LEADIN. Print nothing if no symbolic name is found nearby.
  459. Optionally also print source file and line number, if available.
  460. DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
  461. or to interpret it as a possible C++ name and convert it back to source
  462. form. However note that DO_DEMANGLE can be overridden by the specific
  463. settings of the demangle and asm_demangle variables. Returns
  464. non-zero if anything was printed; zero otherwise. */
  465. int
  466. print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
  467. struct ui_file *stream,
  468. int do_demangle, const char *leadin)
  469. {
  470. std::string name, filename;
  471. int unmapped = 0;
  472. int offset = 0;
  473. int line = 0;
  474. if (build_address_symbolic (gdbarch, addr, do_demangle, false, &name,
  475. &offset, &filename, &line, &unmapped))
  476. return 0;
  477. gdb_puts (leadin, stream);
  478. if (unmapped)
  479. gdb_puts ("<*", stream);
  480. else
  481. gdb_puts ("<", stream);
  482. fputs_styled (name.c_str (), function_name_style.style (), stream);
  483. if (offset != 0)
  484. gdb_printf (stream, "%+d", offset);
  485. /* Append source filename and line number if desired. Give specific
  486. line # of this addr, if we have it; else line # of the nearest symbol. */
  487. if (print_symbol_filename && !filename.empty ())
  488. {
  489. gdb_puts (line == -1 ? " in " : " at ", stream);
  490. fputs_styled (filename.c_str (), file_name_style.style (), stream);
  491. if (line != -1)
  492. gdb_printf (stream, ":%d", line);
  493. }
  494. if (unmapped)
  495. gdb_puts ("*>", stream);
  496. else
  497. gdb_puts (">", stream);
  498. return 1;
  499. }
  500. /* See valprint.h. */
  501. int
  502. build_address_symbolic (struct gdbarch *gdbarch,
  503. CORE_ADDR addr, /* IN */
  504. bool do_demangle, /* IN */
  505. bool prefer_sym_over_minsym, /* IN */
  506. std::string *name, /* OUT */
  507. int *offset, /* OUT */
  508. std::string *filename, /* OUT */
  509. int *line, /* OUT */
  510. int *unmapped) /* OUT */
  511. {
  512. struct bound_minimal_symbol msymbol;
  513. struct symbol *symbol;
  514. CORE_ADDR name_location = 0;
  515. struct obj_section *section = NULL;
  516. const char *name_temp = "";
  517. /* Let's say it is mapped (not unmapped). */
  518. *unmapped = 0;
  519. /* Determine if the address is in an overlay, and whether it is
  520. mapped. */
  521. if (overlay_debugging)
  522. {
  523. section = find_pc_overlay (addr);
  524. if (pc_in_unmapped_range (addr, section))
  525. {
  526. *unmapped = 1;
  527. addr = overlay_mapped_address (addr, section);
  528. }
  529. }
  530. /* Try to find the address in both the symbol table and the minsyms.
  531. In most cases, we'll prefer to use the symbol instead of the
  532. minsym. However, there are cases (see below) where we'll choose
  533. to use the minsym instead. */
  534. /* This is defective in the sense that it only finds text symbols. So
  535. really this is kind of pointless--we should make sure that the
  536. minimal symbols have everything we need (by changing that we could
  537. save some memory, but for many debug format--ELF/DWARF or
  538. anything/stabs--it would be inconvenient to eliminate those minimal
  539. symbols anyway). */
  540. msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
  541. symbol = find_pc_sect_function (addr, section);
  542. if (symbol)
  543. {
  544. /* If this is a function (i.e. a code address), strip out any
  545. non-address bits. For instance, display a pointer to the
  546. first instruction of a Thumb function as <function>; the
  547. second instruction will be <function+2>, even though the
  548. pointer is <function+3>. This matches the ISA behavior. */
  549. addr = gdbarch_addr_bits_remove (gdbarch, addr);
  550. name_location = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (symbol));
  551. if (do_demangle || asm_demangle)
  552. name_temp = symbol->print_name ();
  553. else
  554. name_temp = symbol->linkage_name ();
  555. }
  556. if (msymbol.minsym != NULL
  557. && MSYMBOL_HAS_SIZE (msymbol.minsym)
  558. && MSYMBOL_SIZE (msymbol.minsym) == 0
  559. && MSYMBOL_TYPE (msymbol.minsym) != mst_text
  560. && MSYMBOL_TYPE (msymbol.minsym) != mst_text_gnu_ifunc
  561. && MSYMBOL_TYPE (msymbol.minsym) != mst_file_text)
  562. msymbol.minsym = NULL;
  563. if (msymbol.minsym != NULL)
  564. {
  565. /* Use the minsym if no symbol is found.
  566. Additionally, use the minsym instead of a (found) symbol if
  567. the following conditions all hold:
  568. 1) The prefer_sym_over_minsym flag is false.
  569. 2) The minsym address is identical to that of the address under
  570. consideration.
  571. 3) The symbol address is not identical to that of the address
  572. under consideration. */
  573. if (symbol == NULL ||
  574. (!prefer_sym_over_minsym
  575. && BMSYMBOL_VALUE_ADDRESS (msymbol) == addr
  576. && name_location != addr))
  577. {
  578. /* If this is a function (i.e. a code address), strip out any
  579. non-address bits. For instance, display a pointer to the
  580. first instruction of a Thumb function as <function>; the
  581. second instruction will be <function+2>, even though the
  582. pointer is <function+3>. This matches the ISA behavior. */
  583. if (MSYMBOL_TYPE (msymbol.minsym) == mst_text
  584. || MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc
  585. || MSYMBOL_TYPE (msymbol.minsym) == mst_file_text
  586. || MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
  587. addr = gdbarch_addr_bits_remove (gdbarch, addr);
  588. symbol = 0;
  589. name_location = BMSYMBOL_VALUE_ADDRESS (msymbol);
  590. if (do_demangle || asm_demangle)
  591. name_temp = msymbol.minsym->print_name ();
  592. else
  593. name_temp = msymbol.minsym->linkage_name ();
  594. }
  595. }
  596. if (symbol == NULL && msymbol.minsym == NULL)
  597. return 1;
  598. /* If the nearest symbol is too far away, don't print anything symbolic. */
  599. /* For when CORE_ADDR is larger than unsigned int, we do math in
  600. CORE_ADDR. But when we detect unsigned wraparound in the
  601. CORE_ADDR math, we ignore this test and print the offset,
  602. because addr+max_symbolic_offset has wrapped through the end
  603. of the address space back to the beginning, giving bogus comparison. */
  604. if (addr > name_location + max_symbolic_offset
  605. && name_location + max_symbolic_offset > name_location)
  606. return 1;
  607. *offset = (LONGEST) addr - name_location;
  608. *name = name_temp;
  609. if (print_symbol_filename)
  610. {
  611. struct symtab_and_line sal;
  612. sal = find_pc_sect_line (addr, section, 0);
  613. if (sal.symtab)
  614. {
  615. *filename = symtab_to_filename_for_display (sal.symtab);
  616. *line = sal.line;
  617. }
  618. }
  619. return 0;
  620. }
  621. /* Print address ADDR symbolically on STREAM.
  622. First print it as a number. Then perhaps print
  623. <SYMBOL + OFFSET> after the number. */
  624. void
  625. print_address (struct gdbarch *gdbarch,
  626. CORE_ADDR addr, struct ui_file *stream)
  627. {
  628. fputs_styled (paddress (gdbarch, addr), address_style.style (), stream);
  629. print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
  630. }
  631. /* Return a prefix for instruction address:
  632. "=> " for current instruction, else " ". */
  633. const char *
  634. pc_prefix (CORE_ADDR addr)
  635. {
  636. if (has_stack_frames ())
  637. {
  638. struct frame_info *frame;
  639. CORE_ADDR pc;
  640. frame = get_selected_frame (NULL);
  641. if (get_frame_pc_if_available (frame, &pc) && pc == addr)
  642. return "=> ";
  643. }
  644. return " ";
  645. }
  646. /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
  647. controls whether to print the symbolic name "raw" or demangled.
  648. Return non-zero if anything was printed; zero otherwise. */
  649. int
  650. print_address_demangle (const struct value_print_options *opts,
  651. struct gdbarch *gdbarch, CORE_ADDR addr,
  652. struct ui_file *stream, int do_demangle)
  653. {
  654. if (opts->addressprint)
  655. {
  656. fputs_styled (paddress (gdbarch, addr), address_style.style (), stream);
  657. print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
  658. }
  659. else
  660. {
  661. return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
  662. }
  663. return 1;
  664. }
  665. /* Find the address of the instruction that is INST_COUNT instructions before
  666. the instruction at ADDR.
  667. Since some architectures have variable-length instructions, we can't just
  668. simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
  669. number information to locate the nearest known instruction boundary,
  670. and disassemble forward from there. If we go out of the symbol range
  671. during disassembling, we return the lowest address we've got so far and
  672. set the number of instructions read to INST_READ. */
  673. static CORE_ADDR
  674. find_instruction_backward (struct gdbarch *gdbarch, CORE_ADDR addr,
  675. int inst_count, int *inst_read)
  676. {
  677. /* The vector PCS is used to store instruction addresses within
  678. a pc range. */
  679. CORE_ADDR loop_start, loop_end, p;
  680. std::vector<CORE_ADDR> pcs;
  681. struct symtab_and_line sal;
  682. *inst_read = 0;
  683. loop_start = loop_end = addr;
  684. /* In each iteration of the outer loop, we get a pc range that ends before
  685. LOOP_START, then we count and store every instruction address of the range
  686. iterated in the loop.
  687. If the number of instructions counted reaches INST_COUNT, return the
  688. stored address that is located INST_COUNT instructions back from ADDR.
  689. If INST_COUNT is not reached, we subtract the number of counted
  690. instructions from INST_COUNT, and go to the next iteration. */
  691. do
  692. {
  693. pcs.clear ();
  694. sal = find_pc_sect_line (loop_start, NULL, 1);
  695. if (sal.line <= 0)
  696. {
  697. /* We reach here when line info is not available. In this case,
  698. we print a message and just exit the loop. The return value
  699. is calculated after the loop. */
  700. gdb_printf (_("No line number information available "
  701. "for address "));
  702. gdb_stdout->wrap_here (2);
  703. print_address (gdbarch, loop_start - 1, gdb_stdout);
  704. gdb_printf ("\n");
  705. break;
  706. }
  707. loop_end = loop_start;
  708. loop_start = sal.pc;
  709. /* This loop pushes instruction addresses in the range from
  710. LOOP_START to LOOP_END. */
  711. for (p = loop_start; p < loop_end;)
  712. {
  713. pcs.push_back (p);
  714. p += gdb_insn_length (gdbarch, p);
  715. }
  716. inst_count -= pcs.size ();
  717. *inst_read += pcs.size ();
  718. }
  719. while (inst_count > 0);
  720. /* After the loop, the vector PCS has instruction addresses of the last
  721. source line we processed, and INST_COUNT has a negative value.
  722. We return the address at the index of -INST_COUNT in the vector for
  723. the reason below.
  724. Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
  725. Line X of File
  726. 0x4000
  727. 0x4001
  728. 0x4005
  729. Line Y of File
  730. 0x4009
  731. 0x400c
  732. => 0x400e
  733. 0x4011
  734. find_instruction_backward is called with INST_COUNT = 4 and expected to
  735. return 0x4001. When we reach here, INST_COUNT is set to -1 because
  736. it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
  737. 4001 is located at the index 1 of the last iterated line (= Line X),
  738. which is simply calculated by -INST_COUNT.
  739. The case when the length of PCS is 0 means that we reached an area for
  740. which line info is not available. In such case, we return LOOP_START,
  741. which was the lowest instruction address that had line info. */
  742. p = pcs.size () > 0 ? pcs[-inst_count] : loop_start;
  743. /* INST_READ includes all instruction addresses in a pc range. Need to
  744. exclude the beginning part up to the address we're returning. That
  745. is, exclude {0x4000} in the example above. */
  746. if (inst_count < 0)
  747. *inst_read += inst_count;
  748. return p;
  749. }
  750. /* Backward read LEN bytes of target memory from address MEMADDR + LEN,
  751. placing the results in GDB's memory from MYADDR + LEN. Returns
  752. a count of the bytes actually read. */
  753. static int
  754. read_memory_backward (struct gdbarch *gdbarch,
  755. CORE_ADDR memaddr, gdb_byte *myaddr, int len)
  756. {
  757. int errcode;
  758. int nread; /* Number of bytes actually read. */
  759. /* First try a complete read. */
  760. errcode = target_read_memory (memaddr, myaddr, len);
  761. if (errcode == 0)
  762. {
  763. /* Got it all. */
  764. nread = len;
  765. }
  766. else
  767. {
  768. /* Loop, reading one byte at a time until we get as much as we can. */
  769. memaddr += len;
  770. myaddr += len;
  771. for (nread = 0; nread < len; ++nread)
  772. {
  773. errcode = target_read_memory (--memaddr, --myaddr, 1);
  774. if (errcode != 0)
  775. {
  776. /* The read was unsuccessful, so exit the loop. */
  777. gdb_printf (_("Cannot access memory at address %s\n"),
  778. paddress (gdbarch, memaddr));
  779. break;
  780. }
  781. }
  782. }
  783. return nread;
  784. }
  785. /* Returns true if X (which is LEN bytes wide) is the number zero. */
  786. static int
  787. integer_is_zero (const gdb_byte *x, int len)
  788. {
  789. int i = 0;
  790. while (i < len && x[i] == 0)
  791. ++i;
  792. return (i == len);
  793. }
  794. /* Find the start address of a string in which ADDR is included.
  795. Basically we search for '\0' and return the next address,
  796. but if OPTIONS->PRINT_MAX is smaller than the length of a string,
  797. we stop searching and return the address to print characters as many as
  798. PRINT_MAX from the string. */
  799. static CORE_ADDR
  800. find_string_backward (struct gdbarch *gdbarch,
  801. CORE_ADDR addr, int count, int char_size,
  802. const struct value_print_options *options,
  803. int *strings_counted)
  804. {
  805. const int chunk_size = 0x20;
  806. int read_error = 0;
  807. int chars_read = 0;
  808. int chars_to_read = chunk_size;
  809. int chars_counted = 0;
  810. int count_original = count;
  811. CORE_ADDR string_start_addr = addr;
  812. gdb_assert (char_size == 1 || char_size == 2 || char_size == 4);
  813. gdb::byte_vector buffer (chars_to_read * char_size);
  814. while (count > 0 && read_error == 0)
  815. {
  816. int i;
  817. addr -= chars_to_read * char_size;
  818. chars_read = read_memory_backward (gdbarch, addr, buffer.data (),
  819. chars_to_read * char_size);
  820. chars_read /= char_size;
  821. read_error = (chars_read == chars_to_read) ? 0 : 1;
  822. /* Searching for '\0' from the end of buffer in backward direction. */
  823. for (i = 0; i < chars_read && count > 0 ; ++i, ++chars_counted)
  824. {
  825. int offset = (chars_to_read - i - 1) * char_size;
  826. if (integer_is_zero (&buffer[offset], char_size)
  827. || chars_counted == options->print_max)
  828. {
  829. /* Found '\0' or reached print_max. As OFFSET is the offset to
  830. '\0', we add CHAR_SIZE to return the start address of
  831. a string. */
  832. --count;
  833. string_start_addr = addr + offset + char_size;
  834. chars_counted = 0;
  835. }
  836. }
  837. }
  838. /* Update STRINGS_COUNTED with the actual number of loaded strings. */
  839. *strings_counted = count_original - count;
  840. if (read_error != 0)
  841. {
  842. /* In error case, STRING_START_ADDR is pointing to the string that
  843. was last successfully loaded. Rewind the partially loaded string. */
  844. string_start_addr -= chars_counted * char_size;
  845. }
  846. return string_start_addr;
  847. }
  848. /* Examine data at address ADDR in format FMT.
  849. Fetch it from memory and print on gdb_stdout. */
  850. static void
  851. do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
  852. {
  853. char format = 0;
  854. char size;
  855. int count = 1;
  856. struct type *val_type = NULL;
  857. int i;
  858. int maxelts;
  859. struct value_print_options opts;
  860. int need_to_update_next_address = 0;
  861. CORE_ADDR addr_rewound = 0;
  862. format = fmt.format;
  863. size = fmt.size;
  864. count = fmt.count;
  865. next_gdbarch = gdbarch;
  866. next_address = addr;
  867. /* Instruction format implies fetch single bytes
  868. regardless of the specified size.
  869. The case of strings is handled in decode_format, only explicit
  870. size operator are not changed to 'b'. */
  871. if (format == 'i')
  872. size = 'b';
  873. if (size == 'a')
  874. {
  875. /* Pick the appropriate size for an address. */
  876. if (gdbarch_ptr_bit (next_gdbarch) == 64)
  877. size = 'g';
  878. else if (gdbarch_ptr_bit (next_gdbarch) == 32)
  879. size = 'w';
  880. else if (gdbarch_ptr_bit (next_gdbarch) == 16)
  881. size = 'h';
  882. else
  883. /* Bad value for gdbarch_ptr_bit. */
  884. internal_error (__FILE__, __LINE__,
  885. _("failed internal consistency check"));
  886. }
  887. if (size == 'b')
  888. val_type = builtin_type (next_gdbarch)->builtin_int8;
  889. else if (size == 'h')
  890. val_type = builtin_type (next_gdbarch)->builtin_int16;
  891. else if (size == 'w')
  892. val_type = builtin_type (next_gdbarch)->builtin_int32;
  893. else if (size == 'g')
  894. val_type = builtin_type (next_gdbarch)->builtin_int64;
  895. if (format == 's')
  896. {
  897. struct type *char_type = NULL;
  898. /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
  899. if type is not found. */
  900. if (size == 'h')
  901. char_type = builtin_type (next_gdbarch)->builtin_char16;
  902. else if (size == 'w')
  903. char_type = builtin_type (next_gdbarch)->builtin_char32;
  904. if (char_type)
  905. val_type = char_type;
  906. else
  907. {
  908. if (size != '\0' && size != 'b')
  909. warning (_("Unable to display strings with "
  910. "size '%c', using 'b' instead."), size);
  911. size = 'b';
  912. val_type = builtin_type (next_gdbarch)->builtin_int8;
  913. }
  914. }
  915. maxelts = 8;
  916. if (size == 'w')
  917. maxelts = 4;
  918. if (size == 'g')
  919. maxelts = 2;
  920. if (format == 's' || format == 'i')
  921. maxelts = 1;
  922. get_formatted_print_options (&opts, format);
  923. if (count < 0)
  924. {
  925. /* This is the negative repeat count case.
  926. We rewind the address based on the given repeat count and format,
  927. then examine memory from there in forward direction. */
  928. count = -count;
  929. if (format == 'i')
  930. {
  931. next_address = find_instruction_backward (gdbarch, addr, count,
  932. &count);
  933. }
  934. else if (format == 's')
  935. {
  936. next_address = find_string_backward (gdbarch, addr, count,
  937. TYPE_LENGTH (val_type),
  938. &opts, &count);
  939. }
  940. else
  941. {
  942. next_address = addr - count * TYPE_LENGTH (val_type);
  943. }
  944. /* The following call to print_formatted updates next_address in every
  945. iteration. In backward case, we store the start address here
  946. and update next_address with it before exiting the function. */
  947. addr_rewound = (format == 's'
  948. ? next_address - TYPE_LENGTH (val_type)
  949. : next_address);
  950. need_to_update_next_address = 1;
  951. }
  952. /* Whether we need to print the memory tag information for the current
  953. address range. */
  954. bool print_range_tag = true;
  955. uint32_t gsize = gdbarch_memtag_granule_size (gdbarch);
  956. /* Print as many objects as specified in COUNT, at most maxelts per line,
  957. with the address of the next one at the start of each line. */
  958. while (count > 0)
  959. {
  960. QUIT;
  961. CORE_ADDR tag_laddr = 0, tag_haddr = 0;
  962. /* Print the memory tag information if requested. */
  963. if (fmt.print_tags && print_range_tag
  964. && target_supports_memory_tagging ())
  965. {
  966. tag_laddr = align_down (next_address, gsize);
  967. tag_haddr = align_down (next_address + gsize, gsize);
  968. struct value *v_addr
  969. = value_from_ulongest (builtin_type (gdbarch)->builtin_data_ptr,
  970. tag_laddr);
  971. if (gdbarch_tagged_address_p (target_gdbarch (), v_addr))
  972. {
  973. /* Fetch the allocation tag. */
  974. struct value *tag
  975. = gdbarch_get_memtag (gdbarch, v_addr, memtag_type::allocation);
  976. std::string atag
  977. = gdbarch_memtag_to_string (gdbarch, tag);
  978. if (!atag.empty ())
  979. {
  980. gdb_printf (_("<Allocation Tag %s for range [%s,%s)>\n"),
  981. atag.c_str (),
  982. paddress (gdbarch, tag_laddr),
  983. paddress (gdbarch, tag_haddr));
  984. }
  985. }
  986. print_range_tag = false;
  987. }
  988. if (format == 'i')
  989. gdb_puts (pc_prefix (next_address));
  990. print_address (next_gdbarch, next_address, gdb_stdout);
  991. gdb_printf (":");
  992. for (i = maxelts;
  993. i > 0 && count > 0;
  994. i--, count--)
  995. {
  996. gdb_printf ("\t");
  997. /* Note that print_formatted sets next_address for the next
  998. object. */
  999. last_examine_address = next_address;
  1000. /* The value to be displayed is not fetched greedily.
  1001. Instead, to avoid the possibility of a fetched value not
  1002. being used, its retrieval is delayed until the print code
  1003. uses it. When examining an instruction stream, the
  1004. disassembler will perform its own memory fetch using just
  1005. the address stored in LAST_EXAMINE_VALUE. FIXME: Should
  1006. the disassembler be modified so that LAST_EXAMINE_VALUE
  1007. is left with the byte sequence from the last complete
  1008. instruction fetched from memory? */
  1009. last_examine_value
  1010. = release_value (value_at_lazy (val_type, next_address));
  1011. print_formatted (last_examine_value.get (), size, &opts, gdb_stdout);
  1012. /* Display any branch delay slots following the final insn. */
  1013. if (format == 'i' && count == 1)
  1014. count += branch_delay_insns;
  1015. /* Update the tag range based on the current address being
  1016. processed. */
  1017. if (tag_haddr <= next_address)
  1018. print_range_tag = true;
  1019. }
  1020. gdb_printf ("\n");
  1021. }
  1022. if (need_to_update_next_address)
  1023. next_address = addr_rewound;
  1024. }
  1025. static void
  1026. validate_format (struct format_data fmt, const char *cmdname)
  1027. {
  1028. if (fmt.size != 0)
  1029. error (_("Size letters are meaningless in \"%s\" command."), cmdname);
  1030. if (fmt.count != 1)
  1031. error (_("Item count other than 1 is meaningless in \"%s\" command."),
  1032. cmdname);
  1033. if (fmt.format == 'i')
  1034. error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
  1035. fmt.format, cmdname);
  1036. }
  1037. /* Parse print command format string into *OPTS and update *EXPP.
  1038. CMDNAME should name the current command. */
  1039. void
  1040. print_command_parse_format (const char **expp, const char *cmdname,
  1041. value_print_options *opts)
  1042. {
  1043. const char *exp = *expp;
  1044. /* opts->raw value might already have been set by 'set print raw-values'
  1045. or by using 'print -raw-values'.
  1046. So, do not set opts->raw to 0, only set it to 1 if /r is given. */
  1047. if (exp && *exp == '/')
  1048. {
  1049. format_data fmt;
  1050. exp++;
  1051. fmt = decode_format (&exp, last_format, 0);
  1052. validate_format (fmt, cmdname);
  1053. last_format = fmt.format;
  1054. opts->format = fmt.format;
  1055. opts->raw = opts->raw || fmt.raw;
  1056. }
  1057. else
  1058. {
  1059. opts->format = 0;
  1060. }
  1061. *expp = exp;
  1062. }
  1063. /* See valprint.h. */
  1064. void
  1065. print_value (value *val, const value_print_options &opts)
  1066. {
  1067. int histindex = record_latest_value (val);
  1068. annotate_value_history_begin (histindex, value_type (val));
  1069. gdb_printf ("$%d = ", histindex);
  1070. annotate_value_history_value ();
  1071. print_formatted (val, 0, &opts, gdb_stdout);
  1072. gdb_printf ("\n");
  1073. annotate_value_history_end ();
  1074. }
  1075. /* Returns true if memory tags should be validated. False otherwise. */
  1076. static bool
  1077. should_validate_memtags (struct value *value)
  1078. {
  1079. gdb_assert (value != nullptr && value_type (value) != nullptr);
  1080. if (!target_supports_memory_tagging ())
  1081. return false;
  1082. enum type_code code = value_type (value)->code ();
  1083. /* Skip non-address values. */
  1084. if (code != TYPE_CODE_PTR
  1085. && !TYPE_IS_REFERENCE (value_type (value)))
  1086. return false;
  1087. /* OK, we have an address value. Check we have a complete value we
  1088. can extract. */
  1089. if (value_optimized_out (value)
  1090. || !value_entirely_available (value))
  1091. return false;
  1092. /* We do. Check whether it includes any tags. */
  1093. return gdbarch_tagged_address_p (target_gdbarch (), value);
  1094. }
  1095. /* Helper for parsing arguments for print_command_1. */
  1096. static struct value *
  1097. process_print_command_args (const char *args, value_print_options *print_opts,
  1098. bool voidprint)
  1099. {
  1100. get_user_print_options (print_opts);
  1101. /* Override global settings with explicit options, if any. */
  1102. auto group = make_value_print_options_def_group (print_opts);
  1103. gdb::option::process_options
  1104. (&args, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group);
  1105. print_command_parse_format (&args, "print", print_opts);
  1106. const char *exp = args;
  1107. if (exp != nullptr && *exp)
  1108. {
  1109. /* VOIDPRINT is true to indicate that we do want to print a void
  1110. value, so invert it for parse_expression. */
  1111. expression_up expr = parse_expression (exp, nullptr, !voidprint);
  1112. return evaluate_expression (expr.get ());
  1113. }
  1114. return access_value_history (0);
  1115. }
  1116. /* Implementation of the "print" and "call" commands. */
  1117. static void
  1118. print_command_1 (const char *args, int voidprint)
  1119. {
  1120. value_print_options print_opts;
  1121. struct value *val = process_print_command_args (args, &print_opts, voidprint);
  1122. if (voidprint || (val && value_type (val) &&
  1123. value_type (val)->code () != TYPE_CODE_VOID))
  1124. {
  1125. /* If memory tagging validation is on, check if the tag is valid. */
  1126. if (print_opts.memory_tag_violations)
  1127. {
  1128. try
  1129. {
  1130. if (should_validate_memtags (val)
  1131. && !gdbarch_memtag_matches_p (target_gdbarch (), val))
  1132. {
  1133. /* Fetch the logical tag. */
  1134. struct value *tag
  1135. = gdbarch_get_memtag (target_gdbarch (), val,
  1136. memtag_type::logical);
  1137. std::string ltag
  1138. = gdbarch_memtag_to_string (target_gdbarch (), tag);
  1139. /* Fetch the allocation tag. */
  1140. tag = gdbarch_get_memtag (target_gdbarch (), val,
  1141. memtag_type::allocation);
  1142. std::string atag
  1143. = gdbarch_memtag_to_string (target_gdbarch (), tag);
  1144. gdb_printf (_("Logical tag (%s) does not match the "
  1145. "allocation tag (%s).\n"),
  1146. ltag.c_str (), atag.c_str ());
  1147. }
  1148. }
  1149. catch (gdb_exception_error &ex)
  1150. {
  1151. if (ex.error == TARGET_CLOSE_ERROR)
  1152. throw;
  1153. gdb_printf (gdb_stderr,
  1154. _("Could not validate memory tag: %s\n"),
  1155. ex.message->c_str ());
  1156. }
  1157. }
  1158. print_value (val, print_opts);
  1159. }
  1160. }
  1161. /* Called from command completion function to skip over /FMT
  1162. specifications, allowing the rest of the line to be completed. Returns
  1163. true if the /FMT is at the end of the current line and there is nothing
  1164. left to complete, otherwise false is returned.
  1165. In either case *ARGS can be updated to point after any part of /FMT that
  1166. is present.
  1167. This function is designed so that trying to complete '/' will offer no
  1168. completions, the user needs to insert the format specification
  1169. themselves. Trying to complete '/FMT' (where FMT is any non-empty set
  1170. of alpha-numeric characters) will cause readline to insert a single
  1171. space, setting the user up to enter the expression. */
  1172. static bool
  1173. skip_over_slash_fmt (completion_tracker &tracker, const char **args)
  1174. {
  1175. const char *text = *args;
  1176. if (text[0] == '/')
  1177. {
  1178. bool in_fmt;
  1179. tracker.set_use_custom_word_point (true);
  1180. if (text[1] == '\0')
  1181. {
  1182. /* The user tried to complete after typing just the '/' character
  1183. of the /FMT string. Step the completer past the '/', but we
  1184. don't offer any completions. */
  1185. in_fmt = true;
  1186. ++text;
  1187. }
  1188. else
  1189. {
  1190. /* The user has typed some characters after the '/', we assume
  1191. this is a complete /FMT string, first skip over it. */
  1192. text = skip_to_space (text);
  1193. if (*text == '\0')
  1194. {
  1195. /* We're at the end of the input string. The user has typed
  1196. '/FMT' and asked for a completion. Push an empty
  1197. completion string, this will cause readline to insert a
  1198. space so the user now has '/FMT '. */
  1199. in_fmt = true;
  1200. tracker.add_completion (make_unique_xstrdup (text));
  1201. }
  1202. else
  1203. {
  1204. /* The user has already typed things after the /FMT, skip the
  1205. whitespace and return false. Whoever called this function
  1206. should then try to complete what comes next. */
  1207. in_fmt = false;
  1208. text = skip_spaces (text);
  1209. }
  1210. }
  1211. tracker.advance_custom_word_point_by (text - *args);
  1212. *args = text;
  1213. return in_fmt;
  1214. }
  1215. return false;
  1216. }
  1217. /* See valprint.h. */
  1218. void
  1219. print_command_completer (struct cmd_list_element *ignore,
  1220. completion_tracker &tracker,
  1221. const char *text, const char * /*word*/)
  1222. {
  1223. const auto group = make_value_print_options_def_group (nullptr);
  1224. if (gdb::option::complete_options
  1225. (tracker, &text, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group))
  1226. return;
  1227. if (skip_over_slash_fmt (tracker, &text))
  1228. return;
  1229. const char *word = advance_to_expression_complete_word_point (tracker, text);
  1230. expression_completer (ignore, tracker, text, word);
  1231. }
  1232. static void
  1233. print_command (const char *exp, int from_tty)
  1234. {
  1235. print_command_1 (exp, true);
  1236. }
  1237. /* Same as print, except it doesn't print void results. */
  1238. static void
  1239. call_command (const char *exp, int from_tty)
  1240. {
  1241. print_command_1 (exp, false);
  1242. }
  1243. /* Implementation of the "output" command. */
  1244. void
  1245. output_command (const char *exp, int from_tty)
  1246. {
  1247. char format = 0;
  1248. struct value *val;
  1249. struct format_data fmt;
  1250. struct value_print_options opts;
  1251. fmt.size = 0;
  1252. fmt.raw = 0;
  1253. if (exp && *exp == '/')
  1254. {
  1255. exp++;
  1256. fmt = decode_format (&exp, 0, 0);
  1257. validate_format (fmt, "output");
  1258. format = fmt.format;
  1259. }
  1260. expression_up expr = parse_expression (exp);
  1261. val = evaluate_expression (expr.get ());
  1262. annotate_value_begin (value_type (val));
  1263. get_formatted_print_options (&opts, format);
  1264. opts.raw = fmt.raw;
  1265. print_formatted (val, fmt.size, &opts, gdb_stdout);
  1266. annotate_value_end ();
  1267. gdb_flush (gdb_stdout);
  1268. }
  1269. static void
  1270. set_command (const char *exp, int from_tty)
  1271. {
  1272. expression_up expr = parse_expression (exp);
  1273. switch (expr->op->opcode ())
  1274. {
  1275. case UNOP_PREINCREMENT:
  1276. case UNOP_POSTINCREMENT:
  1277. case UNOP_PREDECREMENT:
  1278. case UNOP_POSTDECREMENT:
  1279. case BINOP_ASSIGN:
  1280. case BINOP_ASSIGN_MODIFY:
  1281. case BINOP_COMMA:
  1282. break;
  1283. default:
  1284. warning
  1285. (_("Expression is not an assignment (and might have no effect)"));
  1286. }
  1287. evaluate_expression (expr.get ());
  1288. }
  1289. static void
  1290. info_symbol_command (const char *arg, int from_tty)
  1291. {
  1292. struct minimal_symbol *msymbol;
  1293. struct obj_section *osect;
  1294. CORE_ADDR addr, sect_addr;
  1295. int matches = 0;
  1296. unsigned int offset;
  1297. if (!arg)
  1298. error_no_arg (_("address"));
  1299. addr = parse_and_eval_address (arg);
  1300. for (objfile *objfile : current_program_space->objfiles ())
  1301. ALL_OBJFILE_OSECTIONS (objfile, osect)
  1302. {
  1303. /* Only process each object file once, even if there's a separate
  1304. debug file. */
  1305. if (objfile->separate_debug_objfile_backlink)
  1306. continue;
  1307. sect_addr = overlay_mapped_address (addr, osect);
  1308. if (osect->addr () <= sect_addr && sect_addr < osect->endaddr ()
  1309. && (msymbol
  1310. = lookup_minimal_symbol_by_pc_section (sect_addr,
  1311. osect).minsym))
  1312. {
  1313. const char *obj_name, *mapped, *sec_name, *msym_name;
  1314. const char *loc_string;
  1315. matches = 1;
  1316. offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
  1317. mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
  1318. sec_name = osect->the_bfd_section->name;
  1319. msym_name = msymbol->print_name ();
  1320. /* Don't print the offset if it is zero.
  1321. We assume there's no need to handle i18n of "sym + offset". */
  1322. std::string string_holder;
  1323. if (offset)
  1324. {
  1325. string_holder = string_printf ("%s + %u", msym_name, offset);
  1326. loc_string = string_holder.c_str ();
  1327. }
  1328. else
  1329. loc_string = msym_name;
  1330. gdb_assert (osect->objfile && objfile_name (osect->objfile));
  1331. obj_name = objfile_name (osect->objfile);
  1332. if (current_program_space->multi_objfile_p ())
  1333. if (pc_in_unmapped_range (addr, osect))
  1334. if (section_is_overlay (osect))
  1335. gdb_printf (_("%s in load address range of "
  1336. "%s overlay section %s of %s\n"),
  1337. loc_string, mapped, sec_name, obj_name);
  1338. else
  1339. gdb_printf (_("%s in load address range of "
  1340. "section %s of %s\n"),
  1341. loc_string, sec_name, obj_name);
  1342. else
  1343. if (section_is_overlay (osect))
  1344. gdb_printf (_("%s in %s overlay section %s of %s\n"),
  1345. loc_string, mapped, sec_name, obj_name);
  1346. else
  1347. gdb_printf (_("%s in section %s of %s\n"),
  1348. loc_string, sec_name, obj_name);
  1349. else
  1350. if (pc_in_unmapped_range (addr, osect))
  1351. if (section_is_overlay (osect))
  1352. gdb_printf (_("%s in load address range of %s overlay "
  1353. "section %s\n"),
  1354. loc_string, mapped, sec_name);
  1355. else
  1356. gdb_printf
  1357. (_("%s in load address range of section %s\n"),
  1358. loc_string, sec_name);
  1359. else
  1360. if (section_is_overlay (osect))
  1361. gdb_printf (_("%s in %s overlay section %s\n"),
  1362. loc_string, mapped, sec_name);
  1363. else
  1364. gdb_printf (_("%s in section %s\n"),
  1365. loc_string, sec_name);
  1366. }
  1367. }
  1368. if (matches == 0)
  1369. gdb_printf (_("No symbol matches %s.\n"), arg);
  1370. }
  1371. static void
  1372. info_address_command (const char *exp, int from_tty)
  1373. {
  1374. struct gdbarch *gdbarch;
  1375. int regno;
  1376. struct symbol *sym;
  1377. struct bound_minimal_symbol msymbol;
  1378. long val;
  1379. struct obj_section *section;
  1380. CORE_ADDR load_addr, context_pc = 0;
  1381. struct field_of_this_result is_a_field_of_this;
  1382. if (exp == 0)
  1383. error (_("Argument required."));
  1384. sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
  1385. &is_a_field_of_this).symbol;
  1386. if (sym == NULL)
  1387. {
  1388. if (is_a_field_of_this.type != NULL)
  1389. {
  1390. gdb_printf ("Symbol \"");
  1391. fprintf_symbol (gdb_stdout, exp,
  1392. current_language->la_language, DMGL_ANSI);
  1393. gdb_printf ("\" is a field of the local class variable ");
  1394. if (current_language->la_language == language_objc)
  1395. gdb_printf ("`self'\n"); /* ObjC equivalent of "this" */
  1396. else
  1397. gdb_printf ("`this'\n");
  1398. return;
  1399. }
  1400. msymbol = lookup_bound_minimal_symbol (exp);
  1401. if (msymbol.minsym != NULL)
  1402. {
  1403. struct objfile *objfile = msymbol.objfile;
  1404. gdbarch = objfile->arch ();
  1405. load_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  1406. gdb_printf ("Symbol \"");
  1407. fprintf_symbol (gdb_stdout, exp,
  1408. current_language->la_language, DMGL_ANSI);
  1409. gdb_printf ("\" is at ");
  1410. fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
  1411. gdb_stdout);
  1412. gdb_printf (" in a file compiled without debugging");
  1413. section = msymbol.minsym->obj_section (objfile);
  1414. if (section_is_overlay (section))
  1415. {
  1416. load_addr = overlay_unmapped_address (load_addr, section);
  1417. gdb_printf (",\n -- loaded at ");
  1418. fputs_styled (paddress (gdbarch, load_addr),
  1419. address_style.style (),
  1420. gdb_stdout);
  1421. gdb_printf (" in overlay section %s",
  1422. section->the_bfd_section->name);
  1423. }
  1424. gdb_printf (".\n");
  1425. }
  1426. else
  1427. error (_("No symbol \"%s\" in current context."), exp);
  1428. return;
  1429. }
  1430. gdb_printf ("Symbol \"");
  1431. gdb_puts (sym->print_name ());
  1432. gdb_printf ("\" is ");
  1433. val = SYMBOL_VALUE (sym);
  1434. if (sym->is_objfile_owned ())
  1435. section = sym->obj_section (symbol_objfile (sym));
  1436. else
  1437. section = NULL;
  1438. gdbarch = symbol_arch (sym);
  1439. if (SYMBOL_COMPUTED_OPS (sym) != NULL)
  1440. {
  1441. SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
  1442. gdb_stdout);
  1443. gdb_printf (".\n");
  1444. return;
  1445. }
  1446. switch (sym->aclass ())
  1447. {
  1448. case LOC_CONST:
  1449. case LOC_CONST_BYTES:
  1450. gdb_printf ("constant");
  1451. break;
  1452. case LOC_LABEL:
  1453. gdb_printf ("a label at address ");
  1454. load_addr = SYMBOL_VALUE_ADDRESS (sym);
  1455. fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
  1456. gdb_stdout);
  1457. if (section_is_overlay (section))
  1458. {
  1459. load_addr = overlay_unmapped_address (load_addr, section);
  1460. gdb_printf (",\n -- loaded at ");
  1461. fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
  1462. gdb_stdout);
  1463. gdb_printf (" in overlay section %s",
  1464. section->the_bfd_section->name);
  1465. }
  1466. break;
  1467. case LOC_COMPUTED:
  1468. gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
  1469. case LOC_REGISTER:
  1470. /* GDBARCH is the architecture associated with the objfile the symbol
  1471. is defined in; the target architecture may be different, and may
  1472. provide additional registers. However, we do not know the target
  1473. architecture at this point. We assume the objfile architecture
  1474. will contain all the standard registers that occur in debug info
  1475. in that objfile. */
  1476. regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
  1477. if (sym->is_argument ())
  1478. gdb_printf (_("an argument in register %s"),
  1479. gdbarch_register_name (gdbarch, regno));
  1480. else
  1481. gdb_printf (_("a variable in register %s"),
  1482. gdbarch_register_name (gdbarch, regno));
  1483. break;
  1484. case LOC_STATIC:
  1485. gdb_printf (_("static storage at address "));
  1486. load_addr = SYMBOL_VALUE_ADDRESS (sym);
  1487. fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
  1488. gdb_stdout);
  1489. if (section_is_overlay (section))
  1490. {
  1491. load_addr = overlay_unmapped_address (load_addr, section);
  1492. gdb_printf (_(",\n -- loaded at "));
  1493. fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
  1494. gdb_stdout);
  1495. gdb_printf (_(" in overlay section %s"),
  1496. section->the_bfd_section->name);
  1497. }
  1498. break;
  1499. case LOC_REGPARM_ADDR:
  1500. /* Note comment at LOC_REGISTER. */
  1501. regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
  1502. gdb_printf (_("address of an argument in register %s"),
  1503. gdbarch_register_name (gdbarch, regno));
  1504. break;
  1505. case LOC_ARG:
  1506. gdb_printf (_("an argument at offset %ld"), val);
  1507. break;
  1508. case LOC_LOCAL:
  1509. gdb_printf (_("a local variable at frame offset %ld"), val);
  1510. break;
  1511. case LOC_REF_ARG:
  1512. gdb_printf (_("a reference argument at offset %ld"), val);
  1513. break;
  1514. case LOC_TYPEDEF:
  1515. gdb_printf (_("a typedef"));
  1516. break;
  1517. case LOC_BLOCK:
  1518. gdb_printf (_("a function at address "));
  1519. load_addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
  1520. fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
  1521. gdb_stdout);
  1522. if (section_is_overlay (section))
  1523. {
  1524. load_addr = overlay_unmapped_address (load_addr, section);
  1525. gdb_printf (_(",\n -- loaded at "));
  1526. fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
  1527. gdb_stdout);
  1528. gdb_printf (_(" in overlay section %s"),
  1529. section->the_bfd_section->name);
  1530. }
  1531. break;
  1532. case LOC_UNRESOLVED:
  1533. {
  1534. struct bound_minimal_symbol msym;
  1535. msym = lookup_bound_minimal_symbol (sym->linkage_name ());
  1536. if (msym.minsym == NULL)
  1537. gdb_printf ("unresolved");
  1538. else
  1539. {
  1540. section = msym.obj_section ();
  1541. if (section
  1542. && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
  1543. {
  1544. load_addr = MSYMBOL_VALUE_RAW_ADDRESS (msym.minsym);
  1545. gdb_printf (_("a thread-local variable at offset %s "
  1546. "in the thread-local storage for `%s'"),
  1547. paddress (gdbarch, load_addr),
  1548. objfile_name (section->objfile));
  1549. }
  1550. else
  1551. {
  1552. load_addr = BMSYMBOL_VALUE_ADDRESS (msym);
  1553. gdb_printf (_("static storage at address "));
  1554. fputs_styled (paddress (gdbarch, load_addr),
  1555. address_style.style (), gdb_stdout);
  1556. if (section_is_overlay (section))
  1557. {
  1558. load_addr = overlay_unmapped_address (load_addr, section);
  1559. gdb_printf (_(",\n -- loaded at "));
  1560. fputs_styled (paddress (gdbarch, load_addr),
  1561. address_style.style (),
  1562. gdb_stdout);
  1563. gdb_printf (_(" in overlay section %s"),
  1564. section->the_bfd_section->name);
  1565. }
  1566. }
  1567. }
  1568. }
  1569. break;
  1570. case LOC_OPTIMIZED_OUT:
  1571. gdb_printf (_("optimized out"));
  1572. break;
  1573. default:
  1574. gdb_printf (_("of unknown (botched) type"));
  1575. break;
  1576. }
  1577. gdb_printf (".\n");
  1578. }
  1579. static void
  1580. x_command (const char *exp, int from_tty)
  1581. {
  1582. struct format_data fmt;
  1583. struct value *val;
  1584. fmt.format = last_format ? last_format : 'x';
  1585. fmt.print_tags = last_print_tags;
  1586. fmt.size = last_size;
  1587. fmt.count = 1;
  1588. fmt.raw = 0;
  1589. /* If there is no expression and no format, use the most recent
  1590. count. */
  1591. if (exp == nullptr && last_count > 0)
  1592. fmt.count = last_count;
  1593. if (exp && *exp == '/')
  1594. {
  1595. const char *tmp = exp + 1;
  1596. fmt = decode_format (&tmp, last_format, last_size);
  1597. exp = (char *) tmp;
  1598. }
  1599. last_count = fmt.count;
  1600. /* If we have an expression, evaluate it and use it as the address. */
  1601. if (exp != 0 && *exp != 0)
  1602. {
  1603. expression_up expr = parse_expression (exp);
  1604. /* Cause expression not to be there any more if this command is
  1605. repeated with Newline. But don't clobber a user-defined
  1606. command's definition. */
  1607. if (from_tty)
  1608. set_repeat_arguments ("");
  1609. val = evaluate_expression (expr.get ());
  1610. if (TYPE_IS_REFERENCE (value_type (val)))
  1611. val = coerce_ref (val);
  1612. /* In rvalue contexts, such as this, functions are coerced into
  1613. pointers to functions. This makes "x/i main" work. */
  1614. if (value_type (val)->code () == TYPE_CODE_FUNC
  1615. && VALUE_LVAL (val) == lval_memory)
  1616. next_address = value_address (val);
  1617. else
  1618. next_address = value_as_address (val);
  1619. next_gdbarch = expr->gdbarch;
  1620. }
  1621. if (!next_gdbarch)
  1622. error_no_arg (_("starting display address"));
  1623. do_examine (fmt, next_gdbarch, next_address);
  1624. /* If the examine succeeds, we remember its size and format for next
  1625. time. Set last_size to 'b' for strings. */
  1626. if (fmt.format == 's')
  1627. last_size = 'b';
  1628. else
  1629. last_size = fmt.size;
  1630. last_format = fmt.format;
  1631. /* Remember tag-printing setting. */
  1632. last_print_tags = fmt.print_tags;
  1633. /* Set a couple of internal variables if appropriate. */
  1634. if (last_examine_value != nullptr)
  1635. {
  1636. /* Make last address examined available to the user as $_. Use
  1637. the correct pointer type. */
  1638. struct type *pointer_type
  1639. = lookup_pointer_type (value_type (last_examine_value.get ()));
  1640. set_internalvar (lookup_internalvar ("_"),
  1641. value_from_pointer (pointer_type,
  1642. last_examine_address));
  1643. /* Make contents of last address examined available to the user
  1644. as $__. If the last value has not been fetched from memory
  1645. then don't fetch it now; instead mark it by voiding the $__
  1646. variable. */
  1647. if (value_lazy (last_examine_value.get ()))
  1648. clear_internalvar (lookup_internalvar ("__"));
  1649. else
  1650. set_internalvar (lookup_internalvar ("__"), last_examine_value.get ());
  1651. }
  1652. }
  1653. /* Command completion for the 'display' and 'x' commands. */
  1654. static void
  1655. display_and_x_command_completer (struct cmd_list_element *ignore,
  1656. completion_tracker &tracker,
  1657. const char *text, const char * /*word*/)
  1658. {
  1659. if (skip_over_slash_fmt (tracker, &text))
  1660. return;
  1661. const char *word = advance_to_expression_complete_word_point (tracker, text);
  1662. expression_completer (ignore, tracker, text, word);
  1663. }
  1664. /* Add an expression to the auto-display chain.
  1665. Specify the expression. */
  1666. static void
  1667. display_command (const char *arg, int from_tty)
  1668. {
  1669. struct format_data fmt;
  1670. struct display *newobj;
  1671. const char *exp = arg;
  1672. if (exp == 0)
  1673. {
  1674. do_displays ();
  1675. return;
  1676. }
  1677. if (*exp == '/')
  1678. {
  1679. exp++;
  1680. fmt = decode_format (&exp, 0, 0);
  1681. if (fmt.size && fmt.format == 0)
  1682. fmt.format = 'x';
  1683. if (fmt.format == 'i' || fmt.format == 's')
  1684. fmt.size = 'b';
  1685. }
  1686. else
  1687. {
  1688. fmt.format = 0;
  1689. fmt.size = 0;
  1690. fmt.count = 0;
  1691. fmt.raw = 0;
  1692. }
  1693. innermost_block_tracker tracker;
  1694. expression_up expr = parse_expression (exp, &tracker);
  1695. newobj = new display (exp, std::move (expr), fmt,
  1696. current_program_space, tracker.block ());
  1697. all_displays.emplace_back (newobj);
  1698. if (from_tty)
  1699. do_one_display (newobj);
  1700. dont_repeat ();
  1701. }
  1702. /* Clear out the display_chain. Done when new symtabs are loaded,
  1703. since this invalidates the types stored in many expressions. */
  1704. void
  1705. clear_displays ()
  1706. {
  1707. all_displays.clear ();
  1708. }
  1709. /* Delete the auto-display DISPLAY. */
  1710. static void
  1711. delete_display (struct display *display)
  1712. {
  1713. gdb_assert (display != NULL);
  1714. auto iter = std::find_if (all_displays.begin (),
  1715. all_displays.end (),
  1716. [=] (const std::unique_ptr<struct display> &item)
  1717. {
  1718. return item.get () == display;
  1719. });
  1720. gdb_assert (iter != all_displays.end ());
  1721. all_displays.erase (iter);
  1722. }
  1723. /* Call FUNCTION on each of the displays whose numbers are given in
  1724. ARGS. DATA is passed unmodified to FUNCTION. */
  1725. static void
  1726. map_display_numbers (const char *args,
  1727. gdb::function_view<void (struct display *)> function)
  1728. {
  1729. int num;
  1730. if (args == NULL)
  1731. error_no_arg (_("one or more display numbers"));
  1732. number_or_range_parser parser (args);
  1733. while (!parser.finished ())
  1734. {
  1735. const char *p = parser.cur_tok ();
  1736. num = parser.get_number ();
  1737. if (num == 0)
  1738. warning (_("bad display number at or near '%s'"), p);
  1739. else
  1740. {
  1741. auto iter = std::find_if (all_displays.begin (),
  1742. all_displays.end (),
  1743. [=] (const std::unique_ptr<display> &item)
  1744. {
  1745. return item->number == num;
  1746. });
  1747. if (iter == all_displays.end ())
  1748. gdb_printf (_("No display number %d.\n"), num);
  1749. else
  1750. function (iter->get ());
  1751. }
  1752. }
  1753. }
  1754. /* "undisplay" command. */
  1755. static void
  1756. undisplay_command (const char *args, int from_tty)
  1757. {
  1758. if (args == NULL)
  1759. {
  1760. if (query (_("Delete all auto-display expressions? ")))
  1761. clear_displays ();
  1762. dont_repeat ();
  1763. return;
  1764. }
  1765. map_display_numbers (args, delete_display);
  1766. dont_repeat ();
  1767. }
  1768. /* Display a single auto-display.
  1769. Do nothing if the display cannot be printed in the current context,
  1770. or if the display is disabled. */
  1771. static void
  1772. do_one_display (struct display *d)
  1773. {
  1774. int within_current_scope;
  1775. if (!d->enabled_p)
  1776. return;
  1777. /* The expression carries the architecture that was used at parse time.
  1778. This is a problem if the expression depends on architecture features
  1779. (e.g. register numbers), and the current architecture is now different.
  1780. For example, a display statement like "display/i $pc" is expected to
  1781. display the PC register of the current architecture, not the arch at
  1782. the time the display command was given. Therefore, we re-parse the
  1783. expression if the current architecture has changed. */
  1784. if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
  1785. {
  1786. d->exp.reset ();
  1787. d->block = NULL;
  1788. }
  1789. if (d->exp == NULL)
  1790. {
  1791. try
  1792. {
  1793. innermost_block_tracker tracker;
  1794. d->exp = parse_expression (d->exp_string.c_str (), &tracker);
  1795. d->block = tracker.block ();
  1796. }
  1797. catch (const gdb_exception &ex)
  1798. {
  1799. /* Can't re-parse the expression. Disable this display item. */
  1800. d->enabled_p = false;
  1801. warning (_("Unable to display \"%s\": %s"),
  1802. d->exp_string.c_str (), ex.what ());
  1803. return;
  1804. }
  1805. }
  1806. if (d->block)
  1807. {
  1808. if (d->pspace == current_program_space)
  1809. within_current_scope = contained_in (get_selected_block (0), d->block,
  1810. true);
  1811. else
  1812. within_current_scope = 0;
  1813. }
  1814. else
  1815. within_current_scope = 1;
  1816. if (!within_current_scope)
  1817. return;
  1818. scoped_restore save_display_number
  1819. = make_scoped_restore (&current_display_number, d->number);
  1820. annotate_display_begin ();
  1821. gdb_printf ("%d", d->number);
  1822. annotate_display_number_end ();
  1823. gdb_printf (": ");
  1824. if (d->format.size)
  1825. {
  1826. annotate_display_format ();
  1827. gdb_printf ("x/");
  1828. if (d->format.count != 1)
  1829. gdb_printf ("%d", d->format.count);
  1830. gdb_printf ("%c", d->format.format);
  1831. if (d->format.format != 'i' && d->format.format != 's')
  1832. gdb_printf ("%c", d->format.size);
  1833. gdb_printf (" ");
  1834. annotate_display_expression ();
  1835. gdb_puts (d->exp_string.c_str ());
  1836. annotate_display_expression_end ();
  1837. if (d->format.count != 1 || d->format.format == 'i')
  1838. gdb_printf ("\n");
  1839. else
  1840. gdb_printf (" ");
  1841. annotate_display_value ();
  1842. try
  1843. {
  1844. struct value *val;
  1845. CORE_ADDR addr;
  1846. val = evaluate_expression (d->exp.get ());
  1847. addr = value_as_address (val);
  1848. if (d->format.format == 'i')
  1849. addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
  1850. do_examine (d->format, d->exp->gdbarch, addr);
  1851. }
  1852. catch (const gdb_exception_error &ex)
  1853. {
  1854. gdb_printf (_("%p[<error: %s>%p]\n"),
  1855. metadata_style.style ().ptr (), ex.what (),
  1856. nullptr);
  1857. }
  1858. }
  1859. else
  1860. {
  1861. struct value_print_options opts;
  1862. annotate_display_format ();
  1863. if (d->format.format)
  1864. gdb_printf ("/%c ", d->format.format);
  1865. annotate_display_expression ();
  1866. gdb_puts (d->exp_string.c_str ());
  1867. annotate_display_expression_end ();
  1868. gdb_printf (" = ");
  1869. annotate_display_expression ();
  1870. get_formatted_print_options (&opts, d->format.format);
  1871. opts.raw = d->format.raw;
  1872. try
  1873. {
  1874. struct value *val;
  1875. val = evaluate_expression (d->exp.get ());
  1876. print_formatted (val, d->format.size, &opts, gdb_stdout);
  1877. }
  1878. catch (const gdb_exception_error &ex)
  1879. {
  1880. fprintf_styled (gdb_stdout, metadata_style.style (),
  1881. _("<error: %s>"), ex.what ());
  1882. }
  1883. gdb_printf ("\n");
  1884. }
  1885. annotate_display_end ();
  1886. gdb_flush (gdb_stdout);
  1887. }
  1888. /* Display all of the values on the auto-display chain which can be
  1889. evaluated in the current scope. */
  1890. void
  1891. do_displays (void)
  1892. {
  1893. for (auto &d : all_displays)
  1894. do_one_display (d.get ());
  1895. }
  1896. /* Delete the auto-display which we were in the process of displaying.
  1897. This is done when there is an error or a signal. */
  1898. void
  1899. disable_display (int num)
  1900. {
  1901. for (auto &d : all_displays)
  1902. if (d->number == num)
  1903. {
  1904. d->enabled_p = false;
  1905. return;
  1906. }
  1907. gdb_printf (_("No display number %d.\n"), num);
  1908. }
  1909. void
  1910. disable_current_display (void)
  1911. {
  1912. if (current_display_number >= 0)
  1913. {
  1914. disable_display (current_display_number);
  1915. gdb_printf (gdb_stderr,
  1916. _("Disabling display %d to "
  1917. "avoid infinite recursion.\n"),
  1918. current_display_number);
  1919. }
  1920. current_display_number = -1;
  1921. }
  1922. static void
  1923. info_display_command (const char *ignore, int from_tty)
  1924. {
  1925. if (all_displays.empty ())
  1926. gdb_printf (_("There are no auto-display expressions now.\n"));
  1927. else
  1928. gdb_printf (_("Auto-display expressions now in effect:\n\
  1929. Num Enb Expression\n"));
  1930. for (auto &d : all_displays)
  1931. {
  1932. gdb_printf ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
  1933. if (d->format.size)
  1934. gdb_printf ("/%d%c%c ", d->format.count, d->format.size,
  1935. d->format.format);
  1936. else if (d->format.format)
  1937. gdb_printf ("/%c ", d->format.format);
  1938. gdb_puts (d->exp_string.c_str ());
  1939. if (d->block && !contained_in (get_selected_block (0), d->block, true))
  1940. gdb_printf (_(" (cannot be evaluated in the current context)"));
  1941. gdb_printf ("\n");
  1942. }
  1943. }
  1944. /* Implementation of both the "disable display" and "enable display"
  1945. commands. ENABLE decides what to do. */
  1946. static void
  1947. enable_disable_display_command (const char *args, int from_tty, bool enable)
  1948. {
  1949. if (args == NULL)
  1950. {
  1951. for (auto &d : all_displays)
  1952. d->enabled_p = enable;
  1953. return;
  1954. }
  1955. map_display_numbers (args,
  1956. [=] (struct display *d)
  1957. {
  1958. d->enabled_p = enable;
  1959. });
  1960. }
  1961. /* The "enable display" command. */
  1962. static void
  1963. enable_display_command (const char *args, int from_tty)
  1964. {
  1965. enable_disable_display_command (args, from_tty, true);
  1966. }
  1967. /* The "disable display" command. */
  1968. static void
  1969. disable_display_command (const char *args, int from_tty)
  1970. {
  1971. enable_disable_display_command (args, from_tty, false);
  1972. }
  1973. /* display_chain items point to blocks and expressions. Some expressions in
  1974. turn may point to symbols.
  1975. Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
  1976. obstack_free'd when a shared library is unloaded.
  1977. Clear pointers that are about to become dangling.
  1978. Both .exp and .block fields will be restored next time we need to display
  1979. an item by re-parsing .exp_string field in the new execution context. */
  1980. static void
  1981. clear_dangling_display_expressions (struct objfile *objfile)
  1982. {
  1983. struct program_space *pspace;
  1984. /* With no symbol file we cannot have a block or expression from it. */
  1985. if (objfile == NULL)
  1986. return;
  1987. pspace = objfile->pspace;
  1988. if (objfile->separate_debug_objfile_backlink)
  1989. {
  1990. objfile = objfile->separate_debug_objfile_backlink;
  1991. gdb_assert (objfile->pspace == pspace);
  1992. }
  1993. for (auto &d : all_displays)
  1994. {
  1995. if (d->pspace != pspace)
  1996. continue;
  1997. struct objfile *bl_objf = nullptr;
  1998. if (d->block != nullptr)
  1999. {
  2000. bl_objf = block_objfile (d->block);
  2001. if (bl_objf->separate_debug_objfile_backlink != nullptr)
  2002. bl_objf = bl_objf->separate_debug_objfile_backlink;
  2003. }
  2004. if (bl_objf == objfile
  2005. || (d->exp != NULL && exp_uses_objfile (d->exp.get (), objfile)))
  2006. {
  2007. d->exp.reset ();
  2008. d->block = NULL;
  2009. }
  2010. }
  2011. }
  2012. /* Print the value in stack frame FRAME of a variable specified by a
  2013. struct symbol. NAME is the name to print; if NULL then VAR's print
  2014. name will be used. STREAM is the ui_file on which to print the
  2015. value. INDENT specifies the number of indent levels to print
  2016. before printing the variable name.
  2017. This function invalidates FRAME. */
  2018. void
  2019. print_variable_and_value (const char *name, struct symbol *var,
  2020. struct frame_info *frame,
  2021. struct ui_file *stream, int indent)
  2022. {
  2023. if (!name)
  2024. name = var->print_name ();
  2025. gdb_printf (stream, "%*s%ps = ", 2 * indent, "",
  2026. styled_string (variable_name_style.style (), name));
  2027. try
  2028. {
  2029. struct value *val;
  2030. struct value_print_options opts;
  2031. /* READ_VAR_VALUE needs a block in order to deal with non-local
  2032. references (i.e. to handle nested functions). In this context, we
  2033. print variables that are local to this frame, so we can avoid passing
  2034. a block to it. */
  2035. val = read_var_value (var, NULL, frame);
  2036. get_user_print_options (&opts);
  2037. opts.deref_ref = 1;
  2038. common_val_print_checked (val, stream, indent, &opts, current_language);
  2039. /* common_val_print invalidates FRAME when a pretty printer calls inferior
  2040. function. */
  2041. frame = NULL;
  2042. }
  2043. catch (const gdb_exception_error &except)
  2044. {
  2045. fprintf_styled (stream, metadata_style.style (),
  2046. "<error reading variable %s (%s)>", name,
  2047. except.what ());
  2048. }
  2049. gdb_printf (stream, "\n");
  2050. }
  2051. /* Subroutine of ui_printf to simplify it.
  2052. Print VALUE to STREAM using FORMAT.
  2053. VALUE is a C-style string either on the target or
  2054. in a GDB internal variable. */
  2055. static void
  2056. printf_c_string (struct ui_file *stream, const char *format,
  2057. struct value *value)
  2058. {
  2059. const gdb_byte *str;
  2060. if (value_type (value)->code () != TYPE_CODE_PTR
  2061. && VALUE_LVAL (value) == lval_internalvar
  2062. && c_is_string_type_p (value_type (value)))
  2063. {
  2064. size_t len = TYPE_LENGTH (value_type (value));
  2065. /* Copy the internal var value to TEM_STR and append a terminating null
  2066. character. This protects against corrupted C-style strings that lack
  2067. the terminating null char. It also allows Ada-style strings (not
  2068. null terminated) to be printed without problems. */
  2069. gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
  2070. memcpy (tem_str, value_contents (value).data (), len);
  2071. tem_str [len] = 0;
  2072. str = tem_str;
  2073. }
  2074. else
  2075. {
  2076. CORE_ADDR tem = value_as_address (value);;
  2077. if (tem == 0)
  2078. {
  2079. DIAGNOSTIC_PUSH
  2080. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2081. gdb_printf (stream, format, "(null)");
  2082. DIAGNOSTIC_POP
  2083. return;
  2084. }
  2085. /* This is a %s argument. Find the length of the string. */
  2086. size_t len;
  2087. for (len = 0;; len++)
  2088. {
  2089. gdb_byte c;
  2090. QUIT;
  2091. read_memory (tem + len, &c, 1);
  2092. if (c == 0)
  2093. break;
  2094. }
  2095. /* Copy the string contents into a string inside GDB. */
  2096. gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
  2097. if (len != 0)
  2098. read_memory (tem, tem_str, len);
  2099. tem_str[len] = 0;
  2100. str = tem_str;
  2101. }
  2102. DIAGNOSTIC_PUSH
  2103. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2104. gdb_printf (stream, format, (char *) str);
  2105. DIAGNOSTIC_POP
  2106. }
  2107. /* Subroutine of ui_printf to simplify it.
  2108. Print VALUE to STREAM using FORMAT.
  2109. VALUE is a wide C-style string on the target or
  2110. in a GDB internal variable. */
  2111. static void
  2112. printf_wide_c_string (struct ui_file *stream, const char *format,
  2113. struct value *value)
  2114. {
  2115. const gdb_byte *str;
  2116. size_t len;
  2117. struct gdbarch *gdbarch = value_type (value)->arch ();
  2118. struct type *wctype = lookup_typename (current_language,
  2119. "wchar_t", NULL, 0);
  2120. int wcwidth = TYPE_LENGTH (wctype);
  2121. if (VALUE_LVAL (value) == lval_internalvar
  2122. && c_is_string_type_p (value_type (value)))
  2123. {
  2124. str = value_contents (value).data ();
  2125. len = TYPE_LENGTH (value_type (value));
  2126. }
  2127. else
  2128. {
  2129. CORE_ADDR tem = value_as_address (value);
  2130. if (tem == 0)
  2131. {
  2132. DIAGNOSTIC_PUSH
  2133. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2134. gdb_printf (stream, format, "(null)");
  2135. DIAGNOSTIC_POP
  2136. return;
  2137. }
  2138. /* This is a %s argument. Find the length of the string. */
  2139. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2140. gdb_byte *buf = (gdb_byte *) alloca (wcwidth);
  2141. for (len = 0;; len += wcwidth)
  2142. {
  2143. QUIT;
  2144. read_memory (tem + len, buf, wcwidth);
  2145. if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
  2146. break;
  2147. }
  2148. /* Copy the string contents into a string inside GDB. */
  2149. gdb_byte *tem_str = (gdb_byte *) alloca (len + wcwidth);
  2150. if (len != 0)
  2151. read_memory (tem, tem_str, len);
  2152. memset (&tem_str[len], 0, wcwidth);
  2153. str = tem_str;
  2154. }
  2155. auto_obstack output;
  2156. convert_between_encodings (target_wide_charset (gdbarch),
  2157. host_charset (),
  2158. str, len, wcwidth,
  2159. &output, translit_char);
  2160. obstack_grow_str0 (&output, "");
  2161. DIAGNOSTIC_PUSH
  2162. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2163. gdb_printf (stream, format, obstack_base (&output));
  2164. DIAGNOSTIC_POP
  2165. }
  2166. /* Subroutine of ui_printf to simplify it.
  2167. Print VALUE, a floating point value, to STREAM using FORMAT. */
  2168. static void
  2169. printf_floating (struct ui_file *stream, const char *format,
  2170. struct value *value, enum argclass argclass)
  2171. {
  2172. /* Parameter data. */
  2173. struct type *param_type = value_type (value);
  2174. struct gdbarch *gdbarch = param_type->arch ();
  2175. /* Determine target type corresponding to the format string. */
  2176. struct type *fmt_type;
  2177. switch (argclass)
  2178. {
  2179. case double_arg:
  2180. fmt_type = builtin_type (gdbarch)->builtin_double;
  2181. break;
  2182. case long_double_arg:
  2183. fmt_type = builtin_type (gdbarch)->builtin_long_double;
  2184. break;
  2185. case dec32float_arg:
  2186. fmt_type = builtin_type (gdbarch)->builtin_decfloat;
  2187. break;
  2188. case dec64float_arg:
  2189. fmt_type = builtin_type (gdbarch)->builtin_decdouble;
  2190. break;
  2191. case dec128float_arg:
  2192. fmt_type = builtin_type (gdbarch)->builtin_declong;
  2193. break;
  2194. default:
  2195. gdb_assert_not_reached ("unexpected argument class");
  2196. }
  2197. /* To match the traditional GDB behavior, the conversion is
  2198. done differently depending on the type of the parameter:
  2199. - if the parameter has floating-point type, it's value
  2200. is converted to the target type;
  2201. - otherwise, if the parameter has a type that is of the
  2202. same size as a built-in floating-point type, the value
  2203. bytes are interpreted as if they were of that type, and
  2204. then converted to the target type (this is not done for
  2205. decimal floating-point argument classes);
  2206. - otherwise, if the source value has an integer value,
  2207. it's value is converted to the target type;
  2208. - otherwise, an error is raised.
  2209. In either case, the result of the conversion is a byte buffer
  2210. formatted in the target format for the target type. */
  2211. if (fmt_type->code () == TYPE_CODE_FLT)
  2212. {
  2213. param_type = float_type_from_length (param_type);
  2214. if (param_type != value_type (value))
  2215. value = value_from_contents (param_type,
  2216. value_contents (value).data ());
  2217. }
  2218. value = value_cast (fmt_type, value);
  2219. /* Convert the value to a string and print it. */
  2220. std::string str
  2221. = target_float_to_string (value_contents (value).data (), fmt_type, format);
  2222. gdb_puts (str.c_str (), stream);
  2223. }
  2224. /* Subroutine of ui_printf to simplify it.
  2225. Print VALUE, a target pointer, to STREAM using FORMAT. */
  2226. static void
  2227. printf_pointer (struct ui_file *stream, const char *format,
  2228. struct value *value)
  2229. {
  2230. /* We avoid the host's %p because pointers are too
  2231. likely to be the wrong size. The only interesting
  2232. modifier for %p is a width; extract that, and then
  2233. handle %p as glibc would: %#x or a literal "(nil)". */
  2234. const char *p;
  2235. char *fmt, *fmt_p;
  2236. #ifdef PRINTF_HAS_LONG_LONG
  2237. long long val = value_as_long (value);
  2238. #else
  2239. long val = value_as_long (value);
  2240. #endif
  2241. fmt = (char *) alloca (strlen (format) + 5);
  2242. /* Copy up to the leading %. */
  2243. p = format;
  2244. fmt_p = fmt;
  2245. while (*p)
  2246. {
  2247. int is_percent = (*p == '%');
  2248. *fmt_p++ = *p++;
  2249. if (is_percent)
  2250. {
  2251. if (*p == '%')
  2252. *fmt_p++ = *p++;
  2253. else
  2254. break;
  2255. }
  2256. }
  2257. if (val != 0)
  2258. *fmt_p++ = '#';
  2259. /* Copy any width or flags. Only the "-" flag is valid for pointers
  2260. -- see the format_pieces constructor. */
  2261. while (*p == '-' || (*p >= '0' && *p < '9'))
  2262. *fmt_p++ = *p++;
  2263. gdb_assert (*p == 'p' && *(p + 1) == '\0');
  2264. if (val != 0)
  2265. {
  2266. #ifdef PRINTF_HAS_LONG_LONG
  2267. *fmt_p++ = 'l';
  2268. #endif
  2269. *fmt_p++ = 'l';
  2270. *fmt_p++ = 'x';
  2271. *fmt_p++ = '\0';
  2272. DIAGNOSTIC_PUSH
  2273. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2274. gdb_printf (stream, fmt, val);
  2275. DIAGNOSTIC_POP
  2276. }
  2277. else
  2278. {
  2279. *fmt_p++ = 's';
  2280. *fmt_p++ = '\0';
  2281. DIAGNOSTIC_PUSH
  2282. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2283. gdb_printf (stream, fmt, "(nil)");
  2284. DIAGNOSTIC_POP
  2285. }
  2286. }
  2287. /* printf "printf format string" ARG to STREAM. */
  2288. static void
  2289. ui_printf (const char *arg, struct ui_file *stream)
  2290. {
  2291. const char *s = arg;
  2292. std::vector<struct value *> val_args;
  2293. if (s == 0)
  2294. error_no_arg (_("format-control string and values to print"));
  2295. s = skip_spaces (s);
  2296. /* A format string should follow, enveloped in double quotes. */
  2297. if (*s++ != '"')
  2298. error (_("Bad format string, missing '\"'."));
  2299. format_pieces fpieces (&s);
  2300. if (*s++ != '"')
  2301. error (_("Bad format string, non-terminated '\"'."));
  2302. s = skip_spaces (s);
  2303. if (*s != ',' && *s != 0)
  2304. error (_("Invalid argument syntax"));
  2305. if (*s == ',')
  2306. s++;
  2307. s = skip_spaces (s);
  2308. {
  2309. int nargs_wanted;
  2310. int i;
  2311. const char *current_substring;
  2312. nargs_wanted = 0;
  2313. for (auto &&piece : fpieces)
  2314. if (piece.argclass != literal_piece)
  2315. ++nargs_wanted;
  2316. /* Now, parse all arguments and evaluate them.
  2317. Store the VALUEs in VAL_ARGS. */
  2318. while (*s != '\0')
  2319. {
  2320. const char *s1;
  2321. s1 = s;
  2322. val_args.push_back (parse_to_comma_and_eval (&s1));
  2323. s = s1;
  2324. if (*s == ',')
  2325. s++;
  2326. }
  2327. if (val_args.size () != nargs_wanted)
  2328. error (_("Wrong number of arguments for specified format-string"));
  2329. /* Now actually print them. */
  2330. i = 0;
  2331. for (auto &&piece : fpieces)
  2332. {
  2333. current_substring = piece.string;
  2334. switch (piece.argclass)
  2335. {
  2336. case string_arg:
  2337. printf_c_string (stream, current_substring, val_args[i]);
  2338. break;
  2339. case wide_string_arg:
  2340. printf_wide_c_string (stream, current_substring, val_args[i]);
  2341. break;
  2342. case wide_char_arg:
  2343. {
  2344. struct gdbarch *gdbarch = value_type (val_args[i])->arch ();
  2345. struct type *wctype = lookup_typename (current_language,
  2346. "wchar_t", NULL, 0);
  2347. struct type *valtype;
  2348. const gdb_byte *bytes;
  2349. valtype = value_type (val_args[i]);
  2350. if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
  2351. || valtype->code () != TYPE_CODE_INT)
  2352. error (_("expected wchar_t argument for %%lc"));
  2353. bytes = value_contents (val_args[i]).data ();
  2354. auto_obstack output;
  2355. convert_between_encodings (target_wide_charset (gdbarch),
  2356. host_charset (),
  2357. bytes, TYPE_LENGTH (valtype),
  2358. TYPE_LENGTH (valtype),
  2359. &output, translit_char);
  2360. obstack_grow_str0 (&output, "");
  2361. DIAGNOSTIC_PUSH
  2362. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2363. gdb_printf (stream, current_substring,
  2364. obstack_base (&output));
  2365. DIAGNOSTIC_POP
  2366. }
  2367. break;
  2368. case long_long_arg:
  2369. #ifdef PRINTF_HAS_LONG_LONG
  2370. {
  2371. long long val = value_as_long (val_args[i]);
  2372. DIAGNOSTIC_PUSH
  2373. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2374. gdb_printf (stream, current_substring, val);
  2375. DIAGNOSTIC_POP
  2376. break;
  2377. }
  2378. #else
  2379. error (_("long long not supported in printf"));
  2380. #endif
  2381. case int_arg:
  2382. {
  2383. int val = value_as_long (val_args[i]);
  2384. DIAGNOSTIC_PUSH
  2385. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2386. gdb_printf (stream, current_substring, val);
  2387. DIAGNOSTIC_POP
  2388. break;
  2389. }
  2390. case long_arg:
  2391. {
  2392. long val = value_as_long (val_args[i]);
  2393. DIAGNOSTIC_PUSH
  2394. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2395. gdb_printf (stream, current_substring, val);
  2396. DIAGNOSTIC_POP
  2397. break;
  2398. }
  2399. case size_t_arg:
  2400. {
  2401. size_t val = value_as_long (val_args[i]);
  2402. DIAGNOSTIC_PUSH
  2403. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2404. gdb_printf (stream, current_substring, val);
  2405. DIAGNOSTIC_POP
  2406. break;
  2407. }
  2408. /* Handles floating-point values. */
  2409. case double_arg:
  2410. case long_double_arg:
  2411. case dec32float_arg:
  2412. case dec64float_arg:
  2413. case dec128float_arg:
  2414. printf_floating (stream, current_substring, val_args[i],
  2415. piece.argclass);
  2416. break;
  2417. case ptr_arg:
  2418. printf_pointer (stream, current_substring, val_args[i]);
  2419. break;
  2420. case literal_piece:
  2421. /* Print a portion of the format string that has no
  2422. directives. Note that this will not include any
  2423. ordinary %-specs, but it might include "%%". That is
  2424. why we use gdb_printf and not gdb_puts here.
  2425. Also, we pass a dummy argument because some platforms
  2426. have modified GCC to include -Wformat-security by
  2427. default, which will warn here if there is no
  2428. argument. */
  2429. DIAGNOSTIC_PUSH
  2430. DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
  2431. gdb_printf (stream, current_substring, 0);
  2432. DIAGNOSTIC_POP
  2433. break;
  2434. default:
  2435. internal_error (__FILE__, __LINE__,
  2436. _("failed internal consistency check"));
  2437. }
  2438. /* Maybe advance to the next argument. */
  2439. if (piece.argclass != literal_piece)
  2440. ++i;
  2441. }
  2442. }
  2443. }
  2444. /* Implement the "printf" command. */
  2445. static void
  2446. printf_command (const char *arg, int from_tty)
  2447. {
  2448. ui_printf (arg, gdb_stdout);
  2449. gdb_stdout->reset_style ();
  2450. gdb_stdout->wrap_here (0);
  2451. gdb_stdout->flush ();
  2452. }
  2453. /* Implement the "eval" command. */
  2454. static void
  2455. eval_command (const char *arg, int from_tty)
  2456. {
  2457. string_file stb;
  2458. ui_printf (arg, &stb);
  2459. std::string expanded = insert_user_defined_cmd_args (stb.c_str ());
  2460. execute_command (expanded.c_str (), from_tty);
  2461. }
  2462. /* Convenience function for error checking in memory-tag commands. */
  2463. static void
  2464. show_addr_not_tagged (CORE_ADDR address)
  2465. {
  2466. error (_("Address %s not in a region mapped with a memory tagging flag."),
  2467. paddress (target_gdbarch (), address));
  2468. }
  2469. /* Convenience function for error checking in memory-tag commands. */
  2470. static void
  2471. show_memory_tagging_unsupported (void)
  2472. {
  2473. error (_("Memory tagging not supported or disabled by the current"
  2474. " architecture."));
  2475. }
  2476. /* Implement the "memory-tag" prefix command. */
  2477. static void
  2478. memory_tag_command (const char *arg, int from_tty)
  2479. {
  2480. help_list (memory_tag_list, "memory-tag ", all_commands, gdb_stdout);
  2481. }
  2482. /* Helper for print-logical-tag and print-allocation-tag. */
  2483. static void
  2484. memory_tag_print_tag_command (const char *args, enum memtag_type tag_type)
  2485. {
  2486. if (args == nullptr)
  2487. error_no_arg (_("address or pointer"));
  2488. /* Parse args into a value. If the value is a pointer or an address,
  2489. then fetch the logical or allocation tag. */
  2490. value_print_options print_opts;
  2491. struct value *val = process_print_command_args (args, &print_opts, true);
  2492. /* If the address is not in a region memory mapped with a memory tagging
  2493. flag, it is no use trying to access/manipulate its allocation tag.
  2494. It is OK to manipulate the logical tag though. */
  2495. if (tag_type == memtag_type::allocation
  2496. && !gdbarch_tagged_address_p (target_gdbarch (), val))
  2497. show_addr_not_tagged (value_as_address (val));
  2498. struct value *tag_value
  2499. = gdbarch_get_memtag (target_gdbarch (), val, tag_type);
  2500. std::string tag = gdbarch_memtag_to_string (target_gdbarch (), tag_value);
  2501. if (tag.empty ())
  2502. gdb_printf (_("%s tag unavailable.\n"),
  2503. tag_type
  2504. == memtag_type::logical? "Logical" : "Allocation");
  2505. struct value *v_tag = process_print_command_args (tag.c_str (),
  2506. &print_opts,
  2507. true);
  2508. print_opts.output_format = 'x';
  2509. print_value (v_tag, print_opts);
  2510. }
  2511. /* Implement the "memory-tag print-logical-tag" command. */
  2512. static void
  2513. memory_tag_print_logical_tag_command (const char *args, int from_tty)
  2514. {
  2515. if (!target_supports_memory_tagging ())
  2516. show_memory_tagging_unsupported ();
  2517. memory_tag_print_tag_command (args, memtag_type::logical);
  2518. }
  2519. /* Implement the "memory-tag print-allocation-tag" command. */
  2520. static void
  2521. memory_tag_print_allocation_tag_command (const char *args, int from_tty)
  2522. {
  2523. if (!target_supports_memory_tagging ())
  2524. show_memory_tagging_unsupported ();
  2525. memory_tag_print_tag_command (args, memtag_type::allocation);
  2526. }
  2527. /* Parse ARGS and extract ADDR and TAG.
  2528. ARGS should have format <expression> <tag bytes>. */
  2529. static void
  2530. parse_with_logical_tag_input (const char *args, struct value **val,
  2531. gdb::byte_vector &tags,
  2532. value_print_options *print_opts)
  2533. {
  2534. /* Fetch the address. */
  2535. std::string address_string = extract_string_maybe_quoted (&args);
  2536. /* Parse the address into a value. */
  2537. *val = process_print_command_args (address_string.c_str (), print_opts,
  2538. true);
  2539. /* Fetch the tag bytes. */
  2540. std::string tag_string = extract_string_maybe_quoted (&args);
  2541. /* Validate the input. */
  2542. if (address_string.empty () || tag_string.empty ())
  2543. error (_("Missing arguments."));
  2544. if (tag_string.length () != 2)
  2545. error (_("Error parsing tags argument. The tag should be 2 digits."));
  2546. tags = hex2bin (tag_string.c_str ());
  2547. }
  2548. /* Implement the "memory-tag with-logical-tag" command. */
  2549. static void
  2550. memory_tag_with_logical_tag_command (const char *args, int from_tty)
  2551. {
  2552. if (!target_supports_memory_tagging ())
  2553. show_memory_tagging_unsupported ();
  2554. if (args == nullptr)
  2555. error_no_arg (_("<address> <tag>"));
  2556. gdb::byte_vector tags;
  2557. struct value *val;
  2558. value_print_options print_opts;
  2559. /* Parse the input. */
  2560. parse_with_logical_tag_input (args, &val, tags, &print_opts);
  2561. /* Setting the logical tag is just a local operation that does not touch
  2562. any memory from the target. Given an input value, we modify the value
  2563. to include the appropriate tag.
  2564. For this reason we need to cast the argument value to a
  2565. (void *) pointer. This is so we have the right type for the gdbarch
  2566. hook to manipulate the value and insert the tag.
  2567. Otherwise, this would fail if, for example, GDB parsed the argument value
  2568. into an int-sized value and the pointer value has a type of greater
  2569. length. */
  2570. /* Cast to (void *). */
  2571. val = value_cast (builtin_type (target_gdbarch ())->builtin_data_ptr,
  2572. val);
  2573. /* Length doesn't matter for a logical tag. Pass 0. */
  2574. if (!gdbarch_set_memtags (target_gdbarch (), val, 0, tags,
  2575. memtag_type::logical))
  2576. gdb_printf (_("Could not update the logical tag data.\n"));
  2577. else
  2578. {
  2579. /* Always print it in hex format. */
  2580. print_opts.output_format = 'x';
  2581. print_value (val, print_opts);
  2582. }
  2583. }
  2584. /* Parse ARGS and extract ADDR, LENGTH and TAGS. */
  2585. static void
  2586. parse_set_allocation_tag_input (const char *args, struct value **val,
  2587. size_t *length, gdb::byte_vector &tags)
  2588. {
  2589. /* Fetch the address. */
  2590. std::string address_string = extract_string_maybe_quoted (&args);
  2591. /* Parse the address into a value. */
  2592. value_print_options print_opts;
  2593. *val = process_print_command_args (address_string.c_str (), &print_opts,
  2594. true);
  2595. /* Fetch the length. */
  2596. std::string length_string = extract_string_maybe_quoted (&args);
  2597. /* Fetch the tag bytes. */
  2598. std::string tags_string = extract_string_maybe_quoted (&args);
  2599. /* Validate the input. */
  2600. if (address_string.empty () || length_string.empty () || tags_string.empty ())
  2601. error (_("Missing arguments."));
  2602. errno = 0;
  2603. const char *trailer = nullptr;
  2604. LONGEST parsed_length = strtoulst (length_string.c_str (), &trailer, 10);
  2605. if (errno != 0 || (trailer != nullptr && trailer[0] != '\0'))
  2606. error (_("Error parsing length argument."));
  2607. if (parsed_length <= 0)
  2608. error (_("Invalid zero or negative length."));
  2609. *length = parsed_length;
  2610. if (tags_string.length () % 2)
  2611. error (_("Error parsing tags argument. Tags should be 2 digits per byte."));
  2612. tags = hex2bin (tags_string.c_str ());
  2613. /* If the address is not in a region memory mapped with a memory tagging
  2614. flag, it is no use trying to access/manipulate its allocation tag. */
  2615. if (!gdbarch_tagged_address_p (target_gdbarch (), *val))
  2616. show_addr_not_tagged (value_as_address (*val));
  2617. }
  2618. /* Implement the "memory-tag set-allocation-tag" command.
  2619. ARGS should be in the format <address> <length> <tags>. */
  2620. static void
  2621. memory_tag_set_allocation_tag_command (const char *args, int from_tty)
  2622. {
  2623. if (!target_supports_memory_tagging ())
  2624. show_memory_tagging_unsupported ();
  2625. if (args == nullptr)
  2626. error_no_arg (_("<starting address> <length> <tag bytes>"));
  2627. gdb::byte_vector tags;
  2628. size_t length = 0;
  2629. struct value *val;
  2630. /* Parse the input. */
  2631. parse_set_allocation_tag_input (args, &val, &length, tags);
  2632. if (!gdbarch_set_memtags (target_gdbarch (), val, length, tags,
  2633. memtag_type::allocation))
  2634. gdb_printf (_("Could not update the allocation tag(s).\n"));
  2635. else
  2636. gdb_printf (_("Allocation tag(s) updated successfully.\n"));
  2637. }
  2638. /* Implement the "memory-tag check" command. */
  2639. static void
  2640. memory_tag_check_command (const char *args, int from_tty)
  2641. {
  2642. if (!target_supports_memory_tagging ())
  2643. show_memory_tagging_unsupported ();
  2644. if (args == nullptr)
  2645. error (_("Argument required (address or pointer)"));
  2646. /* Parse the expression into a value. If the value is an address or
  2647. pointer, then check its logical tag against the allocation tag. */
  2648. value_print_options print_opts;
  2649. struct value *val = process_print_command_args (args, &print_opts, true);
  2650. /* If the address is not in a region memory mapped with a memory tagging
  2651. flag, it is no use trying to access/manipulate its allocation tag. */
  2652. if (!gdbarch_tagged_address_p (target_gdbarch (), val))
  2653. show_addr_not_tagged (value_as_address (val));
  2654. CORE_ADDR addr = value_as_address (val);
  2655. /* Check if the tag is valid. */
  2656. if (!gdbarch_memtag_matches_p (target_gdbarch (), val))
  2657. {
  2658. struct value *tag
  2659. = gdbarch_get_memtag (target_gdbarch (), val, memtag_type::logical);
  2660. std::string ltag
  2661. = gdbarch_memtag_to_string (target_gdbarch (), tag);
  2662. tag = gdbarch_get_memtag (target_gdbarch (), val,
  2663. memtag_type::allocation);
  2664. std::string atag
  2665. = gdbarch_memtag_to_string (target_gdbarch (), tag);
  2666. gdb_printf (_("Logical tag (%s) does not match"
  2667. " the allocation tag (%s) for address %s.\n"),
  2668. ltag.c_str (), atag.c_str (),
  2669. paddress (target_gdbarch (), addr));
  2670. }
  2671. else
  2672. {
  2673. struct value *tag
  2674. = gdbarch_get_memtag (target_gdbarch (), val, memtag_type::logical);
  2675. std::string ltag
  2676. = gdbarch_memtag_to_string (target_gdbarch (), tag);
  2677. gdb_printf (_("Memory tags for address %s match (%s).\n"),
  2678. paddress (target_gdbarch (), addr), ltag.c_str ());
  2679. }
  2680. }
  2681. void _initialize_printcmd ();
  2682. void
  2683. _initialize_printcmd ()
  2684. {
  2685. struct cmd_list_element *c;
  2686. current_display_number = -1;
  2687. gdb::observers::free_objfile.attach (clear_dangling_display_expressions,
  2688. "printcmd");
  2689. add_info ("address", info_address_command,
  2690. _("Describe where symbol SYM is stored.\n\
  2691. Usage: info address SYM"));
  2692. add_info ("symbol", info_symbol_command, _("\
  2693. Describe what symbol is at location ADDR.\n\
  2694. Usage: info symbol ADDR\n\
  2695. Only for symbols with fixed locations (global or static scope)."));
  2696. c = add_com ("x", class_vars, x_command, _("\
  2697. Examine memory: x/FMT ADDRESS.\n\
  2698. ADDRESS is an expression for the memory address to examine.\n\
  2699. FMT is a repeat count followed by a format letter and a size letter.\n\
  2700. Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
  2701. t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
  2702. and z(hex, zero padded on the left).\n\
  2703. Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
  2704. The specified number of objects of the specified size are printed\n\
  2705. according to the format. If a negative number is specified, memory is\n\
  2706. examined backward from the address.\n\n\
  2707. Defaults for format and size letters are those previously used.\n\
  2708. Default count is 1. Default address is following last thing printed\n\
  2709. with this command or \"print\"."));
  2710. set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
  2711. add_info ("display", info_display_command, _("\
  2712. Expressions to display when program stops, with code numbers.\n\
  2713. Usage: info display"));
  2714. add_cmd ("undisplay", class_vars, undisplay_command, _("\
  2715. Cancel some expressions to be displayed when program stops.\n\
  2716. Usage: undisplay [NUM]...\n\
  2717. Arguments are the code numbers of the expressions to stop displaying.\n\
  2718. No argument means cancel all automatic-display expressions.\n\
  2719. \"delete display\" has the same effect as this command.\n\
  2720. Do \"info display\" to see current list of code numbers."),
  2721. &cmdlist);
  2722. c = add_com ("display", class_vars, display_command, _("\
  2723. Print value of expression EXP each time the program stops.\n\
  2724. Usage: display[/FMT] EXP\n\
  2725. /FMT may be used before EXP as in the \"print\" command.\n\
  2726. /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
  2727. as in the \"x\" command, and then EXP is used to get the address to examine\n\
  2728. and examining is done as in the \"x\" command.\n\n\
  2729. With no argument, display all currently requested auto-display expressions.\n\
  2730. Use \"undisplay\" to cancel display requests previously made."));
  2731. set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
  2732. add_cmd ("display", class_vars, enable_display_command, _("\
  2733. Enable some expressions to be displayed when program stops.\n\
  2734. Usage: enable display [NUM]...\n\
  2735. Arguments are the code numbers of the expressions to resume displaying.\n\
  2736. No argument means enable all automatic-display expressions.\n\
  2737. Do \"info display\" to see current list of code numbers."), &enablelist);
  2738. add_cmd ("display", class_vars, disable_display_command, _("\
  2739. Disable some expressions to be displayed when program stops.\n\
  2740. Usage: disable display [NUM]...\n\
  2741. Arguments are the code numbers of the expressions to stop displaying.\n\
  2742. No argument means disable all automatic-display expressions.\n\
  2743. Do \"info display\" to see current list of code numbers."), &disablelist);
  2744. add_cmd ("display", class_vars, undisplay_command, _("\
  2745. Cancel some expressions to be displayed when program stops.\n\
  2746. Usage: delete display [NUM]...\n\
  2747. Arguments are the code numbers of the expressions to stop displaying.\n\
  2748. No argument means cancel all automatic-display expressions.\n\
  2749. Do \"info display\" to see current list of code numbers."), &deletelist);
  2750. add_com ("printf", class_vars, printf_command, _("\
  2751. Formatted printing, like the C \"printf\" function.\n\
  2752. Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
  2753. This supports most C printf format specifications, like %s, %d, etc."));
  2754. add_com ("output", class_vars, output_command, _("\
  2755. Like \"print\" but don't put in value history and don't print newline.\n\
  2756. Usage: output EXP\n\
  2757. This is useful in user-defined commands."));
  2758. add_prefix_cmd ("set", class_vars, set_command, _("\
  2759. Evaluate expression EXP and assign result to variable VAR.\n\
  2760. Usage: set VAR = EXP\n\
  2761. This uses assignment syntax appropriate for the current language\n\
  2762. (VAR = EXP or VAR := EXP for example).\n\
  2763. VAR may be a debugger \"convenience\" variable (names starting\n\
  2764. with $), a register (a few standard names starting with $), or an actual\n\
  2765. variable in the program being debugged. EXP is any valid expression.\n\
  2766. Use \"set variable\" for variables with names identical to set subcommands.\n\
  2767. \n\
  2768. With a subcommand, this command modifies parts of the gdb environment.\n\
  2769. You can see these environment settings with the \"show\" command."),
  2770. &setlist, 1, &cmdlist);
  2771. /* "call" is the same as "set", but handy for dbx users to call fns. */
  2772. c = add_com ("call", class_vars, call_command, _("\
  2773. Call a function in the program.\n\
  2774. Usage: call EXP\n\
  2775. The argument is the function name and arguments, in the notation of the\n\
  2776. current working language. The result is printed and saved in the value\n\
  2777. history, if it is not void."));
  2778. set_cmd_completer_handle_brkchars (c, print_command_completer);
  2779. cmd_list_element *set_variable_cmd
  2780. = add_cmd ("variable", class_vars, set_command, _("\
  2781. Evaluate expression EXP and assign result to variable VAR.\n\
  2782. Usage: set variable VAR = EXP\n\
  2783. This uses assignment syntax appropriate for the current language\n\
  2784. (VAR = EXP or VAR := EXP for example).\n\
  2785. VAR may be a debugger \"convenience\" variable (names starting\n\
  2786. with $), a register (a few standard names starting with $), or an actual\n\
  2787. variable in the program being debugged. EXP is any valid expression.\n\
  2788. This may usually be abbreviated to simply \"set\"."),
  2789. &setlist);
  2790. add_alias_cmd ("var", set_variable_cmd, class_vars, 0, &setlist);
  2791. const auto print_opts = make_value_print_options_def_group (nullptr);
  2792. static const std::string print_help = gdb::option::build_help (_("\
  2793. Print value of expression EXP.\n\
  2794. Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
  2795. \n\
  2796. Options:\n\
  2797. %OPTIONS%\n\
  2798. \n\
  2799. Note: because this command accepts arbitrary expressions, if you\n\
  2800. specify any command option, you must use a double dash (\"--\")\n\
  2801. to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
  2802. \n\
  2803. Variables accessible are those of the lexical environment of the selected\n\
  2804. stack frame, plus all those whose scope is global or an entire file.\n\
  2805. \n\
  2806. $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
  2807. $$NUM refers to NUM'th value back from the last one.\n\
  2808. Names starting with $ refer to registers (with the values they would have\n\
  2809. if the program were to return to the stack frame now selected, restoring\n\
  2810. all registers saved by frames farther in) or else to debugger\n\
  2811. \"convenience\" variables (any such name not a known register).\n\
  2812. Use assignment expressions to give values to convenience variables.\n\
  2813. \n\
  2814. {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
  2815. @ is a binary operator for treating consecutive data objects\n\
  2816. anywhere in memory as an array. FOO@NUM gives an array whose first\n\
  2817. element is FOO, whose second element is stored in the space following\n\
  2818. where FOO is stored, etc. FOO must be an expression whose value\n\
  2819. resides in memory.\n\
  2820. \n\
  2821. EXP may be preceded with /FMT, where FMT is a format letter\n\
  2822. but no count or size letter (see \"x\" command)."),
  2823. print_opts);
  2824. cmd_list_element *print_cmd
  2825. = add_com ("print", class_vars, print_command, print_help.c_str ());
  2826. set_cmd_completer_handle_brkchars (print_cmd, print_command_completer);
  2827. add_com_alias ("p", print_cmd, class_vars, 1);
  2828. add_com_alias ("inspect", print_cmd, class_vars, 1);
  2829. add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
  2830. &max_symbolic_offset, _("\
  2831. Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
  2832. Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
  2833. Tell GDB to only display the symbolic form of an address if the\n\
  2834. offset between the closest earlier symbol and the address is less than\n\
  2835. the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
  2836. to always print the symbolic form of an address if any symbol precedes\n\
  2837. it. Zero is equivalent to \"unlimited\"."),
  2838. NULL,
  2839. show_max_symbolic_offset,
  2840. &setprintlist, &showprintlist);
  2841. add_setshow_boolean_cmd ("symbol-filename", no_class,
  2842. &print_symbol_filename, _("\
  2843. Set printing of source filename and line number with <SYMBOL>."), _("\
  2844. Show printing of source filename and line number with <SYMBOL>."), NULL,
  2845. NULL,
  2846. show_print_symbol_filename,
  2847. &setprintlist, &showprintlist);
  2848. add_com ("eval", no_class, eval_command, _("\
  2849. Construct a GDB command and then evaluate it.\n\
  2850. Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
  2851. Convert the arguments to a string as \"printf\" would, but then\n\
  2852. treat this string as a command line, and evaluate it."));
  2853. /* Memory tagging commands. */
  2854. add_prefix_cmd ("memory-tag", class_vars, memory_tag_command, _("\
  2855. Generic command for printing and manipulating memory tag properties."),
  2856. &memory_tag_list, 0, &cmdlist);
  2857. add_cmd ("print-logical-tag", class_vars,
  2858. memory_tag_print_logical_tag_command,
  2859. ("Print the logical tag from POINTER.\n\
  2860. Usage: memory-tag print-logical-tag <POINTER>.\n\
  2861. <POINTER> is an expression that evaluates to a pointer.\n\
  2862. Print the logical tag contained in POINTER. The tag interpretation is\n\
  2863. architecture-specific."),
  2864. &memory_tag_list);
  2865. add_cmd ("print-allocation-tag", class_vars,
  2866. memory_tag_print_allocation_tag_command,
  2867. _("Print the allocation tag for ADDRESS.\n\
  2868. Usage: memory-tag print-allocation-tag <ADDRESS>.\n\
  2869. <ADDRESS> is an expression that evaluates to a memory address.\n\
  2870. Print the allocation tag associated with the memory address ADDRESS.\n\
  2871. The tag interpretation is architecture-specific."),
  2872. &memory_tag_list);
  2873. add_cmd ("with-logical-tag", class_vars, memory_tag_with_logical_tag_command,
  2874. _("Print a POINTER with a specific logical TAG.\n\
  2875. Usage: memory-tag with-logical-tag <POINTER> <TAG>\n\
  2876. <POINTER> is an expression that evaluates to a pointer.\n\
  2877. <TAG> is a sequence of hex bytes that is interpreted by the architecture\n\
  2878. as a single memory tag."),
  2879. &memory_tag_list);
  2880. add_cmd ("set-allocation-tag", class_vars,
  2881. memory_tag_set_allocation_tag_command,
  2882. _("Set the allocation tag(s) for a memory range.\n\
  2883. Usage: memory-tag set-allocation-tag <ADDRESS> <LENGTH> <TAG_BYTES>\n\
  2884. <ADDRESS> is an expression that evaluates to a memory address\n\
  2885. <LENGTH> is the number of bytes that is added to <ADDRESS> to calculate\n\
  2886. the memory range.\n\
  2887. <TAG_BYTES> is a sequence of hex bytes that is interpreted by the\n\
  2888. architecture as one or more memory tags.\n\
  2889. Sets the tags of the memory range [ADDRESS, ADDRESS + LENGTH)\n\
  2890. to TAG_BYTES.\n\
  2891. \n\
  2892. If the number of tags is greater than or equal to the number of tag granules\n\
  2893. in the [ADDRESS, ADDRESS + LENGTH) range, only the tags up to the\n\
  2894. number of tag granules are updated.\n\
  2895. \n\
  2896. If the number of tags is less than the number of tag granules, then the\n\
  2897. command is a fill operation. The TAG_BYTES are interpreted as a pattern\n\
  2898. that gets repeated until the number of tag granules in the memory range\n\
  2899. [ADDRESS, ADDRESS + LENGTH) is updated."),
  2900. &memory_tag_list);
  2901. add_cmd ("check", class_vars, memory_tag_check_command,
  2902. _("Validate a pointer's logical tag against the allocation tag.\n\
  2903. Usage: memory-tag check <POINTER>\n\
  2904. <POINTER> is an expression that evaluates to a pointer\n\
  2905. Fetch the logical and allocation tags for POINTER and compare them\n\
  2906. for equality. If the tags do not match, print additional information about\n\
  2907. the tag mismatch."),
  2908. &memory_tag_list);
  2909. }