tracepoint.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166
  1. /* Tracing functionality for remote targets in custom GDB protocol
  2. Copyright (C) 1997-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 "arch-utils.h"
  16. #include "symtab.h"
  17. #include "frame.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "gdbcmd.h"
  21. #include "value.h"
  22. #include "target.h"
  23. #include "target-dcache.h"
  24. #include "language.h"
  25. #include "inferior.h"
  26. #include "breakpoint.h"
  27. #include "tracepoint.h"
  28. #include "linespec.h"
  29. #include "regcache.h"
  30. #include "completer.h"
  31. #include "block.h"
  32. #include "dictionary.h"
  33. #include "observable.h"
  34. #include "user-regs.h"
  35. #include "valprint.h"
  36. #include "gdbcore.h"
  37. #include "objfiles.h"
  38. #include "filenames.h"
  39. #include "gdbthread.h"
  40. #include "stack.h"
  41. #include "remote.h"
  42. #include "source.h"
  43. #include "ax.h"
  44. #include "ax-gdb.h"
  45. #include "memrange.h"
  46. #include "cli/cli-utils.h"
  47. #include "probe.h"
  48. #include "gdbsupport/filestuff.h"
  49. #include "gdbsupport/rsp-low.h"
  50. #include "tracefile.h"
  51. #include "location.h"
  52. #include <algorithm>
  53. #include "cli/cli-style.h"
  54. #include "expop.h"
  55. #include "gdbsupport/buildargv.h"
  56. #include <unistd.h>
  57. /* Maximum length of an agent aexpression.
  58. This accounts for the fact that packets are limited to 400 bytes
  59. (which includes everything -- including the checksum), and assumes
  60. the worst case of maximum length for each of the pieces of a
  61. continuation packet.
  62. NOTE: expressions get mem2hex'ed otherwise this would be twice as
  63. large. (400 - 31)/2 == 184 */
  64. #define MAX_AGENT_EXPR_LEN 184
  65. /*
  66. Tracepoint.c:
  67. This module defines the following debugger commands:
  68. trace : set a tracepoint on a function, line, or address.
  69. info trace : list all debugger-defined tracepoints.
  70. delete trace : delete one or more tracepoints.
  71. enable trace : enable one or more tracepoints.
  72. disable trace : disable one or more tracepoints.
  73. actions : specify actions to be taken at a tracepoint.
  74. passcount : specify a pass count for a tracepoint.
  75. tstart : start a trace experiment.
  76. tstop : stop a trace experiment.
  77. tstatus : query the status of a trace experiment.
  78. tfind : find a trace frame in the trace buffer.
  79. tdump : print everything collected at the current tracepoint.
  80. save-tracepoints : write tracepoint setup into a file.
  81. This module defines the following user-visible debugger variables:
  82. $trace_frame : sequence number of trace frame currently being debugged.
  83. $trace_line : source line of trace frame currently being debugged.
  84. $trace_file : source file of trace frame currently being debugged.
  85. $tracepoint : tracepoint number of trace frame currently being debugged.
  86. */
  87. /* ======= Important global variables: ======= */
  88. /* The list of all trace state variables. We don't retain pointers to
  89. any of these for any reason - API is by name or number only - so it
  90. works to have a vector of objects. */
  91. static std::vector<trace_state_variable> tvariables;
  92. /* The next integer to assign to a variable. */
  93. static int next_tsv_number = 1;
  94. /* Number of last traceframe collected. */
  95. static int traceframe_number;
  96. /* Tracepoint for last traceframe collected. */
  97. static int tracepoint_number;
  98. /* The traceframe info of the current traceframe. NULL if we haven't
  99. yet attempted to fetch it, or if the target does not support
  100. fetching this object, or if we're not inspecting a traceframe
  101. presently. */
  102. static traceframe_info_up current_traceframe_info;
  103. /* Tracing command lists. */
  104. static struct cmd_list_element *tfindlist;
  105. /* List of expressions to collect by default at each tracepoint hit. */
  106. std::string default_collect;
  107. static bool disconnected_tracing;
  108. /* This variable controls whether we ask the target for a linear or
  109. circular trace buffer. */
  110. static bool circular_trace_buffer;
  111. /* This variable is the requested trace buffer size, or -1 to indicate
  112. that we don't care and leave it up to the target to set a size. */
  113. static int trace_buffer_size = -1;
  114. /* Textual notes applying to the current and/or future trace runs. */
  115. static std::string trace_user;
  116. /* Textual notes applying to the current and/or future trace runs. */
  117. static std::string trace_notes;
  118. /* Textual notes applying to the stopping of a trace. */
  119. static std::string trace_stop_notes;
  120. /* support routines */
  121. struct collection_list;
  122. static char *mem2hex (gdb_byte *, char *, int);
  123. static counted_command_line all_tracepoint_actions (struct breakpoint *);
  124. static struct trace_status trace_status;
  125. const char *stop_reason_names[] = {
  126. "tunknown",
  127. "tnotrun",
  128. "tstop",
  129. "tfull",
  130. "tdisconnected",
  131. "tpasscount",
  132. "terror"
  133. };
  134. struct trace_status *
  135. current_trace_status (void)
  136. {
  137. return &trace_status;
  138. }
  139. /* Free and clear the traceframe info cache of the current
  140. traceframe. */
  141. static void
  142. clear_traceframe_info (void)
  143. {
  144. current_traceframe_info = NULL;
  145. }
  146. /* Set traceframe number to NUM. */
  147. static void
  148. set_traceframe_num (int num)
  149. {
  150. traceframe_number = num;
  151. set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
  152. }
  153. /* Set tracepoint number to NUM. */
  154. static void
  155. set_tracepoint_num (int num)
  156. {
  157. tracepoint_number = num;
  158. set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
  159. }
  160. /* Set externally visible debug variables for querying/printing
  161. the traceframe context (line, function, file). */
  162. static void
  163. set_traceframe_context (struct frame_info *trace_frame)
  164. {
  165. CORE_ADDR trace_pc;
  166. struct symbol *traceframe_fun;
  167. symtab_and_line traceframe_sal;
  168. /* Save as globals for internal use. */
  169. if (trace_frame != NULL
  170. && get_frame_pc_if_available (trace_frame, &trace_pc))
  171. {
  172. traceframe_sal = find_pc_line (trace_pc, 0);
  173. traceframe_fun = find_pc_function (trace_pc);
  174. /* Save linenumber as "$trace_line", a debugger variable visible to
  175. users. */
  176. set_internalvar_integer (lookup_internalvar ("trace_line"),
  177. traceframe_sal.line);
  178. }
  179. else
  180. {
  181. traceframe_fun = NULL;
  182. set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
  183. }
  184. /* Save func name as "$trace_func", a debugger variable visible to
  185. users. */
  186. if (traceframe_fun == NULL
  187. || traceframe_fun->linkage_name () == NULL)
  188. clear_internalvar (lookup_internalvar ("trace_func"));
  189. else
  190. set_internalvar_string (lookup_internalvar ("trace_func"),
  191. traceframe_fun->linkage_name ());
  192. /* Save file name as "$trace_file", a debugger variable visible to
  193. users. */
  194. if (traceframe_sal.symtab == NULL)
  195. clear_internalvar (lookup_internalvar ("trace_file"));
  196. else
  197. set_internalvar_string (lookup_internalvar ("trace_file"),
  198. symtab_to_filename_for_display (traceframe_sal.symtab));
  199. }
  200. /* Create a new trace state variable with the given name. */
  201. struct trace_state_variable *
  202. create_trace_state_variable (const char *name)
  203. {
  204. tvariables.emplace_back (name, next_tsv_number++);
  205. return &tvariables.back ();
  206. }
  207. /* Look for a trace state variable of the given name. */
  208. struct trace_state_variable *
  209. find_trace_state_variable (const char *name)
  210. {
  211. for (trace_state_variable &tsv : tvariables)
  212. if (tsv.name == name)
  213. return &tsv;
  214. return NULL;
  215. }
  216. /* Look for a trace state variable of the given number. Return NULL if
  217. not found. */
  218. struct trace_state_variable *
  219. find_trace_state_variable_by_number (int number)
  220. {
  221. for (trace_state_variable &tsv : tvariables)
  222. if (tsv.number == number)
  223. return &tsv;
  224. return NULL;
  225. }
  226. static void
  227. delete_trace_state_variable (const char *name)
  228. {
  229. for (auto it = tvariables.begin (); it != tvariables.end (); it++)
  230. if (it->name == name)
  231. {
  232. gdb::observers::tsv_deleted.notify (&*it);
  233. tvariables.erase (it);
  234. return;
  235. }
  236. warning (_("No trace variable named \"$%s\", not deleting"), name);
  237. }
  238. /* Throws an error if NAME is not valid syntax for a trace state
  239. variable's name. */
  240. void
  241. validate_trace_state_variable_name (const char *name)
  242. {
  243. const char *p;
  244. if (*name == '\0')
  245. error (_("Must supply a non-empty variable name"));
  246. /* All digits in the name is reserved for value history
  247. references. */
  248. for (p = name; isdigit (*p); p++)
  249. ;
  250. if (*p == '\0')
  251. error (_("$%s is not a valid trace state variable name"), name);
  252. for (p = name; isalnum (*p) || *p == '_'; p++)
  253. ;
  254. if (*p != '\0')
  255. error (_("$%s is not a valid trace state variable name"), name);
  256. }
  257. /* The 'tvariable' command collects a name and optional expression to
  258. evaluate into an initial value. */
  259. static void
  260. trace_variable_command (const char *args, int from_tty)
  261. {
  262. LONGEST initval = 0;
  263. struct trace_state_variable *tsv;
  264. const char *name_start, *p;
  265. if (!args || !*args)
  266. error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
  267. /* Only allow two syntaxes; "$name" and "$name=value". */
  268. p = skip_spaces (args);
  269. if (*p++ != '$')
  270. error (_("Name of trace variable should start with '$'"));
  271. name_start = p;
  272. while (isalnum (*p) || *p == '_')
  273. p++;
  274. std::string name (name_start, p - name_start);
  275. p = skip_spaces (p);
  276. if (*p != '=' && *p != '\0')
  277. error (_("Syntax must be $NAME [ = EXPR ]"));
  278. validate_trace_state_variable_name (name.c_str ());
  279. if (*p == '=')
  280. initval = value_as_long (parse_and_eval (++p));
  281. /* If the variable already exists, just change its initial value. */
  282. tsv = find_trace_state_variable (name.c_str ());
  283. if (tsv)
  284. {
  285. if (tsv->initial_value != initval)
  286. {
  287. tsv->initial_value = initval;
  288. gdb::observers::tsv_modified.notify (tsv);
  289. }
  290. gdb_printf (_("Trace state variable $%s "
  291. "now has initial value %s.\n"),
  292. tsv->name.c_str (), plongest (tsv->initial_value));
  293. return;
  294. }
  295. /* Create a new variable. */
  296. tsv = create_trace_state_variable (name.c_str ());
  297. tsv->initial_value = initval;
  298. gdb::observers::tsv_created.notify (tsv);
  299. gdb_printf (_("Trace state variable $%s "
  300. "created, with initial value %s.\n"),
  301. tsv->name.c_str (), plongest (tsv->initial_value));
  302. }
  303. static void
  304. delete_trace_variable_command (const char *args, int from_tty)
  305. {
  306. if (args == NULL)
  307. {
  308. if (query (_("Delete all trace state variables? ")))
  309. tvariables.clear ();
  310. dont_repeat ();
  311. gdb::observers::tsv_deleted.notify (NULL);
  312. return;
  313. }
  314. gdb_argv argv (args);
  315. for (char *arg : argv)
  316. {
  317. if (*arg == '$')
  318. delete_trace_state_variable (arg + 1);
  319. else
  320. warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg);
  321. }
  322. dont_repeat ();
  323. }
  324. void
  325. tvariables_info_1 (void)
  326. {
  327. struct ui_out *uiout = current_uiout;
  328. /* Try to acquire values from the target. */
  329. for (trace_state_variable &tsv : tvariables)
  330. tsv.value_known
  331. = target_get_trace_state_variable_value (tsv.number, &tsv.value);
  332. {
  333. ui_out_emit_table table_emitter (uiout, 3, tvariables.size (),
  334. "trace-variables");
  335. uiout->table_header (15, ui_left, "name", "Name");
  336. uiout->table_header (11, ui_left, "initial", "Initial");
  337. uiout->table_header (11, ui_left, "current", "Current");
  338. uiout->table_body ();
  339. for (const trace_state_variable &tsv : tvariables)
  340. {
  341. const char *c;
  342. ui_out_emit_tuple tuple_emitter (uiout, "variable");
  343. uiout->field_string ("name", std::string ("$") + tsv.name);
  344. uiout->field_string ("initial", plongest (tsv.initial_value));
  345. ui_file_style style;
  346. if (tsv.value_known)
  347. c = plongest (tsv.value);
  348. else if (uiout->is_mi_like_p ())
  349. /* For MI, we prefer not to use magic string constants, but rather
  350. omit the field completely. The difference between unknown and
  351. undefined does not seem important enough to represent. */
  352. c = NULL;
  353. else if (current_trace_status ()->running || traceframe_number >= 0)
  354. {
  355. /* The value is/was defined, but we don't have it. */
  356. c = "<unknown>";
  357. style = metadata_style.style ();
  358. }
  359. else
  360. {
  361. /* It is not meaningful to ask about the value. */
  362. c = "<undefined>";
  363. style = metadata_style.style ();
  364. }
  365. if (c)
  366. uiout->field_string ("current", c, style);
  367. uiout->text ("\n");
  368. }
  369. }
  370. if (tvariables.empty ())
  371. uiout->text (_("No trace state variables.\n"));
  372. }
  373. /* List all the trace state variables. */
  374. static void
  375. info_tvariables_command (const char *args, int from_tty)
  376. {
  377. tvariables_info_1 ();
  378. }
  379. /* Stash definitions of tsvs into the given file. */
  380. void
  381. save_trace_state_variables (struct ui_file *fp)
  382. {
  383. for (const trace_state_variable &tsv : tvariables)
  384. {
  385. gdb_printf (fp, "tvariable $%s", tsv.name.c_str ());
  386. if (tsv.initial_value)
  387. gdb_printf (fp, " = %s", plongest (tsv.initial_value));
  388. gdb_printf (fp, "\n");
  389. }
  390. }
  391. /* ACTIONS functions: */
  392. /* The three functions:
  393. collect_pseudocommand,
  394. while_stepping_pseudocommand, and
  395. end_actions_pseudocommand
  396. are placeholders for "commands" that are actually ONLY to be used
  397. within a tracepoint action list. If the actual function is ever called,
  398. it means that somebody issued the "command" at the top level,
  399. which is always an error. */
  400. static void
  401. end_actions_pseudocommand (const char *args, int from_tty)
  402. {
  403. error (_("This command cannot be used at the top level."));
  404. }
  405. static void
  406. while_stepping_pseudocommand (const char *args, int from_tty)
  407. {
  408. error (_("This command can only be used in a tracepoint actions list."));
  409. }
  410. static void
  411. collect_pseudocommand (const char *args, int from_tty)
  412. {
  413. error (_("This command can only be used in a tracepoint actions list."));
  414. }
  415. static void
  416. teval_pseudocommand (const char *args, int from_tty)
  417. {
  418. error (_("This command can only be used in a tracepoint actions list."));
  419. }
  420. /* Parse any collection options, such as /s for strings. */
  421. const char *
  422. decode_agent_options (const char *exp, int *trace_string)
  423. {
  424. struct value_print_options opts;
  425. *trace_string = 0;
  426. if (*exp != '/')
  427. return exp;
  428. /* Call this to borrow the print elements default for collection
  429. size. */
  430. get_user_print_options (&opts);
  431. exp++;
  432. if (*exp == 's')
  433. {
  434. if (target_supports_string_tracing ())
  435. {
  436. /* Allow an optional decimal number giving an explicit maximum
  437. string length, defaulting it to the "print elements" value;
  438. so "collect/s80 mystr" gets at most 80 bytes of string. */
  439. *trace_string = opts.print_max;
  440. exp++;
  441. if (*exp >= '0' && *exp <= '9')
  442. *trace_string = atoi (exp);
  443. while (*exp >= '0' && *exp <= '9')
  444. exp++;
  445. }
  446. else
  447. error (_("Target does not support \"/s\" option for string tracing."));
  448. }
  449. else
  450. error (_("Undefined collection format \"%c\"."), *exp);
  451. exp = skip_spaces (exp);
  452. return exp;
  453. }
  454. /* Enter a list of actions for a tracepoint. */
  455. static void
  456. actions_command (const char *args, int from_tty)
  457. {
  458. struct tracepoint *t;
  459. t = get_tracepoint_by_number (&args, NULL);
  460. if (t)
  461. {
  462. std::string tmpbuf =
  463. string_printf ("Enter actions for tracepoint %d, one per line.",
  464. t->number);
  465. counted_command_line l = read_command_lines (tmpbuf.c_str (),
  466. from_tty, 1,
  467. [=] (const char *line)
  468. {
  469. validate_actionline (line, t);
  470. });
  471. breakpoint_set_commands (t, std::move (l));
  472. }
  473. /* else just return */
  474. }
  475. /* Report the results of checking the agent expression, as errors or
  476. internal errors. */
  477. static void
  478. report_agent_reqs_errors (struct agent_expr *aexpr)
  479. {
  480. /* All of the "flaws" are serious bytecode generation issues that
  481. should never occur. */
  482. if (aexpr->flaw != agent_flaw_none)
  483. internal_error (__FILE__, __LINE__, _("expression is malformed"));
  484. /* If analysis shows a stack underflow, GDB must have done something
  485. badly wrong in its bytecode generation. */
  486. if (aexpr->min_height < 0)
  487. internal_error (__FILE__, __LINE__,
  488. _("expression has min height < 0"));
  489. /* Issue this error if the stack is predicted to get too deep. The
  490. limit is rather arbitrary; a better scheme might be for the
  491. target to report how much stack it will have available. The
  492. depth roughly corresponds to parenthesization, so a limit of 20
  493. amounts to 20 levels of expression nesting, which is actually
  494. a pretty big hairy expression. */
  495. if (aexpr->max_height > 20)
  496. error (_("Expression is too complicated."));
  497. }
  498. /* Call ax_reqs on AEXPR and raise an error if something is wrong. */
  499. static void
  500. finalize_tracepoint_aexpr (struct agent_expr *aexpr)
  501. {
  502. ax_reqs (aexpr);
  503. if (aexpr->len > MAX_AGENT_EXPR_LEN)
  504. error (_("Expression is too complicated."));
  505. report_agent_reqs_errors (aexpr);
  506. }
  507. /* worker function */
  508. void
  509. validate_actionline (const char *line, struct breakpoint *b)
  510. {
  511. struct cmd_list_element *c;
  512. const char *tmp_p;
  513. const char *p;
  514. struct tracepoint *t = (struct tracepoint *) b;
  515. /* If EOF is typed, *line is NULL. */
  516. if (line == NULL)
  517. return;
  518. p = skip_spaces (line);
  519. /* Symbol lookup etc. */
  520. if (*p == '\0') /* empty line: just prompt for another line. */
  521. return;
  522. if (*p == '#') /* comment line */
  523. return;
  524. c = lookup_cmd (&p, cmdlist, "", NULL, -1, 1);
  525. if (c == 0)
  526. error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
  527. if (cmd_simple_func_eq (c, collect_pseudocommand))
  528. {
  529. int trace_string = 0;
  530. if (*p == '/')
  531. p = decode_agent_options (p, &trace_string);
  532. do
  533. { /* Repeat over a comma-separated list. */
  534. QUIT; /* Allow user to bail out with ^C. */
  535. p = skip_spaces (p);
  536. if (*p == '$') /* Look for special pseudo-symbols. */
  537. {
  538. if (0 == strncasecmp ("reg", p + 1, 3)
  539. || 0 == strncasecmp ("arg", p + 1, 3)
  540. || 0 == strncasecmp ("loc", p + 1, 3)
  541. || 0 == strncasecmp ("_ret", p + 1, 4)
  542. || 0 == strncasecmp ("_sdata", p + 1, 6))
  543. {
  544. p = strchr (p, ',');
  545. continue;
  546. }
  547. /* else fall thru, treat p as an expression and parse it! */
  548. }
  549. tmp_p = p;
  550. for (bp_location *loc : t->locations ())
  551. {
  552. p = tmp_p;
  553. expression_up exp = parse_exp_1 (&p, loc->address,
  554. block_for_pc (loc->address), 1);
  555. if (exp->first_opcode () == OP_VAR_VALUE)
  556. {
  557. symbol *sym;
  558. expr::var_value_operation *vvop
  559. = (dynamic_cast<expr::var_value_operation *>
  560. (exp->op.get ()));
  561. sym = vvop->get_symbol ();
  562. if (sym->aclass () == LOC_CONST)
  563. {
  564. error (_("constant `%s' (value %s) "
  565. "will not be collected."),
  566. sym->print_name (),
  567. plongest (SYMBOL_VALUE (sym)));
  568. }
  569. else if (sym->aclass () == LOC_OPTIMIZED_OUT)
  570. {
  571. error (_("`%s' is optimized away "
  572. "and cannot be collected."),
  573. sym->print_name ());
  574. }
  575. }
  576. /* We have something to collect, make sure that the expr to
  577. bytecode translator can handle it and that it's not too
  578. long. */
  579. agent_expr_up aexpr = gen_trace_for_expr (loc->address,
  580. exp.get (),
  581. trace_string);
  582. finalize_tracepoint_aexpr (aexpr.get ());
  583. }
  584. }
  585. while (p && *p++ == ',');
  586. }
  587. else if (cmd_simple_func_eq (c, teval_pseudocommand))
  588. {
  589. do
  590. { /* Repeat over a comma-separated list. */
  591. QUIT; /* Allow user to bail out with ^C. */
  592. p = skip_spaces (p);
  593. tmp_p = p;
  594. for (bp_location *loc : t->locations ())
  595. {
  596. p = tmp_p;
  597. /* Only expressions are allowed for this action. */
  598. expression_up exp = parse_exp_1 (&p, loc->address,
  599. block_for_pc (loc->address), 1);
  600. /* We have something to evaluate, make sure that the expr to
  601. bytecode translator can handle it and that it's not too
  602. long. */
  603. agent_expr_up aexpr = gen_eval_for_expr (loc->address, exp.get ());
  604. finalize_tracepoint_aexpr (aexpr.get ());
  605. }
  606. }
  607. while (p && *p++ == ',');
  608. }
  609. else if (cmd_simple_func_eq (c, while_stepping_pseudocommand))
  610. {
  611. char *endp;
  612. p = skip_spaces (p);
  613. t->step_count = strtol (p, &endp, 0);
  614. if (endp == p || t->step_count == 0)
  615. error (_("while-stepping step count `%s' is malformed."), line);
  616. p = endp;
  617. }
  618. else if (cmd_simple_func_eq (c, end_actions_pseudocommand))
  619. ;
  620. else
  621. error (_("`%s' is not a supported tracepoint action."), line);
  622. }
  623. enum {
  624. memrange_absolute = -1
  625. };
  626. /* MEMRANGE functions: */
  627. /* Compare memranges for std::sort. */
  628. static bool
  629. memrange_comp (const memrange &a, const memrange &b)
  630. {
  631. if (a.type == b.type)
  632. {
  633. if (a.type == memrange_absolute)
  634. return (bfd_vma) a.start < (bfd_vma) b.start;
  635. else
  636. return a.start < b.start;
  637. }
  638. return a.type < b.type;
  639. }
  640. /* Sort the memrange list using std::sort, and merge adjacent memranges. */
  641. static void
  642. memrange_sortmerge (std::vector<memrange> &memranges)
  643. {
  644. if (!memranges.empty ())
  645. {
  646. int a, b;
  647. std::sort (memranges.begin (), memranges.end (), memrange_comp);
  648. for (a = 0, b = 1; b < memranges.size (); b++)
  649. {
  650. /* If memrange b overlaps or is adjacent to memrange a,
  651. merge them. */
  652. if (memranges[a].type == memranges[b].type
  653. && memranges[b].start <= memranges[a].end)
  654. {
  655. if (memranges[b].end > memranges[a].end)
  656. memranges[a].end = memranges[b].end;
  657. continue; /* next b, same a */
  658. }
  659. a++; /* next a */
  660. if (a != b)
  661. memranges[a] = memranges[b];
  662. }
  663. memranges.resize (a + 1);
  664. }
  665. }
  666. /* Add remote register number REGNO to the collection list mask. */
  667. void
  668. collection_list::add_remote_register (unsigned int regno)
  669. {
  670. if (info_verbose)
  671. gdb_printf ("collect register %d\n", regno);
  672. m_regs_mask.at (regno / 8) |= 1 << (regno % 8);
  673. }
  674. /* Add all the registers from the mask in AEXPR to the mask in the
  675. collection list. Registers in the AEXPR mask are already remote
  676. register numbers. */
  677. void
  678. collection_list::add_ax_registers (struct agent_expr *aexpr)
  679. {
  680. if (aexpr->reg_mask_len > 0)
  681. {
  682. for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
  683. {
  684. QUIT; /* Allow user to bail out with ^C. */
  685. if (aexpr->reg_mask[ndx1] != 0)
  686. {
  687. /* Assume chars have 8 bits. */
  688. for (int ndx2 = 0; ndx2 < 8; ndx2++)
  689. if (aexpr->reg_mask[ndx1] & (1 << ndx2))
  690. /* It's used -- record it. */
  691. add_remote_register (ndx1 * 8 + ndx2);
  692. }
  693. }
  694. }
  695. }
  696. /* If REGNO is raw, add its corresponding remote register number to
  697. the mask. If REGNO is a pseudo-register, figure out the necessary
  698. registers using a temporary agent expression, and add it to the
  699. list if it needs more than just a mask. */
  700. void
  701. collection_list::add_local_register (struct gdbarch *gdbarch,
  702. unsigned int regno,
  703. CORE_ADDR scope)
  704. {
  705. if (regno < gdbarch_num_regs (gdbarch))
  706. {
  707. int remote_regno = gdbarch_remote_register_number (gdbarch, regno);
  708. if (remote_regno < 0)
  709. error (_("Can't collect register %d"), regno);
  710. add_remote_register (remote_regno);
  711. }
  712. else
  713. {
  714. agent_expr_up aexpr (new agent_expr (gdbarch, scope));
  715. ax_reg_mask (aexpr.get (), regno);
  716. finalize_tracepoint_aexpr (aexpr.get ());
  717. add_ax_registers (aexpr.get ());
  718. /* Usually ax_reg_mask for a pseudo-regiser only sets the
  719. corresponding raw registers in the ax mask, but if this isn't
  720. the case add the expression that is generated to the
  721. collection list. */
  722. if (aexpr->len > 0)
  723. add_aexpr (std::move (aexpr));
  724. }
  725. }
  726. /* Add a memrange to a collection list. */
  727. void
  728. collection_list::add_memrange (struct gdbarch *gdbarch,
  729. int type, bfd_signed_vma base,
  730. unsigned long len, CORE_ADDR scope)
  731. {
  732. if (info_verbose)
  733. gdb_printf ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len);
  734. /* type: memrange_absolute == memory, other n == basereg */
  735. /* base: addr if memory, offset if reg relative. */
  736. /* len: we actually save end (base + len) for convenience */
  737. m_memranges.emplace_back (type, base, base + len);
  738. if (type != memrange_absolute) /* Better collect the base register! */
  739. add_local_register (gdbarch, type, scope);
  740. }
  741. /* Add a symbol to a collection list. */
  742. void
  743. collection_list::collect_symbol (struct symbol *sym,
  744. struct gdbarch *gdbarch,
  745. long frame_regno, long frame_offset,
  746. CORE_ADDR scope,
  747. int trace_string)
  748. {
  749. unsigned long len;
  750. unsigned int reg;
  751. bfd_signed_vma offset;
  752. int treat_as_expr = 0;
  753. len = TYPE_LENGTH (check_typedef (sym->type ()));
  754. switch (sym->aclass ())
  755. {
  756. default:
  757. gdb_printf ("%s: don't know symbol class %d\n",
  758. sym->print_name (), sym->aclass ());
  759. break;
  760. case LOC_CONST:
  761. gdb_printf ("constant %s (value %s) will not be collected.\n",
  762. sym->print_name (), plongest (SYMBOL_VALUE (sym)));
  763. break;
  764. case LOC_STATIC:
  765. offset = SYMBOL_VALUE_ADDRESS (sym);
  766. if (info_verbose)
  767. {
  768. gdb_printf ("LOC_STATIC %s: collect %ld bytes at %s.\n",
  769. sym->print_name (), len,
  770. paddress (gdbarch, offset));
  771. }
  772. /* A struct may be a C++ class with static fields, go to general
  773. expression handling. */
  774. if (sym->type ()->code () == TYPE_CODE_STRUCT)
  775. treat_as_expr = 1;
  776. else
  777. add_memrange (gdbarch, memrange_absolute, offset, len, scope);
  778. break;
  779. case LOC_REGISTER:
  780. reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
  781. if (info_verbose)
  782. gdb_printf ("LOC_REG[parm] %s: ", sym->print_name ());
  783. add_local_register (gdbarch, reg, scope);
  784. /* Check for doubles stored in two registers. */
  785. /* FIXME: how about larger types stored in 3 or more regs? */
  786. if (sym->type ()->code () == TYPE_CODE_FLT &&
  787. len > register_size (gdbarch, reg))
  788. add_local_register (gdbarch, reg + 1, scope);
  789. break;
  790. case LOC_REF_ARG:
  791. gdb_printf ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
  792. gdb_printf (" (will not collect %s)\n", sym->print_name ());
  793. break;
  794. case LOC_ARG:
  795. reg = frame_regno;
  796. offset = frame_offset + SYMBOL_VALUE (sym);
  797. if (info_verbose)
  798. {
  799. gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
  800. " from frame ptr reg %d\n", sym->print_name (), len,
  801. paddress (gdbarch, offset), reg);
  802. }
  803. add_memrange (gdbarch, reg, offset, len, scope);
  804. break;
  805. case LOC_REGPARM_ADDR:
  806. reg = SYMBOL_VALUE (sym);
  807. offset = 0;
  808. if (info_verbose)
  809. {
  810. gdb_printf ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
  811. " from reg %d\n", sym->print_name (), len,
  812. paddress (gdbarch, offset), reg);
  813. }
  814. add_memrange (gdbarch, reg, offset, len, scope);
  815. break;
  816. case LOC_LOCAL:
  817. reg = frame_regno;
  818. offset = frame_offset + SYMBOL_VALUE (sym);
  819. if (info_verbose)
  820. {
  821. gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
  822. " from frame ptr reg %d\n", sym->print_name (), len,
  823. paddress (gdbarch, offset), reg);
  824. }
  825. add_memrange (gdbarch, reg, offset, len, scope);
  826. break;
  827. case LOC_UNRESOLVED:
  828. treat_as_expr = 1;
  829. break;
  830. case LOC_OPTIMIZED_OUT:
  831. gdb_printf ("%s has been optimized out of existence.\n",
  832. sym->print_name ());
  833. break;
  834. case LOC_COMPUTED:
  835. treat_as_expr = 1;
  836. break;
  837. }
  838. /* Expressions are the most general case. */
  839. if (treat_as_expr)
  840. {
  841. agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch,
  842. sym, trace_string);
  843. /* It can happen that the symbol is recorded as a computed
  844. location, but it's been optimized away and doesn't actually
  845. have a location expression. */
  846. if (!aexpr)
  847. {
  848. gdb_printf ("%s has been optimized out of existence.\n",
  849. sym->print_name ());
  850. return;
  851. }
  852. finalize_tracepoint_aexpr (aexpr.get ());
  853. /* Take care of the registers. */
  854. add_ax_registers (aexpr.get ());
  855. add_aexpr (std::move (aexpr));
  856. }
  857. }
  858. void
  859. collection_list::add_wholly_collected (const char *print_name)
  860. {
  861. m_wholly_collected.push_back (print_name);
  862. }
  863. /* Add all locals (or args) symbols to collection list. */
  864. void
  865. collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
  866. long frame_regno, long frame_offset, int type,
  867. int trace_string)
  868. {
  869. const struct block *block;
  870. int count = 0;
  871. auto do_collect_symbol = [&] (const char *print_name,
  872. struct symbol *sym)
  873. {
  874. collect_symbol (sym, gdbarch, frame_regno,
  875. frame_offset, pc, trace_string);
  876. count++;
  877. add_wholly_collected (print_name);
  878. };
  879. if (type == 'L')
  880. {
  881. block = block_for_pc (pc);
  882. if (block == NULL)
  883. {
  884. warning (_("Can't collect locals; "
  885. "no symbol table info available.\n"));
  886. return;
  887. }
  888. iterate_over_block_local_vars (block, do_collect_symbol);
  889. if (count == 0)
  890. warning (_("No locals found in scope."));
  891. }
  892. else
  893. {
  894. CORE_ADDR fn_pc = get_pc_function_start (pc);
  895. block = block_for_pc (fn_pc);
  896. if (block == NULL)
  897. {
  898. warning (_("Can't collect args; no symbol table info available."));
  899. return;
  900. }
  901. iterate_over_block_arg_vars (block, do_collect_symbol);
  902. if (count == 0)
  903. warning (_("No args found in scope."));
  904. }
  905. }
  906. void
  907. collection_list::add_static_trace_data ()
  908. {
  909. if (info_verbose)
  910. gdb_printf ("collect static trace data\n");
  911. m_strace_data = true;
  912. }
  913. collection_list::collection_list ()
  914. : m_strace_data (false)
  915. {
  916. int max_remote_regno = 0;
  917. for (int i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
  918. {
  919. int remote_regno = (gdbarch_remote_register_number
  920. (target_gdbarch (), i));
  921. if (remote_regno >= 0 && remote_regno > max_remote_regno)
  922. max_remote_regno = remote_regno;
  923. }
  924. m_regs_mask.resize ((max_remote_regno / 8) + 1);
  925. m_memranges.reserve (128);
  926. m_aexprs.reserve (128);
  927. }
  928. /* Reduce a collection list to string form (for gdb protocol). */
  929. std::vector<std::string>
  930. collection_list::stringify ()
  931. {
  932. gdb::char_vector temp_buf (2048);
  933. int count;
  934. char *end;
  935. long i;
  936. std::vector<std::string> str_list;
  937. if (m_strace_data)
  938. {
  939. if (info_verbose)
  940. gdb_printf ("\nCollecting static trace data\n");
  941. end = temp_buf.data ();
  942. *end++ = 'L';
  943. str_list.emplace_back (temp_buf.data (), end - temp_buf.data ());
  944. }
  945. for (i = m_regs_mask.size () - 1; i > 0; i--)
  946. if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
  947. break;
  948. if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
  949. {
  950. if (info_verbose)
  951. gdb_printf ("\nCollecting registers (mask): 0x");
  952. /* One char for 'R', one for the null terminator and two per
  953. mask byte. */
  954. std::size_t new_size = (i + 1) * 2 + 2;
  955. if (new_size > temp_buf.size ())
  956. temp_buf.resize (new_size);
  957. end = temp_buf.data ();
  958. *end++ = 'R';
  959. for (; i >= 0; i--)
  960. {
  961. QUIT; /* Allow user to bail out with ^C. */
  962. if (info_verbose)
  963. gdb_printf ("%02X", m_regs_mask[i]);
  964. end = pack_hex_byte (end, m_regs_mask[i]);
  965. }
  966. *end = '\0';
  967. str_list.emplace_back (temp_buf.data ());
  968. }
  969. if (info_verbose)
  970. gdb_printf ("\n");
  971. if (!m_memranges.empty () && info_verbose)
  972. gdb_printf ("Collecting memranges: \n");
  973. for (i = 0, count = 0, end = temp_buf.data ();
  974. i < m_memranges.size (); i++)
  975. {
  976. QUIT; /* Allow user to bail out with ^C. */
  977. if (info_verbose)
  978. {
  979. gdb_printf ("(%d, %s, %ld)\n",
  980. m_memranges[i].type,
  981. paddress (target_gdbarch (),
  982. m_memranges[i].start),
  983. (long) (m_memranges[i].end
  984. - m_memranges[i].start));
  985. }
  986. if (count + 27 > MAX_AGENT_EXPR_LEN)
  987. {
  988. str_list.emplace_back (temp_buf.data (), count);
  989. count = 0;
  990. end = temp_buf.data ();
  991. }
  992. {
  993. bfd_signed_vma length
  994. = m_memranges[i].end - m_memranges[i].start;
  995. /* The "%X" conversion specifier expects an unsigned argument,
  996. so passing -1 (memrange_absolute) to it directly gives you
  997. "FFFFFFFF" (or more, depending on sizeof (unsigned)).
  998. Special-case it. */
  999. if (m_memranges[i].type == memrange_absolute)
  1000. sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
  1001. (long) length);
  1002. else
  1003. sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
  1004. phex_nz (m_memranges[i].start, 0), (long) length);
  1005. }
  1006. count += strlen (end);
  1007. end = temp_buf.data () + count;
  1008. }
  1009. for (i = 0; i < m_aexprs.size (); i++)
  1010. {
  1011. QUIT; /* Allow user to bail out with ^C. */
  1012. if ((count + 10 + 2 * m_aexprs[i]->len) > MAX_AGENT_EXPR_LEN)
  1013. {
  1014. str_list.emplace_back (temp_buf.data (), count);
  1015. count = 0;
  1016. end = temp_buf.data ();
  1017. }
  1018. sprintf (end, "X%08X,", m_aexprs[i]->len);
  1019. end += 10; /* 'X' + 8 hex digits + ',' */
  1020. count += 10;
  1021. end = mem2hex (m_aexprs[i]->buf, end, m_aexprs[i]->len);
  1022. count += 2 * m_aexprs[i]->len;
  1023. }
  1024. if (count != 0)
  1025. {
  1026. str_list.emplace_back (temp_buf.data (), count);
  1027. count = 0;
  1028. end = temp_buf.data ();
  1029. }
  1030. return str_list;
  1031. }
  1032. /* Add the expression STR to M_COMPUTED. */
  1033. void
  1034. collection_list::append_exp (std::string &&str)
  1035. {
  1036. m_computed.push_back (std::move (str));
  1037. }
  1038. void
  1039. collection_list::finish ()
  1040. {
  1041. memrange_sortmerge (m_memranges);
  1042. }
  1043. static void
  1044. encode_actions_1 (struct command_line *action,
  1045. struct bp_location *tloc,
  1046. int frame_reg,
  1047. LONGEST frame_offset,
  1048. struct collection_list *collect,
  1049. struct collection_list *stepping_list)
  1050. {
  1051. const char *action_exp;
  1052. int i;
  1053. struct value *tempval;
  1054. struct cmd_list_element *cmd;
  1055. for (; action; action = action->next)
  1056. {
  1057. QUIT; /* Allow user to bail out with ^C. */
  1058. action_exp = action->line;
  1059. action_exp = skip_spaces (action_exp);
  1060. cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1);
  1061. if (cmd == 0)
  1062. error (_("Bad action list item: %s"), action_exp);
  1063. if (cmd_simple_func_eq (cmd, collect_pseudocommand))
  1064. {
  1065. int trace_string = 0;
  1066. if (*action_exp == '/')
  1067. action_exp = decode_agent_options (action_exp, &trace_string);
  1068. do
  1069. { /* Repeat over a comma-separated list. */
  1070. QUIT; /* Allow user to bail out with ^C. */
  1071. action_exp = skip_spaces (action_exp);
  1072. if (0 == strncasecmp ("$reg", action_exp, 4))
  1073. {
  1074. for (i = 0; i < gdbarch_num_regs (target_gdbarch ());
  1075. i++)
  1076. {
  1077. int remote_regno = (gdbarch_remote_register_number
  1078. (target_gdbarch (), i));
  1079. /* Ignore arch regnos without a corresponding
  1080. remote regno. This can happen for regnos not
  1081. in the tdesc. */
  1082. if (remote_regno >= 0)
  1083. collect->add_remote_register (remote_regno);
  1084. }
  1085. action_exp = strchr (action_exp, ','); /* more? */
  1086. }
  1087. else if (0 == strncasecmp ("$arg", action_exp, 4))
  1088. {
  1089. collect->add_local_symbols (target_gdbarch (),
  1090. tloc->address,
  1091. frame_reg,
  1092. frame_offset,
  1093. 'A',
  1094. trace_string);
  1095. action_exp = strchr (action_exp, ','); /* more? */
  1096. }
  1097. else if (0 == strncasecmp ("$loc", action_exp, 4))
  1098. {
  1099. collect->add_local_symbols (target_gdbarch (),
  1100. tloc->address,
  1101. frame_reg,
  1102. frame_offset,
  1103. 'L',
  1104. trace_string);
  1105. action_exp = strchr (action_exp, ','); /* more? */
  1106. }
  1107. else if (0 == strncasecmp ("$_ret", action_exp, 5))
  1108. {
  1109. agent_expr_up aexpr
  1110. = gen_trace_for_return_address (tloc->address,
  1111. target_gdbarch (),
  1112. trace_string);
  1113. finalize_tracepoint_aexpr (aexpr.get ());
  1114. /* take care of the registers */
  1115. collect->add_ax_registers (aexpr.get ());
  1116. collect->add_aexpr (std::move (aexpr));
  1117. action_exp = strchr (action_exp, ','); /* more? */
  1118. }
  1119. else if (0 == strncasecmp ("$_sdata", action_exp, 7))
  1120. {
  1121. collect->add_static_trace_data ();
  1122. action_exp = strchr (action_exp, ','); /* more? */
  1123. }
  1124. else
  1125. {
  1126. unsigned long addr;
  1127. const char *exp_start = action_exp;
  1128. expression_up exp = parse_exp_1 (&action_exp, tloc->address,
  1129. block_for_pc (tloc->address),
  1130. 1);
  1131. switch (exp->first_opcode ())
  1132. {
  1133. case OP_REGISTER:
  1134. {
  1135. expr::register_operation *regop
  1136. = (dynamic_cast<expr::register_operation *>
  1137. (exp->op.get ()));
  1138. const char *name = regop->get_name ();
  1139. i = user_reg_map_name_to_regnum (target_gdbarch (),
  1140. name, strlen (name));
  1141. if (i == -1)
  1142. internal_error (__FILE__, __LINE__,
  1143. _("Register $%s not available"),
  1144. name);
  1145. if (info_verbose)
  1146. gdb_printf ("OP_REGISTER: ");
  1147. collect->add_local_register (target_gdbarch (),
  1148. i, tloc->address);
  1149. break;
  1150. }
  1151. case UNOP_MEMVAL:
  1152. {
  1153. /* Safe because we know it's a simple expression. */
  1154. tempval = evaluate_expression (exp.get ());
  1155. addr = value_address (tempval);
  1156. expr::unop_memval_operation *memop
  1157. = (dynamic_cast<expr::unop_memval_operation *>
  1158. (exp->op.get ()));
  1159. struct type *type = memop->get_type ();
  1160. /* Initialize the TYPE_LENGTH if it is a typedef. */
  1161. check_typedef (type);
  1162. collect->add_memrange (target_gdbarch (),
  1163. memrange_absolute, addr,
  1164. TYPE_LENGTH (type),
  1165. tloc->address);
  1166. collect->append_exp (std::string (exp_start,
  1167. action_exp));
  1168. }
  1169. break;
  1170. case OP_VAR_VALUE:
  1171. {
  1172. expr::var_value_operation *vvo
  1173. = (dynamic_cast<expr::var_value_operation *>
  1174. (exp->op.get ()));
  1175. struct symbol *sym = vvo->get_symbol ();
  1176. const char *name = sym->natural_name ();
  1177. collect->collect_symbol (sym,
  1178. target_gdbarch (),
  1179. frame_reg,
  1180. frame_offset,
  1181. tloc->address,
  1182. trace_string);
  1183. collect->add_wholly_collected (name);
  1184. }
  1185. break;
  1186. default: /* Full-fledged expression. */
  1187. agent_expr_up aexpr = gen_trace_for_expr (tloc->address,
  1188. exp.get (),
  1189. trace_string);
  1190. finalize_tracepoint_aexpr (aexpr.get ());
  1191. /* Take care of the registers. */
  1192. collect->add_ax_registers (aexpr.get ());
  1193. collect->add_aexpr (std::move (aexpr));
  1194. collect->append_exp (std::string (exp_start,
  1195. action_exp));
  1196. break;
  1197. } /* switch */
  1198. } /* do */
  1199. }
  1200. while (action_exp && *action_exp++ == ',');
  1201. } /* if */
  1202. else if (cmd_simple_func_eq (cmd, teval_pseudocommand))
  1203. {
  1204. do
  1205. { /* Repeat over a comma-separated list. */
  1206. QUIT; /* Allow user to bail out with ^C. */
  1207. action_exp = skip_spaces (action_exp);
  1208. {
  1209. expression_up exp = parse_exp_1 (&action_exp, tloc->address,
  1210. block_for_pc (tloc->address),
  1211. 1);
  1212. agent_expr_up aexpr = gen_eval_for_expr (tloc->address,
  1213. exp.get ());
  1214. finalize_tracepoint_aexpr (aexpr.get ());
  1215. /* Even though we're not officially collecting, add
  1216. to the collect list anyway. */
  1217. collect->add_aexpr (std::move (aexpr));
  1218. } /* do */
  1219. }
  1220. while (action_exp && *action_exp++ == ',');
  1221. } /* if */
  1222. else if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand))
  1223. {
  1224. /* We check against nested while-stepping when setting
  1225. breakpoint action, so no way to run into nested
  1226. here. */
  1227. gdb_assert (stepping_list);
  1228. encode_actions_1 (action->body_list_0.get (), tloc, frame_reg,
  1229. frame_offset, stepping_list, NULL);
  1230. }
  1231. else
  1232. error (_("Invalid tracepoint command '%s'"), action->line);
  1233. } /* for */
  1234. }
  1235. /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
  1236. and STEPPING_LIST. */
  1237. void
  1238. encode_actions (struct bp_location *tloc,
  1239. struct collection_list *tracepoint_list,
  1240. struct collection_list *stepping_list)
  1241. {
  1242. int frame_reg;
  1243. LONGEST frame_offset;
  1244. gdbarch_virtual_frame_pointer (tloc->gdbarch,
  1245. tloc->address, &frame_reg, &frame_offset);
  1246. counted_command_line actions = all_tracepoint_actions (tloc->owner);
  1247. encode_actions_1 (actions.get (), tloc, frame_reg, frame_offset,
  1248. tracepoint_list, stepping_list);
  1249. encode_actions_1 (breakpoint_commands (tloc->owner), tloc,
  1250. frame_reg, frame_offset, tracepoint_list, stepping_list);
  1251. tracepoint_list->finish ();
  1252. stepping_list->finish ();
  1253. }
  1254. /* Render all actions into gdb protocol. */
  1255. void
  1256. encode_actions_rsp (struct bp_location *tloc,
  1257. std::vector<std::string> *tdp_actions,
  1258. std::vector<std::string> *stepping_actions)
  1259. {
  1260. struct collection_list tracepoint_list, stepping_list;
  1261. encode_actions (tloc, &tracepoint_list, &stepping_list);
  1262. *tdp_actions = tracepoint_list.stringify ();
  1263. *stepping_actions = stepping_list.stringify ();
  1264. }
  1265. void
  1266. collection_list::add_aexpr (agent_expr_up aexpr)
  1267. {
  1268. m_aexprs.push_back (std::move (aexpr));
  1269. }
  1270. static void
  1271. process_tracepoint_on_disconnect (void)
  1272. {
  1273. int has_pending_p = 0;
  1274. /* Check whether we still have pending tracepoint. If we have, warn the
  1275. user that pending tracepoint will no longer work. */
  1276. for (breakpoint *b : all_tracepoints ())
  1277. {
  1278. if (b->loc == NULL)
  1279. {
  1280. has_pending_p = 1;
  1281. break;
  1282. }
  1283. else
  1284. {
  1285. for (bp_location *loc1 : b->locations ())
  1286. {
  1287. if (loc1->shlib_disabled)
  1288. {
  1289. has_pending_p = 1;
  1290. break;
  1291. }
  1292. }
  1293. if (has_pending_p)
  1294. break;
  1295. }
  1296. }
  1297. if (has_pending_p)
  1298. warning (_("Pending tracepoints will not be resolved while"
  1299. " GDB is disconnected\n"));
  1300. }
  1301. /* Reset local state of tracing. */
  1302. void
  1303. trace_reset_local_state (void)
  1304. {
  1305. set_traceframe_num (-1);
  1306. set_tracepoint_num (-1);
  1307. set_traceframe_context (NULL);
  1308. clear_traceframe_info ();
  1309. }
  1310. void
  1311. start_tracing (const char *notes)
  1312. {
  1313. int any_enabled = 0, num_to_download = 0;
  1314. int ret;
  1315. auto tracepoint_range = all_tracepoints ();
  1316. /* No point in tracing without any tracepoints... */
  1317. if (tracepoint_range.begin () == tracepoint_range.end ())
  1318. error (_("No tracepoints defined, not starting trace"));
  1319. for (breakpoint *b : tracepoint_range)
  1320. {
  1321. if (b->enable_state == bp_enabled)
  1322. any_enabled = 1;
  1323. if ((b->type == bp_fast_tracepoint
  1324. ? may_insert_fast_tracepoints
  1325. : may_insert_tracepoints))
  1326. ++num_to_download;
  1327. else
  1328. warning (_("May not insert %stracepoints, skipping tracepoint %d"),
  1329. (b->type == bp_fast_tracepoint ? "fast " : ""), b->number);
  1330. }
  1331. if (!any_enabled)
  1332. {
  1333. if (target_supports_enable_disable_tracepoint ())
  1334. warning (_("No tracepoints enabled"));
  1335. else
  1336. {
  1337. /* No point in tracing with only disabled tracepoints that
  1338. cannot be re-enabled. */
  1339. error (_("No tracepoints enabled, not starting trace"));
  1340. }
  1341. }
  1342. if (num_to_download <= 0)
  1343. error (_("No tracepoints that may be downloaded, not starting trace"));
  1344. target_trace_init ();
  1345. for (breakpoint *b : tracepoint_range)
  1346. {
  1347. struct tracepoint *t = (struct tracepoint *) b;
  1348. int bp_location_downloaded = 0;
  1349. /* Clear `inserted' flag. */
  1350. for (bp_location *loc : b->locations ())
  1351. loc->inserted = 0;
  1352. if ((b->type == bp_fast_tracepoint
  1353. ? !may_insert_fast_tracepoints
  1354. : !may_insert_tracepoints))
  1355. continue;
  1356. t->number_on_target = 0;
  1357. for (bp_location *loc : b->locations ())
  1358. {
  1359. /* Since tracepoint locations are never duplicated, `inserted'
  1360. flag should be zero. */
  1361. gdb_assert (!loc->inserted);
  1362. target_download_tracepoint (loc);
  1363. loc->inserted = 1;
  1364. bp_location_downloaded = 1;
  1365. }
  1366. t->number_on_target = b->number;
  1367. for (bp_location *loc : b->locations ())
  1368. if (loc->probe.prob != NULL)
  1369. loc->probe.prob->set_semaphore (loc->probe.objfile,
  1370. loc->gdbarch);
  1371. if (bp_location_downloaded)
  1372. gdb::observers::breakpoint_modified.notify (b);
  1373. }
  1374. /* Send down all the trace state variables too. */
  1375. for (const trace_state_variable &tsv : tvariables)
  1376. target_download_trace_state_variable (tsv);
  1377. /* Tell target to treat text-like sections as transparent. */
  1378. target_trace_set_readonly_regions ();
  1379. /* Set some mode flags. */
  1380. target_set_disconnected_tracing (disconnected_tracing);
  1381. target_set_circular_trace_buffer (circular_trace_buffer);
  1382. target_set_trace_buffer_size (trace_buffer_size);
  1383. if (!notes)
  1384. notes = trace_notes.c_str ();
  1385. ret = target_set_trace_notes (trace_user.c_str (), notes, NULL);
  1386. if (!ret && (!trace_user.empty () || notes))
  1387. warning (_("Target does not support trace user/notes, info ignored"));
  1388. /* Now insert traps and begin collecting data. */
  1389. target_trace_start ();
  1390. /* Reset our local state. */
  1391. trace_reset_local_state ();
  1392. current_trace_status()->running = 1;
  1393. }
  1394. /* The tstart command requests the target to start a new trace run.
  1395. The command passes any arguments it has to the target verbatim, as
  1396. an optional "trace note". This is useful as for instance a warning
  1397. to other users if the trace runs disconnected, and you don't want
  1398. anybody else messing with the target. */
  1399. static void
  1400. tstart_command (const char *args, int from_tty)
  1401. {
  1402. dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
  1403. if (current_trace_status ()->running)
  1404. {
  1405. if (from_tty
  1406. && !query (_("A trace is running already. Start a new run? ")))
  1407. error (_("New trace run not started."));
  1408. }
  1409. start_tracing (args);
  1410. }
  1411. /* The tstop command stops the tracing run. The command passes any
  1412. supplied arguments to the target verbatim as a "stop note"; if the
  1413. target supports trace notes, then it will be reported back as part
  1414. of the trace run's status. */
  1415. static void
  1416. tstop_command (const char *args, int from_tty)
  1417. {
  1418. if (!current_trace_status ()->running)
  1419. error (_("Trace is not running."));
  1420. stop_tracing (args);
  1421. }
  1422. void
  1423. stop_tracing (const char *note)
  1424. {
  1425. int ret;
  1426. target_trace_stop ();
  1427. for (breakpoint *t : all_tracepoints ())
  1428. {
  1429. if ((t->type == bp_fast_tracepoint
  1430. ? !may_insert_fast_tracepoints
  1431. : !may_insert_tracepoints))
  1432. continue;
  1433. for (bp_location *loc : t->locations ())
  1434. {
  1435. /* GDB can be totally absent in some disconnected trace scenarios,
  1436. but we don't really care if this semaphore goes out of sync.
  1437. That's why we are decrementing it here, but not taking care
  1438. in other places. */
  1439. if (loc->probe.prob != NULL)
  1440. loc->probe.prob->clear_semaphore (loc->probe.objfile,
  1441. loc->gdbarch);
  1442. }
  1443. }
  1444. if (!note)
  1445. note = trace_stop_notes.c_str ();
  1446. ret = target_set_trace_notes (NULL, NULL, note);
  1447. if (!ret && note)
  1448. warning (_("Target does not support trace notes, note ignored"));
  1449. /* Should change in response to reply? */
  1450. current_trace_status ()->running = 0;
  1451. }
  1452. /* tstatus command */
  1453. static void
  1454. tstatus_command (const char *args, int from_tty)
  1455. {
  1456. struct trace_status *ts = current_trace_status ();
  1457. int status;
  1458. status = target_get_trace_status (ts);
  1459. if (status == -1)
  1460. {
  1461. if (ts->filename != NULL)
  1462. gdb_printf (_("Using a trace file.\n"));
  1463. else
  1464. {
  1465. gdb_printf (_("Trace can not be run on this target.\n"));
  1466. return;
  1467. }
  1468. }
  1469. if (!ts->running_known)
  1470. {
  1471. gdb_printf (_("Run/stop status is unknown.\n"));
  1472. }
  1473. else if (ts->running)
  1474. {
  1475. gdb_printf (_("Trace is running on the target.\n"));
  1476. }
  1477. else
  1478. {
  1479. switch (ts->stop_reason)
  1480. {
  1481. case trace_never_run:
  1482. gdb_printf (_("No trace has been run on the target.\n"));
  1483. break;
  1484. case trace_stop_command:
  1485. if (ts->stop_desc)
  1486. gdb_printf (_("Trace stopped by a tstop command (%s).\n"),
  1487. ts->stop_desc);
  1488. else
  1489. gdb_printf (_("Trace stopped by a tstop command.\n"));
  1490. break;
  1491. case trace_buffer_full:
  1492. gdb_printf (_("Trace stopped because the buffer was full.\n"));
  1493. break;
  1494. case trace_disconnected:
  1495. gdb_printf (_("Trace stopped because of disconnection.\n"));
  1496. break;
  1497. case tracepoint_passcount:
  1498. gdb_printf (_("Trace stopped by tracepoint %d.\n"),
  1499. ts->stopping_tracepoint);
  1500. break;
  1501. case tracepoint_error:
  1502. if (ts->stopping_tracepoint)
  1503. gdb_printf (_("Trace stopped by an "
  1504. "error (%s, tracepoint %d).\n"),
  1505. ts->stop_desc, ts->stopping_tracepoint);
  1506. else
  1507. gdb_printf (_("Trace stopped by an error (%s).\n"),
  1508. ts->stop_desc);
  1509. break;
  1510. case trace_stop_reason_unknown:
  1511. gdb_printf (_("Trace stopped for an unknown reason.\n"));
  1512. break;
  1513. default:
  1514. gdb_printf (_("Trace stopped for some other reason (%d).\n"),
  1515. ts->stop_reason);
  1516. break;
  1517. }
  1518. }
  1519. if (ts->traceframes_created >= 0
  1520. && ts->traceframe_count != ts->traceframes_created)
  1521. {
  1522. gdb_printf (_("Buffer contains %d trace "
  1523. "frames (of %d created total).\n"),
  1524. ts->traceframe_count, ts->traceframes_created);
  1525. }
  1526. else if (ts->traceframe_count >= 0)
  1527. {
  1528. gdb_printf (_("Collected %d trace frames.\n"),
  1529. ts->traceframe_count);
  1530. }
  1531. if (ts->buffer_free >= 0)
  1532. {
  1533. if (ts->buffer_size >= 0)
  1534. {
  1535. gdb_printf (_("Trace buffer has %d bytes of %d bytes free"),
  1536. ts->buffer_free, ts->buffer_size);
  1537. if (ts->buffer_size > 0)
  1538. gdb_printf (_(" (%d%% full)"),
  1539. ((int) ((((long long) (ts->buffer_size
  1540. - ts->buffer_free)) * 100)
  1541. / ts->buffer_size)));
  1542. gdb_printf (_(".\n"));
  1543. }
  1544. else
  1545. gdb_printf (_("Trace buffer has %d bytes free.\n"),
  1546. ts->buffer_free);
  1547. }
  1548. if (ts->disconnected_tracing)
  1549. gdb_printf (_("Trace will continue if GDB disconnects.\n"));
  1550. else
  1551. gdb_printf (_("Trace will stop if GDB disconnects.\n"));
  1552. if (ts->circular_buffer)
  1553. gdb_printf (_("Trace buffer is circular.\n"));
  1554. if (ts->user_name && strlen (ts->user_name) > 0)
  1555. gdb_printf (_("Trace user is %s.\n"), ts->user_name);
  1556. if (ts->notes && strlen (ts->notes) > 0)
  1557. gdb_printf (_("Trace notes: %s.\n"), ts->notes);
  1558. /* Now report on what we're doing with tfind. */
  1559. if (traceframe_number >= 0)
  1560. gdb_printf (_("Looking at trace frame %d, tracepoint %d.\n"),
  1561. traceframe_number, tracepoint_number);
  1562. else
  1563. gdb_printf (_("Not looking at any trace frame.\n"));
  1564. /* Report start/stop times if supplied. */
  1565. if (ts->start_time)
  1566. {
  1567. if (ts->stop_time)
  1568. {
  1569. LONGEST run_time = ts->stop_time - ts->start_time;
  1570. /* Reporting a run time is more readable than two long numbers. */
  1571. gdb_printf (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
  1572. (long int) (ts->start_time / 1000000),
  1573. (long int) (ts->start_time % 1000000),
  1574. (long int) (run_time / 1000000),
  1575. (long int) (run_time % 1000000));
  1576. }
  1577. else
  1578. gdb_printf (_("Trace started at %ld.%06ld secs.\n"),
  1579. (long int) (ts->start_time / 1000000),
  1580. (long int) (ts->start_time % 1000000));
  1581. }
  1582. else if (ts->stop_time)
  1583. gdb_printf (_("Trace stopped at %ld.%06ld secs.\n"),
  1584. (long int) (ts->stop_time / 1000000),
  1585. (long int) (ts->stop_time % 1000000));
  1586. /* Now report any per-tracepoint status available. */
  1587. for (breakpoint *t : all_tracepoints ())
  1588. target_get_tracepoint_status (t, NULL);
  1589. }
  1590. /* Report the trace status to uiout, in a way suitable for MI, and not
  1591. suitable for CLI. If ON_STOP is true, suppress a few fields that
  1592. are not meaningful in the -trace-stop response.
  1593. The implementation is essentially parallel to trace_status_command, but
  1594. merging them will result in unreadable code. */
  1595. void
  1596. trace_status_mi (int on_stop)
  1597. {
  1598. struct ui_out *uiout = current_uiout;
  1599. struct trace_status *ts = current_trace_status ();
  1600. int status;
  1601. status = target_get_trace_status (ts);
  1602. if (status == -1 && ts->filename == NULL)
  1603. {
  1604. uiout->field_string ("supported", "0");
  1605. return;
  1606. }
  1607. if (ts->filename != NULL)
  1608. uiout->field_string ("supported", "file");
  1609. else if (!on_stop)
  1610. uiout->field_string ("supported", "1");
  1611. if (ts->filename != NULL)
  1612. uiout->field_string ("trace-file", ts->filename);
  1613. gdb_assert (ts->running_known);
  1614. if (ts->running)
  1615. {
  1616. uiout->field_string ("running", "1");
  1617. /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
  1618. Given that the frontend gets the status either on -trace-stop, or from
  1619. -trace-status after re-connection, it does not seem like this
  1620. information is necessary for anything. It is not necessary for either
  1621. figuring the vital state of the target nor for navigation of trace
  1622. frames. If the frontend wants to show the current state is some
  1623. configure dialog, it can request the value when such dialog is
  1624. invoked by the user. */
  1625. }
  1626. else
  1627. {
  1628. const char *stop_reason = NULL;
  1629. int stopping_tracepoint = -1;
  1630. if (!on_stop)
  1631. uiout->field_string ("running", "0");
  1632. if (ts->stop_reason != trace_stop_reason_unknown)
  1633. {
  1634. switch (ts->stop_reason)
  1635. {
  1636. case trace_stop_command:
  1637. stop_reason = "request";
  1638. break;
  1639. case trace_buffer_full:
  1640. stop_reason = "overflow";
  1641. break;
  1642. case trace_disconnected:
  1643. stop_reason = "disconnection";
  1644. break;
  1645. case tracepoint_passcount:
  1646. stop_reason = "passcount";
  1647. stopping_tracepoint = ts->stopping_tracepoint;
  1648. break;
  1649. case tracepoint_error:
  1650. stop_reason = "error";
  1651. stopping_tracepoint = ts->stopping_tracepoint;
  1652. break;
  1653. }
  1654. if (stop_reason)
  1655. {
  1656. uiout->field_string ("stop-reason", stop_reason);
  1657. if (stopping_tracepoint != -1)
  1658. uiout->field_signed ("stopping-tracepoint",
  1659. stopping_tracepoint);
  1660. if (ts->stop_reason == tracepoint_error)
  1661. uiout->field_string ("error-description",
  1662. ts->stop_desc);
  1663. }
  1664. }
  1665. }
  1666. if (ts->traceframe_count != -1)
  1667. uiout->field_signed ("frames", ts->traceframe_count);
  1668. if (ts->traceframes_created != -1)
  1669. uiout->field_signed ("frames-created", ts->traceframes_created);
  1670. if (ts->buffer_size != -1)
  1671. uiout->field_signed ("buffer-size", ts->buffer_size);
  1672. if (ts->buffer_free != -1)
  1673. uiout->field_signed ("buffer-free", ts->buffer_free);
  1674. uiout->field_signed ("disconnected", ts->disconnected_tracing);
  1675. uiout->field_signed ("circular", ts->circular_buffer);
  1676. uiout->field_string ("user-name", ts->user_name);
  1677. uiout->field_string ("notes", ts->notes);
  1678. {
  1679. char buf[100];
  1680. xsnprintf (buf, sizeof buf, "%ld.%06ld",
  1681. (long int) (ts->start_time / 1000000),
  1682. (long int) (ts->start_time % 1000000));
  1683. uiout->field_string ("start-time", buf);
  1684. xsnprintf (buf, sizeof buf, "%ld.%06ld",
  1685. (long int) (ts->stop_time / 1000000),
  1686. (long int) (ts->stop_time % 1000000));
  1687. uiout->field_string ("stop-time", buf);
  1688. }
  1689. }
  1690. /* Check if a trace run is ongoing. If so, and FROM_TTY, query the
  1691. user if she really wants to detach. */
  1692. void
  1693. query_if_trace_running (int from_tty)
  1694. {
  1695. if (!from_tty)
  1696. return;
  1697. /* It can happen that the target that was tracing went away on its
  1698. own, and we didn't notice. Get a status update, and if the
  1699. current target doesn't even do tracing, then assume it's not
  1700. running anymore. */
  1701. if (target_get_trace_status (current_trace_status ()) < 0)
  1702. current_trace_status ()->running = 0;
  1703. /* If running interactively, give the user the option to cancel and
  1704. then decide what to do differently with the run. Scripts are
  1705. just going to disconnect and let the target deal with it,
  1706. according to how it's been instructed previously via
  1707. disconnected-tracing. */
  1708. if (current_trace_status ()->running)
  1709. {
  1710. process_tracepoint_on_disconnect ();
  1711. if (current_trace_status ()->disconnected_tracing)
  1712. {
  1713. if (!query (_("Trace is running and will "
  1714. "continue after detach; detach anyway? ")))
  1715. error (_("Not confirmed."));
  1716. }
  1717. else
  1718. {
  1719. if (!query (_("Trace is running but will "
  1720. "stop on detach; detach anyway? ")))
  1721. error (_("Not confirmed."));
  1722. }
  1723. }
  1724. }
  1725. /* This function handles the details of what to do about an ongoing
  1726. tracing run if the user has asked to detach or otherwise disconnect
  1727. from the target. */
  1728. void
  1729. disconnect_tracing (void)
  1730. {
  1731. /* Also we want to be out of tfind mode, otherwise things can get
  1732. confusing upon reconnection. Just use these calls instead of
  1733. full tfind_1 behavior because we're in the middle of detaching,
  1734. and there's no point to updating current stack frame etc. */
  1735. trace_reset_local_state ();
  1736. }
  1737. /* Worker function for the various flavors of the tfind command. */
  1738. void
  1739. tfind_1 (enum trace_find_type type, int num,
  1740. CORE_ADDR addr1, CORE_ADDR addr2,
  1741. int from_tty)
  1742. {
  1743. int target_frameno = -1, target_tracept = -1;
  1744. struct frame_id old_frame_id = null_frame_id;
  1745. struct tracepoint *tp;
  1746. struct ui_out *uiout = current_uiout;
  1747. /* Only try to get the current stack frame if we have a chance of
  1748. succeeding. In particular, if we're trying to get a first trace
  1749. frame while all threads are running, it's not going to succeed,
  1750. so leave it with a default value and let the frame comparison
  1751. below (correctly) decide to print out the source location of the
  1752. trace frame. */
  1753. if (!(type == tfind_number && num == -1)
  1754. && (has_stack_frames () || traceframe_number >= 0))
  1755. old_frame_id = get_frame_id (get_current_frame ());
  1756. target_frameno = target_trace_find (type, num, addr1, addr2,
  1757. &target_tracept);
  1758. if (type == tfind_number
  1759. && num == -1
  1760. && target_frameno == -1)
  1761. {
  1762. /* We told the target to get out of tfind mode, and it did. */
  1763. }
  1764. else if (target_frameno == -1)
  1765. {
  1766. /* A request for a non-existent trace frame has failed.
  1767. Our response will be different, depending on FROM_TTY:
  1768. If FROM_TTY is true, meaning that this command was
  1769. typed interactively by the user, then give an error
  1770. and DO NOT change the state of traceframe_number etc.
  1771. However if FROM_TTY is false, meaning that we're either
  1772. in a script, a loop, or a user-defined command, then
  1773. DON'T give an error, but DO change the state of
  1774. traceframe_number etc. to invalid.
  1775. The rationale is that if you typed the command, you
  1776. might just have committed a typo or something, and you'd
  1777. like to NOT lose your current debugging state. However
  1778. if you're in a user-defined command or especially in a
  1779. loop, then you need a way to detect that the command
  1780. failed WITHOUT aborting. This allows you to write
  1781. scripts that search thru the trace buffer until the end,
  1782. and then continue on to do something else. */
  1783. if (from_tty)
  1784. error (_("Target failed to find requested trace frame."));
  1785. else
  1786. {
  1787. if (info_verbose)
  1788. gdb_printf ("End of trace buffer.\n");
  1789. #if 0 /* dubious now? */
  1790. /* The following will not recurse, since it's
  1791. special-cased. */
  1792. tfind_command ("-1", from_tty);
  1793. #endif
  1794. }
  1795. }
  1796. tp = get_tracepoint_by_number_on_target (target_tracept);
  1797. reinit_frame_cache ();
  1798. target_dcache_invalidate ();
  1799. set_tracepoint_num (tp ? tp->number : target_tracept);
  1800. if (target_frameno != get_traceframe_number ())
  1801. gdb::observers::traceframe_changed.notify (target_frameno, tracepoint_number);
  1802. set_current_traceframe (target_frameno);
  1803. if (target_frameno == -1)
  1804. set_traceframe_context (NULL);
  1805. else
  1806. set_traceframe_context (get_current_frame ());
  1807. if (traceframe_number >= 0)
  1808. {
  1809. /* Use different branches for MI and CLI to make CLI messages
  1810. i18n-eable. */
  1811. if (uiout->is_mi_like_p ())
  1812. {
  1813. uiout->field_string ("found", "1");
  1814. uiout->field_signed ("tracepoint", tracepoint_number);
  1815. uiout->field_signed ("traceframe", traceframe_number);
  1816. }
  1817. else
  1818. {
  1819. gdb_printf (_("Found trace frame %d, tracepoint %d\n"),
  1820. traceframe_number, tracepoint_number);
  1821. }
  1822. }
  1823. else
  1824. {
  1825. if (uiout->is_mi_like_p ())
  1826. uiout->field_string ("found", "0");
  1827. else if (type == tfind_number && num == -1)
  1828. gdb_printf (_("No longer looking at any trace frame\n"));
  1829. else /* This case may never occur, check. */
  1830. gdb_printf (_("No trace frame found\n"));
  1831. }
  1832. /* If we're in nonstop mode and getting out of looking at trace
  1833. frames, there won't be any current frame to go back to and
  1834. display. */
  1835. if (from_tty
  1836. && (has_stack_frames () || traceframe_number >= 0))
  1837. {
  1838. enum print_what print_what;
  1839. /* NOTE: in imitation of the step command, try to determine
  1840. whether we have made a transition from one function to
  1841. another. If so, we'll print the "stack frame" (ie. the new
  1842. function and it's arguments) -- otherwise we'll just show the
  1843. new source line. */
  1844. if (frame_id_eq (old_frame_id,
  1845. get_frame_id (get_current_frame ())))
  1846. print_what = SRC_LINE;
  1847. else
  1848. print_what = SRC_AND_LOC;
  1849. print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
  1850. do_displays ();
  1851. }
  1852. }
  1853. /* Error on looking at traceframes while trace is running. */
  1854. void
  1855. check_trace_running (struct trace_status *status)
  1856. {
  1857. if (status->running && status->filename == NULL)
  1858. error (_("May not look at trace frames while trace is running."));
  1859. }
  1860. /* trace_find_command takes a trace frame number n,
  1861. sends "QTFrame:<n>" to the target,
  1862. and accepts a reply that may contain several optional pieces
  1863. of information: a frame number, a tracepoint number, and an
  1864. indication of whether this is a trap frame or a stepping frame.
  1865. The minimal response is just "OK" (which indicates that the
  1866. target does not give us a frame number or a tracepoint number).
  1867. Instead of that, the target may send us a string containing
  1868. any combination of:
  1869. F<hexnum> (gives the selected frame number)
  1870. T<hexnum> (gives the selected tracepoint number)
  1871. */
  1872. /* tfind command */
  1873. static void
  1874. tfind_command_1 (const char *args, int from_tty)
  1875. { /* This should only be called with a numeric argument. */
  1876. int frameno = -1;
  1877. check_trace_running (current_trace_status ());
  1878. if (args == 0 || *args == 0)
  1879. { /* TFIND with no args means find NEXT trace frame. */
  1880. if (traceframe_number == -1)
  1881. frameno = 0; /* "next" is first one. */
  1882. else
  1883. frameno = traceframe_number + 1;
  1884. }
  1885. else if (0 == strcmp (args, "-"))
  1886. {
  1887. if (traceframe_number == -1)
  1888. error (_("not debugging trace buffer"));
  1889. else if (from_tty && traceframe_number == 0)
  1890. error (_("already at start of trace buffer"));
  1891. frameno = traceframe_number - 1;
  1892. }
  1893. /* A hack to work around eval's need for fp to have been collected. */
  1894. else if (0 == strcmp (args, "-1"))
  1895. frameno = -1;
  1896. else
  1897. frameno = parse_and_eval_long (args);
  1898. if (frameno < -1)
  1899. error (_("invalid input (%d is less than zero)"), frameno);
  1900. tfind_1 (tfind_number, frameno, 0, 0, from_tty);
  1901. }
  1902. static void
  1903. tfind_command (const char *args, int from_tty)
  1904. {
  1905. tfind_command_1 (args, from_tty);
  1906. }
  1907. /* tfind end */
  1908. static void
  1909. tfind_end_command (const char *args, int from_tty)
  1910. {
  1911. tfind_command_1 ("-1", from_tty);
  1912. }
  1913. /* tfind start */
  1914. static void
  1915. tfind_start_command (const char *args, int from_tty)
  1916. {
  1917. tfind_command_1 ("0", from_tty);
  1918. }
  1919. /* tfind pc command */
  1920. static void
  1921. tfind_pc_command (const char *args, int from_tty)
  1922. {
  1923. CORE_ADDR pc;
  1924. check_trace_running (current_trace_status ());
  1925. if (args == 0 || *args == 0)
  1926. pc = regcache_read_pc (get_current_regcache ());
  1927. else
  1928. pc = parse_and_eval_address (args);
  1929. tfind_1 (tfind_pc, 0, pc, 0, from_tty);
  1930. }
  1931. /* tfind tracepoint command */
  1932. static void
  1933. tfind_tracepoint_command (const char *args, int from_tty)
  1934. {
  1935. int tdp;
  1936. struct tracepoint *tp;
  1937. check_trace_running (current_trace_status ());
  1938. if (args == 0 || *args == 0)
  1939. {
  1940. if (tracepoint_number == -1)
  1941. error (_("No current tracepoint -- please supply an argument."));
  1942. else
  1943. tdp = tracepoint_number; /* Default is current TDP. */
  1944. }
  1945. else
  1946. tdp = parse_and_eval_long (args);
  1947. /* If we have the tracepoint on hand, use the number that the
  1948. target knows about (which may be different if we disconnected
  1949. and reconnected). */
  1950. tp = get_tracepoint (tdp);
  1951. if (tp)
  1952. tdp = tp->number_on_target;
  1953. tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
  1954. }
  1955. /* TFIND LINE command:
  1956. This command will take a sourceline for argument, just like BREAK
  1957. or TRACE (ie. anything that "decode_line_1" can handle).
  1958. With no argument, this command will find the next trace frame
  1959. corresponding to a source line OTHER THAN THE CURRENT ONE. */
  1960. static void
  1961. tfind_line_command (const char *args, int from_tty)
  1962. {
  1963. check_trace_running (current_trace_status ());
  1964. symtab_and_line sal;
  1965. if (args == 0 || *args == 0)
  1966. {
  1967. sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
  1968. }
  1969. else
  1970. {
  1971. std::vector<symtab_and_line> sals
  1972. = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
  1973. sal = sals[0];
  1974. }
  1975. if (sal.symtab == 0)
  1976. error (_("No line number information available."));
  1977. CORE_ADDR start_pc, end_pc;
  1978. if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
  1979. {
  1980. if (start_pc == end_pc)
  1981. {
  1982. gdb_printf ("Line %d of \"%s\"",
  1983. sal.line,
  1984. symtab_to_filename_for_display (sal.symtab));
  1985. gdb_stdout->wrap_here (2);
  1986. gdb_printf (" is at address ");
  1987. print_address (get_current_arch (), start_pc, gdb_stdout);
  1988. gdb_stdout->wrap_here (2);
  1989. gdb_printf (" but contains no code.\n");
  1990. sal = find_pc_line (start_pc, 0);
  1991. if (sal.line > 0
  1992. && find_line_pc_range (sal, &start_pc, &end_pc)
  1993. && start_pc != end_pc)
  1994. gdb_printf ("Attempting to find line %d instead.\n",
  1995. sal.line);
  1996. else
  1997. error (_("Cannot find a good line."));
  1998. }
  1999. }
  2000. else
  2001. {
  2002. /* Is there any case in which we get here, and have an address
  2003. which the user would want to see? If we have debugging
  2004. symbols and no line numbers? */
  2005. error (_("Line number %d is out of range for \"%s\"."),
  2006. sal.line, symtab_to_filename_for_display (sal.symtab));
  2007. }
  2008. /* Find within range of stated line. */
  2009. if (args && *args)
  2010. tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
  2011. else
  2012. tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
  2013. }
  2014. /* tfind range command */
  2015. static void
  2016. tfind_range_command (const char *args, int from_tty)
  2017. {
  2018. static CORE_ADDR start, stop;
  2019. const char *tmp;
  2020. check_trace_running (current_trace_status ());
  2021. if (args == 0 || *args == 0)
  2022. { /* XXX FIXME: what should default behavior be? */
  2023. gdb_printf ("Usage: tfind range STARTADDR, ENDADDR\n");
  2024. return;
  2025. }
  2026. if (0 != (tmp = strchr (args, ',')))
  2027. {
  2028. std::string start_addr (args, tmp);
  2029. ++tmp;
  2030. tmp = skip_spaces (tmp);
  2031. start = parse_and_eval_address (start_addr.c_str ());
  2032. stop = parse_and_eval_address (tmp);
  2033. }
  2034. else
  2035. { /* No explicit end address? */
  2036. start = parse_and_eval_address (args);
  2037. stop = start + 1; /* ??? */
  2038. }
  2039. tfind_1 (tfind_range, 0, start, stop, from_tty);
  2040. }
  2041. /* tfind outside command */
  2042. static void
  2043. tfind_outside_command (const char *args, int from_tty)
  2044. {
  2045. CORE_ADDR start, stop;
  2046. const char *tmp;
  2047. if (current_trace_status ()->running
  2048. && current_trace_status ()->filename == NULL)
  2049. error (_("May not look at trace frames while trace is running."));
  2050. if (args == 0 || *args == 0)
  2051. { /* XXX FIXME: what should default behavior be? */
  2052. gdb_printf ("Usage: tfind outside STARTADDR, ENDADDR\n");
  2053. return;
  2054. }
  2055. if (0 != (tmp = strchr (args, ',')))
  2056. {
  2057. std::string start_addr (args, tmp);
  2058. ++tmp;
  2059. tmp = skip_spaces (tmp);
  2060. start = parse_and_eval_address (start_addr.c_str ());
  2061. stop = parse_and_eval_address (tmp);
  2062. }
  2063. else
  2064. { /* No explicit end address? */
  2065. start = parse_and_eval_address (args);
  2066. stop = start + 1; /* ??? */
  2067. }
  2068. tfind_1 (tfind_outside, 0, start, stop, from_tty);
  2069. }
  2070. /* info scope command: list the locals for a scope. */
  2071. static void
  2072. info_scope_command (const char *args_in, int from_tty)
  2073. {
  2074. struct symbol *sym;
  2075. struct bound_minimal_symbol msym;
  2076. const struct block *block;
  2077. const char *symname;
  2078. const char *save_args = args_in;
  2079. struct block_iterator iter;
  2080. int j, count = 0;
  2081. struct gdbarch *gdbarch;
  2082. int regno;
  2083. const char *args = args_in;
  2084. if (args == 0 || *args == 0)
  2085. error (_("requires an argument (function, "
  2086. "line or *addr) to define a scope"));
  2087. event_location_up location = string_to_event_location (&args,
  2088. current_language);
  2089. std::vector<symtab_and_line> sals
  2090. = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
  2091. NULL, NULL, 0);
  2092. if (sals.empty ())
  2093. {
  2094. /* Presumably decode_line_1 has already warned. */
  2095. return;
  2096. }
  2097. /* Resolve line numbers to PC. */
  2098. resolve_sal_pc (&sals[0]);
  2099. block = block_for_pc (sals[0].pc);
  2100. while (block != 0)
  2101. {
  2102. QUIT; /* Allow user to bail out with ^C. */
  2103. ALL_BLOCK_SYMBOLS (block, iter, sym)
  2104. {
  2105. QUIT; /* Allow user to bail out with ^C. */
  2106. if (count == 0)
  2107. gdb_printf ("Scope for %s:\n", save_args);
  2108. count++;
  2109. symname = sym->print_name ();
  2110. if (symname == NULL || *symname == '\0')
  2111. continue; /* Probably botched, certainly useless. */
  2112. gdbarch = symbol_arch (sym);
  2113. gdb_printf ("Symbol %s is ", symname);
  2114. if (SYMBOL_COMPUTED_OPS (sym) != NULL)
  2115. SYMBOL_COMPUTED_OPS (sym)->describe_location (sym,
  2116. BLOCK_ENTRY_PC (block),
  2117. gdb_stdout);
  2118. else
  2119. {
  2120. switch (sym->aclass ())
  2121. {
  2122. default:
  2123. case LOC_UNDEF: /* Messed up symbol? */
  2124. gdb_printf ("a bogus symbol, class %d.\n",
  2125. sym->aclass ());
  2126. count--; /* Don't count this one. */
  2127. continue;
  2128. case LOC_CONST:
  2129. gdb_printf ("a constant with value %s (%s)",
  2130. plongest (SYMBOL_VALUE (sym)),
  2131. hex_string (SYMBOL_VALUE (sym)));
  2132. break;
  2133. case LOC_CONST_BYTES:
  2134. gdb_printf ("constant bytes: ");
  2135. if (sym->type ())
  2136. for (j = 0; j < TYPE_LENGTH (sym->type ()); j++)
  2137. gdb_printf (" %02x",
  2138. (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
  2139. break;
  2140. case LOC_STATIC:
  2141. gdb_printf ("in static storage at address ");
  2142. gdb_printf ("%s", paddress (gdbarch,
  2143. SYMBOL_VALUE_ADDRESS (sym)));
  2144. break;
  2145. case LOC_REGISTER:
  2146. /* GDBARCH is the architecture associated with the objfile
  2147. the symbol is defined in; the target architecture may be
  2148. different, and may provide additional registers. However,
  2149. we do not know the target architecture at this point.
  2150. We assume the objfile architecture will contain all the
  2151. standard registers that occur in debug info in that
  2152. objfile. */
  2153. regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
  2154. gdbarch);
  2155. if (sym->is_argument ())
  2156. gdb_printf ("an argument in register $%s",
  2157. gdbarch_register_name (gdbarch, regno));
  2158. else
  2159. gdb_printf ("a local variable in register $%s",
  2160. gdbarch_register_name (gdbarch, regno));
  2161. break;
  2162. case LOC_ARG:
  2163. gdb_printf ("an argument at stack/frame offset %s",
  2164. plongest (SYMBOL_VALUE (sym)));
  2165. break;
  2166. case LOC_LOCAL:
  2167. gdb_printf ("a local variable at frame offset %s",
  2168. plongest (SYMBOL_VALUE (sym)));
  2169. break;
  2170. case LOC_REF_ARG:
  2171. gdb_printf ("a reference argument at offset %s",
  2172. plongest (SYMBOL_VALUE (sym)));
  2173. break;
  2174. case LOC_REGPARM_ADDR:
  2175. /* Note comment at LOC_REGISTER. */
  2176. regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
  2177. gdbarch);
  2178. gdb_printf ("the address of an argument, in register $%s",
  2179. gdbarch_register_name (gdbarch, regno));
  2180. break;
  2181. case LOC_TYPEDEF:
  2182. gdb_printf ("a typedef.\n");
  2183. continue;
  2184. case LOC_LABEL:
  2185. gdb_printf ("a label at address ");
  2186. gdb_printf ("%s", paddress (gdbarch,
  2187. SYMBOL_VALUE_ADDRESS (sym)));
  2188. break;
  2189. case LOC_BLOCK:
  2190. gdb_printf ("a function at address ");
  2191. gdb_printf ("%s",
  2192. paddress (gdbarch, BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym))));
  2193. break;
  2194. case LOC_UNRESOLVED:
  2195. msym = lookup_minimal_symbol (sym->linkage_name (),
  2196. NULL, NULL);
  2197. if (msym.minsym == NULL)
  2198. gdb_printf ("Unresolved Static");
  2199. else
  2200. {
  2201. gdb_printf ("static storage at address ");
  2202. gdb_printf ("%s",
  2203. paddress (gdbarch,
  2204. BMSYMBOL_VALUE_ADDRESS (msym)));
  2205. }
  2206. break;
  2207. case LOC_OPTIMIZED_OUT:
  2208. gdb_printf ("optimized out.\n");
  2209. continue;
  2210. case LOC_COMPUTED:
  2211. gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
  2212. }
  2213. }
  2214. if (sym->type ())
  2215. {
  2216. struct type *t = check_typedef (sym->type ());
  2217. gdb_printf (", length %s.\n", pulongest (TYPE_LENGTH (t)));
  2218. }
  2219. }
  2220. if (BLOCK_FUNCTION (block))
  2221. break;
  2222. else
  2223. block = BLOCK_SUPERBLOCK (block);
  2224. }
  2225. if (count <= 0)
  2226. gdb_printf ("Scope for %s contains no locals or arguments.\n",
  2227. save_args);
  2228. }
  2229. /* Helper for trace_dump_command. Dump the action list starting at
  2230. ACTION. STEPPING_ACTIONS is true if we're iterating over the
  2231. actions of the body of a while-stepping action. STEPPING_FRAME is
  2232. set if the current traceframe was determined to be a while-stepping
  2233. traceframe. */
  2234. static void
  2235. trace_dump_actions (struct command_line *action,
  2236. int stepping_actions, int stepping_frame,
  2237. int from_tty)
  2238. {
  2239. const char *action_exp, *next_comma;
  2240. for (; action != NULL; action = action->next)
  2241. {
  2242. struct cmd_list_element *cmd;
  2243. QUIT; /* Allow user to bail out with ^C. */
  2244. action_exp = action->line;
  2245. action_exp = skip_spaces (action_exp);
  2246. /* The collection actions to be done while stepping are
  2247. bracketed by the commands "while-stepping" and "end". */
  2248. if (*action_exp == '#') /* comment line */
  2249. continue;
  2250. cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1);
  2251. if (cmd == 0)
  2252. error (_("Bad action list item: %s"), action_exp);
  2253. if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand))
  2254. {
  2255. gdb_assert (action->body_list_1 == nullptr);
  2256. trace_dump_actions (action->body_list_0.get (),
  2257. 1, stepping_frame, from_tty);
  2258. }
  2259. else if (cmd_simple_func_eq (cmd, collect_pseudocommand))
  2260. {
  2261. /* Display the collected data.
  2262. For the trap frame, display only what was collected at
  2263. the trap. Likewise for stepping frames, display only
  2264. what was collected while stepping. This means that the
  2265. two boolean variables, STEPPING_FRAME and
  2266. STEPPING_ACTIONS should be equal. */
  2267. if (stepping_frame == stepping_actions)
  2268. {
  2269. int trace_string = 0;
  2270. if (*action_exp == '/')
  2271. action_exp = decode_agent_options (action_exp, &trace_string);
  2272. do
  2273. { /* Repeat over a comma-separated list. */
  2274. QUIT; /* Allow user to bail out with ^C. */
  2275. if (*action_exp == ',')
  2276. action_exp++;
  2277. action_exp = skip_spaces (action_exp);
  2278. next_comma = strchr (action_exp, ',');
  2279. if (0 == strncasecmp (action_exp, "$reg", 4))
  2280. registers_info (NULL, from_tty);
  2281. else if (0 == strncasecmp (action_exp, "$_ret", 5))
  2282. ;
  2283. else if (0 == strncasecmp (action_exp, "$loc", 4))
  2284. info_locals_command (NULL, from_tty);
  2285. else if (0 == strncasecmp (action_exp, "$arg", 4))
  2286. info_args_command (NULL, from_tty);
  2287. else
  2288. { /* variable */
  2289. std::string contents;
  2290. const char *exp = action_exp;
  2291. if (next_comma != NULL)
  2292. {
  2293. size_t len = next_comma - action_exp;
  2294. contents = std::string (action_exp, len);
  2295. exp = contents.c_str ();
  2296. }
  2297. gdb_printf ("%s = ", exp);
  2298. output_command (exp, from_tty);
  2299. gdb_printf ("\n");
  2300. }
  2301. action_exp = next_comma;
  2302. }
  2303. while (action_exp && *action_exp == ',');
  2304. }
  2305. }
  2306. }
  2307. }
  2308. /* Return bp_location of the tracepoint associated with the current
  2309. traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
  2310. is a stepping traceframe. */
  2311. struct bp_location *
  2312. get_traceframe_location (int *stepping_frame_p)
  2313. {
  2314. struct tracepoint *t;
  2315. struct regcache *regcache;
  2316. if (tracepoint_number == -1)
  2317. error (_("No current trace frame."));
  2318. t = get_tracepoint (tracepoint_number);
  2319. if (t == NULL)
  2320. error (_("No known tracepoint matches 'current' tracepoint #%d."),
  2321. tracepoint_number);
  2322. /* The current frame is a trap frame if the frame PC is equal to the
  2323. tracepoint PC. If not, then the current frame was collected
  2324. during single-stepping. */
  2325. regcache = get_current_regcache ();
  2326. /* If the traceframe's address matches any of the tracepoint's
  2327. locations, assume it is a direct hit rather than a while-stepping
  2328. frame. (FIXME this is not reliable, should record each frame's
  2329. type.) */
  2330. for (bp_location *tloc : t->locations ())
  2331. if (tloc->address == regcache_read_pc (regcache))
  2332. {
  2333. *stepping_frame_p = 0;
  2334. return tloc;
  2335. }
  2336. /* If this is a stepping frame, we don't know which location
  2337. triggered. The first is as good (or bad) a guess as any... */
  2338. *stepping_frame_p = 1;
  2339. return t->loc;
  2340. }
  2341. /* Return the default collect actions of a tracepoint T. */
  2342. static counted_command_line
  2343. all_tracepoint_actions (struct breakpoint *t)
  2344. {
  2345. counted_command_line actions (nullptr, command_lines_deleter ());
  2346. /* If there are default expressions to collect, make up a collect
  2347. action and prepend to the action list to encode. Note that since
  2348. validation is per-tracepoint (local var "xyz" might be valid for
  2349. one tracepoint and not another, etc), we make up the action on
  2350. the fly, and don't cache it. */
  2351. if (!default_collect.empty ())
  2352. {
  2353. gdb::unique_xmalloc_ptr<char> default_collect_line
  2354. = xstrprintf ("collect %s", default_collect.c_str ());
  2355. validate_actionline (default_collect_line.get (), t);
  2356. actions.reset (new struct command_line (simple_control,
  2357. default_collect_line.release ()),
  2358. command_lines_deleter ());
  2359. }
  2360. return actions;
  2361. }
  2362. /* The tdump command. */
  2363. static void
  2364. tdump_command (const char *args, int from_tty)
  2365. {
  2366. int stepping_frame = 0;
  2367. struct bp_location *loc;
  2368. /* This throws an error is not inspecting a trace frame. */
  2369. loc = get_traceframe_location (&stepping_frame);
  2370. gdb_printf ("Data collected at tracepoint %d, trace frame %d:\n",
  2371. tracepoint_number, traceframe_number);
  2372. /* This command only makes sense for the current frame, not the
  2373. selected frame. */
  2374. scoped_restore_current_thread restore_thread;
  2375. select_frame (get_current_frame ());
  2376. counted_command_line actions = all_tracepoint_actions (loc->owner);
  2377. trace_dump_actions (actions.get (), 0, stepping_frame, from_tty);
  2378. trace_dump_actions (breakpoint_commands (loc->owner), 0, stepping_frame,
  2379. from_tty);
  2380. }
  2381. /* Encode a piece of a tracepoint's source-level definition in a form
  2382. that is suitable for both protocol and saving in files. */
  2383. /* This version does not do multiple encodes for long strings; it should
  2384. return an offset to the next piece to encode. FIXME */
  2385. int
  2386. encode_source_string (int tpnum, ULONGEST addr,
  2387. const char *srctype, const char *src,
  2388. char *buf, int buf_size)
  2389. {
  2390. if (80 + strlen (srctype) > buf_size)
  2391. error (_("Buffer too small for source encoding"));
  2392. sprintf (buf, "%x:%s:%s:%x:%x:",
  2393. tpnum, phex_nz (addr, sizeof (addr)),
  2394. srctype, 0, (int) strlen (src));
  2395. if (strlen (buf) + strlen (src) * 2 >= buf_size)
  2396. error (_("Source string too long for buffer"));
  2397. bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
  2398. return -1;
  2399. }
  2400. /* Tell the target what to do with an ongoing tracing run if GDB
  2401. disconnects for some reason. */
  2402. static void
  2403. set_disconnected_tracing (const char *args, int from_tty,
  2404. struct cmd_list_element *c)
  2405. {
  2406. target_set_disconnected_tracing (disconnected_tracing);
  2407. }
  2408. static void
  2409. set_circular_trace_buffer (const char *args, int from_tty,
  2410. struct cmd_list_element *c)
  2411. {
  2412. target_set_circular_trace_buffer (circular_trace_buffer);
  2413. }
  2414. static void
  2415. set_trace_buffer_size (const char *args, int from_tty,
  2416. struct cmd_list_element *c)
  2417. {
  2418. target_set_trace_buffer_size (trace_buffer_size);
  2419. }
  2420. static void
  2421. set_trace_user (const char *args, int from_tty,
  2422. struct cmd_list_element *c)
  2423. {
  2424. int ret;
  2425. ret = target_set_trace_notes (trace_user.c_str (), NULL, NULL);
  2426. if (!ret)
  2427. warning (_("Target does not support trace notes, user ignored"));
  2428. }
  2429. static void
  2430. set_trace_notes (const char *args, int from_tty,
  2431. struct cmd_list_element *c)
  2432. {
  2433. int ret;
  2434. ret = target_set_trace_notes (NULL, trace_notes.c_str (), NULL);
  2435. if (!ret)
  2436. warning (_("Target does not support trace notes, note ignored"));
  2437. }
  2438. static void
  2439. set_trace_stop_notes (const char *args, int from_tty,
  2440. struct cmd_list_element *c)
  2441. {
  2442. int ret;
  2443. ret = target_set_trace_notes (NULL, NULL, trace_stop_notes.c_str ());
  2444. if (!ret)
  2445. warning (_("Target does not support trace notes, stop note ignored"));
  2446. }
  2447. /* Convert the memory pointed to by mem into hex, placing result in buf.
  2448. * Return a pointer to the last char put in buf (null)
  2449. * "stolen" from sparc-stub.c
  2450. */
  2451. static const char hexchars[] = "0123456789abcdef";
  2452. static char *
  2453. mem2hex (gdb_byte *mem, char *buf, int count)
  2454. {
  2455. gdb_byte ch;
  2456. while (count-- > 0)
  2457. {
  2458. ch = *mem++;
  2459. *buf++ = hexchars[ch >> 4];
  2460. *buf++ = hexchars[ch & 0xf];
  2461. }
  2462. *buf = 0;
  2463. return buf;
  2464. }
  2465. int
  2466. get_traceframe_number (void)
  2467. {
  2468. return traceframe_number;
  2469. }
  2470. int
  2471. get_tracepoint_number (void)
  2472. {
  2473. return tracepoint_number;
  2474. }
  2475. /* Make the traceframe NUM be the current trace frame. Does nothing
  2476. if NUM is already current. */
  2477. void
  2478. set_current_traceframe (int num)
  2479. {
  2480. int newnum;
  2481. if (traceframe_number == num)
  2482. {
  2483. /* Nothing to do. */
  2484. return;
  2485. }
  2486. newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
  2487. if (newnum != num)
  2488. warning (_("could not change traceframe"));
  2489. set_traceframe_num (newnum);
  2490. /* Changing the traceframe changes our view of registers and of the
  2491. frame chain. */
  2492. registers_changed ();
  2493. clear_traceframe_info ();
  2494. }
  2495. scoped_restore_current_traceframe::scoped_restore_current_traceframe ()
  2496. : m_traceframe_number (traceframe_number)
  2497. {}
  2498. /* Given a number and address, return an uploaded tracepoint with that
  2499. number, creating if necessary. */
  2500. struct uploaded_tp *
  2501. get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
  2502. {
  2503. struct uploaded_tp *utp;
  2504. for (utp = *utpp; utp; utp = utp->next)
  2505. if (utp->number == num && utp->addr == addr)
  2506. return utp;
  2507. utp = new uploaded_tp;
  2508. utp->number = num;
  2509. utp->addr = addr;
  2510. utp->next = *utpp;
  2511. *utpp = utp;
  2512. return utp;
  2513. }
  2514. void
  2515. free_uploaded_tps (struct uploaded_tp **utpp)
  2516. {
  2517. struct uploaded_tp *next_one;
  2518. while (*utpp)
  2519. {
  2520. next_one = (*utpp)->next;
  2521. delete *utpp;
  2522. *utpp = next_one;
  2523. }
  2524. }
  2525. /* Given a number and address, return an uploaded tracepoint with that
  2526. number, creating if necessary. */
  2527. struct uploaded_tsv *
  2528. get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
  2529. {
  2530. struct uploaded_tsv *utsv;
  2531. for (utsv = *utsvp; utsv; utsv = utsv->next)
  2532. if (utsv->number == num)
  2533. return utsv;
  2534. utsv = XCNEW (struct uploaded_tsv);
  2535. utsv->number = num;
  2536. utsv->next = *utsvp;
  2537. *utsvp = utsv;
  2538. return utsv;
  2539. }
  2540. void
  2541. free_uploaded_tsvs (struct uploaded_tsv **utsvp)
  2542. {
  2543. struct uploaded_tsv *next_one;
  2544. while (*utsvp)
  2545. {
  2546. next_one = (*utsvp)->next;
  2547. xfree (*utsvp);
  2548. *utsvp = next_one;
  2549. }
  2550. }
  2551. /* FIXME this function is heuristic and will miss the cases where the
  2552. conditional is semantically identical but differs in whitespace,
  2553. such as "x == 0" vs "x==0". */
  2554. static int
  2555. cond_string_is_same (char *str1, char *str2)
  2556. {
  2557. if (str1 == NULL || str2 == NULL)
  2558. return (str1 == str2);
  2559. return (strcmp (str1, str2) == 0);
  2560. }
  2561. /* Look for an existing tracepoint that seems similar enough to the
  2562. uploaded one. Enablement isn't compared, because the user can
  2563. toggle that freely, and may have done so in anticipation of the
  2564. next trace run. Return the location of matched tracepoint. */
  2565. static struct bp_location *
  2566. find_matching_tracepoint_location (struct uploaded_tp *utp)
  2567. {
  2568. struct bp_location *loc;
  2569. for (breakpoint *b : all_tracepoints ())
  2570. {
  2571. struct tracepoint *t = (struct tracepoint *) b;
  2572. if (b->type == utp->type
  2573. && t->step_count == utp->step
  2574. && t->pass_count == utp->pass
  2575. && cond_string_is_same (t->cond_string.get (),
  2576. utp->cond_string.get ())
  2577. /* FIXME also test actions. */
  2578. )
  2579. {
  2580. /* Scan the locations for an address match. */
  2581. for (loc = b->loc; loc; loc = loc->next)
  2582. {
  2583. if (loc->address == utp->addr)
  2584. return loc;
  2585. }
  2586. }
  2587. }
  2588. return NULL;
  2589. }
  2590. /* Given a list of tracepoints uploaded from a target, attempt to
  2591. match them up with existing tracepoints, and create new ones if not
  2592. found. */
  2593. void
  2594. merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
  2595. {
  2596. struct uploaded_tp *utp;
  2597. /* A set of tracepoints which are modified. */
  2598. std::vector<breakpoint *> modified_tp;
  2599. /* Look for GDB tracepoints that match up with our uploaded versions. */
  2600. for (utp = *uploaded_tps; utp; utp = utp->next)
  2601. {
  2602. struct bp_location *loc;
  2603. struct tracepoint *t;
  2604. loc = find_matching_tracepoint_location (utp);
  2605. if (loc)
  2606. {
  2607. int found = 0;
  2608. /* Mark this location as already inserted. */
  2609. loc->inserted = 1;
  2610. t = (struct tracepoint *) loc->owner;
  2611. gdb_printf (_("Assuming tracepoint %d is same "
  2612. "as target's tracepoint %d at %s.\n"),
  2613. loc->owner->number, utp->number,
  2614. paddress (loc->gdbarch, utp->addr));
  2615. /* The tracepoint LOC->owner was modified (the location LOC
  2616. was marked as inserted in the target). Save it in
  2617. MODIFIED_TP if not there yet. The 'breakpoint-modified'
  2618. observers will be notified later once for each tracepoint
  2619. saved in MODIFIED_TP. */
  2620. for (breakpoint *b : modified_tp)
  2621. if (b == loc->owner)
  2622. {
  2623. found = 1;
  2624. break;
  2625. }
  2626. if (!found)
  2627. modified_tp.push_back (loc->owner);
  2628. }
  2629. else
  2630. {
  2631. t = create_tracepoint_from_upload (utp);
  2632. if (t)
  2633. gdb_printf (_("Created tracepoint %d for "
  2634. "target's tracepoint %d at %s.\n"),
  2635. t->number, utp->number,
  2636. paddress (get_current_arch (), utp->addr));
  2637. else
  2638. gdb_printf (_("Failed to create tracepoint for target's "
  2639. "tracepoint %d at %s, skipping it.\n"),
  2640. utp->number,
  2641. paddress (get_current_arch (), utp->addr));
  2642. }
  2643. /* Whether found or created, record the number used by the
  2644. target, to help with mapping target tracepoints back to their
  2645. counterparts here. */
  2646. if (t)
  2647. t->number_on_target = utp->number;
  2648. }
  2649. /* Notify 'breakpoint-modified' observer that at least one of B's
  2650. locations was changed. */
  2651. for (breakpoint *b : modified_tp)
  2652. gdb::observers::breakpoint_modified.notify (b);
  2653. free_uploaded_tps (uploaded_tps);
  2654. }
  2655. /* Trace state variables don't have much to identify them beyond their
  2656. name, so just use that to detect matches. */
  2657. static struct trace_state_variable *
  2658. find_matching_tsv (struct uploaded_tsv *utsv)
  2659. {
  2660. if (!utsv->name)
  2661. return NULL;
  2662. return find_trace_state_variable (utsv->name);
  2663. }
  2664. static struct trace_state_variable *
  2665. create_tsv_from_upload (struct uploaded_tsv *utsv)
  2666. {
  2667. const char *namebase;
  2668. std::string buf;
  2669. int try_num = 0;
  2670. struct trace_state_variable *tsv;
  2671. if (utsv->name)
  2672. {
  2673. namebase = utsv->name;
  2674. buf = namebase;
  2675. }
  2676. else
  2677. {
  2678. namebase = "__tsv";
  2679. buf = string_printf ("%s_%d", namebase, try_num++);
  2680. }
  2681. /* Fish for a name that is not in use. */
  2682. /* (should check against all internal vars?) */
  2683. while (find_trace_state_variable (buf.c_str ()))
  2684. buf = string_printf ("%s_%d", namebase, try_num++);
  2685. /* We have an available name, create the variable. */
  2686. tsv = create_trace_state_variable (buf.c_str ());
  2687. tsv->initial_value = utsv->initial_value;
  2688. tsv->builtin = utsv->builtin;
  2689. gdb::observers::tsv_created.notify (tsv);
  2690. return tsv;
  2691. }
  2692. /* Given a list of uploaded trace state variables, try to match them
  2693. up with existing variables, or create additional ones. */
  2694. void
  2695. merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
  2696. {
  2697. struct uploaded_tsv *utsv;
  2698. int highest;
  2699. /* Most likely some numbers will have to be reassigned as part of
  2700. the merge, so clear them all in anticipation. */
  2701. for (trace_state_variable &tsv : tvariables)
  2702. tsv.number = 0;
  2703. for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
  2704. {
  2705. struct trace_state_variable *tsv = find_matching_tsv (utsv);
  2706. if (tsv)
  2707. {
  2708. if (info_verbose)
  2709. gdb_printf (_("Assuming trace state variable $%s "
  2710. "is same as target's variable %d.\n"),
  2711. tsv->name.c_str (), utsv->number);
  2712. }
  2713. else
  2714. {
  2715. tsv = create_tsv_from_upload (utsv);
  2716. if (info_verbose)
  2717. gdb_printf (_("Created trace state variable "
  2718. "$%s for target's variable %d.\n"),
  2719. tsv->name.c_str (), utsv->number);
  2720. }
  2721. /* Give precedence to numberings that come from the target. */
  2722. if (tsv)
  2723. tsv->number = utsv->number;
  2724. }
  2725. /* Renumber everything that didn't get a target-assigned number. */
  2726. highest = 0;
  2727. for (const trace_state_variable &tsv : tvariables)
  2728. highest = std::max (tsv.number, highest);
  2729. ++highest;
  2730. for (trace_state_variable &tsv : tvariables)
  2731. if (tsv.number == 0)
  2732. tsv.number = highest++;
  2733. free_uploaded_tsvs (uploaded_tsvs);
  2734. }
  2735. /* Parse the part of trace status syntax that is shared between
  2736. the remote protocol and the trace file reader. */
  2737. void
  2738. parse_trace_status (const char *line, struct trace_status *ts)
  2739. {
  2740. const char *p = line, *p1, *p2, *p3, *p_temp;
  2741. int end;
  2742. ULONGEST val;
  2743. ts->running_known = 1;
  2744. ts->running = (*p++ == '1');
  2745. ts->stop_reason = trace_stop_reason_unknown;
  2746. xfree (ts->stop_desc);
  2747. ts->stop_desc = NULL;
  2748. ts->traceframe_count = -1;
  2749. ts->traceframes_created = -1;
  2750. ts->buffer_free = -1;
  2751. ts->buffer_size = -1;
  2752. ts->disconnected_tracing = 0;
  2753. ts->circular_buffer = 0;
  2754. xfree (ts->user_name);
  2755. ts->user_name = NULL;
  2756. xfree (ts->notes);
  2757. ts->notes = NULL;
  2758. ts->start_time = ts->stop_time = 0;
  2759. while (*p++)
  2760. {
  2761. p1 = strchr (p, ':');
  2762. if (p1 == NULL)
  2763. error (_("Malformed trace status, at %s\n\
  2764. Status line: '%s'\n"), p, line);
  2765. p3 = strchr (p, ';');
  2766. if (p3 == NULL)
  2767. p3 = p + strlen (p);
  2768. if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
  2769. {
  2770. p = unpack_varlen_hex (++p1, &val);
  2771. ts->stop_reason = trace_buffer_full;
  2772. }
  2773. else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
  2774. {
  2775. p = unpack_varlen_hex (++p1, &val);
  2776. ts->stop_reason = trace_never_run;
  2777. }
  2778. else if (strncmp (p, stop_reason_names[tracepoint_passcount],
  2779. p1 - p) == 0)
  2780. {
  2781. p = unpack_varlen_hex (++p1, &val);
  2782. ts->stop_reason = tracepoint_passcount;
  2783. ts->stopping_tracepoint = val;
  2784. }
  2785. else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0)
  2786. {
  2787. p2 = strchr (++p1, ':');
  2788. if (!p2 || p2 > p3)
  2789. {
  2790. /*older style*/
  2791. p2 = p1;
  2792. }
  2793. else if (p2 != p1)
  2794. {
  2795. ts->stop_desc = (char *) xmalloc (strlen (line));
  2796. end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
  2797. ts->stop_desc[end] = '\0';
  2798. }
  2799. else
  2800. ts->stop_desc = xstrdup ("");
  2801. p = unpack_varlen_hex (++p2, &val);
  2802. ts->stop_reason = trace_stop_command;
  2803. }
  2804. else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
  2805. {
  2806. p = unpack_varlen_hex (++p1, &val);
  2807. ts->stop_reason = trace_disconnected;
  2808. }
  2809. else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
  2810. {
  2811. p2 = strchr (++p1, ':');
  2812. if (p2 != p1)
  2813. {
  2814. ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1);
  2815. end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
  2816. ts->stop_desc[end] = '\0';
  2817. }
  2818. else
  2819. ts->stop_desc = xstrdup ("");
  2820. p = unpack_varlen_hex (++p2, &val);
  2821. ts->stopping_tracepoint = val;
  2822. ts->stop_reason = tracepoint_error;
  2823. }
  2824. else if (strncmp (p, "tframes", p1 - p) == 0)
  2825. {
  2826. p = unpack_varlen_hex (++p1, &val);
  2827. ts->traceframe_count = val;
  2828. }
  2829. else if (strncmp (p, "tcreated", p1 - p) == 0)
  2830. {
  2831. p = unpack_varlen_hex (++p1, &val);
  2832. ts->traceframes_created = val;
  2833. }
  2834. else if (strncmp (p, "tfree", p1 - p) == 0)
  2835. {
  2836. p = unpack_varlen_hex (++p1, &val);
  2837. ts->buffer_free = val;
  2838. }
  2839. else if (strncmp (p, "tsize", p1 - p) == 0)
  2840. {
  2841. p = unpack_varlen_hex (++p1, &val);
  2842. ts->buffer_size = val;
  2843. }
  2844. else if (strncmp (p, "disconn", p1 - p) == 0)
  2845. {
  2846. p = unpack_varlen_hex (++p1, &val);
  2847. ts->disconnected_tracing = val;
  2848. }
  2849. else if (strncmp (p, "circular", p1 - p) == 0)
  2850. {
  2851. p = unpack_varlen_hex (++p1, &val);
  2852. ts->circular_buffer = val;
  2853. }
  2854. else if (strncmp (p, "starttime", p1 - p) == 0)
  2855. {
  2856. p = unpack_varlen_hex (++p1, &val);
  2857. ts->start_time = val;
  2858. }
  2859. else if (strncmp (p, "stoptime", p1 - p) == 0)
  2860. {
  2861. p = unpack_varlen_hex (++p1, &val);
  2862. ts->stop_time = val;
  2863. }
  2864. else if (strncmp (p, "username", p1 - p) == 0)
  2865. {
  2866. ++p1;
  2867. ts->user_name = (char *) xmalloc (strlen (p) / 2);
  2868. end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
  2869. ts->user_name[end] = '\0';
  2870. p = p3;
  2871. }
  2872. else if (strncmp (p, "notes", p1 - p) == 0)
  2873. {
  2874. ++p1;
  2875. ts->notes = (char *) xmalloc (strlen (p) / 2);
  2876. end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
  2877. ts->notes[end] = '\0';
  2878. p = p3;
  2879. }
  2880. else
  2881. {
  2882. /* Silently skip unknown optional info. */
  2883. p_temp = strchr (p1 + 1, ';');
  2884. if (p_temp)
  2885. p = p_temp;
  2886. else
  2887. /* Must be at the end. */
  2888. break;
  2889. }
  2890. }
  2891. }
  2892. void
  2893. parse_tracepoint_status (const char *p, struct breakpoint *bp,
  2894. struct uploaded_tp *utp)
  2895. {
  2896. ULONGEST uval;
  2897. struct tracepoint *tp = (struct tracepoint *) bp;
  2898. p = unpack_varlen_hex (p, &uval);
  2899. if (tp)
  2900. tp->hit_count += uval;
  2901. else
  2902. utp->hit_count += uval;
  2903. p = unpack_varlen_hex (p + 1, &uval);
  2904. if (tp)
  2905. tp->traceframe_usage += uval;
  2906. else
  2907. utp->traceframe_usage += uval;
  2908. /* Ignore any extra, allowing for future extensions. */
  2909. }
  2910. /* Given a line of text defining a part of a tracepoint, parse it into
  2911. an "uploaded tracepoint". */
  2912. void
  2913. parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
  2914. {
  2915. const char *p;
  2916. char piece;
  2917. ULONGEST num, addr, step, pass, orig_size, xlen, start;
  2918. int enabled, end;
  2919. enum bptype type;
  2920. const char *srctype;
  2921. char *buf;
  2922. struct uploaded_tp *utp = NULL;
  2923. p = line;
  2924. /* Both tracepoint and action definitions start with the same number
  2925. and address sequence. */
  2926. piece = *p++;
  2927. p = unpack_varlen_hex (p, &num);
  2928. p++; /* skip a colon */
  2929. p = unpack_varlen_hex (p, &addr);
  2930. p++; /* skip a colon */
  2931. if (piece == 'T')
  2932. {
  2933. gdb::unique_xmalloc_ptr<char[]> cond;
  2934. enabled = (*p++ == 'E');
  2935. p++; /* skip a colon */
  2936. p = unpack_varlen_hex (p, &step);
  2937. p++; /* skip a colon */
  2938. p = unpack_varlen_hex (p, &pass);
  2939. type = bp_tracepoint;
  2940. /* Thumb through optional fields. */
  2941. while (*p == ':')
  2942. {
  2943. p++; /* skip a colon */
  2944. if (*p == 'F')
  2945. {
  2946. type = bp_fast_tracepoint;
  2947. p++;
  2948. p = unpack_varlen_hex (p, &orig_size);
  2949. }
  2950. else if (*p == 'S')
  2951. {
  2952. type = bp_static_tracepoint;
  2953. p++;
  2954. }
  2955. else if (*p == 'X')
  2956. {
  2957. p++;
  2958. p = unpack_varlen_hex (p, &xlen);
  2959. p++; /* skip a comma */
  2960. cond.reset ((char *) xmalloc (2 * xlen + 1));
  2961. strncpy (&cond[0], p, 2 * xlen);
  2962. cond[2 * xlen] = '\0';
  2963. p += 2 * xlen;
  2964. }
  2965. else
  2966. warning (_("Unrecognized char '%c' in tracepoint "
  2967. "definition, skipping rest"), *p);
  2968. }
  2969. utp = get_uploaded_tp (num, addr, utpp);
  2970. utp->type = type;
  2971. utp->enabled = enabled;
  2972. utp->step = step;
  2973. utp->pass = pass;
  2974. utp->cond = std::move (cond);
  2975. }
  2976. else if (piece == 'A')
  2977. {
  2978. utp = get_uploaded_tp (num, addr, utpp);
  2979. utp->actions.emplace_back (xstrdup (p));
  2980. }
  2981. else if (piece == 'S')
  2982. {
  2983. utp = get_uploaded_tp (num, addr, utpp);
  2984. utp->step_actions.emplace_back (xstrdup (p));
  2985. }
  2986. else if (piece == 'Z')
  2987. {
  2988. /* Parse a chunk of source form definition. */
  2989. utp = get_uploaded_tp (num, addr, utpp);
  2990. srctype = p;
  2991. p = strchr (p, ':');
  2992. p++; /* skip a colon */
  2993. p = unpack_varlen_hex (p, &start);
  2994. p++; /* skip a colon */
  2995. p = unpack_varlen_hex (p, &xlen);
  2996. p++; /* skip a colon */
  2997. buf = (char *) alloca (strlen (line));
  2998. end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
  2999. buf[end] = '\0';
  3000. if (startswith (srctype, "at:"))
  3001. utp->at_string.reset (xstrdup (buf));
  3002. else if (startswith (srctype, "cond:"))
  3003. utp->cond_string.reset (xstrdup (buf));
  3004. else if (startswith (srctype, "cmd:"))
  3005. utp->cmd_strings.emplace_back (xstrdup (buf));
  3006. }
  3007. else if (piece == 'V')
  3008. {
  3009. utp = get_uploaded_tp (num, addr, utpp);
  3010. parse_tracepoint_status (p, NULL, utp);
  3011. }
  3012. else
  3013. {
  3014. /* Don't error out, the target might be sending us optional
  3015. info that we don't care about. */
  3016. warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
  3017. }
  3018. }
  3019. /* Convert a textual description of a trace state variable into an
  3020. uploaded object. */
  3021. void
  3022. parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp)
  3023. {
  3024. const char *p;
  3025. char *buf;
  3026. ULONGEST num, initval, builtin;
  3027. int end;
  3028. struct uploaded_tsv *utsv = NULL;
  3029. buf = (char *) alloca (strlen (line));
  3030. p = line;
  3031. p = unpack_varlen_hex (p, &num);
  3032. p++; /* skip a colon */
  3033. p = unpack_varlen_hex (p, &initval);
  3034. p++; /* skip a colon */
  3035. p = unpack_varlen_hex (p, &builtin);
  3036. p++; /* skip a colon */
  3037. end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
  3038. buf[end] = '\0';
  3039. utsv = get_uploaded_tsv (num, utsvp);
  3040. utsv->initial_value = initval;
  3041. utsv->builtin = builtin;
  3042. utsv->name = xstrdup (buf);
  3043. }
  3044. /* Given a line of text defining a static tracepoint marker, parse it
  3045. into a "static tracepoint marker" object. Throws an error is
  3046. parsing fails. If PP is non-null, it points to one past the end of
  3047. the parsed marker definition. */
  3048. void
  3049. parse_static_tracepoint_marker_definition (const char *line, const char **pp,
  3050. static_tracepoint_marker *marker)
  3051. {
  3052. const char *p, *endp;
  3053. ULONGEST addr;
  3054. p = line;
  3055. p = unpack_varlen_hex (p, &addr);
  3056. p++; /* skip a colon */
  3057. marker->gdbarch = target_gdbarch ();
  3058. marker->address = (CORE_ADDR) addr;
  3059. endp = strchr (p, ':');
  3060. if (endp == NULL)
  3061. error (_("bad marker definition: %s"), line);
  3062. marker->str_id = hex2str (p, (endp - p) / 2);
  3063. p = endp;
  3064. p++; /* skip a colon */
  3065. /* This definition may be followed by another one, separated by a comma. */
  3066. int hex_len;
  3067. endp = strchr (p, ',');
  3068. if (endp != nullptr)
  3069. hex_len = endp - p;
  3070. else
  3071. hex_len = strlen (p);
  3072. marker->extra = hex2str (p, hex_len / 2);
  3073. if (pp != nullptr)
  3074. *pp = p + hex_len;
  3075. }
  3076. /* Print MARKER to gdb_stdout. */
  3077. static void
  3078. print_one_static_tracepoint_marker (int count,
  3079. const static_tracepoint_marker &marker)
  3080. {
  3081. struct symbol *sym;
  3082. struct ui_out *uiout = current_uiout;
  3083. symtab_and_line sal;
  3084. sal.pc = marker.address;
  3085. std::vector<breakpoint *> tracepoints
  3086. = static_tracepoints_here (marker.address);
  3087. ui_out_emit_tuple tuple_emitter (uiout, "marker");
  3088. /* A counter field to help readability. This is not a stable
  3089. identifier! */
  3090. uiout->field_signed ("count", count);
  3091. uiout->field_string ("marker-id", marker.str_id);
  3092. uiout->field_fmt ("enabled", "%c",
  3093. !tracepoints.empty () ? 'y' : 'n');
  3094. uiout->spaces (2);
  3095. int wrap_indent = 35;
  3096. if (gdbarch_addr_bit (marker.gdbarch) <= 32)
  3097. wrap_indent += 11;
  3098. else
  3099. wrap_indent += 19;
  3100. const char *extra_field_indent = " ";
  3101. uiout->field_core_addr ("addr", marker.gdbarch, marker.address);
  3102. sal = find_pc_line (marker.address, 0);
  3103. sym = find_pc_sect_function (marker.address, NULL);
  3104. if (sym)
  3105. {
  3106. uiout->text ("in ");
  3107. uiout->field_string ("func", sym->print_name (),
  3108. function_name_style.style ());
  3109. uiout->wrap_hint (wrap_indent);
  3110. uiout->text (" at ");
  3111. }
  3112. else
  3113. uiout->field_skip ("func");
  3114. if (sal.symtab != NULL)
  3115. {
  3116. uiout->field_string ("file",
  3117. symtab_to_filename_for_display (sal.symtab),
  3118. file_name_style.style ());
  3119. uiout->text (":");
  3120. if (uiout->is_mi_like_p ())
  3121. {
  3122. const char *fullname = symtab_to_fullname (sal.symtab);
  3123. uiout->field_string ("fullname", fullname);
  3124. }
  3125. else
  3126. uiout->field_skip ("fullname");
  3127. uiout->field_signed ("line", sal.line);
  3128. }
  3129. else
  3130. {
  3131. uiout->field_skip ("fullname");
  3132. uiout->field_skip ("line");
  3133. }
  3134. uiout->text ("\n");
  3135. uiout->text (extra_field_indent);
  3136. uiout->text (_("Data: \""));
  3137. uiout->field_string ("extra-data", marker.extra);
  3138. uiout->text ("\"\n");
  3139. if (!tracepoints.empty ())
  3140. {
  3141. int ix;
  3142. {
  3143. ui_out_emit_tuple inner_tuple_emitter (uiout, "tracepoints-at");
  3144. uiout->text (extra_field_indent);
  3145. uiout->text (_("Probed by static tracepoints: "));
  3146. for (ix = 0; ix < tracepoints.size (); ix++)
  3147. {
  3148. if (ix > 0)
  3149. uiout->text (", ");
  3150. uiout->text ("#");
  3151. uiout->field_signed ("tracepoint-id", tracepoints[ix]->number);
  3152. }
  3153. }
  3154. if (uiout->is_mi_like_p ())
  3155. uiout->field_signed ("number-of-tracepoints", tracepoints.size ());
  3156. else
  3157. uiout->text ("\n");
  3158. }
  3159. }
  3160. static void
  3161. info_static_tracepoint_markers_command (const char *arg, int from_tty)
  3162. {
  3163. struct ui_out *uiout = current_uiout;
  3164. std::vector<static_tracepoint_marker> markers
  3165. = target_static_tracepoint_markers_by_strid (NULL);
  3166. /* We don't have to check target_can_use_agent and agent's capability on
  3167. static tracepoint here, in order to be compatible with older GDBserver.
  3168. We don't check USE_AGENT is true or not, because static tracepoints
  3169. don't work without in-process agent, so we don't bother users to type
  3170. `set agent on' when to use static tracepoint. */
  3171. ui_out_emit_table table_emitter (uiout, 5, -1,
  3172. "StaticTracepointMarkersTable");
  3173. uiout->table_header (7, ui_left, "counter", "Cnt");
  3174. uiout->table_header (40, ui_left, "marker-id", "ID");
  3175. uiout->table_header (3, ui_left, "enabled", "Enb");
  3176. if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
  3177. uiout->table_header (10, ui_left, "addr", "Address");
  3178. else
  3179. uiout->table_header (18, ui_left, "addr", "Address");
  3180. uiout->table_header (40, ui_noalign, "what", "What");
  3181. uiout->table_body ();
  3182. for (int i = 0; i < markers.size (); i++)
  3183. print_one_static_tracepoint_marker (i + 1, markers[i]);
  3184. }
  3185. /* The $_sdata convenience variable is a bit special. We don't know
  3186. for sure type of the value until we actually have a chance to fetch
  3187. the data --- the size of the object depends on what has been
  3188. collected. We solve this by making $_sdata be an internalvar that
  3189. creates a new value on access. */
  3190. /* Return a new value with the correct type for the sdata object of
  3191. the current trace frame. Return a void value if there's no object
  3192. available. */
  3193. static struct value *
  3194. sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
  3195. void *ignore)
  3196. {
  3197. /* We need to read the whole object before we know its size. */
  3198. gdb::optional<gdb::byte_vector> buf
  3199. = target_read_alloc (current_inferior ()->top_target (),
  3200. TARGET_OBJECT_STATIC_TRACE_DATA,
  3201. NULL);
  3202. if (buf)
  3203. {
  3204. struct value *v;
  3205. struct type *type;
  3206. type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
  3207. buf->size ());
  3208. v = allocate_value (type);
  3209. memcpy (value_contents_raw (v).data (), buf->data (), buf->size ());
  3210. return v;
  3211. }
  3212. else
  3213. return allocate_value (builtin_type (gdbarch)->builtin_void);
  3214. }
  3215. #if !defined(HAVE_LIBEXPAT)
  3216. struct std::unique_ptr<traceframe_info>
  3217. parse_traceframe_info (const char *tframe_info)
  3218. {
  3219. static int have_warned;
  3220. if (!have_warned)
  3221. {
  3222. have_warned = 1;
  3223. warning (_("Can not parse XML trace frame info; XML support "
  3224. "was disabled at compile time"));
  3225. }
  3226. return NULL;
  3227. }
  3228. #else /* HAVE_LIBEXPAT */
  3229. #include "xml-support.h"
  3230. /* Handle the start of a <memory> element. */
  3231. static void
  3232. traceframe_info_start_memory (struct gdb_xml_parser *parser,
  3233. const struct gdb_xml_element *element,
  3234. void *user_data,
  3235. std::vector<gdb_xml_value> &attributes)
  3236. {
  3237. struct traceframe_info *info = (struct traceframe_info *) user_data;
  3238. ULONGEST *start_p, *length_p;
  3239. start_p
  3240. = (ULONGEST *) xml_find_attribute (attributes, "start")->value.get ();
  3241. length_p
  3242. = (ULONGEST *) xml_find_attribute (attributes, "length")->value.get ();
  3243. info->memory.emplace_back (*start_p, *length_p);
  3244. }
  3245. /* Handle the start of a <tvar> element. */
  3246. static void
  3247. traceframe_info_start_tvar (struct gdb_xml_parser *parser,
  3248. const struct gdb_xml_element *element,
  3249. void *user_data,
  3250. std::vector<gdb_xml_value> &attributes)
  3251. {
  3252. struct traceframe_info *info = (struct traceframe_info *) user_data;
  3253. const char *id_attrib
  3254. = (const char *) xml_find_attribute (attributes, "id")->value.get ();
  3255. int id = gdb_xml_parse_ulongest (parser, id_attrib);
  3256. info->tvars.push_back (id);
  3257. }
  3258. /* The allowed elements and attributes for an XML memory map. */
  3259. static const struct gdb_xml_attribute memory_attributes[] = {
  3260. { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  3261. { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  3262. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  3263. };
  3264. static const struct gdb_xml_attribute tvar_attributes[] = {
  3265. { "id", GDB_XML_AF_NONE, NULL, NULL },
  3266. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  3267. };
  3268. static const struct gdb_xml_element traceframe_info_children[] = {
  3269. { "memory", memory_attributes, NULL,
  3270. GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  3271. traceframe_info_start_memory, NULL },
  3272. { "tvar", tvar_attributes, NULL,
  3273. GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  3274. traceframe_info_start_tvar, NULL },
  3275. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  3276. };
  3277. static const struct gdb_xml_element traceframe_info_elements[] = {
  3278. { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
  3279. NULL, NULL },
  3280. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  3281. };
  3282. /* Parse a traceframe-info XML document. */
  3283. traceframe_info_up
  3284. parse_traceframe_info (const char *tframe_info)
  3285. {
  3286. traceframe_info_up result (new traceframe_info);
  3287. if (gdb_xml_parse_quick (_("trace frame info"),
  3288. "traceframe-info.dtd", traceframe_info_elements,
  3289. tframe_info, result.get ()) == 0)
  3290. return result;
  3291. return NULL;
  3292. }
  3293. #endif /* HAVE_LIBEXPAT */
  3294. /* Returns the traceframe_info object for the current traceframe.
  3295. This is where we avoid re-fetching the object from the target if we
  3296. already have it cached. */
  3297. struct traceframe_info *
  3298. get_traceframe_info (void)
  3299. {
  3300. if (current_traceframe_info == NULL)
  3301. current_traceframe_info = target_traceframe_info ();
  3302. return current_traceframe_info.get ();
  3303. }
  3304. /* If the target supports the query, return in RESULT the set of
  3305. collected memory in the current traceframe, found within the LEN
  3306. bytes range starting at MEMADDR. Returns true if the target
  3307. supports the query, otherwise returns false, and RESULT is left
  3308. undefined. */
  3309. int
  3310. traceframe_available_memory (std::vector<mem_range> *result,
  3311. CORE_ADDR memaddr, ULONGEST len)
  3312. {
  3313. struct traceframe_info *info = get_traceframe_info ();
  3314. if (info != NULL)
  3315. {
  3316. result->clear ();
  3317. for (mem_range &r : info->memory)
  3318. if (mem_ranges_overlap (r.start, r.length, memaddr, len))
  3319. {
  3320. ULONGEST lo1, hi1, lo2, hi2;
  3321. lo1 = memaddr;
  3322. hi1 = memaddr + len;
  3323. lo2 = r.start;
  3324. hi2 = r.start + r.length;
  3325. CORE_ADDR start = std::max (lo1, lo2);
  3326. int length = std::min (hi1, hi2) - start;
  3327. result->emplace_back (start, length);
  3328. }
  3329. normalize_mem_ranges (result);
  3330. return 1;
  3331. }
  3332. return 0;
  3333. }
  3334. /* Implementation of `sdata' variable. */
  3335. static const struct internalvar_funcs sdata_funcs =
  3336. {
  3337. sdata_make_value,
  3338. NULL
  3339. };
  3340. /* See tracepoint.h. */
  3341. cmd_list_element *while_stepping_cmd_element = nullptr;
  3342. /* module initialization */
  3343. void _initialize_tracepoint ();
  3344. void
  3345. _initialize_tracepoint ()
  3346. {
  3347. struct cmd_list_element *c;
  3348. /* Explicitly create without lookup, since that tries to create a
  3349. value with a void typed value, and when we get here, gdbarch
  3350. isn't initialized yet. At this point, we're quite sure there
  3351. isn't another convenience variable of the same name. */
  3352. create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
  3353. traceframe_number = -1;
  3354. tracepoint_number = -1;
  3355. add_info ("scope", info_scope_command,
  3356. _("List the variables local to a scope."));
  3357. add_cmd ("tracepoints", class_trace,
  3358. _("Tracing of program execution without stopping the program."),
  3359. &cmdlist);
  3360. add_com ("tdump", class_trace, tdump_command,
  3361. _("Print everything collected at the current tracepoint."));
  3362. c = add_com ("tvariable", class_trace, trace_variable_command,_("\
  3363. Define a trace state variable.\n\
  3364. Argument is a $-prefixed name, optionally followed\n\
  3365. by '=' and an expression that sets the initial value\n\
  3366. at the start of tracing."));
  3367. set_cmd_completer (c, expression_completer);
  3368. add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
  3369. Delete one or more trace state variables.\n\
  3370. Arguments are the names of the variables to delete.\n\
  3371. If no arguments are supplied, delete all variables."), &deletelist);
  3372. /* FIXME add a trace variable completer. */
  3373. add_info ("tvariables", info_tvariables_command, _("\
  3374. Status of trace state variables and their values."));
  3375. add_info ("static-tracepoint-markers",
  3376. info_static_tracepoint_markers_command, _("\
  3377. List target static tracepoints markers."));
  3378. add_prefix_cmd ("tfind", class_trace, tfind_command, _("\
  3379. Select a trace frame.\n\
  3380. No argument means forward by one frame; '-' means backward by one frame."),
  3381. &tfindlist, 1, &cmdlist);
  3382. add_cmd ("outside", class_trace, tfind_outside_command, _("\
  3383. Select a trace frame whose PC is outside the given range (exclusive).\n\
  3384. Usage: tfind outside ADDR1, ADDR2"),
  3385. &tfindlist);
  3386. add_cmd ("range", class_trace, tfind_range_command, _("\
  3387. Select a trace frame whose PC is in the given range (inclusive).\n\
  3388. Usage: tfind range ADDR1, ADDR2"),
  3389. &tfindlist);
  3390. add_cmd ("line", class_trace, tfind_line_command, _("\
  3391. Select a trace frame by source line.\n\
  3392. Argument can be a line number (with optional source file),\n\
  3393. a function name, or '*' followed by an address.\n\
  3394. Default argument is 'the next source line that was traced'."),
  3395. &tfindlist);
  3396. add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\
  3397. Select a trace frame by tracepoint number.\n\
  3398. Default is the tracepoint for the current trace frame."),
  3399. &tfindlist);
  3400. add_cmd ("pc", class_trace, tfind_pc_command, _("\
  3401. Select a trace frame by PC.\n\
  3402. Default is the current PC, or the PC of the current trace frame."),
  3403. &tfindlist);
  3404. cmd_list_element *tfind_end_cmd
  3405. = add_cmd ("end", class_trace, tfind_end_command, _("\
  3406. De-select any trace frame and resume 'live' debugging."), &tfindlist);
  3407. add_alias_cmd ("none", tfind_end_cmd, class_trace, 0, &tfindlist);
  3408. add_cmd ("start", class_trace, tfind_start_command,
  3409. _("Select the first trace frame in the trace buffer."),
  3410. &tfindlist);
  3411. add_com ("tstatus", class_trace, tstatus_command,
  3412. _("Display the status of the current trace data collection."));
  3413. add_com ("tstop", class_trace, tstop_command, _("\
  3414. Stop trace data collection.\n\
  3415. Usage: tstop [NOTES]...\n\
  3416. Any arguments supplied are recorded with the trace as a stop reason and\n\
  3417. reported by tstatus (if the target supports trace notes)."));
  3418. add_com ("tstart", class_trace, tstart_command, _("\
  3419. Start trace data collection.\n\
  3420. Usage: tstart [NOTES]...\n\
  3421. Any arguments supplied are recorded with the trace as a note and\n\
  3422. reported by tstatus (if the target supports trace notes)."));
  3423. add_com ("end", class_trace, end_actions_pseudocommand, _("\
  3424. Ends a list of commands or actions.\n\
  3425. Several GDB commands allow you to enter a list of commands or actions.\n\
  3426. Entering \"end\" on a line by itself is the normal way to terminate\n\
  3427. such a list.\n\n\
  3428. Note: the \"end\" command cannot be used at the gdb prompt."));
  3429. while_stepping_cmd_element = add_com ("while-stepping", class_trace,
  3430. while_stepping_pseudocommand, _("\
  3431. Specify single-stepping behavior at a tracepoint.\n\
  3432. Argument is number of instructions to trace in single-step mode\n\
  3433. following the tracepoint. This command is normally followed by\n\
  3434. one or more \"collect\" commands, to specify what to collect\n\
  3435. while single-stepping.\n\n\
  3436. Note: this command can only be used in a tracepoint \"actions\" list."));
  3437. add_com_alias ("ws", while_stepping_cmd_element, class_trace, 0);
  3438. add_com_alias ("stepping", while_stepping_cmd_element, class_trace, 0);
  3439. add_com ("collect", class_trace, collect_pseudocommand, _("\
  3440. Specify one or more data items to be collected at a tracepoint.\n\
  3441. Accepts a comma-separated list of (one or more) expressions. GDB will\n\
  3442. collect all data (variables, registers) referenced by that expression.\n\
  3443. Also accepts the following special arguments:\n\
  3444. $regs -- all registers.\n\
  3445. $args -- all function arguments.\n\
  3446. $locals -- all variables local to the block/function scope.\n\
  3447. $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
  3448. Note: this command can only be used in a tracepoint \"actions\" list."));
  3449. add_com ("teval", class_trace, teval_pseudocommand, _("\
  3450. Specify one or more expressions to be evaluated at a tracepoint.\n\
  3451. Accepts a comma-separated list of (one or more) expressions.\n\
  3452. The result of each evaluation will be discarded.\n\
  3453. Note: this command can only be used in a tracepoint \"actions\" list."));
  3454. add_com ("actions", class_trace, actions_command, _("\
  3455. Specify the actions to be taken at a tracepoint.\n\
  3456. Tracepoint actions may include collecting of specified data,\n\
  3457. single-stepping, or enabling/disabling other tracepoints,\n\
  3458. depending on target's capabilities."));
  3459. add_setshow_string_cmd ("default-collect", class_trace,
  3460. &default_collect, _("\
  3461. Set the list of expressions to collect by default."), _("\
  3462. Show the list of expressions to collect by default."), NULL,
  3463. NULL, NULL,
  3464. &setlist, &showlist);
  3465. add_setshow_boolean_cmd ("disconnected-tracing", no_class,
  3466. &disconnected_tracing, _("\
  3467. Set whether tracing continues after GDB disconnects."), _("\
  3468. Show whether tracing continues after GDB disconnects."), _("\
  3469. Use this to continue a tracing run even if GDB disconnects\n\
  3470. or detaches from the target. You can reconnect later and look at\n\
  3471. trace data collected in the meantime."),
  3472. set_disconnected_tracing,
  3473. NULL,
  3474. &setlist,
  3475. &showlist);
  3476. add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
  3477. &circular_trace_buffer, _("\
  3478. Set target's use of circular trace buffer."), _("\
  3479. Show target's use of circular trace buffer."), _("\
  3480. Use this to make the trace buffer into a circular buffer,\n\
  3481. which will discard traceframes (oldest first) instead of filling\n\
  3482. up and stopping the trace run."),
  3483. set_circular_trace_buffer,
  3484. NULL,
  3485. &setlist,
  3486. &showlist);
  3487. add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
  3488. &trace_buffer_size, _("\
  3489. Set requested size of trace buffer."), _("\
  3490. Show requested size of trace buffer."), _("\
  3491. Use this to choose a size for the trace buffer. Some targets\n\
  3492. may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
  3493. disables any attempt to set the buffer size and lets the target choose."),
  3494. set_trace_buffer_size, NULL,
  3495. &setlist, &showlist);
  3496. add_setshow_string_cmd ("trace-user", class_trace,
  3497. &trace_user, _("\
  3498. Set the user name to use for current and future trace runs."), _("\
  3499. Show the user name to use for current and future trace runs."), NULL,
  3500. set_trace_user, NULL,
  3501. &setlist, &showlist);
  3502. add_setshow_string_cmd ("trace-notes", class_trace,
  3503. &trace_notes, _("\
  3504. Set notes string to use for current and future trace runs."), _("\
  3505. Show the notes string to use for current and future trace runs."), NULL,
  3506. set_trace_notes, NULL,
  3507. &setlist, &showlist);
  3508. add_setshow_string_cmd ("trace-stop-notes", class_trace,
  3509. &trace_stop_notes, _("\
  3510. Set notes string to use for future tstop commands."), _("\
  3511. Show the notes string to use for future tstop commands."), NULL,
  3512. set_trace_stop_notes, NULL,
  3513. &setlist, &showlist);
  3514. }