btrace.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506
  1. /* Branch trace support for GDB, the GNU debugger.
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. Contributed by Intel Corp. <markus.t.metzger@intel.com>
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "btrace.h"
  17. #include "gdbthread.h"
  18. #include "inferior.h"
  19. #include "target.h"
  20. #include "record.h"
  21. #include "symtab.h"
  22. #include "disasm.h"
  23. #include "source.h"
  24. #include "filenames.h"
  25. #include "xml-support.h"
  26. #include "regcache.h"
  27. #include "gdbsupport/rsp-low.h"
  28. #include "gdbcmd.h"
  29. #include "cli/cli-utils.h"
  30. #include "gdbarch.h"
  31. /* For maintenance commands. */
  32. #include "record-btrace.h"
  33. #include <inttypes.h>
  34. #include <ctype.h>
  35. #include <algorithm>
  36. /* Command lists for btrace maintenance commands. */
  37. static struct cmd_list_element *maint_btrace_cmdlist;
  38. static struct cmd_list_element *maint_btrace_set_cmdlist;
  39. static struct cmd_list_element *maint_btrace_show_cmdlist;
  40. static struct cmd_list_element *maint_btrace_pt_set_cmdlist;
  41. static struct cmd_list_element *maint_btrace_pt_show_cmdlist;
  42. /* Control whether to skip PAD packets when computing the packet history. */
  43. static bool maint_btrace_pt_skip_pad = true;
  44. static void btrace_add_pc (struct thread_info *tp);
  45. /* Print a record debug message. Use do ... while (0) to avoid ambiguities
  46. when used in if statements. */
  47. #define DEBUG(msg, args...) \
  48. do \
  49. { \
  50. if (record_debug != 0) \
  51. gdb_printf (gdb_stdlog, \
  52. "[btrace] " msg "\n", ##args); \
  53. } \
  54. while (0)
  55. #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args)
  56. /* Return the function name of a recorded function segment for printing.
  57. This function never returns NULL. */
  58. static const char *
  59. ftrace_print_function_name (const struct btrace_function *bfun)
  60. {
  61. struct minimal_symbol *msym;
  62. struct symbol *sym;
  63. msym = bfun->msym;
  64. sym = bfun->sym;
  65. if (sym != NULL)
  66. return sym->print_name ();
  67. if (msym != NULL)
  68. return msym->print_name ();
  69. return "<unknown>";
  70. }
  71. /* Return the file name of a recorded function segment for printing.
  72. This function never returns NULL. */
  73. static const char *
  74. ftrace_print_filename (const struct btrace_function *bfun)
  75. {
  76. struct symbol *sym;
  77. const char *filename;
  78. sym = bfun->sym;
  79. if (sym != NULL)
  80. filename = symtab_to_filename_for_display (symbol_symtab (sym));
  81. else
  82. filename = "<unknown>";
  83. return filename;
  84. }
  85. /* Return a string representation of the address of an instruction.
  86. This function never returns NULL. */
  87. static const char *
  88. ftrace_print_insn_addr (const struct btrace_insn *insn)
  89. {
  90. if (insn == NULL)
  91. return "<nil>";
  92. return core_addr_to_string_nz (insn->pc);
  93. }
  94. /* Print an ftrace debug status message. */
  95. static void
  96. ftrace_debug (const struct btrace_function *bfun, const char *prefix)
  97. {
  98. const char *fun, *file;
  99. unsigned int ibegin, iend;
  100. int level;
  101. fun = ftrace_print_function_name (bfun);
  102. file = ftrace_print_filename (bfun);
  103. level = bfun->level;
  104. ibegin = bfun->insn_offset;
  105. iend = ibegin + bfun->insn.size ();
  106. DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
  107. prefix, fun, file, level, ibegin, iend);
  108. }
  109. /* Return the number of instructions in a given function call segment. */
  110. static unsigned int
  111. ftrace_call_num_insn (const struct btrace_function* bfun)
  112. {
  113. if (bfun == NULL)
  114. return 0;
  115. /* A gap is always counted as one instruction. */
  116. if (bfun->errcode != 0)
  117. return 1;
  118. return bfun->insn.size ();
  119. }
  120. /* Return the function segment with the given NUMBER or NULL if no such segment
  121. exists. BTINFO is the branch trace information for the current thread. */
  122. static struct btrace_function *
  123. ftrace_find_call_by_number (struct btrace_thread_info *btinfo,
  124. unsigned int number)
  125. {
  126. if (number == 0 || number > btinfo->functions.size ())
  127. return NULL;
  128. return &btinfo->functions[number - 1];
  129. }
  130. /* A const version of the function above. */
  131. static const struct btrace_function *
  132. ftrace_find_call_by_number (const struct btrace_thread_info *btinfo,
  133. unsigned int number)
  134. {
  135. if (number == 0 || number > btinfo->functions.size ())
  136. return NULL;
  137. return &btinfo->functions[number - 1];
  138. }
  139. /* Return non-zero if BFUN does not match MFUN and FUN,
  140. return zero otherwise. */
  141. static int
  142. ftrace_function_switched (const struct btrace_function *bfun,
  143. const struct minimal_symbol *mfun,
  144. const struct symbol *fun)
  145. {
  146. struct minimal_symbol *msym;
  147. struct symbol *sym;
  148. msym = bfun->msym;
  149. sym = bfun->sym;
  150. /* If the minimal symbol changed, we certainly switched functions. */
  151. if (mfun != NULL && msym != NULL
  152. && strcmp (mfun->linkage_name (), msym->linkage_name ()) != 0)
  153. return 1;
  154. /* If the symbol changed, we certainly switched functions. */
  155. if (fun != NULL && sym != NULL)
  156. {
  157. const char *bfname, *fname;
  158. /* Check the function name. */
  159. if (strcmp (fun->linkage_name (), sym->linkage_name ()) != 0)
  160. return 1;
  161. /* Check the location of those functions, as well. */
  162. bfname = symtab_to_fullname (symbol_symtab (sym));
  163. fname = symtab_to_fullname (symbol_symtab (fun));
  164. if (filename_cmp (fname, bfname) != 0)
  165. return 1;
  166. }
  167. /* If we lost symbol information, we switched functions. */
  168. if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
  169. return 1;
  170. /* If we gained symbol information, we switched functions. */
  171. if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
  172. return 1;
  173. return 0;
  174. }
  175. /* Allocate and initialize a new branch trace function segment at the end of
  176. the trace.
  177. BTINFO is the branch trace information for the current thread.
  178. MFUN and FUN are the symbol information we have for this function.
  179. This invalidates all struct btrace_function pointer currently held. */
  180. static struct btrace_function *
  181. ftrace_new_function (struct btrace_thread_info *btinfo,
  182. struct minimal_symbol *mfun,
  183. struct symbol *fun)
  184. {
  185. int level;
  186. unsigned int number, insn_offset;
  187. if (btinfo->functions.empty ())
  188. {
  189. /* Start counting NUMBER and INSN_OFFSET at one. */
  190. level = 0;
  191. number = 1;
  192. insn_offset = 1;
  193. }
  194. else
  195. {
  196. const struct btrace_function *prev = &btinfo->functions.back ();
  197. level = prev->level;
  198. number = prev->number + 1;
  199. insn_offset = prev->insn_offset + ftrace_call_num_insn (prev);
  200. }
  201. btinfo->functions.emplace_back (mfun, fun, number, insn_offset, level);
  202. return &btinfo->functions.back ();
  203. }
  204. /* Update the UP field of a function segment. */
  205. static void
  206. ftrace_update_caller (struct btrace_function *bfun,
  207. struct btrace_function *caller,
  208. btrace_function_flags flags)
  209. {
  210. if (bfun->up != 0)
  211. ftrace_debug (bfun, "updating caller");
  212. bfun->up = caller->number;
  213. bfun->flags = flags;
  214. ftrace_debug (bfun, "set caller");
  215. ftrace_debug (caller, "..to");
  216. }
  217. /* Fix up the caller for all segments of a function. */
  218. static void
  219. ftrace_fixup_caller (struct btrace_thread_info *btinfo,
  220. struct btrace_function *bfun,
  221. struct btrace_function *caller,
  222. btrace_function_flags flags)
  223. {
  224. unsigned int prev, next;
  225. prev = bfun->prev;
  226. next = bfun->next;
  227. ftrace_update_caller (bfun, caller, flags);
  228. /* Update all function segments belonging to the same function. */
  229. for (; prev != 0; prev = bfun->prev)
  230. {
  231. bfun = ftrace_find_call_by_number (btinfo, prev);
  232. ftrace_update_caller (bfun, caller, flags);
  233. }
  234. for (; next != 0; next = bfun->next)
  235. {
  236. bfun = ftrace_find_call_by_number (btinfo, next);
  237. ftrace_update_caller (bfun, caller, flags);
  238. }
  239. }
  240. /* Add a new function segment for a call at the end of the trace.
  241. BTINFO is the branch trace information for the current thread.
  242. MFUN and FUN are the symbol information we have for this function. */
  243. static struct btrace_function *
  244. ftrace_new_call (struct btrace_thread_info *btinfo,
  245. struct minimal_symbol *mfun,
  246. struct symbol *fun)
  247. {
  248. const unsigned int length = btinfo->functions.size ();
  249. struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
  250. bfun->up = length;
  251. bfun->level += 1;
  252. ftrace_debug (bfun, "new call");
  253. return bfun;
  254. }
  255. /* Add a new function segment for a tail call at the end of the trace.
  256. BTINFO is the branch trace information for the current thread.
  257. MFUN and FUN are the symbol information we have for this function. */
  258. static struct btrace_function *
  259. ftrace_new_tailcall (struct btrace_thread_info *btinfo,
  260. struct minimal_symbol *mfun,
  261. struct symbol *fun)
  262. {
  263. const unsigned int length = btinfo->functions.size ();
  264. struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
  265. bfun->up = length;
  266. bfun->level += 1;
  267. bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
  268. ftrace_debug (bfun, "new tail call");
  269. return bfun;
  270. }
  271. /* Return the caller of BFUN or NULL if there is none. This function skips
  272. tail calls in the call chain. BTINFO is the branch trace information for
  273. the current thread. */
  274. static struct btrace_function *
  275. ftrace_get_caller (struct btrace_thread_info *btinfo,
  276. struct btrace_function *bfun)
  277. {
  278. for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
  279. if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
  280. return ftrace_find_call_by_number (btinfo, bfun->up);
  281. return NULL;
  282. }
  283. /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
  284. symbol information. BTINFO is the branch trace information for the current
  285. thread. */
  286. static struct btrace_function *
  287. ftrace_find_caller (struct btrace_thread_info *btinfo,
  288. struct btrace_function *bfun,
  289. struct minimal_symbol *mfun,
  290. struct symbol *fun)
  291. {
  292. for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
  293. {
  294. /* Skip functions with incompatible symbol information. */
  295. if (ftrace_function_switched (bfun, mfun, fun))
  296. continue;
  297. /* This is the function segment we're looking for. */
  298. break;
  299. }
  300. return bfun;
  301. }
  302. /* Find the innermost caller in the back trace of BFUN, skipping all
  303. function segments that do not end with a call instruction (e.g.
  304. tail calls ending with a jump). BTINFO is the branch trace information for
  305. the current thread. */
  306. static struct btrace_function *
  307. ftrace_find_call (struct btrace_thread_info *btinfo,
  308. struct btrace_function *bfun)
  309. {
  310. for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
  311. {
  312. /* Skip gaps. */
  313. if (bfun->errcode != 0)
  314. continue;
  315. btrace_insn &last = bfun->insn.back ();
  316. if (last.iclass == BTRACE_INSN_CALL)
  317. break;
  318. }
  319. return bfun;
  320. }
  321. /* Add a continuation segment for a function into which we return at the end of
  322. the trace.
  323. BTINFO is the branch trace information for the current thread.
  324. MFUN and FUN are the symbol information we have for this function. */
  325. static struct btrace_function *
  326. ftrace_new_return (struct btrace_thread_info *btinfo,
  327. struct minimal_symbol *mfun,
  328. struct symbol *fun)
  329. {
  330. struct btrace_function *prev, *bfun, *caller;
  331. bfun = ftrace_new_function (btinfo, mfun, fun);
  332. prev = ftrace_find_call_by_number (btinfo, bfun->number - 1);
  333. /* It is important to start at PREV's caller. Otherwise, we might find
  334. PREV itself, if PREV is a recursive function. */
  335. caller = ftrace_find_call_by_number (btinfo, prev->up);
  336. caller = ftrace_find_caller (btinfo, caller, mfun, fun);
  337. if (caller != NULL)
  338. {
  339. /* The caller of PREV is the preceding btrace function segment in this
  340. function instance. */
  341. gdb_assert (caller->next == 0);
  342. caller->next = bfun->number;
  343. bfun->prev = caller->number;
  344. /* Maintain the function level. */
  345. bfun->level = caller->level;
  346. /* Maintain the call stack. */
  347. bfun->up = caller->up;
  348. bfun->flags = caller->flags;
  349. ftrace_debug (bfun, "new return");
  350. }
  351. else
  352. {
  353. /* We did not find a caller. This could mean that something went
  354. wrong or that the call is simply not included in the trace. */
  355. /* Let's search for some actual call. */
  356. caller = ftrace_find_call_by_number (btinfo, prev->up);
  357. caller = ftrace_find_call (btinfo, caller);
  358. if (caller == NULL)
  359. {
  360. /* There is no call in PREV's back trace. We assume that the
  361. branch trace did not include it. */
  362. /* Let's find the topmost function and add a new caller for it.
  363. This should handle a series of initial tail calls. */
  364. while (prev->up != 0)
  365. prev = ftrace_find_call_by_number (btinfo, prev->up);
  366. bfun->level = prev->level - 1;
  367. /* Fix up the call stack for PREV. */
  368. ftrace_fixup_caller (btinfo, prev, bfun, BFUN_UP_LINKS_TO_RET);
  369. ftrace_debug (bfun, "new return - no caller");
  370. }
  371. else
  372. {
  373. /* There is a call in PREV's back trace to which we should have
  374. returned but didn't. Let's start a new, separate back trace
  375. from PREV's level. */
  376. bfun->level = prev->level - 1;
  377. /* We fix up the back trace for PREV but leave other function segments
  378. on the same level as they are.
  379. This should handle things like schedule () correctly where we're
  380. switching contexts. */
  381. prev->up = bfun->number;
  382. prev->flags = BFUN_UP_LINKS_TO_RET;
  383. ftrace_debug (bfun, "new return - unknown caller");
  384. }
  385. }
  386. return bfun;
  387. }
  388. /* Add a new function segment for a function switch at the end of the trace.
  389. BTINFO is the branch trace information for the current thread.
  390. MFUN and FUN are the symbol information we have for this function. */
  391. static struct btrace_function *
  392. ftrace_new_switch (struct btrace_thread_info *btinfo,
  393. struct minimal_symbol *mfun,
  394. struct symbol *fun)
  395. {
  396. struct btrace_function *prev, *bfun;
  397. /* This is an unexplained function switch. We can't really be sure about the
  398. call stack, yet the best I can think of right now is to preserve it. */
  399. bfun = ftrace_new_function (btinfo, mfun, fun);
  400. prev = ftrace_find_call_by_number (btinfo, bfun->number - 1);
  401. bfun->up = prev->up;
  402. bfun->flags = prev->flags;
  403. ftrace_debug (bfun, "new switch");
  404. return bfun;
  405. }
  406. /* Add a new function segment for a gap in the trace due to a decode error at
  407. the end of the trace.
  408. BTINFO is the branch trace information for the current thread.
  409. ERRCODE is the format-specific error code. */
  410. static struct btrace_function *
  411. ftrace_new_gap (struct btrace_thread_info *btinfo, int errcode,
  412. std::vector<unsigned int> &gaps)
  413. {
  414. struct btrace_function *bfun;
  415. if (btinfo->functions.empty ())
  416. bfun = ftrace_new_function (btinfo, NULL, NULL);
  417. else
  418. {
  419. /* We hijack the previous function segment if it was empty. */
  420. bfun = &btinfo->functions.back ();
  421. if (bfun->errcode != 0 || !bfun->insn.empty ())
  422. bfun = ftrace_new_function (btinfo, NULL, NULL);
  423. }
  424. bfun->errcode = errcode;
  425. gaps.push_back (bfun->number);
  426. ftrace_debug (bfun, "new gap");
  427. return bfun;
  428. }
  429. /* Update the current function segment at the end of the trace in BTINFO with
  430. respect to the instruction at PC. This may create new function segments.
  431. Return the chronologically latest function segment, never NULL. */
  432. static struct btrace_function *
  433. ftrace_update_function (struct btrace_thread_info *btinfo, CORE_ADDR pc)
  434. {
  435. struct bound_minimal_symbol bmfun;
  436. struct minimal_symbol *mfun;
  437. struct symbol *fun;
  438. struct btrace_function *bfun;
  439. /* Try to determine the function we're in. We use both types of symbols
  440. to avoid surprises when we sometimes get a full symbol and sometimes
  441. only a minimal symbol. */
  442. fun = find_pc_function (pc);
  443. bmfun = lookup_minimal_symbol_by_pc (pc);
  444. mfun = bmfun.minsym;
  445. if (fun == NULL && mfun == NULL)
  446. DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
  447. /* If we didn't have a function, we create one. */
  448. if (btinfo->functions.empty ())
  449. return ftrace_new_function (btinfo, mfun, fun);
  450. /* If we had a gap before, we create a function. */
  451. bfun = &btinfo->functions.back ();
  452. if (bfun->errcode != 0)
  453. return ftrace_new_function (btinfo, mfun, fun);
  454. /* Check the last instruction, if we have one.
  455. We do this check first, since it allows us to fill in the call stack
  456. links in addition to the normal flow links. */
  457. btrace_insn *last = NULL;
  458. if (!bfun->insn.empty ())
  459. last = &bfun->insn.back ();
  460. if (last != NULL)
  461. {
  462. switch (last->iclass)
  463. {
  464. case BTRACE_INSN_RETURN:
  465. {
  466. const char *fname;
  467. /* On some systems, _dl_runtime_resolve returns to the resolved
  468. function instead of jumping to it. From our perspective,
  469. however, this is a tailcall.
  470. If we treated it as return, we wouldn't be able to find the
  471. resolved function in our stack back trace. Hence, we would
  472. lose the current stack back trace and start anew with an empty
  473. back trace. When the resolved function returns, we would then
  474. create a stack back trace with the same function names but
  475. different frame id's. This will confuse stepping. */
  476. fname = ftrace_print_function_name (bfun);
  477. if (strcmp (fname, "_dl_runtime_resolve") == 0)
  478. return ftrace_new_tailcall (btinfo, mfun, fun);
  479. return ftrace_new_return (btinfo, mfun, fun);
  480. }
  481. case BTRACE_INSN_CALL:
  482. /* Ignore calls to the next instruction. They are used for PIC. */
  483. if (last->pc + last->size == pc)
  484. break;
  485. return ftrace_new_call (btinfo, mfun, fun);
  486. case BTRACE_INSN_JUMP:
  487. {
  488. CORE_ADDR start;
  489. start = get_pc_function_start (pc);
  490. /* A jump to the start of a function is (typically) a tail call. */
  491. if (start == pc)
  492. return ftrace_new_tailcall (btinfo, mfun, fun);
  493. /* Some versions of _Unwind_RaiseException use an indirect
  494. jump to 'return' to the exception handler of the caller
  495. handling the exception instead of a return. Let's restrict
  496. this heuristic to that and related functions. */
  497. const char *fname = ftrace_print_function_name (bfun);
  498. if (strncmp (fname, "_Unwind_", strlen ("_Unwind_")) == 0)
  499. {
  500. struct btrace_function *caller
  501. = ftrace_find_call_by_number (btinfo, bfun->up);
  502. caller = ftrace_find_caller (btinfo, caller, mfun, fun);
  503. if (caller != NULL)
  504. return ftrace_new_return (btinfo, mfun, fun);
  505. }
  506. /* If we can't determine the function for PC, we treat a jump at
  507. the end of the block as tail call if we're switching functions
  508. and as an intra-function branch if we don't. */
  509. if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
  510. return ftrace_new_tailcall (btinfo, mfun, fun);
  511. break;
  512. }
  513. }
  514. }
  515. /* Check if we're switching functions for some other reason. */
  516. if (ftrace_function_switched (bfun, mfun, fun))
  517. {
  518. DEBUG_FTRACE ("switching from %s in %s at %s",
  519. ftrace_print_insn_addr (last),
  520. ftrace_print_function_name (bfun),
  521. ftrace_print_filename (bfun));
  522. return ftrace_new_switch (btinfo, mfun, fun);
  523. }
  524. return bfun;
  525. }
  526. /* Add the instruction at PC to BFUN's instructions. */
  527. static void
  528. ftrace_update_insns (struct btrace_function *bfun, const btrace_insn &insn)
  529. {
  530. bfun->insn.push_back (insn);
  531. if (record_debug > 1)
  532. ftrace_debug (bfun, "update insn");
  533. }
  534. /* Classify the instruction at PC. */
  535. static enum btrace_insn_class
  536. ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
  537. {
  538. enum btrace_insn_class iclass;
  539. iclass = BTRACE_INSN_OTHER;
  540. try
  541. {
  542. if (gdbarch_insn_is_call (gdbarch, pc))
  543. iclass = BTRACE_INSN_CALL;
  544. else if (gdbarch_insn_is_ret (gdbarch, pc))
  545. iclass = BTRACE_INSN_RETURN;
  546. else if (gdbarch_insn_is_jump (gdbarch, pc))
  547. iclass = BTRACE_INSN_JUMP;
  548. }
  549. catch (const gdb_exception_error &error)
  550. {
  551. }
  552. return iclass;
  553. }
  554. /* Try to match the back trace at LHS to the back trace at RHS. Returns the
  555. number of matching function segments or zero if the back traces do not
  556. match. BTINFO is the branch trace information for the current thread. */
  557. static int
  558. ftrace_match_backtrace (struct btrace_thread_info *btinfo,
  559. struct btrace_function *lhs,
  560. struct btrace_function *rhs)
  561. {
  562. int matches;
  563. for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
  564. {
  565. if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
  566. return 0;
  567. lhs = ftrace_get_caller (btinfo, lhs);
  568. rhs = ftrace_get_caller (btinfo, rhs);
  569. }
  570. return matches;
  571. }
  572. /* Add ADJUSTMENT to the level of BFUN and succeeding function segments.
  573. BTINFO is the branch trace information for the current thread. */
  574. static void
  575. ftrace_fixup_level (struct btrace_thread_info *btinfo,
  576. struct btrace_function *bfun, int adjustment)
  577. {
  578. if (adjustment == 0)
  579. return;
  580. DEBUG_FTRACE ("fixup level (%+d)", adjustment);
  581. ftrace_debug (bfun, "..bfun");
  582. while (bfun != NULL)
  583. {
  584. bfun->level += adjustment;
  585. bfun = ftrace_find_call_by_number (btinfo, bfun->number + 1);
  586. }
  587. }
  588. /* Recompute the global level offset. Traverse the function trace and compute
  589. the global level offset as the negative of the minimal function level. */
  590. static void
  591. ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
  592. {
  593. int level = INT_MAX;
  594. if (btinfo == NULL)
  595. return;
  596. if (btinfo->functions.empty ())
  597. return;
  598. unsigned int length = btinfo->functions.size() - 1;
  599. for (unsigned int i = 0; i < length; ++i)
  600. level = std::min (level, btinfo->functions[i].level);
  601. /* The last function segment contains the current instruction, which is not
  602. really part of the trace. If it contains just this one instruction, we
  603. ignore the segment. */
  604. struct btrace_function *last = &btinfo->functions.back();
  605. if (last->insn.size () != 1)
  606. level = std::min (level, last->level);
  607. DEBUG_FTRACE ("setting global level offset: %d", -level);
  608. btinfo->level = -level;
  609. }
  610. /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
  611. ftrace_connect_backtrace. BTINFO is the branch trace information for the
  612. current thread. */
  613. static void
  614. ftrace_connect_bfun (struct btrace_thread_info *btinfo,
  615. struct btrace_function *prev,
  616. struct btrace_function *next)
  617. {
  618. DEBUG_FTRACE ("connecting...");
  619. ftrace_debug (prev, "..prev");
  620. ftrace_debug (next, "..next");
  621. /* The function segments are not yet connected. */
  622. gdb_assert (prev->next == 0);
  623. gdb_assert (next->prev == 0);
  624. prev->next = next->number;
  625. next->prev = prev->number;
  626. /* We may have moved NEXT to a different function level. */
  627. ftrace_fixup_level (btinfo, next, prev->level - next->level);
  628. /* If we run out of back trace for one, let's use the other's. */
  629. if (prev->up == 0)
  630. {
  631. const btrace_function_flags flags = next->flags;
  632. next = ftrace_find_call_by_number (btinfo, next->up);
  633. if (next != NULL)
  634. {
  635. DEBUG_FTRACE ("using next's callers");
  636. ftrace_fixup_caller (btinfo, prev, next, flags);
  637. }
  638. }
  639. else if (next->up == 0)
  640. {
  641. const btrace_function_flags flags = prev->flags;
  642. prev = ftrace_find_call_by_number (btinfo, prev->up);
  643. if (prev != NULL)
  644. {
  645. DEBUG_FTRACE ("using prev's callers");
  646. ftrace_fixup_caller (btinfo, next, prev, flags);
  647. }
  648. }
  649. else
  650. {
  651. /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
  652. link to add the tail callers to NEXT's back trace.
  653. This removes NEXT->UP from NEXT's back trace. It will be added back
  654. when connecting NEXT and PREV's callers - provided they exist.
  655. If PREV's back trace consists of a series of tail calls without an
  656. actual call, there will be no further connection and NEXT's caller will
  657. be removed for good. To catch this case, we handle it here and connect
  658. the top of PREV's back trace to NEXT's caller. */
  659. if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
  660. {
  661. struct btrace_function *caller;
  662. btrace_function_flags next_flags, prev_flags;
  663. /* We checked NEXT->UP above so CALLER can't be NULL. */
  664. caller = ftrace_find_call_by_number (btinfo, next->up);
  665. next_flags = next->flags;
  666. prev_flags = prev->flags;
  667. DEBUG_FTRACE ("adding prev's tail calls to next");
  668. prev = ftrace_find_call_by_number (btinfo, prev->up);
  669. ftrace_fixup_caller (btinfo, next, prev, prev_flags);
  670. for (; prev != NULL; prev = ftrace_find_call_by_number (btinfo,
  671. prev->up))
  672. {
  673. /* At the end of PREV's back trace, continue with CALLER. */
  674. if (prev->up == 0)
  675. {
  676. DEBUG_FTRACE ("fixing up link for tailcall chain");
  677. ftrace_debug (prev, "..top");
  678. ftrace_debug (caller, "..up");
  679. ftrace_fixup_caller (btinfo, prev, caller, next_flags);
  680. /* If we skipped any tail calls, this may move CALLER to a
  681. different function level.
  682. Note that changing CALLER's level is only OK because we
  683. know that this is the last iteration of the bottom-to-top
  684. walk in ftrace_connect_backtrace.
  685. Otherwise we will fix up CALLER's level when we connect it
  686. to PREV's caller in the next iteration. */
  687. ftrace_fixup_level (btinfo, caller,
  688. prev->level - caller->level - 1);
  689. break;
  690. }
  691. /* There's nothing to do if we find a real call. */
  692. if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
  693. {
  694. DEBUG_FTRACE ("will fix up link in next iteration");
  695. break;
  696. }
  697. }
  698. }
  699. }
  700. }
  701. /* Connect function segments on the same level in the back trace at LHS and RHS.
  702. The back traces at LHS and RHS are expected to match according to
  703. ftrace_match_backtrace. BTINFO is the branch trace information for the
  704. current thread. */
  705. static void
  706. ftrace_connect_backtrace (struct btrace_thread_info *btinfo,
  707. struct btrace_function *lhs,
  708. struct btrace_function *rhs)
  709. {
  710. while (lhs != NULL && rhs != NULL)
  711. {
  712. struct btrace_function *prev, *next;
  713. gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
  714. /* Connecting LHS and RHS may change the up link. */
  715. prev = lhs;
  716. next = rhs;
  717. lhs = ftrace_get_caller (btinfo, lhs);
  718. rhs = ftrace_get_caller (btinfo, rhs);
  719. ftrace_connect_bfun (btinfo, prev, next);
  720. }
  721. }
  722. /* Bridge the gap between two function segments left and right of a gap if their
  723. respective back traces match in at least MIN_MATCHES functions. BTINFO is
  724. the branch trace information for the current thread.
  725. Returns non-zero if the gap could be bridged, zero otherwise. */
  726. static int
  727. ftrace_bridge_gap (struct btrace_thread_info *btinfo,
  728. struct btrace_function *lhs, struct btrace_function *rhs,
  729. int min_matches)
  730. {
  731. struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
  732. int best_matches;
  733. DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
  734. rhs->insn_offset - 1, min_matches);
  735. best_matches = 0;
  736. best_l = NULL;
  737. best_r = NULL;
  738. /* We search the back traces of LHS and RHS for valid connections and connect
  739. the two function segments that give the longest combined back trace. */
  740. for (cand_l = lhs; cand_l != NULL;
  741. cand_l = ftrace_get_caller (btinfo, cand_l))
  742. for (cand_r = rhs; cand_r != NULL;
  743. cand_r = ftrace_get_caller (btinfo, cand_r))
  744. {
  745. int matches;
  746. matches = ftrace_match_backtrace (btinfo, cand_l, cand_r);
  747. if (best_matches < matches)
  748. {
  749. best_matches = matches;
  750. best_l = cand_l;
  751. best_r = cand_r;
  752. }
  753. }
  754. /* We need at least MIN_MATCHES matches. */
  755. gdb_assert (min_matches > 0);
  756. if (best_matches < min_matches)
  757. return 0;
  758. DEBUG_FTRACE ("..matches: %d", best_matches);
  759. /* We will fix up the level of BEST_R and succeeding function segments such
  760. that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
  761. This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
  762. BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
  763. To catch this, we already fix up the level here where we can start at RHS
  764. instead of at BEST_R. We will ignore the level fixup when connecting
  765. BEST_L to BEST_R as they will already be on the same level. */
  766. ftrace_fixup_level (btinfo, rhs, best_l->level - best_r->level);
  767. ftrace_connect_backtrace (btinfo, best_l, best_r);
  768. return best_matches;
  769. }
  770. /* Try to bridge gaps due to overflow or decode errors by connecting the
  771. function segments that are separated by the gap. */
  772. static void
  773. btrace_bridge_gaps (struct thread_info *tp, std::vector<unsigned int> &gaps)
  774. {
  775. struct btrace_thread_info *btinfo = &tp->btrace;
  776. std::vector<unsigned int> remaining;
  777. int min_matches;
  778. DEBUG ("bridge gaps");
  779. /* We require a minimum amount of matches for bridging a gap. The number of
  780. required matches will be lowered with each iteration.
  781. The more matches the higher our confidence that the bridging is correct.
  782. For big gaps or small traces, however, it may not be feasible to require a
  783. high number of matches. */
  784. for (min_matches = 5; min_matches > 0; --min_matches)
  785. {
  786. /* Let's try to bridge as many gaps as we can. In some cases, we need to
  787. skip a gap and revisit it again after we closed later gaps. */
  788. while (!gaps.empty ())
  789. {
  790. for (const unsigned int number : gaps)
  791. {
  792. struct btrace_function *gap, *lhs, *rhs;
  793. int bridged;
  794. gap = ftrace_find_call_by_number (btinfo, number);
  795. /* We may have a sequence of gaps if we run from one error into
  796. the next as we try to re-sync onto the trace stream. Ignore
  797. all but the leftmost gap in such a sequence.
  798. Also ignore gaps at the beginning of the trace. */
  799. lhs = ftrace_find_call_by_number (btinfo, gap->number - 1);
  800. if (lhs == NULL || lhs->errcode != 0)
  801. continue;
  802. /* Skip gaps to the right. */
  803. rhs = ftrace_find_call_by_number (btinfo, gap->number + 1);
  804. while (rhs != NULL && rhs->errcode != 0)
  805. rhs = ftrace_find_call_by_number (btinfo, rhs->number + 1);
  806. /* Ignore gaps at the end of the trace. */
  807. if (rhs == NULL)
  808. continue;
  809. bridged = ftrace_bridge_gap (btinfo, lhs, rhs, min_matches);
  810. /* Keep track of gaps we were not able to bridge and try again.
  811. If we just pushed them to the end of GAPS we would risk an
  812. infinite loop in case we simply cannot bridge a gap. */
  813. if (bridged == 0)
  814. remaining.push_back (number);
  815. }
  816. /* Let's see if we made any progress. */
  817. if (remaining.size () == gaps.size ())
  818. break;
  819. gaps.clear ();
  820. gaps.swap (remaining);
  821. }
  822. /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
  823. if (gaps.empty ())
  824. break;
  825. remaining.clear ();
  826. }
  827. /* We may omit this in some cases. Not sure it is worth the extra
  828. complication, though. */
  829. ftrace_compute_global_level_offset (btinfo);
  830. }
  831. /* Compute the function branch trace from BTS trace. */
  832. static void
  833. btrace_compute_ftrace_bts (struct thread_info *tp,
  834. const struct btrace_data_bts *btrace,
  835. std::vector<unsigned int> &gaps)
  836. {
  837. /* We may end up doing target calls that require the current thread to be TP,
  838. for example reading memory through gdb_insn_length. Make sure TP is the
  839. current thread. */
  840. scoped_restore_current_thread restore_thread;
  841. switch_to_thread (tp);
  842. struct btrace_thread_info *btinfo;
  843. struct gdbarch *gdbarch;
  844. unsigned int blk;
  845. int level;
  846. gdbarch = target_gdbarch ();
  847. btinfo = &tp->btrace;
  848. blk = btrace->blocks->size ();
  849. if (btinfo->functions.empty ())
  850. level = INT_MAX;
  851. else
  852. level = -btinfo->level;
  853. while (blk != 0)
  854. {
  855. CORE_ADDR pc;
  856. blk -= 1;
  857. const btrace_block &block = btrace->blocks->at (blk);
  858. pc = block.begin;
  859. for (;;)
  860. {
  861. struct btrace_function *bfun;
  862. struct btrace_insn insn;
  863. int size;
  864. /* We should hit the end of the block. Warn if we went too far. */
  865. if (block.end < pc)
  866. {
  867. /* Indicate the gap in the trace. */
  868. bfun = ftrace_new_gap (btinfo, BDE_BTS_OVERFLOW, gaps);
  869. warning (_("Recorded trace may be corrupted at instruction "
  870. "%u (pc = %s)."), bfun->insn_offset - 1,
  871. core_addr_to_string_nz (pc));
  872. break;
  873. }
  874. bfun = ftrace_update_function (btinfo, pc);
  875. /* Maintain the function level offset.
  876. For all but the last block, we do it here. */
  877. if (blk != 0)
  878. level = std::min (level, bfun->level);
  879. size = 0;
  880. try
  881. {
  882. size = gdb_insn_length (gdbarch, pc);
  883. }
  884. catch (const gdb_exception_error &error)
  885. {
  886. }
  887. insn.pc = pc;
  888. insn.size = size;
  889. insn.iclass = ftrace_classify_insn (gdbarch, pc);
  890. insn.flags = 0;
  891. ftrace_update_insns (bfun, insn);
  892. /* We're done once we pushed the instruction at the end. */
  893. if (block.end == pc)
  894. break;
  895. /* We can't continue if we fail to compute the size. */
  896. if (size <= 0)
  897. {
  898. /* Indicate the gap in the trace. We just added INSN so we're
  899. not at the beginning. */
  900. bfun = ftrace_new_gap (btinfo, BDE_BTS_INSN_SIZE, gaps);
  901. warning (_("Recorded trace may be incomplete at instruction %u "
  902. "(pc = %s)."), bfun->insn_offset - 1,
  903. core_addr_to_string_nz (pc));
  904. break;
  905. }
  906. pc += size;
  907. /* Maintain the function level offset.
  908. For the last block, we do it here to not consider the last
  909. instruction.
  910. Since the last instruction corresponds to the current instruction
  911. and is not really part of the execution history, it shouldn't
  912. affect the level. */
  913. if (blk == 0)
  914. level = std::min (level, bfun->level);
  915. }
  916. }
  917. /* LEVEL is the minimal function level of all btrace function segments.
  918. Define the global level offset to -LEVEL so all function levels are
  919. normalized to start at zero. */
  920. btinfo->level = -level;
  921. }
  922. #if defined (HAVE_LIBIPT)
  923. static enum btrace_insn_class
  924. pt_reclassify_insn (enum pt_insn_class iclass)
  925. {
  926. switch (iclass)
  927. {
  928. case ptic_call:
  929. return BTRACE_INSN_CALL;
  930. case ptic_return:
  931. return BTRACE_INSN_RETURN;
  932. case ptic_jump:
  933. return BTRACE_INSN_JUMP;
  934. default:
  935. return BTRACE_INSN_OTHER;
  936. }
  937. }
  938. /* Return the btrace instruction flags for INSN. */
  939. static btrace_insn_flags
  940. pt_btrace_insn_flags (const struct pt_insn &insn)
  941. {
  942. btrace_insn_flags flags = 0;
  943. if (insn.speculative)
  944. flags |= BTRACE_INSN_FLAG_SPECULATIVE;
  945. return flags;
  946. }
  947. /* Return the btrace instruction for INSN. */
  948. static btrace_insn
  949. pt_btrace_insn (const struct pt_insn &insn)
  950. {
  951. return {(CORE_ADDR) insn.ip, (gdb_byte) insn.size,
  952. pt_reclassify_insn (insn.iclass),
  953. pt_btrace_insn_flags (insn)};
  954. }
  955. /* Handle instruction decode events (libipt-v2). */
  956. static int
  957. handle_pt_insn_events (struct btrace_thread_info *btinfo,
  958. struct pt_insn_decoder *decoder,
  959. std::vector<unsigned int> &gaps, int status)
  960. {
  961. #if defined (HAVE_PT_INSN_EVENT)
  962. while (status & pts_event_pending)
  963. {
  964. struct btrace_function *bfun;
  965. struct pt_event event;
  966. uint64_t offset;
  967. status = pt_insn_event (decoder, &event, sizeof (event));
  968. if (status < 0)
  969. break;
  970. switch (event.type)
  971. {
  972. default:
  973. break;
  974. case ptev_enabled:
  975. if (event.status_update != 0)
  976. break;
  977. if (event.variant.enabled.resumed == 0 && !btinfo->functions.empty ())
  978. {
  979. bfun = ftrace_new_gap (btinfo, BDE_PT_DISABLED, gaps);
  980. pt_insn_get_offset (decoder, &offset);
  981. warning (_("Non-contiguous trace at instruction %u (offset = 0x%"
  982. PRIx64 ")."), bfun->insn_offset - 1, offset);
  983. }
  984. break;
  985. case ptev_overflow:
  986. bfun = ftrace_new_gap (btinfo, BDE_PT_OVERFLOW, gaps);
  987. pt_insn_get_offset (decoder, &offset);
  988. warning (_("Overflow at instruction %u (offset = 0x%" PRIx64 ")."),
  989. bfun->insn_offset - 1, offset);
  990. break;
  991. }
  992. }
  993. #endif /* defined (HAVE_PT_INSN_EVENT) */
  994. return status;
  995. }
  996. /* Handle events indicated by flags in INSN (libipt-v1). */
  997. static void
  998. handle_pt_insn_event_flags (struct btrace_thread_info *btinfo,
  999. struct pt_insn_decoder *decoder,
  1000. const struct pt_insn &insn,
  1001. std::vector<unsigned int> &gaps)
  1002. {
  1003. #if defined (HAVE_STRUCT_PT_INSN_ENABLED)
  1004. /* Tracing is disabled and re-enabled each time we enter the kernel. Most
  1005. times, we continue from the same instruction we stopped before. This is
  1006. indicated via the RESUMED instruction flag. The ENABLED instruction flag
  1007. means that we continued from some other instruction. Indicate this as a
  1008. trace gap except when tracing just started. */
  1009. if (insn.enabled && !btinfo->functions.empty ())
  1010. {
  1011. struct btrace_function *bfun;
  1012. uint64_t offset;
  1013. bfun = ftrace_new_gap (btinfo, BDE_PT_DISABLED, gaps);
  1014. pt_insn_get_offset (decoder, &offset);
  1015. warning (_("Non-contiguous trace at instruction %u (offset = 0x%" PRIx64
  1016. ", pc = 0x%" PRIx64 ")."), bfun->insn_offset - 1, offset,
  1017. insn.ip);
  1018. }
  1019. #endif /* defined (HAVE_STRUCT_PT_INSN_ENABLED) */
  1020. #if defined (HAVE_STRUCT_PT_INSN_RESYNCED)
  1021. /* Indicate trace overflows. */
  1022. if (insn.resynced)
  1023. {
  1024. struct btrace_function *bfun;
  1025. uint64_t offset;
  1026. bfun = ftrace_new_gap (btinfo, BDE_PT_OVERFLOW, gaps);
  1027. pt_insn_get_offset (decoder, &offset);
  1028. warning (_("Overflow at instruction %u (offset = 0x%" PRIx64 ", pc = 0x%"
  1029. PRIx64 ")."), bfun->insn_offset - 1, offset, insn.ip);
  1030. }
  1031. #endif /* defined (HAVE_STRUCT_PT_INSN_RESYNCED) */
  1032. }
  1033. /* Add function branch trace to BTINFO using DECODER. */
  1034. static void
  1035. ftrace_add_pt (struct btrace_thread_info *btinfo,
  1036. struct pt_insn_decoder *decoder,
  1037. int *plevel,
  1038. std::vector<unsigned int> &gaps)
  1039. {
  1040. struct btrace_function *bfun;
  1041. uint64_t offset;
  1042. int status;
  1043. for (;;)
  1044. {
  1045. struct pt_insn insn;
  1046. status = pt_insn_sync_forward (decoder);
  1047. if (status < 0)
  1048. {
  1049. if (status != -pte_eos)
  1050. warning (_("Failed to synchronize onto the Intel Processor "
  1051. "Trace stream: %s."), pt_errstr (pt_errcode (status)));
  1052. break;
  1053. }
  1054. for (;;)
  1055. {
  1056. /* Handle events from the previous iteration or synchronization. */
  1057. status = handle_pt_insn_events (btinfo, decoder, gaps, status);
  1058. if (status < 0)
  1059. break;
  1060. status = pt_insn_next (decoder, &insn, sizeof(insn));
  1061. if (status < 0)
  1062. break;
  1063. /* Handle events indicated by flags in INSN. */
  1064. handle_pt_insn_event_flags (btinfo, decoder, insn, gaps);
  1065. bfun = ftrace_update_function (btinfo, insn.ip);
  1066. /* Maintain the function level offset. */
  1067. *plevel = std::min (*plevel, bfun->level);
  1068. ftrace_update_insns (bfun, pt_btrace_insn (insn));
  1069. }
  1070. if (status == -pte_eos)
  1071. break;
  1072. /* Indicate the gap in the trace. */
  1073. bfun = ftrace_new_gap (btinfo, status, gaps);
  1074. pt_insn_get_offset (decoder, &offset);
  1075. warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
  1076. ", pc = 0x%" PRIx64 "): %s."), status, bfun->insn_offset - 1,
  1077. offset, insn.ip, pt_errstr (pt_errcode (status)));
  1078. }
  1079. }
  1080. /* A callback function to allow the trace decoder to read the inferior's
  1081. memory. */
  1082. static int
  1083. btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
  1084. const struct pt_asid *asid, uint64_t pc,
  1085. void *context)
  1086. {
  1087. int result, errcode;
  1088. result = (int) size;
  1089. try
  1090. {
  1091. errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
  1092. if (errcode != 0)
  1093. result = -pte_nomap;
  1094. }
  1095. catch (const gdb_exception_error &error)
  1096. {
  1097. result = -pte_nomap;
  1098. }
  1099. return result;
  1100. }
  1101. /* Translate the vendor from one enum to another. */
  1102. static enum pt_cpu_vendor
  1103. pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
  1104. {
  1105. switch (vendor)
  1106. {
  1107. default:
  1108. return pcv_unknown;
  1109. case CV_INTEL:
  1110. return pcv_intel;
  1111. }
  1112. }
  1113. /* Finalize the function branch trace after decode. */
  1114. static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
  1115. struct thread_info *tp, int level)
  1116. {
  1117. pt_insn_free_decoder (decoder);
  1118. /* LEVEL is the minimal function level of all btrace function segments.
  1119. Define the global level offset to -LEVEL so all function levels are
  1120. normalized to start at zero. */
  1121. tp->btrace.level = -level;
  1122. /* Add a single last instruction entry for the current PC.
  1123. This allows us to compute the backtrace at the current PC using both
  1124. standard unwind and btrace unwind.
  1125. This extra entry is ignored by all record commands. */
  1126. btrace_add_pc (tp);
  1127. }
  1128. /* Compute the function branch trace from Intel Processor Trace
  1129. format. */
  1130. static void
  1131. btrace_compute_ftrace_pt (struct thread_info *tp,
  1132. const struct btrace_data_pt *btrace,
  1133. std::vector<unsigned int> &gaps)
  1134. {
  1135. /* We may end up doing target calls that require the current thread to be TP,
  1136. for example reading memory through btrace_pt_readmem_callback. Make sure
  1137. TP is the current thread. */
  1138. scoped_restore_current_thread restore_thread;
  1139. switch_to_thread (tp);
  1140. struct btrace_thread_info *btinfo;
  1141. struct pt_insn_decoder *decoder;
  1142. struct pt_config config;
  1143. int level, errcode;
  1144. if (btrace->size == 0)
  1145. return;
  1146. btinfo = &tp->btrace;
  1147. if (btinfo->functions.empty ())
  1148. level = INT_MAX;
  1149. else
  1150. level = -btinfo->level;
  1151. pt_config_init(&config);
  1152. config.begin = btrace->data;
  1153. config.end = btrace->data + btrace->size;
  1154. /* We treat an unknown vendor as 'no errata'. */
  1155. if (btrace->config.cpu.vendor != CV_UNKNOWN)
  1156. {
  1157. config.cpu.vendor
  1158. = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
  1159. config.cpu.family = btrace->config.cpu.family;
  1160. config.cpu.model = btrace->config.cpu.model;
  1161. config.cpu.stepping = btrace->config.cpu.stepping;
  1162. errcode = pt_cpu_errata (&config.errata, &config.cpu);
  1163. if (errcode < 0)
  1164. error (_("Failed to configure the Intel Processor Trace "
  1165. "decoder: %s."), pt_errstr (pt_errcode (errcode)));
  1166. }
  1167. decoder = pt_insn_alloc_decoder (&config);
  1168. if (decoder == NULL)
  1169. error (_("Failed to allocate the Intel Processor Trace decoder."));
  1170. try
  1171. {
  1172. struct pt_image *image;
  1173. image = pt_insn_get_image(decoder);
  1174. if (image == NULL)
  1175. error (_("Failed to configure the Intel Processor Trace decoder."));
  1176. errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
  1177. if (errcode < 0)
  1178. error (_("Failed to configure the Intel Processor Trace decoder: "
  1179. "%s."), pt_errstr (pt_errcode (errcode)));
  1180. ftrace_add_pt (btinfo, decoder, &level, gaps);
  1181. }
  1182. catch (const gdb_exception &error)
  1183. {
  1184. /* Indicate a gap in the trace if we quit trace processing. */
  1185. if (error.reason == RETURN_QUIT && !btinfo->functions.empty ())
  1186. ftrace_new_gap (btinfo, BDE_PT_USER_QUIT, gaps);
  1187. btrace_finalize_ftrace_pt (decoder, tp, level);
  1188. throw;
  1189. }
  1190. btrace_finalize_ftrace_pt (decoder, tp, level);
  1191. }
  1192. #else /* defined (HAVE_LIBIPT) */
  1193. static void
  1194. btrace_compute_ftrace_pt (struct thread_info *tp,
  1195. const struct btrace_data_pt *btrace,
  1196. std::vector<unsigned int> &gaps)
  1197. {
  1198. internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
  1199. }
  1200. #endif /* defined (HAVE_LIBIPT) */
  1201. /* Compute the function branch trace from a block branch trace BTRACE for
  1202. a thread given by BTINFO. If CPU is not NULL, overwrite the cpu in the
  1203. branch trace configuration. This is currently only used for the PT
  1204. format. */
  1205. static void
  1206. btrace_compute_ftrace_1 (struct thread_info *tp,
  1207. struct btrace_data *btrace,
  1208. const struct btrace_cpu *cpu,
  1209. std::vector<unsigned int> &gaps)
  1210. {
  1211. DEBUG ("compute ftrace");
  1212. switch (btrace->format)
  1213. {
  1214. case BTRACE_FORMAT_NONE:
  1215. return;
  1216. case BTRACE_FORMAT_BTS:
  1217. btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
  1218. return;
  1219. case BTRACE_FORMAT_PT:
  1220. /* Overwrite the cpu we use for enabling errata workarounds. */
  1221. if (cpu != nullptr)
  1222. btrace->variant.pt.config.cpu = *cpu;
  1223. btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
  1224. return;
  1225. }
  1226. internal_error (__FILE__, __LINE__, _("Unknown branch trace format."));
  1227. }
  1228. static void
  1229. btrace_finalize_ftrace (struct thread_info *tp, std::vector<unsigned int> &gaps)
  1230. {
  1231. if (!gaps.empty ())
  1232. {
  1233. tp->btrace.ngaps += gaps.size ();
  1234. btrace_bridge_gaps (tp, gaps);
  1235. }
  1236. }
  1237. static void
  1238. btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace,
  1239. const struct btrace_cpu *cpu)
  1240. {
  1241. std::vector<unsigned int> gaps;
  1242. try
  1243. {
  1244. btrace_compute_ftrace_1 (tp, btrace, cpu, gaps);
  1245. }
  1246. catch (const gdb_exception &error)
  1247. {
  1248. btrace_finalize_ftrace (tp, gaps);
  1249. throw;
  1250. }
  1251. btrace_finalize_ftrace (tp, gaps);
  1252. }
  1253. /* Add an entry for the current PC. */
  1254. static void
  1255. btrace_add_pc (struct thread_info *tp)
  1256. {
  1257. struct btrace_data btrace;
  1258. struct regcache *regcache;
  1259. CORE_ADDR pc;
  1260. regcache = get_thread_regcache (tp);
  1261. pc = regcache_read_pc (regcache);
  1262. btrace.format = BTRACE_FORMAT_BTS;
  1263. btrace.variant.bts.blocks = new std::vector<btrace_block>;
  1264. btrace.variant.bts.blocks->emplace_back (pc, pc);
  1265. btrace_compute_ftrace (tp, &btrace, NULL);
  1266. }
  1267. /* See btrace.h. */
  1268. void
  1269. btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
  1270. {
  1271. if (tp->btrace.target != NULL)
  1272. error (_("Recording already enabled on thread %s (%s)."),
  1273. print_thread_id (tp), target_pid_to_str (tp->ptid).c_str ());
  1274. #if !defined (HAVE_LIBIPT)
  1275. if (conf->format == BTRACE_FORMAT_PT)
  1276. error (_("Intel Processor Trace support was disabled at compile time."));
  1277. #endif /* !defined (HAVE_LIBIPT) */
  1278. DEBUG ("enable thread %s (%s)", print_thread_id (tp),
  1279. tp->ptid.to_string ().c_str ());
  1280. tp->btrace.target = target_enable_btrace (tp, conf);
  1281. if (tp->btrace.target == NULL)
  1282. error (_("Failed to enable recording on thread %s (%s)."),
  1283. print_thread_id (tp), target_pid_to_str (tp->ptid).c_str ());
  1284. /* We need to undo the enable in case of errors. */
  1285. try
  1286. {
  1287. /* Add an entry for the current PC so we start tracing from where we
  1288. enabled it.
  1289. If we can't access TP's registers, TP is most likely running. In this
  1290. case, we can't really say where tracing was enabled so it should be
  1291. safe to simply skip this step.
  1292. This is not relevant for BTRACE_FORMAT_PT since the trace will already
  1293. start at the PC at which tracing was enabled. */
  1294. if (conf->format != BTRACE_FORMAT_PT
  1295. && can_access_registers_thread (tp))
  1296. btrace_add_pc (tp);
  1297. }
  1298. catch (const gdb_exception &exception)
  1299. {
  1300. btrace_disable (tp);
  1301. throw;
  1302. }
  1303. }
  1304. /* See btrace.h. */
  1305. const struct btrace_config *
  1306. btrace_conf (const struct btrace_thread_info *btinfo)
  1307. {
  1308. if (btinfo->target == NULL)
  1309. return NULL;
  1310. return target_btrace_conf (btinfo->target);
  1311. }
  1312. /* See btrace.h. */
  1313. void
  1314. btrace_disable (struct thread_info *tp)
  1315. {
  1316. struct btrace_thread_info *btp = &tp->btrace;
  1317. if (btp->target == NULL)
  1318. error (_("Recording not enabled on thread %s (%s)."),
  1319. print_thread_id (tp), target_pid_to_str (tp->ptid).c_str ());
  1320. DEBUG ("disable thread %s (%s)", print_thread_id (tp),
  1321. tp->ptid.to_string ().c_str ());
  1322. target_disable_btrace (btp->target);
  1323. btp->target = NULL;
  1324. btrace_clear (tp);
  1325. }
  1326. /* See btrace.h. */
  1327. void
  1328. btrace_teardown (struct thread_info *tp)
  1329. {
  1330. struct btrace_thread_info *btp = &tp->btrace;
  1331. if (btp->target == NULL)
  1332. return;
  1333. DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
  1334. tp->ptid.to_string ().c_str ());
  1335. target_teardown_btrace (btp->target);
  1336. btp->target = NULL;
  1337. btrace_clear (tp);
  1338. }
  1339. /* Stitch branch trace in BTS format. */
  1340. static int
  1341. btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
  1342. {
  1343. struct btrace_thread_info *btinfo;
  1344. struct btrace_function *last_bfun;
  1345. btrace_block *first_new_block;
  1346. btinfo = &tp->btrace;
  1347. gdb_assert (!btinfo->functions.empty ());
  1348. gdb_assert (!btrace->blocks->empty ());
  1349. last_bfun = &btinfo->functions.back ();
  1350. /* If the existing trace ends with a gap, we just glue the traces
  1351. together. We need to drop the last (i.e. chronologically first) block
  1352. of the new trace, though, since we can't fill in the start address.*/
  1353. if (last_bfun->insn.empty ())
  1354. {
  1355. btrace->blocks->pop_back ();
  1356. return 0;
  1357. }
  1358. /* Beware that block trace starts with the most recent block, so the
  1359. chronologically first block in the new trace is the last block in
  1360. the new trace's block vector. */
  1361. first_new_block = &btrace->blocks->back ();
  1362. const btrace_insn &last_insn = last_bfun->insn.back ();
  1363. /* If the current PC at the end of the block is the same as in our current
  1364. trace, there are two explanations:
  1365. 1. we executed the instruction and some branch brought us back.
  1366. 2. we have not made any progress.
  1367. In the first case, the delta trace vector should contain at least two
  1368. entries.
  1369. In the second case, the delta trace vector should contain exactly one
  1370. entry for the partial block containing the current PC. Remove it. */
  1371. if (first_new_block->end == last_insn.pc && btrace->blocks->size () == 1)
  1372. {
  1373. btrace->blocks->pop_back ();
  1374. return 0;
  1375. }
  1376. DEBUG ("stitching %s to %s", ftrace_print_insn_addr (&last_insn),
  1377. core_addr_to_string_nz (first_new_block->end));
  1378. /* Do a simple sanity check to make sure we don't accidentally end up
  1379. with a bad block. This should not occur in practice. */
  1380. if (first_new_block->end < last_insn.pc)
  1381. {
  1382. warning (_("Error while trying to read delta trace. Falling back to "
  1383. "a full read."));
  1384. return -1;
  1385. }
  1386. /* We adjust the last block to start at the end of our current trace. */
  1387. gdb_assert (first_new_block->begin == 0);
  1388. first_new_block->begin = last_insn.pc;
  1389. /* We simply pop the last insn so we can insert it again as part of
  1390. the normal branch trace computation.
  1391. Since instruction iterators are based on indices in the instructions
  1392. vector, we don't leave any pointers dangling. */
  1393. DEBUG ("pruning insn at %s for stitching",
  1394. ftrace_print_insn_addr (&last_insn));
  1395. last_bfun->insn.pop_back ();
  1396. /* The instructions vector may become empty temporarily if this has
  1397. been the only instruction in this function segment.
  1398. This violates the invariant but will be remedied shortly by
  1399. btrace_compute_ftrace when we add the new trace. */
  1400. /* The only case where this would hurt is if the entire trace consisted
  1401. of just that one instruction. If we remove it, we might turn the now
  1402. empty btrace function segment into a gap. But we don't want gaps at
  1403. the beginning. To avoid this, we remove the entire old trace. */
  1404. if (last_bfun->number == 1 && last_bfun->insn.empty ())
  1405. btrace_clear (tp);
  1406. return 0;
  1407. }
  1408. /* Adjust the block trace in order to stitch old and new trace together.
  1409. BTRACE is the new delta trace between the last and the current stop.
  1410. TP is the traced thread.
  1411. May modifx BTRACE as well as the existing trace in TP.
  1412. Return 0 on success, -1 otherwise. */
  1413. static int
  1414. btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
  1415. {
  1416. /* If we don't have trace, there's nothing to do. */
  1417. if (btrace->empty ())
  1418. return 0;
  1419. switch (btrace->format)
  1420. {
  1421. case BTRACE_FORMAT_NONE:
  1422. return 0;
  1423. case BTRACE_FORMAT_BTS:
  1424. return btrace_stitch_bts (&btrace->variant.bts, tp);
  1425. case BTRACE_FORMAT_PT:
  1426. /* Delta reads are not supported. */
  1427. return -1;
  1428. }
  1429. internal_error (__FILE__, __LINE__, _("Unknown branch trace format."));
  1430. }
  1431. /* Clear the branch trace histories in BTINFO. */
  1432. static void
  1433. btrace_clear_history (struct btrace_thread_info *btinfo)
  1434. {
  1435. xfree (btinfo->insn_history);
  1436. xfree (btinfo->call_history);
  1437. xfree (btinfo->replay);
  1438. btinfo->insn_history = NULL;
  1439. btinfo->call_history = NULL;
  1440. btinfo->replay = NULL;
  1441. }
  1442. /* Clear the branch trace maintenance histories in BTINFO. */
  1443. static void
  1444. btrace_maint_clear (struct btrace_thread_info *btinfo)
  1445. {
  1446. switch (btinfo->data.format)
  1447. {
  1448. default:
  1449. break;
  1450. case BTRACE_FORMAT_BTS:
  1451. btinfo->maint.variant.bts.packet_history.begin = 0;
  1452. btinfo->maint.variant.bts.packet_history.end = 0;
  1453. break;
  1454. #if defined (HAVE_LIBIPT)
  1455. case BTRACE_FORMAT_PT:
  1456. delete btinfo->maint.variant.pt.packets;
  1457. btinfo->maint.variant.pt.packets = NULL;
  1458. btinfo->maint.variant.pt.packet_history.begin = 0;
  1459. btinfo->maint.variant.pt.packet_history.end = 0;
  1460. break;
  1461. #endif /* defined (HAVE_LIBIPT) */
  1462. }
  1463. }
  1464. /* See btrace.h. */
  1465. const char *
  1466. btrace_decode_error (enum btrace_format format, int errcode)
  1467. {
  1468. switch (format)
  1469. {
  1470. case BTRACE_FORMAT_BTS:
  1471. switch (errcode)
  1472. {
  1473. case BDE_BTS_OVERFLOW:
  1474. return _("instruction overflow");
  1475. case BDE_BTS_INSN_SIZE:
  1476. return _("unknown instruction");
  1477. default:
  1478. break;
  1479. }
  1480. break;
  1481. #if defined (HAVE_LIBIPT)
  1482. case BTRACE_FORMAT_PT:
  1483. switch (errcode)
  1484. {
  1485. case BDE_PT_USER_QUIT:
  1486. return _("trace decode cancelled");
  1487. case BDE_PT_DISABLED:
  1488. return _("disabled");
  1489. case BDE_PT_OVERFLOW:
  1490. return _("overflow");
  1491. default:
  1492. if (errcode < 0)
  1493. return pt_errstr (pt_errcode (errcode));
  1494. break;
  1495. }
  1496. break;
  1497. #endif /* defined (HAVE_LIBIPT) */
  1498. default:
  1499. break;
  1500. }
  1501. return _("unknown");
  1502. }
  1503. /* See btrace.h. */
  1504. void
  1505. btrace_fetch (struct thread_info *tp, const struct btrace_cpu *cpu)
  1506. {
  1507. struct btrace_thread_info *btinfo;
  1508. struct btrace_target_info *tinfo;
  1509. struct btrace_data btrace;
  1510. int errcode;
  1511. DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
  1512. tp->ptid.to_string ().c_str ());
  1513. btinfo = &tp->btrace;
  1514. tinfo = btinfo->target;
  1515. if (tinfo == NULL)
  1516. return;
  1517. /* There's no way we could get new trace while replaying.
  1518. On the other hand, delta trace would return a partial record with the
  1519. current PC, which is the replay PC, not the last PC, as expected. */
  1520. if (btinfo->replay != NULL)
  1521. return;
  1522. /* With CLI usage, TP is always the current thread when we get here.
  1523. However, since we can also store a gdb.Record object in Python
  1524. referring to a different thread than the current one, we need to
  1525. temporarily set the current thread. */
  1526. scoped_restore_current_thread restore_thread;
  1527. switch_to_thread (tp);
  1528. /* We should not be called on running or exited threads. */
  1529. gdb_assert (can_access_registers_thread (tp));
  1530. /* Let's first try to extend the trace we already have. */
  1531. if (!btinfo->functions.empty ())
  1532. {
  1533. errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
  1534. if (errcode == 0)
  1535. {
  1536. /* Success. Let's try to stitch the traces together. */
  1537. errcode = btrace_stitch_trace (&btrace, tp);
  1538. }
  1539. else
  1540. {
  1541. /* We failed to read delta trace. Let's try to read new trace. */
  1542. errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
  1543. /* If we got any new trace, discard what we have. */
  1544. if (errcode == 0 && !btrace.empty ())
  1545. btrace_clear (tp);
  1546. }
  1547. /* If we were not able to read the trace, we start over. */
  1548. if (errcode != 0)
  1549. {
  1550. btrace_clear (tp);
  1551. errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
  1552. }
  1553. }
  1554. else
  1555. errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
  1556. /* If we were not able to read the branch trace, signal an error. */
  1557. if (errcode != 0)
  1558. error (_("Failed to read branch trace."));
  1559. /* Compute the trace, provided we have any. */
  1560. if (!btrace.empty ())
  1561. {
  1562. /* Store the raw trace data. The stored data will be cleared in
  1563. btrace_clear, so we always append the new trace. */
  1564. btrace_data_append (&btinfo->data, &btrace);
  1565. btrace_maint_clear (btinfo);
  1566. btrace_clear_history (btinfo);
  1567. btrace_compute_ftrace (tp, &btrace, cpu);
  1568. }
  1569. }
  1570. /* See btrace.h. */
  1571. void
  1572. btrace_clear (struct thread_info *tp)
  1573. {
  1574. struct btrace_thread_info *btinfo;
  1575. DEBUG ("clear thread %s (%s)", print_thread_id (tp),
  1576. tp->ptid.to_string ().c_str ());
  1577. /* Make sure btrace frames that may hold a pointer into the branch
  1578. trace data are destroyed. */
  1579. reinit_frame_cache ();
  1580. btinfo = &tp->btrace;
  1581. btinfo->functions.clear ();
  1582. btinfo->ngaps = 0;
  1583. /* Must clear the maint data before - it depends on BTINFO->DATA. */
  1584. btrace_maint_clear (btinfo);
  1585. btinfo->data.clear ();
  1586. btrace_clear_history (btinfo);
  1587. }
  1588. /* See btrace.h. */
  1589. void
  1590. btrace_free_objfile (struct objfile *objfile)
  1591. {
  1592. DEBUG ("free objfile");
  1593. for (thread_info *tp : all_non_exited_threads ())
  1594. btrace_clear (tp);
  1595. }
  1596. #if defined (HAVE_LIBEXPAT)
  1597. /* Check the btrace document version. */
  1598. static void
  1599. check_xml_btrace_version (struct gdb_xml_parser *parser,
  1600. const struct gdb_xml_element *element,
  1601. void *user_data,
  1602. std::vector<gdb_xml_value> &attributes)
  1603. {
  1604. const char *version
  1605. = (const char *) xml_find_attribute (attributes, "version")->value.get ();
  1606. if (strcmp (version, "1.0") != 0)
  1607. gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
  1608. }
  1609. /* Parse a btrace "block" xml record. */
  1610. static void
  1611. parse_xml_btrace_block (struct gdb_xml_parser *parser,
  1612. const struct gdb_xml_element *element,
  1613. void *user_data,
  1614. std::vector<gdb_xml_value> &attributes)
  1615. {
  1616. struct btrace_data *btrace;
  1617. ULONGEST *begin, *end;
  1618. btrace = (struct btrace_data *) user_data;
  1619. switch (btrace->format)
  1620. {
  1621. case BTRACE_FORMAT_BTS:
  1622. break;
  1623. case BTRACE_FORMAT_NONE:
  1624. btrace->format = BTRACE_FORMAT_BTS;
  1625. btrace->variant.bts.blocks = new std::vector<btrace_block>;
  1626. break;
  1627. default:
  1628. gdb_xml_error (parser, _("Btrace format error."));
  1629. }
  1630. begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value.get ();
  1631. end = (ULONGEST *) xml_find_attribute (attributes, "end")->value.get ();
  1632. btrace->variant.bts.blocks->emplace_back (*begin, *end);
  1633. }
  1634. /* Parse a "raw" xml record. */
  1635. static void
  1636. parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
  1637. gdb_byte **pdata, size_t *psize)
  1638. {
  1639. gdb_byte *bin;
  1640. size_t len, size;
  1641. len = strlen (body_text);
  1642. if (len % 2 != 0)
  1643. gdb_xml_error (parser, _("Bad raw data size."));
  1644. size = len / 2;
  1645. gdb::unique_xmalloc_ptr<gdb_byte> data ((gdb_byte *) xmalloc (size));
  1646. bin = data.get ();
  1647. /* We use hex encoding - see gdbsupport/rsp-low.h. */
  1648. while (len > 0)
  1649. {
  1650. char hi, lo;
  1651. hi = *body_text++;
  1652. lo = *body_text++;
  1653. if (hi == 0 || lo == 0)
  1654. gdb_xml_error (parser, _("Bad hex encoding."));
  1655. *bin++ = fromhex (hi) * 16 + fromhex (lo);
  1656. len -= 2;
  1657. }
  1658. *pdata = data.release ();
  1659. *psize = size;
  1660. }
  1661. /* Parse a btrace pt-config "cpu" xml record. */
  1662. static void
  1663. parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
  1664. const struct gdb_xml_element *element,
  1665. void *user_data,
  1666. std::vector<gdb_xml_value> &attributes)
  1667. {
  1668. struct btrace_data *btrace;
  1669. const char *vendor;
  1670. ULONGEST *family, *model, *stepping;
  1671. vendor =
  1672. (const char *) xml_find_attribute (attributes, "vendor")->value.get ();
  1673. family
  1674. = (ULONGEST *) xml_find_attribute (attributes, "family")->value.get ();
  1675. model
  1676. = (ULONGEST *) xml_find_attribute (attributes, "model")->value.get ();
  1677. stepping
  1678. = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value.get ();
  1679. btrace = (struct btrace_data *) user_data;
  1680. if (strcmp (vendor, "GenuineIntel") == 0)
  1681. btrace->variant.pt.config.cpu.vendor = CV_INTEL;
  1682. btrace->variant.pt.config.cpu.family = *family;
  1683. btrace->variant.pt.config.cpu.model = *model;
  1684. btrace->variant.pt.config.cpu.stepping = *stepping;
  1685. }
  1686. /* Parse a btrace pt "raw" xml record. */
  1687. static void
  1688. parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
  1689. const struct gdb_xml_element *element,
  1690. void *user_data, const char *body_text)
  1691. {
  1692. struct btrace_data *btrace;
  1693. btrace = (struct btrace_data *) user_data;
  1694. parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
  1695. &btrace->variant.pt.size);
  1696. }
  1697. /* Parse a btrace "pt" xml record. */
  1698. static void
  1699. parse_xml_btrace_pt (struct gdb_xml_parser *parser,
  1700. const struct gdb_xml_element *element,
  1701. void *user_data,
  1702. std::vector<gdb_xml_value> &attributes)
  1703. {
  1704. struct btrace_data *btrace;
  1705. btrace = (struct btrace_data *) user_data;
  1706. btrace->format = BTRACE_FORMAT_PT;
  1707. btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
  1708. btrace->variant.pt.data = NULL;
  1709. btrace->variant.pt.size = 0;
  1710. }
  1711. static const struct gdb_xml_attribute block_attributes[] = {
  1712. { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  1713. { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  1714. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  1715. };
  1716. static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
  1717. { "vendor", GDB_XML_AF_NONE, NULL, NULL },
  1718. { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  1719. { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  1720. { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  1721. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  1722. };
  1723. static const struct gdb_xml_element btrace_pt_config_children[] = {
  1724. { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
  1725. parse_xml_btrace_pt_config_cpu, NULL },
  1726. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  1727. };
  1728. static const struct gdb_xml_element btrace_pt_children[] = {
  1729. { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
  1730. NULL },
  1731. { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
  1732. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  1733. };
  1734. static const struct gdb_xml_attribute btrace_attributes[] = {
  1735. { "version", GDB_XML_AF_NONE, NULL, NULL },
  1736. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  1737. };
  1738. static const struct gdb_xml_element btrace_children[] = {
  1739. { "block", block_attributes, NULL,
  1740. GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
  1741. { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
  1742. NULL },
  1743. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  1744. };
  1745. static const struct gdb_xml_element btrace_elements[] = {
  1746. { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
  1747. check_xml_btrace_version, NULL },
  1748. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  1749. };
  1750. #endif /* defined (HAVE_LIBEXPAT) */
  1751. /* See btrace.h. */
  1752. void
  1753. parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
  1754. {
  1755. #if defined (HAVE_LIBEXPAT)
  1756. int errcode;
  1757. btrace_data result;
  1758. result.format = BTRACE_FORMAT_NONE;
  1759. errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
  1760. buffer, &result);
  1761. if (errcode != 0)
  1762. error (_("Error parsing branch trace."));
  1763. /* Keep parse results. */
  1764. *btrace = std::move (result);
  1765. #else /* !defined (HAVE_LIBEXPAT) */
  1766. error (_("Cannot process branch trace. XML support was disabled at "
  1767. "compile time."));
  1768. #endif /* !defined (HAVE_LIBEXPAT) */
  1769. }
  1770. #if defined (HAVE_LIBEXPAT)
  1771. /* Parse a btrace-conf "bts" xml record. */
  1772. static void
  1773. parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
  1774. const struct gdb_xml_element *element,
  1775. void *user_data,
  1776. std::vector<gdb_xml_value> &attributes)
  1777. {
  1778. struct btrace_config *conf;
  1779. struct gdb_xml_value *size;
  1780. conf = (struct btrace_config *) user_data;
  1781. conf->format = BTRACE_FORMAT_BTS;
  1782. conf->bts.size = 0;
  1783. size = xml_find_attribute (attributes, "size");
  1784. if (size != NULL)
  1785. conf->bts.size = (unsigned int) *(ULONGEST *) size->value.get ();
  1786. }
  1787. /* Parse a btrace-conf "pt" xml record. */
  1788. static void
  1789. parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
  1790. const struct gdb_xml_element *element,
  1791. void *user_data,
  1792. std::vector<gdb_xml_value> &attributes)
  1793. {
  1794. struct btrace_config *conf;
  1795. struct gdb_xml_value *size;
  1796. conf = (struct btrace_config *) user_data;
  1797. conf->format = BTRACE_FORMAT_PT;
  1798. conf->pt.size = 0;
  1799. size = xml_find_attribute (attributes, "size");
  1800. if (size != NULL)
  1801. conf->pt.size = (unsigned int) *(ULONGEST *) size->value.get ();
  1802. }
  1803. static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
  1804. { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
  1805. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  1806. };
  1807. static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
  1808. { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
  1809. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  1810. };
  1811. static const struct gdb_xml_element btrace_conf_children[] = {
  1812. { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
  1813. parse_xml_btrace_conf_bts, NULL },
  1814. { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
  1815. parse_xml_btrace_conf_pt, NULL },
  1816. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  1817. };
  1818. static const struct gdb_xml_attribute btrace_conf_attributes[] = {
  1819. { "version", GDB_XML_AF_NONE, NULL, NULL },
  1820. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  1821. };
  1822. static const struct gdb_xml_element btrace_conf_elements[] = {
  1823. { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
  1824. GDB_XML_EF_NONE, NULL, NULL },
  1825. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  1826. };
  1827. #endif /* defined (HAVE_LIBEXPAT) */
  1828. /* See btrace.h. */
  1829. void
  1830. parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
  1831. {
  1832. #if defined (HAVE_LIBEXPAT)
  1833. int errcode;
  1834. errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
  1835. btrace_conf_elements, xml, conf);
  1836. if (errcode != 0)
  1837. error (_("Error parsing branch trace configuration."));
  1838. #else /* !defined (HAVE_LIBEXPAT) */
  1839. error (_("Cannot process the branch trace configuration. XML support "
  1840. "was disabled at compile time."));
  1841. #endif /* !defined (HAVE_LIBEXPAT) */
  1842. }
  1843. /* See btrace.h. */
  1844. const struct btrace_insn *
  1845. btrace_insn_get (const struct btrace_insn_iterator *it)
  1846. {
  1847. const struct btrace_function *bfun;
  1848. unsigned int index, end;
  1849. index = it->insn_index;
  1850. bfun = &it->btinfo->functions[it->call_index];
  1851. /* Check if the iterator points to a gap in the trace. */
  1852. if (bfun->errcode != 0)
  1853. return NULL;
  1854. /* The index is within the bounds of this function's instruction vector. */
  1855. end = bfun->insn.size ();
  1856. gdb_assert (0 < end);
  1857. gdb_assert (index < end);
  1858. return &bfun->insn[index];
  1859. }
  1860. /* See btrace.h. */
  1861. int
  1862. btrace_insn_get_error (const struct btrace_insn_iterator *it)
  1863. {
  1864. return it->btinfo->functions[it->call_index].errcode;
  1865. }
  1866. /* See btrace.h. */
  1867. unsigned int
  1868. btrace_insn_number (const struct btrace_insn_iterator *it)
  1869. {
  1870. return it->btinfo->functions[it->call_index].insn_offset + it->insn_index;
  1871. }
  1872. /* See btrace.h. */
  1873. void
  1874. btrace_insn_begin (struct btrace_insn_iterator *it,
  1875. const struct btrace_thread_info *btinfo)
  1876. {
  1877. if (btinfo->functions.empty ())
  1878. error (_("No trace."));
  1879. it->btinfo = btinfo;
  1880. it->call_index = 0;
  1881. it->insn_index = 0;
  1882. }
  1883. /* See btrace.h. */
  1884. void
  1885. btrace_insn_end (struct btrace_insn_iterator *it,
  1886. const struct btrace_thread_info *btinfo)
  1887. {
  1888. const struct btrace_function *bfun;
  1889. unsigned int length;
  1890. if (btinfo->functions.empty ())
  1891. error (_("No trace."));
  1892. bfun = &btinfo->functions.back ();
  1893. length = bfun->insn.size ();
  1894. /* The last function may either be a gap or it contains the current
  1895. instruction, which is one past the end of the execution trace; ignore
  1896. it. */
  1897. if (length > 0)
  1898. length -= 1;
  1899. it->btinfo = btinfo;
  1900. it->call_index = bfun->number - 1;
  1901. it->insn_index = length;
  1902. }
  1903. /* See btrace.h. */
  1904. unsigned int
  1905. btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
  1906. {
  1907. const struct btrace_function *bfun;
  1908. unsigned int index, steps;
  1909. bfun = &it->btinfo->functions[it->call_index];
  1910. steps = 0;
  1911. index = it->insn_index;
  1912. while (stride != 0)
  1913. {
  1914. unsigned int end, space, adv;
  1915. end = bfun->insn.size ();
  1916. /* An empty function segment represents a gap in the trace. We count
  1917. it as one instruction. */
  1918. if (end == 0)
  1919. {
  1920. const struct btrace_function *next;
  1921. next = ftrace_find_call_by_number (it->btinfo, bfun->number + 1);
  1922. if (next == NULL)
  1923. break;
  1924. stride -= 1;
  1925. steps += 1;
  1926. bfun = next;
  1927. index = 0;
  1928. continue;
  1929. }
  1930. gdb_assert (0 < end);
  1931. gdb_assert (index < end);
  1932. /* Compute the number of instructions remaining in this segment. */
  1933. space = end - index;
  1934. /* Advance the iterator as far as possible within this segment. */
  1935. adv = std::min (space, stride);
  1936. stride -= adv;
  1937. index += adv;
  1938. steps += adv;
  1939. /* Move to the next function if we're at the end of this one. */
  1940. if (index == end)
  1941. {
  1942. const struct btrace_function *next;
  1943. next = ftrace_find_call_by_number (it->btinfo, bfun->number + 1);
  1944. if (next == NULL)
  1945. {
  1946. /* We stepped past the last function.
  1947. Let's adjust the index to point to the last instruction in
  1948. the previous function. */
  1949. index -= 1;
  1950. steps -= 1;
  1951. break;
  1952. }
  1953. /* We now point to the first instruction in the new function. */
  1954. bfun = next;
  1955. index = 0;
  1956. }
  1957. /* We did make progress. */
  1958. gdb_assert (adv > 0);
  1959. }
  1960. /* Update the iterator. */
  1961. it->call_index = bfun->number - 1;
  1962. it->insn_index = index;
  1963. return steps;
  1964. }
  1965. /* See btrace.h. */
  1966. unsigned int
  1967. btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
  1968. {
  1969. const struct btrace_function *bfun;
  1970. unsigned int index, steps;
  1971. bfun = &it->btinfo->functions[it->call_index];
  1972. steps = 0;
  1973. index = it->insn_index;
  1974. while (stride != 0)
  1975. {
  1976. unsigned int adv;
  1977. /* Move to the previous function if we're at the start of this one. */
  1978. if (index == 0)
  1979. {
  1980. const struct btrace_function *prev;
  1981. prev = ftrace_find_call_by_number (it->btinfo, bfun->number - 1);
  1982. if (prev == NULL)
  1983. break;
  1984. /* We point to one after the last instruction in the new function. */
  1985. bfun = prev;
  1986. index = bfun->insn.size ();
  1987. /* An empty function segment represents a gap in the trace. We count
  1988. it as one instruction. */
  1989. if (index == 0)
  1990. {
  1991. stride -= 1;
  1992. steps += 1;
  1993. continue;
  1994. }
  1995. }
  1996. /* Advance the iterator as far as possible within this segment. */
  1997. adv = std::min (index, stride);
  1998. stride -= adv;
  1999. index -= adv;
  2000. steps += adv;
  2001. /* We did make progress. */
  2002. gdb_assert (adv > 0);
  2003. }
  2004. /* Update the iterator. */
  2005. it->call_index = bfun->number - 1;
  2006. it->insn_index = index;
  2007. return steps;
  2008. }
  2009. /* See btrace.h. */
  2010. int
  2011. btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
  2012. const struct btrace_insn_iterator *rhs)
  2013. {
  2014. gdb_assert (lhs->btinfo == rhs->btinfo);
  2015. if (lhs->call_index != rhs->call_index)
  2016. return lhs->call_index - rhs->call_index;
  2017. return lhs->insn_index - rhs->insn_index;
  2018. }
  2019. /* See btrace.h. */
  2020. int
  2021. btrace_find_insn_by_number (struct btrace_insn_iterator *it,
  2022. const struct btrace_thread_info *btinfo,
  2023. unsigned int number)
  2024. {
  2025. const struct btrace_function *bfun;
  2026. unsigned int upper, lower;
  2027. if (btinfo->functions.empty ())
  2028. return 0;
  2029. lower = 0;
  2030. bfun = &btinfo->functions[lower];
  2031. if (number < bfun->insn_offset)
  2032. return 0;
  2033. upper = btinfo->functions.size () - 1;
  2034. bfun = &btinfo->functions[upper];
  2035. if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
  2036. return 0;
  2037. /* We assume that there are no holes in the numbering. */
  2038. for (;;)
  2039. {
  2040. const unsigned int average = lower + (upper - lower) / 2;
  2041. bfun = &btinfo->functions[average];
  2042. if (number < bfun->insn_offset)
  2043. {
  2044. upper = average - 1;
  2045. continue;
  2046. }
  2047. if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
  2048. {
  2049. lower = average + 1;
  2050. continue;
  2051. }
  2052. break;
  2053. }
  2054. it->btinfo = btinfo;
  2055. it->call_index = bfun->number - 1;
  2056. it->insn_index = number - bfun->insn_offset;
  2057. return 1;
  2058. }
  2059. /* Returns true if the recording ends with a function segment that
  2060. contains only a single (i.e. the current) instruction. */
  2061. static bool
  2062. btrace_ends_with_single_insn (const struct btrace_thread_info *btinfo)
  2063. {
  2064. const btrace_function *bfun;
  2065. if (btinfo->functions.empty ())
  2066. return false;
  2067. bfun = &btinfo->functions.back ();
  2068. if (bfun->errcode != 0)
  2069. return false;
  2070. return ftrace_call_num_insn (bfun) == 1;
  2071. }
  2072. /* See btrace.h. */
  2073. const struct btrace_function *
  2074. btrace_call_get (const struct btrace_call_iterator *it)
  2075. {
  2076. if (it->index >= it->btinfo->functions.size ())
  2077. return NULL;
  2078. return &it->btinfo->functions[it->index];
  2079. }
  2080. /* See btrace.h. */
  2081. unsigned int
  2082. btrace_call_number (const struct btrace_call_iterator *it)
  2083. {
  2084. const unsigned int length = it->btinfo->functions.size ();
  2085. /* If the last function segment contains only a single instruction (i.e. the
  2086. current instruction), skip it. */
  2087. if ((it->index == length) && btrace_ends_with_single_insn (it->btinfo))
  2088. return length;
  2089. return it->index + 1;
  2090. }
  2091. /* See btrace.h. */
  2092. void
  2093. btrace_call_begin (struct btrace_call_iterator *it,
  2094. const struct btrace_thread_info *btinfo)
  2095. {
  2096. if (btinfo->functions.empty ())
  2097. error (_("No trace."));
  2098. it->btinfo = btinfo;
  2099. it->index = 0;
  2100. }
  2101. /* See btrace.h. */
  2102. void
  2103. btrace_call_end (struct btrace_call_iterator *it,
  2104. const struct btrace_thread_info *btinfo)
  2105. {
  2106. if (btinfo->functions.empty ())
  2107. error (_("No trace."));
  2108. it->btinfo = btinfo;
  2109. it->index = btinfo->functions.size ();
  2110. }
  2111. /* See btrace.h. */
  2112. unsigned int
  2113. btrace_call_next (struct btrace_call_iterator *it, unsigned int stride)
  2114. {
  2115. const unsigned int length = it->btinfo->functions.size ();
  2116. if (it->index + stride < length - 1)
  2117. /* Default case: Simply advance the iterator. */
  2118. it->index += stride;
  2119. else if (it->index + stride == length - 1)
  2120. {
  2121. /* We land exactly at the last function segment. If it contains only one
  2122. instruction (i.e. the current instruction) it is not actually part of
  2123. the trace. */
  2124. if (btrace_ends_with_single_insn (it->btinfo))
  2125. it->index = length;
  2126. else
  2127. it->index = length - 1;
  2128. }
  2129. else
  2130. {
  2131. /* We land past the last function segment and have to adjust the stride.
  2132. If the last function segment contains only one instruction (i.e. the
  2133. current instruction) it is not actually part of the trace. */
  2134. if (btrace_ends_with_single_insn (it->btinfo))
  2135. stride = length - it->index - 1;
  2136. else
  2137. stride = length - it->index;
  2138. it->index = length;
  2139. }
  2140. return stride;
  2141. }
  2142. /* See btrace.h. */
  2143. unsigned int
  2144. btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride)
  2145. {
  2146. const unsigned int length = it->btinfo->functions.size ();
  2147. int steps = 0;
  2148. gdb_assert (it->index <= length);
  2149. if (stride == 0 || it->index == 0)
  2150. return 0;
  2151. /* If we are at the end, the first step is a special case. If the last
  2152. function segment contains only one instruction (i.e. the current
  2153. instruction) it is not actually part of the trace. To be able to step
  2154. over this instruction, we need at least one more function segment. */
  2155. if ((it->index == length) && (length > 1))
  2156. {
  2157. if (btrace_ends_with_single_insn (it->btinfo))
  2158. it->index = length - 2;
  2159. else
  2160. it->index = length - 1;
  2161. steps = 1;
  2162. stride -= 1;
  2163. }
  2164. stride = std::min (stride, it->index);
  2165. it->index -= stride;
  2166. return steps + stride;
  2167. }
  2168. /* See btrace.h. */
  2169. int
  2170. btrace_call_cmp (const struct btrace_call_iterator *lhs,
  2171. const struct btrace_call_iterator *rhs)
  2172. {
  2173. gdb_assert (lhs->btinfo == rhs->btinfo);
  2174. return (int) (lhs->index - rhs->index);
  2175. }
  2176. /* See btrace.h. */
  2177. int
  2178. btrace_find_call_by_number (struct btrace_call_iterator *it,
  2179. const struct btrace_thread_info *btinfo,
  2180. unsigned int number)
  2181. {
  2182. const unsigned int length = btinfo->functions.size ();
  2183. if ((number == 0) || (number > length))
  2184. return 0;
  2185. it->btinfo = btinfo;
  2186. it->index = number - 1;
  2187. return 1;
  2188. }
  2189. /* See btrace.h. */
  2190. void
  2191. btrace_set_insn_history (struct btrace_thread_info *btinfo,
  2192. const struct btrace_insn_iterator *begin,
  2193. const struct btrace_insn_iterator *end)
  2194. {
  2195. if (btinfo->insn_history == NULL)
  2196. btinfo->insn_history = XCNEW (struct btrace_insn_history);
  2197. btinfo->insn_history->begin = *begin;
  2198. btinfo->insn_history->end = *end;
  2199. }
  2200. /* See btrace.h. */
  2201. void
  2202. btrace_set_call_history (struct btrace_thread_info *btinfo,
  2203. const struct btrace_call_iterator *begin,
  2204. const struct btrace_call_iterator *end)
  2205. {
  2206. gdb_assert (begin->btinfo == end->btinfo);
  2207. if (btinfo->call_history == NULL)
  2208. btinfo->call_history = XCNEW (struct btrace_call_history);
  2209. btinfo->call_history->begin = *begin;
  2210. btinfo->call_history->end = *end;
  2211. }
  2212. /* See btrace.h. */
  2213. int
  2214. btrace_is_replaying (struct thread_info *tp)
  2215. {
  2216. return tp->btrace.replay != NULL;
  2217. }
  2218. /* See btrace.h. */
  2219. int
  2220. btrace_is_empty (struct thread_info *tp)
  2221. {
  2222. struct btrace_insn_iterator begin, end;
  2223. struct btrace_thread_info *btinfo;
  2224. btinfo = &tp->btrace;
  2225. if (btinfo->functions.empty ())
  2226. return 1;
  2227. btrace_insn_begin (&begin, btinfo);
  2228. btrace_insn_end (&end, btinfo);
  2229. return btrace_insn_cmp (&begin, &end) == 0;
  2230. }
  2231. #if defined (HAVE_LIBIPT)
  2232. /* Print a single packet. */
  2233. static void
  2234. pt_print_packet (const struct pt_packet *packet)
  2235. {
  2236. switch (packet->type)
  2237. {
  2238. default:
  2239. gdb_printf (("[??: %x]"), packet->type);
  2240. break;
  2241. case ppt_psb:
  2242. gdb_printf (("psb"));
  2243. break;
  2244. case ppt_psbend:
  2245. gdb_printf (("psbend"));
  2246. break;
  2247. case ppt_pad:
  2248. gdb_printf (("pad"));
  2249. break;
  2250. case ppt_tip:
  2251. gdb_printf (("tip %u: 0x%" PRIx64 ""),
  2252. packet->payload.ip.ipc,
  2253. packet->payload.ip.ip);
  2254. break;
  2255. case ppt_tip_pge:
  2256. gdb_printf (("tip.pge %u: 0x%" PRIx64 ""),
  2257. packet->payload.ip.ipc,
  2258. packet->payload.ip.ip);
  2259. break;
  2260. case ppt_tip_pgd:
  2261. gdb_printf (("tip.pgd %u: 0x%" PRIx64 ""),
  2262. packet->payload.ip.ipc,
  2263. packet->payload.ip.ip);
  2264. break;
  2265. case ppt_fup:
  2266. gdb_printf (("fup %u: 0x%" PRIx64 ""),
  2267. packet->payload.ip.ipc,
  2268. packet->payload.ip.ip);
  2269. break;
  2270. case ppt_tnt_8:
  2271. gdb_printf (("tnt-8 %u: 0x%" PRIx64 ""),
  2272. packet->payload.tnt.bit_size,
  2273. packet->payload.tnt.payload);
  2274. break;
  2275. case ppt_tnt_64:
  2276. gdb_printf (("tnt-64 %u: 0x%" PRIx64 ""),
  2277. packet->payload.tnt.bit_size,
  2278. packet->payload.tnt.payload);
  2279. break;
  2280. case ppt_pip:
  2281. gdb_printf (("pip %" PRIx64 "%s"), packet->payload.pip.cr3,
  2282. packet->payload.pip.nr ? (" nr") : (""));
  2283. break;
  2284. case ppt_tsc:
  2285. gdb_printf (("tsc %" PRIx64 ""), packet->payload.tsc.tsc);
  2286. break;
  2287. case ppt_cbr:
  2288. gdb_printf (("cbr %u"), packet->payload.cbr.ratio);
  2289. break;
  2290. case ppt_mode:
  2291. switch (packet->payload.mode.leaf)
  2292. {
  2293. default:
  2294. gdb_printf (("mode %u"), packet->payload.mode.leaf);
  2295. break;
  2296. case pt_mol_exec:
  2297. gdb_printf (("mode.exec%s%s"),
  2298. packet->payload.mode.bits.exec.csl
  2299. ? (" cs.l") : (""),
  2300. packet->payload.mode.bits.exec.csd
  2301. ? (" cs.d") : (""));
  2302. break;
  2303. case pt_mol_tsx:
  2304. gdb_printf (("mode.tsx%s%s"),
  2305. packet->payload.mode.bits.tsx.intx
  2306. ? (" intx") : (""),
  2307. packet->payload.mode.bits.tsx.abrt
  2308. ? (" abrt") : (""));
  2309. break;
  2310. }
  2311. break;
  2312. case ppt_ovf:
  2313. gdb_printf (("ovf"));
  2314. break;
  2315. case ppt_stop:
  2316. gdb_printf (("stop"));
  2317. break;
  2318. case ppt_vmcs:
  2319. gdb_printf (("vmcs %" PRIx64 ""), packet->payload.vmcs.base);
  2320. break;
  2321. case ppt_tma:
  2322. gdb_printf (("tma %x %x"), packet->payload.tma.ctc,
  2323. packet->payload.tma.fc);
  2324. break;
  2325. case ppt_mtc:
  2326. gdb_printf (("mtc %x"), packet->payload.mtc.ctc);
  2327. break;
  2328. case ppt_cyc:
  2329. gdb_printf (("cyc %" PRIx64 ""), packet->payload.cyc.value);
  2330. break;
  2331. case ppt_mnt:
  2332. gdb_printf (("mnt %" PRIx64 ""), packet->payload.mnt.payload);
  2333. break;
  2334. }
  2335. }
  2336. /* Decode packets into MAINT using DECODER. */
  2337. static void
  2338. btrace_maint_decode_pt (struct btrace_maint_info *maint,
  2339. struct pt_packet_decoder *decoder)
  2340. {
  2341. int errcode;
  2342. if (maint->variant.pt.packets == NULL)
  2343. maint->variant.pt.packets = new std::vector<btrace_pt_packet>;
  2344. for (;;)
  2345. {
  2346. struct btrace_pt_packet packet;
  2347. errcode = pt_pkt_sync_forward (decoder);
  2348. if (errcode < 0)
  2349. break;
  2350. for (;;)
  2351. {
  2352. pt_pkt_get_offset (decoder, &packet.offset);
  2353. errcode = pt_pkt_next (decoder, &packet.packet,
  2354. sizeof(packet.packet));
  2355. if (errcode < 0)
  2356. break;
  2357. if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad)
  2358. {
  2359. packet.errcode = pt_errcode (errcode);
  2360. maint->variant.pt.packets->push_back (packet);
  2361. }
  2362. }
  2363. if (errcode == -pte_eos)
  2364. break;
  2365. packet.errcode = pt_errcode (errcode);
  2366. maint->variant.pt.packets->push_back (packet);
  2367. warning (_("Error at trace offset 0x%" PRIx64 ": %s."),
  2368. packet.offset, pt_errstr (packet.errcode));
  2369. }
  2370. if (errcode != -pte_eos)
  2371. warning (_("Failed to synchronize onto the Intel Processor Trace "
  2372. "stream: %s."), pt_errstr (pt_errcode (errcode)));
  2373. }
  2374. /* Update the packet history in BTINFO. */
  2375. static void
  2376. btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo)
  2377. {
  2378. struct pt_packet_decoder *decoder;
  2379. const struct btrace_cpu *cpu;
  2380. struct btrace_data_pt *pt;
  2381. struct pt_config config;
  2382. int errcode;
  2383. pt = &btinfo->data.variant.pt;
  2384. /* Nothing to do if there is no trace. */
  2385. if (pt->size == 0)
  2386. return;
  2387. memset (&config, 0, sizeof(config));
  2388. config.size = sizeof (config);
  2389. config.begin = pt->data;
  2390. config.end = pt->data + pt->size;
  2391. cpu = record_btrace_get_cpu ();
  2392. if (cpu == nullptr)
  2393. cpu = &pt->config.cpu;
  2394. /* We treat an unknown vendor as 'no errata'. */
  2395. if (cpu->vendor != CV_UNKNOWN)
  2396. {
  2397. config.cpu.vendor = pt_translate_cpu_vendor (cpu->vendor);
  2398. config.cpu.family = cpu->family;
  2399. config.cpu.model = cpu->model;
  2400. config.cpu.stepping = cpu->stepping;
  2401. errcode = pt_cpu_errata (&config.errata, &config.cpu);
  2402. if (errcode < 0)
  2403. error (_("Failed to configure the Intel Processor Trace "
  2404. "decoder: %s."), pt_errstr (pt_errcode (errcode)));
  2405. }
  2406. decoder = pt_pkt_alloc_decoder (&config);
  2407. if (decoder == NULL)
  2408. error (_("Failed to allocate the Intel Processor Trace decoder."));
  2409. try
  2410. {
  2411. btrace_maint_decode_pt (&btinfo->maint, decoder);
  2412. }
  2413. catch (const gdb_exception &except)
  2414. {
  2415. pt_pkt_free_decoder (decoder);
  2416. if (except.reason < 0)
  2417. throw;
  2418. }
  2419. pt_pkt_free_decoder (decoder);
  2420. }
  2421. #endif /* !defined (HAVE_LIBIPT) */
  2422. /* Update the packet maintenance information for BTINFO and store the
  2423. low and high bounds into BEGIN and END, respectively.
  2424. Store the current iterator state into FROM and TO. */
  2425. static void
  2426. btrace_maint_update_packets (struct btrace_thread_info *btinfo,
  2427. unsigned int *begin, unsigned int *end,
  2428. unsigned int *from, unsigned int *to)
  2429. {
  2430. switch (btinfo->data.format)
  2431. {
  2432. default:
  2433. *begin = 0;
  2434. *end = 0;
  2435. *from = 0;
  2436. *to = 0;
  2437. break;
  2438. case BTRACE_FORMAT_BTS:
  2439. /* Nothing to do - we operate directly on BTINFO->DATA. */
  2440. *begin = 0;
  2441. *end = btinfo->data.variant.bts.blocks->size ();
  2442. *from = btinfo->maint.variant.bts.packet_history.begin;
  2443. *to = btinfo->maint.variant.bts.packet_history.end;
  2444. break;
  2445. #if defined (HAVE_LIBIPT)
  2446. case BTRACE_FORMAT_PT:
  2447. if (btinfo->maint.variant.pt.packets == nullptr)
  2448. btinfo->maint.variant.pt.packets = new std::vector<btrace_pt_packet>;
  2449. if (btinfo->maint.variant.pt.packets->empty ())
  2450. btrace_maint_update_pt_packets (btinfo);
  2451. *begin = 0;
  2452. *end = btinfo->maint.variant.pt.packets->size ();
  2453. *from = btinfo->maint.variant.pt.packet_history.begin;
  2454. *to = btinfo->maint.variant.pt.packet_history.end;
  2455. break;
  2456. #endif /* defined (HAVE_LIBIPT) */
  2457. }
  2458. }
  2459. /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
  2460. update the current iterator position. */
  2461. static void
  2462. btrace_maint_print_packets (struct btrace_thread_info *btinfo,
  2463. unsigned int begin, unsigned int end)
  2464. {
  2465. switch (btinfo->data.format)
  2466. {
  2467. default:
  2468. break;
  2469. case BTRACE_FORMAT_BTS:
  2470. {
  2471. const std::vector<btrace_block> &blocks
  2472. = *btinfo->data.variant.bts.blocks;
  2473. unsigned int blk;
  2474. for (blk = begin; blk < end; ++blk)
  2475. {
  2476. const btrace_block &block = blocks.at (blk);
  2477. gdb_printf ("%u\tbegin: %s, end: %s\n", blk,
  2478. core_addr_to_string_nz (block.begin),
  2479. core_addr_to_string_nz (block.end));
  2480. }
  2481. btinfo->maint.variant.bts.packet_history.begin = begin;
  2482. btinfo->maint.variant.bts.packet_history.end = end;
  2483. }
  2484. break;
  2485. #if defined (HAVE_LIBIPT)
  2486. case BTRACE_FORMAT_PT:
  2487. {
  2488. const std::vector<btrace_pt_packet> &packets
  2489. = *btinfo->maint.variant.pt.packets;
  2490. unsigned int pkt;
  2491. for (pkt = begin; pkt < end; ++pkt)
  2492. {
  2493. const struct btrace_pt_packet &packet = packets.at (pkt);
  2494. gdb_printf ("%u\t", pkt);
  2495. gdb_printf ("0x%" PRIx64 "\t", packet.offset);
  2496. if (packet.errcode == pte_ok)
  2497. pt_print_packet (&packet.packet);
  2498. else
  2499. gdb_printf ("[error: %s]", pt_errstr (packet.errcode));
  2500. gdb_printf ("\n");
  2501. }
  2502. btinfo->maint.variant.pt.packet_history.begin = begin;
  2503. btinfo->maint.variant.pt.packet_history.end = end;
  2504. }
  2505. break;
  2506. #endif /* defined (HAVE_LIBIPT) */
  2507. }
  2508. }
  2509. /* Read a number from an argument string. */
  2510. static unsigned int
  2511. get_uint (const char **arg)
  2512. {
  2513. const char *begin, *pos;
  2514. char *end;
  2515. unsigned long number;
  2516. begin = *arg;
  2517. pos = skip_spaces (begin);
  2518. if (!isdigit (*pos))
  2519. error (_("Expected positive number, got: %s."), pos);
  2520. number = strtoul (pos, &end, 10);
  2521. if (number > UINT_MAX)
  2522. error (_("Number too big."));
  2523. *arg += (end - begin);
  2524. return (unsigned int) number;
  2525. }
  2526. /* Read a context size from an argument string. */
  2527. static int
  2528. get_context_size (const char **arg)
  2529. {
  2530. const char *pos = skip_spaces (*arg);
  2531. if (!isdigit (*pos))
  2532. error (_("Expected positive number, got: %s."), pos);
  2533. char *end;
  2534. long result = strtol (pos, &end, 10);
  2535. *arg = end;
  2536. return result;
  2537. }
  2538. /* Complain about junk at the end of an argument string. */
  2539. static void
  2540. no_chunk (const char *arg)
  2541. {
  2542. if (*arg != 0)
  2543. error (_("Junk after argument: %s."), arg);
  2544. }
  2545. /* The "maintenance btrace packet-history" command. */
  2546. static void
  2547. maint_btrace_packet_history_cmd (const char *arg, int from_tty)
  2548. {
  2549. struct btrace_thread_info *btinfo;
  2550. unsigned int size, begin, end, from, to;
  2551. thread_info *tp = find_thread_ptid (current_inferior (), inferior_ptid);
  2552. if (tp == NULL)
  2553. error (_("No thread."));
  2554. size = 10;
  2555. btinfo = &tp->btrace;
  2556. btrace_maint_update_packets (btinfo, &begin, &end, &from, &to);
  2557. if (begin == end)
  2558. {
  2559. gdb_printf (_("No trace.\n"));
  2560. return;
  2561. }
  2562. if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
  2563. {
  2564. from = to;
  2565. if (end - from < size)
  2566. size = end - from;
  2567. to = from + size;
  2568. }
  2569. else if (strcmp (arg, "-") == 0)
  2570. {
  2571. to = from;
  2572. if (to - begin < size)
  2573. size = to - begin;
  2574. from = to - size;
  2575. }
  2576. else
  2577. {
  2578. from = get_uint (&arg);
  2579. if (end <= from)
  2580. error (_("'%u' is out of range."), from);
  2581. arg = skip_spaces (arg);
  2582. if (*arg == ',')
  2583. {
  2584. arg = skip_spaces (++arg);
  2585. if (*arg == '+')
  2586. {
  2587. arg += 1;
  2588. size = get_context_size (&arg);
  2589. no_chunk (arg);
  2590. if (end - from < size)
  2591. size = end - from;
  2592. to = from + size;
  2593. }
  2594. else if (*arg == '-')
  2595. {
  2596. arg += 1;
  2597. size = get_context_size (&arg);
  2598. no_chunk (arg);
  2599. /* Include the packet given as first argument. */
  2600. from += 1;
  2601. to = from;
  2602. if (to - begin < size)
  2603. size = to - begin;
  2604. from = to - size;
  2605. }
  2606. else
  2607. {
  2608. to = get_uint (&arg);
  2609. /* Include the packet at the second argument and silently
  2610. truncate the range. */
  2611. if (to < end)
  2612. to += 1;
  2613. else
  2614. to = end;
  2615. no_chunk (arg);
  2616. }
  2617. }
  2618. else
  2619. {
  2620. no_chunk (arg);
  2621. if (end - from < size)
  2622. size = end - from;
  2623. to = from + size;
  2624. }
  2625. dont_repeat ();
  2626. }
  2627. btrace_maint_print_packets (btinfo, from, to);
  2628. }
  2629. /* The "maintenance btrace clear-packet-history" command. */
  2630. static void
  2631. maint_btrace_clear_packet_history_cmd (const char *args, int from_tty)
  2632. {
  2633. if (args != NULL && *args != 0)
  2634. error (_("Invalid argument."));
  2635. if (inferior_ptid == null_ptid)
  2636. error (_("No thread."));
  2637. thread_info *tp = inferior_thread ();
  2638. btrace_thread_info *btinfo = &tp->btrace;
  2639. /* Must clear the maint data before - it depends on BTINFO->DATA. */
  2640. btrace_maint_clear (btinfo);
  2641. btinfo->data.clear ();
  2642. }
  2643. /* The "maintenance btrace clear" command. */
  2644. static void
  2645. maint_btrace_clear_cmd (const char *args, int from_tty)
  2646. {
  2647. if (args != NULL && *args != 0)
  2648. error (_("Invalid argument."));
  2649. if (inferior_ptid == null_ptid)
  2650. error (_("No thread."));
  2651. thread_info *tp = inferior_thread ();
  2652. btrace_clear (tp);
  2653. }
  2654. /* The "maintenance info btrace" command. */
  2655. static void
  2656. maint_info_btrace_cmd (const char *args, int from_tty)
  2657. {
  2658. struct btrace_thread_info *btinfo;
  2659. const struct btrace_config *conf;
  2660. if (args != NULL && *args != 0)
  2661. error (_("Invalid argument."));
  2662. if (inferior_ptid == null_ptid)
  2663. error (_("No thread."));
  2664. thread_info *tp = inferior_thread ();
  2665. btinfo = &tp->btrace;
  2666. conf = btrace_conf (btinfo);
  2667. if (conf == NULL)
  2668. error (_("No btrace configuration."));
  2669. gdb_printf (_("Format: %s.\n"),
  2670. btrace_format_string (conf->format));
  2671. switch (conf->format)
  2672. {
  2673. default:
  2674. break;
  2675. case BTRACE_FORMAT_BTS:
  2676. gdb_printf (_("Number of packets: %zu.\n"),
  2677. btinfo->data.variant.bts.blocks->size ());
  2678. break;
  2679. #if defined (HAVE_LIBIPT)
  2680. case BTRACE_FORMAT_PT:
  2681. {
  2682. struct pt_version version;
  2683. version = pt_library_version ();
  2684. gdb_printf (_("Version: %u.%u.%u%s.\n"), version.major,
  2685. version.minor, version.build,
  2686. version.ext != NULL ? version.ext : "");
  2687. btrace_maint_update_pt_packets (btinfo);
  2688. gdb_printf (_("Number of packets: %zu.\n"),
  2689. ((btinfo->maint.variant.pt.packets == nullptr)
  2690. ? 0 : btinfo->maint.variant.pt.packets->size ()));
  2691. }
  2692. break;
  2693. #endif /* defined (HAVE_LIBIPT) */
  2694. }
  2695. }
  2696. /* The "maint show btrace pt skip-pad" show value function. */
  2697. static void
  2698. show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty,
  2699. struct cmd_list_element *c,
  2700. const char *value)
  2701. {
  2702. gdb_printf (file, _("Skip PAD packets is %s.\n"), value);
  2703. }
  2704. /* Initialize btrace maintenance commands. */
  2705. void _initialize_btrace ();
  2706. void
  2707. _initialize_btrace ()
  2708. {
  2709. add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd,
  2710. _("Info about branch tracing data."), &maintenanceinfolist);
  2711. add_basic_prefix_cmd ("btrace", class_maintenance,
  2712. _("Branch tracing maintenance commands."),
  2713. &maint_btrace_cmdlist, 0, &maintenancelist);
  2714. add_setshow_prefix_cmd ("btrace", class_maintenance,
  2715. _("Set branch tracing specific variables."),
  2716. _("Show branch tracing specific variables."),
  2717. &maint_btrace_set_cmdlist,
  2718. &maint_btrace_show_cmdlist,
  2719. &maintenance_set_cmdlist,
  2720. &maintenance_show_cmdlist);
  2721. add_setshow_prefix_cmd ("pt", class_maintenance,
  2722. _("Set Intel Processor Trace specific variables."),
  2723. _("Show Intel Processor Trace specific variables."),
  2724. &maint_btrace_pt_set_cmdlist,
  2725. &maint_btrace_pt_show_cmdlist,
  2726. &maint_btrace_set_cmdlist,
  2727. &maint_btrace_show_cmdlist);
  2728. add_setshow_boolean_cmd ("skip-pad", class_maintenance,
  2729. &maint_btrace_pt_skip_pad, _("\
  2730. Set whether PAD packets should be skipped in the btrace packet history."), _("\
  2731. Show whether PAD packets should be skipped in the btrace packet history."),_("\
  2732. When enabled, PAD packets are ignored in the btrace packet history."),
  2733. NULL, show_maint_btrace_pt_skip_pad,
  2734. &maint_btrace_pt_set_cmdlist,
  2735. &maint_btrace_pt_show_cmdlist);
  2736. add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd,
  2737. _("Print the raw branch tracing data.\n\
  2738. With no argument, print ten more packets after the previous ten-line print.\n\
  2739. With '-' as argument print ten packets before a previous ten-line print.\n\
  2740. One argument specifies the starting packet of a ten-line print.\n\
  2741. Two arguments with comma between specify starting and ending packets to \
  2742. print.\n\
  2743. Preceded with '+'/'-' the second argument specifies the distance from the \
  2744. first."),
  2745. &maint_btrace_cmdlist);
  2746. add_cmd ("clear-packet-history", class_maintenance,
  2747. maint_btrace_clear_packet_history_cmd,
  2748. _("Clears the branch tracing packet history.\n\
  2749. Discards the raw branch tracing data but not the execution history data."),
  2750. &maint_btrace_cmdlist);
  2751. add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd,
  2752. _("Clears the branch tracing data.\n\
  2753. Discards the raw branch tracing data and the execution history data.\n\
  2754. The next 'record' command will fetch the branch tracing data anew."),
  2755. &maint_btrace_cmdlist);
  2756. }