builtin-trace.c 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786
  1. /*
  2. * builtin-trace.c
  3. *
  4. * Builtin 'trace' command:
  5. *
  6. * Display a continuously updated trace of any workload, CPU, specific PID,
  7. * system wide, etc. Default format is loosely strace like, but any other
  8. * event may be specified using --event.
  9. *
  10. * Copyright (C) 2012, 2013, 2014, 2015 Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  11. *
  12. * Initially based on the 'trace' prototype by Thomas Gleixner:
  13. *
  14. * http://lwn.net/Articles/415728/ ("Announcing a new utility: 'trace'")
  15. */
  16. #include "util/record.h"
  17. #include <api/fs/tracing_path.h>
  18. #ifdef HAVE_LIBBPF_SUPPORT
  19. #include <bpf/bpf.h>
  20. #include <bpf/libbpf.h>
  21. #include <bpf/btf.h>
  22. #endif
  23. #include "util/bpf_map.h"
  24. #include "util/rlimit.h"
  25. #include "builtin.h"
  26. #include "util/cgroup.h"
  27. #include "util/color.h"
  28. #include "util/config.h"
  29. #include "util/debug.h"
  30. #include "util/dso.h"
  31. #include "util/env.h"
  32. #include "util/event.h"
  33. #include "util/evsel.h"
  34. #include "util/evsel_fprintf.h"
  35. #include "util/synthetic-events.h"
  36. #include "util/evlist.h"
  37. #include "util/evswitch.h"
  38. #include "util/hashmap.h"
  39. #include "util/mmap.h"
  40. #include <subcmd/pager.h>
  41. #include <subcmd/exec-cmd.h>
  42. #include "util/machine.h"
  43. #include "util/map.h"
  44. #include "util/symbol.h"
  45. #include "util/path.h"
  46. #include "util/session.h"
  47. #include "util/thread.h"
  48. #include <subcmd/parse-options.h>
  49. #include "util/strlist.h"
  50. #include "util/intlist.h"
  51. #include "util/thread_map.h"
  52. #include "util/stat.h"
  53. #include "util/tool.h"
  54. #include "util/trace.h"
  55. #include "util/util.h"
  56. #include "trace/beauty/beauty.h"
  57. #include "trace-event.h"
  58. #include "util/parse-events.h"
  59. #include "util/tracepoint.h"
  60. #include "callchain.h"
  61. #include "print_binary.h"
  62. #include "string2.h"
  63. #include "syscalltbl.h"
  64. #include "../perf.h"
  65. #include "trace_augment.h"
  66. #include "dwarf-regs.h"
  67. #include <errno.h>
  68. #include <inttypes.h>
  69. #include <poll.h>
  70. #include <signal.h>
  71. #include <stdlib.h>
  72. #include <string.h>
  73. #include <linux/err.h>
  74. #include <linux/filter.h>
  75. #include <linux/kernel.h>
  76. #include <linux/list_sort.h>
  77. #include <linux/random.h>
  78. #include <linux/stringify.h>
  79. #include <linux/time64.h>
  80. #include <linux/zalloc.h>
  81. #include <fcntl.h>
  82. #include <sys/sysmacros.h>
  83. #include <linux/ctype.h>
  84. #include <perf/mmap.h>
  85. #include <tools/libc_compat.h>
  86. #ifdef HAVE_LIBTRACEEVENT
  87. #include <event-parse.h>
  88. #endif
  89. #ifndef O_CLOEXEC
  90. # define O_CLOEXEC 02000000
  91. #endif
  92. #ifndef F_LINUX_SPECIFIC_BASE
  93. # define F_LINUX_SPECIFIC_BASE 1024
  94. #endif
  95. #define RAW_SYSCALL_ARGS_NUM 6
  96. /*
  97. * strtoul: Go from a string to a value, i.e. for msr: MSR_FS_BASE to 0xc0000100
  98. *
  99. * We have to explicitely mark the direction of the flow of data, if from the
  100. * kernel to user space or the other way around, since the BPF collector we
  101. * have so far copies only from user to kernel space, mark the arguments that
  102. * go that direction, so that we don´t end up collecting the previous contents
  103. * for syscall args that goes from kernel to user space.
  104. */
  105. struct syscall_arg_fmt {
  106. size_t (*scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  107. bool (*strtoul)(char *bf, size_t size, struct syscall_arg *arg, u64 *val);
  108. unsigned long (*mask_val)(struct syscall_arg *arg, unsigned long val);
  109. void *parm;
  110. const char *name;
  111. u16 nr_entries; // for arrays
  112. bool from_user;
  113. bool show_zero;
  114. #ifdef HAVE_LIBBPF_SUPPORT
  115. const struct btf_type *type;
  116. int type_id; /* used in btf_dump */
  117. #endif
  118. };
  119. struct syscall_fmt {
  120. const char *name;
  121. const char *alias;
  122. struct {
  123. const char *sys_enter,
  124. *sys_exit;
  125. } bpf_prog_name;
  126. struct syscall_arg_fmt arg[RAW_SYSCALL_ARGS_NUM];
  127. u8 nr_args;
  128. bool errpid;
  129. bool timeout;
  130. bool hexret;
  131. };
  132. struct trace {
  133. struct perf_env host_env;
  134. struct perf_tool tool;
  135. struct {
  136. /** Sorted sycall numbers used by the trace. */
  137. struct syscall **table;
  138. /** Size of table. */
  139. size_t table_size;
  140. struct {
  141. struct evsel *sys_enter,
  142. *sys_exit,
  143. *bpf_output;
  144. } events;
  145. } syscalls;
  146. #ifdef HAVE_LIBBPF_SUPPORT
  147. struct btf *btf;
  148. #endif
  149. struct record_opts opts;
  150. struct evlist *evlist;
  151. struct machine *host;
  152. struct thread *current;
  153. struct cgroup *cgroup;
  154. u64 base_time;
  155. FILE *output;
  156. unsigned long nr_events;
  157. unsigned long nr_events_printed;
  158. unsigned long max_events;
  159. struct evswitch evswitch;
  160. struct strlist *ev_qualifier;
  161. struct {
  162. size_t nr;
  163. int *entries;
  164. } ev_qualifier_ids;
  165. struct {
  166. size_t nr;
  167. pid_t *entries;
  168. struct bpf_map *map;
  169. } filter_pids;
  170. /*
  171. * TODO: The map is from an ID (aka system call number) to struct
  172. * syscall_stats. If there is >1 e_machine, such as i386 and x86-64
  173. * processes, then the stats here will gather wrong the statistics for
  174. * the non EM_HOST system calls. A fix would be to add the e_machine
  175. * into the key, but this would make the code inconsistent with the
  176. * per-thread version.
  177. */
  178. struct hashmap *syscall_stats;
  179. double duration_filter;
  180. double runtime_ms;
  181. unsigned long pfmaj, pfmin;
  182. struct {
  183. u64 vfs_getname,
  184. proc_getname;
  185. } stats;
  186. unsigned int max_stack;
  187. unsigned int min_stack;
  188. enum trace_summary_mode summary_mode;
  189. int max_summary;
  190. int raw_augmented_syscalls_args_size;
  191. bool raw_augmented_syscalls;
  192. bool fd_path_disabled;
  193. bool sort_events;
  194. bool not_ev_qualifier;
  195. bool live;
  196. bool full_time;
  197. bool sched;
  198. bool multiple_threads;
  199. bool summary;
  200. bool summary_only;
  201. bool errno_summary;
  202. bool failure_only;
  203. bool show_comm;
  204. bool print_sample;
  205. bool show_tool_stats;
  206. bool trace_syscalls;
  207. bool libtraceevent_print;
  208. bool kernel_syscallchains;
  209. s16 args_alignment;
  210. bool show_tstamp;
  211. bool show_duration;
  212. bool show_zeros;
  213. bool show_arg_names;
  214. bool show_string_prefix;
  215. bool force;
  216. bool vfs_getname;
  217. bool force_btf;
  218. bool summary_bpf;
  219. int trace_pgfaults;
  220. char *perfconfig_events;
  221. struct {
  222. struct ordered_events data;
  223. u64 last;
  224. } oe;
  225. const char *uid_str;
  226. };
  227. static void trace__load_vmlinux_btf(struct trace *trace __maybe_unused)
  228. {
  229. #ifdef HAVE_LIBBPF_SUPPORT
  230. if (trace->btf != NULL)
  231. return;
  232. trace->btf = btf__load_vmlinux_btf();
  233. if (verbose > 0) {
  234. fprintf(trace->output, trace->btf ? "vmlinux BTF loaded\n" :
  235. "Failed to load vmlinux BTF\n");
  236. }
  237. #endif
  238. }
  239. struct tp_field {
  240. int offset;
  241. union {
  242. u64 (*integer)(struct tp_field *field, struct perf_sample *sample);
  243. void *(*pointer)(struct tp_field *field, struct perf_sample *sample);
  244. };
  245. };
  246. #define TP_UINT_FIELD(bits) \
  247. static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
  248. { \
  249. u##bits value; \
  250. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  251. return value; \
  252. }
  253. TP_UINT_FIELD(8);
  254. TP_UINT_FIELD(16);
  255. TP_UINT_FIELD(32);
  256. TP_UINT_FIELD(64);
  257. #define TP_UINT_FIELD__SWAPPED(bits) \
  258. static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
  259. { \
  260. u##bits value; \
  261. memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
  262. return bswap_##bits(value);\
  263. }
  264. TP_UINT_FIELD__SWAPPED(16);
  265. TP_UINT_FIELD__SWAPPED(32);
  266. TP_UINT_FIELD__SWAPPED(64);
  267. static int __tp_field__init_uint(struct tp_field *field, int size, int offset, bool needs_swap)
  268. {
  269. field->offset = offset;
  270. switch (size) {
  271. case 1:
  272. field->integer = tp_field__u8;
  273. break;
  274. case 2:
  275. field->integer = needs_swap ? tp_field__swapped_u16 : tp_field__u16;
  276. break;
  277. case 4:
  278. field->integer = needs_swap ? tp_field__swapped_u32 : tp_field__u32;
  279. break;
  280. case 8:
  281. field->integer = needs_swap ? tp_field__swapped_u64 : tp_field__u64;
  282. break;
  283. default:
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. static int tp_field__init_uint(struct tp_field *field, struct tep_format_field *format_field, bool needs_swap)
  289. {
  290. return __tp_field__init_uint(field, format_field->size, format_field->offset, needs_swap);
  291. }
  292. static void *tp_field__ptr(struct tp_field *field, struct perf_sample *sample)
  293. {
  294. return sample->raw_data + field->offset;
  295. }
  296. static int __tp_field__init_ptr(struct tp_field *field, int offset)
  297. {
  298. field->offset = offset;
  299. field->pointer = tp_field__ptr;
  300. return 0;
  301. }
  302. static int tp_field__init_ptr(struct tp_field *field, struct tep_format_field *format_field)
  303. {
  304. return __tp_field__init_ptr(field, format_field->offset);
  305. }
  306. struct syscall_tp {
  307. struct tp_field id;
  308. union {
  309. struct tp_field args, ret;
  310. };
  311. };
  312. /*
  313. * The evsel->priv as used by 'perf trace'
  314. * sc: for raw_syscalls:sys_{enter,exit} and syscalls:sys_{enter,exit}_SYSCALLNAME
  315. * fmt: for all the other tracepoints
  316. */
  317. struct evsel_trace {
  318. struct syscall_tp sc;
  319. struct syscall_arg_fmt *fmt;
  320. };
  321. static struct evsel_trace *evsel_trace__new(void)
  322. {
  323. return zalloc(sizeof(struct evsel_trace));
  324. }
  325. static void evsel_trace__delete(struct evsel_trace *et)
  326. {
  327. if (et == NULL)
  328. return;
  329. zfree(&et->fmt);
  330. free(et);
  331. }
  332. /*
  333. * Used with raw_syscalls:sys_{enter,exit} and with the
  334. * syscalls:sys_{enter,exit}_SYSCALL tracepoints
  335. */
  336. static inline struct syscall_tp *__evsel__syscall_tp(struct evsel *evsel)
  337. {
  338. struct evsel_trace *et = evsel->priv;
  339. return &et->sc;
  340. }
  341. static struct syscall_tp *evsel__syscall_tp(struct evsel *evsel)
  342. {
  343. if (evsel->priv == NULL) {
  344. evsel->priv = evsel_trace__new();
  345. if (evsel->priv == NULL)
  346. return NULL;
  347. }
  348. return __evsel__syscall_tp(evsel);
  349. }
  350. /*
  351. * Used with all the other tracepoints.
  352. */
  353. static inline struct syscall_arg_fmt *__evsel__syscall_arg_fmt(struct evsel *evsel)
  354. {
  355. struct evsel_trace *et = evsel->priv;
  356. return et->fmt;
  357. }
  358. static struct syscall_arg_fmt *evsel__syscall_arg_fmt(struct evsel *evsel)
  359. {
  360. struct evsel_trace *et = evsel->priv;
  361. if (evsel->priv == NULL) {
  362. et = evsel->priv = evsel_trace__new();
  363. if (et == NULL)
  364. return NULL;
  365. }
  366. if (et->fmt == NULL) {
  367. const struct tep_event *tp_format = evsel__tp_format(evsel);
  368. if (tp_format == NULL)
  369. goto out_delete;
  370. et->fmt = calloc(tp_format->format.nr_fields, sizeof(struct syscall_arg_fmt));
  371. if (et->fmt == NULL)
  372. goto out_delete;
  373. }
  374. return __evsel__syscall_arg_fmt(evsel);
  375. out_delete:
  376. evsel_trace__delete(evsel->priv);
  377. evsel->priv = NULL;
  378. return NULL;
  379. }
  380. static int evsel__init_tp_uint_field(struct evsel *evsel, struct tp_field *field, const char *name)
  381. {
  382. struct tep_format_field *format_field = evsel__field(evsel, name);
  383. if (format_field == NULL)
  384. return -1;
  385. return tp_field__init_uint(field, format_field, evsel->needs_swap);
  386. }
  387. #define perf_evsel__init_sc_tp_uint_field(evsel, name) \
  388. ({ struct syscall_tp *sc = __evsel__syscall_tp(evsel);\
  389. evsel__init_tp_uint_field(evsel, &sc->name, #name); })
  390. static int evsel__init_tp_ptr_field(struct evsel *evsel, struct tp_field *field, const char *name)
  391. {
  392. struct tep_format_field *format_field = evsel__field(evsel, name);
  393. if (format_field == NULL)
  394. return -1;
  395. return tp_field__init_ptr(field, format_field);
  396. }
  397. #define perf_evsel__init_sc_tp_ptr_field(evsel, name) \
  398. ({ struct syscall_tp *sc = __evsel__syscall_tp(evsel);\
  399. evsel__init_tp_ptr_field(evsel, &sc->name, #name); })
  400. static void evsel__delete_priv(struct evsel *evsel)
  401. {
  402. zfree(&evsel->priv);
  403. evsel__delete(evsel);
  404. }
  405. static int evsel__init_syscall_tp(struct evsel *evsel)
  406. {
  407. struct syscall_tp *sc = evsel__syscall_tp(evsel);
  408. if (sc != NULL) {
  409. if (evsel__init_tp_uint_field(evsel, &sc->id, "__syscall_nr") &&
  410. evsel__init_tp_uint_field(evsel, &sc->id, "nr"))
  411. return -ENOENT;
  412. return 0;
  413. }
  414. return -ENOMEM;
  415. }
  416. static int evsel__init_augmented_syscall_tp(struct evsel *evsel, struct evsel *tp)
  417. {
  418. struct syscall_tp *sc = evsel__syscall_tp(evsel);
  419. if (sc != NULL) {
  420. struct tep_format_field *syscall_id = evsel__field(tp, "id");
  421. if (syscall_id == NULL)
  422. syscall_id = evsel__field(tp, "__syscall_nr");
  423. if (syscall_id == NULL ||
  424. __tp_field__init_uint(&sc->id, syscall_id->size, syscall_id->offset, evsel->needs_swap))
  425. return -EINVAL;
  426. return 0;
  427. }
  428. return -ENOMEM;
  429. }
  430. static int evsel__init_augmented_syscall_tp_args(struct evsel *evsel)
  431. {
  432. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  433. return __tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64));
  434. }
  435. static int evsel__init_augmented_syscall_tp_ret(struct evsel *evsel)
  436. {
  437. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  438. return __tp_field__init_uint(&sc->ret, sizeof(u64), sc->id.offset + sizeof(u64), evsel->needs_swap);
  439. }
  440. static int evsel__init_raw_syscall_tp(struct evsel *evsel, void *handler)
  441. {
  442. if (evsel__syscall_tp(evsel) != NULL) {
  443. if (perf_evsel__init_sc_tp_uint_field(evsel, id))
  444. return -ENOENT;
  445. evsel->handler = handler;
  446. return 0;
  447. }
  448. return -ENOMEM;
  449. }
  450. static struct evsel *perf_evsel__raw_syscall_newtp(const char *direction, void *handler)
  451. {
  452. struct evsel *evsel = evsel__newtp("raw_syscalls", direction);
  453. /* older kernel (e.g., RHEL6) use syscalls:{enter,exit} */
  454. if (IS_ERR(evsel))
  455. evsel = evsel__newtp("syscalls", direction);
  456. if (IS_ERR(evsel))
  457. return NULL;
  458. if (evsel__init_raw_syscall_tp(evsel, handler))
  459. goto out_delete;
  460. return evsel;
  461. out_delete:
  462. evsel__delete_priv(evsel);
  463. return NULL;
  464. }
  465. #define perf_evsel__sc_tp_uint(evsel, name, sample) \
  466. ({ struct syscall_tp *fields = __evsel__syscall_tp(evsel); \
  467. fields->name.integer(&fields->name, sample); })
  468. #define perf_evsel__sc_tp_ptr(evsel, name, sample) \
  469. ({ struct syscall_tp *fields = __evsel__syscall_tp(evsel); \
  470. fields->name.pointer(&fields->name, sample); })
  471. size_t strarray__scnprintf_suffix(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_suffix, int val)
  472. {
  473. int idx = val - sa->offset;
  474. if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
  475. size_t printed = scnprintf(bf, size, intfmt, val);
  476. if (show_suffix)
  477. printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
  478. return printed;
  479. }
  480. return scnprintf(bf, size, "%s%s", sa->entries[idx], show_suffix ? sa->prefix : "");
  481. }
  482. size_t strarray__scnprintf(struct strarray *sa, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
  483. {
  484. int idx = val - sa->offset;
  485. if (idx < 0 || idx >= sa->nr_entries || sa->entries[idx] == NULL) {
  486. size_t printed = scnprintf(bf, size, intfmt, val);
  487. if (show_prefix)
  488. printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sa->prefix);
  489. return printed;
  490. }
  491. return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
  492. }
  493. static size_t __syscall_arg__scnprintf_strarray(char *bf, size_t size,
  494. const char *intfmt,
  495. struct syscall_arg *arg)
  496. {
  497. return strarray__scnprintf(arg->parm, bf, size, intfmt, arg->show_string_prefix, arg->val);
  498. }
  499. static size_t syscall_arg__scnprintf_strarray(char *bf, size_t size,
  500. struct syscall_arg *arg)
  501. {
  502. return __syscall_arg__scnprintf_strarray(bf, size, "%d", arg);
  503. }
  504. #define SCA_STRARRAY syscall_arg__scnprintf_strarray
  505. bool syscall_arg__strtoul_strarray(char *bf, size_t size, struct syscall_arg *arg, u64 *ret)
  506. {
  507. return strarray__strtoul(arg->parm, bf, size, ret);
  508. }
  509. bool syscall_arg__strtoul_strarray_flags(char *bf, size_t size, struct syscall_arg *arg, u64 *ret)
  510. {
  511. return strarray__strtoul_flags(arg->parm, bf, size, ret);
  512. }
  513. bool syscall_arg__strtoul_strarrays(char *bf, size_t size, struct syscall_arg *arg, u64 *ret)
  514. {
  515. return strarrays__strtoul(arg->parm, bf, size, ret);
  516. }
  517. size_t syscall_arg__scnprintf_strarray_flags(char *bf, size_t size, struct syscall_arg *arg)
  518. {
  519. return strarray__scnprintf_flags(arg->parm, bf, size, arg->show_string_prefix, arg->val);
  520. }
  521. size_t strarrays__scnprintf(struct strarrays *sas, char *bf, size_t size, const char *intfmt, bool show_prefix, int val)
  522. {
  523. size_t printed;
  524. int i;
  525. for (i = 0; i < sas->nr_entries; ++i) {
  526. struct strarray *sa = sas->entries[i];
  527. int idx = val - sa->offset;
  528. if (idx >= 0 && idx < sa->nr_entries) {
  529. if (sa->entries[idx] == NULL)
  530. break;
  531. return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", sa->entries[idx]);
  532. }
  533. }
  534. printed = scnprintf(bf, size, intfmt, val);
  535. if (show_prefix)
  536. printed += scnprintf(bf + printed, size - printed, " /* %s??? */", sas->entries[0]->prefix);
  537. return printed;
  538. }
  539. bool strarray__strtoul(struct strarray *sa, char *bf, size_t size, u64 *ret)
  540. {
  541. int i;
  542. for (i = 0; i < sa->nr_entries; ++i) {
  543. if (sa->entries[i] && strncmp(sa->entries[i], bf, size) == 0 && sa->entries[i][size] == '\0') {
  544. *ret = sa->offset + i;
  545. return true;
  546. }
  547. }
  548. return false;
  549. }
  550. bool strarray__strtoul_flags(struct strarray *sa, char *bf, size_t size, u64 *ret)
  551. {
  552. u64 val = 0;
  553. char *tok = bf, *sep, *end;
  554. *ret = 0;
  555. while (size != 0) {
  556. int toklen = size;
  557. sep = memchr(tok, '|', size);
  558. if (sep != NULL) {
  559. size -= sep - tok + 1;
  560. end = sep - 1;
  561. while (end > tok && isspace(*end))
  562. --end;
  563. toklen = end - tok + 1;
  564. }
  565. while (isspace(*tok))
  566. ++tok;
  567. if (isalpha(*tok) || *tok == '_') {
  568. if (!strarray__strtoul(sa, tok, toklen, &val))
  569. return false;
  570. } else
  571. val = strtoul(tok, NULL, 0);
  572. *ret |= (1 << (val - 1));
  573. if (sep == NULL)
  574. break;
  575. tok = sep + 1;
  576. }
  577. return true;
  578. }
  579. bool strarrays__strtoul(struct strarrays *sas, char *bf, size_t size, u64 *ret)
  580. {
  581. int i;
  582. for (i = 0; i < sas->nr_entries; ++i) {
  583. struct strarray *sa = sas->entries[i];
  584. if (strarray__strtoul(sa, bf, size, ret))
  585. return true;
  586. }
  587. return false;
  588. }
  589. size_t syscall_arg__scnprintf_strarrays(char *bf, size_t size,
  590. struct syscall_arg *arg)
  591. {
  592. return strarrays__scnprintf(arg->parm, bf, size, "%d", arg->show_string_prefix, arg->val);
  593. }
  594. #ifndef AT_FDCWD
  595. #define AT_FDCWD -100
  596. #endif
  597. static size_t syscall_arg__scnprintf_fd_at(char *bf, size_t size,
  598. struct syscall_arg *arg)
  599. {
  600. int fd = arg->val;
  601. const char *prefix = "AT_FD";
  602. if (fd == AT_FDCWD)
  603. return scnprintf(bf, size, "%s%s", arg->show_string_prefix ? prefix : "", "CWD");
  604. return syscall_arg__scnprintf_fd(bf, size, arg);
  605. }
  606. #define SCA_FDAT syscall_arg__scnprintf_fd_at
  607. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  608. struct syscall_arg *arg);
  609. #define SCA_CLOSE_FD syscall_arg__scnprintf_close_fd
  610. size_t syscall_arg__scnprintf_hex(char *bf, size_t size, struct syscall_arg *arg)
  611. {
  612. return scnprintf(bf, size, "%#lx", arg->val);
  613. }
  614. size_t syscall_arg__scnprintf_ptr(char *bf, size_t size, struct syscall_arg *arg)
  615. {
  616. if (arg->val == 0)
  617. return scnprintf(bf, size, "NULL");
  618. return syscall_arg__scnprintf_hex(bf, size, arg);
  619. }
  620. size_t syscall_arg__scnprintf_int(char *bf, size_t size, struct syscall_arg *arg)
  621. {
  622. return scnprintf(bf, size, "%d", arg->val);
  623. }
  624. size_t syscall_arg__scnprintf_long(char *bf, size_t size, struct syscall_arg *arg)
  625. {
  626. return scnprintf(bf, size, "%ld", arg->val);
  627. }
  628. static size_t syscall_arg__scnprintf_char_array(char *bf, size_t size, struct syscall_arg *arg)
  629. {
  630. // XXX Hey, maybe for sched:sched_switch prev/next comm fields we can
  631. // fill missing comms using thread__set_comm()...
  632. // here or in a special syscall_arg__scnprintf_pid_sched_tp...
  633. return scnprintf(bf, size, "\"%-.*s\"", arg->fmt->nr_entries ?: arg->len, arg->val);
  634. }
  635. #define SCA_CHAR_ARRAY syscall_arg__scnprintf_char_array
  636. static const char *bpf_cmd[] = {
  637. "MAP_CREATE", "MAP_LOOKUP_ELEM", "MAP_UPDATE_ELEM", "MAP_DELETE_ELEM",
  638. "MAP_GET_NEXT_KEY", "PROG_LOAD", "OBJ_PIN", "OBJ_GET", "PROG_ATTACH",
  639. "PROG_DETACH", "PROG_TEST_RUN", "PROG_GET_NEXT_ID", "MAP_GET_NEXT_ID",
  640. "PROG_GET_FD_BY_ID", "MAP_GET_FD_BY_ID", "OBJ_GET_INFO_BY_FD",
  641. "PROG_QUERY", "RAW_TRACEPOINT_OPEN", "BTF_LOAD", "BTF_GET_FD_BY_ID",
  642. "TASK_FD_QUERY", "MAP_LOOKUP_AND_DELETE_ELEM", "MAP_FREEZE",
  643. "BTF_GET_NEXT_ID", "MAP_LOOKUP_BATCH", "MAP_LOOKUP_AND_DELETE_BATCH",
  644. "MAP_UPDATE_BATCH", "MAP_DELETE_BATCH", "LINK_CREATE", "LINK_UPDATE",
  645. "LINK_GET_FD_BY_ID", "LINK_GET_NEXT_ID", "ENABLE_STATS", "ITER_CREATE",
  646. "LINK_DETACH", "PROG_BIND_MAP",
  647. };
  648. static DEFINE_STRARRAY(bpf_cmd, "BPF_");
  649. static const char *fsmount_flags[] = {
  650. [1] = "CLOEXEC",
  651. };
  652. static DEFINE_STRARRAY(fsmount_flags, "FSMOUNT_");
  653. #include "trace/beauty/generated/fsconfig_arrays.c"
  654. static DEFINE_STRARRAY(fsconfig_cmds, "FSCONFIG_");
  655. static const char *epoll_ctl_ops[] = { "ADD", "DEL", "MOD", };
  656. static DEFINE_STRARRAY_OFFSET(epoll_ctl_ops, "EPOLL_CTL_", 1);
  657. static const char *itimers[] = { "REAL", "VIRTUAL", "PROF", };
  658. static DEFINE_STRARRAY(itimers, "ITIMER_");
  659. static const char *keyctl_options[] = {
  660. "GET_KEYRING_ID", "JOIN_SESSION_KEYRING", "UPDATE", "REVOKE", "CHOWN",
  661. "SETPERM", "DESCRIBE", "CLEAR", "LINK", "UNLINK", "SEARCH", "READ",
  662. "INSTANTIATE", "NEGATE", "SET_REQKEY_KEYRING", "SET_TIMEOUT",
  663. "ASSUME_AUTHORITY", "GET_SECURITY", "SESSION_TO_PARENT", "REJECT",
  664. "INSTANTIATE_IOV", "INVALIDATE", "GET_PERSISTENT",
  665. };
  666. static DEFINE_STRARRAY(keyctl_options, "KEYCTL_");
  667. static const char *whences[] = { "SET", "CUR", "END",
  668. #ifdef SEEK_DATA
  669. "DATA",
  670. #endif
  671. #ifdef SEEK_HOLE
  672. "HOLE",
  673. #endif
  674. };
  675. static DEFINE_STRARRAY(whences, "SEEK_");
  676. static const char *fcntl_cmds[] = {
  677. "DUPFD", "GETFD", "SETFD", "GETFL", "SETFL", "GETLK", "SETLK",
  678. "SETLKW", "SETOWN", "GETOWN", "SETSIG", "GETSIG", "GETLK64",
  679. "SETLK64", "SETLKW64", "SETOWN_EX", "GETOWN_EX",
  680. "GETOWNER_UIDS",
  681. };
  682. static DEFINE_STRARRAY(fcntl_cmds, "F_");
  683. static const char *fcntl_linux_specific_cmds[] = {
  684. "SETLEASE", "GETLEASE", "NOTIFY", "DUPFD_QUERY", [5] = "CANCELLK", "DUPFD_CLOEXEC",
  685. "SETPIPE_SZ", "GETPIPE_SZ", "ADD_SEALS", "GET_SEALS",
  686. "GET_RW_HINT", "SET_RW_HINT", "GET_FILE_RW_HINT", "SET_FILE_RW_HINT",
  687. };
  688. static DEFINE_STRARRAY_OFFSET(fcntl_linux_specific_cmds, "F_", F_LINUX_SPECIFIC_BASE);
  689. static struct strarray *fcntl_cmds_arrays[] = {
  690. &strarray__fcntl_cmds,
  691. &strarray__fcntl_linux_specific_cmds,
  692. };
  693. static DEFINE_STRARRAYS(fcntl_cmds_arrays);
  694. static const char *rlimit_resources[] = {
  695. "CPU", "FSIZE", "DATA", "STACK", "CORE", "RSS", "NPROC", "NOFILE",
  696. "MEMLOCK", "AS", "LOCKS", "SIGPENDING", "MSGQUEUE", "NICE", "RTPRIO",
  697. "RTTIME",
  698. };
  699. static DEFINE_STRARRAY(rlimit_resources, "RLIMIT_");
  700. static const char *sighow[] = { "BLOCK", "UNBLOCK", "SETMASK", };
  701. static DEFINE_STRARRAY(sighow, "SIG_");
  702. static const char *clockid[] = {
  703. "REALTIME", "MONOTONIC", "PROCESS_CPUTIME_ID", "THREAD_CPUTIME_ID",
  704. "MONOTONIC_RAW", "REALTIME_COARSE", "MONOTONIC_COARSE", "BOOTTIME",
  705. "REALTIME_ALARM", "BOOTTIME_ALARM", "SGI_CYCLE", "TAI"
  706. };
  707. static DEFINE_STRARRAY(clockid, "CLOCK_");
  708. static size_t syscall_arg__scnprintf_access_mode(char *bf, size_t size,
  709. struct syscall_arg *arg)
  710. {
  711. bool show_prefix = arg->show_string_prefix;
  712. const char *suffix = "_OK";
  713. size_t printed = 0;
  714. int mode = arg->val;
  715. if (mode == F_OK) /* 0 */
  716. return scnprintf(bf, size, "F%s", show_prefix ? suffix : "");
  717. #define P_MODE(n) \
  718. if (mode & n##_OK) { \
  719. printed += scnprintf(bf + printed, size - printed, "%s%s", #n, show_prefix ? suffix : ""); \
  720. mode &= ~n##_OK; \
  721. }
  722. P_MODE(R);
  723. P_MODE(W);
  724. P_MODE(X);
  725. #undef P_MODE
  726. if (mode)
  727. printed += scnprintf(bf + printed, size - printed, "|%#x", mode);
  728. return printed;
  729. }
  730. #define SCA_ACCMODE syscall_arg__scnprintf_access_mode
  731. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  732. struct syscall_arg *arg);
  733. #define SCA_FILENAME syscall_arg__scnprintf_filename
  734. // 'argname' is just documentational at this point, to remove the previous comment with that info
  735. #define SCA_FILENAME_FROM_USER(argname) \
  736. { .scnprintf = SCA_FILENAME, \
  737. .from_user = true, }
  738. static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct syscall_arg *arg);
  739. #define SCA_BUF syscall_arg__scnprintf_buf
  740. static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
  741. struct syscall_arg *arg)
  742. {
  743. bool show_prefix = arg->show_string_prefix;
  744. const char *prefix = "O_";
  745. int printed = 0, flags = arg->val;
  746. #define P_FLAG(n) \
  747. if (flags & O_##n) { \
  748. printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
  749. flags &= ~O_##n; \
  750. }
  751. P_FLAG(CLOEXEC);
  752. P_FLAG(NONBLOCK);
  753. #undef P_FLAG
  754. if (flags)
  755. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  756. return printed;
  757. }
  758. #define SCA_PIPE_FLAGS syscall_arg__scnprintf_pipe_flags
  759. #ifndef GRND_NONBLOCK
  760. #define GRND_NONBLOCK 0x0001
  761. #endif
  762. #ifndef GRND_RANDOM
  763. #define GRND_RANDOM 0x0002
  764. #endif
  765. static size_t syscall_arg__scnprintf_getrandom_flags(char *bf, size_t size,
  766. struct syscall_arg *arg)
  767. {
  768. bool show_prefix = arg->show_string_prefix;
  769. const char *prefix = "GRND_";
  770. int printed = 0, flags = arg->val;
  771. #define P_FLAG(n) \
  772. if (flags & GRND_##n) { \
  773. printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
  774. flags &= ~GRND_##n; \
  775. }
  776. P_FLAG(RANDOM);
  777. P_FLAG(NONBLOCK);
  778. #undef P_FLAG
  779. if (flags)
  780. printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
  781. return printed;
  782. }
  783. #define SCA_GETRANDOM_FLAGS syscall_arg__scnprintf_getrandom_flags
  784. #ifdef HAVE_LIBBPF_SUPPORT
  785. static void syscall_arg_fmt__cache_btf_enum(struct syscall_arg_fmt *arg_fmt, struct btf *btf, char *type)
  786. {
  787. int id;
  788. type = strstr(type, "enum ");
  789. if (type == NULL)
  790. return;
  791. type += 5; // skip "enum " to get the enumeration name
  792. id = btf__find_by_name(btf, type);
  793. if (id < 0)
  794. return;
  795. arg_fmt->type = btf__type_by_id(btf, id);
  796. }
  797. static bool syscall_arg__strtoul_btf_enum(char *bf, size_t size, struct syscall_arg *arg, u64 *val)
  798. {
  799. const struct btf_type *bt = arg->fmt->type;
  800. struct btf *btf = arg->trace->btf;
  801. struct btf_enum *be = btf_enum(bt);
  802. for (int i = 0; i < btf_vlen(bt); ++i, ++be) {
  803. const char *name = btf__name_by_offset(btf, be->name_off);
  804. int max_len = max(size, strlen(name));
  805. if (strncmp(name, bf, max_len) == 0) {
  806. *val = be->val;
  807. return true;
  808. }
  809. }
  810. return false;
  811. }
  812. static bool syscall_arg__strtoul_btf_type(char *bf, size_t size, struct syscall_arg *arg, u64 *val)
  813. {
  814. const struct btf_type *bt;
  815. char *type = arg->type_name;
  816. struct btf *btf;
  817. trace__load_vmlinux_btf(arg->trace);
  818. btf = arg->trace->btf;
  819. if (btf == NULL)
  820. return false;
  821. if (arg->fmt->type == NULL) {
  822. // See if this is an enum
  823. syscall_arg_fmt__cache_btf_enum(arg->fmt, btf, type);
  824. }
  825. // Now let's see if we have a BTF type resolved
  826. bt = arg->fmt->type;
  827. if (bt == NULL)
  828. return false;
  829. // If it is an enum:
  830. if (btf_is_enum(arg->fmt->type))
  831. return syscall_arg__strtoul_btf_enum(bf, size, arg, val);
  832. return false;
  833. }
  834. static size_t btf_enum_scnprintf(const struct btf_type *type, struct btf *btf, char *bf, size_t size, int val)
  835. {
  836. struct btf_enum *be = btf_enum(type);
  837. const int nr_entries = btf_vlen(type);
  838. for (int i = 0; i < nr_entries; ++i, ++be) {
  839. if (be->val == val) {
  840. return scnprintf(bf, size, "%s",
  841. btf__name_by_offset(btf, be->name_off));
  842. }
  843. }
  844. return 0;
  845. }
  846. struct trace_btf_dump_snprintf_ctx {
  847. char *bf;
  848. size_t printed, size;
  849. };
  850. static void trace__btf_dump_snprintf(void *vctx, const char *fmt, va_list args)
  851. {
  852. struct trace_btf_dump_snprintf_ctx *ctx = vctx;
  853. ctx->printed += vscnprintf(ctx->bf + ctx->printed, ctx->size - ctx->printed, fmt, args);
  854. }
  855. static size_t btf_struct_scnprintf(const struct btf_type *type, struct btf *btf, char *bf, size_t size, struct syscall_arg *arg)
  856. {
  857. struct trace_btf_dump_snprintf_ctx ctx = {
  858. .bf = bf,
  859. .size = size,
  860. };
  861. struct augmented_arg *augmented_arg = arg->augmented.args;
  862. int type_id = arg->fmt->type_id, consumed;
  863. struct btf_dump *btf_dump;
  864. LIBBPF_OPTS(btf_dump_opts, dump_opts);
  865. LIBBPF_OPTS(btf_dump_type_data_opts, dump_data_opts);
  866. if (arg == NULL || arg->augmented.args == NULL)
  867. return 0;
  868. dump_data_opts.compact = true;
  869. dump_data_opts.skip_names = !arg->trace->show_arg_names;
  870. btf_dump = btf_dump__new(btf, trace__btf_dump_snprintf, &ctx, &dump_opts);
  871. if (btf_dump == NULL)
  872. return 0;
  873. /* pretty print the struct data here */
  874. if (btf_dump__dump_type_data(btf_dump, type_id, arg->augmented.args->value, type->size, &dump_data_opts) == 0)
  875. return 0;
  876. consumed = sizeof(*augmented_arg) + augmented_arg->size;
  877. arg->augmented.args = ((void *)arg->augmented.args) + consumed;
  878. arg->augmented.size -= consumed;
  879. btf_dump__free(btf_dump);
  880. return ctx.printed;
  881. }
  882. static size_t trace__btf_scnprintf(struct trace *trace, struct syscall_arg *arg, char *bf,
  883. size_t size, int val, char *type)
  884. {
  885. struct syscall_arg_fmt *arg_fmt = arg->fmt;
  886. if (trace->btf == NULL)
  887. return 0;
  888. if (arg_fmt->type == NULL) {
  889. // Check if this is an enum and if we have the BTF type for it.
  890. syscall_arg_fmt__cache_btf_enum(arg_fmt, trace->btf, type);
  891. }
  892. // Did we manage to find a BTF type for the syscall/tracepoint argument?
  893. if (arg_fmt->type == NULL)
  894. return 0;
  895. if (btf_is_enum(arg_fmt->type))
  896. return btf_enum_scnprintf(arg_fmt->type, trace->btf, bf, size, val);
  897. else if (btf_is_struct(arg_fmt->type) || btf_is_union(arg_fmt->type))
  898. return btf_struct_scnprintf(arg_fmt->type, trace->btf, bf, size, arg);
  899. return 0;
  900. }
  901. #else // HAVE_LIBBPF_SUPPORT
  902. static size_t trace__btf_scnprintf(struct trace *trace __maybe_unused, struct syscall_arg *arg __maybe_unused,
  903. char *bf __maybe_unused, size_t size __maybe_unused, int val __maybe_unused,
  904. char *type __maybe_unused)
  905. {
  906. return 0;
  907. }
  908. static bool syscall_arg__strtoul_btf_type(char *bf __maybe_unused, size_t size __maybe_unused,
  909. struct syscall_arg *arg __maybe_unused, u64 *val __maybe_unused)
  910. {
  911. return false;
  912. }
  913. #endif // HAVE_LIBBPF_SUPPORT
  914. #define STUL_BTF_TYPE syscall_arg__strtoul_btf_type
  915. #define STRARRAY(name, array) \
  916. { .scnprintf = SCA_STRARRAY, \
  917. .strtoul = STUL_STRARRAY, \
  918. .parm = &strarray__##array, \
  919. .show_zero = true, }
  920. #define STRARRAY_FLAGS(name, array) \
  921. { .scnprintf = SCA_STRARRAY_FLAGS, \
  922. .strtoul = STUL_STRARRAY_FLAGS, \
  923. .parm = &strarray__##array, \
  924. .show_zero = true, }
  925. #include "trace/beauty/eventfd.c"
  926. #include "trace/beauty/futex_op.c"
  927. #include "trace/beauty/futex_val3.c"
  928. #include "trace/beauty/mmap.c"
  929. #include "trace/beauty/mode_t.c"
  930. #include "trace/beauty/msg_flags.c"
  931. #include "trace/beauty/open_flags.c"
  932. #include "trace/beauty/perf_event_open.c"
  933. #include "trace/beauty/pid.c"
  934. #include "trace/beauty/sched_policy.c"
  935. #include "trace/beauty/seccomp.c"
  936. #include "trace/beauty/signum.c"
  937. #include "trace/beauty/socket_type.c"
  938. #include "trace/beauty/waitid_options.c"
  939. static const struct syscall_fmt syscall_fmts[] = {
  940. { .name = "access",
  941. .arg = { [1] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
  942. { .name = "arch_prctl",
  943. .arg = { [0] = { .scnprintf = SCA_X86_ARCH_PRCTL_CODE, /* code */ },
  944. [1] = { .scnprintf = SCA_PTR, /* arg2 */ }, }, },
  945. { .name = "bind",
  946. .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
  947. [1] = SCA_SOCKADDR_FROM_USER(umyaddr),
  948. [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
  949. { .name = "bpf",
  950. .arg = { [0] = STRARRAY(cmd, bpf_cmd),
  951. [1] = { .from_user = true /* attr */, }, } },
  952. { .name = "brk", .hexret = true,
  953. .arg = { [0] = { .scnprintf = SCA_PTR, /* brk */ }, }, },
  954. { .name = "clock_gettime",
  955. .arg = { [0] = STRARRAY(clk_id, clockid), }, },
  956. { .name = "clock_nanosleep",
  957. .arg = { [2] = SCA_TIMESPEC_FROM_USER(req), }, },
  958. { .name = "clone", .errpid = true, .nr_args = 5,
  959. .arg = { [0] = { .name = "flags", .scnprintf = SCA_CLONE_FLAGS, },
  960. [1] = { .name = "child_stack", .scnprintf = SCA_HEX, },
  961. [2] = { .name = "parent_tidptr", .scnprintf = SCA_HEX, },
  962. [3] = { .name = "child_tidptr", .scnprintf = SCA_HEX, },
  963. [4] = { .name = "tls", .scnprintf = SCA_HEX, }, }, },
  964. { .name = "close",
  965. .arg = { [0] = { .scnprintf = SCA_CLOSE_FD, /* fd */ }, }, },
  966. { .name = "connect",
  967. .arg = { [0] = { .scnprintf = SCA_INT, /* fd */ },
  968. [1] = SCA_SOCKADDR_FROM_USER(servaddr),
  969. [2] = { .scnprintf = SCA_INT, /* addrlen */ }, }, },
  970. { .name = "epoll_ctl",
  971. .arg = { [1] = STRARRAY(op, epoll_ctl_ops), }, },
  972. { .name = "eventfd2",
  973. .arg = { [1] = { .scnprintf = SCA_EFD_FLAGS, /* flags */ }, }, },
  974. { .name = "faccessat",
  975. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ },
  976. [1] = SCA_FILENAME_FROM_USER(pathname),
  977. [2] = { .scnprintf = SCA_ACCMODE, /* mode */ }, }, },
  978. { .name = "faccessat2",
  979. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ },
  980. [1] = SCA_FILENAME_FROM_USER(pathname),
  981. [2] = { .scnprintf = SCA_ACCMODE, /* mode */ },
  982. [3] = { .scnprintf = SCA_FACCESSAT2_FLAGS, /* flags */ }, }, },
  983. { .name = "fchmodat",
  984. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  985. { .name = "fchownat",
  986. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  987. { .name = "fcntl",
  988. .arg = { [1] = { .scnprintf = SCA_FCNTL_CMD, /* cmd */
  989. .strtoul = STUL_STRARRAYS,
  990. .parm = &strarrays__fcntl_cmds_arrays,
  991. .show_zero = true, },
  992. [2] = { .scnprintf = SCA_FCNTL_ARG, /* arg */ }, }, },
  993. { .name = "flock",
  994. .arg = { [1] = { .scnprintf = SCA_FLOCK, /* cmd */ }, }, },
  995. { .name = "fsconfig",
  996. .arg = { [1] = STRARRAY(cmd, fsconfig_cmds), }, },
  997. { .name = "fsmount",
  998. .arg = { [1] = STRARRAY_FLAGS(flags, fsmount_flags),
  999. [2] = { .scnprintf = SCA_FSMOUNT_ATTR_FLAGS, /* attr_flags */ }, }, },
  1000. { .name = "fspick",
  1001. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  1002. [1] = SCA_FILENAME_FROM_USER(path),
  1003. [2] = { .scnprintf = SCA_FSPICK_FLAGS, /* flags */ }, }, },
  1004. { .name = "fstat", .alias = "newfstat", },
  1005. { .name = "futex",
  1006. .arg = { [1] = { .scnprintf = SCA_FUTEX_OP, /* op */ },
  1007. [5] = { .scnprintf = SCA_FUTEX_VAL3, /* val3 */ }, }, },
  1008. { .name = "futimesat",
  1009. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  1010. { .name = "getitimer",
  1011. .arg = { [0] = STRARRAY(which, itimers), }, },
  1012. { .name = "getpid", .errpid = true, },
  1013. { .name = "getpgid", .errpid = true, },
  1014. { .name = "getppid", .errpid = true, },
  1015. { .name = "getrandom",
  1016. .arg = { [2] = { .scnprintf = SCA_GETRANDOM_FLAGS, /* flags */ }, }, },
  1017. { .name = "getrlimit",
  1018. .arg = { [0] = STRARRAY(resource, rlimit_resources), }, },
  1019. { .name = "getsockopt",
  1020. .arg = { [1] = STRARRAY(level, socket_level), }, },
  1021. { .name = "gettid", .errpid = true, },
  1022. { .name = "ioctl",
  1023. .arg = {
  1024. #if defined(__i386__) || defined(__x86_64__)
  1025. /*
  1026. * FIXME: Make this available to all arches.
  1027. */
  1028. [1] = { .scnprintf = SCA_IOCTL_CMD, /* cmd */ },
  1029. [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
  1030. #else
  1031. [2] = { .scnprintf = SCA_HEX, /* arg */ }, }, },
  1032. #endif
  1033. { .name = "kcmp", .nr_args = 5,
  1034. .arg = { [0] = { .name = "pid1", .scnprintf = SCA_PID, },
  1035. [1] = { .name = "pid2", .scnprintf = SCA_PID, },
  1036. [2] = { .name = "type", .scnprintf = SCA_KCMP_TYPE, },
  1037. [3] = { .name = "idx1", .scnprintf = SCA_KCMP_IDX, },
  1038. [4] = { .name = "idx2", .scnprintf = SCA_KCMP_IDX, }, }, },
  1039. { .name = "keyctl",
  1040. .arg = { [0] = STRARRAY(option, keyctl_options), }, },
  1041. { .name = "kill",
  1042. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  1043. { .name = "linkat",
  1044. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  1045. { .name = "lseek",
  1046. .arg = { [2] = STRARRAY(whence, whences), }, },
  1047. { .name = "lstat", .alias = "newlstat", },
  1048. { .name = "madvise",
  1049. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  1050. [2] = { .scnprintf = SCA_MADV_BHV, /* behavior */ }, }, },
  1051. { .name = "mkdirat",
  1052. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  1053. { .name = "mknodat",
  1054. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fd */ }, }, },
  1055. { .name = "mmap", .hexret = true,
  1056. /* The standard mmap maps to old_mmap on s390x */
  1057. #if defined(__s390x__)
  1058. .alias = "old_mmap",
  1059. #endif
  1060. .arg = { [2] = { .scnprintf = SCA_MMAP_PROT, .show_zero = true, /* prot */ },
  1061. [3] = { .scnprintf = SCA_MMAP_FLAGS, /* flags */
  1062. .strtoul = STUL_STRARRAY_FLAGS,
  1063. .parm = &strarray__mmap_flags, },
  1064. [5] = { .scnprintf = SCA_HEX, /* offset */ }, }, },
  1065. { .name = "mount",
  1066. .arg = { [0] = SCA_FILENAME_FROM_USER(devname),
  1067. [3] = { .scnprintf = SCA_MOUNT_FLAGS, /* flags */
  1068. .mask_val = SCAMV_MOUNT_FLAGS, /* flags */ }, }, },
  1069. { .name = "move_mount",
  1070. .arg = { [0] = { .scnprintf = SCA_FDAT, /* from_dfd */ },
  1071. [1] = SCA_FILENAME_FROM_USER(pathname),
  1072. [2] = { .scnprintf = SCA_FDAT, /* to_dfd */ },
  1073. [3] = SCA_FILENAME_FROM_USER(pathname),
  1074. [4] = { .scnprintf = SCA_MOVE_MOUNT_FLAGS, /* flags */ }, }, },
  1075. { .name = "mprotect",
  1076. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  1077. [2] = { .scnprintf = SCA_MMAP_PROT, .show_zero = true, /* prot */ }, }, },
  1078. { .name = "mq_unlink",
  1079. .arg = { [0] = SCA_FILENAME_FROM_USER(u_name), }, },
  1080. { .name = "mremap", .hexret = true,
  1081. .arg = { [3] = { .scnprintf = SCA_MREMAP_FLAGS, /* flags */ }, }, },
  1082. { .name = "name_to_handle_at",
  1083. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  1084. { .name = "nanosleep",
  1085. .arg = { [0] = SCA_TIMESPEC_FROM_USER(req), }, },
  1086. { .name = "newfstatat", .alias = "fstatat",
  1087. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ },
  1088. [1] = SCA_FILENAME_FROM_USER(pathname),
  1089. [3] = { .scnprintf = SCA_FS_AT_FLAGS, /* flags */ }, }, },
  1090. { .name = "open",
  1091. .arg = { [1] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  1092. { .name = "open_by_handle_at",
  1093. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  1094. [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  1095. { .name = "openat",
  1096. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  1097. [2] = { .scnprintf = SCA_OPEN_FLAGS, /* flags */ }, }, },
  1098. { .name = "perf_event_open",
  1099. .arg = { [0] = SCA_PERF_ATTR_FROM_USER(attr),
  1100. [2] = { .scnprintf = SCA_INT, /* cpu */ },
  1101. [3] = { .scnprintf = SCA_FD, /* group_fd */ },
  1102. [4] = { .scnprintf = SCA_PERF_FLAGS, /* flags */ }, }, },
  1103. { .name = "pipe2",
  1104. .arg = { [1] = { .scnprintf = SCA_PIPE_FLAGS, /* flags */ }, }, },
  1105. { .name = "pkey_alloc",
  1106. .arg = { [1] = { .scnprintf = SCA_PKEY_ALLOC_ACCESS_RIGHTS, /* access_rights */ }, }, },
  1107. { .name = "pkey_free",
  1108. .arg = { [0] = { .scnprintf = SCA_INT, /* key */ }, }, },
  1109. { .name = "pkey_mprotect",
  1110. .arg = { [0] = { .scnprintf = SCA_HEX, /* start */ },
  1111. [2] = { .scnprintf = SCA_MMAP_PROT, .show_zero = true, /* prot */ },
  1112. [3] = { .scnprintf = SCA_INT, /* pkey */ }, }, },
  1113. { .name = "poll", .timeout = true, },
  1114. { .name = "ppoll", .timeout = true, },
  1115. { .name = "prctl",
  1116. .arg = { [0] = { .scnprintf = SCA_PRCTL_OPTION, /* option */
  1117. .strtoul = STUL_STRARRAY,
  1118. .parm = &strarray__prctl_options, },
  1119. [1] = { .scnprintf = SCA_PRCTL_ARG2, /* arg2 */ },
  1120. [2] = { .scnprintf = SCA_PRCTL_ARG3, /* arg3 */ }, }, },
  1121. { .name = "pread", .alias = "pread64", },
  1122. { .name = "preadv", .alias = "pread", },
  1123. { .name = "prlimit64",
  1124. .arg = { [1] = STRARRAY(resource, rlimit_resources),
  1125. [2] = { .from_user = true /* new_rlim */, }, }, },
  1126. { .name = "pwrite", .alias = "pwrite64", },
  1127. { .name = "readlinkat",
  1128. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  1129. { .name = "recvfrom",
  1130. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  1131. { .name = "recvmmsg",
  1132. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  1133. { .name = "recvmsg",
  1134. .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  1135. { .name = "renameat",
  1136. .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
  1137. [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ }, }, },
  1138. { .name = "renameat2",
  1139. .arg = { [0] = { .scnprintf = SCA_FDAT, /* olddirfd */ },
  1140. [2] = { .scnprintf = SCA_FDAT, /* newdirfd */ },
  1141. [4] = { .scnprintf = SCA_RENAMEAT2_FLAGS, /* flags */ }, }, },
  1142. { .name = "rseq",
  1143. .arg = { [0] = { .from_user = true /* rseq */, }, }, },
  1144. { .name = "rt_sigaction",
  1145. .arg = { [0] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  1146. { .name = "rt_sigprocmask",
  1147. .arg = { [0] = STRARRAY(how, sighow), }, },
  1148. { .name = "rt_sigqueueinfo",
  1149. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  1150. { .name = "rt_tgsigqueueinfo",
  1151. .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  1152. { .name = "sched_setscheduler",
  1153. .arg = { [1] = { .scnprintf = SCA_SCHED_POLICY, /* policy */ }, }, },
  1154. { .name = "seccomp",
  1155. .arg = { [0] = { .scnprintf = SCA_SECCOMP_OP, /* op */ },
  1156. [1] = { .scnprintf = SCA_SECCOMP_FLAGS, /* flags */ }, }, },
  1157. { .name = "select", .timeout = true, },
  1158. { .name = "sendfile", .alias = "sendfile64", },
  1159. { .name = "sendmmsg",
  1160. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  1161. { .name = "sendmsg",
  1162. .arg = { [2] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ }, }, },
  1163. { .name = "sendto",
  1164. .arg = { [3] = { .scnprintf = SCA_MSG_FLAGS, /* flags */ },
  1165. [4] = SCA_SOCKADDR_FROM_USER(addr), }, },
  1166. { .name = "set_robust_list",
  1167. .arg = { [0] = { .from_user = true /* head */, }, }, },
  1168. { .name = "set_tid_address", .errpid = true, },
  1169. { .name = "setitimer",
  1170. .arg = { [0] = STRARRAY(which, itimers), }, },
  1171. { .name = "setrlimit",
  1172. .arg = { [0] = STRARRAY(resource, rlimit_resources),
  1173. [1] = { .from_user = true /* rlim */, }, }, },
  1174. { .name = "setsockopt",
  1175. .arg = { [1] = STRARRAY(level, socket_level), }, },
  1176. { .name = "socket",
  1177. .arg = { [0] = STRARRAY(family, socket_families),
  1178. [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
  1179. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  1180. { .name = "socketpair",
  1181. .arg = { [0] = STRARRAY(family, socket_families),
  1182. [1] = { .scnprintf = SCA_SK_TYPE, /* type */ },
  1183. [2] = { .scnprintf = SCA_SK_PROTO, /* protocol */ }, }, },
  1184. { .name = "stat", .alias = "newstat", },
  1185. { .name = "statx",
  1186. .arg = { [0] = { .scnprintf = SCA_FDAT, /* fdat */ },
  1187. [2] = { .scnprintf = SCA_FS_AT_FLAGS, /* flags */ } ,
  1188. [3] = { .scnprintf = SCA_STATX_MASK, /* mask */ }, }, },
  1189. { .name = "swapoff",
  1190. .arg = { [0] = SCA_FILENAME_FROM_USER(specialfile), }, },
  1191. { .name = "swapon",
  1192. .arg = { [0] = SCA_FILENAME_FROM_USER(specialfile), }, },
  1193. { .name = "symlinkat",
  1194. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ }, }, },
  1195. { .name = "sync_file_range",
  1196. .arg = { [3] = { .scnprintf = SCA_SYNC_FILE_RANGE_FLAGS, /* flags */ }, }, },
  1197. { .name = "tgkill",
  1198. .arg = { [2] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  1199. { .name = "tkill",
  1200. .arg = { [1] = { .scnprintf = SCA_SIGNUM, /* sig */ }, }, },
  1201. { .name = "umount2", .alias = "umount",
  1202. .arg = { [0] = SCA_FILENAME_FROM_USER(name), }, },
  1203. { .name = "uname", .alias = "newuname", },
  1204. { .name = "unlinkat",
  1205. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dfd */ },
  1206. [1] = SCA_FILENAME_FROM_USER(pathname),
  1207. [2] = { .scnprintf = SCA_FS_AT_FLAGS, /* flags */ }, }, },
  1208. { .name = "utimensat",
  1209. .arg = { [0] = { .scnprintf = SCA_FDAT, /* dirfd */ }, }, },
  1210. { .name = "wait4", .errpid = true,
  1211. .arg = { [2] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  1212. { .name = "waitid", .errpid = true,
  1213. .arg = { [3] = { .scnprintf = SCA_WAITID_OPTIONS, /* options */ }, }, },
  1214. { .name = "write",
  1215. .arg = { [1] = { .scnprintf = SCA_BUF /* buf */, .from_user = true, }, }, },
  1216. };
  1217. static int syscall_fmt__cmp(const void *name, const void *fmtp)
  1218. {
  1219. const struct syscall_fmt *fmt = fmtp;
  1220. return strcmp(name, fmt->name);
  1221. }
  1222. static const struct syscall_fmt *__syscall_fmt__find(const struct syscall_fmt *fmts,
  1223. const int nmemb,
  1224. const char *name)
  1225. {
  1226. return bsearch(name, fmts, nmemb, sizeof(struct syscall_fmt), syscall_fmt__cmp);
  1227. }
  1228. static const struct syscall_fmt *syscall_fmt__find(const char *name)
  1229. {
  1230. const int nmemb = ARRAY_SIZE(syscall_fmts);
  1231. return __syscall_fmt__find(syscall_fmts, nmemb, name);
  1232. }
  1233. static const struct syscall_fmt *__syscall_fmt__find_by_alias(const struct syscall_fmt *fmts,
  1234. const int nmemb, const char *alias)
  1235. {
  1236. int i;
  1237. for (i = 0; i < nmemb; ++i) {
  1238. if (fmts[i].alias && strcmp(fmts[i].alias, alias) == 0)
  1239. return &fmts[i];
  1240. }
  1241. return NULL;
  1242. }
  1243. static const struct syscall_fmt *syscall_fmt__find_by_alias(const char *alias)
  1244. {
  1245. const int nmemb = ARRAY_SIZE(syscall_fmts);
  1246. return __syscall_fmt__find_by_alias(syscall_fmts, nmemb, alias);
  1247. }
  1248. /**
  1249. * struct syscall
  1250. */
  1251. struct syscall {
  1252. /** @e_machine: The ELF machine associated with the entry. */
  1253. int e_machine;
  1254. /** @id: id value from the tracepoint, the system call number. */
  1255. int id;
  1256. struct tep_event *tp_format;
  1257. int nr_args;
  1258. /**
  1259. * @args_size: sum of the sizes of the syscall arguments, anything
  1260. * after that is augmented stuff: pathname for openat, etc.
  1261. */
  1262. int args_size;
  1263. struct {
  1264. struct bpf_program *sys_enter,
  1265. *sys_exit;
  1266. } bpf_prog;
  1267. /** @is_exit: is this "exit" or "exit_group"? */
  1268. bool is_exit;
  1269. /**
  1270. * @is_open: is this "open" or "openat"? To associate the fd returned in
  1271. * sys_exit with the pathname in sys_enter.
  1272. */
  1273. bool is_open;
  1274. /**
  1275. * @nonexistent: Name lookup failed. Just a hole in the syscall table,
  1276. * syscall id not allocated.
  1277. */
  1278. bool nonexistent;
  1279. bool use_btf;
  1280. struct tep_format_field *args;
  1281. const char *name;
  1282. const struct syscall_fmt *fmt;
  1283. struct syscall_arg_fmt *arg_fmt;
  1284. };
  1285. /*
  1286. * We need to have this 'calculated' boolean because in some cases we really
  1287. * don't know what is the duration of a syscall, for instance, when we start
  1288. * a session and some threads are waiting for a syscall to finish, say 'poll',
  1289. * in which case all we can do is to print "( ? ) for duration and for the
  1290. * start timestamp.
  1291. */
  1292. static size_t fprintf_duration(unsigned long t, bool calculated, FILE *fp)
  1293. {
  1294. double duration = (double)t / NSEC_PER_MSEC;
  1295. size_t printed = fprintf(fp, "(");
  1296. if (!calculated)
  1297. printed += fprintf(fp, " ");
  1298. else if (duration >= 1.0)
  1299. printed += color_fprintf(fp, PERF_COLOR_RED, "%6.3f ms", duration);
  1300. else if (duration >= 0.01)
  1301. printed += color_fprintf(fp, PERF_COLOR_YELLOW, "%6.3f ms", duration);
  1302. else
  1303. printed += color_fprintf(fp, PERF_COLOR_NORMAL, "%6.3f ms", duration);
  1304. return printed + fprintf(fp, "): ");
  1305. }
  1306. /**
  1307. * filename.ptr: The filename char pointer that will be vfs_getname'd
  1308. * filename.entry_str_pos: Where to insert the string translated from
  1309. * filename.ptr by the vfs_getname tracepoint/kprobe.
  1310. * ret_scnprintf: syscall args may set this to a different syscall return
  1311. * formatter, for instance, fcntl may return fds, file flags, etc.
  1312. */
  1313. struct thread_trace {
  1314. u64 entry_time;
  1315. bool entry_pending;
  1316. unsigned long nr_events;
  1317. unsigned long pfmaj, pfmin;
  1318. char *entry_str;
  1319. double runtime_ms;
  1320. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg);
  1321. struct {
  1322. unsigned long ptr;
  1323. short int entry_str_pos;
  1324. bool pending_open;
  1325. unsigned int namelen;
  1326. char *name;
  1327. } filename;
  1328. struct {
  1329. int max;
  1330. struct file *table;
  1331. } files;
  1332. struct hashmap *syscall_stats;
  1333. };
  1334. static size_t syscall_id_hash(long key, void *ctx __maybe_unused)
  1335. {
  1336. return key;
  1337. }
  1338. static bool syscall_id_equal(long key1, long key2, void *ctx __maybe_unused)
  1339. {
  1340. return key1 == key2;
  1341. }
  1342. static struct hashmap *alloc_syscall_stats(void)
  1343. {
  1344. return hashmap__new(syscall_id_hash, syscall_id_equal, NULL);
  1345. }
  1346. static void delete_syscall_stats(struct hashmap *syscall_stats)
  1347. {
  1348. struct hashmap_entry *pos;
  1349. size_t bkt;
  1350. if (syscall_stats == NULL)
  1351. return;
  1352. hashmap__for_each_entry(syscall_stats, pos, bkt)
  1353. zfree(&pos->pvalue);
  1354. hashmap__free(syscall_stats);
  1355. }
  1356. static struct thread_trace *thread_trace__new(struct trace *trace)
  1357. {
  1358. struct thread_trace *ttrace = zalloc(sizeof(struct thread_trace));
  1359. if (ttrace) {
  1360. ttrace->files.max = -1;
  1361. if (trace->summary) {
  1362. ttrace->syscall_stats = alloc_syscall_stats();
  1363. if (IS_ERR(ttrace->syscall_stats))
  1364. zfree(&ttrace);
  1365. }
  1366. }
  1367. return ttrace;
  1368. }
  1369. static void thread_trace__free_files(struct thread_trace *ttrace);
  1370. static void thread_trace__delete(void *pttrace)
  1371. {
  1372. struct thread_trace *ttrace = pttrace;
  1373. if (!ttrace)
  1374. return;
  1375. delete_syscall_stats(ttrace->syscall_stats);
  1376. ttrace->syscall_stats = NULL;
  1377. thread_trace__free_files(ttrace);
  1378. zfree(&ttrace->entry_str);
  1379. free(ttrace);
  1380. }
  1381. static struct thread_trace *thread__trace(struct thread *thread, struct trace *trace)
  1382. {
  1383. struct thread_trace *ttrace;
  1384. if (thread == NULL)
  1385. goto fail;
  1386. if (thread__priv(thread) == NULL)
  1387. thread__set_priv(thread, thread_trace__new(trace));
  1388. if (thread__priv(thread) == NULL)
  1389. goto fail;
  1390. ttrace = thread__priv(thread);
  1391. ++ttrace->nr_events;
  1392. return ttrace;
  1393. fail:
  1394. color_fprintf(trace->output, PERF_COLOR_RED,
  1395. "WARNING: not enough memory, dropping samples!\n");
  1396. return NULL;
  1397. }
  1398. void syscall_arg__set_ret_scnprintf(struct syscall_arg *arg,
  1399. size_t (*ret_scnprintf)(char *bf, size_t size, struct syscall_arg *arg))
  1400. {
  1401. struct thread_trace *ttrace = thread__priv(arg->thread);
  1402. ttrace->ret_scnprintf = ret_scnprintf;
  1403. }
  1404. #define TRACE_PFMAJ (1 << 0)
  1405. #define TRACE_PFMIN (1 << 1)
  1406. static const size_t trace__entry_str_size = 2048;
  1407. static void thread_trace__free_files(struct thread_trace *ttrace)
  1408. {
  1409. for (int i = 0; i <= ttrace->files.max; ++i) {
  1410. struct file *file = ttrace->files.table + i;
  1411. zfree(&file->pathname);
  1412. }
  1413. zfree(&ttrace->files.table);
  1414. ttrace->files.max = -1;
  1415. }
  1416. static struct file *thread_trace__files_entry(struct thread_trace *ttrace, int fd)
  1417. {
  1418. if (fd < 0)
  1419. return NULL;
  1420. if (fd > ttrace->files.max) {
  1421. struct file *nfiles = realloc(ttrace->files.table, (fd + 1) * sizeof(struct file));
  1422. if (nfiles == NULL)
  1423. return NULL;
  1424. if (ttrace->files.max != -1) {
  1425. memset(nfiles + ttrace->files.max + 1, 0,
  1426. (fd - ttrace->files.max) * sizeof(struct file));
  1427. } else {
  1428. memset(nfiles, 0, (fd + 1) * sizeof(struct file));
  1429. }
  1430. ttrace->files.table = nfiles;
  1431. ttrace->files.max = fd;
  1432. }
  1433. return ttrace->files.table + fd;
  1434. }
  1435. struct file *thread__files_entry(struct thread *thread, int fd)
  1436. {
  1437. return thread_trace__files_entry(thread__priv(thread), fd);
  1438. }
  1439. static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
  1440. {
  1441. struct thread_trace *ttrace = thread__priv(thread);
  1442. struct file *file = thread_trace__files_entry(ttrace, fd);
  1443. if (file != NULL) {
  1444. struct stat st;
  1445. if (stat(pathname, &st) == 0)
  1446. file->dev_maj = major(st.st_rdev);
  1447. file->pathname = strdup(pathname);
  1448. if (file->pathname)
  1449. return 0;
  1450. }
  1451. return -1;
  1452. }
  1453. static int thread__read_fd_path(struct thread *thread, int fd)
  1454. {
  1455. char linkname[PATH_MAX], pathname[PATH_MAX];
  1456. struct stat st;
  1457. int ret;
  1458. if (thread__pid(thread) == thread__tid(thread)) {
  1459. scnprintf(linkname, sizeof(linkname),
  1460. "/proc/%d/fd/%d", thread__pid(thread), fd);
  1461. } else {
  1462. scnprintf(linkname, sizeof(linkname),
  1463. "/proc/%d/task/%d/fd/%d",
  1464. thread__pid(thread), thread__tid(thread), fd);
  1465. }
  1466. if (lstat(linkname, &st) < 0 || st.st_size + 1 > (off_t)sizeof(pathname))
  1467. return -1;
  1468. ret = readlink(linkname, pathname, sizeof(pathname));
  1469. if (ret < 0 || ret > st.st_size)
  1470. return -1;
  1471. pathname[ret] = '\0';
  1472. return trace__set_fd_pathname(thread, fd, pathname);
  1473. }
  1474. static const char *thread__fd_path(struct thread *thread, int fd,
  1475. struct trace *trace)
  1476. {
  1477. struct thread_trace *ttrace = thread__priv(thread);
  1478. if (ttrace == NULL || trace->fd_path_disabled)
  1479. return NULL;
  1480. if (fd < 0)
  1481. return NULL;
  1482. if ((fd > ttrace->files.max || ttrace->files.table[fd].pathname == NULL)) {
  1483. if (!trace->live)
  1484. return NULL;
  1485. ++trace->stats.proc_getname;
  1486. if (thread__read_fd_path(thread, fd))
  1487. return NULL;
  1488. }
  1489. return ttrace->files.table[fd].pathname;
  1490. }
  1491. size_t syscall_arg__scnprintf_fd(char *bf, size_t size, struct syscall_arg *arg)
  1492. {
  1493. int fd = arg->val;
  1494. size_t printed = scnprintf(bf, size, "%d", fd);
  1495. const char *path = thread__fd_path(arg->thread, fd, arg->trace);
  1496. if (path)
  1497. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  1498. return printed;
  1499. }
  1500. size_t pid__scnprintf_fd(struct trace *trace, pid_t pid, int fd, char *bf, size_t size)
  1501. {
  1502. size_t printed = scnprintf(bf, size, "%d", fd);
  1503. struct thread *thread = machine__find_thread(trace->host, pid, pid);
  1504. if (thread) {
  1505. const char *path = thread__fd_path(thread, fd, trace);
  1506. if (path)
  1507. printed += scnprintf(bf + printed, size - printed, "<%s>", path);
  1508. thread__put(thread);
  1509. }
  1510. return printed;
  1511. }
  1512. static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
  1513. struct syscall_arg *arg)
  1514. {
  1515. int fd = arg->val;
  1516. size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
  1517. struct thread_trace *ttrace = thread__priv(arg->thread);
  1518. if (ttrace && fd >= 0 && fd <= ttrace->files.max)
  1519. zfree(&ttrace->files.table[fd].pathname);
  1520. return printed;
  1521. }
  1522. static void thread__set_filename_pos(struct thread *thread, const char *bf,
  1523. unsigned long ptr)
  1524. {
  1525. struct thread_trace *ttrace = thread__priv(thread);
  1526. ttrace->filename.ptr = ptr;
  1527. ttrace->filename.entry_str_pos = bf - ttrace->entry_str;
  1528. }
  1529. static size_t syscall_arg__scnprintf_augmented_string(struct syscall_arg *arg, char *bf, size_t size)
  1530. {
  1531. struct augmented_arg *augmented_arg = arg->augmented.args;
  1532. size_t printed = scnprintf(bf, size, "\"%.*s\"", augmented_arg->size, augmented_arg->value);
  1533. /*
  1534. * So that the next arg with a payload can consume its augmented arg, i.e. for rename* syscalls
  1535. * we would have two strings, each prefixed by its size.
  1536. */
  1537. int consumed = sizeof(*augmented_arg) + augmented_arg->size;
  1538. arg->augmented.args = ((void *)arg->augmented.args) + consumed;
  1539. arg->augmented.size -= consumed;
  1540. return printed;
  1541. }
  1542. static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
  1543. struct syscall_arg *arg)
  1544. {
  1545. unsigned long ptr = arg->val;
  1546. if (arg->augmented.args)
  1547. return syscall_arg__scnprintf_augmented_string(arg, bf, size);
  1548. if (!arg->trace->vfs_getname)
  1549. return scnprintf(bf, size, "%#x", ptr);
  1550. thread__set_filename_pos(arg->thread, bf, ptr);
  1551. return 0;
  1552. }
  1553. #define MAX_CONTROL_CHAR 31
  1554. #define MAX_ASCII 127
  1555. static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct syscall_arg *arg)
  1556. {
  1557. struct augmented_arg *augmented_arg = arg->augmented.args;
  1558. unsigned char *orig = (unsigned char *)augmented_arg->value;
  1559. size_t printed = 0;
  1560. int consumed;
  1561. if (augmented_arg == NULL)
  1562. return 0;
  1563. for (int j = 0; j < augmented_arg->size; ++j) {
  1564. bool control_char = orig[j] <= MAX_CONTROL_CHAR || orig[j] >= MAX_ASCII;
  1565. /* print control characters (0~31 and 127), and non-ascii characters in \(digits) */
  1566. printed += scnprintf(bf + printed, size - printed, control_char ? "\\%d" : "%c", (int)orig[j]);
  1567. }
  1568. consumed = sizeof(*augmented_arg) + augmented_arg->size;
  1569. arg->augmented.args = ((void *)arg->augmented.args) + consumed;
  1570. arg->augmented.size -= consumed;
  1571. return printed;
  1572. }
  1573. static bool trace__filter_duration(struct trace *trace, double t)
  1574. {
  1575. return t < (trace->duration_filter * NSEC_PER_MSEC);
  1576. }
  1577. static size_t __trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  1578. {
  1579. double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;
  1580. return fprintf(fp, "%10.3f ", ts);
  1581. }
  1582. /*
  1583. * We're handling tstamp=0 as an undefined tstamp, i.e. like when we are
  1584. * using ttrace->entry_time for a thread that receives a sys_exit without
  1585. * first having received a sys_enter ("poll" issued before tracing session
  1586. * starts, lost sys_enter exit due to ring buffer overflow).
  1587. */
  1588. static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
  1589. {
  1590. if (tstamp > 0)
  1591. return __trace__fprintf_tstamp(trace, tstamp, fp);
  1592. return fprintf(fp, " ? ");
  1593. }
  1594. static pid_t workload_pid = -1;
  1595. static volatile sig_atomic_t done = false;
  1596. static volatile sig_atomic_t interrupted = false;
  1597. static void sighandler_interrupt(int sig __maybe_unused)
  1598. {
  1599. done = interrupted = true;
  1600. }
  1601. static void sighandler_chld(int sig __maybe_unused, siginfo_t *info,
  1602. void *context __maybe_unused)
  1603. {
  1604. if (info->si_pid == workload_pid)
  1605. done = true;
  1606. }
  1607. static size_t trace__fprintf_comm_tid(struct trace *trace, struct thread *thread, FILE *fp)
  1608. {
  1609. size_t printed = 0;
  1610. if (trace->multiple_threads) {
  1611. if (trace->show_comm)
  1612. printed += fprintf(fp, "%.14s/", thread__comm_str(thread));
  1613. printed += fprintf(fp, "%d ", thread__tid(thread));
  1614. }
  1615. return printed;
  1616. }
  1617. static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
  1618. u64 duration, bool duration_calculated, u64 tstamp, FILE *fp)
  1619. {
  1620. size_t printed = 0;
  1621. if (trace->show_tstamp)
  1622. printed = trace__fprintf_tstamp(trace, tstamp, fp);
  1623. if (trace->show_duration)
  1624. printed += fprintf_duration(duration, duration_calculated, fp);
  1625. return printed + trace__fprintf_comm_tid(trace, thread, fp);
  1626. }
  1627. static int trace__process_event(struct trace *trace, struct machine *machine,
  1628. union perf_event *event, struct perf_sample *sample)
  1629. {
  1630. int ret = 0;
  1631. switch (event->header.type) {
  1632. case PERF_RECORD_LOST:
  1633. color_fprintf(trace->output, PERF_COLOR_RED,
  1634. "LOST %" PRIu64 " events!\n", (u64)event->lost.lost);
  1635. ret = machine__process_lost_event(machine, event, sample);
  1636. break;
  1637. default:
  1638. ret = machine__process_event(machine, event, sample);
  1639. break;
  1640. }
  1641. return ret;
  1642. }
  1643. static int trace__tool_process(const struct perf_tool *tool,
  1644. union perf_event *event,
  1645. struct perf_sample *sample,
  1646. struct machine *machine)
  1647. {
  1648. struct trace *trace = container_of(tool, struct trace, tool);
  1649. return trace__process_event(trace, machine, event, sample);
  1650. }
  1651. static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
  1652. {
  1653. struct machine *machine = vmachine;
  1654. if (machine->kptr_restrict_warned)
  1655. return NULL;
  1656. if (symbol_conf.kptr_restrict) {
  1657. pr_warning("Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  1658. "Check /proc/sys/kernel/kptr_restrict and /proc/sys/kernel/perf_event_paranoid.\n\n"
  1659. "Kernel samples will not be resolved.\n");
  1660. machine->kptr_restrict_warned = true;
  1661. return NULL;
  1662. }
  1663. return machine__resolve_kernel_addr(vmachine, addrp, modp);
  1664. }
  1665. static int trace__symbols_init(struct trace *trace, int argc, const char **argv,
  1666. struct evlist *evlist)
  1667. {
  1668. int err = symbol__init(NULL);
  1669. if (err)
  1670. return err;
  1671. perf_env__init(&trace->host_env);
  1672. err = perf_env__set_cmdline(&trace->host_env, argc, argv);
  1673. if (err)
  1674. goto out;
  1675. trace->host = machine__new_host(&trace->host_env);
  1676. if (trace->host == NULL) {
  1677. err = -ENOMEM;
  1678. goto out;
  1679. }
  1680. thread__set_priv_destructor(thread_trace__delete);
  1681. err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
  1682. if (err < 0)
  1683. goto out;
  1684. err = __machine__synthesize_threads(trace->host, &trace->tool, &trace->opts.target,
  1685. evlist->core.threads, trace__tool_process,
  1686. /*needs_mmap=*/callchain_param.enabled,
  1687. /*mmap_data=*/false,
  1688. /*nr_threads_synthesize=*/1);
  1689. out:
  1690. if (err) {
  1691. perf_env__exit(&trace->host_env);
  1692. symbol__exit();
  1693. }
  1694. return err;
  1695. }
  1696. static void trace__symbols__exit(struct trace *trace)
  1697. {
  1698. machine__exit(trace->host);
  1699. trace->host = NULL;
  1700. perf_env__exit(&trace->host_env);
  1701. symbol__exit();
  1702. }
  1703. static int syscall__alloc_arg_fmts(struct syscall *sc, int nr_args)
  1704. {
  1705. int idx;
  1706. if (nr_args == RAW_SYSCALL_ARGS_NUM && sc->fmt && sc->fmt->nr_args != 0)
  1707. nr_args = sc->fmt->nr_args;
  1708. sc->arg_fmt = calloc(nr_args, sizeof(*sc->arg_fmt));
  1709. if (sc->arg_fmt == NULL)
  1710. return -1;
  1711. for (idx = 0; idx < nr_args; ++idx) {
  1712. if (sc->fmt)
  1713. sc->arg_fmt[idx] = sc->fmt->arg[idx];
  1714. }
  1715. sc->nr_args = nr_args;
  1716. return 0;
  1717. }
  1718. static const struct syscall_arg_fmt syscall_arg_fmts__by_name[] = {
  1719. { .name = "msr", .scnprintf = SCA_X86_MSR, .strtoul = STUL_X86_MSR, },
  1720. { .name = "vector", .scnprintf = SCA_X86_IRQ_VECTORS, .strtoul = STUL_X86_IRQ_VECTORS, },
  1721. };
  1722. static int syscall_arg_fmt__cmp(const void *name, const void *fmtp)
  1723. {
  1724. const struct syscall_arg_fmt *fmt = fmtp;
  1725. return strcmp(name, fmt->name);
  1726. }
  1727. static const struct syscall_arg_fmt *
  1728. __syscall_arg_fmt__find_by_name(const struct syscall_arg_fmt *fmts, const int nmemb,
  1729. const char *name)
  1730. {
  1731. return bsearch(name, fmts, nmemb, sizeof(struct syscall_arg_fmt), syscall_arg_fmt__cmp);
  1732. }
  1733. static const struct syscall_arg_fmt *syscall_arg_fmt__find_by_name(const char *name)
  1734. {
  1735. const int nmemb = ARRAY_SIZE(syscall_arg_fmts__by_name);
  1736. return __syscall_arg_fmt__find_by_name(syscall_arg_fmts__by_name, nmemb, name);
  1737. }
  1738. /*
  1739. * v6.19 kernel added new fields to read userspace memory for event tracing.
  1740. * But it's not used by perf and confuses the syscall parameters.
  1741. */
  1742. static bool is_internal_field(struct tep_format_field *field)
  1743. {
  1744. return !strcmp(field->type, "__data_loc char[]");
  1745. }
  1746. static struct tep_format_field *
  1747. syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field *field,
  1748. bool *use_btf)
  1749. {
  1750. struct tep_format_field *last_field = NULL;
  1751. int len;
  1752. for (; field; field = field->next, ++arg) {
  1753. /* assume it's the last argument */
  1754. if (is_internal_field(field))
  1755. continue;
  1756. last_field = field;
  1757. if (arg->scnprintf)
  1758. continue;
  1759. len = strlen(field->name);
  1760. // As far as heuristics (or intention) goes this seems to hold true, and makes sense!
  1761. if ((field->flags & TEP_FIELD_IS_POINTER) && strstarts(field->type, "const "))
  1762. arg->from_user = true;
  1763. if (strcmp(field->type, "const char *") == 0 &&
  1764. ((len >= 4 && strcmp(field->name + len - 4, "name") == 0) ||
  1765. strstr(field->name, "path") != NULL)) {
  1766. arg->scnprintf = SCA_FILENAME;
  1767. } else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
  1768. arg->scnprintf = SCA_PTR;
  1769. else if (strcmp(field->type, "pid_t") == 0)
  1770. arg->scnprintf = SCA_PID;
  1771. else if (strcmp(field->type, "umode_t") == 0)
  1772. arg->scnprintf = SCA_MODE_T;
  1773. else if ((field->flags & TEP_FIELD_IS_ARRAY) && strstr(field->type, "char")) {
  1774. arg->scnprintf = SCA_CHAR_ARRAY;
  1775. arg->nr_entries = field->arraylen;
  1776. } else if ((strcmp(field->type, "int") == 0 ||
  1777. strcmp(field->type, "unsigned int") == 0 ||
  1778. strcmp(field->type, "long") == 0) &&
  1779. len >= 2 && strcmp(field->name + len - 2, "fd") == 0) {
  1780. /*
  1781. * /sys/kernel/tracing/events/syscalls/sys_enter*
  1782. * grep -E 'field:.*fd;' .../format|sed -r 's/.*field:([a-z ]+) [a-z_]*fd.+/\1/g'|sort|uniq -c
  1783. * 65 int
  1784. * 23 unsigned int
  1785. * 7 unsigned long
  1786. */
  1787. arg->scnprintf = SCA_FD;
  1788. } else if (strstr(field->type, "enum") && use_btf != NULL) {
  1789. *use_btf = true;
  1790. arg->strtoul = STUL_BTF_TYPE;
  1791. } else {
  1792. const struct syscall_arg_fmt *fmt =
  1793. syscall_arg_fmt__find_by_name(field->name);
  1794. if (fmt) {
  1795. arg->scnprintf = fmt->scnprintf;
  1796. arg->strtoul = fmt->strtoul;
  1797. }
  1798. }
  1799. }
  1800. return last_field;
  1801. }
  1802. static int syscall__set_arg_fmts(struct syscall *sc)
  1803. {
  1804. struct tep_format_field *last_field = syscall_arg_fmt__init_array(sc->arg_fmt, sc->args,
  1805. &sc->use_btf);
  1806. if (last_field)
  1807. sc->args_size = last_field->offset + last_field->size;
  1808. return 0;
  1809. }
  1810. static int syscall__read_info(struct syscall *sc, struct trace *trace)
  1811. {
  1812. char tp_name[128];
  1813. const char *name;
  1814. struct tep_format_field *field;
  1815. int err;
  1816. if (sc->nonexistent)
  1817. return -EEXIST;
  1818. if (sc->name) {
  1819. /* Info already read. */
  1820. return 0;
  1821. }
  1822. name = syscalltbl__name(sc->e_machine, sc->id);
  1823. if (name == NULL) {
  1824. sc->nonexistent = true;
  1825. return -EEXIST;
  1826. }
  1827. sc->name = name;
  1828. sc->fmt = syscall_fmt__find(sc->name);
  1829. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->name);
  1830. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1831. if (IS_ERR(sc->tp_format) && sc->fmt && sc->fmt->alias) {
  1832. snprintf(tp_name, sizeof(tp_name), "sys_enter_%s", sc->fmt->alias);
  1833. sc->tp_format = trace_event__tp_format("syscalls", tp_name);
  1834. }
  1835. /*
  1836. * Fails to read trace point format via sysfs node, so the trace point
  1837. * doesn't exist. Set the 'nonexistent' flag as true.
  1838. */
  1839. if (IS_ERR(sc->tp_format)) {
  1840. sc->nonexistent = true;
  1841. err = PTR_ERR(sc->tp_format);
  1842. sc->tp_format = NULL;
  1843. return err;
  1844. }
  1845. /*
  1846. * The tracepoint format contains __syscall_nr field, so it's one more
  1847. * than the actual number of syscall arguments.
  1848. */
  1849. if (syscall__alloc_arg_fmts(sc, sc->tp_format->format.nr_fields - 1))
  1850. return -ENOMEM;
  1851. sc->args = sc->tp_format->format.fields;
  1852. /*
  1853. * We need to check and discard the first variable '__syscall_nr'
  1854. * or 'nr' that mean the syscall number. It is needless here.
  1855. * So drop '__syscall_nr' or 'nr' field but does not exist on older kernels.
  1856. */
  1857. if (sc->args && (!strcmp(sc->args->name, "__syscall_nr") || !strcmp(sc->args->name, "nr"))) {
  1858. sc->args = sc->args->next;
  1859. --sc->nr_args;
  1860. }
  1861. field = sc->args;
  1862. while (field) {
  1863. if (is_internal_field(field))
  1864. --sc->nr_args;
  1865. field = field->next;
  1866. }
  1867. sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
  1868. sc->is_open = !strcmp(name, "open") || !strcmp(name, "openat");
  1869. err = syscall__set_arg_fmts(sc);
  1870. /* after calling syscall__set_arg_fmts() we'll know whether use_btf is true */
  1871. if (sc->use_btf)
  1872. trace__load_vmlinux_btf(trace);
  1873. return err;
  1874. }
  1875. static int evsel__init_tp_arg_scnprintf(struct evsel *evsel, bool *use_btf)
  1876. {
  1877. struct syscall_arg_fmt *fmt = evsel__syscall_arg_fmt(evsel);
  1878. if (fmt != NULL) {
  1879. const struct tep_event *tp_format = evsel__tp_format(evsel);
  1880. if (tp_format) {
  1881. syscall_arg_fmt__init_array(fmt, tp_format->format.fields, use_btf);
  1882. return 0;
  1883. }
  1884. }
  1885. return -ENOMEM;
  1886. }
  1887. static int intcmp(const void *a, const void *b)
  1888. {
  1889. const int *one = a, *another = b;
  1890. return *one - *another;
  1891. }
  1892. static int trace__validate_ev_qualifier(struct trace *trace)
  1893. {
  1894. int err = 0;
  1895. bool printed_invalid_prefix = false;
  1896. struct str_node *pos;
  1897. size_t nr_used = 0, nr_allocated = strlist__nr_entries(trace->ev_qualifier);
  1898. trace->ev_qualifier_ids.entries = malloc(nr_allocated *
  1899. sizeof(trace->ev_qualifier_ids.entries[0]));
  1900. if (trace->ev_qualifier_ids.entries == NULL) {
  1901. fputs("Error:\tNot enough memory for allocating events qualifier ids\n",
  1902. trace->output);
  1903. err = -EINVAL;
  1904. goto out;
  1905. }
  1906. strlist__for_each_entry(pos, trace->ev_qualifier) {
  1907. const char *sc = pos->s;
  1908. /*
  1909. * TODO: Assume more than the validation/warnings are all for
  1910. * the same binary type as perf.
  1911. */
  1912. int id = syscalltbl__id(EM_HOST, sc), match_next = -1;
  1913. if (id < 0) {
  1914. id = syscalltbl__strglobmatch_first(EM_HOST, sc, &match_next);
  1915. if (id >= 0)
  1916. goto matches;
  1917. if (!printed_invalid_prefix) {
  1918. pr_debug("Skipping unknown syscalls: ");
  1919. printed_invalid_prefix = true;
  1920. } else {
  1921. pr_debug(", ");
  1922. }
  1923. pr_debug("%s", sc);
  1924. continue;
  1925. }
  1926. matches:
  1927. trace->ev_qualifier_ids.entries[nr_used++] = id;
  1928. if (match_next == -1)
  1929. continue;
  1930. while (1) {
  1931. id = syscalltbl__strglobmatch_next(EM_HOST, sc, &match_next);
  1932. if (id < 0)
  1933. break;
  1934. if (nr_allocated == nr_used) {
  1935. void *entries;
  1936. nr_allocated += 8;
  1937. entries = realloc(trace->ev_qualifier_ids.entries,
  1938. nr_allocated * sizeof(trace->ev_qualifier_ids.entries[0]));
  1939. if (entries == NULL) {
  1940. err = -ENOMEM;
  1941. fputs("\nError:\t Not enough memory for parsing\n", trace->output);
  1942. goto out_free;
  1943. }
  1944. trace->ev_qualifier_ids.entries = entries;
  1945. }
  1946. trace->ev_qualifier_ids.entries[nr_used++] = id;
  1947. }
  1948. }
  1949. trace->ev_qualifier_ids.nr = nr_used;
  1950. qsort(trace->ev_qualifier_ids.entries, nr_used, sizeof(int), intcmp);
  1951. out:
  1952. if (printed_invalid_prefix)
  1953. pr_debug("\n");
  1954. return err;
  1955. out_free:
  1956. zfree(&trace->ev_qualifier_ids.entries);
  1957. trace->ev_qualifier_ids.nr = 0;
  1958. goto out;
  1959. }
  1960. static __maybe_unused bool trace__syscall_enabled(struct trace *trace, int id)
  1961. {
  1962. bool in_ev_qualifier;
  1963. if (trace->ev_qualifier_ids.nr == 0)
  1964. return true;
  1965. in_ev_qualifier = bsearch(&id, trace->ev_qualifier_ids.entries,
  1966. trace->ev_qualifier_ids.nr, sizeof(int), intcmp) != NULL;
  1967. if (in_ev_qualifier)
  1968. return !trace->not_ev_qualifier;
  1969. return trace->not_ev_qualifier;
  1970. }
  1971. /*
  1972. * args is to be interpreted as a series of longs but we need to handle
  1973. * 8-byte unaligned accesses. args points to raw_data within the event
  1974. * and raw_data is guaranteed to be 8-byte unaligned because it is
  1975. * preceded by raw_size which is a u32. So we need to copy args to a temp
  1976. * variable to read it. Most notably this avoids extended load instructions
  1977. * on unaligned addresses
  1978. */
  1979. unsigned long syscall_arg__val(struct syscall_arg *arg, u8 idx)
  1980. {
  1981. unsigned long val;
  1982. unsigned char *p = arg->args + sizeof(unsigned long) * idx;
  1983. memcpy(&val, p, sizeof(val));
  1984. return val;
  1985. }
  1986. static size_t syscall__scnprintf_name(struct syscall *sc, char *bf, size_t size,
  1987. struct syscall_arg *arg)
  1988. {
  1989. if (sc->arg_fmt && sc->arg_fmt[arg->idx].name)
  1990. return scnprintf(bf, size, "%s: ", sc->arg_fmt[arg->idx].name);
  1991. return scnprintf(bf, size, "arg%d: ", arg->idx);
  1992. }
  1993. /*
  1994. * Check if the value is in fact zero, i.e. mask whatever needs masking, such
  1995. * as mount 'flags' argument that needs ignoring some magic flag, see comment
  1996. * in tools/perf/trace/beauty/mount_flags.c
  1997. */
  1998. static unsigned long syscall_arg_fmt__mask_val(struct syscall_arg_fmt *fmt, struct syscall_arg *arg, unsigned long val)
  1999. {
  2000. if (fmt && fmt->mask_val)
  2001. return fmt->mask_val(arg, val);
  2002. return val;
  2003. }
  2004. static size_t syscall_arg_fmt__scnprintf_val(struct syscall_arg_fmt *fmt, char *bf, size_t size,
  2005. struct syscall_arg *arg, unsigned long val)
  2006. {
  2007. if (fmt && fmt->scnprintf) {
  2008. arg->val = val;
  2009. if (fmt->parm)
  2010. arg->parm = fmt->parm;
  2011. return fmt->scnprintf(bf, size, arg);
  2012. }
  2013. return scnprintf(bf, size, "%ld", val);
  2014. }
  2015. static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
  2016. unsigned char *args, void *augmented_args, int augmented_args_size,
  2017. struct trace *trace, struct thread *thread)
  2018. {
  2019. size_t printed = 0, btf_printed;
  2020. unsigned long val;
  2021. u8 bit = 1;
  2022. struct syscall_arg arg = {
  2023. .args = args,
  2024. .augmented = {
  2025. .size = augmented_args_size,
  2026. .args = augmented_args,
  2027. },
  2028. .idx = 0,
  2029. .mask = 0,
  2030. .trace = trace,
  2031. .thread = thread,
  2032. .show_string_prefix = trace->show_string_prefix,
  2033. };
  2034. struct thread_trace *ttrace = thread__priv(thread);
  2035. void *default_scnprintf;
  2036. /*
  2037. * Things like fcntl will set this in its 'cmd' formatter to pick the
  2038. * right formatter for the return value (an fd? file flags?), which is
  2039. * not needed for syscalls that always return a given type, say an fd.
  2040. */
  2041. ttrace->ret_scnprintf = NULL;
  2042. if (sc->args != NULL) {
  2043. struct tep_format_field *field;
  2044. for (field = sc->args; field;
  2045. field = field->next, ++arg.idx, bit <<= 1) {
  2046. if (arg.mask & bit)
  2047. continue;
  2048. arg.fmt = &sc->arg_fmt[arg.idx];
  2049. val = syscall_arg__val(&arg, arg.idx);
  2050. /*
  2051. * Some syscall args need some mask, most don't and
  2052. * return val untouched.
  2053. */
  2054. val = syscall_arg_fmt__mask_val(&sc->arg_fmt[arg.idx], &arg, val);
  2055. /*
  2056. * Suppress this argument if its value is zero and show_zero
  2057. * property isn't set.
  2058. *
  2059. * If it has a BTF type, then override the zero suppression knob
  2060. * as the common case is for zero in an enum to have an associated entry.
  2061. */
  2062. if (val == 0 && !trace->show_zeros &&
  2063. !(sc->arg_fmt && sc->arg_fmt[arg.idx].show_zero) &&
  2064. !(sc->arg_fmt && sc->arg_fmt[arg.idx].strtoul == STUL_BTF_TYPE))
  2065. continue;
  2066. printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
  2067. if (trace->show_arg_names)
  2068. printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
  2069. default_scnprintf = sc->arg_fmt[arg.idx].scnprintf;
  2070. if (trace->force_btf || default_scnprintf == NULL || default_scnprintf == SCA_PTR) {
  2071. btf_printed = trace__btf_scnprintf(trace, &arg, bf + printed,
  2072. size - printed, val, field->type);
  2073. if (btf_printed) {
  2074. printed += btf_printed;
  2075. continue;
  2076. }
  2077. }
  2078. printed += syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx],
  2079. bf + printed, size - printed, &arg, val);
  2080. }
  2081. } else if (IS_ERR(sc->tp_format)) {
  2082. /*
  2083. * If we managed to read the tracepoint /format file, then we
  2084. * may end up not having any args, like with gettid(), so only
  2085. * print the raw args when we didn't manage to read it.
  2086. */
  2087. while (arg.idx < sc->nr_args) {
  2088. if (arg.mask & bit)
  2089. goto next_arg;
  2090. val = syscall_arg__val(&arg, arg.idx);
  2091. if (printed)
  2092. printed += scnprintf(bf + printed, size - printed, ", ");
  2093. printed += syscall__scnprintf_name(sc, bf + printed, size - printed, &arg);
  2094. printed += syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx], bf + printed, size - printed, &arg, val);
  2095. next_arg:
  2096. ++arg.idx;
  2097. bit <<= 1;
  2098. }
  2099. }
  2100. return printed;
  2101. }
  2102. static struct syscall *syscall__new(int e_machine, int id)
  2103. {
  2104. struct syscall *sc = zalloc(sizeof(*sc));
  2105. if (!sc)
  2106. return NULL;
  2107. sc->e_machine = e_machine;
  2108. sc->id = id;
  2109. return sc;
  2110. }
  2111. static void syscall__delete(struct syscall *sc)
  2112. {
  2113. if (!sc)
  2114. return;
  2115. free(sc->arg_fmt);
  2116. free(sc);
  2117. }
  2118. static int syscall__bsearch_cmp(const void *key, const void *entry)
  2119. {
  2120. const struct syscall *a = key, *b = *((const struct syscall **)entry);
  2121. if (a->e_machine != b->e_machine)
  2122. return a->e_machine - b->e_machine;
  2123. return a->id - b->id;
  2124. }
  2125. static int syscall__cmp(const void *va, const void *vb)
  2126. {
  2127. const struct syscall *a = *((const struct syscall **)va);
  2128. const struct syscall *b = *((const struct syscall **)vb);
  2129. if (a->e_machine != b->e_machine)
  2130. return a->e_machine - b->e_machine;
  2131. return a->id - b->id;
  2132. }
  2133. static struct syscall *trace__find_syscall(struct trace *trace, int e_machine, int id)
  2134. {
  2135. struct syscall key = {
  2136. .e_machine = e_machine,
  2137. .id = id,
  2138. };
  2139. struct syscall *sc, **tmp;
  2140. if (trace->syscalls.table) {
  2141. struct syscall **sc_entry = bsearch(&key, trace->syscalls.table,
  2142. trace->syscalls.table_size,
  2143. sizeof(trace->syscalls.table[0]),
  2144. syscall__bsearch_cmp);
  2145. if (sc_entry)
  2146. return *sc_entry;
  2147. }
  2148. sc = syscall__new(e_machine, id);
  2149. if (!sc)
  2150. return NULL;
  2151. tmp = reallocarray(trace->syscalls.table, trace->syscalls.table_size + 1,
  2152. sizeof(trace->syscalls.table[0]));
  2153. if (!tmp) {
  2154. syscall__delete(sc);
  2155. return NULL;
  2156. }
  2157. trace->syscalls.table = tmp;
  2158. trace->syscalls.table[trace->syscalls.table_size++] = sc;
  2159. qsort(trace->syscalls.table, trace->syscalls.table_size, sizeof(trace->syscalls.table[0]),
  2160. syscall__cmp);
  2161. return sc;
  2162. }
  2163. typedef int (*tracepoint_handler)(struct trace *trace, struct evsel *evsel,
  2164. union perf_event *event,
  2165. struct perf_sample *sample);
  2166. static struct syscall *trace__syscall_info(struct trace *trace, struct evsel *evsel,
  2167. int e_machine, int id)
  2168. {
  2169. struct syscall *sc;
  2170. int err = 0;
  2171. if (id < 0) {
  2172. /*
  2173. * XXX: Noticed on x86_64, reproduced as far back as 3.0.36, haven't tried
  2174. * before that, leaving at a higher verbosity level till that is
  2175. * explained. Reproduced with plain ftrace with:
  2176. *
  2177. * echo 1 > /t/events/raw_syscalls/sys_exit/enable
  2178. * grep "NR -1 " /t/trace_pipe
  2179. *
  2180. * After generating some load on the machine.
  2181. */
  2182. if (verbose > 1) {
  2183. static u64 n;
  2184. fprintf(trace->output, "Invalid syscall %d id, skipping (%s, %" PRIu64 ") ...\n",
  2185. id, evsel__name(evsel), ++n);
  2186. }
  2187. return NULL;
  2188. }
  2189. err = -EINVAL;
  2190. sc = trace__find_syscall(trace, e_machine, id);
  2191. if (sc)
  2192. err = syscall__read_info(sc, trace);
  2193. if (err && verbose > 0) {
  2194. errno = -err;
  2195. fprintf(trace->output, "Problems reading syscall %d: %m", id);
  2196. if (sc && sc->name)
  2197. fprintf(trace->output, " (%s)", sc->name);
  2198. fputs(" information\n", trace->output);
  2199. }
  2200. return err ? NULL : sc;
  2201. }
  2202. struct syscall_stats {
  2203. struct stats stats;
  2204. u64 nr_failures;
  2205. int max_errno;
  2206. u32 *errnos;
  2207. };
  2208. static void thread__update_stats(struct thread *thread, struct thread_trace *ttrace,
  2209. int id, struct perf_sample *sample, long err,
  2210. struct trace *trace)
  2211. {
  2212. struct hashmap *syscall_stats = ttrace->syscall_stats;
  2213. struct syscall_stats *stats = NULL;
  2214. u64 duration = 0;
  2215. if (trace->summary_bpf)
  2216. return;
  2217. if (trace->summary_mode == SUMMARY__BY_TOTAL)
  2218. syscall_stats = trace->syscall_stats;
  2219. if (!hashmap__find(syscall_stats, id, &stats)) {
  2220. stats = zalloc(sizeof(*stats));
  2221. if (stats == NULL)
  2222. return;
  2223. init_stats(&stats->stats);
  2224. if (hashmap__add(syscall_stats, id, stats) < 0) {
  2225. free(stats);
  2226. return;
  2227. }
  2228. }
  2229. if (ttrace->entry_time && sample->time > ttrace->entry_time)
  2230. duration = sample->time - ttrace->entry_time;
  2231. update_stats(&stats->stats, duration);
  2232. if (err < 0) {
  2233. ++stats->nr_failures;
  2234. if (!trace->errno_summary)
  2235. return;
  2236. err = -err;
  2237. if (err > stats->max_errno) {
  2238. u32 *new_errnos = realloc(stats->errnos, err * sizeof(u32));
  2239. if (new_errnos) {
  2240. memset(new_errnos + stats->max_errno, 0, (err - stats->max_errno) * sizeof(u32));
  2241. } else {
  2242. pr_debug("Not enough memory for errno stats for thread \"%s\"(%d/%d), results will be incomplete\n",
  2243. thread__comm_str(thread), thread__pid(thread),
  2244. thread__tid(thread));
  2245. return;
  2246. }
  2247. stats->errnos = new_errnos;
  2248. stats->max_errno = err;
  2249. }
  2250. ++stats->errnos[err - 1];
  2251. }
  2252. }
  2253. static int trace__printf_interrupted_entry(struct trace *trace)
  2254. {
  2255. struct thread_trace *ttrace;
  2256. size_t printed;
  2257. int len;
  2258. if (trace->failure_only || trace->current == NULL)
  2259. return 0;
  2260. ttrace = thread__priv(trace->current);
  2261. if (!ttrace->entry_pending)
  2262. return 0;
  2263. printed = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
  2264. printed += len = fprintf(trace->output, "%s)", ttrace->entry_str);
  2265. if (len < trace->args_alignment - 4)
  2266. printed += fprintf(trace->output, "%-*s", trace->args_alignment - 4 - len, " ");
  2267. printed += fprintf(trace->output, " ...\n");
  2268. ttrace->entry_pending = false;
  2269. ++trace->nr_events_printed;
  2270. return printed;
  2271. }
  2272. static int trace__fprintf_sample(struct trace *trace, struct evsel *evsel,
  2273. struct perf_sample *sample, struct thread *thread)
  2274. {
  2275. int printed = 0;
  2276. if (trace->print_sample) {
  2277. double ts = (double)sample->time / NSEC_PER_MSEC;
  2278. printed += fprintf(trace->output, "%22s %10.3f %s %d/%d [%d]\n",
  2279. evsel__name(evsel), ts,
  2280. thread__comm_str(thread),
  2281. sample->pid, sample->tid, sample->cpu);
  2282. }
  2283. return printed;
  2284. }
  2285. static void *syscall__augmented_args(struct syscall *sc, struct perf_sample *sample, int *augmented_args_size, int raw_augmented_args_size)
  2286. {
  2287. /*
  2288. * For now with BPF raw_augmented we hook into raw_syscalls:sys_enter
  2289. * and there we get all 6 syscall args plus the tracepoint common fields
  2290. * that gets calculated at the start and the syscall_nr (another long).
  2291. * So we check if that is the case and if so don't look after the
  2292. * sc->args_size but always after the full raw_syscalls:sys_enter payload,
  2293. * which is fixed.
  2294. *
  2295. * We'll revisit this later to pass s->args_size to the BPF augmenter
  2296. * (now tools/perf/examples/bpf/augmented_raw_syscalls.c, so that it
  2297. * copies only what we need for each syscall, like what happens when we
  2298. * use syscalls:sys_enter_NAME, so that we reduce the kernel/userspace
  2299. * traffic to just what is needed for each syscall.
  2300. */
  2301. int args_size = raw_augmented_args_size ?: sc->args_size;
  2302. *augmented_args_size = sample->raw_size - args_size;
  2303. if (*augmented_args_size > 0) {
  2304. static uintptr_t argbuf[1024]; /* assuming single-threaded */
  2305. if ((size_t)(*augmented_args_size) > sizeof(argbuf))
  2306. return NULL;
  2307. /*
  2308. * The perf ring-buffer is 8-byte aligned but sample->raw_data
  2309. * is not because it's preceded by u32 size. Later, beautifier
  2310. * will use the augmented args with stricter alignments like in
  2311. * some struct. To make sure it's aligned, let's copy the args
  2312. * into a static buffer as it's single-threaded for now.
  2313. */
  2314. memcpy(argbuf, sample->raw_data + args_size, *augmented_args_size);
  2315. return argbuf;
  2316. }
  2317. return NULL;
  2318. }
  2319. static int trace__sys_enter(struct trace *trace, struct evsel *evsel,
  2320. union perf_event *event __maybe_unused,
  2321. struct perf_sample *sample)
  2322. {
  2323. char *msg;
  2324. void *args;
  2325. int printed = 0;
  2326. struct thread *thread;
  2327. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  2328. int augmented_args_size = 0, e_machine;
  2329. void *augmented_args = NULL;
  2330. struct syscall *sc;
  2331. struct thread_trace *ttrace;
  2332. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2333. e_machine = thread__e_machine(thread, trace->host, /*e_flags=*/NULL);
  2334. sc = trace__syscall_info(trace, evsel, e_machine, id);
  2335. if (sc == NULL)
  2336. goto out_put;
  2337. ttrace = thread__trace(thread, trace);
  2338. if (ttrace == NULL)
  2339. goto out_put;
  2340. trace__fprintf_sample(trace, evsel, sample, thread);
  2341. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  2342. if (ttrace->entry_str == NULL) {
  2343. ttrace->entry_str = malloc(trace__entry_str_size);
  2344. if (!ttrace->entry_str)
  2345. goto out_put;
  2346. }
  2347. if (!(trace->duration_filter || trace->summary_only || trace->min_stack))
  2348. trace__printf_interrupted_entry(trace);
  2349. /*
  2350. * If this is raw_syscalls.sys_enter, then it always comes with the 6 possible
  2351. * arguments, even if the syscall being handled, say "openat", uses only 4 arguments
  2352. * this breaks syscall__augmented_args() check for augmented args, as we calculate
  2353. * syscall->args_size using each syscalls:sys_enter_NAME tracefs format file,
  2354. * so when handling, say the openat syscall, we end up getting 6 args for the
  2355. * raw_syscalls:sys_enter event, when we expected just 4, we end up mistakenly
  2356. * thinking that the extra 2 u64 args are the augmented filename, so just check
  2357. * here and avoid using augmented syscalls when the evsel is the raw_syscalls one.
  2358. */
  2359. if (evsel != trace->syscalls.events.sys_enter)
  2360. augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
  2361. ttrace->entry_time = sample->time;
  2362. msg = ttrace->entry_str;
  2363. printed += scnprintf(msg + printed, trace__entry_str_size - printed, "%s(", sc->name);
  2364. printed += syscall__scnprintf_args(sc, msg + printed, trace__entry_str_size - printed,
  2365. args, augmented_args, augmented_args_size, trace, thread);
  2366. if (sc->is_exit) {
  2367. if (!(trace->duration_filter || trace->summary_only || trace->failure_only || trace->min_stack)) {
  2368. int alignment = 0;
  2369. trace__fprintf_entry_head(trace, thread, 0, false, ttrace->entry_time, trace->output);
  2370. printed = fprintf(trace->output, "%s)", ttrace->entry_str);
  2371. if (trace->args_alignment > printed)
  2372. alignment = trace->args_alignment - printed;
  2373. fprintf(trace->output, "%*s= ?\n", alignment, " ");
  2374. }
  2375. } else {
  2376. ttrace->entry_pending = true;
  2377. /* See trace__vfs_getname & trace__sys_exit */
  2378. ttrace->filename.pending_open = false;
  2379. }
  2380. if (trace->current != thread) {
  2381. thread__put(trace->current);
  2382. trace->current = thread__get(thread);
  2383. }
  2384. err = 0;
  2385. out_put:
  2386. thread__put(thread);
  2387. return err;
  2388. }
  2389. static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel,
  2390. struct perf_sample *sample)
  2391. {
  2392. struct thread_trace *ttrace;
  2393. struct thread *thread;
  2394. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
  2395. struct syscall *sc;
  2396. char msg[1024];
  2397. void *args, *augmented_args = NULL;
  2398. int augmented_args_size, e_machine;
  2399. size_t printed = 0;
  2400. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2401. e_machine = thread__e_machine(thread, trace->host, /*e_flags=*/NULL);
  2402. sc = trace__syscall_info(trace, evsel, e_machine, id);
  2403. if (sc == NULL)
  2404. goto out_put;
  2405. ttrace = thread__trace(thread, trace);
  2406. /*
  2407. * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
  2408. * and the rest of the beautifiers accessing it via struct syscall_arg touches it.
  2409. */
  2410. if (ttrace == NULL)
  2411. goto out_put;
  2412. args = perf_evsel__sc_tp_ptr(evsel, args, sample);
  2413. augmented_args = syscall__augmented_args(sc, sample, &augmented_args_size, trace->raw_augmented_syscalls_args_size);
  2414. printed += syscall__scnprintf_args(sc, msg, sizeof(msg), args, augmented_args, augmented_args_size, trace, thread);
  2415. fprintf(trace->output, "%.*s", (int)printed, msg);
  2416. err = 0;
  2417. out_put:
  2418. thread__put(thread);
  2419. return err;
  2420. }
  2421. static int trace__resolve_callchain(struct trace *trace, struct evsel *evsel,
  2422. struct perf_sample *sample,
  2423. struct callchain_cursor *cursor)
  2424. {
  2425. struct addr_location al;
  2426. int max_stack = evsel->core.attr.sample_max_stack ?
  2427. evsel->core.attr.sample_max_stack :
  2428. trace->max_stack;
  2429. int err = -1;
  2430. addr_location__init(&al);
  2431. if (machine__resolve(trace->host, &al, sample) < 0)
  2432. goto out;
  2433. err = thread__resolve_callchain(al.thread, cursor, evsel, sample, NULL, NULL, max_stack);
  2434. out:
  2435. addr_location__exit(&al);
  2436. return err;
  2437. }
  2438. static int trace__fprintf_callchain(struct trace *trace, struct perf_sample *sample)
  2439. {
  2440. /* TODO: user-configurable print_opts */
  2441. const unsigned int print_opts = EVSEL__PRINT_SYM |
  2442. EVSEL__PRINT_DSO |
  2443. EVSEL__PRINT_UNKNOWN_AS_ADDR;
  2444. return sample__fprintf_callchain(sample, 38, print_opts, get_tls_callchain_cursor(), symbol_conf.bt_stop_list, trace->output);
  2445. }
  2446. static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
  2447. union perf_event *event __maybe_unused,
  2448. struct perf_sample *sample)
  2449. {
  2450. long ret;
  2451. u64 duration = 0;
  2452. bool duration_calculated = false;
  2453. struct thread *thread;
  2454. int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
  2455. int alignment = trace->args_alignment, e_machine;
  2456. struct syscall *sc;
  2457. struct thread_trace *ttrace;
  2458. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2459. e_machine = thread__e_machine(thread, trace->host, /*e_flags=*/NULL);
  2460. sc = trace__syscall_info(trace, evsel, e_machine, id);
  2461. if (sc == NULL)
  2462. goto out_put;
  2463. ttrace = thread__trace(thread, trace);
  2464. if (ttrace == NULL)
  2465. goto out_put;
  2466. trace__fprintf_sample(trace, evsel, sample, thread);
  2467. ret = perf_evsel__sc_tp_uint(evsel, ret, sample);
  2468. if (trace->summary)
  2469. thread__update_stats(thread, ttrace, id, sample, ret, trace);
  2470. if (!trace->fd_path_disabled && sc->is_open && ret >= 0 && ttrace->filename.pending_open) {
  2471. trace__set_fd_pathname(thread, ret, ttrace->filename.name);
  2472. ttrace->filename.pending_open = false;
  2473. ++trace->stats.vfs_getname;
  2474. }
  2475. if (ttrace->entry_time) {
  2476. duration = sample->time - ttrace->entry_time;
  2477. if (trace__filter_duration(trace, duration))
  2478. goto out;
  2479. duration_calculated = true;
  2480. } else if (trace->duration_filter)
  2481. goto out;
  2482. if (sample->callchain) {
  2483. struct callchain_cursor *cursor = get_tls_callchain_cursor();
  2484. callchain_ret = trace__resolve_callchain(trace, evsel, sample, cursor);
  2485. if (callchain_ret == 0) {
  2486. if (cursor->nr < trace->min_stack)
  2487. goto out;
  2488. callchain_ret = 1;
  2489. }
  2490. }
  2491. if (trace->summary_only || (ret >= 0 && trace->failure_only))
  2492. goto out;
  2493. trace__fprintf_entry_head(trace, thread, duration, duration_calculated, ttrace->entry_time, trace->output);
  2494. if (ttrace->entry_pending) {
  2495. printed = fprintf(trace->output, "%s", ttrace->entry_str);
  2496. } else {
  2497. printed += fprintf(trace->output, " ... [");
  2498. color_fprintf(trace->output, PERF_COLOR_YELLOW, "continued");
  2499. printed += 9;
  2500. printed += fprintf(trace->output, "]: %s()", sc->name);
  2501. }
  2502. printed++; /* the closing ')' */
  2503. if (alignment > printed)
  2504. alignment -= printed;
  2505. else
  2506. alignment = 0;
  2507. fprintf(trace->output, ")%*s= ", alignment, " ");
  2508. if (sc->fmt == NULL) {
  2509. if (ret < 0)
  2510. goto errno_print;
  2511. signed_print:
  2512. fprintf(trace->output, "%ld", ret);
  2513. } else if (ret < 0) {
  2514. errno_print: {
  2515. char bf[STRERR_BUFSIZE];
  2516. struct perf_env *env = evsel__env(evsel) ?: &trace->host_env;
  2517. const char *emsg = str_error_r(-ret, bf, sizeof(bf));
  2518. const char *e = perf_env__arch_strerrno(env, err);
  2519. fprintf(trace->output, "-1 %s (%s)", e, emsg);
  2520. }
  2521. } else if (ret == 0 && sc->fmt->timeout)
  2522. fprintf(trace->output, "0 (Timeout)");
  2523. else if (ttrace->ret_scnprintf) {
  2524. char bf[1024];
  2525. struct syscall_arg arg = {
  2526. .val = ret,
  2527. .thread = thread,
  2528. .trace = trace,
  2529. };
  2530. ttrace->ret_scnprintf(bf, sizeof(bf), &arg);
  2531. ttrace->ret_scnprintf = NULL;
  2532. fprintf(trace->output, "%s", bf);
  2533. } else if (sc->fmt->hexret)
  2534. fprintf(trace->output, "%#lx", ret);
  2535. else if (sc->fmt->errpid) {
  2536. struct thread *child = machine__find_thread(trace->host, ret, ret);
  2537. fprintf(trace->output, "%ld", ret);
  2538. if (child != NULL) {
  2539. if (thread__comm_set(child))
  2540. fprintf(trace->output, " (%s)", thread__comm_str(child));
  2541. thread__put(child);
  2542. }
  2543. } else
  2544. goto signed_print;
  2545. fputc('\n', trace->output);
  2546. /*
  2547. * We only consider an 'event' for the sake of --max-events a non-filtered
  2548. * sys_enter + sys_exit and other tracepoint events.
  2549. */
  2550. if (++trace->nr_events_printed == trace->max_events && trace->max_events != ULONG_MAX)
  2551. interrupted = true;
  2552. if (callchain_ret > 0)
  2553. trace__fprintf_callchain(trace, sample);
  2554. else if (callchain_ret < 0)
  2555. pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel));
  2556. out:
  2557. ttrace->entry_pending = false;
  2558. err = 0;
  2559. out_put:
  2560. thread__put(thread);
  2561. return err;
  2562. }
  2563. static int trace__vfs_getname(struct trace *trace, struct evsel *evsel,
  2564. union perf_event *event __maybe_unused,
  2565. struct perf_sample *sample)
  2566. {
  2567. struct thread *thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2568. struct thread_trace *ttrace;
  2569. size_t filename_len, entry_str_len, to_move;
  2570. ssize_t remaining_space;
  2571. char *pos;
  2572. const char *filename = evsel__rawptr(evsel, sample, "pathname");
  2573. if (!thread)
  2574. goto out;
  2575. ttrace = thread__priv(thread);
  2576. if (!ttrace)
  2577. goto out_put;
  2578. filename_len = strlen(filename);
  2579. if (filename_len == 0)
  2580. goto out_put;
  2581. if (ttrace->filename.namelen < filename_len) {
  2582. char *f = realloc(ttrace->filename.name, filename_len + 1);
  2583. if (f == NULL)
  2584. goto out_put;
  2585. ttrace->filename.namelen = filename_len;
  2586. ttrace->filename.name = f;
  2587. }
  2588. strcpy(ttrace->filename.name, filename);
  2589. ttrace->filename.pending_open = true;
  2590. if (!ttrace->filename.ptr)
  2591. goto out_put;
  2592. entry_str_len = strlen(ttrace->entry_str);
  2593. remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
  2594. if (remaining_space <= 0)
  2595. goto out_put;
  2596. if (filename_len > (size_t)remaining_space) {
  2597. filename += filename_len - remaining_space;
  2598. filename_len = remaining_space;
  2599. }
  2600. to_move = entry_str_len - ttrace->filename.entry_str_pos + 1; /* \0 */
  2601. pos = ttrace->entry_str + ttrace->filename.entry_str_pos;
  2602. memmove(pos + filename_len, pos, to_move);
  2603. memcpy(pos, filename, filename_len);
  2604. ttrace->filename.ptr = 0;
  2605. ttrace->filename.entry_str_pos = 0;
  2606. out_put:
  2607. thread__put(thread);
  2608. out:
  2609. return 0;
  2610. }
  2611. static int trace__sched_stat_runtime(struct trace *trace, struct evsel *evsel,
  2612. union perf_event *event __maybe_unused,
  2613. struct perf_sample *sample)
  2614. {
  2615. u64 runtime = evsel__intval(evsel, sample, "runtime");
  2616. double runtime_ms = (double)runtime / NSEC_PER_MSEC;
  2617. struct thread *thread = machine__findnew_thread(trace->host,
  2618. sample->pid,
  2619. sample->tid);
  2620. struct thread_trace *ttrace = thread__trace(thread, trace);
  2621. if (ttrace == NULL)
  2622. goto out_dump;
  2623. ttrace->runtime_ms += runtime_ms;
  2624. trace->runtime_ms += runtime_ms;
  2625. out_put:
  2626. thread__put(thread);
  2627. return 0;
  2628. out_dump:
  2629. fprintf(trace->output, "%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
  2630. evsel->name,
  2631. evsel__strval(evsel, sample, "comm"),
  2632. (pid_t)evsel__intval(evsel, sample, "pid"),
  2633. runtime,
  2634. evsel__intval(evsel, sample, "vruntime"));
  2635. goto out_put;
  2636. }
  2637. static int bpf_output__printer(enum binary_printer_ops op,
  2638. unsigned int val, void *extra __maybe_unused, FILE *fp)
  2639. {
  2640. unsigned char ch = (unsigned char)val;
  2641. switch (op) {
  2642. case BINARY_PRINT_CHAR_DATA:
  2643. return fprintf(fp, "%c", isprint(ch) ? ch : '.');
  2644. case BINARY_PRINT_DATA_BEGIN:
  2645. case BINARY_PRINT_LINE_BEGIN:
  2646. case BINARY_PRINT_ADDR:
  2647. case BINARY_PRINT_NUM_DATA:
  2648. case BINARY_PRINT_NUM_PAD:
  2649. case BINARY_PRINT_SEP:
  2650. case BINARY_PRINT_CHAR_PAD:
  2651. case BINARY_PRINT_LINE_END:
  2652. case BINARY_PRINT_DATA_END:
  2653. default:
  2654. break;
  2655. }
  2656. return 0;
  2657. }
  2658. static void bpf_output__fprintf(struct trace *trace,
  2659. struct perf_sample *sample)
  2660. {
  2661. binary__fprintf(sample->raw_data, sample->raw_size, 8,
  2662. bpf_output__printer, NULL, trace->output);
  2663. ++trace->nr_events_printed;
  2664. }
  2665. static size_t trace__fprintf_tp_fields(struct trace *trace, struct evsel *evsel, struct perf_sample *sample,
  2666. struct thread *thread, void *augmented_args, int augmented_args_size)
  2667. {
  2668. char bf[2048];
  2669. size_t size = sizeof(bf);
  2670. const struct tep_event *tp_format = evsel__tp_format(evsel);
  2671. struct tep_format_field *field = tp_format ? tp_format->format.fields : NULL;
  2672. struct syscall_arg_fmt *arg = __evsel__syscall_arg_fmt(evsel);
  2673. size_t printed = 0, btf_printed;
  2674. unsigned long val;
  2675. u8 bit = 1;
  2676. struct syscall_arg syscall_arg = {
  2677. .augmented = {
  2678. .size = augmented_args_size,
  2679. .args = augmented_args,
  2680. },
  2681. .idx = 0,
  2682. .mask = 0,
  2683. .trace = trace,
  2684. .thread = thread,
  2685. .show_string_prefix = trace->show_string_prefix,
  2686. };
  2687. for (; field && arg; field = field->next, ++syscall_arg.idx, bit <<= 1, ++arg) {
  2688. if (syscall_arg.mask & bit)
  2689. continue;
  2690. syscall_arg.len = 0;
  2691. syscall_arg.fmt = arg;
  2692. if (field->flags & TEP_FIELD_IS_ARRAY) {
  2693. int offset = field->offset;
  2694. if (field->flags & TEP_FIELD_IS_DYNAMIC) {
  2695. offset = format_field__intval(field, sample, evsel->needs_swap);
  2696. syscall_arg.len = offset >> 16;
  2697. offset &= 0xffff;
  2698. if (tep_field_is_relative(field->flags))
  2699. offset += field->offset + field->size;
  2700. }
  2701. val = (uintptr_t)(sample->raw_data + offset);
  2702. } else
  2703. val = format_field__intval(field, sample, evsel->needs_swap);
  2704. /*
  2705. * Some syscall args need some mask, most don't and
  2706. * return val untouched.
  2707. */
  2708. val = syscall_arg_fmt__mask_val(arg, &syscall_arg, val);
  2709. /* Suppress this argument if its value is zero and show_zero property isn't set. */
  2710. if (val == 0 && !trace->show_zeros && !arg->show_zero && arg->strtoul != STUL_BTF_TYPE)
  2711. continue;
  2712. printed += scnprintf(bf + printed, size - printed, "%s", printed ? ", " : "");
  2713. if (trace->show_arg_names)
  2714. printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
  2715. btf_printed = trace__btf_scnprintf(trace, &syscall_arg, bf + printed, size - printed, val, field->type);
  2716. if (btf_printed) {
  2717. printed += btf_printed;
  2718. continue;
  2719. }
  2720. printed += syscall_arg_fmt__scnprintf_val(arg, bf + printed, size - printed, &syscall_arg, val);
  2721. }
  2722. return fprintf(trace->output, "%.*s", (int)printed, bf);
  2723. }
  2724. static int trace__event_handler(struct trace *trace, struct evsel *evsel,
  2725. union perf_event *event __maybe_unused,
  2726. struct perf_sample *sample)
  2727. {
  2728. struct thread *thread;
  2729. int callchain_ret = 0;
  2730. if (evsel->nr_events_printed >= evsel->max_events)
  2731. return 0;
  2732. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2733. if (sample->callchain) {
  2734. struct callchain_cursor *cursor = get_tls_callchain_cursor();
  2735. callchain_ret = trace__resolve_callchain(trace, evsel, sample, cursor);
  2736. if (callchain_ret == 0) {
  2737. if (cursor->nr < trace->min_stack)
  2738. goto out;
  2739. callchain_ret = 1;
  2740. }
  2741. }
  2742. trace__printf_interrupted_entry(trace);
  2743. trace__fprintf_tstamp(trace, sample->time, trace->output);
  2744. if (trace->trace_syscalls && trace->show_duration)
  2745. fprintf(trace->output, "( ): ");
  2746. if (thread)
  2747. trace__fprintf_comm_tid(trace, thread, trace->output);
  2748. if (evsel == trace->syscalls.events.bpf_output) {
  2749. int id = perf_evsel__sc_tp_uint(evsel, id, sample);
  2750. int e_machine = thread
  2751. ? thread__e_machine(thread, trace->host, /*e_flags=*/NULL)
  2752. : EM_HOST;
  2753. struct syscall *sc = trace__syscall_info(trace, evsel, e_machine, id);
  2754. if (sc) {
  2755. fprintf(trace->output, "%s(", sc->name);
  2756. trace__fprintf_sys_enter(trace, evsel, sample);
  2757. fputc(')', trace->output);
  2758. goto newline;
  2759. }
  2760. /*
  2761. * XXX: Not having the associated syscall info or not finding/adding
  2762. * the thread should never happen, but if it does...
  2763. * fall thru and print it as a bpf_output event.
  2764. */
  2765. }
  2766. fprintf(trace->output, "%s(", evsel->name);
  2767. if (evsel__is_bpf_output(evsel)) {
  2768. bpf_output__fprintf(trace, sample);
  2769. } else {
  2770. const struct tep_event *tp_format = evsel__tp_format(evsel);
  2771. if (tp_format && (strncmp(tp_format->name, "sys_enter_", 10) ||
  2772. trace__fprintf_sys_enter(trace, evsel, sample))) {
  2773. if (trace->libtraceevent_print) {
  2774. event_format__fprintf(tp_format, sample->cpu,
  2775. sample->raw_data, sample->raw_size,
  2776. trace->output);
  2777. } else {
  2778. trace__fprintf_tp_fields(trace, evsel, sample, thread, NULL, 0);
  2779. }
  2780. }
  2781. }
  2782. newline:
  2783. fprintf(trace->output, ")\n");
  2784. if (callchain_ret > 0)
  2785. trace__fprintf_callchain(trace, sample);
  2786. else if (callchain_ret < 0)
  2787. pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel));
  2788. ++trace->nr_events_printed;
  2789. if (evsel->max_events != ULONG_MAX && ++evsel->nr_events_printed == evsel->max_events) {
  2790. evsel__disable(evsel);
  2791. evsel__close(evsel);
  2792. }
  2793. out:
  2794. thread__put(thread);
  2795. return 0;
  2796. }
  2797. static void print_location(FILE *f, struct perf_sample *sample,
  2798. struct addr_location *al,
  2799. bool print_dso, bool print_sym)
  2800. {
  2801. if ((verbose > 0 || print_dso) && al->map)
  2802. fprintf(f, "%s@", dso__long_name(map__dso(al->map)));
  2803. if ((verbose > 0 || print_sym) && al->sym)
  2804. fprintf(f, "%s+0x%" PRIx64, al->sym->name,
  2805. al->addr - al->sym->start);
  2806. else if (al->map)
  2807. fprintf(f, "0x%" PRIx64, al->addr);
  2808. else
  2809. fprintf(f, "0x%" PRIx64, sample->addr);
  2810. }
  2811. static int trace__pgfault(struct trace *trace,
  2812. struct evsel *evsel,
  2813. union perf_event *event __maybe_unused,
  2814. struct perf_sample *sample)
  2815. {
  2816. struct thread *thread;
  2817. struct addr_location al;
  2818. char map_type = 'd';
  2819. struct thread_trace *ttrace;
  2820. int err = -1;
  2821. int callchain_ret = 0;
  2822. addr_location__init(&al);
  2823. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2824. if (sample->callchain) {
  2825. struct callchain_cursor *cursor = get_tls_callchain_cursor();
  2826. callchain_ret = trace__resolve_callchain(trace, evsel, sample, cursor);
  2827. if (callchain_ret == 0) {
  2828. if (cursor->nr < trace->min_stack)
  2829. goto out_put;
  2830. callchain_ret = 1;
  2831. }
  2832. }
  2833. ttrace = thread__trace(thread, trace);
  2834. if (ttrace == NULL)
  2835. goto out_put;
  2836. if (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ) {
  2837. ttrace->pfmaj++;
  2838. trace->pfmaj++;
  2839. } else {
  2840. ttrace->pfmin++;
  2841. trace->pfmin++;
  2842. }
  2843. if (trace->summary_only)
  2844. goto out;
  2845. thread__find_symbol(thread, sample->cpumode, sample->ip, &al);
  2846. trace__fprintf_entry_head(trace, thread, 0, true, sample->time, trace->output);
  2847. fprintf(trace->output, "%sfault [",
  2848. evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ?
  2849. "maj" : "min");
  2850. print_location(trace->output, sample, &al, false, true);
  2851. fprintf(trace->output, "] => ");
  2852. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  2853. if (!al.map) {
  2854. thread__find_symbol(thread, sample->cpumode, sample->addr, &al);
  2855. if (al.map)
  2856. map_type = 'x';
  2857. else
  2858. map_type = '?';
  2859. }
  2860. print_location(trace->output, sample, &al, true, false);
  2861. fprintf(trace->output, " (%c%c)\n", map_type, al.level);
  2862. if (callchain_ret > 0)
  2863. trace__fprintf_callchain(trace, sample);
  2864. else if (callchain_ret < 0)
  2865. pr_err("Problem processing %s callchain, skipping...\n", evsel__name(evsel));
  2866. ++trace->nr_events_printed;
  2867. out:
  2868. err = 0;
  2869. out_put:
  2870. thread__put(thread);
  2871. addr_location__exit(&al);
  2872. return err;
  2873. }
  2874. static void trace__set_base_time(struct trace *trace,
  2875. struct evsel *evsel,
  2876. struct perf_sample *sample)
  2877. {
  2878. /*
  2879. * BPF events were not setting PERF_SAMPLE_TIME, so be more robust
  2880. * and don't use sample->time unconditionally, we may end up having
  2881. * some other event in the future without PERF_SAMPLE_TIME for good
  2882. * reason, i.e. we may not be interested in its timestamps, just in
  2883. * it taking place, picking some piece of information when it
  2884. * appears in our event stream (vfs_getname comes to mind).
  2885. */
  2886. if (trace->base_time == 0 && !trace->full_time &&
  2887. (evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
  2888. trace->base_time = sample->time;
  2889. }
  2890. static int trace__process_sample(const struct perf_tool *tool,
  2891. union perf_event *event,
  2892. struct perf_sample *sample,
  2893. struct evsel *evsel,
  2894. struct machine *machine __maybe_unused)
  2895. {
  2896. struct trace *trace = container_of(tool, struct trace, tool);
  2897. struct thread *thread;
  2898. int err = 0;
  2899. tracepoint_handler handler = evsel->handler;
  2900. thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
  2901. if (thread && thread__is_filtered(thread))
  2902. goto out;
  2903. trace__set_base_time(trace, evsel, sample);
  2904. if (handler) {
  2905. ++trace->nr_events;
  2906. handler(trace, evsel, event, sample);
  2907. }
  2908. out:
  2909. thread__put(thread);
  2910. return err;
  2911. }
  2912. static int trace__record(struct trace *trace, int argc, const char **argv)
  2913. {
  2914. unsigned int rec_argc, i, j;
  2915. const char **rec_argv;
  2916. const char * const record_args[] = {
  2917. "record",
  2918. "-R",
  2919. "-m", "1024",
  2920. "-c", "1",
  2921. };
  2922. pid_t pid = getpid();
  2923. char *filter = asprintf__tp_filter_pids(1, &pid);
  2924. const char * const sc_args[] = { "-e", };
  2925. unsigned int sc_args_nr = ARRAY_SIZE(sc_args);
  2926. const char * const majpf_args[] = { "-e", "major-faults" };
  2927. unsigned int majpf_args_nr = ARRAY_SIZE(majpf_args);
  2928. const char * const minpf_args[] = { "-e", "minor-faults" };
  2929. unsigned int minpf_args_nr = ARRAY_SIZE(minpf_args);
  2930. int err = -1;
  2931. /* +3 is for the event string below and the pid filter */
  2932. rec_argc = ARRAY_SIZE(record_args) + sc_args_nr + 3 +
  2933. majpf_args_nr + minpf_args_nr + argc;
  2934. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  2935. if (rec_argv == NULL || filter == NULL)
  2936. goto out_free;
  2937. j = 0;
  2938. for (i = 0; i < ARRAY_SIZE(record_args); i++)
  2939. rec_argv[j++] = record_args[i];
  2940. if (trace->trace_syscalls) {
  2941. for (i = 0; i < sc_args_nr; i++)
  2942. rec_argv[j++] = sc_args[i];
  2943. /* event string may be different for older kernels - e.g., RHEL6 */
  2944. if (is_valid_tracepoint("raw_syscalls:sys_enter"))
  2945. rec_argv[j++] = "raw_syscalls:sys_enter,raw_syscalls:sys_exit";
  2946. else if (is_valid_tracepoint("syscalls:sys_enter"))
  2947. rec_argv[j++] = "syscalls:sys_enter,syscalls:sys_exit";
  2948. else {
  2949. pr_err("Neither raw_syscalls nor syscalls events exist.\n");
  2950. goto out_free;
  2951. }
  2952. }
  2953. rec_argv[j++] = "--filter";
  2954. rec_argv[j++] = filter;
  2955. if (trace->trace_pgfaults & TRACE_PFMAJ)
  2956. for (i = 0; i < majpf_args_nr; i++)
  2957. rec_argv[j++] = majpf_args[i];
  2958. if (trace->trace_pgfaults & TRACE_PFMIN)
  2959. for (i = 0; i < minpf_args_nr; i++)
  2960. rec_argv[j++] = minpf_args[i];
  2961. for (i = 0; i < (unsigned int)argc; i++)
  2962. rec_argv[j++] = argv[i];
  2963. err = cmd_record(j, rec_argv);
  2964. out_free:
  2965. free(filter);
  2966. free(rec_argv);
  2967. return err;
  2968. }
  2969. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp);
  2970. static size_t trace__fprintf_total_summary(struct trace *trace, FILE *fp);
  2971. static bool evlist__add_vfs_getname(struct evlist *evlist)
  2972. {
  2973. bool found = false;
  2974. struct evsel *evsel, *tmp;
  2975. struct parse_events_error err;
  2976. int ret;
  2977. parse_events_error__init(&err);
  2978. ret = parse_events(evlist, "probe:vfs_getname*", &err);
  2979. parse_events_error__exit(&err);
  2980. if (ret)
  2981. return false;
  2982. evlist__for_each_entry_safe(evlist, evsel, tmp) {
  2983. if (!strstarts(evsel__name(evsel), "probe:vfs_getname"))
  2984. continue;
  2985. if (evsel__field(evsel, "pathname")) {
  2986. evsel->handler = trace__vfs_getname;
  2987. found = true;
  2988. continue;
  2989. }
  2990. list_del_init(&evsel->core.node);
  2991. evsel->evlist = NULL;
  2992. evsel__delete(evsel);
  2993. }
  2994. return found;
  2995. }
  2996. static struct evsel *evsel__new_pgfault(u64 config)
  2997. {
  2998. struct evsel *evsel;
  2999. struct perf_event_attr attr = {
  3000. .type = PERF_TYPE_SOFTWARE,
  3001. .mmap_data = 1,
  3002. };
  3003. attr.config = config;
  3004. attr.sample_period = 1;
  3005. event_attr_init(&attr);
  3006. evsel = evsel__new(&attr);
  3007. if (evsel)
  3008. evsel->handler = trace__pgfault;
  3009. return evsel;
  3010. }
  3011. static void evlist__free_syscall_tp_fields(struct evlist *evlist)
  3012. {
  3013. struct evsel *evsel;
  3014. evlist__for_each_entry(evlist, evsel) {
  3015. evsel_trace__delete(evsel->priv);
  3016. evsel->priv = NULL;
  3017. }
  3018. }
  3019. static void trace__handle_event(struct trace *trace, union perf_event *event, struct perf_sample *sample)
  3020. {
  3021. const u32 type = event->header.type;
  3022. struct evsel *evsel;
  3023. if (type != PERF_RECORD_SAMPLE) {
  3024. trace__process_event(trace, trace->host, event, sample);
  3025. return;
  3026. }
  3027. evsel = evlist__id2evsel(trace->evlist, sample->id);
  3028. if (evsel == NULL) {
  3029. fprintf(trace->output, "Unknown tp ID %" PRIu64 ", skipping...\n", sample->id);
  3030. return;
  3031. }
  3032. if (evswitch__discard(&trace->evswitch, evsel))
  3033. return;
  3034. trace__set_base_time(trace, evsel, sample);
  3035. if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
  3036. sample->raw_data == NULL) {
  3037. fprintf(trace->output, "%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
  3038. evsel__name(evsel), sample->tid,
  3039. sample->cpu, sample->raw_size);
  3040. } else {
  3041. tracepoint_handler handler = evsel->handler;
  3042. handler(trace, evsel, event, sample);
  3043. }
  3044. if (trace->nr_events_printed >= trace->max_events && trace->max_events != ULONG_MAX)
  3045. interrupted = true;
  3046. }
  3047. static int trace__add_syscall_newtp(struct trace *trace)
  3048. {
  3049. int ret = -1;
  3050. struct evlist *evlist = trace->evlist;
  3051. struct evsel *sys_enter, *sys_exit;
  3052. sys_enter = perf_evsel__raw_syscall_newtp("sys_enter", trace__sys_enter);
  3053. if (sys_enter == NULL)
  3054. goto out;
  3055. if (perf_evsel__init_sc_tp_ptr_field(sys_enter, args))
  3056. goto out_delete_sys_enter;
  3057. sys_exit = perf_evsel__raw_syscall_newtp("sys_exit", trace__sys_exit);
  3058. if (sys_exit == NULL)
  3059. goto out_delete_sys_enter;
  3060. if (perf_evsel__init_sc_tp_uint_field(sys_exit, ret))
  3061. goto out_delete_sys_exit;
  3062. evsel__config_callchain(sys_enter, &trace->opts, &callchain_param);
  3063. evsel__config_callchain(sys_exit, &trace->opts, &callchain_param);
  3064. evlist__add(evlist, sys_enter);
  3065. evlist__add(evlist, sys_exit);
  3066. if (callchain_param.enabled && !trace->kernel_syscallchains) {
  3067. /*
  3068. * We're interested only in the user space callchain
  3069. * leading to the syscall, allow overriding that for
  3070. * debugging reasons using --kernel_syscall_callchains
  3071. */
  3072. sys_exit->core.attr.exclude_callchain_kernel = 1;
  3073. }
  3074. trace->syscalls.events.sys_enter = sys_enter;
  3075. trace->syscalls.events.sys_exit = sys_exit;
  3076. ret = 0;
  3077. out:
  3078. return ret;
  3079. out_delete_sys_exit:
  3080. evsel__delete_priv(sys_exit);
  3081. out_delete_sys_enter:
  3082. evsel__delete_priv(sys_enter);
  3083. goto out;
  3084. }
  3085. static int trace__set_ev_qualifier_tp_filter(struct trace *trace)
  3086. {
  3087. int err = -1;
  3088. struct evsel *sys_exit;
  3089. char *filter = asprintf_expr_inout_ints("id", !trace->not_ev_qualifier,
  3090. trace->ev_qualifier_ids.nr,
  3091. trace->ev_qualifier_ids.entries);
  3092. if (filter == NULL)
  3093. goto out_enomem;
  3094. if (!evsel__append_tp_filter(trace->syscalls.events.sys_enter, filter)) {
  3095. sys_exit = trace->syscalls.events.sys_exit;
  3096. err = evsel__append_tp_filter(sys_exit, filter);
  3097. }
  3098. free(filter);
  3099. out:
  3100. return err;
  3101. out_enomem:
  3102. errno = ENOMEM;
  3103. goto out;
  3104. }
  3105. #ifdef HAVE_LIBBPF_SUPPORT
  3106. static struct bpf_program *unaugmented_prog;
  3107. static int syscall_arg_fmt__cache_btf_struct(struct syscall_arg_fmt *arg_fmt, struct btf *btf, char *type)
  3108. {
  3109. int id;
  3110. if (arg_fmt->type != NULL)
  3111. return -1;
  3112. id = btf__find_by_name(btf, type);
  3113. if (id < 0)
  3114. return -1;
  3115. arg_fmt->type = btf__type_by_id(btf, id);
  3116. arg_fmt->type_id = id;
  3117. return 0;
  3118. }
  3119. static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace __maybe_unused,
  3120. struct syscall *sc,
  3121. const char *prog_name, const char *type)
  3122. {
  3123. struct bpf_program *prog;
  3124. if (prog_name == NULL) {
  3125. char default_prog_name[256];
  3126. scnprintf(default_prog_name, sizeof(default_prog_name), "tp/syscalls/sys_%s_%s", type, sc->name);
  3127. prog = augmented_syscalls__find_by_title(default_prog_name);
  3128. if (prog != NULL)
  3129. goto out_found;
  3130. if (sc->fmt && sc->fmt->alias) {
  3131. scnprintf(default_prog_name, sizeof(default_prog_name), "tp/syscalls/sys_%s_%s", type, sc->fmt->alias);
  3132. prog = augmented_syscalls__find_by_title(default_prog_name);
  3133. if (prog != NULL)
  3134. goto out_found;
  3135. }
  3136. goto out_unaugmented;
  3137. }
  3138. prog = augmented_syscalls__find_by_title(prog_name);
  3139. if (prog != NULL) {
  3140. out_found:
  3141. return prog;
  3142. }
  3143. pr_debug("Couldn't find BPF prog \"%s\" to associate with syscalls:sys_%s_%s, not augmenting it\n",
  3144. prog_name, type, sc->name);
  3145. out_unaugmented:
  3146. return unaugmented_prog;
  3147. }
  3148. static void trace__init_syscall_bpf_progs(struct trace *trace, int e_machine, int id)
  3149. {
  3150. struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, id);
  3151. if (sc == NULL)
  3152. return;
  3153. sc->bpf_prog.sys_enter = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_enter : NULL, "enter");
  3154. sc->bpf_prog.sys_exit = trace__find_syscall_bpf_prog(trace, sc, sc->fmt ? sc->fmt->bpf_prog_name.sys_exit : NULL, "exit");
  3155. }
  3156. static int trace__bpf_prog_sys_enter_fd(struct trace *trace, int e_machine, int id)
  3157. {
  3158. struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, id);
  3159. return sc ? bpf_program__fd(sc->bpf_prog.sys_enter) : bpf_program__fd(unaugmented_prog);
  3160. }
  3161. static int trace__bpf_prog_sys_exit_fd(struct trace *trace, int e_machine, int id)
  3162. {
  3163. struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, id);
  3164. return sc ? bpf_program__fd(sc->bpf_prog.sys_exit) : bpf_program__fd(unaugmented_prog);
  3165. }
  3166. static int trace__bpf_sys_enter_beauty_map(struct trace *trace, int e_machine, int key, unsigned int *beauty_array)
  3167. {
  3168. struct tep_format_field *field;
  3169. struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, key);
  3170. const struct btf_type *bt;
  3171. char *struct_offset, *tmp, name[32];
  3172. bool can_augment = false;
  3173. int i, cnt;
  3174. if (sc == NULL)
  3175. return -1;
  3176. trace__load_vmlinux_btf(trace);
  3177. if (trace->btf == NULL)
  3178. return -1;
  3179. for (i = 0, field = sc->args; field; ++i, field = field->next) {
  3180. // XXX We're only collecting pointer payloads _from_ user space
  3181. if (!sc->arg_fmt[i].from_user)
  3182. continue;
  3183. struct_offset = strstr(field->type, "struct ");
  3184. if (struct_offset == NULL)
  3185. struct_offset = strstr(field->type, "union ");
  3186. else
  3187. struct_offset++; // "union" is shorter
  3188. if (field->flags & TEP_FIELD_IS_POINTER && struct_offset) { /* struct or union (think BPF's attr arg) */
  3189. struct_offset += 6;
  3190. /* for 'struct foo *', we only want 'foo' */
  3191. for (tmp = struct_offset, cnt = 0; *tmp != ' ' && *tmp != '\0'; ++tmp, ++cnt) {
  3192. }
  3193. strncpy(name, struct_offset, cnt);
  3194. name[cnt] = '\0';
  3195. /* cache struct's btf_type and type_id */
  3196. if (syscall_arg_fmt__cache_btf_struct(&sc->arg_fmt[i], trace->btf, name))
  3197. continue;
  3198. bt = sc->arg_fmt[i].type;
  3199. beauty_array[i] = bt->size;
  3200. can_augment = true;
  3201. } else if (field->flags & TEP_FIELD_IS_POINTER && /* string */
  3202. strcmp(field->type, "const char *") == 0 &&
  3203. (strstr(field->name, "name") ||
  3204. strstr(field->name, "path") ||
  3205. strstr(field->name, "file") ||
  3206. strstr(field->name, "root") ||
  3207. strstr(field->name, "key") ||
  3208. strstr(field->name, "special") ||
  3209. strstr(field->name, "type") ||
  3210. strstr(field->name, "description"))) {
  3211. beauty_array[i] = 1;
  3212. can_augment = true;
  3213. } else if (field->flags & TEP_FIELD_IS_POINTER && /* buffer */
  3214. strstr(field->type, "char *") &&
  3215. (strstr(field->name, "buf") ||
  3216. strstr(field->name, "val") ||
  3217. strstr(field->name, "msg"))) {
  3218. int j;
  3219. struct tep_format_field *field_tmp;
  3220. /* find the size of the buffer that appears in pairs with buf */
  3221. for (j = 0, field_tmp = sc->args; field_tmp; ++j, field_tmp = field_tmp->next) {
  3222. if (!(field_tmp->flags & TEP_FIELD_IS_POINTER) && /* only integers */
  3223. (strstr(field_tmp->name, "count") ||
  3224. strstr(field_tmp->name, "siz") || /* size, bufsiz */
  3225. (strstr(field_tmp->name, "len") && strcmp(field_tmp->name, "filename")))) {
  3226. /* filename's got 'len' in it, we don't want that */
  3227. beauty_array[i] = -(j + 1);
  3228. can_augment = true;
  3229. break;
  3230. }
  3231. }
  3232. }
  3233. }
  3234. if (can_augment)
  3235. return 0;
  3236. return -1;
  3237. }
  3238. static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace *trace,
  3239. struct syscall *sc)
  3240. {
  3241. struct tep_format_field *field, *candidate_field;
  3242. /*
  3243. * We're only interested in syscalls that have a pointer:
  3244. */
  3245. for (field = sc->args; field; field = field->next) {
  3246. if (field->flags & TEP_FIELD_IS_POINTER)
  3247. goto try_to_find_pair;
  3248. }
  3249. return NULL;
  3250. try_to_find_pair:
  3251. for (int i = 0, num_idx = syscalltbl__num_idx(sc->e_machine); i < num_idx; ++i) {
  3252. int id = syscalltbl__id_at_idx(sc->e_machine, i);
  3253. struct syscall *pair = trace__syscall_info(trace, NULL, sc->e_machine, id);
  3254. struct bpf_program *pair_prog;
  3255. bool is_candidate = false;
  3256. if (pair == NULL || pair->id == sc->id ||
  3257. pair->bpf_prog.sys_enter == unaugmented_prog)
  3258. continue;
  3259. for (field = sc->args, candidate_field = pair->args;
  3260. field && candidate_field; field = field->next, candidate_field = candidate_field->next) {
  3261. bool is_pointer = field->flags & TEP_FIELD_IS_POINTER,
  3262. candidate_is_pointer = candidate_field->flags & TEP_FIELD_IS_POINTER;
  3263. if (is_pointer) {
  3264. if (!candidate_is_pointer) {
  3265. // The candidate just doesn't copies our pointer arg, might copy other pointers we want.
  3266. continue;
  3267. }
  3268. } else {
  3269. if (candidate_is_pointer) {
  3270. // The candidate might copy a pointer we don't have, skip it.
  3271. goto next_candidate;
  3272. }
  3273. continue;
  3274. }
  3275. if (strcmp(field->type, candidate_field->type))
  3276. goto next_candidate;
  3277. /*
  3278. * This is limited in the BPF program but sys_write
  3279. * uses "const char *" for its "buf" arg so we need to
  3280. * use some heuristic that is kinda future proof...
  3281. */
  3282. if (strcmp(field->type, "const char *") == 0 &&
  3283. !(strstr(field->name, "name") ||
  3284. strstr(field->name, "path") ||
  3285. strstr(field->name, "file") ||
  3286. strstr(field->name, "root") ||
  3287. strstr(field->name, "description")))
  3288. goto next_candidate;
  3289. is_candidate = true;
  3290. }
  3291. if (!is_candidate)
  3292. goto next_candidate;
  3293. /*
  3294. * Check if the tentative pair syscall augmenter has more pointers, if it has,
  3295. * then it may be collecting that and we then can't use it, as it would collect
  3296. * more than what is common to the two syscalls.
  3297. */
  3298. if (candidate_field) {
  3299. for (candidate_field = candidate_field->next; candidate_field; candidate_field = candidate_field->next)
  3300. if (candidate_field->flags & TEP_FIELD_IS_POINTER)
  3301. goto next_candidate;
  3302. }
  3303. pair_prog = pair->bpf_prog.sys_enter;
  3304. /*
  3305. * If the pair isn't enabled, then its bpf_prog.sys_enter will not
  3306. * have been searched for, so search it here and if it returns the
  3307. * unaugmented one, then ignore it, otherwise we'll reuse that BPF
  3308. * program for a filtered syscall on a non-filtered one.
  3309. *
  3310. * For instance, we have "!syscalls:sys_enter_renameat" and that is
  3311. * useful for "renameat2".
  3312. */
  3313. if (pair_prog == NULL) {
  3314. pair_prog = trace__find_syscall_bpf_prog(trace, pair, pair->fmt ? pair->fmt->bpf_prog_name.sys_enter : NULL, "enter");
  3315. if (pair_prog == unaugmented_prog)
  3316. goto next_candidate;
  3317. }
  3318. pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->name,
  3319. sc->name);
  3320. return pair_prog;
  3321. next_candidate:
  3322. continue;
  3323. }
  3324. return NULL;
  3325. }
  3326. static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace, int e_machine)
  3327. {
  3328. int map_enter_fd;
  3329. int map_exit_fd;
  3330. int beauty_map_fd;
  3331. int err = 0;
  3332. unsigned int beauty_array[6];
  3333. if (augmented_syscalls__get_map_fds(&map_enter_fd, &map_exit_fd, &beauty_map_fd) < 0)
  3334. return -1;
  3335. unaugmented_prog = augmented_syscalls__unaugmented();
  3336. for (int i = 0, num_idx = syscalltbl__num_idx(e_machine); i < num_idx; ++i) {
  3337. int prog_fd, key = syscalltbl__id_at_idx(e_machine, i);
  3338. if (!trace__syscall_enabled(trace, key))
  3339. continue;
  3340. trace__init_syscall_bpf_progs(trace, e_machine, key);
  3341. // It'll get at least the "!raw_syscalls:unaugmented"
  3342. prog_fd = trace__bpf_prog_sys_enter_fd(trace, e_machine, key);
  3343. err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
  3344. if (err)
  3345. break;
  3346. prog_fd = trace__bpf_prog_sys_exit_fd(trace, e_machine, key);
  3347. err = bpf_map_update_elem(map_exit_fd, &key, &prog_fd, BPF_ANY);
  3348. if (err)
  3349. break;
  3350. /* use beauty_map to tell BPF how many bytes to collect, set beauty_map's value here */
  3351. memset(beauty_array, 0, sizeof(beauty_array));
  3352. err = trace__bpf_sys_enter_beauty_map(trace, e_machine, key, (unsigned int *)beauty_array);
  3353. if (err)
  3354. continue;
  3355. err = bpf_map_update_elem(beauty_map_fd, &key, beauty_array, BPF_ANY);
  3356. if (err)
  3357. break;
  3358. }
  3359. /*
  3360. * Now lets do a second pass looking for enabled syscalls without
  3361. * an augmenter that have a signature that is a superset of another
  3362. * syscall with an augmenter so that we can auto-reuse it.
  3363. *
  3364. * I.e. if we have an augmenter for the "open" syscall that has
  3365. * this signature:
  3366. *
  3367. * int open(const char *pathname, int flags, mode_t mode);
  3368. *
  3369. * I.e. that will collect just the first string argument, then we
  3370. * can reuse it for the 'creat' syscall, that has this signature:
  3371. *
  3372. * int creat(const char *pathname, mode_t mode);
  3373. *
  3374. * and for:
  3375. *
  3376. * int stat(const char *pathname, struct stat *statbuf);
  3377. * int lstat(const char *pathname, struct stat *statbuf);
  3378. *
  3379. * Because the 'open' augmenter will collect the first arg as a string,
  3380. * and leave alone all the other args, which already helps with
  3381. * beautifying 'stat' and 'lstat''s pathname arg.
  3382. *
  3383. * Then, in time, when 'stat' gets an augmenter that collects both
  3384. * first and second arg (this one on the raw_syscalls:sys_exit prog
  3385. * array tail call, then that one will be used.
  3386. */
  3387. for (int i = 0, num_idx = syscalltbl__num_idx(e_machine); i < num_idx; ++i) {
  3388. int key = syscalltbl__id_at_idx(e_machine, i);
  3389. struct syscall *sc = trace__syscall_info(trace, NULL, e_machine, key);
  3390. struct bpf_program *pair_prog;
  3391. int prog_fd;
  3392. if (sc == NULL || sc->bpf_prog.sys_enter == NULL)
  3393. continue;
  3394. /*
  3395. * For now we're just reusing the sys_enter prog, and if it
  3396. * already has an augmenter, we don't need to find one.
  3397. */
  3398. if (sc->bpf_prog.sys_enter != unaugmented_prog)
  3399. continue;
  3400. /*
  3401. * Look at all the other syscalls for one that has a signature
  3402. * that is close enough that we can share:
  3403. */
  3404. pair_prog = trace__find_usable_bpf_prog_entry(trace, sc);
  3405. if (pair_prog == NULL)
  3406. continue;
  3407. sc->bpf_prog.sys_enter = pair_prog;
  3408. /*
  3409. * Update the BPF_MAP_TYPE_PROG_SHARED for raw_syscalls:sys_enter
  3410. * with the fd for the program we're reusing:
  3411. */
  3412. prog_fd = bpf_program__fd(sc->bpf_prog.sys_enter);
  3413. err = bpf_map_update_elem(map_enter_fd, &key, &prog_fd, BPF_ANY);
  3414. if (err)
  3415. break;
  3416. }
  3417. return err;
  3418. }
  3419. #else // !HAVE_LIBBPF_SUPPORT
  3420. static int trace__init_syscalls_bpf_prog_array_maps(struct trace *trace __maybe_unused,
  3421. int e_machine __maybe_unused)
  3422. {
  3423. return -1;
  3424. }
  3425. #endif // HAVE_LIBBPF_SUPPORT
  3426. static int trace__set_ev_qualifier_filter(struct trace *trace)
  3427. {
  3428. if (trace->syscalls.events.sys_enter)
  3429. return trace__set_ev_qualifier_tp_filter(trace);
  3430. return 0;
  3431. }
  3432. static int trace__set_filter_loop_pids(struct trace *trace)
  3433. {
  3434. unsigned int nr = 1, err;
  3435. pid_t pids[32] = {
  3436. getpid(),
  3437. };
  3438. struct thread *thread = machine__find_thread(trace->host, pids[0], pids[0]);
  3439. while (thread && nr < ARRAY_SIZE(pids)) {
  3440. struct thread *parent = machine__find_thread(trace->host,
  3441. thread__ppid(thread),
  3442. thread__ppid(thread));
  3443. if (parent == NULL)
  3444. break;
  3445. if (!strcmp(thread__comm_str(parent), "sshd") ||
  3446. strstarts(thread__comm_str(parent), "gnome-terminal")) {
  3447. pids[nr++] = thread__tid(parent);
  3448. thread__put(parent);
  3449. break;
  3450. }
  3451. thread__put(thread);
  3452. thread = parent;
  3453. }
  3454. thread__put(thread);
  3455. err = evlist__append_tp_filter_pids(trace->evlist, nr, pids);
  3456. if (!err)
  3457. err = augmented_syscalls__set_filter_pids(nr, pids);
  3458. return err;
  3459. }
  3460. static int trace__set_filter_pids(struct trace *trace)
  3461. {
  3462. int err = 0;
  3463. /*
  3464. * Better not use !target__has_task() here because we need to cover the
  3465. * case where no threads were specified in the command line, but a
  3466. * workload was, and in that case we will fill in the thread_map when
  3467. * we fork the workload in evlist__prepare_workload.
  3468. */
  3469. if (trace->filter_pids.nr > 0) {
  3470. err = evlist__append_tp_filter_pids(trace->evlist, trace->filter_pids.nr,
  3471. trace->filter_pids.entries);
  3472. if (!err) {
  3473. err = augmented_syscalls__set_filter_pids(trace->filter_pids.nr,
  3474. trace->filter_pids.entries);
  3475. }
  3476. } else if (perf_thread_map__pid(trace->evlist->core.threads, 0) == -1) {
  3477. err = trace__set_filter_loop_pids(trace);
  3478. }
  3479. return err;
  3480. }
  3481. static int __trace__deliver_event(struct trace *trace, union perf_event *event)
  3482. {
  3483. struct evlist *evlist = trace->evlist;
  3484. struct perf_sample sample;
  3485. int err;
  3486. perf_sample__init(&sample, /*all=*/false);
  3487. err = evlist__parse_sample(evlist, event, &sample);
  3488. if (err)
  3489. fprintf(trace->output, "Can't parse sample, err = %d, skipping...\n", err);
  3490. else
  3491. trace__handle_event(trace, event, &sample);
  3492. perf_sample__exit(&sample);
  3493. return 0;
  3494. }
  3495. static int __trace__flush_events(struct trace *trace)
  3496. {
  3497. u64 first = ordered_events__first_time(&trace->oe.data);
  3498. u64 flush = trace->oe.last - NSEC_PER_SEC;
  3499. /* Is there some thing to flush.. */
  3500. if (first && first < flush)
  3501. return ordered_events__flush_time(&trace->oe.data, flush);
  3502. return 0;
  3503. }
  3504. static int trace__flush_events(struct trace *trace)
  3505. {
  3506. return !trace->sort_events ? 0 : __trace__flush_events(trace);
  3507. }
  3508. static int trace__deliver_event(struct trace *trace, union perf_event *event)
  3509. {
  3510. int err;
  3511. if (!trace->sort_events)
  3512. return __trace__deliver_event(trace, event);
  3513. err = evlist__parse_sample_timestamp(trace->evlist, event, &trace->oe.last);
  3514. if (err && err != -1)
  3515. return err;
  3516. err = ordered_events__queue(&trace->oe.data, event, trace->oe.last, 0, NULL);
  3517. if (err)
  3518. return err;
  3519. return trace__flush_events(trace);
  3520. }
  3521. static int ordered_events__deliver_event(struct ordered_events *oe,
  3522. struct ordered_event *event)
  3523. {
  3524. struct trace *trace = container_of(oe, struct trace, oe.data);
  3525. return __trace__deliver_event(trace, event->event);
  3526. }
  3527. static struct syscall_arg_fmt *evsel__find_syscall_arg_fmt_by_name(struct evsel *evsel, char *arg,
  3528. char **type)
  3529. {
  3530. struct syscall_arg_fmt *fmt = __evsel__syscall_arg_fmt(evsel);
  3531. const struct tep_event *tp_format;
  3532. if (!fmt)
  3533. return NULL;
  3534. tp_format = evsel__tp_format(evsel);
  3535. if (!tp_format)
  3536. return NULL;
  3537. for (const struct tep_format_field *field = tp_format->format.fields; field;
  3538. field = field->next, ++fmt) {
  3539. if (strcmp(field->name, arg) == 0) {
  3540. *type = field->type;
  3541. return fmt;
  3542. }
  3543. }
  3544. return NULL;
  3545. }
  3546. static int trace__expand_filter(struct trace *trace, struct evsel *evsel)
  3547. {
  3548. char *tok, *left = evsel->filter, *new_filter = evsel->filter;
  3549. while ((tok = strpbrk(left, "=<>!")) != NULL) {
  3550. char *right = tok + 1, *right_end;
  3551. if (*right == '=')
  3552. ++right;
  3553. while (isspace(*right))
  3554. ++right;
  3555. if (*right == '\0')
  3556. break;
  3557. while (!isalpha(*left))
  3558. if (++left == tok) {
  3559. /*
  3560. * Bail out, can't find the name of the argument that is being
  3561. * used in the filter, let it try to set this filter, will fail later.
  3562. */
  3563. return 0;
  3564. }
  3565. right_end = right + 1;
  3566. while (isalnum(*right_end) || *right_end == '_' || *right_end == '|')
  3567. ++right_end;
  3568. if (isalpha(*right)) {
  3569. struct syscall_arg_fmt *fmt;
  3570. int left_size = tok - left,
  3571. right_size = right_end - right;
  3572. char arg[128], *type;
  3573. while (isspace(left[left_size - 1]))
  3574. --left_size;
  3575. scnprintf(arg, sizeof(arg), "%.*s", left_size, left);
  3576. fmt = evsel__find_syscall_arg_fmt_by_name(evsel, arg, &type);
  3577. if (fmt == NULL) {
  3578. pr_err("\"%s\" not found in \"%s\", can't set filter \"%s\"\n",
  3579. arg, evsel->name, evsel->filter);
  3580. return -1;
  3581. }
  3582. pr_debug2("trying to expand \"%s\" \"%.*s\" \"%.*s\" -> ",
  3583. arg, (int)(right - tok), tok, right_size, right);
  3584. if (fmt->strtoul) {
  3585. u64 val;
  3586. struct syscall_arg syscall_arg = {
  3587. .trace = trace,
  3588. .fmt = fmt,
  3589. .type_name = type,
  3590. .parm = fmt->parm,
  3591. };
  3592. if (fmt->strtoul(right, right_size, &syscall_arg, &val)) {
  3593. char *n, expansion[19];
  3594. int expansion_lenght = scnprintf(expansion, sizeof(expansion), "%#" PRIx64, val);
  3595. int expansion_offset = right - new_filter;
  3596. pr_debug("%s", expansion);
  3597. if (asprintf(&n, "%.*s%s%s", expansion_offset, new_filter, expansion, right_end) < 0) {
  3598. pr_debug(" out of memory!\n");
  3599. free(new_filter);
  3600. return -1;
  3601. }
  3602. if (new_filter != evsel->filter)
  3603. free(new_filter);
  3604. left = n + expansion_offset + expansion_lenght;
  3605. new_filter = n;
  3606. } else {
  3607. pr_err("\"%.*s\" not found for \"%s\" in \"%s\", can't set filter \"%s\"\n",
  3608. right_size, right, arg, evsel->name, evsel->filter);
  3609. return -1;
  3610. }
  3611. } else {
  3612. pr_err("No resolver (strtoul) for \"%s\" in \"%s\", can't set filter \"%s\"\n",
  3613. arg, evsel->name, evsel->filter);
  3614. return -1;
  3615. }
  3616. pr_debug("\n");
  3617. } else {
  3618. left = right_end;
  3619. }
  3620. }
  3621. if (new_filter != evsel->filter) {
  3622. pr_debug("New filter for %s: %s\n", evsel->name, new_filter);
  3623. evsel__set_filter(evsel, new_filter);
  3624. free(new_filter);
  3625. }
  3626. return 0;
  3627. }
  3628. static int trace__expand_filters(struct trace *trace, struct evsel **err_evsel)
  3629. {
  3630. struct evlist *evlist = trace->evlist;
  3631. struct evsel *evsel;
  3632. evlist__for_each_entry(evlist, evsel) {
  3633. if (evsel->filter == NULL)
  3634. continue;
  3635. if (trace__expand_filter(trace, evsel)) {
  3636. *err_evsel = evsel;
  3637. return -1;
  3638. }
  3639. }
  3640. return 0;
  3641. }
  3642. static int trace__run(struct trace *trace, int argc, const char **argv)
  3643. {
  3644. struct evlist *evlist = trace->evlist;
  3645. struct evsel *evsel, *pgfault_maj = NULL, *pgfault_min = NULL;
  3646. int err = -1, i;
  3647. unsigned long before;
  3648. const bool forks = argc > 0;
  3649. bool draining = false;
  3650. trace->live = true;
  3651. if (trace->summary_bpf) {
  3652. if (trace_prepare_bpf_summary(trace->summary_mode) < 0)
  3653. goto out_delete_evlist;
  3654. if (trace->summary_only)
  3655. goto create_maps;
  3656. }
  3657. if (!trace->raw_augmented_syscalls) {
  3658. if (trace->trace_syscalls && trace__add_syscall_newtp(trace))
  3659. goto out_error_raw_syscalls;
  3660. if (trace->trace_syscalls)
  3661. trace->vfs_getname = evlist__add_vfs_getname(evlist);
  3662. }
  3663. if ((trace->trace_pgfaults & TRACE_PFMAJ)) {
  3664. pgfault_maj = evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MAJ);
  3665. if (pgfault_maj == NULL)
  3666. goto out_error_mem;
  3667. evsel__config_callchain(pgfault_maj, &trace->opts, &callchain_param);
  3668. evlist__add(evlist, pgfault_maj);
  3669. }
  3670. if ((trace->trace_pgfaults & TRACE_PFMIN)) {
  3671. pgfault_min = evsel__new_pgfault(PERF_COUNT_SW_PAGE_FAULTS_MIN);
  3672. if (pgfault_min == NULL)
  3673. goto out_error_mem;
  3674. evsel__config_callchain(pgfault_min, &trace->opts, &callchain_param);
  3675. evlist__add(evlist, pgfault_min);
  3676. }
  3677. /* Enable ignoring missing threads when -p option is defined. */
  3678. trace->opts.ignore_missing_thread = trace->opts.target.pid;
  3679. if (trace->sched &&
  3680. evlist__add_newtp(evlist, "sched", "sched_stat_runtime", trace__sched_stat_runtime))
  3681. goto out_error_sched_stat_runtime;
  3682. /*
  3683. * If a global cgroup was set, apply it to all the events without an
  3684. * explicit cgroup. I.e.:
  3685. *
  3686. * trace -G A -e sched:*switch
  3687. *
  3688. * Will set all raw_syscalls:sys_{enter,exit}, pgfault, vfs_getname, etc
  3689. * _and_ sched:sched_switch to the 'A' cgroup, while:
  3690. *
  3691. * trace -e sched:*switch -G A
  3692. *
  3693. * will only set the sched:sched_switch event to the 'A' cgroup, all the
  3694. * other events (raw_syscalls:sys_{enter,exit}, etc are left "without"
  3695. * a cgroup (on the root cgroup, sys wide, etc).
  3696. *
  3697. * Multiple cgroups:
  3698. *
  3699. * trace -G A -e sched:*switch -G B
  3700. *
  3701. * the syscall ones go to the 'A' cgroup, the sched:sched_switch goes
  3702. * to the 'B' cgroup.
  3703. *
  3704. * evlist__set_default_cgroup() grabs a reference of the passed cgroup
  3705. * only for the evsels still without a cgroup, i.e. evsel->cgroup == NULL.
  3706. */
  3707. if (trace->cgroup)
  3708. evlist__set_default_cgroup(trace->evlist, trace->cgroup);
  3709. create_maps:
  3710. err = evlist__create_maps(evlist, &trace->opts.target);
  3711. if (err < 0) {
  3712. fprintf(trace->output, "Problems parsing the target to trace, check your options!\n");
  3713. goto out_delete_evlist;
  3714. }
  3715. err = trace__symbols_init(trace, argc, argv, evlist);
  3716. if (err < 0) {
  3717. fprintf(trace->output, "Problems initializing symbol libraries!\n");
  3718. goto out_delete_evlist;
  3719. }
  3720. if (trace->summary_mode == SUMMARY__BY_TOTAL && !trace->summary_bpf) {
  3721. trace->syscall_stats = alloc_syscall_stats();
  3722. if (IS_ERR(trace->syscall_stats))
  3723. goto out_delete_evlist;
  3724. }
  3725. evlist__config(evlist, &trace->opts, &callchain_param);
  3726. if (forks) {
  3727. err = evlist__prepare_workload(evlist, &trace->opts.target, argv, false, NULL);
  3728. if (err < 0) {
  3729. fprintf(trace->output, "Couldn't run the workload!\n");
  3730. goto out_delete_evlist;
  3731. }
  3732. workload_pid = evlist->workload.pid;
  3733. }
  3734. err = evlist__open(evlist);
  3735. if (err < 0)
  3736. goto out_error_open;
  3737. augmented_syscalls__setup_bpf_output();
  3738. err = trace__set_filter_pids(trace);
  3739. if (err < 0)
  3740. goto out_error_mem;
  3741. /*
  3742. * TODO: Initialize for all host binary machine types, not just
  3743. * those matching the perf binary.
  3744. */
  3745. trace__init_syscalls_bpf_prog_array_maps(trace, EM_HOST);
  3746. if (trace->ev_qualifier_ids.nr > 0) {
  3747. err = trace__set_ev_qualifier_filter(trace);
  3748. if (err < 0)
  3749. goto out_errno;
  3750. if (trace->syscalls.events.sys_exit) {
  3751. pr_debug("event qualifier tracepoint filter: %s\n",
  3752. trace->syscalls.events.sys_exit->filter);
  3753. }
  3754. }
  3755. /*
  3756. * If the "close" syscall is not traced, then we will not have the
  3757. * opportunity to, in syscall_arg__scnprintf_close_fd() invalidate the
  3758. * fd->pathname table and were ending up showing the last value set by
  3759. * syscalls opening a pathname and associating it with a descriptor or
  3760. * reading it from /proc/pid/fd/ in cases where that doesn't make
  3761. * sense.
  3762. *
  3763. * So just disable this beautifier (SCA_FD, SCA_FDAT) when 'close' is
  3764. * not in use.
  3765. */
  3766. /* TODO: support for more than just perf binary machine type close. */
  3767. trace->fd_path_disabled = !trace__syscall_enabled(trace, syscalltbl__id(EM_HOST, "close"));
  3768. err = trace__expand_filters(trace, &evsel);
  3769. if (err)
  3770. goto out_delete_evlist;
  3771. err = evlist__apply_filters(evlist, &evsel, &trace->opts.target);
  3772. if (err < 0)
  3773. goto out_error_apply_filters;
  3774. if (!trace->summary_only || !trace->summary_bpf) {
  3775. err = evlist__mmap(evlist, trace->opts.mmap_pages);
  3776. if (err < 0)
  3777. goto out_error_mmap;
  3778. }
  3779. if (!target__none(&trace->opts.target) && !trace->opts.target.initial_delay)
  3780. evlist__enable(evlist);
  3781. if (forks)
  3782. evlist__start_workload(evlist);
  3783. if (trace->opts.target.initial_delay) {
  3784. usleep(trace->opts.target.initial_delay * 1000);
  3785. evlist__enable(evlist);
  3786. }
  3787. if (trace->summary_bpf)
  3788. trace_start_bpf_summary();
  3789. trace->multiple_threads = perf_thread_map__pid(evlist->core.threads, 0) == -1 ||
  3790. perf_thread_map__nr(evlist->core.threads) > 1 ||
  3791. evlist__first(evlist)->core.attr.inherit;
  3792. /*
  3793. * Now that we already used evsel->core.attr to ask the kernel to setup the
  3794. * events, lets reuse evsel->core.attr.sample_max_stack as the limit in
  3795. * trace__resolve_callchain(), allowing per-event max-stack settings
  3796. * to override an explicitly set --max-stack global setting.
  3797. */
  3798. evlist__for_each_entry(evlist, evsel) {
  3799. if (evsel__has_callchain(evsel) &&
  3800. evsel->core.attr.sample_max_stack == 0)
  3801. evsel->core.attr.sample_max_stack = trace->max_stack;
  3802. }
  3803. again:
  3804. before = trace->nr_events;
  3805. for (i = 0; i < evlist->core.nr_mmaps; i++) {
  3806. union perf_event *event;
  3807. struct mmap *md;
  3808. md = &evlist->mmap[i];
  3809. if (perf_mmap__read_init(&md->core) < 0)
  3810. continue;
  3811. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  3812. ++trace->nr_events;
  3813. err = trace__deliver_event(trace, event);
  3814. if (err)
  3815. goto out_disable;
  3816. perf_mmap__consume(&md->core);
  3817. if (interrupted)
  3818. goto out_disable;
  3819. if (done && !draining) {
  3820. evlist__disable(evlist);
  3821. draining = true;
  3822. }
  3823. }
  3824. perf_mmap__read_done(&md->core);
  3825. }
  3826. if (trace->nr_events == before) {
  3827. int timeout = done ? 100 : -1;
  3828. if (!draining && evlist__poll(evlist, timeout) > 0) {
  3829. if (evlist__filter_pollfd(evlist, POLLERR | POLLHUP | POLLNVAL) == 0)
  3830. draining = true;
  3831. goto again;
  3832. } else {
  3833. if (trace__flush_events(trace))
  3834. goto out_disable;
  3835. }
  3836. } else {
  3837. goto again;
  3838. }
  3839. out_disable:
  3840. thread__zput(trace->current);
  3841. evlist__disable(evlist);
  3842. if (trace->summary_bpf)
  3843. trace_end_bpf_summary();
  3844. if (trace->sort_events)
  3845. ordered_events__flush(&trace->oe.data, OE_FLUSH__FINAL);
  3846. if (!err) {
  3847. if (trace->summary) {
  3848. if (trace->summary_bpf)
  3849. trace_print_bpf_summary(trace->output, trace->max_summary);
  3850. else if (trace->summary_mode == SUMMARY__BY_TOTAL)
  3851. trace__fprintf_total_summary(trace, trace->output);
  3852. else
  3853. trace__fprintf_thread_summary(trace, trace->output);
  3854. }
  3855. if (trace->show_tool_stats) {
  3856. fprintf(trace->output, "Stats:\n "
  3857. " vfs_getname : %" PRIu64 "\n"
  3858. " proc_getname: %" PRIu64 "\n",
  3859. trace->stats.vfs_getname,
  3860. trace->stats.proc_getname);
  3861. }
  3862. }
  3863. out_delete_evlist:
  3864. trace_cleanup_bpf_summary();
  3865. delete_syscall_stats(trace->syscall_stats);
  3866. trace__symbols__exit(trace);
  3867. evlist__free_syscall_tp_fields(evlist);
  3868. evlist__delete(evlist);
  3869. cgroup__put(trace->cgroup);
  3870. trace->evlist = NULL;
  3871. trace->live = false;
  3872. return err;
  3873. {
  3874. char errbuf[BUFSIZ];
  3875. out_error_sched_stat_runtime:
  3876. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "sched", "sched_stat_runtime");
  3877. goto out_error;
  3878. out_error_raw_syscalls:
  3879. tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "raw_syscalls", "sys_(enter|exit)");
  3880. goto out_error;
  3881. out_error_mmap:
  3882. evlist__strerror_mmap(evlist, errno, errbuf, sizeof(errbuf));
  3883. goto out_error;
  3884. out_error_open:
  3885. evlist__strerror_open(evlist, errno, errbuf, sizeof(errbuf));
  3886. out_error:
  3887. fprintf(trace->output, "%s\n", errbuf);
  3888. goto out_delete_evlist;
  3889. out_error_apply_filters:
  3890. fprintf(trace->output,
  3891. "Failed to set filter \"%s\" on event %s: %m\n",
  3892. evsel->filter, evsel__name(evsel));
  3893. goto out_delete_evlist;
  3894. }
  3895. out_error_mem:
  3896. fprintf(trace->output, "Not enough memory to run!\n");
  3897. goto out_delete_evlist;
  3898. out_errno:
  3899. fprintf(trace->output, "%m\n");
  3900. goto out_delete_evlist;
  3901. }
  3902. static int trace__replay(struct trace *trace)
  3903. {
  3904. const struct evsel_str_handler handlers[] = {
  3905. { "probe:vfs_getname", trace__vfs_getname, },
  3906. };
  3907. struct perf_data data = {
  3908. .path = input_name,
  3909. .mode = PERF_DATA_MODE_READ,
  3910. .force = trace->force,
  3911. };
  3912. struct perf_session *session;
  3913. struct evsel *evsel;
  3914. int err = -1;
  3915. perf_tool__init(&trace->tool, /*ordered_events=*/true);
  3916. trace->tool.sample = trace__process_sample;
  3917. trace->tool.mmap = perf_event__process_mmap;
  3918. trace->tool.mmap2 = perf_event__process_mmap2;
  3919. trace->tool.comm = perf_event__process_comm;
  3920. trace->tool.exit = perf_event__process_exit;
  3921. trace->tool.fork = perf_event__process_fork;
  3922. trace->tool.attr = perf_event__process_attr;
  3923. trace->tool.tracing_data = perf_event__process_tracing_data;
  3924. trace->tool.build_id = perf_event__process_build_id;
  3925. trace->tool.namespaces = perf_event__process_namespaces;
  3926. trace->tool.ordered_events = true;
  3927. trace->tool.ordering_requires_timestamps = true;
  3928. /* add tid to output */
  3929. trace->multiple_threads = true;
  3930. session = perf_session__new(&data, &trace->tool);
  3931. if (IS_ERR(session))
  3932. return PTR_ERR(session);
  3933. if (trace->opts.target.pid)
  3934. symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
  3935. if (trace->opts.target.tid)
  3936. symbol_conf.tid_list_str = strdup(trace->opts.target.tid);
  3937. if (symbol__init(perf_session__env(session)) < 0)
  3938. goto out;
  3939. trace->host = &session->machines.host;
  3940. err = perf_session__set_tracepoints_handlers(session, handlers);
  3941. if (err)
  3942. goto out;
  3943. evsel = evlist__find_tracepoint_by_name(session->evlist, "raw_syscalls:sys_enter");
  3944. trace->syscalls.events.sys_enter = evsel;
  3945. /* older kernels have syscalls tp versus raw_syscalls */
  3946. if (evsel == NULL)
  3947. evsel = evlist__find_tracepoint_by_name(session->evlist, "syscalls:sys_enter");
  3948. if (evsel &&
  3949. (evsel__init_raw_syscall_tp(evsel, trace__sys_enter) < 0 ||
  3950. perf_evsel__init_sc_tp_ptr_field(evsel, args))) {
  3951. pr_err("Error during initialize raw_syscalls:sys_enter event\n");
  3952. goto out;
  3953. }
  3954. evsel = evlist__find_tracepoint_by_name(session->evlist, "raw_syscalls:sys_exit");
  3955. trace->syscalls.events.sys_exit = evsel;
  3956. if (evsel == NULL)
  3957. evsel = evlist__find_tracepoint_by_name(session->evlist, "syscalls:sys_exit");
  3958. if (evsel &&
  3959. (evsel__init_raw_syscall_tp(evsel, trace__sys_exit) < 0 ||
  3960. perf_evsel__init_sc_tp_uint_field(evsel, ret))) {
  3961. pr_err("Error during initialize raw_syscalls:sys_exit event\n");
  3962. goto out;
  3963. }
  3964. evlist__for_each_entry(session->evlist, evsel) {
  3965. if (evsel->core.attr.type == PERF_TYPE_SOFTWARE &&
  3966. (evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MAJ ||
  3967. evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
  3968. evsel->core.attr.config == PERF_COUNT_SW_PAGE_FAULTS))
  3969. evsel->handler = trace__pgfault;
  3970. }
  3971. if (trace->summary_mode == SUMMARY__BY_TOTAL) {
  3972. trace->syscall_stats = alloc_syscall_stats();
  3973. if (IS_ERR(trace->syscall_stats))
  3974. goto out;
  3975. }
  3976. setup_pager();
  3977. err = perf_session__process_events(session);
  3978. if (err)
  3979. pr_err("Failed to process events, error %d", err);
  3980. else if (trace->summary)
  3981. trace__fprintf_thread_summary(trace, trace->output);
  3982. out:
  3983. delete_syscall_stats(trace->syscall_stats);
  3984. perf_session__delete(session);
  3985. return err;
  3986. }
  3987. static size_t trace__fprintf_summary_header(FILE *fp)
  3988. {
  3989. size_t printed;
  3990. printed = fprintf(fp, "\n Summary of events:\n\n");
  3991. return printed;
  3992. }
  3993. struct syscall_entry {
  3994. struct syscall_stats *stats;
  3995. double msecs;
  3996. int syscall;
  3997. };
  3998. static int entry_cmp(const void *e1, const void *e2)
  3999. {
  4000. const struct syscall_entry *entry1 = e1;
  4001. const struct syscall_entry *entry2 = e2;
  4002. return entry1->msecs > entry2->msecs ? -1 : 1;
  4003. }
  4004. static struct syscall_entry *syscall__sort_stats(struct hashmap *syscall_stats)
  4005. {
  4006. struct syscall_entry *entry;
  4007. struct hashmap_entry *pos;
  4008. unsigned bkt, i, nr;
  4009. nr = syscall_stats->sz;
  4010. entry = malloc(nr * sizeof(*entry));
  4011. if (entry == NULL)
  4012. return NULL;
  4013. i = 0;
  4014. hashmap__for_each_entry(syscall_stats, pos, bkt) {
  4015. struct syscall_stats *ss = pos->pvalue;
  4016. struct stats *st = &ss->stats;
  4017. entry[i].stats = ss;
  4018. entry[i].msecs = (u64)st->n * (avg_stats(st) / NSEC_PER_MSEC);
  4019. entry[i].syscall = pos->key;
  4020. i++;
  4021. }
  4022. assert(i == nr);
  4023. qsort(entry, nr, sizeof(*entry), entry_cmp);
  4024. return entry;
  4025. }
  4026. static size_t syscall__dump_stats(struct trace *trace, int e_machine, FILE *fp,
  4027. struct hashmap *syscall_stats)
  4028. {
  4029. size_t printed = 0;
  4030. int lines = 0;
  4031. struct syscall *sc;
  4032. struct syscall_entry *entries;
  4033. entries = syscall__sort_stats(syscall_stats);
  4034. if (entries == NULL)
  4035. return 0;
  4036. printed += fprintf(fp, "\n");
  4037. printed += fprintf(fp, " syscall calls errors total min avg max stddev\n");
  4038. printed += fprintf(fp, " (msec) (msec) (msec) (msec) (%%)\n");
  4039. printed += fprintf(fp, " --------------- -------- ------ -------- --------- --------- --------- ------\n");
  4040. for (size_t i = 0; i < syscall_stats->sz; i++) {
  4041. struct syscall_entry *entry = &entries[i];
  4042. struct syscall_stats *stats = entry->stats;
  4043. if (stats) {
  4044. double min = (double)(stats->stats.min) / NSEC_PER_MSEC;
  4045. double max = (double)(stats->stats.max) / NSEC_PER_MSEC;
  4046. double avg = avg_stats(&stats->stats);
  4047. double pct;
  4048. u64 n = (u64)stats->stats.n;
  4049. pct = avg ? 100.0 * stddev_stats(&stats->stats) / avg : 0.0;
  4050. avg /= NSEC_PER_MSEC;
  4051. sc = trace__syscall_info(trace, /*evsel=*/NULL, e_machine, entry->syscall);
  4052. if (!sc)
  4053. continue;
  4054. printed += fprintf(fp, " %-15s", sc->name);
  4055. printed += fprintf(fp, " %8" PRIu64 " %6" PRIu64 " %9.3f %9.3f %9.3f",
  4056. n, stats->nr_failures, entry->msecs, min, avg);
  4057. printed += fprintf(fp, " %9.3f %9.2f%%\n", max, pct);
  4058. if (trace->errno_summary && stats->nr_failures) {
  4059. int e;
  4060. for (e = 0; e < stats->max_errno; ++e) {
  4061. if (stats->errnos[e] != 0)
  4062. fprintf(fp, "\t\t\t\t%s: %d\n", perf_env__arch_strerrno(trace->host->env, e + 1), stats->errnos[e]);
  4063. }
  4064. }
  4065. lines++;
  4066. }
  4067. if (trace->max_summary && trace->max_summary <= lines)
  4068. break;
  4069. }
  4070. free(entries);
  4071. printed += fprintf(fp, "\n\n");
  4072. return printed;
  4073. }
  4074. static size_t thread__dump_stats(struct thread_trace *ttrace,
  4075. struct trace *trace, int e_machine, FILE *fp)
  4076. {
  4077. return syscall__dump_stats(trace, e_machine, fp, ttrace->syscall_stats);
  4078. }
  4079. static size_t system__dump_stats(struct trace *trace, int e_machine, FILE *fp)
  4080. {
  4081. return syscall__dump_stats(trace, e_machine, fp, trace->syscall_stats);
  4082. }
  4083. static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trace *trace)
  4084. {
  4085. size_t printed = 0;
  4086. struct thread_trace *ttrace = thread__priv(thread);
  4087. int e_machine = thread__e_machine(thread, trace->host, /*e_flags=*/NULL);
  4088. double ratio;
  4089. if (ttrace == NULL)
  4090. return 0;
  4091. ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;
  4092. printed += fprintf(fp, " %s (%d), ", thread__comm_str(thread), thread__tid(thread));
  4093. printed += fprintf(fp, "%lu events, ", ttrace->nr_events);
  4094. printed += fprintf(fp, "%.1f%%", ratio);
  4095. if (ttrace->pfmaj)
  4096. printed += fprintf(fp, ", %lu majfaults", ttrace->pfmaj);
  4097. if (ttrace->pfmin)
  4098. printed += fprintf(fp, ", %lu minfaults", ttrace->pfmin);
  4099. if (trace->sched)
  4100. printed += fprintf(fp, ", %.3f msec\n", ttrace->runtime_ms);
  4101. else if (fputc('\n', fp) != EOF)
  4102. ++printed;
  4103. printed += thread__dump_stats(ttrace, trace, e_machine, fp);
  4104. return printed;
  4105. }
  4106. static unsigned long thread__nr_events(struct thread_trace *ttrace)
  4107. {
  4108. return ttrace ? ttrace->nr_events : 0;
  4109. }
  4110. static int trace_nr_events_cmp(void *priv __maybe_unused,
  4111. const struct list_head *la,
  4112. const struct list_head *lb)
  4113. {
  4114. struct thread_list *a = list_entry(la, struct thread_list, list);
  4115. struct thread_list *b = list_entry(lb, struct thread_list, list);
  4116. unsigned long a_nr_events = thread__nr_events(thread__priv(a->thread));
  4117. unsigned long b_nr_events = thread__nr_events(thread__priv(b->thread));
  4118. if (a_nr_events != b_nr_events)
  4119. return a_nr_events < b_nr_events ? -1 : 1;
  4120. /* Identical number of threads, place smaller tids first. */
  4121. return thread__tid(a->thread) < thread__tid(b->thread)
  4122. ? -1
  4123. : (thread__tid(a->thread) > thread__tid(b->thread) ? 1 : 0);
  4124. }
  4125. static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
  4126. {
  4127. size_t printed = trace__fprintf_summary_header(fp);
  4128. LIST_HEAD(threads);
  4129. if (machine__thread_list(trace->host, &threads) == 0) {
  4130. struct thread_list *pos;
  4131. list_sort(NULL, &threads, trace_nr_events_cmp);
  4132. list_for_each_entry(pos, &threads, list)
  4133. printed += trace__fprintf_thread(fp, pos->thread, trace);
  4134. }
  4135. thread_list__delete(&threads);
  4136. return printed;
  4137. }
  4138. static size_t trace__fprintf_total_summary(struct trace *trace, FILE *fp)
  4139. {
  4140. size_t printed = trace__fprintf_summary_header(fp);
  4141. printed += fprintf(fp, " total, ");
  4142. printed += fprintf(fp, "%lu events", trace->nr_events);
  4143. if (trace->pfmaj)
  4144. printed += fprintf(fp, ", %lu majfaults", trace->pfmaj);
  4145. if (trace->pfmin)
  4146. printed += fprintf(fp, ", %lu minfaults", trace->pfmin);
  4147. if (trace->sched)
  4148. printed += fprintf(fp, ", %.3f msec\n", trace->runtime_ms);
  4149. else if (fputc('\n', fp) != EOF)
  4150. ++printed;
  4151. /* TODO: get all system e_machines. */
  4152. printed += system__dump_stats(trace, EM_HOST, fp);
  4153. return printed;
  4154. }
  4155. static int trace__set_duration(const struct option *opt, const char *str,
  4156. int unset __maybe_unused)
  4157. {
  4158. struct trace *trace = opt->value;
  4159. trace->duration_filter = atof(str);
  4160. return 0;
  4161. }
  4162. static int trace__set_filter_pids_from_option(const struct option *opt, const char *str,
  4163. int unset __maybe_unused)
  4164. {
  4165. int ret = -1;
  4166. size_t i;
  4167. struct trace *trace = opt->value;
  4168. /*
  4169. * FIXME: introduce a intarray class, plain parse csv and create a
  4170. * { int nr, int entries[] } struct...
  4171. */
  4172. struct intlist *list = intlist__new(str);
  4173. if (list == NULL)
  4174. return -1;
  4175. i = trace->filter_pids.nr = intlist__nr_entries(list) + 1;
  4176. trace->filter_pids.entries = calloc(i, sizeof(pid_t));
  4177. if (trace->filter_pids.entries == NULL)
  4178. goto out;
  4179. trace->filter_pids.entries[0] = getpid();
  4180. for (i = 1; i < trace->filter_pids.nr; ++i)
  4181. trace->filter_pids.entries[i] = intlist__entry(list, i - 1)->i;
  4182. intlist__delete(list);
  4183. ret = 0;
  4184. out:
  4185. return ret;
  4186. }
  4187. static int trace__open_output(struct trace *trace, const char *filename)
  4188. {
  4189. struct stat st;
  4190. if (!stat(filename, &st) && st.st_size) {
  4191. char oldname[PATH_MAX];
  4192. scnprintf(oldname, sizeof(oldname), "%s.old", filename);
  4193. unlink(oldname);
  4194. rename(filename, oldname);
  4195. }
  4196. trace->output = fopen(filename, "w");
  4197. return trace->output == NULL ? -errno : 0;
  4198. }
  4199. static int parse_pagefaults(const struct option *opt, const char *str,
  4200. int unset __maybe_unused)
  4201. {
  4202. int *trace_pgfaults = opt->value;
  4203. if (strcmp(str, "all") == 0)
  4204. *trace_pgfaults |= TRACE_PFMAJ | TRACE_PFMIN;
  4205. else if (strcmp(str, "maj") == 0)
  4206. *trace_pgfaults |= TRACE_PFMAJ;
  4207. else if (strcmp(str, "min") == 0)
  4208. *trace_pgfaults |= TRACE_PFMIN;
  4209. else
  4210. return -1;
  4211. return 0;
  4212. }
  4213. static void evlist__set_default_evsel_handler(struct evlist *evlist, void *handler)
  4214. {
  4215. struct evsel *evsel;
  4216. evlist__for_each_entry(evlist, evsel) {
  4217. if (evsel->handler == NULL)
  4218. evsel->handler = handler;
  4219. }
  4220. }
  4221. static void evsel__set_syscall_arg_fmt(struct evsel *evsel, const char *name)
  4222. {
  4223. struct syscall_arg_fmt *fmt = evsel__syscall_arg_fmt(evsel);
  4224. if (fmt) {
  4225. const struct syscall_fmt *scfmt = syscall_fmt__find(name);
  4226. if (scfmt) {
  4227. const struct tep_event *tp_format = evsel__tp_format(evsel);
  4228. if (tp_format) {
  4229. int skip = 0;
  4230. if (strcmp(tp_format->format.fields->name, "__syscall_nr") == 0 ||
  4231. strcmp(tp_format->format.fields->name, "nr") == 0)
  4232. ++skip;
  4233. memcpy(fmt + skip, scfmt->arg,
  4234. (tp_format->format.nr_fields - skip) * sizeof(*fmt));
  4235. }
  4236. }
  4237. }
  4238. }
  4239. static int evlist__set_syscall_tp_fields(struct evlist *evlist, bool *use_btf)
  4240. {
  4241. struct evsel *evsel;
  4242. evlist__for_each_entry(evlist, evsel) {
  4243. const struct tep_event *tp_format;
  4244. if (evsel->priv)
  4245. continue;
  4246. tp_format = evsel__tp_format(evsel);
  4247. if (!tp_format)
  4248. continue;
  4249. if (strcmp(tp_format->system, "syscalls")) {
  4250. evsel__init_tp_arg_scnprintf(evsel, use_btf);
  4251. continue;
  4252. }
  4253. if (evsel__init_syscall_tp(evsel))
  4254. return -1;
  4255. if (!strncmp(tp_format->name, "sys_enter_", 10)) {
  4256. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  4257. if (__tp_field__init_ptr(&sc->args, sc->id.offset + sizeof(u64)))
  4258. return -1;
  4259. evsel__set_syscall_arg_fmt(evsel,
  4260. tp_format->name + sizeof("sys_enter_") - 1);
  4261. } else if (!strncmp(tp_format->name, "sys_exit_", 9)) {
  4262. struct syscall_tp *sc = __evsel__syscall_tp(evsel);
  4263. if (__tp_field__init_uint(&sc->ret, sizeof(u64),
  4264. sc->id.offset + sizeof(u64),
  4265. evsel->needs_swap))
  4266. return -1;
  4267. evsel__set_syscall_arg_fmt(evsel,
  4268. tp_format->name + sizeof("sys_exit_") - 1);
  4269. }
  4270. }
  4271. return 0;
  4272. }
  4273. /*
  4274. * XXX: Hackish, just splitting the combined -e+--event (syscalls
  4275. * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
  4276. * existing facilities unchanged (trace->ev_qualifier + parse_options()).
  4277. *
  4278. * It'd be better to introduce a parse_options() variant that would return a
  4279. * list with the terms it didn't match to an event...
  4280. */
  4281. static int trace__parse_events_option(const struct option *opt, const char *str,
  4282. int unset __maybe_unused)
  4283. {
  4284. struct trace *trace = (struct trace *)opt->value;
  4285. const char *s;
  4286. char *strd, *sep = NULL, *lists[2] = { NULL, NULL, };
  4287. int len = strlen(str) + 1, err = -1, list, idx;
  4288. char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
  4289. char group_name[PATH_MAX];
  4290. const struct syscall_fmt *fmt;
  4291. if (strace_groups_dir == NULL)
  4292. return -1;
  4293. s = strd = strdup(str);
  4294. if (strd == NULL)
  4295. return -1;
  4296. if (*s == '!') {
  4297. ++s;
  4298. trace->not_ev_qualifier = true;
  4299. }
  4300. while (1) {
  4301. if ((sep = strchr((char *)s, ',')) != NULL)
  4302. *sep = '\0';
  4303. list = 0;
  4304. /* TODO: support for more than just perf binary machine type syscalls. */
  4305. if (syscalltbl__id(EM_HOST, s) >= 0 ||
  4306. syscalltbl__strglobmatch_first(EM_HOST, s, &idx) >= 0) {
  4307. list = 1;
  4308. goto do_concat;
  4309. }
  4310. fmt = syscall_fmt__find_by_alias(s);
  4311. if (fmt != NULL) {
  4312. list = 1;
  4313. s = fmt->name;
  4314. } else {
  4315. path__join(group_name, sizeof(group_name), strace_groups_dir, s);
  4316. if (access(group_name, R_OK) == 0)
  4317. list = 1;
  4318. }
  4319. do_concat:
  4320. if (lists[list]) {
  4321. sprintf(lists[list] + strlen(lists[list]), ",%s", s);
  4322. } else {
  4323. lists[list] = malloc(len);
  4324. if (lists[list] == NULL)
  4325. goto out;
  4326. strcpy(lists[list], s);
  4327. }
  4328. if (!sep)
  4329. break;
  4330. *sep = ',';
  4331. s = sep + 1;
  4332. }
  4333. if (lists[1] != NULL) {
  4334. struct strlist_config slist_config = {
  4335. .dirname = strace_groups_dir,
  4336. };
  4337. trace->ev_qualifier = strlist__new(lists[1], &slist_config);
  4338. if (trace->ev_qualifier == NULL) {
  4339. fputs("Not enough memory to parse event qualifier", trace->output);
  4340. goto out;
  4341. }
  4342. if (trace__validate_ev_qualifier(trace))
  4343. goto out;
  4344. trace->trace_syscalls = true;
  4345. }
  4346. err = 0;
  4347. if (lists[0]) {
  4348. struct parse_events_option_args parse_events_option_args = {
  4349. .evlistp = &trace->evlist,
  4350. };
  4351. struct option o = {
  4352. .value = &parse_events_option_args,
  4353. };
  4354. err = parse_events_option(&o, lists[0], 0);
  4355. }
  4356. out:
  4357. free(strace_groups_dir);
  4358. free(lists[0]);
  4359. free(lists[1]);
  4360. free(strd);
  4361. return err;
  4362. }
  4363. static int trace__parse_cgroups(const struct option *opt, const char *str, int unset)
  4364. {
  4365. struct trace *trace = opt->value;
  4366. if (!list_empty(&trace->evlist->core.entries)) {
  4367. struct option o = {
  4368. .value = &trace->evlist,
  4369. };
  4370. return parse_cgroups(&o, str, unset);
  4371. }
  4372. trace->cgroup = evlist__findnew_cgroup(trace->evlist, str);
  4373. return 0;
  4374. }
  4375. static int trace__parse_summary_mode(const struct option *opt, const char *str,
  4376. int unset __maybe_unused)
  4377. {
  4378. struct trace *trace = opt->value;
  4379. if (!strcmp(str, "thread")) {
  4380. trace->summary_mode = SUMMARY__BY_THREAD;
  4381. } else if (!strcmp(str, "total")) {
  4382. trace->summary_mode = SUMMARY__BY_TOTAL;
  4383. } else if (!strcmp(str, "cgroup")) {
  4384. trace->summary_mode = SUMMARY__BY_CGROUP;
  4385. } else {
  4386. pr_err("Unknown summary mode: %s\n", str);
  4387. return -1;
  4388. }
  4389. return 0;
  4390. }
  4391. static int trace__config(const char *var, const char *value, void *arg)
  4392. {
  4393. struct trace *trace = arg;
  4394. int err = 0;
  4395. if (!strcmp(var, "trace.add_events")) {
  4396. trace->perfconfig_events = strdup(value);
  4397. if (trace->perfconfig_events == NULL) {
  4398. pr_err("Not enough memory for %s\n", "trace.add_events");
  4399. return -1;
  4400. }
  4401. } else if (!strcmp(var, "trace.show_timestamp")) {
  4402. trace->show_tstamp = perf_config_bool(var, value);
  4403. } else if (!strcmp(var, "trace.show_duration")) {
  4404. trace->show_duration = perf_config_bool(var, value);
  4405. } else if (!strcmp(var, "trace.show_arg_names")) {
  4406. trace->show_arg_names = perf_config_bool(var, value);
  4407. if (!trace->show_arg_names)
  4408. trace->show_zeros = true;
  4409. } else if (!strcmp(var, "trace.show_zeros")) {
  4410. bool new_show_zeros = perf_config_bool(var, value);
  4411. if (!trace->show_arg_names && !new_show_zeros) {
  4412. pr_warning("trace.show_zeros has to be set when trace.show_arg_names=no\n");
  4413. goto out;
  4414. }
  4415. trace->show_zeros = new_show_zeros;
  4416. } else if (!strcmp(var, "trace.show_prefix")) {
  4417. trace->show_string_prefix = perf_config_bool(var, value);
  4418. } else if (!strcmp(var, "trace.no_inherit")) {
  4419. trace->opts.no_inherit = perf_config_bool(var, value);
  4420. } else if (!strcmp(var, "trace.args_alignment")) {
  4421. int args_alignment = 0;
  4422. if (perf_config_int(&args_alignment, var, value) == 0)
  4423. trace->args_alignment = args_alignment;
  4424. } else if (!strcmp(var, "trace.tracepoint_beautifiers")) {
  4425. if (strcasecmp(value, "libtraceevent") == 0)
  4426. trace->libtraceevent_print = true;
  4427. else if (strcasecmp(value, "libbeauty") == 0)
  4428. trace->libtraceevent_print = false;
  4429. }
  4430. out:
  4431. return err;
  4432. }
  4433. static void trace__exit(struct trace *trace)
  4434. {
  4435. thread__zput(trace->current);
  4436. strlist__delete(trace->ev_qualifier);
  4437. zfree(&trace->ev_qualifier_ids.entries);
  4438. if (trace->syscalls.table) {
  4439. for (size_t i = 0; i < trace->syscalls.table_size; i++)
  4440. syscall__delete(trace->syscalls.table[i]);
  4441. zfree(&trace->syscalls.table);
  4442. }
  4443. zfree(&trace->perfconfig_events);
  4444. evlist__delete(trace->evlist);
  4445. trace->evlist = NULL;
  4446. ordered_events__free(&trace->oe.data);
  4447. #ifdef HAVE_LIBBPF_SUPPORT
  4448. btf__free(trace->btf);
  4449. trace->btf = NULL;
  4450. #endif
  4451. }
  4452. int cmd_trace(int argc, const char **argv)
  4453. {
  4454. const char *trace_usage[] = {
  4455. "perf trace [<options>] [<command>]",
  4456. "perf trace [<options>] -- <command> [<options>]",
  4457. "perf trace record [<options>] [<command>]",
  4458. "perf trace record [<options>] -- <command> [<options>]",
  4459. NULL
  4460. };
  4461. struct trace trace = {
  4462. .opts = {
  4463. .target = {
  4464. .uses_mmap = true,
  4465. },
  4466. .user_freq = UINT_MAX,
  4467. .user_interval = ULLONG_MAX,
  4468. .no_buffering = true,
  4469. .mmap_pages = UINT_MAX,
  4470. },
  4471. .output = stderr,
  4472. .show_comm = true,
  4473. .show_tstamp = true,
  4474. .show_duration = true,
  4475. .show_arg_names = true,
  4476. .args_alignment = 70,
  4477. .trace_syscalls = false,
  4478. .kernel_syscallchains = false,
  4479. .max_stack = UINT_MAX,
  4480. .max_events = ULONG_MAX,
  4481. };
  4482. const char *output_name = NULL;
  4483. const struct option trace_options[] = {
  4484. OPT_CALLBACK('e', "event", &trace, "event",
  4485. "event/syscall selector. use 'perf list' to list available events",
  4486. trace__parse_events_option),
  4487. OPT_CALLBACK(0, "filter", &trace.evlist, "filter",
  4488. "event filter", parse_filter),
  4489. OPT_BOOLEAN(0, "comm", &trace.show_comm,
  4490. "show the thread COMM next to its id"),
  4491. OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
  4492. OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
  4493. trace__parse_events_option),
  4494. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  4495. OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
  4496. OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
  4497. "trace events on existing process id"),
  4498. OPT_STRING('t', "tid", &trace.opts.target.tid, "tid",
  4499. "trace events on existing thread id"),
  4500. OPT_CALLBACK(0, "filter-pids", &trace, "CSV list of pids",
  4501. "pids to filter (by the kernel)", trace__set_filter_pids_from_option),
  4502. OPT_BOOLEAN('a', "all-cpus", &trace.opts.target.system_wide,
  4503. "system-wide collection from all CPUs"),
  4504. OPT_STRING('C', "cpu", &trace.opts.target.cpu_list, "cpu",
  4505. "list of cpus to monitor"),
  4506. OPT_BOOLEAN(0, "no-inherit", &trace.opts.no_inherit,
  4507. "child tasks do not inherit counters"),
  4508. OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages",
  4509. "number of mmap data pages", evlist__parse_mmap_pages),
  4510. OPT_STRING('u', "uid", &trace.uid_str, "user", "user to profile"),
  4511. OPT_CALLBACK(0, "duration", &trace, "float",
  4512. "show only events with duration > N.M ms",
  4513. trace__set_duration),
  4514. OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
  4515. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  4516. OPT_BOOLEAN('T', "time", &trace.full_time,
  4517. "Show full timestamp, not time relative to first start"),
  4518. OPT_BOOLEAN(0, "failure", &trace.failure_only,
  4519. "Show only syscalls that failed"),
  4520. OPT_BOOLEAN('s', "summary", &trace.summary_only,
  4521. "Show only syscall summary with statistics"),
  4522. OPT_BOOLEAN('S', "with-summary", &trace.summary,
  4523. "Show all syscalls and summary with statistics"),
  4524. OPT_BOOLEAN(0, "errno-summary", &trace.errno_summary,
  4525. "Show errno stats per syscall, use with -s or -S"),
  4526. OPT_CALLBACK(0, "summary-mode", &trace, "mode",
  4527. "How to show summary: select thread (default), total or cgroup",
  4528. trace__parse_summary_mode),
  4529. OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
  4530. "Trace pagefaults", parse_pagefaults, "maj"),
  4531. OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
  4532. OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
  4533. OPT_CALLBACK(0, "call-graph", &trace.opts,
  4534. "record_mode[,record_size]", record_callchain_help,
  4535. &record_parse_callchain_opt),
  4536. OPT_BOOLEAN(0, "libtraceevent_print", &trace.libtraceevent_print,
  4537. "Use libtraceevent to print the tracepoint arguments."),
  4538. OPT_BOOLEAN(0, "kernel-syscall-graph", &trace.kernel_syscallchains,
  4539. "Show the kernel callchains on the syscall exit path"),
  4540. OPT_ULONG(0, "max-events", &trace.max_events,
  4541. "Set the maximum number of events to print, exit after that is reached. "),
  4542. OPT_UINTEGER(0, "min-stack", &trace.min_stack,
  4543. "Set the minimum stack depth when parsing the callchain, "
  4544. "anything below the specified depth will be ignored."),
  4545. OPT_UINTEGER(0, "max-stack", &trace.max_stack,
  4546. "Set the maximum stack depth when parsing the callchain, "
  4547. "anything beyond the specified depth will be ignored. "
  4548. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  4549. OPT_BOOLEAN(0, "sort-events", &trace.sort_events,
  4550. "Sort batch of events before processing, use if getting out of order events"),
  4551. OPT_BOOLEAN(0, "print-sample", &trace.print_sample,
  4552. "print the PERF_RECORD_SAMPLE PERF_SAMPLE_ info, for debugging"),
  4553. OPT_UINTEGER(0, "proc-map-timeout", &proc_map_timeout,
  4554. "per thread proc mmap processing timeout in ms"),
  4555. OPT_CALLBACK('G', "cgroup", &trace, "name", "monitor event in cgroup name only",
  4556. trace__parse_cgroups),
  4557. OPT_INTEGER('D', "delay", &trace.opts.target.initial_delay,
  4558. "ms to wait before starting measurement after program "
  4559. "start"),
  4560. OPT_BOOLEAN(0, "force-btf", &trace.force_btf, "Prefer btf_dump general pretty printer"
  4561. "to customized ones"),
  4562. OPT_BOOLEAN(0, "bpf-summary", &trace.summary_bpf, "Summary syscall stats in BPF"),
  4563. OPT_INTEGER(0, "max-summary", &trace.max_summary,
  4564. "Max number of entries in the summary."),
  4565. OPTS_EVSWITCH(&trace.evswitch),
  4566. OPT_END()
  4567. };
  4568. bool __maybe_unused max_stack_user_set = true;
  4569. bool mmap_pages_user_set = true;
  4570. struct evsel *evsel;
  4571. const char * const trace_subcommands[] = { "record", NULL };
  4572. int err = -1;
  4573. char bf[BUFSIZ];
  4574. struct sigaction sigchld_act;
  4575. signal(SIGSEGV, sighandler_dump_stack);
  4576. signal(SIGFPE, sighandler_dump_stack);
  4577. signal(SIGINT, sighandler_interrupt);
  4578. memset(&sigchld_act, 0, sizeof(sigchld_act));
  4579. sigchld_act.sa_flags = SA_SIGINFO;
  4580. sigchld_act.sa_sigaction = sighandler_chld;
  4581. sigaction(SIGCHLD, &sigchld_act, NULL);
  4582. ordered_events__init(&trace.oe.data, ordered_events__deliver_event, &trace);
  4583. ordered_events__set_copy_on_queue(&trace.oe.data, true);
  4584. trace.evlist = evlist__new();
  4585. if (trace.evlist == NULL) {
  4586. pr_err("Not enough memory to run!\n");
  4587. err = -ENOMEM;
  4588. goto out;
  4589. }
  4590. /*
  4591. * Parsing .perfconfig may entail creating a BPF event, that may need
  4592. * to create BPF maps, so bump RLIM_MEMLOCK as the default 64K setting
  4593. * is too small. This affects just this process, not touching the
  4594. * global setting. If it fails we'll get something in 'perf trace -v'
  4595. * to help diagnose the problem.
  4596. */
  4597. rlimit__bump_memlock();
  4598. err = perf_config(trace__config, &trace);
  4599. if (err)
  4600. goto out;
  4601. argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
  4602. trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  4603. /*
  4604. * Here we already passed thru trace__parse_events_option() and it has
  4605. * already figured out if -e syscall_name, if not but if --event
  4606. * foo:bar was used, the user is interested _just_ in those, say,
  4607. * tracepoint events, not in the strace-like syscall-name-based mode.
  4608. *
  4609. * This is important because we need to check if strace-like mode is
  4610. * needed to decided if we should filter out the eBPF
  4611. * __augmented_syscalls__ code, if it is in the mix, say, via
  4612. * .perfconfig trace.add_events, and filter those out.
  4613. */
  4614. if (!trace.trace_syscalls && !trace.trace_pgfaults &&
  4615. trace.evlist->core.nr_entries == 0 /* Was --events used? */) {
  4616. trace.trace_syscalls = true;
  4617. }
  4618. /*
  4619. * Now that we have --verbose figured out, lets see if we need to parse
  4620. * events from .perfconfig, so that if those events fail parsing, say some
  4621. * BPF program fails, then we'll be able to use --verbose to see what went
  4622. * wrong in more detail.
  4623. */
  4624. if (trace.perfconfig_events != NULL) {
  4625. struct parse_events_error parse_err;
  4626. parse_events_error__init(&parse_err);
  4627. err = parse_events(trace.evlist, trace.perfconfig_events, &parse_err);
  4628. if (err)
  4629. parse_events_error__print(&parse_err, trace.perfconfig_events);
  4630. parse_events_error__exit(&parse_err);
  4631. if (err)
  4632. goto out;
  4633. }
  4634. if ((nr_cgroups || trace.cgroup) && !trace.opts.target.system_wide) {
  4635. usage_with_options_msg(trace_usage, trace_options,
  4636. "cgroup monitoring only available in system-wide mode");
  4637. }
  4638. if (!trace.trace_syscalls)
  4639. goto skip_augmentation;
  4640. if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) {
  4641. pr_debug("Syscall augmentation fails with record, disabling augmentation");
  4642. goto skip_augmentation;
  4643. }
  4644. if (trace.summary_bpf) {
  4645. if (!trace.opts.target.system_wide) {
  4646. /* TODO: Add filters in the BPF to support other targets. */
  4647. pr_err("Error: --bpf-summary only works for system-wide mode.\n");
  4648. goto out;
  4649. }
  4650. if (trace.summary_only)
  4651. goto skip_augmentation;
  4652. }
  4653. err = augmented_syscalls__prepare();
  4654. if (err < 0)
  4655. goto skip_augmentation;
  4656. trace__add_syscall_newtp(&trace);
  4657. err = augmented_syscalls__create_bpf_output(trace.evlist);
  4658. if (err == 0)
  4659. trace.syscalls.events.bpf_output = evlist__last(trace.evlist);
  4660. skip_augmentation:
  4661. err = -1;
  4662. if (trace.trace_pgfaults) {
  4663. trace.opts.sample_address = true;
  4664. trace.opts.sample_time = true;
  4665. }
  4666. if (trace.opts.mmap_pages == UINT_MAX)
  4667. mmap_pages_user_set = false;
  4668. if (trace.max_stack == UINT_MAX) {
  4669. trace.max_stack = input_name ? PERF_MAX_STACK_DEPTH : sysctl__max_stack();
  4670. max_stack_user_set = false;
  4671. }
  4672. #ifdef HAVE_DWARF_UNWIND_SUPPORT
  4673. if ((trace.min_stack || max_stack_user_set) && !callchain_param.enabled) {
  4674. record_opts__parse_callchain(&trace.opts, &callchain_param, "dwarf", false);
  4675. }
  4676. #endif
  4677. if (callchain_param.enabled) {
  4678. if (!mmap_pages_user_set && geteuid() == 0)
  4679. trace.opts.mmap_pages = perf_event_mlock_kb_in_pages() * 4;
  4680. symbol_conf.use_callchain = true;
  4681. }
  4682. if (trace.evlist->core.nr_entries > 0) {
  4683. bool use_btf = false;
  4684. evlist__set_default_evsel_handler(trace.evlist, trace__event_handler);
  4685. if (evlist__set_syscall_tp_fields(trace.evlist, &use_btf)) {
  4686. perror("failed to set syscalls:* tracepoint fields");
  4687. goto out;
  4688. }
  4689. if (use_btf)
  4690. trace__load_vmlinux_btf(&trace);
  4691. }
  4692. /*
  4693. * If we are augmenting syscalls, then combine what we put in the
  4694. * __augmented_syscalls__ BPF map with what is in the
  4695. * syscalls:sys_exit_FOO tracepoints, i.e. just like we do without BPF,
  4696. * combining raw_syscalls:sys_enter with raw_syscalls:sys_exit.
  4697. *
  4698. * We'll switch to look at two BPF maps, one for sys_enter and the
  4699. * other for sys_exit when we start augmenting the sys_exit paths with
  4700. * buffers that are being copied from kernel to userspace, think 'read'
  4701. * syscall.
  4702. */
  4703. if (trace.syscalls.events.bpf_output) {
  4704. evlist__for_each_entry(trace.evlist, evsel) {
  4705. bool raw_syscalls_sys_exit = evsel__name_is(evsel, "raw_syscalls:sys_exit");
  4706. if (raw_syscalls_sys_exit) {
  4707. trace.raw_augmented_syscalls = true;
  4708. goto init_augmented_syscall_tp;
  4709. }
  4710. if (trace.syscalls.events.bpf_output->priv == NULL &&
  4711. strstr(evsel__name(evsel), "syscalls:sys_enter")) {
  4712. struct evsel *augmented = trace.syscalls.events.bpf_output;
  4713. if (evsel__init_augmented_syscall_tp(augmented, evsel) ||
  4714. evsel__init_augmented_syscall_tp_args(augmented))
  4715. goto out;
  4716. /*
  4717. * Augmented is __augmented_syscalls__ BPF_OUTPUT event
  4718. * Above we made sure we can get from the payload the tp fields
  4719. * that we get from syscalls:sys_enter tracefs format file.
  4720. */
  4721. augmented->handler = trace__sys_enter;
  4722. /*
  4723. * Now we do the same for the *syscalls:sys_enter event so that
  4724. * if we handle it directly, i.e. if the BPF prog returns 0 so
  4725. * as not to filter it, then we'll handle it just like we would
  4726. * for the BPF_OUTPUT one:
  4727. */
  4728. if (evsel__init_augmented_syscall_tp(evsel, evsel) ||
  4729. evsel__init_augmented_syscall_tp_args(evsel))
  4730. goto out;
  4731. evsel->handler = trace__sys_enter;
  4732. }
  4733. if (strstarts(evsel__name(evsel), "syscalls:sys_exit_")) {
  4734. struct syscall_tp *sc;
  4735. init_augmented_syscall_tp:
  4736. if (evsel__init_augmented_syscall_tp(evsel, evsel))
  4737. goto out;
  4738. sc = __evsel__syscall_tp(evsel);
  4739. /*
  4740. * For now with BPF raw_augmented we hook into
  4741. * raw_syscalls:sys_enter and there we get all
  4742. * 6 syscall args plus the tracepoint common
  4743. * fields and the syscall_nr (another long).
  4744. * So we check if that is the case and if so
  4745. * don't look after the sc->args_size but
  4746. * always after the full raw_syscalls:sys_enter
  4747. * payload, which is fixed.
  4748. *
  4749. * We'll revisit this later to pass
  4750. * s->args_size to the BPF augmenter (now
  4751. * tools/perf/examples/bpf/augmented_raw_syscalls.c,
  4752. * so that it copies only what we need for each
  4753. * syscall, like what happens when we use
  4754. * syscalls:sys_enter_NAME, so that we reduce
  4755. * the kernel/userspace traffic to just what is
  4756. * needed for each syscall.
  4757. */
  4758. if (trace.raw_augmented_syscalls)
  4759. trace.raw_augmented_syscalls_args_size = (6 + 1) * sizeof(long) + sc->id.offset;
  4760. evsel__init_augmented_syscall_tp_ret(evsel);
  4761. evsel->handler = trace__sys_exit;
  4762. }
  4763. }
  4764. }
  4765. if ((argc >= 1) && (strcmp(argv[0], "record") == 0)) {
  4766. err = trace__record(&trace, argc-1, &argv[1]);
  4767. goto out;
  4768. }
  4769. /* Using just --errno-summary will trigger --summary */
  4770. if (trace.errno_summary && !trace.summary && !trace.summary_only)
  4771. trace.summary_only = true;
  4772. /* summary_only implies summary option, but don't overwrite summary if set */
  4773. if (trace.summary_only)
  4774. trace.summary = trace.summary_only;
  4775. /* Keep exited threads, otherwise information might be lost for summary */
  4776. if (trace.summary) {
  4777. symbol_conf.keep_exited_threads = true;
  4778. if (trace.summary_mode == SUMMARY__NONE)
  4779. trace.summary_mode = SUMMARY__BY_THREAD;
  4780. if (!trace.summary_bpf && trace.summary_mode == SUMMARY__BY_CGROUP) {
  4781. pr_err("Error: --summary-mode=cgroup only works with --bpf-summary\n");
  4782. err = -EINVAL;
  4783. goto out;
  4784. }
  4785. }
  4786. if (output_name != NULL) {
  4787. err = trace__open_output(&trace, output_name);
  4788. if (err < 0) {
  4789. perror("failed to create output file");
  4790. goto out;
  4791. }
  4792. }
  4793. err = evswitch__init(&trace.evswitch, trace.evlist, stderr);
  4794. if (err)
  4795. goto out_close;
  4796. err = target__validate(&trace.opts.target);
  4797. if (err) {
  4798. target__strerror(&trace.opts.target, err, bf, sizeof(bf));
  4799. fprintf(trace.output, "%s", bf);
  4800. goto out_close;
  4801. }
  4802. if (trace.uid_str) {
  4803. uid_t uid = parse_uid(trace.uid_str);
  4804. if (uid == UINT_MAX) {
  4805. ui__error("Invalid User: %s", trace.uid_str);
  4806. err = -EINVAL;
  4807. goto out_close;
  4808. }
  4809. err = parse_uid_filter(trace.evlist, uid);
  4810. if (err)
  4811. goto out_close;
  4812. trace.opts.target.system_wide = true;
  4813. }
  4814. if (!argc && target__none(&trace.opts.target))
  4815. trace.opts.target.system_wide = true;
  4816. if (input_name)
  4817. err = trace__replay(&trace);
  4818. else
  4819. err = trace__run(&trace, argc, argv);
  4820. out_close:
  4821. if (output_name != NULL)
  4822. fclose(trace.output);
  4823. out:
  4824. trace__exit(&trace);
  4825. augmented_syscalls__cleanup();
  4826. return err;
  4827. }