remote.c 440 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441
  1. /* Remote target communications for serial-line targets in custom GDB protocol
  2. Copyright (C) 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* See the GDB User Guide for details of the GDB remote protocol. */
  15. #include "defs.h"
  16. #include <ctype.h>
  17. #include <fcntl.h>
  18. #include "inferior.h"
  19. #include "infrun.h"
  20. #include "bfd.h"
  21. #include "symfile.h"
  22. #include "target.h"
  23. #include "process-stratum-target.h"
  24. #include "gdbcmd.h"
  25. #include "objfiles.h"
  26. #include "gdb-stabs.h"
  27. #include "gdbthread.h"
  28. #include "remote.h"
  29. #include "remote-notif.h"
  30. #include "regcache.h"
  31. #include "value.h"
  32. #include "observable.h"
  33. #include "solib.h"
  34. #include "cli/cli-decode.h"
  35. #include "cli/cli-setshow.h"
  36. #include "target-descriptions.h"
  37. #include "gdb_bfd.h"
  38. #include "gdbsupport/filestuff.h"
  39. #include "gdbsupport/rsp-low.h"
  40. #include "disasm.h"
  41. #include "location.h"
  42. #include "gdbsupport/gdb_sys_time.h"
  43. #include "gdbsupport/event-loop.h"
  44. #include "event-top.h"
  45. #include "inf-loop.h"
  46. #include <signal.h>
  47. #include "serial.h"
  48. #include "gdbcore.h"
  49. #include "remote-fileio.h"
  50. #include "gdb/fileio.h"
  51. #include <sys/stat.h>
  52. #include "xml-support.h"
  53. #include "memory-map.h"
  54. #include "tracepoint.h"
  55. #include "ax.h"
  56. #include "ax-gdb.h"
  57. #include "gdbsupport/agent.h"
  58. #include "btrace.h"
  59. #include "record-btrace.h"
  60. #include <algorithm>
  61. #include "gdbsupport/scoped_restore.h"
  62. #include "gdbsupport/environ.h"
  63. #include "gdbsupport/byte-vector.h"
  64. #include "gdbsupport/search.h"
  65. #include <algorithm>
  66. #include <unordered_map>
  67. #include "async-event.h"
  68. #include "gdbsupport/selftest.h"
  69. /* The remote target. */
  70. static const char remote_doc[] = N_("\
  71. Use a remote computer via a serial line, using a gdb-specific protocol.\n\
  72. Specify the serial device it is connected to\n\
  73. (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).");
  74. /* See remote.h */
  75. bool remote_debug = false;
  76. #define OPAQUETHREADBYTES 8
  77. /* a 64 bit opaque identifier */
  78. typedef unsigned char threadref[OPAQUETHREADBYTES];
  79. struct gdb_ext_thread_info;
  80. struct threads_listing_context;
  81. typedef int (*rmt_thread_action) (threadref *ref, void *context);
  82. struct protocol_feature;
  83. struct packet_reg;
  84. struct stop_reply;
  85. typedef std::unique_ptr<stop_reply> stop_reply_up;
  86. /* Generic configuration support for packets the stub optionally
  87. supports. Allows the user to specify the use of the packet as well
  88. as allowing GDB to auto-detect support in the remote stub. */
  89. enum packet_support
  90. {
  91. PACKET_SUPPORT_UNKNOWN = 0,
  92. PACKET_ENABLE,
  93. PACKET_DISABLE
  94. };
  95. /* Analyze a packet's return value and update the packet config
  96. accordingly. */
  97. enum packet_result
  98. {
  99. PACKET_ERROR,
  100. PACKET_OK,
  101. PACKET_UNKNOWN
  102. };
  103. struct threads_listing_context;
  104. /* Stub vCont actions support.
  105. Each field is a boolean flag indicating whether the stub reports
  106. support for the corresponding action. */
  107. struct vCont_action_support
  108. {
  109. /* vCont;t */
  110. bool t = false;
  111. /* vCont;r */
  112. bool r = false;
  113. /* vCont;s */
  114. bool s = false;
  115. /* vCont;S */
  116. bool S = false;
  117. };
  118. /* About this many threadids fit in a packet. */
  119. #define MAXTHREADLISTRESULTS 32
  120. /* Data for the vFile:pread readahead cache. */
  121. struct readahead_cache
  122. {
  123. /* Invalidate the readahead cache. */
  124. void invalidate ();
  125. /* Invalidate the readahead cache if it is holding data for FD. */
  126. void invalidate_fd (int fd);
  127. /* Serve pread from the readahead cache. Returns number of bytes
  128. read, or 0 if the request can't be served from the cache. */
  129. int pread (int fd, gdb_byte *read_buf, size_t len, ULONGEST offset);
  130. /* The file descriptor for the file that is being cached. -1 if the
  131. cache is invalid. */
  132. int fd = -1;
  133. /* The offset into the file that the cache buffer corresponds
  134. to. */
  135. ULONGEST offset = 0;
  136. /* The buffer holding the cache contents. */
  137. gdb_byte *buf = nullptr;
  138. /* The buffer's size. We try to read as much as fits into a packet
  139. at a time. */
  140. size_t bufsize = 0;
  141. /* Cache hit and miss counters. */
  142. ULONGEST hit_count = 0;
  143. ULONGEST miss_count = 0;
  144. };
  145. /* Description of the remote protocol for a given architecture. */
  146. struct packet_reg
  147. {
  148. long offset; /* Offset into G packet. */
  149. long regnum; /* GDB's internal register number. */
  150. LONGEST pnum; /* Remote protocol register number. */
  151. int in_g_packet; /* Always part of G packet. */
  152. /* long size in bytes; == register_size (target_gdbarch (), regnum);
  153. at present. */
  154. /* char *name; == gdbarch_register_name (target_gdbarch (), regnum);
  155. at present. */
  156. };
  157. struct remote_arch_state
  158. {
  159. explicit remote_arch_state (struct gdbarch *gdbarch);
  160. /* Description of the remote protocol registers. */
  161. long sizeof_g_packet;
  162. /* Description of the remote protocol registers indexed by REGNUM
  163. (making an array gdbarch_num_regs in size). */
  164. std::unique_ptr<packet_reg[]> regs;
  165. /* This is the size (in chars) of the first response to the ``g''
  166. packet. It is used as a heuristic when determining the maximum
  167. size of memory-read and memory-write packets. A target will
  168. typically only reserve a buffer large enough to hold the ``g''
  169. packet. The size does not include packet overhead (headers and
  170. trailers). */
  171. long actual_register_packet_size;
  172. /* This is the maximum size (in chars) of a non read/write packet.
  173. It is also used as a cap on the size of read/write packets. */
  174. long remote_packet_size;
  175. };
  176. /* Description of the remote protocol state for the currently
  177. connected target. This is per-target state, and independent of the
  178. selected architecture. */
  179. class remote_state
  180. {
  181. public:
  182. remote_state ();
  183. ~remote_state ();
  184. /* Get the remote arch state for GDBARCH. */
  185. struct remote_arch_state *get_remote_arch_state (struct gdbarch *gdbarch);
  186. public: /* data */
  187. /* A buffer to use for incoming packets, and its current size. The
  188. buffer is grown dynamically for larger incoming packets.
  189. Outgoing packets may also be constructed in this buffer.
  190. The size of the buffer is always at least REMOTE_PACKET_SIZE;
  191. REMOTE_PACKET_SIZE should be used to limit the length of outgoing
  192. packets. */
  193. gdb::char_vector buf;
  194. /* True if we're going through initial connection setup (finding out
  195. about the remote side's threads, relocating symbols, etc.). */
  196. bool starting_up = false;
  197. /* If we negotiated packet size explicitly (and thus can bypass
  198. heuristics for the largest packet size that will not overflow
  199. a buffer in the stub), this will be set to that packet size.
  200. Otherwise zero, meaning to use the guessed size. */
  201. long explicit_packet_size = 0;
  202. /* True, if in no ack mode. That is, neither GDB nor the stub will
  203. expect acks from each other. The connection is assumed to be
  204. reliable. */
  205. bool noack_mode = false;
  206. /* True if we're connected in extended remote mode. */
  207. bool extended = false;
  208. /* True if we resumed the target and we're waiting for the target to
  209. stop. In the mean time, we can't start another command/query.
  210. The remote server wouldn't be ready to process it, so we'd
  211. timeout waiting for a reply that would never come and eventually
  212. we'd close the connection. This can happen in asynchronous mode
  213. because we allow GDB commands while the target is running. */
  214. bool waiting_for_stop_reply = false;
  215. /* The status of the stub support for the various vCont actions. */
  216. vCont_action_support supports_vCont;
  217. /* Whether vCont support was probed already. This is a workaround
  218. until packet_support is per-connection. */
  219. bool supports_vCont_probed;
  220. /* True if the user has pressed Ctrl-C, but the target hasn't
  221. responded to that. */
  222. bool ctrlc_pending_p = false;
  223. /* True if we saw a Ctrl-C while reading or writing from/to the
  224. remote descriptor. At that point it is not safe to send a remote
  225. interrupt packet, so we instead remember we saw the Ctrl-C and
  226. process it once we're done with sending/receiving the current
  227. packet, which should be shortly. If however that takes too long,
  228. and the user presses Ctrl-C again, we offer to disconnect. */
  229. bool got_ctrlc_during_io = false;
  230. /* Descriptor for I/O to remote machine. Initialize it to NULL so that
  231. remote_open knows that we don't have a file open when the program
  232. starts. */
  233. struct serial *remote_desc = nullptr;
  234. /* These are the threads which we last sent to the remote system. The
  235. TID member will be -1 for all or -2 for not sent yet. */
  236. ptid_t general_thread = null_ptid;
  237. ptid_t continue_thread = null_ptid;
  238. /* This is the traceframe which we last selected on the remote system.
  239. It will be -1 if no traceframe is selected. */
  240. int remote_traceframe_number = -1;
  241. char *last_pass_packet = nullptr;
  242. /* The last QProgramSignals packet sent to the target. We bypass
  243. sending a new program signals list down to the target if the new
  244. packet is exactly the same as the last we sent. IOW, we only let
  245. the target know about program signals list changes. */
  246. char *last_program_signals_packet = nullptr;
  247. gdb_signal last_sent_signal = GDB_SIGNAL_0;
  248. bool last_sent_step = false;
  249. /* The execution direction of the last resume we got. */
  250. exec_direction_kind last_resume_exec_dir = EXEC_FORWARD;
  251. char *finished_object = nullptr;
  252. char *finished_annex = nullptr;
  253. ULONGEST finished_offset = 0;
  254. /* Should we try the 'ThreadInfo' query packet?
  255. This variable (NOT available to the user: auto-detect only!)
  256. determines whether GDB will use the new, simpler "ThreadInfo"
  257. query or the older, more complex syntax for thread queries.
  258. This is an auto-detect variable (set to true at each connect,
  259. and set to false when the target fails to recognize it). */
  260. bool use_threadinfo_query = false;
  261. bool use_threadextra_query = false;
  262. threadref echo_nextthread {};
  263. threadref nextthread {};
  264. threadref resultthreadlist[MAXTHREADLISTRESULTS] {};
  265. /* The state of remote notification. */
  266. struct remote_notif_state *notif_state = nullptr;
  267. /* The branch trace configuration. */
  268. struct btrace_config btrace_config {};
  269. /* The argument to the last "vFile:setfs:" packet we sent, used
  270. to avoid sending repeated unnecessary "vFile:setfs:" packets.
  271. Initialized to -1 to indicate that no "vFile:setfs:" packet
  272. has yet been sent. */
  273. int fs_pid = -1;
  274. /* A readahead cache for vFile:pread. Often, reading a binary
  275. involves a sequence of small reads. E.g., when parsing an ELF
  276. file. A readahead cache helps mostly the case of remote
  277. debugging on a connection with higher latency, due to the
  278. request/reply nature of the RSP. We only cache data for a single
  279. file descriptor at a time. */
  280. struct readahead_cache readahead_cache;
  281. /* The list of already fetched and acknowledged stop events. This
  282. queue is used for notification Stop, and other notifications
  283. don't need queue for their events, because the notification
  284. events of Stop can't be consumed immediately, so that events
  285. should be queued first, and be consumed by remote_wait_{ns,as}
  286. one per time. Other notifications can consume their events
  287. immediately, so queue is not needed for them. */
  288. std::vector<stop_reply_up> stop_reply_queue;
  289. /* Asynchronous signal handle registered as event loop source for
  290. when we have pending events ready to be passed to the core. */
  291. struct async_event_handler *remote_async_inferior_event_token = nullptr;
  292. /* FIXME: cagney/1999-09-23: Even though getpkt was called with
  293. ``forever'' still use the normal timeout mechanism. This is
  294. currently used by the ASYNC code to guarentee that target reads
  295. during the initial connect always time-out. Once getpkt has been
  296. modified to return a timeout indication and, in turn
  297. remote_wait()/wait_for_inferior() have gained a timeout parameter
  298. this can go away. */
  299. int wait_forever_enabled_p = 1;
  300. private:
  301. /* Mapping of remote protocol data for each gdbarch. Usually there
  302. is only one entry here, though we may see more with stubs that
  303. support multi-process. */
  304. std::unordered_map<struct gdbarch *, remote_arch_state>
  305. m_arch_states;
  306. };
  307. static const target_info remote_target_info = {
  308. "remote",
  309. N_("Remote target using gdb-specific protocol"),
  310. remote_doc
  311. };
  312. class remote_target : public process_stratum_target
  313. {
  314. public:
  315. remote_target () = default;
  316. ~remote_target () override;
  317. const target_info &info () const override
  318. { return remote_target_info; }
  319. const char *connection_string () override;
  320. thread_control_capabilities get_thread_control_capabilities () override
  321. { return tc_schedlock; }
  322. /* Open a remote connection. */
  323. static void open (const char *, int);
  324. void close () override;
  325. void detach (inferior *, int) override;
  326. void disconnect (const char *, int) override;
  327. void commit_resumed () override;
  328. void resume (ptid_t, int, enum gdb_signal) override;
  329. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  330. bool has_pending_events () override;
  331. void fetch_registers (struct regcache *, int) override;
  332. void store_registers (struct regcache *, int) override;
  333. void prepare_to_store (struct regcache *) override;
  334. int insert_breakpoint (struct gdbarch *, struct bp_target_info *) override;
  335. int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
  336. enum remove_bp_reason) override;
  337. bool stopped_by_sw_breakpoint () override;
  338. bool supports_stopped_by_sw_breakpoint () override;
  339. bool stopped_by_hw_breakpoint () override;
  340. bool supports_stopped_by_hw_breakpoint () override;
  341. bool stopped_by_watchpoint () override;
  342. bool stopped_data_address (CORE_ADDR *) override;
  343. bool watchpoint_addr_within_range (CORE_ADDR, CORE_ADDR, int) override;
  344. int can_use_hw_breakpoint (enum bptype, int, int) override;
  345. int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
  346. int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
  347. int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
  348. int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  349. struct expression *) override;
  350. int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  351. struct expression *) override;
  352. void kill () override;
  353. void load (const char *, int) override;
  354. void mourn_inferior () override;
  355. void pass_signals (gdb::array_view<const unsigned char>) override;
  356. int set_syscall_catchpoint (int, bool, int,
  357. gdb::array_view<const int>) override;
  358. void program_signals (gdb::array_view<const unsigned char>) override;
  359. bool thread_alive (ptid_t ptid) override;
  360. const char *thread_name (struct thread_info *) override;
  361. void update_thread_list () override;
  362. std::string pid_to_str (ptid_t) override;
  363. const char *extra_thread_info (struct thread_info *) override;
  364. ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
  365. thread_info *thread_handle_to_thread_info (const gdb_byte *thread_handle,
  366. int handle_len,
  367. inferior *inf) override;
  368. gdb::byte_vector thread_info_to_thread_handle (struct thread_info *tp)
  369. override;
  370. void stop (ptid_t) override;
  371. void interrupt () override;
  372. void pass_ctrlc () override;
  373. enum target_xfer_status xfer_partial (enum target_object object,
  374. const char *annex,
  375. gdb_byte *readbuf,
  376. const gdb_byte *writebuf,
  377. ULONGEST offset, ULONGEST len,
  378. ULONGEST *xfered_len) override;
  379. ULONGEST get_memory_xfer_limit () override;
  380. void rcmd (const char *command, struct ui_file *output) override;
  381. char *pid_to_exec_file (int pid) override;
  382. void log_command (const char *cmd) override
  383. {
  384. serial_log_command (this, cmd);
  385. }
  386. CORE_ADDR get_thread_local_address (ptid_t ptid,
  387. CORE_ADDR load_module_addr,
  388. CORE_ADDR offset) override;
  389. bool can_execute_reverse () override;
  390. std::vector<mem_region> memory_map () override;
  391. void flash_erase (ULONGEST address, LONGEST length) override;
  392. void flash_done () override;
  393. const struct target_desc *read_description () override;
  394. int search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
  395. const gdb_byte *pattern, ULONGEST pattern_len,
  396. CORE_ADDR *found_addrp) override;
  397. bool can_async_p () override;
  398. bool is_async_p () override;
  399. void async (int) override;
  400. int async_wait_fd () override;
  401. void thread_events (int) override;
  402. int can_do_single_step () override;
  403. void terminal_inferior () override;
  404. void terminal_ours () override;
  405. bool supports_non_stop () override;
  406. bool supports_multi_process () override;
  407. bool supports_disable_randomization () override;
  408. bool filesystem_is_local () override;
  409. int fileio_open (struct inferior *inf, const char *filename,
  410. int flags, int mode, int warn_if_slow,
  411. int *target_errno) override;
  412. int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
  413. ULONGEST offset, int *target_errno) override;
  414. int fileio_pread (int fd, gdb_byte *read_buf, int len,
  415. ULONGEST offset, int *target_errno) override;
  416. int fileio_fstat (int fd, struct stat *sb, int *target_errno) override;
  417. int fileio_close (int fd, int *target_errno) override;
  418. int fileio_unlink (struct inferior *inf,
  419. const char *filename,
  420. int *target_errno) override;
  421. gdb::optional<std::string>
  422. fileio_readlink (struct inferior *inf,
  423. const char *filename,
  424. int *target_errno) override;
  425. bool supports_enable_disable_tracepoint () override;
  426. bool supports_string_tracing () override;
  427. bool supports_evaluation_of_breakpoint_conditions () override;
  428. bool can_run_breakpoint_commands () override;
  429. void trace_init () override;
  430. void download_tracepoint (struct bp_location *location) override;
  431. bool can_download_tracepoint () override;
  432. void download_trace_state_variable (const trace_state_variable &tsv) override;
  433. void enable_tracepoint (struct bp_location *location) override;
  434. void disable_tracepoint (struct bp_location *location) override;
  435. void trace_set_readonly_regions () override;
  436. void trace_start () override;
  437. int get_trace_status (struct trace_status *ts) override;
  438. void get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
  439. override;
  440. void trace_stop () override;
  441. int trace_find (enum trace_find_type type, int num,
  442. CORE_ADDR addr1, CORE_ADDR addr2, int *tpp) override;
  443. bool get_trace_state_variable_value (int tsv, LONGEST *val) override;
  444. int save_trace_data (const char *filename) override;
  445. int upload_tracepoints (struct uploaded_tp **utpp) override;
  446. int upload_trace_state_variables (struct uploaded_tsv **utsvp) override;
  447. LONGEST get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len) override;
  448. int get_min_fast_tracepoint_insn_len () override;
  449. void set_disconnected_tracing (int val) override;
  450. void set_circular_trace_buffer (int val) override;
  451. void set_trace_buffer_size (LONGEST val) override;
  452. bool set_trace_notes (const char *user, const char *notes,
  453. const char *stopnotes) override;
  454. int core_of_thread (ptid_t ptid) override;
  455. int verify_memory (const gdb_byte *data,
  456. CORE_ADDR memaddr, ULONGEST size) override;
  457. bool get_tib_address (ptid_t ptid, CORE_ADDR *addr) override;
  458. void set_permissions () override;
  459. bool static_tracepoint_marker_at (CORE_ADDR,
  460. struct static_tracepoint_marker *marker)
  461. override;
  462. std::vector<static_tracepoint_marker>
  463. static_tracepoint_markers_by_strid (const char *id) override;
  464. traceframe_info_up traceframe_info () override;
  465. bool use_agent (bool use) override;
  466. bool can_use_agent () override;
  467. struct btrace_target_info *
  468. enable_btrace (thread_info *tp, const struct btrace_config *conf) override;
  469. void disable_btrace (struct btrace_target_info *tinfo) override;
  470. void teardown_btrace (struct btrace_target_info *tinfo) override;
  471. enum btrace_error read_btrace (struct btrace_data *data,
  472. struct btrace_target_info *btinfo,
  473. enum btrace_read_type type) override;
  474. const struct btrace_config *btrace_conf (const struct btrace_target_info *) override;
  475. bool augmented_libraries_svr4_read () override;
  476. void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
  477. void follow_exec (inferior *, ptid_t, const char *) override;
  478. int insert_fork_catchpoint (int) override;
  479. int remove_fork_catchpoint (int) override;
  480. int insert_vfork_catchpoint (int) override;
  481. int remove_vfork_catchpoint (int) override;
  482. int insert_exec_catchpoint (int) override;
  483. int remove_exec_catchpoint (int) override;
  484. enum exec_direction_kind execution_direction () override;
  485. bool supports_memory_tagging () override;
  486. bool fetch_memtags (CORE_ADDR address, size_t len,
  487. gdb::byte_vector &tags, int type) override;
  488. bool store_memtags (CORE_ADDR address, size_t len,
  489. const gdb::byte_vector &tags, int type) override;
  490. public: /* Remote specific methods. */
  491. void remote_download_command_source (int num, ULONGEST addr,
  492. struct command_line *cmds);
  493. void remote_file_put (const char *local_file, const char *remote_file,
  494. int from_tty);
  495. void remote_file_get (const char *remote_file, const char *local_file,
  496. int from_tty);
  497. void remote_file_delete (const char *remote_file, int from_tty);
  498. int remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
  499. ULONGEST offset, int *remote_errno);
  500. int remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
  501. ULONGEST offset, int *remote_errno);
  502. int remote_hostio_pread_vFile (int fd, gdb_byte *read_buf, int len,
  503. ULONGEST offset, int *remote_errno);
  504. int remote_hostio_send_command (int command_bytes, int which_packet,
  505. int *remote_errno, const char **attachment,
  506. int *attachment_len);
  507. int remote_hostio_set_filesystem (struct inferior *inf,
  508. int *remote_errno);
  509. /* We should get rid of this and use fileio_open directly. */
  510. int remote_hostio_open (struct inferior *inf, const char *filename,
  511. int flags, int mode, int warn_if_slow,
  512. int *remote_errno);
  513. int remote_hostio_close (int fd, int *remote_errno);
  514. int remote_hostio_unlink (inferior *inf, const char *filename,
  515. int *remote_errno);
  516. struct remote_state *get_remote_state ();
  517. long get_remote_packet_size (void);
  518. long get_memory_packet_size (struct memory_packet_config *config);
  519. long get_memory_write_packet_size ();
  520. long get_memory_read_packet_size ();
  521. char *append_pending_thread_resumptions (char *p, char *endp,
  522. ptid_t ptid);
  523. static void open_1 (const char *name, int from_tty, int extended_p);
  524. void start_remote (int from_tty, int extended_p);
  525. void remote_detach_1 (struct inferior *inf, int from_tty);
  526. char *append_resumption (char *p, char *endp,
  527. ptid_t ptid, int step, gdb_signal siggnal);
  528. int remote_resume_with_vcont (ptid_t ptid, int step,
  529. gdb_signal siggnal);
  530. thread_info *add_current_inferior_and_thread (const char *wait_status);
  531. ptid_t wait_ns (ptid_t ptid, struct target_waitstatus *status,
  532. target_wait_flags options);
  533. ptid_t wait_as (ptid_t ptid, target_waitstatus *status,
  534. target_wait_flags options);
  535. ptid_t process_stop_reply (struct stop_reply *stop_reply,
  536. target_waitstatus *status);
  537. ptid_t select_thread_for_ambiguous_stop_reply
  538. (const struct target_waitstatus &status);
  539. void remote_notice_new_inferior (ptid_t currthread, bool executing);
  540. void print_one_stopped_thread (thread_info *thread);
  541. void process_initial_stop_replies (int from_tty);
  542. thread_info *remote_add_thread (ptid_t ptid, bool running, bool executing,
  543. bool silent_p);
  544. void btrace_sync_conf (const btrace_config *conf);
  545. void remote_btrace_maybe_reopen ();
  546. void remove_new_fork_children (threads_listing_context *context);
  547. void kill_new_fork_children (inferior *inf);
  548. void discard_pending_stop_replies (struct inferior *inf);
  549. int stop_reply_queue_length ();
  550. void check_pending_events_prevent_wildcard_vcont
  551. (bool *may_global_wildcard_vcont);
  552. void discard_pending_stop_replies_in_queue ();
  553. struct stop_reply *remote_notif_remove_queued_reply (ptid_t ptid);
  554. struct stop_reply *queued_stop_reply (ptid_t ptid);
  555. int peek_stop_reply (ptid_t ptid);
  556. void remote_parse_stop_reply (const char *buf, stop_reply *event);
  557. void remote_stop_ns (ptid_t ptid);
  558. void remote_interrupt_as ();
  559. void remote_interrupt_ns ();
  560. char *remote_get_noisy_reply ();
  561. int remote_query_attached (int pid);
  562. inferior *remote_add_inferior (bool fake_pid_p, int pid, int attached,
  563. int try_open_exec);
  564. ptid_t remote_current_thread (ptid_t oldpid);
  565. ptid_t get_current_thread (const char *wait_status);
  566. void set_thread (ptid_t ptid, int gen);
  567. void set_general_thread (ptid_t ptid);
  568. void set_continue_thread (ptid_t ptid);
  569. void set_general_process ();
  570. char *write_ptid (char *buf, const char *endbuf, ptid_t ptid);
  571. int remote_unpack_thread_info_response (const char *pkt, threadref *expectedref,
  572. gdb_ext_thread_info *info);
  573. int remote_get_threadinfo (threadref *threadid, int fieldset,
  574. gdb_ext_thread_info *info);
  575. int parse_threadlist_response (const char *pkt, int result_limit,
  576. threadref *original_echo,
  577. threadref *resultlist,
  578. int *doneflag);
  579. int remote_get_threadlist (int startflag, threadref *nextthread,
  580. int result_limit, int *done, int *result_count,
  581. threadref *threadlist);
  582. int remote_threadlist_iterator (rmt_thread_action stepfunction,
  583. void *context, int looplimit);
  584. int remote_get_threads_with_ql (threads_listing_context *context);
  585. int remote_get_threads_with_qxfer (threads_listing_context *context);
  586. int remote_get_threads_with_qthreadinfo (threads_listing_context *context);
  587. void extended_remote_restart ();
  588. void get_offsets ();
  589. void remote_check_symbols ();
  590. void remote_supported_packet (const struct protocol_feature *feature,
  591. enum packet_support support,
  592. const char *argument);
  593. void remote_query_supported ();
  594. void remote_packet_size (const protocol_feature *feature,
  595. packet_support support, const char *value);
  596. void remote_serial_quit_handler ();
  597. void remote_detach_pid (int pid);
  598. void remote_vcont_probe ();
  599. void remote_resume_with_hc (ptid_t ptid, int step,
  600. gdb_signal siggnal);
  601. void send_interrupt_sequence ();
  602. void interrupt_query ();
  603. void remote_notif_get_pending_events (notif_client *nc);
  604. int fetch_register_using_p (struct regcache *regcache,
  605. packet_reg *reg);
  606. int send_g_packet ();
  607. void process_g_packet (struct regcache *regcache);
  608. void fetch_registers_using_g (struct regcache *regcache);
  609. int store_register_using_P (const struct regcache *regcache,
  610. packet_reg *reg);
  611. void store_registers_using_G (const struct regcache *regcache);
  612. void set_remote_traceframe ();
  613. void check_binary_download (CORE_ADDR addr);
  614. target_xfer_status remote_write_bytes_aux (const char *header,
  615. CORE_ADDR memaddr,
  616. const gdb_byte *myaddr,
  617. ULONGEST len_units,
  618. int unit_size,
  619. ULONGEST *xfered_len_units,
  620. char packet_format,
  621. int use_length);
  622. target_xfer_status remote_write_bytes (CORE_ADDR memaddr,
  623. const gdb_byte *myaddr, ULONGEST len,
  624. int unit_size, ULONGEST *xfered_len);
  625. target_xfer_status remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr,
  626. ULONGEST len_units,
  627. int unit_size, ULONGEST *xfered_len_units);
  628. target_xfer_status remote_xfer_live_readonly_partial (gdb_byte *readbuf,
  629. ULONGEST memaddr,
  630. ULONGEST len,
  631. int unit_size,
  632. ULONGEST *xfered_len);
  633. target_xfer_status remote_read_bytes (CORE_ADDR memaddr,
  634. gdb_byte *myaddr, ULONGEST len,
  635. int unit_size,
  636. ULONGEST *xfered_len);
  637. packet_result remote_send_printf (const char *format, ...)
  638. ATTRIBUTE_PRINTF (2, 3);
  639. target_xfer_status remote_flash_write (ULONGEST address,
  640. ULONGEST length, ULONGEST *xfered_len,
  641. const gdb_byte *data);
  642. int readchar (int timeout);
  643. void remote_serial_write (const char *str, int len);
  644. int putpkt (const char *buf);
  645. int putpkt_binary (const char *buf, int cnt);
  646. int putpkt (const gdb::char_vector &buf)
  647. {
  648. return putpkt (buf.data ());
  649. }
  650. void skip_frame ();
  651. long read_frame (gdb::char_vector *buf_p);
  652. void getpkt (gdb::char_vector *buf, int forever);
  653. int getpkt_or_notif_sane_1 (gdb::char_vector *buf, int forever,
  654. int expecting_notif, int *is_notif);
  655. int getpkt_sane (gdb::char_vector *buf, int forever);
  656. int getpkt_or_notif_sane (gdb::char_vector *buf, int forever,
  657. int *is_notif);
  658. int remote_vkill (int pid);
  659. void remote_kill_k ();
  660. void extended_remote_disable_randomization (int val);
  661. int extended_remote_run (const std::string &args);
  662. void send_environment_packet (const char *action,
  663. const char *packet,
  664. const char *value);
  665. void extended_remote_environment_support ();
  666. void extended_remote_set_inferior_cwd ();
  667. target_xfer_status remote_write_qxfer (const char *object_name,
  668. const char *annex,
  669. const gdb_byte *writebuf,
  670. ULONGEST offset, LONGEST len,
  671. ULONGEST *xfered_len,
  672. struct packet_config *packet);
  673. target_xfer_status remote_read_qxfer (const char *object_name,
  674. const char *annex,
  675. gdb_byte *readbuf, ULONGEST offset,
  676. LONGEST len,
  677. ULONGEST *xfered_len,
  678. struct packet_config *packet);
  679. void push_stop_reply (struct stop_reply *new_event);
  680. bool vcont_r_supported ();
  681. private:
  682. bool start_remote_1 (int from_tty, int extended_p);
  683. /* The remote state. Don't reference this directly. Use the
  684. get_remote_state method instead. */
  685. remote_state m_remote_state;
  686. };
  687. static const target_info extended_remote_target_info = {
  688. "extended-remote",
  689. N_("Extended remote target using gdb-specific protocol"),
  690. remote_doc
  691. };
  692. /* Set up the extended remote target by extending the standard remote
  693. target and adding to it. */
  694. class extended_remote_target final : public remote_target
  695. {
  696. public:
  697. const target_info &info () const override
  698. { return extended_remote_target_info; }
  699. /* Open an extended-remote connection. */
  700. static void open (const char *, int);
  701. bool can_create_inferior () override { return true; }
  702. void create_inferior (const char *, const std::string &,
  703. char **, int) override;
  704. void detach (inferior *, int) override;
  705. bool can_attach () override { return true; }
  706. void attach (const char *, int) override;
  707. void post_attach (int) override;
  708. bool supports_disable_randomization () override;
  709. };
  710. struct stop_reply : public notif_event
  711. {
  712. ~stop_reply ();
  713. /* The identifier of the thread about this event */
  714. ptid_t ptid;
  715. /* The remote state this event is associated with. When the remote
  716. connection, represented by a remote_state object, is closed,
  717. all the associated stop_reply events should be released. */
  718. struct remote_state *rs;
  719. struct target_waitstatus ws;
  720. /* The architecture associated with the expedited registers. */
  721. gdbarch *arch;
  722. /* Expedited registers. This makes remote debugging a bit more
  723. efficient for those targets that provide critical registers as
  724. part of their normal status mechanism (as another roundtrip to
  725. fetch them is avoided). */
  726. std::vector<cached_reg_t> regcache;
  727. enum target_stop_reason stop_reason;
  728. CORE_ADDR watch_data_address;
  729. int core;
  730. };
  731. /* See remote.h. */
  732. bool
  733. is_remote_target (process_stratum_target *target)
  734. {
  735. remote_target *rt = dynamic_cast<remote_target *> (target);
  736. return rt != nullptr;
  737. }
  738. /* Per-program-space data key. */
  739. static const struct program_space_key<char, gdb::xfree_deleter<char>>
  740. remote_pspace_data;
  741. /* The variable registered as the control variable used by the
  742. remote exec-file commands. While the remote exec-file setting is
  743. per-program-space, the set/show machinery uses this as the
  744. location of the remote exec-file value. */
  745. static std::string remote_exec_file_var;
  746. /* The size to align memory write packets, when practical. The protocol
  747. does not guarantee any alignment, and gdb will generate short
  748. writes and unaligned writes, but even as a best-effort attempt this
  749. can improve bulk transfers. For instance, if a write is misaligned
  750. relative to the target's data bus, the stub may need to make an extra
  751. round trip fetching data from the target. This doesn't make a
  752. huge difference, but it's easy to do, so we try to be helpful.
  753. The alignment chosen is arbitrary; usually data bus width is
  754. important here, not the possibly larger cache line size. */
  755. enum { REMOTE_ALIGN_WRITES = 16 };
  756. /* Prototypes for local functions. */
  757. static int hexnumlen (ULONGEST num);
  758. static int stubhex (int ch);
  759. static int hexnumstr (char *, ULONGEST);
  760. static int hexnumnstr (char *, ULONGEST, int);
  761. static CORE_ADDR remote_address_masked (CORE_ADDR);
  762. static int stub_unpack_int (const char *buff, int fieldlength);
  763. struct packet_config;
  764. static void show_remote_protocol_packet_cmd (struct ui_file *file,
  765. int from_tty,
  766. struct cmd_list_element *c,
  767. const char *value);
  768. static ptid_t read_ptid (const char *buf, const char **obuf);
  769. static void remote_async_inferior_event_handler (gdb_client_data);
  770. static bool remote_read_description_p (struct target_ops *target);
  771. static void remote_console_output (const char *msg);
  772. static void remote_btrace_reset (remote_state *rs);
  773. static void remote_unpush_and_throw (remote_target *target);
  774. /* For "remote". */
  775. static struct cmd_list_element *remote_cmdlist;
  776. /* For "set remote" and "show remote". */
  777. static struct cmd_list_element *remote_set_cmdlist;
  778. static struct cmd_list_element *remote_show_cmdlist;
  779. /* Controls whether GDB is willing to use range stepping. */
  780. static bool use_range_stepping = true;
  781. /* From the remote target's point of view, each thread is in one of these three
  782. states. */
  783. enum class resume_state
  784. {
  785. /* Not resumed - we haven't been asked to resume this thread. */
  786. NOT_RESUMED,
  787. /* We have been asked to resume this thread, but haven't sent a vCont action
  788. for it yet. We'll need to consider it next time commit_resume is
  789. called. */
  790. RESUMED_PENDING_VCONT,
  791. /* We have been asked to resume this thread, and we have sent a vCont action
  792. for it. */
  793. RESUMED,
  794. };
  795. /* Information about a thread's pending vCont-resume. Used when a thread is in
  796. the remote_resume_state::RESUMED_PENDING_VCONT state. remote_target::resume
  797. stores this information which is then picked up by
  798. remote_target::commit_resume to know which is the proper action for this
  799. thread to include in the vCont packet. */
  800. struct resumed_pending_vcont_info
  801. {
  802. /* True if the last resume call for this thread was a step request, false
  803. if a continue request. */
  804. bool step;
  805. /* The signal specified in the last resume call for this thread. */
  806. gdb_signal sig;
  807. };
  808. /* Private data that we'll store in (struct thread_info)->priv. */
  809. struct remote_thread_info : public private_thread_info
  810. {
  811. std::string extra;
  812. std::string name;
  813. int core = -1;
  814. /* Thread handle, perhaps a pthread_t or thread_t value, stored as a
  815. sequence of bytes. */
  816. gdb::byte_vector thread_handle;
  817. /* Whether the target stopped for a breakpoint/watchpoint. */
  818. enum target_stop_reason stop_reason = TARGET_STOPPED_BY_NO_REASON;
  819. /* This is set to the data address of the access causing the target
  820. to stop for a watchpoint. */
  821. CORE_ADDR watch_data_address = 0;
  822. /* Get the thread's resume state. */
  823. enum resume_state get_resume_state () const
  824. {
  825. return m_resume_state;
  826. }
  827. /* Put the thread in the NOT_RESUMED state. */
  828. void set_not_resumed ()
  829. {
  830. m_resume_state = resume_state::NOT_RESUMED;
  831. }
  832. /* Put the thread in the RESUMED_PENDING_VCONT state. */
  833. void set_resumed_pending_vcont (bool step, gdb_signal sig)
  834. {
  835. m_resume_state = resume_state::RESUMED_PENDING_VCONT;
  836. m_resumed_pending_vcont_info.step = step;
  837. m_resumed_pending_vcont_info.sig = sig;
  838. }
  839. /* Get the information this thread's pending vCont-resumption.
  840. Must only be called if the thread is in the RESUMED_PENDING_VCONT resume
  841. state. */
  842. const struct resumed_pending_vcont_info &resumed_pending_vcont_info () const
  843. {
  844. gdb_assert (m_resume_state == resume_state::RESUMED_PENDING_VCONT);
  845. return m_resumed_pending_vcont_info;
  846. }
  847. /* Put the thread in the VCONT_RESUMED state. */
  848. void set_resumed ()
  849. {
  850. m_resume_state = resume_state::RESUMED;
  851. }
  852. private:
  853. /* Resume state for this thread. This is used to implement vCont action
  854. coalescing (only when the target operates in non-stop mode).
  855. remote_target::resume moves the thread to the RESUMED_PENDING_VCONT state,
  856. which notes that this thread must be considered in the next commit_resume
  857. call.
  858. remote_target::commit_resume sends a vCont packet with actions for the
  859. threads in the RESUMED_PENDING_VCONT state and moves them to the
  860. VCONT_RESUMED state.
  861. When reporting a stop to the core for a thread, that thread is moved back
  862. to the NOT_RESUMED state. */
  863. enum resume_state m_resume_state = resume_state::NOT_RESUMED;
  864. /* Extra info used if the thread is in the RESUMED_PENDING_VCONT state. */
  865. struct resumed_pending_vcont_info m_resumed_pending_vcont_info;
  866. };
  867. remote_state::remote_state ()
  868. : buf (400)
  869. {
  870. }
  871. remote_state::~remote_state ()
  872. {
  873. xfree (this->last_pass_packet);
  874. xfree (this->last_program_signals_packet);
  875. xfree (this->finished_object);
  876. xfree (this->finished_annex);
  877. }
  878. /* Utility: generate error from an incoming stub packet. */
  879. static void
  880. trace_error (char *buf)
  881. {
  882. if (*buf++ != 'E')
  883. return; /* not an error msg */
  884. switch (*buf)
  885. {
  886. case '1': /* malformed packet error */
  887. if (*++buf == '0') /* general case: */
  888. error (_("remote.c: error in outgoing packet."));
  889. else
  890. error (_("remote.c: error in outgoing packet at field #%ld."),
  891. strtol (buf, NULL, 16));
  892. default:
  893. error (_("Target returns error code '%s'."), buf);
  894. }
  895. }
  896. /* Utility: wait for reply from stub, while accepting "O" packets. */
  897. char *
  898. remote_target::remote_get_noisy_reply ()
  899. {
  900. struct remote_state *rs = get_remote_state ();
  901. do /* Loop on reply from remote stub. */
  902. {
  903. char *buf;
  904. QUIT; /* Allow user to bail out with ^C. */
  905. getpkt (&rs->buf, 0);
  906. buf = rs->buf.data ();
  907. if (buf[0] == 'E')
  908. trace_error (buf);
  909. else if (startswith (buf, "qRelocInsn:"))
  910. {
  911. ULONGEST ul;
  912. CORE_ADDR from, to, org_to;
  913. const char *p, *pp;
  914. int adjusted_size = 0;
  915. int relocated = 0;
  916. p = buf + strlen ("qRelocInsn:");
  917. pp = unpack_varlen_hex (p, &ul);
  918. if (*pp != ';')
  919. error (_("invalid qRelocInsn packet: %s"), buf);
  920. from = ul;
  921. p = pp + 1;
  922. unpack_varlen_hex (p, &ul);
  923. to = ul;
  924. org_to = to;
  925. try
  926. {
  927. gdbarch_relocate_instruction (target_gdbarch (), &to, from);
  928. relocated = 1;
  929. }
  930. catch (const gdb_exception &ex)
  931. {
  932. if (ex.error == MEMORY_ERROR)
  933. {
  934. /* Propagate memory errors silently back to the
  935. target. The stub may have limited the range of
  936. addresses we can write to, for example. */
  937. }
  938. else
  939. {
  940. /* Something unexpectedly bad happened. Be verbose
  941. so we can tell what, and propagate the error back
  942. to the stub, so it doesn't get stuck waiting for
  943. a response. */
  944. exception_fprintf (gdb_stderr, ex,
  945. _("warning: relocating instruction: "));
  946. }
  947. putpkt ("E01");
  948. }
  949. if (relocated)
  950. {
  951. adjusted_size = to - org_to;
  952. xsnprintf (buf, rs->buf.size (), "qRelocInsn:%x", adjusted_size);
  953. putpkt (buf);
  954. }
  955. }
  956. else if (buf[0] == 'O' && buf[1] != 'K')
  957. remote_console_output (buf + 1); /* 'O' message from stub */
  958. else
  959. return buf; /* Here's the actual reply. */
  960. }
  961. while (1);
  962. }
  963. struct remote_arch_state *
  964. remote_state::get_remote_arch_state (struct gdbarch *gdbarch)
  965. {
  966. remote_arch_state *rsa;
  967. auto it = this->m_arch_states.find (gdbarch);
  968. if (it == this->m_arch_states.end ())
  969. {
  970. auto p = this->m_arch_states.emplace (std::piecewise_construct,
  971. std::forward_as_tuple (gdbarch),
  972. std::forward_as_tuple (gdbarch));
  973. rsa = &p.first->second;
  974. /* Make sure that the packet buffer is plenty big enough for
  975. this architecture. */
  976. if (this->buf.size () < rsa->remote_packet_size)
  977. this->buf.resize (2 * rsa->remote_packet_size);
  978. }
  979. else
  980. rsa = &it->second;
  981. return rsa;
  982. }
  983. /* Fetch the global remote target state. */
  984. remote_state *
  985. remote_target::get_remote_state ()
  986. {
  987. /* Make sure that the remote architecture state has been
  988. initialized, because doing so might reallocate rs->buf. Any
  989. function which calls getpkt also needs to be mindful of changes
  990. to rs->buf, but this call limits the number of places which run
  991. into trouble. */
  992. m_remote_state.get_remote_arch_state (target_gdbarch ());
  993. return &m_remote_state;
  994. }
  995. /* Fetch the remote exec-file from the current program space. */
  996. static const char *
  997. get_remote_exec_file (void)
  998. {
  999. char *remote_exec_file;
  1000. remote_exec_file = remote_pspace_data.get (current_program_space);
  1001. if (remote_exec_file == NULL)
  1002. return "";
  1003. return remote_exec_file;
  1004. }
  1005. /* Set the remote exec file for PSPACE. */
  1006. static void
  1007. set_pspace_remote_exec_file (struct program_space *pspace,
  1008. const char *remote_exec_file)
  1009. {
  1010. char *old_file = remote_pspace_data.get (pspace);
  1011. xfree (old_file);
  1012. remote_pspace_data.set (pspace, xstrdup (remote_exec_file));
  1013. }
  1014. /* The "set/show remote exec-file" set command hook. */
  1015. static void
  1016. set_remote_exec_file (const char *ignored, int from_tty,
  1017. struct cmd_list_element *c)
  1018. {
  1019. set_pspace_remote_exec_file (current_program_space,
  1020. remote_exec_file_var.c_str ());
  1021. }
  1022. /* The "set/show remote exec-file" show command hook. */
  1023. static void
  1024. show_remote_exec_file (struct ui_file *file, int from_tty,
  1025. struct cmd_list_element *cmd, const char *value)
  1026. {
  1027. gdb_printf (file, "%s\n", get_remote_exec_file ());
  1028. }
  1029. static int
  1030. map_regcache_remote_table (struct gdbarch *gdbarch, struct packet_reg *regs)
  1031. {
  1032. int regnum, num_remote_regs, offset;
  1033. struct packet_reg **remote_regs;
  1034. for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
  1035. {
  1036. struct packet_reg *r = &regs[regnum];
  1037. if (register_size (gdbarch, regnum) == 0)
  1038. /* Do not try to fetch zero-sized (placeholder) registers. */
  1039. r->pnum = -1;
  1040. else
  1041. r->pnum = gdbarch_remote_register_number (gdbarch, regnum);
  1042. r->regnum = regnum;
  1043. }
  1044. /* Define the g/G packet format as the contents of each register
  1045. with a remote protocol number, in order of ascending protocol
  1046. number. */
  1047. remote_regs = XALLOCAVEC (struct packet_reg *, gdbarch_num_regs (gdbarch));
  1048. for (num_remote_regs = 0, regnum = 0;
  1049. regnum < gdbarch_num_regs (gdbarch);
  1050. regnum++)
  1051. if (regs[regnum].pnum != -1)
  1052. remote_regs[num_remote_regs++] = &regs[regnum];
  1053. std::sort (remote_regs, remote_regs + num_remote_regs,
  1054. [] (const packet_reg *a, const packet_reg *b)
  1055. { return a->pnum < b->pnum; });
  1056. for (regnum = 0, offset = 0; regnum < num_remote_regs; regnum++)
  1057. {
  1058. remote_regs[regnum]->in_g_packet = 1;
  1059. remote_regs[regnum]->offset = offset;
  1060. offset += register_size (gdbarch, remote_regs[regnum]->regnum);
  1061. }
  1062. return offset;
  1063. }
  1064. /* Given the architecture described by GDBARCH, return the remote
  1065. protocol register's number and the register's offset in the g/G
  1066. packets of GDB register REGNUM, in PNUM and POFFSET respectively.
  1067. If the target does not have a mapping for REGNUM, return false,
  1068. otherwise, return true. */
  1069. int
  1070. remote_register_number_and_offset (struct gdbarch *gdbarch, int regnum,
  1071. int *pnum, int *poffset)
  1072. {
  1073. gdb_assert (regnum < gdbarch_num_regs (gdbarch));
  1074. std::vector<packet_reg> regs (gdbarch_num_regs (gdbarch));
  1075. map_regcache_remote_table (gdbarch, regs.data ());
  1076. *pnum = regs[regnum].pnum;
  1077. *poffset = regs[regnum].offset;
  1078. return *pnum != -1;
  1079. }
  1080. remote_arch_state::remote_arch_state (struct gdbarch *gdbarch)
  1081. {
  1082. /* Use the architecture to build a regnum<->pnum table, which will be
  1083. 1:1 unless a feature set specifies otherwise. */
  1084. this->regs.reset (new packet_reg [gdbarch_num_regs (gdbarch)] ());
  1085. /* Record the maximum possible size of the g packet - it may turn out
  1086. to be smaller. */
  1087. this->sizeof_g_packet
  1088. = map_regcache_remote_table (gdbarch, this->regs.get ());
  1089. /* Default maximum number of characters in a packet body. Many
  1090. remote stubs have a hardwired buffer size of 400 bytes
  1091. (c.f. BUFMAX in m68k-stub.c and i386-stub.c). BUFMAX-1 is used
  1092. as the maximum packet-size to ensure that the packet and an extra
  1093. NUL character can always fit in the buffer. This stops GDB
  1094. trashing stubs that try to squeeze an extra NUL into what is
  1095. already a full buffer (As of 1999-12-04 that was most stubs). */
  1096. this->remote_packet_size = 400 - 1;
  1097. /* This one is filled in when a ``g'' packet is received. */
  1098. this->actual_register_packet_size = 0;
  1099. /* Should rsa->sizeof_g_packet needs more space than the
  1100. default, adjust the size accordingly. Remember that each byte is
  1101. encoded as two characters. 32 is the overhead for the packet
  1102. header / footer. NOTE: cagney/1999-10-26: I suspect that 8
  1103. (``$NN:G...#NN'') is a better guess, the below has been padded a
  1104. little. */
  1105. if (this->sizeof_g_packet > ((this->remote_packet_size - 32) / 2))
  1106. this->remote_packet_size = (this->sizeof_g_packet * 2 + 32);
  1107. }
  1108. /* Get a pointer to the current remote target. If not connected to a
  1109. remote target, return NULL. */
  1110. static remote_target *
  1111. get_current_remote_target ()
  1112. {
  1113. target_ops *proc_target = current_inferior ()->process_target ();
  1114. return dynamic_cast<remote_target *> (proc_target);
  1115. }
  1116. /* Return the current allowed size of a remote packet. This is
  1117. inferred from the current architecture, and should be used to
  1118. limit the length of outgoing packets. */
  1119. long
  1120. remote_target::get_remote_packet_size ()
  1121. {
  1122. struct remote_state *rs = get_remote_state ();
  1123. remote_arch_state *rsa = rs->get_remote_arch_state (target_gdbarch ());
  1124. if (rs->explicit_packet_size)
  1125. return rs->explicit_packet_size;
  1126. return rsa->remote_packet_size;
  1127. }
  1128. static struct packet_reg *
  1129. packet_reg_from_regnum (struct gdbarch *gdbarch, struct remote_arch_state *rsa,
  1130. long regnum)
  1131. {
  1132. if (regnum < 0 && regnum >= gdbarch_num_regs (gdbarch))
  1133. return NULL;
  1134. else
  1135. {
  1136. struct packet_reg *r = &rsa->regs[regnum];
  1137. gdb_assert (r->regnum == regnum);
  1138. return r;
  1139. }
  1140. }
  1141. static struct packet_reg *
  1142. packet_reg_from_pnum (struct gdbarch *gdbarch, struct remote_arch_state *rsa,
  1143. LONGEST pnum)
  1144. {
  1145. int i;
  1146. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  1147. {
  1148. struct packet_reg *r = &rsa->regs[i];
  1149. if (r->pnum == pnum)
  1150. return r;
  1151. }
  1152. return NULL;
  1153. }
  1154. /* Allow the user to specify what sequence to send to the remote
  1155. when he requests a program interruption: Although ^C is usually
  1156. what remote systems expect (this is the default, here), it is
  1157. sometimes preferable to send a break. On other systems such
  1158. as the Linux kernel, a break followed by g, which is Magic SysRq g
  1159. is required in order to interrupt the execution. */
  1160. const char interrupt_sequence_control_c[] = "Ctrl-C";
  1161. const char interrupt_sequence_break[] = "BREAK";
  1162. const char interrupt_sequence_break_g[] = "BREAK-g";
  1163. static const char *const interrupt_sequence_modes[] =
  1164. {
  1165. interrupt_sequence_control_c,
  1166. interrupt_sequence_break,
  1167. interrupt_sequence_break_g,
  1168. NULL
  1169. };
  1170. static const char *interrupt_sequence_mode = interrupt_sequence_control_c;
  1171. static void
  1172. show_interrupt_sequence (struct ui_file *file, int from_tty,
  1173. struct cmd_list_element *c,
  1174. const char *value)
  1175. {
  1176. if (interrupt_sequence_mode == interrupt_sequence_control_c)
  1177. gdb_printf (file,
  1178. _("Send the ASCII ETX character (Ctrl-c) "
  1179. "to the remote target to interrupt the "
  1180. "execution of the program.\n"));
  1181. else if (interrupt_sequence_mode == interrupt_sequence_break)
  1182. gdb_printf (file,
  1183. _("send a break signal to the remote target "
  1184. "to interrupt the execution of the program.\n"));
  1185. else if (interrupt_sequence_mode == interrupt_sequence_break_g)
  1186. gdb_printf (file,
  1187. _("Send a break signal and 'g' a.k.a. Magic SysRq g to "
  1188. "the remote target to interrupt the execution "
  1189. "of Linux kernel.\n"));
  1190. else
  1191. internal_error (__FILE__, __LINE__,
  1192. _("Invalid value for interrupt_sequence_mode: %s."),
  1193. interrupt_sequence_mode);
  1194. }
  1195. /* This boolean variable specifies whether interrupt_sequence is sent
  1196. to the remote target when gdb connects to it.
  1197. This is mostly needed when you debug the Linux kernel: The Linux kernel
  1198. expects BREAK g which is Magic SysRq g for connecting gdb. */
  1199. static bool interrupt_on_connect = false;
  1200. /* This variable is used to implement the "set/show remotebreak" commands.
  1201. Since these commands are now deprecated in favor of "set/show remote
  1202. interrupt-sequence", it no longer has any effect on the code. */
  1203. static bool remote_break;
  1204. static void
  1205. set_remotebreak (const char *args, int from_tty, struct cmd_list_element *c)
  1206. {
  1207. if (remote_break)
  1208. interrupt_sequence_mode = interrupt_sequence_break;
  1209. else
  1210. interrupt_sequence_mode = interrupt_sequence_control_c;
  1211. }
  1212. static void
  1213. show_remotebreak (struct ui_file *file, int from_tty,
  1214. struct cmd_list_element *c,
  1215. const char *value)
  1216. {
  1217. }
  1218. /* This variable sets the number of bits in an address that are to be
  1219. sent in a memory ("M" or "m") packet. Normally, after stripping
  1220. leading zeros, the entire address would be sent. This variable
  1221. restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
  1222. initial implementation of remote.c restricted the address sent in
  1223. memory packets to ``host::sizeof long'' bytes - (typically 32
  1224. bits). Consequently, for 64 bit targets, the upper 32 bits of an
  1225. address was never sent. Since fixing this bug may cause a break in
  1226. some remote targets this variable is principally provided to
  1227. facilitate backward compatibility. */
  1228. static unsigned int remote_address_size;
  1229. /* User configurable variables for the number of characters in a
  1230. memory read/write packet. MIN (rsa->remote_packet_size,
  1231. rsa->sizeof_g_packet) is the default. Some targets need smaller
  1232. values (fifo overruns, et.al.) and some users need larger values
  1233. (speed up transfers). The variables ``preferred_*'' (the user
  1234. request), ``current_*'' (what was actually set) and ``forced_*''
  1235. (Positive - a soft limit, negative - a hard limit). */
  1236. struct memory_packet_config
  1237. {
  1238. const char *name;
  1239. long size;
  1240. int fixed_p;
  1241. };
  1242. /* The default max memory-write-packet-size, when the setting is
  1243. "fixed". The 16k is historical. (It came from older GDB's using
  1244. alloca for buffers and the knowledge (folklore?) that some hosts
  1245. don't cope very well with large alloca calls.) */
  1246. #define DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED 16384
  1247. /* The minimum remote packet size for memory transfers. Ensures we
  1248. can write at least one byte. */
  1249. #define MIN_MEMORY_PACKET_SIZE 20
  1250. /* Get the memory packet size, assuming it is fixed. */
  1251. static long
  1252. get_fixed_memory_packet_size (struct memory_packet_config *config)
  1253. {
  1254. gdb_assert (config->fixed_p);
  1255. if (config->size <= 0)
  1256. return DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED;
  1257. else
  1258. return config->size;
  1259. }
  1260. /* Compute the current size of a read/write packet. Since this makes
  1261. use of ``actual_register_packet_size'' the computation is dynamic. */
  1262. long
  1263. remote_target::get_memory_packet_size (struct memory_packet_config *config)
  1264. {
  1265. struct remote_state *rs = get_remote_state ();
  1266. remote_arch_state *rsa = rs->get_remote_arch_state (target_gdbarch ());
  1267. long what_they_get;
  1268. if (config->fixed_p)
  1269. what_they_get = get_fixed_memory_packet_size (config);
  1270. else
  1271. {
  1272. what_they_get = get_remote_packet_size ();
  1273. /* Limit the packet to the size specified by the user. */
  1274. if (config->size > 0
  1275. && what_they_get > config->size)
  1276. what_they_get = config->size;
  1277. /* Limit it to the size of the targets ``g'' response unless we have
  1278. permission from the stub to use a larger packet size. */
  1279. if (rs->explicit_packet_size == 0
  1280. && rsa->actual_register_packet_size > 0
  1281. && what_they_get > rsa->actual_register_packet_size)
  1282. what_they_get = rsa->actual_register_packet_size;
  1283. }
  1284. if (what_they_get < MIN_MEMORY_PACKET_SIZE)
  1285. what_they_get = MIN_MEMORY_PACKET_SIZE;
  1286. /* Make sure there is room in the global buffer for this packet
  1287. (including its trailing NUL byte). */
  1288. if (rs->buf.size () < what_they_get + 1)
  1289. rs->buf.resize (2 * what_they_get);
  1290. return what_they_get;
  1291. }
  1292. /* Update the size of a read/write packet. If they user wants
  1293. something really big then do a sanity check. */
  1294. static void
  1295. set_memory_packet_size (const char *args, struct memory_packet_config *config)
  1296. {
  1297. int fixed_p = config->fixed_p;
  1298. long size = config->size;
  1299. if (args == NULL)
  1300. error (_("Argument required (integer, `fixed' or `limited')."));
  1301. else if (strcmp (args, "hard") == 0
  1302. || strcmp (args, "fixed") == 0)
  1303. fixed_p = 1;
  1304. else if (strcmp (args, "soft") == 0
  1305. || strcmp (args, "limit") == 0)
  1306. fixed_p = 0;
  1307. else
  1308. {
  1309. char *end;
  1310. size = strtoul (args, &end, 0);
  1311. if (args == end)
  1312. error (_("Invalid %s (bad syntax)."), config->name);
  1313. /* Instead of explicitly capping the size of a packet to or
  1314. disallowing it, the user is allowed to set the size to
  1315. something arbitrarily large. */
  1316. }
  1317. /* Extra checks? */
  1318. if (fixed_p && !config->fixed_p)
  1319. {
  1320. /* So that the query shows the correct value. */
  1321. long query_size = (size <= 0
  1322. ? DEFAULT_MAX_MEMORY_PACKET_SIZE_FIXED
  1323. : size);
  1324. if (! query (_("The target may not be able to correctly handle a %s\n"
  1325. "of %ld bytes. Change the packet size? "),
  1326. config->name, query_size))
  1327. error (_("Packet size not changed."));
  1328. }
  1329. /* Update the config. */
  1330. config->fixed_p = fixed_p;
  1331. config->size = size;
  1332. }
  1333. static void
  1334. show_memory_packet_size (struct memory_packet_config *config)
  1335. {
  1336. if (config->size == 0)
  1337. gdb_printf (_("The %s is 0 (default). "), config->name);
  1338. else
  1339. gdb_printf (_("The %s is %ld. "), config->name, config->size);
  1340. if (config->fixed_p)
  1341. gdb_printf (_("Packets are fixed at %ld bytes.\n"),
  1342. get_fixed_memory_packet_size (config));
  1343. else
  1344. {
  1345. remote_target *remote = get_current_remote_target ();
  1346. if (remote != NULL)
  1347. gdb_printf (_("Packets are limited to %ld bytes.\n"),
  1348. remote->get_memory_packet_size (config));
  1349. else
  1350. gdb_puts ("The actual limit will be further reduced "
  1351. "dependent on the target.\n");
  1352. }
  1353. }
  1354. /* FIXME: needs to be per-remote-target. */
  1355. static struct memory_packet_config memory_write_packet_config =
  1356. {
  1357. "memory-write-packet-size",
  1358. };
  1359. static void
  1360. set_memory_write_packet_size (const char *args, int from_tty)
  1361. {
  1362. set_memory_packet_size (args, &memory_write_packet_config);
  1363. }
  1364. static void
  1365. show_memory_write_packet_size (const char *args, int from_tty)
  1366. {
  1367. show_memory_packet_size (&memory_write_packet_config);
  1368. }
  1369. /* Show the number of hardware watchpoints that can be used. */
  1370. static void
  1371. show_hardware_watchpoint_limit (struct ui_file *file, int from_tty,
  1372. struct cmd_list_element *c,
  1373. const char *value)
  1374. {
  1375. gdb_printf (file, _("The maximum number of target hardware "
  1376. "watchpoints is %s.\n"), value);
  1377. }
  1378. /* Show the length limit (in bytes) for hardware watchpoints. */
  1379. static void
  1380. show_hardware_watchpoint_length_limit (struct ui_file *file, int from_tty,
  1381. struct cmd_list_element *c,
  1382. const char *value)
  1383. {
  1384. gdb_printf (file, _("The maximum length (in bytes) of a target "
  1385. "hardware watchpoint is %s.\n"), value);
  1386. }
  1387. /* Show the number of hardware breakpoints that can be used. */
  1388. static void
  1389. show_hardware_breakpoint_limit (struct ui_file *file, int from_tty,
  1390. struct cmd_list_element *c,
  1391. const char *value)
  1392. {
  1393. gdb_printf (file, _("The maximum number of target hardware "
  1394. "breakpoints is %s.\n"), value);
  1395. }
  1396. /* Controls the maximum number of characters to display in the debug output
  1397. for each remote packet. The remaining characters are omitted. */
  1398. static int remote_packet_max_chars = 512;
  1399. /* Show the maximum number of characters to display for each remote packet
  1400. when remote debugging is enabled. */
  1401. static void
  1402. show_remote_packet_max_chars (struct ui_file *file, int from_tty,
  1403. struct cmd_list_element *c,
  1404. const char *value)
  1405. {
  1406. gdb_printf (file, _("Number of remote packet characters to "
  1407. "display is %s.\n"), value);
  1408. }
  1409. long
  1410. remote_target::get_memory_write_packet_size ()
  1411. {
  1412. return get_memory_packet_size (&memory_write_packet_config);
  1413. }
  1414. /* FIXME: needs to be per-remote-target. */
  1415. static struct memory_packet_config memory_read_packet_config =
  1416. {
  1417. "memory-read-packet-size",
  1418. };
  1419. static void
  1420. set_memory_read_packet_size (const char *args, int from_tty)
  1421. {
  1422. set_memory_packet_size (args, &memory_read_packet_config);
  1423. }
  1424. static void
  1425. show_memory_read_packet_size (const char *args, int from_tty)
  1426. {
  1427. show_memory_packet_size (&memory_read_packet_config);
  1428. }
  1429. long
  1430. remote_target::get_memory_read_packet_size ()
  1431. {
  1432. long size = get_memory_packet_size (&memory_read_packet_config);
  1433. /* FIXME: cagney/1999-11-07: Functions like getpkt() need to get an
  1434. extra buffer size argument before the memory read size can be
  1435. increased beyond this. */
  1436. if (size > get_remote_packet_size ())
  1437. size = get_remote_packet_size ();
  1438. return size;
  1439. }
  1440. struct packet_config
  1441. {
  1442. const char *name;
  1443. const char *title;
  1444. /* If auto, GDB auto-detects support for this packet or feature,
  1445. either through qSupported, or by trying the packet and looking
  1446. at the response. If true, GDB assumes the target supports this
  1447. packet. If false, the packet is disabled. Configs that don't
  1448. have an associated command always have this set to auto. */
  1449. enum auto_boolean detect;
  1450. /* The "show remote foo-packet" command created for this packet. */
  1451. cmd_list_element *show_cmd;
  1452. /* Does the target support this packet? */
  1453. enum packet_support support;
  1454. };
  1455. static enum packet_support packet_config_support (struct packet_config *config);
  1456. static enum packet_support packet_support (int packet);
  1457. static void
  1458. show_packet_config_cmd (ui_file *file, struct packet_config *config)
  1459. {
  1460. const char *support = "internal-error";
  1461. switch (packet_config_support (config))
  1462. {
  1463. case PACKET_ENABLE:
  1464. support = "enabled";
  1465. break;
  1466. case PACKET_DISABLE:
  1467. support = "disabled";
  1468. break;
  1469. case PACKET_SUPPORT_UNKNOWN:
  1470. support = "unknown";
  1471. break;
  1472. }
  1473. switch (config->detect)
  1474. {
  1475. case AUTO_BOOLEAN_AUTO:
  1476. gdb_printf (file,
  1477. _("Support for the `%s' packet "
  1478. "is auto-detected, currently %s.\n"),
  1479. config->name, support);
  1480. break;
  1481. case AUTO_BOOLEAN_TRUE:
  1482. case AUTO_BOOLEAN_FALSE:
  1483. gdb_printf (file,
  1484. _("Support for the `%s' packet is currently %s.\n"),
  1485. config->name, support);
  1486. break;
  1487. }
  1488. }
  1489. static void
  1490. add_packet_config_cmd (struct packet_config *config, const char *name,
  1491. const char *title, int legacy)
  1492. {
  1493. config->name = name;
  1494. config->title = title;
  1495. gdb::unique_xmalloc_ptr<char> set_doc
  1496. = xstrprintf ("Set use of remote protocol `%s' (%s) packet.",
  1497. name, title);
  1498. gdb::unique_xmalloc_ptr<char> show_doc
  1499. = xstrprintf ("Show current use of remote protocol `%s' (%s) packet.",
  1500. name, title);
  1501. /* set/show TITLE-packet {auto,on,off} */
  1502. gdb::unique_xmalloc_ptr<char> cmd_name = xstrprintf ("%s-packet", title);
  1503. set_show_commands cmds
  1504. = add_setshow_auto_boolean_cmd (cmd_name.release (), class_obscure,
  1505. &config->detect, set_doc.get (),
  1506. show_doc.get (), NULL, /* help_doc */
  1507. NULL,
  1508. show_remote_protocol_packet_cmd,
  1509. &remote_set_cmdlist, &remote_show_cmdlist);
  1510. config->show_cmd = cmds.show;
  1511. /* set/show remote NAME-packet {auto,on,off} -- legacy. */
  1512. if (legacy)
  1513. {
  1514. /* It's not clear who should take ownership of this string, so, for
  1515. now, make it static, and give copies to each of the add_alias_cmd
  1516. calls below. */
  1517. static gdb::unique_xmalloc_ptr<char> legacy_name
  1518. = xstrprintf ("%s-packet", name);
  1519. add_alias_cmd (legacy_name.get (), cmds.set, class_obscure, 0,
  1520. &remote_set_cmdlist);
  1521. add_alias_cmd (legacy_name.get (), cmds.show, class_obscure, 0,
  1522. &remote_show_cmdlist);
  1523. }
  1524. }
  1525. static enum packet_result
  1526. packet_check_result (const char *buf)
  1527. {
  1528. if (buf[0] != '\0')
  1529. {
  1530. /* The stub recognized the packet request. Check that the
  1531. operation succeeded. */
  1532. if (buf[0] == 'E'
  1533. && isxdigit (buf[1]) && isxdigit (buf[2])
  1534. && buf[3] == '\0')
  1535. /* "Enn" - definitely an error. */
  1536. return PACKET_ERROR;
  1537. /* Always treat "E." as an error. This will be used for
  1538. more verbose error messages, such as E.memtypes. */
  1539. if (buf[0] == 'E' && buf[1] == '.')
  1540. return PACKET_ERROR;
  1541. /* The packet may or may not be OK. Just assume it is. */
  1542. return PACKET_OK;
  1543. }
  1544. else
  1545. /* The stub does not support the packet. */
  1546. return PACKET_UNKNOWN;
  1547. }
  1548. static enum packet_result
  1549. packet_check_result (const gdb::char_vector &buf)
  1550. {
  1551. return packet_check_result (buf.data ());
  1552. }
  1553. static enum packet_result
  1554. packet_ok (const char *buf, struct packet_config *config)
  1555. {
  1556. enum packet_result result;
  1557. if (config->detect != AUTO_BOOLEAN_TRUE
  1558. && config->support == PACKET_DISABLE)
  1559. internal_error (__FILE__, __LINE__,
  1560. _("packet_ok: attempt to use a disabled packet"));
  1561. result = packet_check_result (buf);
  1562. switch (result)
  1563. {
  1564. case PACKET_OK:
  1565. case PACKET_ERROR:
  1566. /* The stub recognized the packet request. */
  1567. if (config->support == PACKET_SUPPORT_UNKNOWN)
  1568. {
  1569. remote_debug_printf ("Packet %s (%s) is supported",
  1570. config->name, config->title);
  1571. config->support = PACKET_ENABLE;
  1572. }
  1573. break;
  1574. case PACKET_UNKNOWN:
  1575. /* The stub does not support the packet. */
  1576. if (config->detect == AUTO_BOOLEAN_AUTO
  1577. && config->support == PACKET_ENABLE)
  1578. {
  1579. /* If the stub previously indicated that the packet was
  1580. supported then there is a protocol error. */
  1581. error (_("Protocol error: %s (%s) conflicting enabled responses."),
  1582. config->name, config->title);
  1583. }
  1584. else if (config->detect == AUTO_BOOLEAN_TRUE)
  1585. {
  1586. /* The user set it wrong. */
  1587. error (_("Enabled packet %s (%s) not recognized by stub"),
  1588. config->name, config->title);
  1589. }
  1590. remote_debug_printf ("Packet %s (%s) is NOT supported",
  1591. config->name, config->title);
  1592. config->support = PACKET_DISABLE;
  1593. break;
  1594. }
  1595. return result;
  1596. }
  1597. static enum packet_result
  1598. packet_ok (const gdb::char_vector &buf, struct packet_config *config)
  1599. {
  1600. return packet_ok (buf.data (), config);
  1601. }
  1602. enum {
  1603. PACKET_vCont = 0,
  1604. PACKET_X,
  1605. PACKET_qSymbol,
  1606. PACKET_P,
  1607. PACKET_p,
  1608. PACKET_Z0,
  1609. PACKET_Z1,
  1610. PACKET_Z2,
  1611. PACKET_Z3,
  1612. PACKET_Z4,
  1613. PACKET_vFile_setfs,
  1614. PACKET_vFile_open,
  1615. PACKET_vFile_pread,
  1616. PACKET_vFile_pwrite,
  1617. PACKET_vFile_close,
  1618. PACKET_vFile_unlink,
  1619. PACKET_vFile_readlink,
  1620. PACKET_vFile_fstat,
  1621. PACKET_qXfer_auxv,
  1622. PACKET_qXfer_features,
  1623. PACKET_qXfer_exec_file,
  1624. PACKET_qXfer_libraries,
  1625. PACKET_qXfer_libraries_svr4,
  1626. PACKET_qXfer_memory_map,
  1627. PACKET_qXfer_osdata,
  1628. PACKET_qXfer_threads,
  1629. PACKET_qXfer_statictrace_read,
  1630. PACKET_qXfer_traceframe_info,
  1631. PACKET_qXfer_uib,
  1632. PACKET_qGetTIBAddr,
  1633. PACKET_qGetTLSAddr,
  1634. PACKET_qSupported,
  1635. PACKET_qTStatus,
  1636. PACKET_QPassSignals,
  1637. PACKET_QCatchSyscalls,
  1638. PACKET_QProgramSignals,
  1639. PACKET_QSetWorkingDir,
  1640. PACKET_QStartupWithShell,
  1641. PACKET_QEnvironmentHexEncoded,
  1642. PACKET_QEnvironmentReset,
  1643. PACKET_QEnvironmentUnset,
  1644. PACKET_qCRC,
  1645. PACKET_qSearch_memory,
  1646. PACKET_vAttach,
  1647. PACKET_vRun,
  1648. PACKET_QStartNoAckMode,
  1649. PACKET_vKill,
  1650. PACKET_qXfer_siginfo_read,
  1651. PACKET_qXfer_siginfo_write,
  1652. PACKET_qAttached,
  1653. /* Support for conditional tracepoints. */
  1654. PACKET_ConditionalTracepoints,
  1655. /* Support for target-side breakpoint conditions. */
  1656. PACKET_ConditionalBreakpoints,
  1657. /* Support for target-side breakpoint commands. */
  1658. PACKET_BreakpointCommands,
  1659. /* Support for fast tracepoints. */
  1660. PACKET_FastTracepoints,
  1661. /* Support for static tracepoints. */
  1662. PACKET_StaticTracepoints,
  1663. /* Support for installing tracepoints while a trace experiment is
  1664. running. */
  1665. PACKET_InstallInTrace,
  1666. PACKET_bc,
  1667. PACKET_bs,
  1668. PACKET_TracepointSource,
  1669. PACKET_QAllow,
  1670. PACKET_qXfer_fdpic,
  1671. PACKET_QDisableRandomization,
  1672. PACKET_QAgent,
  1673. PACKET_QTBuffer_size,
  1674. PACKET_Qbtrace_off,
  1675. PACKET_Qbtrace_bts,
  1676. PACKET_Qbtrace_pt,
  1677. PACKET_qXfer_btrace,
  1678. /* Support for the QNonStop packet. */
  1679. PACKET_QNonStop,
  1680. /* Support for the QThreadEvents packet. */
  1681. PACKET_QThreadEvents,
  1682. /* Support for multi-process extensions. */
  1683. PACKET_multiprocess_feature,
  1684. /* Support for enabling and disabling tracepoints while a trace
  1685. experiment is running. */
  1686. PACKET_EnableDisableTracepoints_feature,
  1687. /* Support for collecting strings using the tracenz bytecode. */
  1688. PACKET_tracenz_feature,
  1689. /* Support for continuing to run a trace experiment while GDB is
  1690. disconnected. */
  1691. PACKET_DisconnectedTracing_feature,
  1692. /* Support for qXfer:libraries-svr4:read with a non-empty annex. */
  1693. PACKET_augmented_libraries_svr4_read_feature,
  1694. /* Support for the qXfer:btrace-conf:read packet. */
  1695. PACKET_qXfer_btrace_conf,
  1696. /* Support for the Qbtrace-conf:bts:size packet. */
  1697. PACKET_Qbtrace_conf_bts_size,
  1698. /* Support for swbreak+ feature. */
  1699. PACKET_swbreak_feature,
  1700. /* Support for hwbreak+ feature. */
  1701. PACKET_hwbreak_feature,
  1702. /* Support for fork events. */
  1703. PACKET_fork_event_feature,
  1704. /* Support for vfork events. */
  1705. PACKET_vfork_event_feature,
  1706. /* Support for the Qbtrace-conf:pt:size packet. */
  1707. PACKET_Qbtrace_conf_pt_size,
  1708. /* Support for exec events. */
  1709. PACKET_exec_event_feature,
  1710. /* Support for query supported vCont actions. */
  1711. PACKET_vContSupported,
  1712. /* Support remote CTRL-C. */
  1713. PACKET_vCtrlC,
  1714. /* Support TARGET_WAITKIND_NO_RESUMED. */
  1715. PACKET_no_resumed,
  1716. /* Support for memory tagging, allocation tag fetch/store
  1717. packets and the tag violation stop replies. */
  1718. PACKET_memory_tagging_feature,
  1719. PACKET_MAX
  1720. };
  1721. /* FIXME: needs to be per-remote-target. Ignoring this for now,
  1722. assuming all remote targets are the same server (thus all support
  1723. the same packets). */
  1724. static struct packet_config remote_protocol_packets[PACKET_MAX];
  1725. /* Returns the packet's corresponding "set remote foo-packet" command
  1726. state. See struct packet_config for more details. */
  1727. static enum auto_boolean
  1728. packet_set_cmd_state (int packet)
  1729. {
  1730. return remote_protocol_packets[packet].detect;
  1731. }
  1732. /* Returns whether a given packet or feature is supported. This takes
  1733. into account the state of the corresponding "set remote foo-packet"
  1734. command, which may be used to bypass auto-detection. */
  1735. static enum packet_support
  1736. packet_config_support (struct packet_config *config)
  1737. {
  1738. switch (config->detect)
  1739. {
  1740. case AUTO_BOOLEAN_TRUE:
  1741. return PACKET_ENABLE;
  1742. case AUTO_BOOLEAN_FALSE:
  1743. return PACKET_DISABLE;
  1744. case AUTO_BOOLEAN_AUTO:
  1745. return config->support;
  1746. default:
  1747. gdb_assert_not_reached ("bad switch");
  1748. }
  1749. }
  1750. /* Same as packet_config_support, but takes the packet's enum value as
  1751. argument. */
  1752. static enum packet_support
  1753. packet_support (int packet)
  1754. {
  1755. struct packet_config *config = &remote_protocol_packets[packet];
  1756. return packet_config_support (config);
  1757. }
  1758. static void
  1759. show_remote_protocol_packet_cmd (struct ui_file *file, int from_tty,
  1760. struct cmd_list_element *c,
  1761. const char *value)
  1762. {
  1763. struct packet_config *packet;
  1764. gdb_assert (c->var.has_value ());
  1765. for (packet = remote_protocol_packets;
  1766. packet < &remote_protocol_packets[PACKET_MAX];
  1767. packet++)
  1768. {
  1769. if (c == packet->show_cmd)
  1770. {
  1771. show_packet_config_cmd (file, packet);
  1772. return;
  1773. }
  1774. }
  1775. internal_error (__FILE__, __LINE__, _("Could not find config for %s"),
  1776. c->name);
  1777. }
  1778. /* Should we try one of the 'Z' requests? */
  1779. enum Z_packet_type
  1780. {
  1781. Z_PACKET_SOFTWARE_BP,
  1782. Z_PACKET_HARDWARE_BP,
  1783. Z_PACKET_WRITE_WP,
  1784. Z_PACKET_READ_WP,
  1785. Z_PACKET_ACCESS_WP,
  1786. NR_Z_PACKET_TYPES
  1787. };
  1788. /* For compatibility with older distributions. Provide a ``set remote
  1789. Z-packet ...'' command that updates all the Z packet types. */
  1790. static enum auto_boolean remote_Z_packet_detect;
  1791. static void
  1792. set_remote_protocol_Z_packet_cmd (const char *args, int from_tty,
  1793. struct cmd_list_element *c)
  1794. {
  1795. int i;
  1796. for (i = 0; i < NR_Z_PACKET_TYPES; i++)
  1797. remote_protocol_packets[PACKET_Z0 + i].detect = remote_Z_packet_detect;
  1798. }
  1799. static void
  1800. show_remote_protocol_Z_packet_cmd (struct ui_file *file, int from_tty,
  1801. struct cmd_list_element *c,
  1802. const char *value)
  1803. {
  1804. int i;
  1805. for (i = 0; i < NR_Z_PACKET_TYPES; i++)
  1806. {
  1807. show_packet_config_cmd (file, &remote_protocol_packets[PACKET_Z0 + i]);
  1808. }
  1809. }
  1810. /* Returns true if the multi-process extensions are in effect. */
  1811. static int
  1812. remote_multi_process_p (struct remote_state *rs)
  1813. {
  1814. return packet_support (PACKET_multiprocess_feature) == PACKET_ENABLE;
  1815. }
  1816. /* Returns true if fork events are supported. */
  1817. static int
  1818. remote_fork_event_p (struct remote_state *rs)
  1819. {
  1820. return packet_support (PACKET_fork_event_feature) == PACKET_ENABLE;
  1821. }
  1822. /* Returns true if vfork events are supported. */
  1823. static int
  1824. remote_vfork_event_p (struct remote_state *rs)
  1825. {
  1826. return packet_support (PACKET_vfork_event_feature) == PACKET_ENABLE;
  1827. }
  1828. /* Returns true if exec events are supported. */
  1829. static int
  1830. remote_exec_event_p (struct remote_state *rs)
  1831. {
  1832. return packet_support (PACKET_exec_event_feature) == PACKET_ENABLE;
  1833. }
  1834. /* Returns true if memory tagging is supported, false otherwise. */
  1835. static bool
  1836. remote_memory_tagging_p ()
  1837. {
  1838. return packet_support (PACKET_memory_tagging_feature) == PACKET_ENABLE;
  1839. }
  1840. /* Insert fork catchpoint target routine. If fork events are enabled
  1841. then return success, nothing more to do. */
  1842. int
  1843. remote_target::insert_fork_catchpoint (int pid)
  1844. {
  1845. struct remote_state *rs = get_remote_state ();
  1846. return !remote_fork_event_p (rs);
  1847. }
  1848. /* Remove fork catchpoint target routine. Nothing to do, just
  1849. return success. */
  1850. int
  1851. remote_target::remove_fork_catchpoint (int pid)
  1852. {
  1853. return 0;
  1854. }
  1855. /* Insert vfork catchpoint target routine. If vfork events are enabled
  1856. then return success, nothing more to do. */
  1857. int
  1858. remote_target::insert_vfork_catchpoint (int pid)
  1859. {
  1860. struct remote_state *rs = get_remote_state ();
  1861. return !remote_vfork_event_p (rs);
  1862. }
  1863. /* Remove vfork catchpoint target routine. Nothing to do, just
  1864. return success. */
  1865. int
  1866. remote_target::remove_vfork_catchpoint (int pid)
  1867. {
  1868. return 0;
  1869. }
  1870. /* Insert exec catchpoint target routine. If exec events are
  1871. enabled, just return success. */
  1872. int
  1873. remote_target::insert_exec_catchpoint (int pid)
  1874. {
  1875. struct remote_state *rs = get_remote_state ();
  1876. return !remote_exec_event_p (rs);
  1877. }
  1878. /* Remove exec catchpoint target routine. Nothing to do, just
  1879. return success. */
  1880. int
  1881. remote_target::remove_exec_catchpoint (int pid)
  1882. {
  1883. return 0;
  1884. }
  1885. /* Take advantage of the fact that the TID field is not used, to tag
  1886. special ptids with it set to != 0. */
  1887. static const ptid_t magic_null_ptid (42000, -1, 1);
  1888. static const ptid_t not_sent_ptid (42000, -2, 1);
  1889. static const ptid_t any_thread_ptid (42000, 0, 1);
  1890. /* Find out if the stub attached to PID (and hence GDB should offer to
  1891. detach instead of killing it when bailing out). */
  1892. int
  1893. remote_target::remote_query_attached (int pid)
  1894. {
  1895. struct remote_state *rs = get_remote_state ();
  1896. size_t size = get_remote_packet_size ();
  1897. if (packet_support (PACKET_qAttached) == PACKET_DISABLE)
  1898. return 0;
  1899. if (remote_multi_process_p (rs))
  1900. xsnprintf (rs->buf.data (), size, "qAttached:%x", pid);
  1901. else
  1902. xsnprintf (rs->buf.data (), size, "qAttached");
  1903. putpkt (rs->buf);
  1904. getpkt (&rs->buf, 0);
  1905. switch (packet_ok (rs->buf,
  1906. &remote_protocol_packets[PACKET_qAttached]))
  1907. {
  1908. case PACKET_OK:
  1909. if (strcmp (rs->buf.data (), "1") == 0)
  1910. return 1;
  1911. break;
  1912. case PACKET_ERROR:
  1913. warning (_("Remote failure reply: %s"), rs->buf.data ());
  1914. break;
  1915. case PACKET_UNKNOWN:
  1916. break;
  1917. }
  1918. return 0;
  1919. }
  1920. /* Add PID to GDB's inferior table. If FAKE_PID_P is true, then PID
  1921. has been invented by GDB, instead of reported by the target. Since
  1922. we can be connected to a remote system before before knowing about
  1923. any inferior, mark the target with execution when we find the first
  1924. inferior. If ATTACHED is 1, then we had just attached to this
  1925. inferior. If it is 0, then we just created this inferior. If it
  1926. is -1, then try querying the remote stub to find out if it had
  1927. attached to the inferior or not. If TRY_OPEN_EXEC is true then
  1928. attempt to open this inferior's executable as the main executable
  1929. if no main executable is open already. */
  1930. inferior *
  1931. remote_target::remote_add_inferior (bool fake_pid_p, int pid, int attached,
  1932. int try_open_exec)
  1933. {
  1934. struct inferior *inf;
  1935. /* Check whether this process we're learning about is to be
  1936. considered attached, or if is to be considered to have been
  1937. spawned by the stub. */
  1938. if (attached == -1)
  1939. attached = remote_query_attached (pid);
  1940. if (gdbarch_has_global_solist (target_gdbarch ()))
  1941. {
  1942. /* If the target shares code across all inferiors, then every
  1943. attach adds a new inferior. */
  1944. inf = add_inferior (pid);
  1945. /* ... and every inferior is bound to the same program space.
  1946. However, each inferior may still have its own address
  1947. space. */
  1948. inf->aspace = maybe_new_address_space ();
  1949. inf->pspace = current_program_space;
  1950. }
  1951. else
  1952. {
  1953. /* In the traditional debugging scenario, there's a 1-1 match
  1954. between program/address spaces. We simply bind the inferior
  1955. to the program space's address space. */
  1956. inf = current_inferior ();
  1957. /* However, if the current inferior is already bound to a
  1958. process, find some other empty inferior. */
  1959. if (inf->pid != 0)
  1960. {
  1961. inf = nullptr;
  1962. for (inferior *it : all_inferiors ())
  1963. if (it->pid == 0)
  1964. {
  1965. inf = it;
  1966. break;
  1967. }
  1968. }
  1969. if (inf == nullptr)
  1970. {
  1971. /* Since all inferiors were already bound to a process, add
  1972. a new inferior. */
  1973. inf = add_inferior_with_spaces ();
  1974. }
  1975. switch_to_inferior_no_thread (inf);
  1976. inf->push_target (this);
  1977. inferior_appeared (inf, pid);
  1978. }
  1979. inf->attach_flag = attached;
  1980. inf->fake_pid_p = fake_pid_p;
  1981. /* If no main executable is currently open then attempt to
  1982. open the file that was executed to create this inferior. */
  1983. if (try_open_exec && get_exec_file (0) == NULL)
  1984. exec_file_locate_attach (pid, 0, 1);
  1985. /* Check for exec file mismatch, and let the user solve it. */
  1986. validate_exec_file (1);
  1987. return inf;
  1988. }
  1989. static remote_thread_info *get_remote_thread_info (thread_info *thread);
  1990. static remote_thread_info *get_remote_thread_info (remote_target *target,
  1991. ptid_t ptid);
  1992. /* Add thread PTID to GDB's thread list. Tag it as executing/running
  1993. according to EXECUTING and RUNNING respectively. If SILENT_P (or the
  1994. remote_state::starting_up flag) is true then the new thread is added
  1995. silently, otherwise the new thread will be announced to the user. */
  1996. thread_info *
  1997. remote_target::remote_add_thread (ptid_t ptid, bool running, bool executing,
  1998. bool silent_p)
  1999. {
  2000. struct remote_state *rs = get_remote_state ();
  2001. struct thread_info *thread;
  2002. /* GDB historically didn't pull threads in the initial connection
  2003. setup. If the remote target doesn't even have a concept of
  2004. threads (e.g., a bare-metal target), even if internally we
  2005. consider that a single-threaded target, mentioning a new thread
  2006. might be confusing to the user. Be silent then, preserving the
  2007. age old behavior. */
  2008. if (rs->starting_up || silent_p)
  2009. thread = add_thread_silent (this, ptid);
  2010. else
  2011. thread = add_thread (this, ptid);
  2012. /* We start by assuming threads are resumed. That state then gets updated
  2013. when we process a matching stop reply. */
  2014. get_remote_thread_info (thread)->set_resumed ();
  2015. set_executing (this, ptid, executing);
  2016. set_running (this, ptid, running);
  2017. return thread;
  2018. }
  2019. /* Come here when we learn about a thread id from the remote target.
  2020. It may be the first time we hear about such thread, so take the
  2021. opportunity to add it to GDB's thread list. In case this is the
  2022. first time we're noticing its corresponding inferior, add it to
  2023. GDB's inferior list as well. EXECUTING indicates whether the
  2024. thread is (internally) executing or stopped. */
  2025. void
  2026. remote_target::remote_notice_new_inferior (ptid_t currthread, bool executing)
  2027. {
  2028. /* In non-stop mode, we assume new found threads are (externally)
  2029. running until proven otherwise with a stop reply. In all-stop,
  2030. we can only get here if all threads are stopped. */
  2031. bool running = target_is_non_stop_p ();
  2032. /* If this is a new thread, add it to GDB's thread list.
  2033. If we leave it up to WFI to do this, bad things will happen. */
  2034. thread_info *tp = find_thread_ptid (this, currthread);
  2035. if (tp != NULL && tp->state == THREAD_EXITED)
  2036. {
  2037. /* We're seeing an event on a thread id we knew had exited.
  2038. This has to be a new thread reusing the old id. Add it. */
  2039. remote_add_thread (currthread, running, executing, false);
  2040. return;
  2041. }
  2042. if (!in_thread_list (this, currthread))
  2043. {
  2044. struct inferior *inf = NULL;
  2045. int pid = currthread.pid ();
  2046. if (inferior_ptid.is_pid ()
  2047. && pid == inferior_ptid.pid ())
  2048. {
  2049. /* inferior_ptid has no thread member yet. This can happen
  2050. with the vAttach -> remote_wait,"TAAthread:" path if the
  2051. stub doesn't support qC. This is the first stop reported
  2052. after an attach, so this is the main thread. Update the
  2053. ptid in the thread list. */
  2054. if (in_thread_list (this, ptid_t (pid)))
  2055. thread_change_ptid (this, inferior_ptid, currthread);
  2056. else
  2057. {
  2058. thread_info *thr
  2059. = remote_add_thread (currthread, running, executing, false);
  2060. switch_to_thread (thr);
  2061. }
  2062. return;
  2063. }
  2064. if (magic_null_ptid == inferior_ptid)
  2065. {
  2066. /* inferior_ptid is not set yet. This can happen with the
  2067. vRun -> remote_wait,"TAAthread:" path if the stub
  2068. doesn't support qC. This is the first stop reported
  2069. after an attach, so this is the main thread. Update the
  2070. ptid in the thread list. */
  2071. thread_change_ptid (this, inferior_ptid, currthread);
  2072. return;
  2073. }
  2074. /* When connecting to a target remote, or to a target
  2075. extended-remote which already was debugging an inferior, we
  2076. may not know about it yet. Add it before adding its child
  2077. thread, so notifications are emitted in a sensible order. */
  2078. if (find_inferior_pid (this, currthread.pid ()) == NULL)
  2079. {
  2080. struct remote_state *rs = get_remote_state ();
  2081. bool fake_pid_p = !remote_multi_process_p (rs);
  2082. inf = remote_add_inferior (fake_pid_p,
  2083. currthread.pid (), -1, 1);
  2084. }
  2085. /* This is really a new thread. Add it. */
  2086. thread_info *new_thr
  2087. = remote_add_thread (currthread, running, executing, false);
  2088. /* If we found a new inferior, let the common code do whatever
  2089. it needs to with it (e.g., read shared libraries, insert
  2090. breakpoints), unless we're just setting up an all-stop
  2091. connection. */
  2092. if (inf != NULL)
  2093. {
  2094. struct remote_state *rs = get_remote_state ();
  2095. if (!rs->starting_up)
  2096. notice_new_inferior (new_thr, executing, 0);
  2097. }
  2098. }
  2099. }
  2100. /* Return THREAD's private thread data, creating it if necessary. */
  2101. static remote_thread_info *
  2102. get_remote_thread_info (thread_info *thread)
  2103. {
  2104. gdb_assert (thread != NULL);
  2105. if (thread->priv == NULL)
  2106. thread->priv.reset (new remote_thread_info);
  2107. return static_cast<remote_thread_info *> (thread->priv.get ());
  2108. }
  2109. /* Return PTID's private thread data, creating it if necessary. */
  2110. static remote_thread_info *
  2111. get_remote_thread_info (remote_target *target, ptid_t ptid)
  2112. {
  2113. thread_info *thr = find_thread_ptid (target, ptid);
  2114. return get_remote_thread_info (thr);
  2115. }
  2116. /* Call this function as a result of
  2117. 1) A halt indication (T packet) containing a thread id
  2118. 2) A direct query of currthread
  2119. 3) Successful execution of set thread */
  2120. static void
  2121. record_currthread (struct remote_state *rs, ptid_t currthread)
  2122. {
  2123. rs->general_thread = currthread;
  2124. }
  2125. /* If 'QPassSignals' is supported, tell the remote stub what signals
  2126. it can simply pass through to the inferior without reporting. */
  2127. void
  2128. remote_target::pass_signals (gdb::array_view<const unsigned char> pass_signals)
  2129. {
  2130. if (packet_support (PACKET_QPassSignals) != PACKET_DISABLE)
  2131. {
  2132. char *pass_packet, *p;
  2133. int count = 0;
  2134. struct remote_state *rs = get_remote_state ();
  2135. gdb_assert (pass_signals.size () < 256);
  2136. for (size_t i = 0; i < pass_signals.size (); i++)
  2137. {
  2138. if (pass_signals[i])
  2139. count++;
  2140. }
  2141. pass_packet = (char *) xmalloc (count * 3 + strlen ("QPassSignals:") + 1);
  2142. strcpy (pass_packet, "QPassSignals:");
  2143. p = pass_packet + strlen (pass_packet);
  2144. for (size_t i = 0; i < pass_signals.size (); i++)
  2145. {
  2146. if (pass_signals[i])
  2147. {
  2148. if (i >= 16)
  2149. *p++ = tohex (i >> 4);
  2150. *p++ = tohex (i & 15);
  2151. if (count)
  2152. *p++ = ';';
  2153. else
  2154. break;
  2155. count--;
  2156. }
  2157. }
  2158. *p = 0;
  2159. if (!rs->last_pass_packet || strcmp (rs->last_pass_packet, pass_packet))
  2160. {
  2161. putpkt (pass_packet);
  2162. getpkt (&rs->buf, 0);
  2163. packet_ok (rs->buf, &remote_protocol_packets[PACKET_QPassSignals]);
  2164. xfree (rs->last_pass_packet);
  2165. rs->last_pass_packet = pass_packet;
  2166. }
  2167. else
  2168. xfree (pass_packet);
  2169. }
  2170. }
  2171. /* If 'QCatchSyscalls' is supported, tell the remote stub
  2172. to report syscalls to GDB. */
  2173. int
  2174. remote_target::set_syscall_catchpoint (int pid, bool needed, int any_count,
  2175. gdb::array_view<const int> syscall_counts)
  2176. {
  2177. const char *catch_packet;
  2178. enum packet_result result;
  2179. int n_sysno = 0;
  2180. if (packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE)
  2181. {
  2182. /* Not supported. */
  2183. return 1;
  2184. }
  2185. if (needed && any_count == 0)
  2186. {
  2187. /* Count how many syscalls are to be caught. */
  2188. for (size_t i = 0; i < syscall_counts.size (); i++)
  2189. {
  2190. if (syscall_counts[i] != 0)
  2191. n_sysno++;
  2192. }
  2193. }
  2194. remote_debug_printf ("pid %d needed %d any_count %d n_sysno %d",
  2195. pid, needed, any_count, n_sysno);
  2196. std::string built_packet;
  2197. if (needed)
  2198. {
  2199. /* Prepare a packet with the sysno list, assuming max 8+1
  2200. characters for a sysno. If the resulting packet size is too
  2201. big, fallback on the non-selective packet. */
  2202. const int maxpktsz = strlen ("QCatchSyscalls:1") + n_sysno * 9 + 1;
  2203. built_packet.reserve (maxpktsz);
  2204. built_packet = "QCatchSyscalls:1";
  2205. if (any_count == 0)
  2206. {
  2207. /* Add in each syscall to be caught. */
  2208. for (size_t i = 0; i < syscall_counts.size (); i++)
  2209. {
  2210. if (syscall_counts[i] != 0)
  2211. string_appendf (built_packet, ";%zx", i);
  2212. }
  2213. }
  2214. if (built_packet.size () > get_remote_packet_size ())
  2215. {
  2216. /* catch_packet too big. Fallback to less efficient
  2217. non selective mode, with GDB doing the filtering. */
  2218. catch_packet = "QCatchSyscalls:1";
  2219. }
  2220. else
  2221. catch_packet = built_packet.c_str ();
  2222. }
  2223. else
  2224. catch_packet = "QCatchSyscalls:0";
  2225. struct remote_state *rs = get_remote_state ();
  2226. putpkt (catch_packet);
  2227. getpkt (&rs->buf, 0);
  2228. result = packet_ok (rs->buf, &remote_protocol_packets[PACKET_QCatchSyscalls]);
  2229. if (result == PACKET_OK)
  2230. return 0;
  2231. else
  2232. return -1;
  2233. }
  2234. /* If 'QProgramSignals' is supported, tell the remote stub what
  2235. signals it should pass through to the inferior when detaching. */
  2236. void
  2237. remote_target::program_signals (gdb::array_view<const unsigned char> signals)
  2238. {
  2239. if (packet_support (PACKET_QProgramSignals) != PACKET_DISABLE)
  2240. {
  2241. char *packet, *p;
  2242. int count = 0;
  2243. struct remote_state *rs = get_remote_state ();
  2244. gdb_assert (signals.size () < 256);
  2245. for (size_t i = 0; i < signals.size (); i++)
  2246. {
  2247. if (signals[i])
  2248. count++;
  2249. }
  2250. packet = (char *) xmalloc (count * 3 + strlen ("QProgramSignals:") + 1);
  2251. strcpy (packet, "QProgramSignals:");
  2252. p = packet + strlen (packet);
  2253. for (size_t i = 0; i < signals.size (); i++)
  2254. {
  2255. if (signal_pass_state (i))
  2256. {
  2257. if (i >= 16)
  2258. *p++ = tohex (i >> 4);
  2259. *p++ = tohex (i & 15);
  2260. if (count)
  2261. *p++ = ';';
  2262. else
  2263. break;
  2264. count--;
  2265. }
  2266. }
  2267. *p = 0;
  2268. if (!rs->last_program_signals_packet
  2269. || strcmp (rs->last_program_signals_packet, packet) != 0)
  2270. {
  2271. putpkt (packet);
  2272. getpkt (&rs->buf, 0);
  2273. packet_ok (rs->buf, &remote_protocol_packets[PACKET_QProgramSignals]);
  2274. xfree (rs->last_program_signals_packet);
  2275. rs->last_program_signals_packet = packet;
  2276. }
  2277. else
  2278. xfree (packet);
  2279. }
  2280. }
  2281. /* If PTID is MAGIC_NULL_PTID, don't set any thread. If PTID is
  2282. MINUS_ONE_PTID, set the thread to -1, so the stub returns the
  2283. thread. If GEN is set, set the general thread, if not, then set
  2284. the step/continue thread. */
  2285. void
  2286. remote_target::set_thread (ptid_t ptid, int gen)
  2287. {
  2288. struct remote_state *rs = get_remote_state ();
  2289. ptid_t state = gen ? rs->general_thread : rs->continue_thread;
  2290. char *buf = rs->buf.data ();
  2291. char *endbuf = buf + get_remote_packet_size ();
  2292. if (state == ptid)
  2293. return;
  2294. *buf++ = 'H';
  2295. *buf++ = gen ? 'g' : 'c';
  2296. if (ptid == magic_null_ptid)
  2297. xsnprintf (buf, endbuf - buf, "0");
  2298. else if (ptid == any_thread_ptid)
  2299. xsnprintf (buf, endbuf - buf, "0");
  2300. else if (ptid == minus_one_ptid)
  2301. xsnprintf (buf, endbuf - buf, "-1");
  2302. else
  2303. write_ptid (buf, endbuf, ptid);
  2304. putpkt (rs->buf);
  2305. getpkt (&rs->buf, 0);
  2306. if (gen)
  2307. rs->general_thread = ptid;
  2308. else
  2309. rs->continue_thread = ptid;
  2310. }
  2311. void
  2312. remote_target::set_general_thread (ptid_t ptid)
  2313. {
  2314. set_thread (ptid, 1);
  2315. }
  2316. void
  2317. remote_target::set_continue_thread (ptid_t ptid)
  2318. {
  2319. set_thread (ptid, 0);
  2320. }
  2321. /* Change the remote current process. Which thread within the process
  2322. ends up selected isn't important, as long as it is the same process
  2323. as what INFERIOR_PTID points to.
  2324. This comes from that fact that there is no explicit notion of
  2325. "selected process" in the protocol. The selected process for
  2326. general operations is the process the selected general thread
  2327. belongs to. */
  2328. void
  2329. remote_target::set_general_process ()
  2330. {
  2331. struct remote_state *rs = get_remote_state ();
  2332. /* If the remote can't handle multiple processes, don't bother. */
  2333. if (!remote_multi_process_p (rs))
  2334. return;
  2335. /* We only need to change the remote current thread if it's pointing
  2336. at some other process. */
  2337. if (rs->general_thread.pid () != inferior_ptid.pid ())
  2338. set_general_thread (inferior_ptid);
  2339. }
  2340. /* Return nonzero if this is the main thread that we made up ourselves
  2341. to model non-threaded targets as single-threaded. */
  2342. static int
  2343. remote_thread_always_alive (ptid_t ptid)
  2344. {
  2345. if (ptid == magic_null_ptid)
  2346. /* The main thread is always alive. */
  2347. return 1;
  2348. if (ptid.pid () != 0 && ptid.lwp () == 0)
  2349. /* The main thread is always alive. This can happen after a
  2350. vAttach, if the remote side doesn't support
  2351. multi-threading. */
  2352. return 1;
  2353. return 0;
  2354. }
  2355. /* Return nonzero if the thread PTID is still alive on the remote
  2356. system. */
  2357. bool
  2358. remote_target::thread_alive (ptid_t ptid)
  2359. {
  2360. struct remote_state *rs = get_remote_state ();
  2361. char *p, *endp;
  2362. /* Check if this is a thread that we made up ourselves to model
  2363. non-threaded targets as single-threaded. */
  2364. if (remote_thread_always_alive (ptid))
  2365. return 1;
  2366. p = rs->buf.data ();
  2367. endp = p + get_remote_packet_size ();
  2368. *p++ = 'T';
  2369. write_ptid (p, endp, ptid);
  2370. putpkt (rs->buf);
  2371. getpkt (&rs->buf, 0);
  2372. return (rs->buf[0] == 'O' && rs->buf[1] == 'K');
  2373. }
  2374. /* Return a pointer to a thread name if we know it and NULL otherwise.
  2375. The thread_info object owns the memory for the name. */
  2376. const char *
  2377. remote_target::thread_name (struct thread_info *info)
  2378. {
  2379. if (info->priv != NULL)
  2380. {
  2381. const std::string &name = get_remote_thread_info (info)->name;
  2382. return !name.empty () ? name.c_str () : NULL;
  2383. }
  2384. return NULL;
  2385. }
  2386. /* About these extended threadlist and threadinfo packets. They are
  2387. variable length packets but, the fields within them are often fixed
  2388. length. They are redundant enough to send over UDP as is the
  2389. remote protocol in general. There is a matching unit test module
  2390. in libstub. */
  2391. /* WARNING: This threadref data structure comes from the remote O.S.,
  2392. libstub protocol encoding, and remote.c. It is not particularly
  2393. changable. */
  2394. /* Right now, the internal structure is int. We want it to be bigger.
  2395. Plan to fix this. */
  2396. typedef int gdb_threadref; /* Internal GDB thread reference. */
  2397. /* gdb_ext_thread_info is an internal GDB data structure which is
  2398. equivalent to the reply of the remote threadinfo packet. */
  2399. struct gdb_ext_thread_info
  2400. {
  2401. threadref threadid; /* External form of thread reference. */
  2402. int active; /* Has state interesting to GDB?
  2403. regs, stack. */
  2404. char display[256]; /* Brief state display, name,
  2405. blocked/suspended. */
  2406. char shortname[32]; /* To be used to name threads. */
  2407. char more_display[256]; /* Long info, statistics, queue depth,
  2408. whatever. */
  2409. };
  2410. /* The volume of remote transfers can be limited by submitting
  2411. a mask containing bits specifying the desired information.
  2412. Use a union of these values as the 'selection' parameter to
  2413. get_thread_info. FIXME: Make these TAG names more thread specific. */
  2414. #define TAG_THREADID 1
  2415. #define TAG_EXISTS 2
  2416. #define TAG_DISPLAY 4
  2417. #define TAG_THREADNAME 8
  2418. #define TAG_MOREDISPLAY 16
  2419. #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES * 2)
  2420. static const char *unpack_nibble (const char *buf, int *val);
  2421. static const char *unpack_byte (const char *buf, int *value);
  2422. static char *pack_int (char *buf, int value);
  2423. static const char *unpack_int (const char *buf, int *value);
  2424. static const char *unpack_string (const char *src, char *dest, int length);
  2425. static char *pack_threadid (char *pkt, threadref *id);
  2426. static const char *unpack_threadid (const char *inbuf, threadref *id);
  2427. void int_to_threadref (threadref *id, int value);
  2428. static int threadref_to_int (threadref *ref);
  2429. static void copy_threadref (threadref *dest, threadref *src);
  2430. static int threadmatch (threadref *dest, threadref *src);
  2431. static char *pack_threadinfo_request (char *pkt, int mode,
  2432. threadref *id);
  2433. static char *pack_threadlist_request (char *pkt, int startflag,
  2434. int threadcount,
  2435. threadref *nextthread);
  2436. static int remote_newthread_step (threadref *ref, void *context);
  2437. /* Write a PTID to BUF. ENDBUF points to one-passed-the-end of the
  2438. buffer we're allowed to write to. Returns
  2439. BUF+CHARACTERS_WRITTEN. */
  2440. char *
  2441. remote_target::write_ptid (char *buf, const char *endbuf, ptid_t ptid)
  2442. {
  2443. int pid, tid;
  2444. struct remote_state *rs = get_remote_state ();
  2445. if (remote_multi_process_p (rs))
  2446. {
  2447. pid = ptid.pid ();
  2448. if (pid < 0)
  2449. buf += xsnprintf (buf, endbuf - buf, "p-%x.", -pid);
  2450. else
  2451. buf += xsnprintf (buf, endbuf - buf, "p%x.", pid);
  2452. }
  2453. tid = ptid.lwp ();
  2454. if (tid < 0)
  2455. buf += xsnprintf (buf, endbuf - buf, "-%x", -tid);
  2456. else
  2457. buf += xsnprintf (buf, endbuf - buf, "%x", tid);
  2458. return buf;
  2459. }
  2460. /* Extract a PTID from BUF. If non-null, OBUF is set to one past the
  2461. last parsed char. Returns null_ptid if no thread id is found, and
  2462. throws an error if the thread id has an invalid format. */
  2463. static ptid_t
  2464. read_ptid (const char *buf, const char **obuf)
  2465. {
  2466. const char *p = buf;
  2467. const char *pp;
  2468. ULONGEST pid = 0, tid = 0;
  2469. if (*p == 'p')
  2470. {
  2471. /* Multi-process ptid. */
  2472. pp = unpack_varlen_hex (p + 1, &pid);
  2473. if (*pp != '.')
  2474. error (_("invalid remote ptid: %s"), p);
  2475. p = pp;
  2476. pp = unpack_varlen_hex (p + 1, &tid);
  2477. if (obuf)
  2478. *obuf = pp;
  2479. return ptid_t (pid, tid);
  2480. }
  2481. /* No multi-process. Just a tid. */
  2482. pp = unpack_varlen_hex (p, &tid);
  2483. /* Return null_ptid when no thread id is found. */
  2484. if (p == pp)
  2485. {
  2486. if (obuf)
  2487. *obuf = pp;
  2488. return null_ptid;
  2489. }
  2490. /* Since the stub is not sending a process id, default to what's
  2491. current_inferior, unless it doesn't have a PID yet. If so,
  2492. then since there's no way to know the pid of the reported
  2493. threads, use the magic number. */
  2494. inferior *inf = current_inferior ();
  2495. if (inf->pid == 0)
  2496. pid = magic_null_ptid.pid ();
  2497. else
  2498. pid = inf->pid;
  2499. if (obuf)
  2500. *obuf = pp;
  2501. return ptid_t (pid, tid);
  2502. }
  2503. static int
  2504. stubhex (int ch)
  2505. {
  2506. if (ch >= 'a' && ch <= 'f')
  2507. return ch - 'a' + 10;
  2508. if (ch >= '0' && ch <= '9')
  2509. return ch - '0';
  2510. if (ch >= 'A' && ch <= 'F')
  2511. return ch - 'A' + 10;
  2512. return -1;
  2513. }
  2514. static int
  2515. stub_unpack_int (const char *buff, int fieldlength)
  2516. {
  2517. int nibble;
  2518. int retval = 0;
  2519. while (fieldlength)
  2520. {
  2521. nibble = stubhex (*buff++);
  2522. retval |= nibble;
  2523. fieldlength--;
  2524. if (fieldlength)
  2525. retval = retval << 4;
  2526. }
  2527. return retval;
  2528. }
  2529. static const char *
  2530. unpack_nibble (const char *buf, int *val)
  2531. {
  2532. *val = fromhex (*buf++);
  2533. return buf;
  2534. }
  2535. static const char *
  2536. unpack_byte (const char *buf, int *value)
  2537. {
  2538. *value = stub_unpack_int (buf, 2);
  2539. return buf + 2;
  2540. }
  2541. static char *
  2542. pack_int (char *buf, int value)
  2543. {
  2544. buf = pack_hex_byte (buf, (value >> 24) & 0xff);
  2545. buf = pack_hex_byte (buf, (value >> 16) & 0xff);
  2546. buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
  2547. buf = pack_hex_byte (buf, (value & 0xff));
  2548. return buf;
  2549. }
  2550. static const char *
  2551. unpack_int (const char *buf, int *value)
  2552. {
  2553. *value = stub_unpack_int (buf, 8);
  2554. return buf + 8;
  2555. }
  2556. #if 0 /* Currently unused, uncomment when needed. */
  2557. static char *pack_string (char *pkt, char *string);
  2558. static char *
  2559. pack_string (char *pkt, char *string)
  2560. {
  2561. char ch;
  2562. int len;
  2563. len = strlen (string);
  2564. if (len > 200)
  2565. len = 200; /* Bigger than most GDB packets, junk??? */
  2566. pkt = pack_hex_byte (pkt, len);
  2567. while (len-- > 0)
  2568. {
  2569. ch = *string++;
  2570. if ((ch == '\0') || (ch == '#'))
  2571. ch = '*'; /* Protect encapsulation. */
  2572. *pkt++ = ch;
  2573. }
  2574. return pkt;
  2575. }
  2576. #endif /* 0 (unused) */
  2577. static const char *
  2578. unpack_string (const char *src, char *dest, int length)
  2579. {
  2580. while (length--)
  2581. *dest++ = *src++;
  2582. *dest = '\0';
  2583. return src;
  2584. }
  2585. static char *
  2586. pack_threadid (char *pkt, threadref *id)
  2587. {
  2588. char *limit;
  2589. unsigned char *altid;
  2590. altid = (unsigned char *) id;
  2591. limit = pkt + BUF_THREAD_ID_SIZE;
  2592. while (pkt < limit)
  2593. pkt = pack_hex_byte (pkt, *altid++);
  2594. return pkt;
  2595. }
  2596. static const char *
  2597. unpack_threadid (const char *inbuf, threadref *id)
  2598. {
  2599. char *altref;
  2600. const char *limit = inbuf + BUF_THREAD_ID_SIZE;
  2601. int x, y;
  2602. altref = (char *) id;
  2603. while (inbuf < limit)
  2604. {
  2605. x = stubhex (*inbuf++);
  2606. y = stubhex (*inbuf++);
  2607. *altref++ = (x << 4) | y;
  2608. }
  2609. return inbuf;
  2610. }
  2611. /* Externally, threadrefs are 64 bits but internally, they are still
  2612. ints. This is due to a mismatch of specifications. We would like
  2613. to use 64bit thread references internally. This is an adapter
  2614. function. */
  2615. void
  2616. int_to_threadref (threadref *id, int value)
  2617. {
  2618. unsigned char *scan;
  2619. scan = (unsigned char *) id;
  2620. {
  2621. int i = 4;
  2622. while (i--)
  2623. *scan++ = 0;
  2624. }
  2625. *scan++ = (value >> 24) & 0xff;
  2626. *scan++ = (value >> 16) & 0xff;
  2627. *scan++ = (value >> 8) & 0xff;
  2628. *scan++ = (value & 0xff);
  2629. }
  2630. static int
  2631. threadref_to_int (threadref *ref)
  2632. {
  2633. int i, value = 0;
  2634. unsigned char *scan;
  2635. scan = *ref;
  2636. scan += 4;
  2637. i = 4;
  2638. while (i-- > 0)
  2639. value = (value << 8) | ((*scan++) & 0xff);
  2640. return value;
  2641. }
  2642. static void
  2643. copy_threadref (threadref *dest, threadref *src)
  2644. {
  2645. int i;
  2646. unsigned char *csrc, *cdest;
  2647. csrc = (unsigned char *) src;
  2648. cdest = (unsigned char *) dest;
  2649. i = 8;
  2650. while (i--)
  2651. *cdest++ = *csrc++;
  2652. }
  2653. static int
  2654. threadmatch (threadref *dest, threadref *src)
  2655. {
  2656. /* Things are broken right now, so just assume we got a match. */
  2657. #if 0
  2658. unsigned char *srcp, *destp;
  2659. int i, result;
  2660. srcp = (char *) src;
  2661. destp = (char *) dest;
  2662. result = 1;
  2663. while (i-- > 0)
  2664. result &= (*srcp++ == *destp++) ? 1 : 0;
  2665. return result;
  2666. #endif
  2667. return 1;
  2668. }
  2669. /*
  2670. threadid:1, # always request threadid
  2671. context_exists:2,
  2672. display:4,
  2673. unique_name:8,
  2674. more_display:16
  2675. */
  2676. /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
  2677. static char *
  2678. pack_threadinfo_request (char *pkt, int mode, threadref *id)
  2679. {
  2680. *pkt++ = 'q'; /* Info Query */
  2681. *pkt++ = 'P'; /* process or thread info */
  2682. pkt = pack_int (pkt, mode); /* mode */
  2683. pkt = pack_threadid (pkt, id); /* threadid */
  2684. *pkt = '\0'; /* terminate */
  2685. return pkt;
  2686. }
  2687. /* These values tag the fields in a thread info response packet. */
  2688. /* Tagging the fields allows us to request specific fields and to
  2689. add more fields as time goes by. */
  2690. #define TAG_THREADID 1 /* Echo the thread identifier. */
  2691. #define TAG_EXISTS 2 /* Is this process defined enough to
  2692. fetch registers and its stack? */
  2693. #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
  2694. #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is. */
  2695. #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
  2696. the process. */
  2697. int
  2698. remote_target::remote_unpack_thread_info_response (const char *pkt,
  2699. threadref *expectedref,
  2700. gdb_ext_thread_info *info)
  2701. {
  2702. struct remote_state *rs = get_remote_state ();
  2703. int mask, length;
  2704. int tag;
  2705. threadref ref;
  2706. const char *limit = pkt + rs->buf.size (); /* Plausible parsing limit. */
  2707. int retval = 1;
  2708. /* info->threadid = 0; FIXME: implement zero_threadref. */
  2709. info->active = 0;
  2710. info->display[0] = '\0';
  2711. info->shortname[0] = '\0';
  2712. info->more_display[0] = '\0';
  2713. /* Assume the characters indicating the packet type have been
  2714. stripped. */
  2715. pkt = unpack_int (pkt, &mask); /* arg mask */
  2716. pkt = unpack_threadid (pkt, &ref);
  2717. if (mask == 0)
  2718. warning (_("Incomplete response to threadinfo request."));
  2719. if (!threadmatch (&ref, expectedref))
  2720. { /* This is an answer to a different request. */
  2721. warning (_("ERROR RMT Thread info mismatch."));
  2722. return 0;
  2723. }
  2724. copy_threadref (&info->threadid, &ref);
  2725. /* Loop on tagged fields , try to bail if something goes wrong. */
  2726. /* Packets are terminated with nulls. */
  2727. while ((pkt < limit) && mask && *pkt)
  2728. {
  2729. pkt = unpack_int (pkt, &tag); /* tag */
  2730. pkt = unpack_byte (pkt, &length); /* length */
  2731. if (!(tag & mask)) /* Tags out of synch with mask. */
  2732. {
  2733. warning (_("ERROR RMT: threadinfo tag mismatch."));
  2734. retval = 0;
  2735. break;
  2736. }
  2737. if (tag == TAG_THREADID)
  2738. {
  2739. if (length != 16)
  2740. {
  2741. warning (_("ERROR RMT: length of threadid is not 16."));
  2742. retval = 0;
  2743. break;
  2744. }
  2745. pkt = unpack_threadid (pkt, &ref);
  2746. mask = mask & ~TAG_THREADID;
  2747. continue;
  2748. }
  2749. if (tag == TAG_EXISTS)
  2750. {
  2751. info->active = stub_unpack_int (pkt, length);
  2752. pkt += length;
  2753. mask = mask & ~(TAG_EXISTS);
  2754. if (length > 8)
  2755. {
  2756. warning (_("ERROR RMT: 'exists' length too long."));
  2757. retval = 0;
  2758. break;
  2759. }
  2760. continue;
  2761. }
  2762. if (tag == TAG_THREADNAME)
  2763. {
  2764. pkt = unpack_string (pkt, &info->shortname[0], length);
  2765. mask = mask & ~TAG_THREADNAME;
  2766. continue;
  2767. }
  2768. if (tag == TAG_DISPLAY)
  2769. {
  2770. pkt = unpack_string (pkt, &info->display[0], length);
  2771. mask = mask & ~TAG_DISPLAY;
  2772. continue;
  2773. }
  2774. if (tag == TAG_MOREDISPLAY)
  2775. {
  2776. pkt = unpack_string (pkt, &info->more_display[0], length);
  2777. mask = mask & ~TAG_MOREDISPLAY;
  2778. continue;
  2779. }
  2780. warning (_("ERROR RMT: unknown thread info tag."));
  2781. break; /* Not a tag we know about. */
  2782. }
  2783. return retval;
  2784. }
  2785. int
  2786. remote_target::remote_get_threadinfo (threadref *threadid,
  2787. int fieldset,
  2788. gdb_ext_thread_info *info)
  2789. {
  2790. struct remote_state *rs = get_remote_state ();
  2791. int result;
  2792. pack_threadinfo_request (rs->buf.data (), fieldset, threadid);
  2793. putpkt (rs->buf);
  2794. getpkt (&rs->buf, 0);
  2795. if (rs->buf[0] == '\0')
  2796. return 0;
  2797. result = remote_unpack_thread_info_response (&rs->buf[2],
  2798. threadid, info);
  2799. return result;
  2800. }
  2801. /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
  2802. static char *
  2803. pack_threadlist_request (char *pkt, int startflag, int threadcount,
  2804. threadref *nextthread)
  2805. {
  2806. *pkt++ = 'q'; /* info query packet */
  2807. *pkt++ = 'L'; /* Process LIST or threadLIST request */
  2808. pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
  2809. pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
  2810. pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
  2811. *pkt = '\0';
  2812. return pkt;
  2813. }
  2814. /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
  2815. int
  2816. remote_target::parse_threadlist_response (const char *pkt, int result_limit,
  2817. threadref *original_echo,
  2818. threadref *resultlist,
  2819. int *doneflag)
  2820. {
  2821. struct remote_state *rs = get_remote_state ();
  2822. int count, resultcount, done;
  2823. resultcount = 0;
  2824. /* Assume the 'q' and 'M chars have been stripped. */
  2825. const char *limit = pkt + (rs->buf.size () - BUF_THREAD_ID_SIZE);
  2826. /* done parse past here */
  2827. pkt = unpack_byte (pkt, &count); /* count field */
  2828. pkt = unpack_nibble (pkt, &done);
  2829. /* The first threadid is the argument threadid. */
  2830. pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
  2831. while ((count-- > 0) && (pkt < limit))
  2832. {
  2833. pkt = unpack_threadid (pkt, resultlist++);
  2834. if (resultcount++ >= result_limit)
  2835. break;
  2836. }
  2837. if (doneflag)
  2838. *doneflag = done;
  2839. return resultcount;
  2840. }
  2841. /* Fetch the next batch of threads from the remote. Returns -1 if the
  2842. qL packet is not supported, 0 on error and 1 on success. */
  2843. int
  2844. remote_target::remote_get_threadlist (int startflag, threadref *nextthread,
  2845. int result_limit, int *done, int *result_count,
  2846. threadref *threadlist)
  2847. {
  2848. struct remote_state *rs = get_remote_state ();
  2849. int result = 1;
  2850. /* Truncate result limit to be smaller than the packet size. */
  2851. if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10)
  2852. >= get_remote_packet_size ())
  2853. result_limit = (get_remote_packet_size () / BUF_THREAD_ID_SIZE) - 2;
  2854. pack_threadlist_request (rs->buf.data (), startflag, result_limit,
  2855. nextthread);
  2856. putpkt (rs->buf);
  2857. getpkt (&rs->buf, 0);
  2858. if (rs->buf[0] == '\0')
  2859. {
  2860. /* Packet not supported. */
  2861. return -1;
  2862. }
  2863. *result_count =
  2864. parse_threadlist_response (&rs->buf[2], result_limit,
  2865. &rs->echo_nextthread, threadlist, done);
  2866. if (!threadmatch (&rs->echo_nextthread, nextthread))
  2867. {
  2868. /* FIXME: This is a good reason to drop the packet. */
  2869. /* Possibly, there is a duplicate response. */
  2870. /* Possibilities :
  2871. retransmit immediatly - race conditions
  2872. retransmit after timeout - yes
  2873. exit
  2874. wait for packet, then exit
  2875. */
  2876. warning (_("HMM: threadlist did not echo arg thread, dropping it."));
  2877. return 0; /* I choose simply exiting. */
  2878. }
  2879. if (*result_count <= 0)
  2880. {
  2881. if (*done != 1)
  2882. {
  2883. warning (_("RMT ERROR : failed to get remote thread list."));
  2884. result = 0;
  2885. }
  2886. return result; /* break; */
  2887. }
  2888. if (*result_count > result_limit)
  2889. {
  2890. *result_count = 0;
  2891. warning (_("RMT ERROR: threadlist response longer than requested."));
  2892. return 0;
  2893. }
  2894. return result;
  2895. }
  2896. /* Fetch the list of remote threads, with the qL packet, and call
  2897. STEPFUNCTION for each thread found. Stops iterating and returns 1
  2898. if STEPFUNCTION returns true. Stops iterating and returns 0 if the
  2899. STEPFUNCTION returns false. If the packet is not supported,
  2900. returns -1. */
  2901. int
  2902. remote_target::remote_threadlist_iterator (rmt_thread_action stepfunction,
  2903. void *context, int looplimit)
  2904. {
  2905. struct remote_state *rs = get_remote_state ();
  2906. int done, i, result_count;
  2907. int startflag = 1;
  2908. int result = 1;
  2909. int loopcount = 0;
  2910. done = 0;
  2911. while (!done)
  2912. {
  2913. if (loopcount++ > looplimit)
  2914. {
  2915. result = 0;
  2916. warning (_("Remote fetch threadlist -infinite loop-."));
  2917. break;
  2918. }
  2919. result = remote_get_threadlist (startflag, &rs->nextthread,
  2920. MAXTHREADLISTRESULTS,
  2921. &done, &result_count,
  2922. rs->resultthreadlist);
  2923. if (result <= 0)
  2924. break;
  2925. /* Clear for later iterations. */
  2926. startflag = 0;
  2927. /* Setup to resume next batch of thread references, set nextthread. */
  2928. if (result_count >= 1)
  2929. copy_threadref (&rs->nextthread,
  2930. &rs->resultthreadlist[result_count - 1]);
  2931. i = 0;
  2932. while (result_count--)
  2933. {
  2934. if (!(*stepfunction) (&rs->resultthreadlist[i++], context))
  2935. {
  2936. result = 0;
  2937. break;
  2938. }
  2939. }
  2940. }
  2941. return result;
  2942. }
  2943. /* A thread found on the remote target. */
  2944. struct thread_item
  2945. {
  2946. explicit thread_item (ptid_t ptid_)
  2947. : ptid (ptid_)
  2948. {}
  2949. thread_item (thread_item &&other) = default;
  2950. thread_item &operator= (thread_item &&other) = default;
  2951. DISABLE_COPY_AND_ASSIGN (thread_item);
  2952. /* The thread's PTID. */
  2953. ptid_t ptid;
  2954. /* The thread's extra info. */
  2955. std::string extra;
  2956. /* The thread's name. */
  2957. std::string name;
  2958. /* The core the thread was running on. -1 if not known. */
  2959. int core = -1;
  2960. /* The thread handle associated with the thread. */
  2961. gdb::byte_vector thread_handle;
  2962. };
  2963. /* Context passed around to the various methods listing remote
  2964. threads. As new threads are found, they're added to the ITEMS
  2965. vector. */
  2966. struct threads_listing_context
  2967. {
  2968. /* Return true if this object contains an entry for a thread with ptid
  2969. PTID. */
  2970. bool contains_thread (ptid_t ptid) const
  2971. {
  2972. auto match_ptid = [&] (const thread_item &item)
  2973. {
  2974. return item.ptid == ptid;
  2975. };
  2976. auto it = std::find_if (this->items.begin (),
  2977. this->items.end (),
  2978. match_ptid);
  2979. return it != this->items.end ();
  2980. }
  2981. /* Remove the thread with ptid PTID. */
  2982. void remove_thread (ptid_t ptid)
  2983. {
  2984. auto match_ptid = [&] (const thread_item &item)
  2985. {
  2986. return item.ptid == ptid;
  2987. };
  2988. auto it = std::remove_if (this->items.begin (),
  2989. this->items.end (),
  2990. match_ptid);
  2991. if (it != this->items.end ())
  2992. this->items.erase (it);
  2993. }
  2994. /* The threads found on the remote target. */
  2995. std::vector<thread_item> items;
  2996. };
  2997. static int
  2998. remote_newthread_step (threadref *ref, void *data)
  2999. {
  3000. struct threads_listing_context *context
  3001. = (struct threads_listing_context *) data;
  3002. int pid = inferior_ptid.pid ();
  3003. int lwp = threadref_to_int (ref);
  3004. ptid_t ptid (pid, lwp);
  3005. context->items.emplace_back (ptid);
  3006. return 1; /* continue iterator */
  3007. }
  3008. #define CRAZY_MAX_THREADS 1000
  3009. ptid_t
  3010. remote_target::remote_current_thread (ptid_t oldpid)
  3011. {
  3012. struct remote_state *rs = get_remote_state ();
  3013. putpkt ("qC");
  3014. getpkt (&rs->buf, 0);
  3015. if (rs->buf[0] == 'Q' && rs->buf[1] == 'C')
  3016. {
  3017. const char *obuf;
  3018. ptid_t result;
  3019. result = read_ptid (&rs->buf[2], &obuf);
  3020. if (*obuf != '\0')
  3021. remote_debug_printf ("warning: garbage in qC reply");
  3022. return result;
  3023. }
  3024. else
  3025. return oldpid;
  3026. }
  3027. /* List remote threads using the deprecated qL packet. */
  3028. int
  3029. remote_target::remote_get_threads_with_ql (threads_listing_context *context)
  3030. {
  3031. if (remote_threadlist_iterator (remote_newthread_step, context,
  3032. CRAZY_MAX_THREADS) >= 0)
  3033. return 1;
  3034. return 0;
  3035. }
  3036. #if defined(HAVE_LIBEXPAT)
  3037. static void
  3038. start_thread (struct gdb_xml_parser *parser,
  3039. const struct gdb_xml_element *element,
  3040. void *user_data,
  3041. std::vector<gdb_xml_value> &attributes)
  3042. {
  3043. struct threads_listing_context *data
  3044. = (struct threads_listing_context *) user_data;
  3045. struct gdb_xml_value *attr;
  3046. char *id = (char *) xml_find_attribute (attributes, "id")->value.get ();
  3047. ptid_t ptid = read_ptid (id, NULL);
  3048. data->items.emplace_back (ptid);
  3049. thread_item &item = data->items.back ();
  3050. attr = xml_find_attribute (attributes, "core");
  3051. if (attr != NULL)
  3052. item.core = *(ULONGEST *) attr->value.get ();
  3053. attr = xml_find_attribute (attributes, "name");
  3054. if (attr != NULL)
  3055. item.name = (const char *) attr->value.get ();
  3056. attr = xml_find_attribute (attributes, "handle");
  3057. if (attr != NULL)
  3058. item.thread_handle = hex2bin ((const char *) attr->value.get ());
  3059. }
  3060. static void
  3061. end_thread (struct gdb_xml_parser *parser,
  3062. const struct gdb_xml_element *element,
  3063. void *user_data, const char *body_text)
  3064. {
  3065. struct threads_listing_context *data
  3066. = (struct threads_listing_context *) user_data;
  3067. if (body_text != NULL && *body_text != '\0')
  3068. data->items.back ().extra = body_text;
  3069. }
  3070. const struct gdb_xml_attribute thread_attributes[] = {
  3071. { "id", GDB_XML_AF_NONE, NULL, NULL },
  3072. { "core", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
  3073. { "name", GDB_XML_AF_OPTIONAL, NULL, NULL },
  3074. { "handle", GDB_XML_AF_OPTIONAL, NULL, NULL },
  3075. { NULL, GDB_XML_AF_NONE, NULL, NULL }
  3076. };
  3077. const struct gdb_xml_element thread_children[] = {
  3078. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  3079. };
  3080. const struct gdb_xml_element threads_children[] = {
  3081. { "thread", thread_attributes, thread_children,
  3082. GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  3083. start_thread, end_thread },
  3084. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  3085. };
  3086. const struct gdb_xml_element threads_elements[] = {
  3087. { "threads", NULL, threads_children,
  3088. GDB_XML_EF_NONE, NULL, NULL },
  3089. { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  3090. };
  3091. #endif
  3092. /* List remote threads using qXfer:threads:read. */
  3093. int
  3094. remote_target::remote_get_threads_with_qxfer (threads_listing_context *context)
  3095. {
  3096. #if defined(HAVE_LIBEXPAT)
  3097. if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
  3098. {
  3099. gdb::optional<gdb::char_vector> xml
  3100. = target_read_stralloc (this, TARGET_OBJECT_THREADS, NULL);
  3101. if (xml && (*xml)[0] != '\0')
  3102. {
  3103. gdb_xml_parse_quick (_("threads"), "threads.dtd",
  3104. threads_elements, xml->data (), context);
  3105. }
  3106. return 1;
  3107. }
  3108. #endif
  3109. return 0;
  3110. }
  3111. /* List remote threads using qfThreadInfo/qsThreadInfo. */
  3112. int
  3113. remote_target::remote_get_threads_with_qthreadinfo (threads_listing_context *context)
  3114. {
  3115. struct remote_state *rs = get_remote_state ();
  3116. if (rs->use_threadinfo_query)
  3117. {
  3118. const char *bufp;
  3119. putpkt ("qfThreadInfo");
  3120. getpkt (&rs->buf, 0);
  3121. bufp = rs->buf.data ();
  3122. if (bufp[0] != '\0') /* q packet recognized */
  3123. {
  3124. while (*bufp++ == 'm') /* reply contains one or more TID */
  3125. {
  3126. do
  3127. {
  3128. ptid_t ptid = read_ptid (bufp, &bufp);
  3129. context->items.emplace_back (ptid);
  3130. }
  3131. while (*bufp++ == ','); /* comma-separated list */
  3132. putpkt ("qsThreadInfo");
  3133. getpkt (&rs->buf, 0);
  3134. bufp = rs->buf.data ();
  3135. }
  3136. return 1;
  3137. }
  3138. else
  3139. {
  3140. /* Packet not recognized. */
  3141. rs->use_threadinfo_query = 0;
  3142. }
  3143. }
  3144. return 0;
  3145. }
  3146. /* Return true if INF only has one non-exited thread. */
  3147. static bool
  3148. has_single_non_exited_thread (inferior *inf)
  3149. {
  3150. int count = 0;
  3151. for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
  3152. if (++count > 1)
  3153. break;
  3154. return count == 1;
  3155. }
  3156. /* Implement the to_update_thread_list function for the remote
  3157. targets. */
  3158. void
  3159. remote_target::update_thread_list ()
  3160. {
  3161. struct threads_listing_context context;
  3162. int got_list = 0;
  3163. /* We have a few different mechanisms to fetch the thread list. Try
  3164. them all, starting with the most preferred one first, falling
  3165. back to older methods. */
  3166. if (remote_get_threads_with_qxfer (&context)
  3167. || remote_get_threads_with_qthreadinfo (&context)
  3168. || remote_get_threads_with_ql (&context))
  3169. {
  3170. got_list = 1;
  3171. if (context.items.empty ()
  3172. && remote_thread_always_alive (inferior_ptid))
  3173. {
  3174. /* Some targets don't really support threads, but still
  3175. reply an (empty) thread list in response to the thread
  3176. listing packets, instead of replying "packet not
  3177. supported". Exit early so we don't delete the main
  3178. thread. */
  3179. return;
  3180. }
  3181. /* CONTEXT now holds the current thread list on the remote
  3182. target end. Delete GDB-side threads no longer found on the
  3183. target. */
  3184. for (thread_info *tp : all_threads_safe ())
  3185. {
  3186. if (tp->inf->process_target () != this)
  3187. continue;
  3188. if (!context.contains_thread (tp->ptid))
  3189. {
  3190. /* Do not remove the thread if it is the last thread in
  3191. the inferior. This situation happens when we have a
  3192. pending exit process status to process. Otherwise we
  3193. may end up with a seemingly live inferior (i.e. pid
  3194. != 0) that has no threads. */
  3195. if (has_single_non_exited_thread (tp->inf))
  3196. continue;
  3197. /* Not found. */
  3198. delete_thread (tp);
  3199. }
  3200. }
  3201. /* Remove any unreported fork child threads from CONTEXT so
  3202. that we don't interfere with follow fork, which is where
  3203. creation of such threads is handled. */
  3204. remove_new_fork_children (&context);
  3205. /* And now add threads we don't know about yet to our list. */
  3206. for (thread_item &item : context.items)
  3207. {
  3208. if (item.ptid != null_ptid)
  3209. {
  3210. /* In non-stop mode, we assume new found threads are
  3211. executing until proven otherwise with a stop reply.
  3212. In all-stop, we can only get here if all threads are
  3213. stopped. */
  3214. bool executing = target_is_non_stop_p ();
  3215. remote_notice_new_inferior (item.ptid, executing);
  3216. thread_info *tp = find_thread_ptid (this, item.ptid);
  3217. remote_thread_info *info = get_remote_thread_info (tp);
  3218. info->core = item.core;
  3219. info->extra = std::move (item.extra);
  3220. info->name = std::move (item.name);
  3221. info->thread_handle = std::move (item.thread_handle);
  3222. }
  3223. }
  3224. }
  3225. if (!got_list)
  3226. {
  3227. /* If no thread listing method is supported, then query whether
  3228. each known thread is alive, one by one, with the T packet.
  3229. If the target doesn't support threads at all, then this is a
  3230. no-op. See remote_thread_alive. */
  3231. prune_threads ();
  3232. }
  3233. }
  3234. /*
  3235. * Collect a descriptive string about the given thread.
  3236. * The target may say anything it wants to about the thread
  3237. * (typically info about its blocked / runnable state, name, etc.).
  3238. * This string will appear in the info threads display.
  3239. *
  3240. * Optional: targets are not required to implement this function.
  3241. */
  3242. const char *
  3243. remote_target::extra_thread_info (thread_info *tp)
  3244. {
  3245. struct remote_state *rs = get_remote_state ();
  3246. int set;
  3247. threadref id;
  3248. struct gdb_ext_thread_info threadinfo;
  3249. if (rs->remote_desc == 0) /* paranoia */
  3250. internal_error (__FILE__, __LINE__,
  3251. _("remote_threads_extra_info"));
  3252. if (tp->ptid == magic_null_ptid
  3253. || (tp->ptid.pid () != 0 && tp->ptid.lwp () == 0))
  3254. /* This is the main thread which was added by GDB. The remote
  3255. server doesn't know about it. */
  3256. return NULL;
  3257. std::string &extra = get_remote_thread_info (tp)->extra;
  3258. /* If already have cached info, use it. */
  3259. if (!extra.empty ())
  3260. return extra.c_str ();
  3261. if (packet_support (PACKET_qXfer_threads) == PACKET_ENABLE)
  3262. {
  3263. /* If we're using qXfer:threads:read, then the extra info is
  3264. included in the XML. So if we didn't have anything cached,
  3265. it's because there's really no extra info. */
  3266. return NULL;
  3267. }
  3268. if (rs->use_threadextra_query)
  3269. {
  3270. char *b = rs->buf.data ();
  3271. char *endb = b + get_remote_packet_size ();
  3272. xsnprintf (b, endb - b, "qThreadExtraInfo,");
  3273. b += strlen (b);
  3274. write_ptid (b, endb, tp->ptid);
  3275. putpkt (rs->buf);
  3276. getpkt (&rs->buf, 0);
  3277. if (rs->buf[0] != 0)
  3278. {
  3279. extra.resize (strlen (rs->buf.data ()) / 2);
  3280. hex2bin (rs->buf.data (), (gdb_byte *) &extra[0], extra.size ());
  3281. return extra.c_str ();
  3282. }
  3283. }
  3284. /* If the above query fails, fall back to the old method. */
  3285. rs->use_threadextra_query = 0;
  3286. set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
  3287. | TAG_MOREDISPLAY | TAG_DISPLAY;
  3288. int_to_threadref (&id, tp->ptid.lwp ());
  3289. if (remote_get_threadinfo (&id, set, &threadinfo))
  3290. if (threadinfo.active)
  3291. {
  3292. if (*threadinfo.shortname)
  3293. string_appendf (extra, " Name: %s", threadinfo.shortname);
  3294. if (*threadinfo.display)
  3295. {
  3296. if (!extra.empty ())
  3297. extra += ',';
  3298. string_appendf (extra, " State: %s", threadinfo.display);
  3299. }
  3300. if (*threadinfo.more_display)
  3301. {
  3302. if (!extra.empty ())
  3303. extra += ',';
  3304. string_appendf (extra, " Priority: %s", threadinfo.more_display);
  3305. }
  3306. return extra.c_str ();
  3307. }
  3308. return NULL;
  3309. }
  3310. bool
  3311. remote_target::static_tracepoint_marker_at (CORE_ADDR addr,
  3312. struct static_tracepoint_marker *marker)
  3313. {
  3314. struct remote_state *rs = get_remote_state ();
  3315. char *p = rs->buf.data ();
  3316. xsnprintf (p, get_remote_packet_size (), "qTSTMat:");
  3317. p += strlen (p);
  3318. p += hexnumstr (p, addr);
  3319. putpkt (rs->buf);
  3320. getpkt (&rs->buf, 0);
  3321. p = rs->buf.data ();
  3322. if (*p == 'E')
  3323. error (_("Remote failure reply: %s"), p);
  3324. if (*p++ == 'm')
  3325. {
  3326. parse_static_tracepoint_marker_definition (p, NULL, marker);
  3327. return true;
  3328. }
  3329. return false;
  3330. }
  3331. std::vector<static_tracepoint_marker>
  3332. remote_target::static_tracepoint_markers_by_strid (const char *strid)
  3333. {
  3334. struct remote_state *rs = get_remote_state ();
  3335. std::vector<static_tracepoint_marker> markers;
  3336. const char *p;
  3337. static_tracepoint_marker marker;
  3338. /* Ask for a first packet of static tracepoint marker
  3339. definition. */
  3340. putpkt ("qTfSTM");
  3341. getpkt (&rs->buf, 0);
  3342. p = rs->buf.data ();
  3343. if (*p == 'E')
  3344. error (_("Remote failure reply: %s"), p);
  3345. while (*p++ == 'm')
  3346. {
  3347. do
  3348. {
  3349. parse_static_tracepoint_marker_definition (p, &p, &marker);
  3350. if (strid == NULL || marker.str_id == strid)
  3351. markers.push_back (std::move (marker));
  3352. }
  3353. while (*p++ == ','); /* comma-separated list */
  3354. /* Ask for another packet of static tracepoint definition. */
  3355. putpkt ("qTsSTM");
  3356. getpkt (&rs->buf, 0);
  3357. p = rs->buf.data ();
  3358. }
  3359. return markers;
  3360. }
  3361. /* Implement the to_get_ada_task_ptid function for the remote targets. */
  3362. ptid_t
  3363. remote_target::get_ada_task_ptid (long lwp, ULONGEST thread)
  3364. {
  3365. return ptid_t (inferior_ptid.pid (), lwp);
  3366. }
  3367. /* Restart the remote side; this is an extended protocol operation. */
  3368. void
  3369. remote_target::extended_remote_restart ()
  3370. {
  3371. struct remote_state *rs = get_remote_state ();
  3372. /* Send the restart command; for reasons I don't understand the
  3373. remote side really expects a number after the "R". */
  3374. xsnprintf (rs->buf.data (), get_remote_packet_size (), "R%x", 0);
  3375. putpkt (rs->buf);
  3376. remote_fileio_reset ();
  3377. }
  3378. /* Clean up connection to a remote debugger. */
  3379. void
  3380. remote_target::close ()
  3381. {
  3382. /* Make sure we leave stdin registered in the event loop. */
  3383. terminal_ours ();
  3384. trace_reset_local_state ();
  3385. delete this;
  3386. }
  3387. remote_target::~remote_target ()
  3388. {
  3389. struct remote_state *rs = get_remote_state ();
  3390. /* Check for NULL because we may get here with a partially
  3391. constructed target/connection. */
  3392. if (rs->remote_desc == nullptr)
  3393. return;
  3394. serial_close (rs->remote_desc);
  3395. /* We are destroying the remote target, so we should discard
  3396. everything of this target. */
  3397. discard_pending_stop_replies_in_queue ();
  3398. if (rs->remote_async_inferior_event_token)
  3399. delete_async_event_handler (&rs->remote_async_inferior_event_token);
  3400. delete rs->notif_state;
  3401. }
  3402. /* Query the remote side for the text, data and bss offsets. */
  3403. void
  3404. remote_target::get_offsets ()
  3405. {
  3406. struct remote_state *rs = get_remote_state ();
  3407. char *buf;
  3408. char *ptr;
  3409. int lose, num_segments = 0, do_sections, do_segments;
  3410. CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
  3411. if (current_program_space->symfile_object_file == NULL)
  3412. return;
  3413. putpkt ("qOffsets");
  3414. getpkt (&rs->buf, 0);
  3415. buf = rs->buf.data ();
  3416. if (buf[0] == '\000')
  3417. return; /* Return silently. Stub doesn't support
  3418. this command. */
  3419. if (buf[0] == 'E')
  3420. {
  3421. warning (_("Remote failure reply: %s"), buf);
  3422. return;
  3423. }
  3424. /* Pick up each field in turn. This used to be done with scanf, but
  3425. scanf will make trouble if CORE_ADDR size doesn't match
  3426. conversion directives correctly. The following code will work
  3427. with any size of CORE_ADDR. */
  3428. text_addr = data_addr = bss_addr = 0;
  3429. ptr = buf;
  3430. lose = 0;
  3431. if (startswith (ptr, "Text="))
  3432. {
  3433. ptr += 5;
  3434. /* Don't use strtol, could lose on big values. */
  3435. while (*ptr && *ptr != ';')
  3436. text_addr = (text_addr << 4) + fromhex (*ptr++);
  3437. if (startswith (ptr, ";Data="))
  3438. {
  3439. ptr += 6;
  3440. while (*ptr && *ptr != ';')
  3441. data_addr = (data_addr << 4) + fromhex (*ptr++);
  3442. }
  3443. else
  3444. lose = 1;
  3445. if (!lose && startswith (ptr, ";Bss="))
  3446. {
  3447. ptr += 5;
  3448. while (*ptr && *ptr != ';')
  3449. bss_addr = (bss_addr << 4) + fromhex (*ptr++);
  3450. if (bss_addr != data_addr)
  3451. warning (_("Target reported unsupported offsets: %s"), buf);
  3452. }
  3453. else
  3454. lose = 1;
  3455. }
  3456. else if (startswith (ptr, "TextSeg="))
  3457. {
  3458. ptr += 8;
  3459. /* Don't use strtol, could lose on big values. */
  3460. while (*ptr && *ptr != ';')
  3461. text_addr = (text_addr << 4) + fromhex (*ptr++);
  3462. num_segments = 1;
  3463. if (startswith (ptr, ";DataSeg="))
  3464. {
  3465. ptr += 9;
  3466. while (*ptr && *ptr != ';')
  3467. data_addr = (data_addr << 4) + fromhex (*ptr++);
  3468. num_segments++;
  3469. }
  3470. }
  3471. else
  3472. lose = 1;
  3473. if (lose)
  3474. error (_("Malformed response to offset query, %s"), buf);
  3475. else if (*ptr != '\0')
  3476. warning (_("Target reported unsupported offsets: %s"), buf);
  3477. objfile *objf = current_program_space->symfile_object_file;
  3478. section_offsets offs = objf->section_offsets;
  3479. symfile_segment_data_up data = get_symfile_segment_data (objf->obfd);
  3480. do_segments = (data != NULL);
  3481. do_sections = num_segments == 0;
  3482. if (num_segments > 0)
  3483. {
  3484. segments[0] = text_addr;
  3485. segments[1] = data_addr;
  3486. }
  3487. /* If we have two segments, we can still try to relocate everything
  3488. by assuming that the .text and .data offsets apply to the whole
  3489. text and data segments. Convert the offsets given in the packet
  3490. to base addresses for symfile_map_offsets_to_segments. */
  3491. else if (data != nullptr && data->segments.size () == 2)
  3492. {
  3493. segments[0] = data->segments[0].base + text_addr;
  3494. segments[1] = data->segments[1].base + data_addr;
  3495. num_segments = 2;
  3496. }
  3497. /* If the object file has only one segment, assume that it is text
  3498. rather than data; main programs with no writable data are rare,
  3499. but programs with no code are useless. Of course the code might
  3500. have ended up in the data segment... to detect that we would need
  3501. the permissions here. */
  3502. else if (data && data->segments.size () == 1)
  3503. {
  3504. segments[0] = data->segments[0].base + text_addr;
  3505. num_segments = 1;
  3506. }
  3507. /* There's no way to relocate by segment. */
  3508. else
  3509. do_segments = 0;
  3510. if (do_segments)
  3511. {
  3512. int ret = symfile_map_offsets_to_segments (objf->obfd,
  3513. data.get (), offs,
  3514. num_segments, segments);
  3515. if (ret == 0 && !do_sections)
  3516. error (_("Can not handle qOffsets TextSeg "
  3517. "response with this symbol file"));
  3518. if (ret > 0)
  3519. do_sections = 0;
  3520. }
  3521. if (do_sections)
  3522. {
  3523. offs[SECT_OFF_TEXT (objf)] = text_addr;
  3524. /* This is a temporary kludge to force data and bss to use the
  3525. same offsets because that's what nlmconv does now. The real
  3526. solution requires changes to the stub and remote.c that I
  3527. don't have time to do right now. */
  3528. offs[SECT_OFF_DATA (objf)] = data_addr;
  3529. offs[SECT_OFF_BSS (objf)] = data_addr;
  3530. }
  3531. objfile_relocate (objf, offs);
  3532. }
  3533. /* Send interrupt_sequence to remote target. */
  3534. void
  3535. remote_target::send_interrupt_sequence ()
  3536. {
  3537. struct remote_state *rs = get_remote_state ();
  3538. if (interrupt_sequence_mode == interrupt_sequence_control_c)
  3539. remote_serial_write ("\x03", 1);
  3540. else if (interrupt_sequence_mode == interrupt_sequence_break)
  3541. serial_send_break (rs->remote_desc);
  3542. else if (interrupt_sequence_mode == interrupt_sequence_break_g)
  3543. {
  3544. serial_send_break (rs->remote_desc);
  3545. remote_serial_write ("g", 1);
  3546. }
  3547. else
  3548. internal_error (__FILE__, __LINE__,
  3549. _("Invalid value for interrupt_sequence_mode: %s."),
  3550. interrupt_sequence_mode);
  3551. }
  3552. /* If STOP_REPLY is a T stop reply, look for the "thread" register,
  3553. and extract the PTID. Returns NULL_PTID if not found. */
  3554. static ptid_t
  3555. stop_reply_extract_thread (const char *stop_reply)
  3556. {
  3557. if (stop_reply[0] == 'T' && strlen (stop_reply) > 3)
  3558. {
  3559. const char *p;
  3560. /* Txx r:val ; r:val (...) */
  3561. p = &stop_reply[3];
  3562. /* Look for "register" named "thread". */
  3563. while (*p != '\0')
  3564. {
  3565. const char *p1;
  3566. p1 = strchr (p, ':');
  3567. if (p1 == NULL)
  3568. return null_ptid;
  3569. if (strncmp (p, "thread", p1 - p) == 0)
  3570. return read_ptid (++p1, &p);
  3571. p1 = strchr (p, ';');
  3572. if (p1 == NULL)
  3573. return null_ptid;
  3574. p1++;
  3575. p = p1;
  3576. }
  3577. }
  3578. return null_ptid;
  3579. }
  3580. /* Determine the remote side's current thread. If we have a stop
  3581. reply handy (in WAIT_STATUS), maybe it's a T stop reply with a
  3582. "thread" register we can extract the current thread from. If not,
  3583. ask the remote which is the current thread with qC. The former
  3584. method avoids a roundtrip. */
  3585. ptid_t
  3586. remote_target::get_current_thread (const char *wait_status)
  3587. {
  3588. ptid_t ptid = null_ptid;
  3589. /* Note we don't use remote_parse_stop_reply as that makes use of
  3590. the target architecture, which we haven't yet fully determined at
  3591. this point. */
  3592. if (wait_status != NULL)
  3593. ptid = stop_reply_extract_thread (wait_status);
  3594. if (ptid == null_ptid)
  3595. ptid = remote_current_thread (inferior_ptid);
  3596. return ptid;
  3597. }
  3598. /* Query the remote target for which is the current thread/process,
  3599. add it to our tables, and update INFERIOR_PTID. The caller is
  3600. responsible for setting the state such that the remote end is ready
  3601. to return the current thread.
  3602. This function is called after handling the '?' or 'vRun' packets,
  3603. whose response is a stop reply from which we can also try
  3604. extracting the thread. If the target doesn't support the explicit
  3605. qC query, we infer the current thread from that stop reply, passed
  3606. in in WAIT_STATUS, which may be NULL.
  3607. The function returns pointer to the main thread of the inferior. */
  3608. thread_info *
  3609. remote_target::add_current_inferior_and_thread (const char *wait_status)
  3610. {
  3611. struct remote_state *rs = get_remote_state ();
  3612. bool fake_pid_p = false;
  3613. switch_to_no_thread ();
  3614. /* Now, if we have thread information, update the current thread's
  3615. ptid. */
  3616. ptid_t curr_ptid = get_current_thread (wait_status);
  3617. if (curr_ptid != null_ptid)
  3618. {
  3619. if (!remote_multi_process_p (rs))
  3620. fake_pid_p = true;
  3621. }
  3622. else
  3623. {
  3624. /* Without this, some commands which require an active target
  3625. (such as kill) won't work. This variable serves (at least)
  3626. double duty as both the pid of the target process (if it has
  3627. such), and as a flag indicating that a target is active. */
  3628. curr_ptid = magic_null_ptid;
  3629. fake_pid_p = true;
  3630. }
  3631. remote_add_inferior (fake_pid_p, curr_ptid.pid (), -1, 1);
  3632. /* Add the main thread and switch to it. Don't try reading
  3633. registers yet, since we haven't fetched the target description
  3634. yet. */
  3635. thread_info *tp = add_thread_silent (this, curr_ptid);
  3636. switch_to_thread_no_regs (tp);
  3637. return tp;
  3638. }
  3639. /* Print info about a thread that was found already stopped on
  3640. connection. */
  3641. void
  3642. remote_target::print_one_stopped_thread (thread_info *thread)
  3643. {
  3644. target_waitstatus ws;
  3645. /* If there is a pending waitstatus, use it. If there isn't it's because
  3646. the thread's stop was reported with TARGET_WAITKIND_STOPPED / GDB_SIGNAL_0
  3647. and process_initial_stop_replies decided it wasn't interesting to save
  3648. and report to the core. */
  3649. if (thread->has_pending_waitstatus ())
  3650. {
  3651. ws = thread->pending_waitstatus ();
  3652. thread->clear_pending_waitstatus ();
  3653. }
  3654. else
  3655. {
  3656. ws.set_stopped (GDB_SIGNAL_0);
  3657. }
  3658. switch_to_thread (thread);
  3659. thread->set_stop_pc (get_frame_pc (get_current_frame ()));
  3660. set_current_sal_from_frame (get_current_frame ());
  3661. /* For "info program". */
  3662. set_last_target_status (this, thread->ptid, ws);
  3663. if (ws.kind () == TARGET_WAITKIND_STOPPED)
  3664. {
  3665. enum gdb_signal sig = ws.sig ();
  3666. if (signal_print_state (sig))
  3667. gdb::observers::signal_received.notify (sig);
  3668. }
  3669. gdb::observers::normal_stop.notify (NULL, 1);
  3670. }
  3671. /* Process all initial stop replies the remote side sent in response
  3672. to the ? packet. These indicate threads that were already stopped
  3673. on initial connection. We mark these threads as stopped and print
  3674. their current frame before giving the user the prompt. */
  3675. void
  3676. remote_target::process_initial_stop_replies (int from_tty)
  3677. {
  3678. int pending_stop_replies = stop_reply_queue_length ();
  3679. struct thread_info *selected = NULL;
  3680. struct thread_info *lowest_stopped = NULL;
  3681. struct thread_info *first = NULL;
  3682. /* This is only used when the target is non-stop. */
  3683. gdb_assert (target_is_non_stop_p ());
  3684. /* Consume the initial pending events. */
  3685. while (pending_stop_replies-- > 0)
  3686. {
  3687. ptid_t waiton_ptid = minus_one_ptid;
  3688. ptid_t event_ptid;
  3689. struct target_waitstatus ws;
  3690. int ignore_event = 0;
  3691. event_ptid = target_wait (waiton_ptid, &ws, TARGET_WNOHANG);
  3692. if (remote_debug)
  3693. print_target_wait_results (waiton_ptid, event_ptid, ws);
  3694. switch (ws.kind ())
  3695. {
  3696. case TARGET_WAITKIND_IGNORE:
  3697. case TARGET_WAITKIND_NO_RESUMED:
  3698. case TARGET_WAITKIND_SIGNALLED:
  3699. case TARGET_WAITKIND_EXITED:
  3700. /* We shouldn't see these, but if we do, just ignore. */
  3701. remote_debug_printf ("event ignored");
  3702. ignore_event = 1;
  3703. break;
  3704. default:
  3705. break;
  3706. }
  3707. if (ignore_event)
  3708. continue;
  3709. thread_info *evthread = find_thread_ptid (this, event_ptid);
  3710. if (ws.kind () == TARGET_WAITKIND_STOPPED)
  3711. {
  3712. enum gdb_signal sig = ws.sig ();
  3713. /* Stubs traditionally report SIGTRAP as initial signal,
  3714. instead of signal 0. Suppress it. */
  3715. if (sig == GDB_SIGNAL_TRAP)
  3716. sig = GDB_SIGNAL_0;
  3717. evthread->set_stop_signal (sig);
  3718. ws.set_stopped (sig);
  3719. }
  3720. if (ws.kind () != TARGET_WAITKIND_STOPPED
  3721. || ws.sig () != GDB_SIGNAL_0)
  3722. evthread->set_pending_waitstatus (ws);
  3723. set_executing (this, event_ptid, false);
  3724. set_running (this, event_ptid, false);
  3725. get_remote_thread_info (evthread)->set_not_resumed ();
  3726. }
  3727. /* "Notice" the new inferiors before anything related to
  3728. registers/memory. */
  3729. for (inferior *inf : all_non_exited_inferiors (this))
  3730. {
  3731. inf->needs_setup = 1;
  3732. if (non_stop)
  3733. {
  3734. thread_info *thread = any_live_thread_of_inferior (inf);
  3735. notice_new_inferior (thread, thread->state == THREAD_RUNNING,
  3736. from_tty);
  3737. }
  3738. }
  3739. /* If all-stop on top of non-stop, pause all threads. Note this
  3740. records the threads' stop pc, so must be done after "noticing"
  3741. the inferiors. */
  3742. if (!non_stop)
  3743. {
  3744. {
  3745. /* At this point, the remote target is not async. It needs to be for
  3746. the poll in stop_all_threads to consider events from it, so enable
  3747. it temporarily. */
  3748. gdb_assert (!this->is_async_p ());
  3749. SCOPE_EXIT { target_async (0); };
  3750. target_async (1);
  3751. stop_all_threads ("remote connect in all-stop");
  3752. }
  3753. /* If all threads of an inferior were already stopped, we
  3754. haven't setup the inferior yet. */
  3755. for (inferior *inf : all_non_exited_inferiors (this))
  3756. {
  3757. if (inf->needs_setup)
  3758. {
  3759. thread_info *thread = any_live_thread_of_inferior (inf);
  3760. switch_to_thread_no_regs (thread);
  3761. setup_inferior (0);
  3762. }
  3763. }
  3764. }
  3765. /* Now go over all threads that are stopped, and print their current
  3766. frame. If all-stop, then if there's a signalled thread, pick
  3767. that as current. */
  3768. for (thread_info *thread : all_non_exited_threads (this))
  3769. {
  3770. if (first == NULL)
  3771. first = thread;
  3772. if (!non_stop)
  3773. thread->set_running (false);
  3774. else if (thread->state != THREAD_STOPPED)
  3775. continue;
  3776. if (selected == nullptr && thread->has_pending_waitstatus ())
  3777. selected = thread;
  3778. if (lowest_stopped == NULL
  3779. || thread->inf->num < lowest_stopped->inf->num
  3780. || thread->per_inf_num < lowest_stopped->per_inf_num)
  3781. lowest_stopped = thread;
  3782. if (non_stop)
  3783. print_one_stopped_thread (thread);
  3784. }
  3785. /* In all-stop, we only print the status of one thread, and leave
  3786. others with their status pending. */
  3787. if (!non_stop)
  3788. {
  3789. thread_info *thread = selected;
  3790. if (thread == NULL)
  3791. thread = lowest_stopped;
  3792. if (thread == NULL)
  3793. thread = first;
  3794. print_one_stopped_thread (thread);
  3795. }
  3796. }
  3797. /* Mark a remote_target as marking (by setting the starting_up flag within
  3798. its remote_state) for the lifetime of this object. The reference count
  3799. on the remote target is temporarily incremented, to prevent the target
  3800. being deleted under our feet. */
  3801. struct scoped_mark_target_starting
  3802. {
  3803. /* Constructor, TARGET is the target to be marked as starting, its
  3804. reference count will be incremented. */
  3805. scoped_mark_target_starting (remote_target *target)
  3806. : m_remote_target (target)
  3807. {
  3808. m_remote_target->incref ();
  3809. remote_state *rs = m_remote_target->get_remote_state ();
  3810. rs->starting_up = true;
  3811. }
  3812. /* Destructor, mark the target being worked on as no longer starting, and
  3813. decrement the reference count. */
  3814. ~scoped_mark_target_starting ()
  3815. {
  3816. remote_state *rs = m_remote_target->get_remote_state ();
  3817. rs->starting_up = false;
  3818. decref_target (m_remote_target);
  3819. }
  3820. private:
  3821. /* The target on which we are operating. */
  3822. remote_target *m_remote_target;
  3823. };
  3824. /* Helper for remote_target::start_remote, start the remote connection and
  3825. sync state. Return true if everything goes OK, otherwise, return false.
  3826. This function exists so that the scoped_restore created within it will
  3827. expire before we return to remote_target::start_remote. */
  3828. bool
  3829. remote_target::start_remote_1 (int from_tty, int extended_p)
  3830. {
  3831. REMOTE_SCOPED_DEBUG_ENTER_EXIT;
  3832. struct remote_state *rs = get_remote_state ();
  3833. struct packet_config *noack_config;
  3834. /* Signal other parts that we're going through the initial setup,
  3835. and so things may not be stable yet. E.g., we don't try to
  3836. install tracepoints until we've relocated symbols. Also, a
  3837. Ctrl-C before we're connected and synced up can't interrupt the
  3838. target. Instead, it offers to drop the (potentially wedged)
  3839. connection. */
  3840. scoped_mark_target_starting target_is_starting (this);
  3841. QUIT;
  3842. if (interrupt_on_connect)
  3843. send_interrupt_sequence ();
  3844. /* Ack any packet which the remote side has already sent. */
  3845. remote_serial_write ("+", 1);
  3846. /* The first packet we send to the target is the optional "supported
  3847. packets" request. If the target can answer this, it will tell us
  3848. which later probes to skip. */
  3849. remote_query_supported ();
  3850. /* If the stub wants to get a QAllow, compose one and send it. */
  3851. if (packet_support (PACKET_QAllow) != PACKET_DISABLE)
  3852. set_permissions ();
  3853. /* gdbserver < 7.7 (before its fix from 2013-12-11) did reply to any
  3854. unknown 'v' packet with string "OK". "OK" gets interpreted by GDB
  3855. as a reply to known packet. For packet "vFile:setfs:" it is an
  3856. invalid reply and GDB would return error in
  3857. remote_hostio_set_filesystem, making remote files access impossible.
  3858. Disable "vFile:setfs:" in such case. Do not disable other 'v' packets as
  3859. other "vFile" packets get correctly detected even on gdbserver < 7.7. */
  3860. {
  3861. const char v_mustreplyempty[] = "vMustReplyEmpty";
  3862. putpkt (v_mustreplyempty);
  3863. getpkt (&rs->buf, 0);
  3864. if (strcmp (rs->buf.data (), "OK") == 0)
  3865. remote_protocol_packets[PACKET_vFile_setfs].support = PACKET_DISABLE;
  3866. else if (strcmp (rs->buf.data (), "") != 0)
  3867. error (_("Remote replied unexpectedly to '%s': %s"), v_mustreplyempty,
  3868. rs->buf.data ());
  3869. }
  3870. /* Next, we possibly activate noack mode.
  3871. If the QStartNoAckMode packet configuration is set to AUTO,
  3872. enable noack mode if the stub reported a wish for it with
  3873. qSupported.
  3874. If set to TRUE, then enable noack mode even if the stub didn't
  3875. report it in qSupported. If the stub doesn't reply OK, the
  3876. session ends with an error.
  3877. If FALSE, then don't activate noack mode, regardless of what the
  3878. stub claimed should be the default with qSupported. */
  3879. noack_config = &remote_protocol_packets[PACKET_QStartNoAckMode];
  3880. if (packet_config_support (noack_config) != PACKET_DISABLE)
  3881. {
  3882. putpkt ("QStartNoAckMode");
  3883. getpkt (&rs->buf, 0);
  3884. if (packet_ok (rs->buf, noack_config) == PACKET_OK)
  3885. rs->noack_mode = 1;
  3886. }
  3887. if (extended_p)
  3888. {
  3889. /* Tell the remote that we are using the extended protocol. */
  3890. putpkt ("!");
  3891. getpkt (&rs->buf, 0);
  3892. }
  3893. /* Let the target know which signals it is allowed to pass down to
  3894. the program. */
  3895. update_signals_program_target ();
  3896. /* Next, if the target can specify a description, read it. We do
  3897. this before anything involving memory or registers. */
  3898. target_find_description ();
  3899. /* Next, now that we know something about the target, update the
  3900. address spaces in the program spaces. */
  3901. update_address_spaces ();
  3902. /* On OSs where the list of libraries is global to all
  3903. processes, we fetch them early. */
  3904. if (gdbarch_has_global_solist (target_gdbarch ()))
  3905. solib_add (NULL, from_tty, auto_solib_add);
  3906. if (target_is_non_stop_p ())
  3907. {
  3908. if (packet_support (PACKET_QNonStop) != PACKET_ENABLE)
  3909. error (_("Non-stop mode requested, but remote "
  3910. "does not support non-stop"));
  3911. putpkt ("QNonStop:1");
  3912. getpkt (&rs->buf, 0);
  3913. if (strcmp (rs->buf.data (), "OK") != 0)
  3914. error (_("Remote refused setting non-stop mode with: %s"),
  3915. rs->buf.data ());
  3916. /* Find about threads and processes the stub is already
  3917. controlling. We default to adding them in the running state.
  3918. The '?' query below will then tell us about which threads are
  3919. stopped. */
  3920. this->update_thread_list ();
  3921. }
  3922. else if (packet_support (PACKET_QNonStop) == PACKET_ENABLE)
  3923. {
  3924. /* Don't assume that the stub can operate in all-stop mode.
  3925. Request it explicitly. */
  3926. putpkt ("QNonStop:0");
  3927. getpkt (&rs->buf, 0);
  3928. if (strcmp (rs->buf.data (), "OK") != 0)
  3929. error (_("Remote refused setting all-stop mode with: %s"),
  3930. rs->buf.data ());
  3931. }
  3932. /* Upload TSVs regardless of whether the target is running or not. The
  3933. remote stub, such as GDBserver, may have some predefined or builtin
  3934. TSVs, even if the target is not running. */
  3935. if (get_trace_status (current_trace_status ()) != -1)
  3936. {
  3937. struct uploaded_tsv *uploaded_tsvs = NULL;
  3938. upload_trace_state_variables (&uploaded_tsvs);
  3939. merge_uploaded_trace_state_variables (&uploaded_tsvs);
  3940. }
  3941. /* Check whether the target is running now. */
  3942. putpkt ("?");
  3943. getpkt (&rs->buf, 0);
  3944. if (!target_is_non_stop_p ())
  3945. {
  3946. char *wait_status = NULL;
  3947. if (rs->buf[0] == 'W' || rs->buf[0] == 'X')
  3948. {
  3949. if (!extended_p)
  3950. error (_("The target is not running (try extended-remote?)"));
  3951. return false;
  3952. }
  3953. else
  3954. {
  3955. /* Save the reply for later. */
  3956. wait_status = (char *) alloca (strlen (rs->buf.data ()) + 1);
  3957. strcpy (wait_status, rs->buf.data ());
  3958. }
  3959. /* Fetch thread list. */
  3960. target_update_thread_list ();
  3961. /* Let the stub know that we want it to return the thread. */
  3962. set_continue_thread (minus_one_ptid);
  3963. if (thread_count (this) == 0)
  3964. {
  3965. /* Target has no concept of threads at all. GDB treats
  3966. non-threaded target as single-threaded; add a main
  3967. thread. */
  3968. thread_info *tp = add_current_inferior_and_thread (wait_status);
  3969. get_remote_thread_info (tp)->set_resumed ();
  3970. }
  3971. else
  3972. {
  3973. /* We have thread information; select the thread the target
  3974. says should be current. If we're reconnecting to a
  3975. multi-threaded program, this will ideally be the thread
  3976. that last reported an event before GDB disconnected. */
  3977. ptid_t curr_thread = get_current_thread (wait_status);
  3978. if (curr_thread == null_ptid)
  3979. {
  3980. /* Odd... The target was able to list threads, but not
  3981. tell us which thread was current (no "thread"
  3982. register in T stop reply?). Just pick the first
  3983. thread in the thread list then. */
  3984. remote_debug_printf ("warning: couldn't determine remote "
  3985. "current thread; picking first in list.");
  3986. for (thread_info *tp : all_non_exited_threads (this,
  3987. minus_one_ptid))
  3988. {
  3989. switch_to_thread (tp);
  3990. break;
  3991. }
  3992. }
  3993. else
  3994. switch_to_thread (find_thread_ptid (this, curr_thread));
  3995. }
  3996. /* init_wait_for_inferior should be called before get_offsets in order
  3997. to manage `inserted' flag in bp loc in a correct state.
  3998. breakpoint_init_inferior, called from init_wait_for_inferior, set
  3999. `inserted' flag to 0, while before breakpoint_re_set, called from
  4000. start_remote, set `inserted' flag to 1. In the initialization of
  4001. inferior, breakpoint_init_inferior should be called first, and then
  4002. breakpoint_re_set can be called. If this order is broken, state of
  4003. `inserted' flag is wrong, and cause some problems on breakpoint
  4004. manipulation. */
  4005. init_wait_for_inferior ();
  4006. get_offsets (); /* Get text, data & bss offsets. */
  4007. /* If we could not find a description using qXfer, and we know
  4008. how to do it some other way, try again. This is not
  4009. supported for non-stop; it could be, but it is tricky if
  4010. there are no stopped threads when we connect. */
  4011. if (remote_read_description_p (this)
  4012. && gdbarch_target_desc (target_gdbarch ()) == NULL)
  4013. {
  4014. target_clear_description ();
  4015. target_find_description ();
  4016. }
  4017. /* Use the previously fetched status. */
  4018. gdb_assert (wait_status != NULL);
  4019. struct notif_event *reply
  4020. = remote_notif_parse (this, &notif_client_stop, wait_status);
  4021. push_stop_reply ((struct stop_reply *) reply);
  4022. ::start_remote (from_tty); /* Initialize gdb process mechanisms. */
  4023. }
  4024. else
  4025. {
  4026. /* Clear WFI global state. Do this before finding about new
  4027. threads and inferiors, and setting the current inferior.
  4028. Otherwise we would clear the proceed status of the current
  4029. inferior when we want its stop_soon state to be preserved
  4030. (see notice_new_inferior). */
  4031. init_wait_for_inferior ();
  4032. /* In non-stop, we will either get an "OK", meaning that there
  4033. are no stopped threads at this time; or, a regular stop
  4034. reply. In the latter case, there may be more than one thread
  4035. stopped --- we pull them all out using the vStopped
  4036. mechanism. */
  4037. if (strcmp (rs->buf.data (), "OK") != 0)
  4038. {
  4039. struct notif_client *notif = &notif_client_stop;
  4040. /* remote_notif_get_pending_replies acks this one, and gets
  4041. the rest out. */
  4042. rs->notif_state->pending_event[notif_client_stop.id]
  4043. = remote_notif_parse (this, notif, rs->buf.data ());
  4044. remote_notif_get_pending_events (notif);
  4045. }
  4046. if (thread_count (this) == 0)
  4047. {
  4048. if (!extended_p)
  4049. error (_("The target is not running (try extended-remote?)"));
  4050. return false;
  4051. }
  4052. /* Report all signals during attach/startup. */
  4053. pass_signals ({});
  4054. /* If there are already stopped threads, mark them stopped and
  4055. report their stops before giving the prompt to the user. */
  4056. process_initial_stop_replies (from_tty);
  4057. if (target_can_async_p ())
  4058. target_async (1);
  4059. }
  4060. /* If we connected to a live target, do some additional setup. */
  4061. if (target_has_execution ())
  4062. {
  4063. /* No use without a symbol-file. */
  4064. if (current_program_space->symfile_object_file)
  4065. remote_check_symbols ();
  4066. }
  4067. /* Possibly the target has been engaged in a trace run started
  4068. previously; find out where things are at. */
  4069. if (get_trace_status (current_trace_status ()) != -1)
  4070. {
  4071. struct uploaded_tp *uploaded_tps = NULL;
  4072. if (current_trace_status ()->running)
  4073. gdb_printf (_("Trace is already running on the target.\n"));
  4074. upload_tracepoints (&uploaded_tps);
  4075. merge_uploaded_tracepoints (&uploaded_tps);
  4076. }
  4077. /* Possibly the target has been engaged in a btrace record started
  4078. previously; find out where things are at. */
  4079. remote_btrace_maybe_reopen ();
  4080. return true;
  4081. }
  4082. /* Start the remote connection and sync state. */
  4083. void
  4084. remote_target::start_remote (int from_tty, int extended_p)
  4085. {
  4086. if (start_remote_1 (from_tty, extended_p)
  4087. && breakpoints_should_be_inserted_now ())
  4088. insert_breakpoints ();
  4089. }
  4090. const char *
  4091. remote_target::connection_string ()
  4092. {
  4093. remote_state *rs = get_remote_state ();
  4094. if (rs->remote_desc->name != NULL)
  4095. return rs->remote_desc->name;
  4096. else
  4097. return NULL;
  4098. }
  4099. /* Open a connection to a remote debugger.
  4100. NAME is the filename used for communication. */
  4101. void
  4102. remote_target::open (const char *name, int from_tty)
  4103. {
  4104. open_1 (name, from_tty, 0);
  4105. }
  4106. /* Open a connection to a remote debugger using the extended
  4107. remote gdb protocol. NAME is the filename used for communication. */
  4108. void
  4109. extended_remote_target::open (const char *name, int from_tty)
  4110. {
  4111. open_1 (name, from_tty, 1 /*extended_p */);
  4112. }
  4113. /* Reset all packets back to "unknown support". Called when opening a
  4114. new connection to a remote target. */
  4115. static void
  4116. reset_all_packet_configs_support (void)
  4117. {
  4118. int i;
  4119. for (i = 0; i < PACKET_MAX; i++)
  4120. remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
  4121. }
  4122. /* Initialize all packet configs. */
  4123. static void
  4124. init_all_packet_configs (void)
  4125. {
  4126. int i;
  4127. for (i = 0; i < PACKET_MAX; i++)
  4128. {
  4129. remote_protocol_packets[i].detect = AUTO_BOOLEAN_AUTO;
  4130. remote_protocol_packets[i].support = PACKET_SUPPORT_UNKNOWN;
  4131. }
  4132. }
  4133. /* Symbol look-up. */
  4134. void
  4135. remote_target::remote_check_symbols ()
  4136. {
  4137. char *tmp;
  4138. int end;
  4139. /* The remote side has no concept of inferiors that aren't running
  4140. yet, it only knows about running processes. If we're connected
  4141. but our current inferior is not running, we should not invite the
  4142. remote target to request symbol lookups related to its
  4143. (unrelated) current process. */
  4144. if (!target_has_execution ())
  4145. return;
  4146. if (packet_support (PACKET_qSymbol) == PACKET_DISABLE)
  4147. return;
  4148. /* Make sure the remote is pointing at the right process. Note
  4149. there's no way to select "no process". */
  4150. set_general_process ();
  4151. /* Allocate a message buffer. We can't reuse the input buffer in RS,
  4152. because we need both at the same time. */
  4153. gdb::char_vector msg (get_remote_packet_size ());
  4154. gdb::char_vector reply (get_remote_packet_size ());
  4155. /* Invite target to request symbol lookups. */
  4156. putpkt ("qSymbol::");
  4157. getpkt (&reply, 0);
  4158. packet_ok (reply, &remote_protocol_packets[PACKET_qSymbol]);
  4159. while (startswith (reply.data (), "qSymbol:"))
  4160. {
  4161. struct bound_minimal_symbol sym;
  4162. tmp = &reply[8];
  4163. end = hex2bin (tmp, reinterpret_cast <gdb_byte *> (msg.data ()),
  4164. strlen (tmp) / 2);
  4165. msg[end] = '\0';
  4166. sym = lookup_minimal_symbol (msg.data (), NULL, NULL);
  4167. if (sym.minsym == NULL)
  4168. xsnprintf (msg.data (), get_remote_packet_size (), "qSymbol::%s",
  4169. &reply[8]);
  4170. else
  4171. {
  4172. int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
  4173. CORE_ADDR sym_addr = BMSYMBOL_VALUE_ADDRESS (sym);
  4174. /* If this is a function address, return the start of code
  4175. instead of any data function descriptor. */
  4176. sym_addr = gdbarch_convert_from_func_ptr_addr
  4177. (target_gdbarch (), sym_addr, current_inferior ()->top_target ());
  4178. xsnprintf (msg.data (), get_remote_packet_size (), "qSymbol:%s:%s",
  4179. phex_nz (sym_addr, addr_size), &reply[8]);
  4180. }
  4181. putpkt (msg.data ());
  4182. getpkt (&reply, 0);
  4183. }
  4184. }
  4185. static struct serial *
  4186. remote_serial_open (const char *name)
  4187. {
  4188. static int udp_warning = 0;
  4189. /* FIXME: Parsing NAME here is a hack. But we want to warn here instead
  4190. of in ser-tcp.c, because it is the remote protocol assuming that the
  4191. serial connection is reliable and not the serial connection promising
  4192. to be. */
  4193. if (!udp_warning && startswith (name, "udp:"))
  4194. {
  4195. warning (_("The remote protocol may be unreliable over UDP.\n"
  4196. "Some events may be lost, rendering further debugging "
  4197. "impossible."));
  4198. udp_warning = 1;
  4199. }
  4200. return serial_open (name);
  4201. }
  4202. /* Inform the target of our permission settings. The permission flags
  4203. work without this, but if the target knows the settings, it can do
  4204. a couple things. First, it can add its own check, to catch cases
  4205. that somehow manage to get by the permissions checks in target
  4206. methods. Second, if the target is wired to disallow particular
  4207. settings (for instance, a system in the field that is not set up to
  4208. be able to stop at a breakpoint), it can object to any unavailable
  4209. permissions. */
  4210. void
  4211. remote_target::set_permissions ()
  4212. {
  4213. struct remote_state *rs = get_remote_state ();
  4214. xsnprintf (rs->buf.data (), get_remote_packet_size (), "QAllow:"
  4215. "WriteReg:%x;WriteMem:%x;"
  4216. "InsertBreak:%x;InsertTrace:%x;"
  4217. "InsertFastTrace:%x;Stop:%x",
  4218. may_write_registers, may_write_memory,
  4219. may_insert_breakpoints, may_insert_tracepoints,
  4220. may_insert_fast_tracepoints, may_stop);
  4221. putpkt (rs->buf);
  4222. getpkt (&rs->buf, 0);
  4223. /* If the target didn't like the packet, warn the user. Do not try
  4224. to undo the user's settings, that would just be maddening. */
  4225. if (strcmp (rs->buf.data (), "OK") != 0)
  4226. warning (_("Remote refused setting permissions with: %s"),
  4227. rs->buf.data ());
  4228. }
  4229. /* This type describes each known response to the qSupported
  4230. packet. */
  4231. struct protocol_feature
  4232. {
  4233. /* The name of this protocol feature. */
  4234. const char *name;
  4235. /* The default for this protocol feature. */
  4236. enum packet_support default_support;
  4237. /* The function to call when this feature is reported, or after
  4238. qSupported processing if the feature is not supported.
  4239. The first argument points to this structure. The second
  4240. argument indicates whether the packet requested support be
  4241. enabled, disabled, or probed (or the default, if this function
  4242. is being called at the end of processing and this feature was
  4243. not reported). The third argument may be NULL; if not NULL, it
  4244. is a NUL-terminated string taken from the packet following
  4245. this feature's name and an equals sign. */
  4246. void (*func) (remote_target *remote, const struct protocol_feature *,
  4247. enum packet_support, const char *);
  4248. /* The corresponding packet for this feature. Only used if
  4249. FUNC is remote_supported_packet. */
  4250. int packet;
  4251. };
  4252. static void
  4253. remote_supported_packet (remote_target *remote,
  4254. const struct protocol_feature *feature,
  4255. enum packet_support support,
  4256. const char *argument)
  4257. {
  4258. if (argument)
  4259. {
  4260. warning (_("Remote qSupported response supplied an unexpected value for"
  4261. " \"%s\"."), feature->name);
  4262. return;
  4263. }
  4264. remote_protocol_packets[feature->packet].support = support;
  4265. }
  4266. void
  4267. remote_target::remote_packet_size (const protocol_feature *feature,
  4268. enum packet_support support, const char *value)
  4269. {
  4270. struct remote_state *rs = get_remote_state ();
  4271. int packet_size;
  4272. char *value_end;
  4273. if (support != PACKET_ENABLE)
  4274. return;
  4275. if (value == NULL || *value == '\0')
  4276. {
  4277. warning (_("Remote target reported \"%s\" without a size."),
  4278. feature->name);
  4279. return;
  4280. }
  4281. errno = 0;
  4282. packet_size = strtol (value, &value_end, 16);
  4283. if (errno != 0 || *value_end != '\0' || packet_size < 0)
  4284. {
  4285. warning (_("Remote target reported \"%s\" with a bad size: \"%s\"."),
  4286. feature->name, value);
  4287. return;
  4288. }
  4289. /* Record the new maximum packet size. */
  4290. rs->explicit_packet_size = packet_size;
  4291. }
  4292. static void
  4293. remote_packet_size (remote_target *remote, const protocol_feature *feature,
  4294. enum packet_support support, const char *value)
  4295. {
  4296. remote->remote_packet_size (feature, support, value);
  4297. }
  4298. static const struct protocol_feature remote_protocol_features[] = {
  4299. { "PacketSize", PACKET_DISABLE, remote_packet_size, -1 },
  4300. { "qXfer:auxv:read", PACKET_DISABLE, remote_supported_packet,
  4301. PACKET_qXfer_auxv },
  4302. { "qXfer:exec-file:read", PACKET_DISABLE, remote_supported_packet,
  4303. PACKET_qXfer_exec_file },
  4304. { "qXfer:features:read", PACKET_DISABLE, remote_supported_packet,
  4305. PACKET_qXfer_features },
  4306. { "qXfer:libraries:read", PACKET_DISABLE, remote_supported_packet,
  4307. PACKET_qXfer_libraries },
  4308. { "qXfer:libraries-svr4:read", PACKET_DISABLE, remote_supported_packet,
  4309. PACKET_qXfer_libraries_svr4 },
  4310. { "augmented-libraries-svr4-read", PACKET_DISABLE,
  4311. remote_supported_packet, PACKET_augmented_libraries_svr4_read_feature },
  4312. { "qXfer:memory-map:read", PACKET_DISABLE, remote_supported_packet,
  4313. PACKET_qXfer_memory_map },
  4314. { "qXfer:osdata:read", PACKET_DISABLE, remote_supported_packet,
  4315. PACKET_qXfer_osdata },
  4316. { "qXfer:threads:read", PACKET_DISABLE, remote_supported_packet,
  4317. PACKET_qXfer_threads },
  4318. { "qXfer:traceframe-info:read", PACKET_DISABLE, remote_supported_packet,
  4319. PACKET_qXfer_traceframe_info },
  4320. { "QPassSignals", PACKET_DISABLE, remote_supported_packet,
  4321. PACKET_QPassSignals },
  4322. { "QCatchSyscalls", PACKET_DISABLE, remote_supported_packet,
  4323. PACKET_QCatchSyscalls },
  4324. { "QProgramSignals", PACKET_DISABLE, remote_supported_packet,
  4325. PACKET_QProgramSignals },
  4326. { "QSetWorkingDir", PACKET_DISABLE, remote_supported_packet,
  4327. PACKET_QSetWorkingDir },
  4328. { "QStartupWithShell", PACKET_DISABLE, remote_supported_packet,
  4329. PACKET_QStartupWithShell },
  4330. { "QEnvironmentHexEncoded", PACKET_DISABLE, remote_supported_packet,
  4331. PACKET_QEnvironmentHexEncoded },
  4332. { "QEnvironmentReset", PACKET_DISABLE, remote_supported_packet,
  4333. PACKET_QEnvironmentReset },
  4334. { "QEnvironmentUnset", PACKET_DISABLE, remote_supported_packet,
  4335. PACKET_QEnvironmentUnset },
  4336. { "QStartNoAckMode", PACKET_DISABLE, remote_supported_packet,
  4337. PACKET_QStartNoAckMode },
  4338. { "multiprocess", PACKET_DISABLE, remote_supported_packet,
  4339. PACKET_multiprocess_feature },
  4340. { "QNonStop", PACKET_DISABLE, remote_supported_packet, PACKET_QNonStop },
  4341. { "qXfer:siginfo:read", PACKET_DISABLE, remote_supported_packet,
  4342. PACKET_qXfer_siginfo_read },
  4343. { "qXfer:siginfo:write", PACKET_DISABLE, remote_supported_packet,
  4344. PACKET_qXfer_siginfo_write },
  4345. { "ConditionalTracepoints", PACKET_DISABLE, remote_supported_packet,
  4346. PACKET_ConditionalTracepoints },
  4347. { "ConditionalBreakpoints", PACKET_DISABLE, remote_supported_packet,
  4348. PACKET_ConditionalBreakpoints },
  4349. { "BreakpointCommands", PACKET_DISABLE, remote_supported_packet,
  4350. PACKET_BreakpointCommands },
  4351. { "FastTracepoints", PACKET_DISABLE, remote_supported_packet,
  4352. PACKET_FastTracepoints },
  4353. { "StaticTracepoints", PACKET_DISABLE, remote_supported_packet,
  4354. PACKET_StaticTracepoints },
  4355. {"InstallInTrace", PACKET_DISABLE, remote_supported_packet,
  4356. PACKET_InstallInTrace},
  4357. { "DisconnectedTracing", PACKET_DISABLE, remote_supported_packet,
  4358. PACKET_DisconnectedTracing_feature },
  4359. { "ReverseContinue", PACKET_DISABLE, remote_supported_packet,
  4360. PACKET_bc },
  4361. { "ReverseStep", PACKET_DISABLE, remote_supported_packet,
  4362. PACKET_bs },
  4363. { "TracepointSource", PACKET_DISABLE, remote_supported_packet,
  4364. PACKET_TracepointSource },
  4365. { "QAllow", PACKET_DISABLE, remote_supported_packet,
  4366. PACKET_QAllow },
  4367. { "EnableDisableTracepoints", PACKET_DISABLE, remote_supported_packet,
  4368. PACKET_EnableDisableTracepoints_feature },
  4369. { "qXfer:fdpic:read", PACKET_DISABLE, remote_supported_packet,
  4370. PACKET_qXfer_fdpic },
  4371. { "qXfer:uib:read", PACKET_DISABLE, remote_supported_packet,
  4372. PACKET_qXfer_uib },
  4373. { "QDisableRandomization", PACKET_DISABLE, remote_supported_packet,
  4374. PACKET_QDisableRandomization },
  4375. { "QAgent", PACKET_DISABLE, remote_supported_packet, PACKET_QAgent},
  4376. { "QTBuffer:size", PACKET_DISABLE,
  4377. remote_supported_packet, PACKET_QTBuffer_size},
  4378. { "tracenz", PACKET_DISABLE, remote_supported_packet, PACKET_tracenz_feature },
  4379. { "Qbtrace:off", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_off },
  4380. { "Qbtrace:bts", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_bts },
  4381. { "Qbtrace:pt", PACKET_DISABLE, remote_supported_packet, PACKET_Qbtrace_pt },
  4382. { "qXfer:btrace:read", PACKET_DISABLE, remote_supported_packet,
  4383. PACKET_qXfer_btrace },
  4384. { "qXfer:btrace-conf:read", PACKET_DISABLE, remote_supported_packet,
  4385. PACKET_qXfer_btrace_conf },
  4386. { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
  4387. PACKET_Qbtrace_conf_bts_size },
  4388. { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
  4389. { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
  4390. { "fork-events", PACKET_DISABLE, remote_supported_packet,
  4391. PACKET_fork_event_feature },
  4392. { "vfork-events", PACKET_DISABLE, remote_supported_packet,
  4393. PACKET_vfork_event_feature },
  4394. { "exec-events", PACKET_DISABLE, remote_supported_packet,
  4395. PACKET_exec_event_feature },
  4396. { "Qbtrace-conf:pt:size", PACKET_DISABLE, remote_supported_packet,
  4397. PACKET_Qbtrace_conf_pt_size },
  4398. { "vContSupported", PACKET_DISABLE, remote_supported_packet, PACKET_vContSupported },
  4399. { "QThreadEvents", PACKET_DISABLE, remote_supported_packet, PACKET_QThreadEvents },
  4400. { "no-resumed", PACKET_DISABLE, remote_supported_packet, PACKET_no_resumed },
  4401. { "memory-tagging", PACKET_DISABLE, remote_supported_packet,
  4402. PACKET_memory_tagging_feature },
  4403. };
  4404. static char *remote_support_xml;
  4405. /* Register string appended to "xmlRegisters=" in qSupported query. */
  4406. void
  4407. register_remote_support_xml (const char *xml)
  4408. {
  4409. #if defined(HAVE_LIBEXPAT)
  4410. if (remote_support_xml == NULL)
  4411. remote_support_xml = concat ("xmlRegisters=", xml, (char *) NULL);
  4412. else
  4413. {
  4414. char *copy = xstrdup (remote_support_xml + 13);
  4415. char *saveptr;
  4416. char *p = strtok_r (copy, ",", &saveptr);
  4417. do
  4418. {
  4419. if (strcmp (p, xml) == 0)
  4420. {
  4421. /* already there */
  4422. xfree (copy);
  4423. return;
  4424. }
  4425. }
  4426. while ((p = strtok_r (NULL, ",", &saveptr)) != NULL);
  4427. xfree (copy);
  4428. remote_support_xml = reconcat (remote_support_xml,
  4429. remote_support_xml, ",", xml,
  4430. (char *) NULL);
  4431. }
  4432. #endif
  4433. }
  4434. static void
  4435. remote_query_supported_append (std::string *msg, const char *append)
  4436. {
  4437. if (!msg->empty ())
  4438. msg->append (";");
  4439. msg->append (append);
  4440. }
  4441. void
  4442. remote_target::remote_query_supported ()
  4443. {
  4444. struct remote_state *rs = get_remote_state ();
  4445. char *next;
  4446. int i;
  4447. unsigned char seen [ARRAY_SIZE (remote_protocol_features)];
  4448. /* The packet support flags are handled differently for this packet
  4449. than for most others. We treat an error, a disabled packet, and
  4450. an empty response identically: any features which must be reported
  4451. to be used will be automatically disabled. An empty buffer
  4452. accomplishes this, since that is also the representation for a list
  4453. containing no features. */
  4454. rs->buf[0] = 0;
  4455. if (packet_support (PACKET_qSupported) != PACKET_DISABLE)
  4456. {
  4457. std::string q;
  4458. if (packet_set_cmd_state (PACKET_multiprocess_feature) != AUTO_BOOLEAN_FALSE)
  4459. remote_query_supported_append (&q, "multiprocess+");
  4460. if (packet_set_cmd_state (PACKET_swbreak_feature) != AUTO_BOOLEAN_FALSE)
  4461. remote_query_supported_append (&q, "swbreak+");
  4462. if (packet_set_cmd_state (PACKET_hwbreak_feature) != AUTO_BOOLEAN_FALSE)
  4463. remote_query_supported_append (&q, "hwbreak+");
  4464. remote_query_supported_append (&q, "qRelocInsn+");
  4465. if (packet_set_cmd_state (PACKET_fork_event_feature)
  4466. != AUTO_BOOLEAN_FALSE)
  4467. remote_query_supported_append (&q, "fork-events+");
  4468. if (packet_set_cmd_state (PACKET_vfork_event_feature)
  4469. != AUTO_BOOLEAN_FALSE)
  4470. remote_query_supported_append (&q, "vfork-events+");
  4471. if (packet_set_cmd_state (PACKET_exec_event_feature)
  4472. != AUTO_BOOLEAN_FALSE)
  4473. remote_query_supported_append (&q, "exec-events+");
  4474. if (packet_set_cmd_state (PACKET_vContSupported) != AUTO_BOOLEAN_FALSE)
  4475. remote_query_supported_append (&q, "vContSupported+");
  4476. if (packet_set_cmd_state (PACKET_QThreadEvents) != AUTO_BOOLEAN_FALSE)
  4477. remote_query_supported_append (&q, "QThreadEvents+");
  4478. if (packet_set_cmd_state (PACKET_no_resumed) != AUTO_BOOLEAN_FALSE)
  4479. remote_query_supported_append (&q, "no-resumed+");
  4480. if (packet_set_cmd_state (PACKET_memory_tagging_feature)
  4481. != AUTO_BOOLEAN_FALSE)
  4482. remote_query_supported_append (&q, "memory-tagging+");
  4483. /* Keep this one last to work around a gdbserver <= 7.10 bug in
  4484. the qSupported:xmlRegisters=i386 handling. */
  4485. if (remote_support_xml != NULL
  4486. && packet_support (PACKET_qXfer_features) != PACKET_DISABLE)
  4487. remote_query_supported_append (&q, remote_support_xml);
  4488. q = "qSupported:" + q;
  4489. putpkt (q.c_str ());
  4490. getpkt (&rs->buf, 0);
  4491. /* If an error occured, warn, but do not return - just reset the
  4492. buffer to empty and go on to disable features. */
  4493. if (packet_ok (rs->buf, &remote_protocol_packets[PACKET_qSupported])
  4494. == PACKET_ERROR)
  4495. {
  4496. warning (_("Remote failure reply: %s"), rs->buf.data ());
  4497. rs->buf[0] = 0;
  4498. }
  4499. }
  4500. memset (seen, 0, sizeof (seen));
  4501. next = rs->buf.data ();
  4502. while (*next)
  4503. {
  4504. enum packet_support is_supported;
  4505. char *p, *end, *name_end, *value;
  4506. /* First separate out this item from the rest of the packet. If
  4507. there's another item after this, we overwrite the separator
  4508. (terminated strings are much easier to work with). */
  4509. p = next;
  4510. end = strchr (p, ';');
  4511. if (end == NULL)
  4512. {
  4513. end = p + strlen (p);
  4514. next = end;
  4515. }
  4516. else
  4517. {
  4518. *end = '\0';
  4519. next = end + 1;
  4520. if (end == p)
  4521. {
  4522. warning (_("empty item in \"qSupported\" response"));
  4523. continue;
  4524. }
  4525. }
  4526. name_end = strchr (p, '=');
  4527. if (name_end)
  4528. {
  4529. /* This is a name=value entry. */
  4530. is_supported = PACKET_ENABLE;
  4531. value = name_end + 1;
  4532. *name_end = '\0';
  4533. }
  4534. else
  4535. {
  4536. value = NULL;
  4537. switch (end[-1])
  4538. {
  4539. case '+':
  4540. is_supported = PACKET_ENABLE;
  4541. break;
  4542. case '-':
  4543. is_supported = PACKET_DISABLE;
  4544. break;
  4545. case '?':
  4546. is_supported = PACKET_SUPPORT_UNKNOWN;
  4547. break;
  4548. default:
  4549. warning (_("unrecognized item \"%s\" "
  4550. "in \"qSupported\" response"), p);
  4551. continue;
  4552. }
  4553. end[-1] = '\0';
  4554. }
  4555. for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
  4556. if (strcmp (remote_protocol_features[i].name, p) == 0)
  4557. {
  4558. const struct protocol_feature *feature;
  4559. seen[i] = 1;
  4560. feature = &remote_protocol_features[i];
  4561. feature->func (this, feature, is_supported, value);
  4562. break;
  4563. }
  4564. }
  4565. /* If we increased the packet size, make sure to increase the global
  4566. buffer size also. We delay this until after parsing the entire
  4567. qSupported packet, because this is the same buffer we were
  4568. parsing. */
  4569. if (rs->buf.size () < rs->explicit_packet_size)
  4570. rs->buf.resize (rs->explicit_packet_size);
  4571. /* Handle the defaults for unmentioned features. */
  4572. for (i = 0; i < ARRAY_SIZE (remote_protocol_features); i++)
  4573. if (!seen[i])
  4574. {
  4575. const struct protocol_feature *feature;
  4576. feature = &remote_protocol_features[i];
  4577. feature->func (this, feature, feature->default_support, NULL);
  4578. }
  4579. }
  4580. /* Serial QUIT handler for the remote serial descriptor.
  4581. Defers handling a Ctrl-C until we're done with the current
  4582. command/response packet sequence, unless:
  4583. - We're setting up the connection. Don't send a remote interrupt
  4584. request, as we're not fully synced yet. Quit immediately
  4585. instead.
  4586. - The target has been resumed in the foreground
  4587. (target_terminal::is_ours is false) with a synchronous resume
  4588. packet, and we're blocked waiting for the stop reply, thus a
  4589. Ctrl-C should be immediately sent to the target.
  4590. - We get a second Ctrl-C while still within the same serial read or
  4591. write. In that case the serial is seemingly wedged --- offer to
  4592. quit/disconnect.
  4593. - We see a second Ctrl-C without target response, after having
  4594. previously interrupted the target. In that case the target/stub
  4595. is probably wedged --- offer to quit/disconnect.
  4596. */
  4597. void
  4598. remote_target::remote_serial_quit_handler ()
  4599. {
  4600. struct remote_state *rs = get_remote_state ();
  4601. if (check_quit_flag ())
  4602. {
  4603. /* If we're starting up, we're not fully synced yet. Quit
  4604. immediately. */
  4605. if (rs->starting_up)
  4606. quit ();
  4607. else if (rs->got_ctrlc_during_io)
  4608. {
  4609. if (query (_("The target is not responding to GDB commands.\n"
  4610. "Stop debugging it? ")))
  4611. remote_unpush_and_throw (this);
  4612. }
  4613. /* If ^C has already been sent once, offer to disconnect. */
  4614. else if (!target_terminal::is_ours () && rs->ctrlc_pending_p)
  4615. interrupt_query ();
  4616. /* All-stop protocol, and blocked waiting for stop reply. Send
  4617. an interrupt request. */
  4618. else if (!target_terminal::is_ours () && rs->waiting_for_stop_reply)
  4619. target_interrupt ();
  4620. else
  4621. rs->got_ctrlc_during_io = 1;
  4622. }
  4623. }
  4624. /* The remote_target that is current while the quit handler is
  4625. overridden with remote_serial_quit_handler. */
  4626. static remote_target *curr_quit_handler_target;
  4627. static void
  4628. remote_serial_quit_handler ()
  4629. {
  4630. curr_quit_handler_target->remote_serial_quit_handler ();
  4631. }
  4632. /* Remove the remote target from the target stack of each inferior
  4633. that is using it. Upper targets depend on it so remove them
  4634. first. */
  4635. static void
  4636. remote_unpush_target (remote_target *target)
  4637. {
  4638. /* We have to unpush the target from all inferiors, even those that
  4639. aren't running. */
  4640. scoped_restore_current_inferior restore_current_inferior;
  4641. for (inferior *inf : all_inferiors (target))
  4642. {
  4643. switch_to_inferior_no_thread (inf);
  4644. pop_all_targets_at_and_above (process_stratum);
  4645. generic_mourn_inferior ();
  4646. }
  4647. /* Don't rely on target_close doing this when the target is popped
  4648. from the last remote inferior above, because something may be
  4649. holding a reference to the target higher up on the stack, meaning
  4650. target_close won't be called yet. We lost the connection to the
  4651. target, so clear these now, otherwise we may later throw
  4652. TARGET_CLOSE_ERROR while trying to tell the remote target to
  4653. close the file. */
  4654. fileio_handles_invalidate_target (target);
  4655. }
  4656. static void
  4657. remote_unpush_and_throw (remote_target *target)
  4658. {
  4659. remote_unpush_target (target);
  4660. throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
  4661. }
  4662. void
  4663. remote_target::open_1 (const char *name, int from_tty, int extended_p)
  4664. {
  4665. remote_target *curr_remote = get_current_remote_target ();
  4666. if (name == 0)
  4667. error (_("To open a remote debug connection, you need to specify what\n"
  4668. "serial device is attached to the remote system\n"
  4669. "(e.g. /dev/ttyS0, /dev/ttya, COM1, etc.)."));
  4670. /* If we're connected to a running target, target_preopen will kill it.
  4671. Ask this question first, before target_preopen has a chance to kill
  4672. anything. */
  4673. if (curr_remote != NULL && !target_has_execution ())
  4674. {
  4675. if (from_tty
  4676. && !query (_("Already connected to a remote target. Disconnect? ")))
  4677. error (_("Still connected."));
  4678. }
  4679. /* Here the possibly existing remote target gets unpushed. */
  4680. target_preopen (from_tty);
  4681. remote_fileio_reset ();
  4682. reopen_exec_file ();
  4683. reread_symbols (from_tty);
  4684. remote_target *remote
  4685. = (extended_p ? new extended_remote_target () : new remote_target ());
  4686. target_ops_up target_holder (remote);
  4687. remote_state *rs = remote->get_remote_state ();
  4688. /* See FIXME above. */
  4689. if (!target_async_permitted)
  4690. rs->wait_forever_enabled_p = 1;
  4691. rs->remote_desc = remote_serial_open (name);
  4692. if (!rs->remote_desc)
  4693. perror_with_name (name);
  4694. if (baud_rate != -1)
  4695. {
  4696. if (serial_setbaudrate (rs->remote_desc, baud_rate))
  4697. {
  4698. /* The requested speed could not be set. Error out to
  4699. top level after closing remote_desc. Take care to
  4700. set remote_desc to NULL to avoid closing remote_desc
  4701. more than once. */
  4702. serial_close (rs->remote_desc);
  4703. rs->remote_desc = NULL;
  4704. perror_with_name (name);
  4705. }
  4706. }
  4707. serial_setparity (rs->remote_desc, serial_parity);
  4708. serial_raw (rs->remote_desc);
  4709. /* If there is something sitting in the buffer we might take it as a
  4710. response to a command, which would be bad. */
  4711. serial_flush_input (rs->remote_desc);
  4712. if (from_tty)
  4713. {
  4714. gdb_puts ("Remote debugging using ");
  4715. gdb_puts (name);
  4716. gdb_puts ("\n");
  4717. }
  4718. /* Switch to using the remote target now. */
  4719. current_inferior ()->push_target (std::move (target_holder));
  4720. /* Register extra event sources in the event loop. */
  4721. rs->remote_async_inferior_event_token
  4722. = create_async_event_handler (remote_async_inferior_event_handler, nullptr,
  4723. "remote");
  4724. rs->notif_state = remote_notif_state_allocate (remote);
  4725. /* Reset the target state; these things will be queried either by
  4726. remote_query_supported or as they are needed. */
  4727. reset_all_packet_configs_support ();
  4728. rs->explicit_packet_size = 0;
  4729. rs->noack_mode = 0;
  4730. rs->extended = extended_p;
  4731. rs->waiting_for_stop_reply = 0;
  4732. rs->ctrlc_pending_p = 0;
  4733. rs->got_ctrlc_during_io = 0;
  4734. rs->general_thread = not_sent_ptid;
  4735. rs->continue_thread = not_sent_ptid;
  4736. rs->remote_traceframe_number = -1;
  4737. rs->last_resume_exec_dir = EXEC_FORWARD;
  4738. /* Probe for ability to use "ThreadInfo" query, as required. */
  4739. rs->use_threadinfo_query = 1;
  4740. rs->use_threadextra_query = 1;
  4741. rs->readahead_cache.invalidate ();
  4742. if (target_async_permitted)
  4743. {
  4744. /* FIXME: cagney/1999-09-23: During the initial connection it is
  4745. assumed that the target is already ready and able to respond to
  4746. requests. Unfortunately remote_start_remote() eventually calls
  4747. wait_for_inferior() with no timeout. wait_forever_enabled_p gets
  4748. around this. Eventually a mechanism that allows
  4749. wait_for_inferior() to expect/get timeouts will be
  4750. implemented. */
  4751. rs->wait_forever_enabled_p = 0;
  4752. }
  4753. /* First delete any symbols previously loaded from shared libraries. */
  4754. no_shared_libraries (NULL, 0);
  4755. /* Start the remote connection. If error() or QUIT, discard this
  4756. target (we'd otherwise be in an inconsistent state) and then
  4757. propogate the error on up the exception chain. This ensures that
  4758. the caller doesn't stumble along blindly assuming that the
  4759. function succeeded. The CLI doesn't have this problem but other
  4760. UI's, such as MI do.
  4761. FIXME: cagney/2002-05-19: Instead of re-throwing the exception,
  4762. this function should return an error indication letting the
  4763. caller restore the previous state. Unfortunately the command
  4764. ``target remote'' is directly wired to this function making that
  4765. impossible. On a positive note, the CLI side of this problem has
  4766. been fixed - the function set_cmd_context() makes it possible for
  4767. all the ``target ....'' commands to share a common callback
  4768. function. See cli-dump.c. */
  4769. {
  4770. try
  4771. {
  4772. remote->start_remote (from_tty, extended_p);
  4773. }
  4774. catch (const gdb_exception &ex)
  4775. {
  4776. /* Pop the partially set up target - unless something else did
  4777. already before throwing the exception. */
  4778. if (ex.error != TARGET_CLOSE_ERROR)
  4779. remote_unpush_target (remote);
  4780. throw;
  4781. }
  4782. }
  4783. remote_btrace_reset (rs);
  4784. if (target_async_permitted)
  4785. rs->wait_forever_enabled_p = 1;
  4786. }
  4787. /* Determine if WS represents a fork status. */
  4788. static bool
  4789. is_fork_status (target_waitkind kind)
  4790. {
  4791. return (kind == TARGET_WAITKIND_FORKED
  4792. || kind == TARGET_WAITKIND_VFORKED);
  4793. }
  4794. /* Return THREAD's pending status if it is a pending fork parent, else
  4795. return nullptr. */
  4796. static const target_waitstatus *
  4797. thread_pending_fork_status (struct thread_info *thread)
  4798. {
  4799. const target_waitstatus &ws
  4800. = (thread->has_pending_waitstatus ()
  4801. ? thread->pending_waitstatus ()
  4802. : thread->pending_follow);
  4803. if (!is_fork_status (ws.kind ()))
  4804. return nullptr;
  4805. return &ws;
  4806. }
  4807. /* Detach the specified process. */
  4808. void
  4809. remote_target::remote_detach_pid (int pid)
  4810. {
  4811. struct remote_state *rs = get_remote_state ();
  4812. /* This should not be necessary, but the handling for D;PID in
  4813. GDBserver versions prior to 8.2 incorrectly assumes that the
  4814. selected process points to the same process we're detaching,
  4815. leading to misbehavior (and possibly GDBserver crashing) when it
  4816. does not. Since it's easy and cheap, work around it by forcing
  4817. GDBserver to select GDB's current process. */
  4818. set_general_process ();
  4819. if (remote_multi_process_p (rs))
  4820. xsnprintf (rs->buf.data (), get_remote_packet_size (), "D;%x", pid);
  4821. else
  4822. strcpy (rs->buf.data (), "D");
  4823. putpkt (rs->buf);
  4824. getpkt (&rs->buf, 0);
  4825. if (rs->buf[0] == 'O' && rs->buf[1] == 'K')
  4826. ;
  4827. else if (rs->buf[0] == '\0')
  4828. error (_("Remote doesn't know how to detach"));
  4829. else
  4830. error (_("Can't detach process."));
  4831. }
  4832. /* This detaches a program to which we previously attached, using
  4833. inferior_ptid to identify the process. After this is done, GDB
  4834. can be used to debug some other program. We better not have left
  4835. any breakpoints in the target program or it'll die when it hits
  4836. one. */
  4837. void
  4838. remote_target::remote_detach_1 (inferior *inf, int from_tty)
  4839. {
  4840. int pid = inferior_ptid.pid ();
  4841. struct remote_state *rs = get_remote_state ();
  4842. int is_fork_parent;
  4843. if (!target_has_execution ())
  4844. error (_("No process to detach from."));
  4845. target_announce_detach (from_tty);
  4846. if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
  4847. {
  4848. /* If we're in breakpoints-always-inserted mode, or the inferior
  4849. is running, we have to remove breakpoints before detaching.
  4850. We don't do this in common code instead because not all
  4851. targets support removing breakpoints while the target is
  4852. running. The remote target / gdbserver does, though. */
  4853. remove_breakpoints_inf (current_inferior ());
  4854. }
  4855. /* Tell the remote target to detach. */
  4856. remote_detach_pid (pid);
  4857. /* Exit only if this is the only active inferior. */
  4858. if (from_tty && !rs->extended && number_of_live_inferiors (this) == 1)
  4859. gdb_puts (_("Ending remote debugging.\n"));
  4860. /* See if any thread of the inferior we are detaching has a pending fork
  4861. status. In that case, we must detach from the child resulting from
  4862. that fork. */
  4863. for (thread_info *thread : inf->non_exited_threads ())
  4864. {
  4865. const target_waitstatus *ws = thread_pending_fork_status (thread);
  4866. if (ws == nullptr)
  4867. continue;
  4868. remote_detach_pid (ws->child_ptid ().pid ());
  4869. }
  4870. /* Check also for any pending fork events in the stop reply queue. */
  4871. remote_notif_get_pending_events (&notif_client_stop);
  4872. for (stop_reply_up &reply : rs->stop_reply_queue)
  4873. {
  4874. if (reply->ptid.pid () != pid)
  4875. continue;
  4876. if (!is_fork_status (reply->ws.kind ()))
  4877. continue;
  4878. remote_detach_pid (reply->ws.child_ptid ().pid ());
  4879. }
  4880. thread_info *tp = find_thread_ptid (this, inferior_ptid);
  4881. /* Check to see if we are detaching a fork parent. Note that if we
  4882. are detaching a fork child, tp == NULL. */
  4883. is_fork_parent = (tp != NULL
  4884. && tp->pending_follow.kind () == TARGET_WAITKIND_FORKED);
  4885. /* If doing detach-on-fork, we don't mourn, because that will delete
  4886. breakpoints that should be available for the followed inferior. */
  4887. if (!is_fork_parent)
  4888. {
  4889. /* Save the pid as a string before mourning, since that will
  4890. unpush the remote target, and we need the string after. */
  4891. std::string infpid = target_pid_to_str (ptid_t (pid));
  4892. target_mourn_inferior (inferior_ptid);
  4893. if (print_inferior_events)
  4894. gdb_printf (_("[Inferior %d (%s) detached]\n"),
  4895. inf->num, infpid.c_str ());
  4896. }
  4897. else
  4898. {
  4899. switch_to_no_thread ();
  4900. detach_inferior (current_inferior ());
  4901. }
  4902. }
  4903. void
  4904. remote_target::detach (inferior *inf, int from_tty)
  4905. {
  4906. remote_detach_1 (inf, from_tty);
  4907. }
  4908. void
  4909. extended_remote_target::detach (inferior *inf, int from_tty)
  4910. {
  4911. remote_detach_1 (inf, from_tty);
  4912. }
  4913. /* Target follow-fork function for remote targets. On entry, and
  4914. at return, the current inferior is the fork parent.
  4915. Note that although this is currently only used for extended-remote,
  4916. it is named remote_follow_fork in anticipation of using it for the
  4917. remote target as well. */
  4918. void
  4919. remote_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
  4920. target_waitkind fork_kind, bool follow_child,
  4921. bool detach_fork)
  4922. {
  4923. process_stratum_target::follow_fork (child_inf, child_ptid,
  4924. fork_kind, follow_child, detach_fork);
  4925. struct remote_state *rs = get_remote_state ();
  4926. if ((fork_kind == TARGET_WAITKIND_FORKED && remote_fork_event_p (rs))
  4927. || (fork_kind == TARGET_WAITKIND_VFORKED && remote_vfork_event_p (rs)))
  4928. {
  4929. /* When following the parent and detaching the child, we detach
  4930. the child here. For the case of following the child and
  4931. detaching the parent, the detach is done in the target-
  4932. independent follow fork code in infrun.c. We can't use
  4933. target_detach when detaching an unfollowed child because
  4934. the client side doesn't know anything about the child. */
  4935. if (detach_fork && !follow_child)
  4936. {
  4937. /* Detach the fork child. */
  4938. remote_detach_pid (child_ptid.pid ());
  4939. }
  4940. }
  4941. }
  4942. /* Target follow-exec function for remote targets. Save EXECD_PATHNAME
  4943. in the program space of the new inferior. */
  4944. void
  4945. remote_target::follow_exec (inferior *follow_inf, ptid_t ptid,
  4946. const char *execd_pathname)
  4947. {
  4948. process_stratum_target::follow_exec (follow_inf, ptid, execd_pathname);
  4949. /* We know that this is a target file name, so if it has the "target:"
  4950. prefix we strip it off before saving it in the program space. */
  4951. if (is_target_filename (execd_pathname))
  4952. execd_pathname += strlen (TARGET_SYSROOT_PREFIX);
  4953. set_pspace_remote_exec_file (follow_inf->pspace, execd_pathname);
  4954. }
  4955. /* Same as remote_detach, but don't send the "D" packet; just disconnect. */
  4956. void
  4957. remote_target::disconnect (const char *args, int from_tty)
  4958. {
  4959. if (args)
  4960. error (_("Argument given to \"disconnect\" when remotely debugging."));
  4961. /* Make sure we unpush even the extended remote targets. Calling
  4962. target_mourn_inferior won't unpush, and
  4963. remote_target::mourn_inferior won't unpush if there is more than
  4964. one inferior left. */
  4965. remote_unpush_target (this);
  4966. if (from_tty)
  4967. gdb_puts ("Ending remote debugging.\n");
  4968. }
  4969. /* Attach to the process specified by ARGS. If FROM_TTY is non-zero,
  4970. be chatty about it. */
  4971. void
  4972. extended_remote_target::attach (const char *args, int from_tty)
  4973. {
  4974. struct remote_state *rs = get_remote_state ();
  4975. int pid;
  4976. char *wait_status = NULL;
  4977. pid = parse_pid_to_attach (args);
  4978. /* Remote PID can be freely equal to getpid, do not check it here the same
  4979. way as in other targets. */
  4980. if (packet_support (PACKET_vAttach) == PACKET_DISABLE)
  4981. error (_("This target does not support attaching to a process"));
  4982. target_announce_attach (from_tty, pid);
  4983. xsnprintf (rs->buf.data (), get_remote_packet_size (), "vAttach;%x", pid);
  4984. putpkt (rs->buf);
  4985. getpkt (&rs->buf, 0);
  4986. switch (packet_ok (rs->buf,
  4987. &remote_protocol_packets[PACKET_vAttach]))
  4988. {
  4989. case PACKET_OK:
  4990. if (!target_is_non_stop_p ())
  4991. {
  4992. /* Save the reply for later. */
  4993. wait_status = (char *) alloca (strlen (rs->buf.data ()) + 1);
  4994. strcpy (wait_status, rs->buf.data ());
  4995. }
  4996. else if (strcmp (rs->buf.data (), "OK") != 0)
  4997. error (_("Attaching to %s failed with: %s"),
  4998. target_pid_to_str (ptid_t (pid)).c_str (),
  4999. rs->buf.data ());
  5000. break;
  5001. case PACKET_UNKNOWN:
  5002. error (_("This target does not support attaching to a process"));
  5003. default:
  5004. error (_("Attaching to %s failed"),
  5005. target_pid_to_str (ptid_t (pid)).c_str ());
  5006. }
  5007. switch_to_inferior_no_thread (remote_add_inferior (false, pid, 1, 0));
  5008. inferior_ptid = ptid_t (pid);
  5009. if (target_is_non_stop_p ())
  5010. {
  5011. /* Get list of threads. */
  5012. update_thread_list ();
  5013. thread_info *thread = first_thread_of_inferior (current_inferior ());
  5014. if (thread != nullptr)
  5015. switch_to_thread (thread);
  5016. /* Invalidate our notion of the remote current thread. */
  5017. record_currthread (rs, minus_one_ptid);
  5018. }
  5019. else
  5020. {
  5021. /* Now, if we have thread information, update the main thread's
  5022. ptid. */
  5023. ptid_t curr_ptid = remote_current_thread (ptid_t (pid));
  5024. /* Add the main thread to the thread list. We add the thread
  5025. silently in this case (the final true parameter). */
  5026. thread_info *thr = remote_add_thread (curr_ptid, true, true, true);
  5027. switch_to_thread (thr);
  5028. }
  5029. /* Next, if the target can specify a description, read it. We do
  5030. this before anything involving memory or registers. */
  5031. target_find_description ();
  5032. if (!target_is_non_stop_p ())
  5033. {
  5034. /* Use the previously fetched status. */
  5035. gdb_assert (wait_status != NULL);
  5036. struct notif_event *reply
  5037. = remote_notif_parse (this, &notif_client_stop, wait_status);
  5038. push_stop_reply ((struct stop_reply *) reply);
  5039. }
  5040. else
  5041. {
  5042. gdb_assert (wait_status == NULL);
  5043. gdb_assert (target_can_async_p ());
  5044. }
  5045. }
  5046. /* Implementation of the to_post_attach method. */
  5047. void
  5048. extended_remote_target::post_attach (int pid)
  5049. {
  5050. /* Get text, data & bss offsets. */
  5051. get_offsets ();
  5052. /* In certain cases GDB might not have had the chance to start
  5053. symbol lookup up until now. This could happen if the debugged
  5054. binary is not using shared libraries, the vsyscall page is not
  5055. present (on Linux) and the binary itself hadn't changed since the
  5056. debugging process was started. */
  5057. if (current_program_space->symfile_object_file != NULL)
  5058. remote_check_symbols();
  5059. }
  5060. /* Check for the availability of vCont. This function should also check
  5061. the response. */
  5062. void
  5063. remote_target::remote_vcont_probe ()
  5064. {
  5065. remote_state *rs = get_remote_state ();
  5066. char *buf;
  5067. strcpy (rs->buf.data (), "vCont?");
  5068. putpkt (rs->buf);
  5069. getpkt (&rs->buf, 0);
  5070. buf = rs->buf.data ();
  5071. /* Make sure that the features we assume are supported. */
  5072. if (startswith (buf, "vCont"))
  5073. {
  5074. char *p = &buf[5];
  5075. int support_c, support_C;
  5076. rs->supports_vCont.s = 0;
  5077. rs->supports_vCont.S = 0;
  5078. support_c = 0;
  5079. support_C = 0;
  5080. rs->supports_vCont.t = 0;
  5081. rs->supports_vCont.r = 0;
  5082. while (p && *p == ';')
  5083. {
  5084. p++;
  5085. if (*p == 's' && (*(p + 1) == ';' || *(p + 1) == 0))
  5086. rs->supports_vCont.s = 1;
  5087. else if (*p == 'S' && (*(p + 1) == ';' || *(p + 1) == 0))
  5088. rs->supports_vCont.S = 1;
  5089. else if (*p == 'c' && (*(p + 1) == ';' || *(p + 1) == 0))
  5090. support_c = 1;
  5091. else if (*p == 'C' && (*(p + 1) == ';' || *(p + 1) == 0))
  5092. support_C = 1;
  5093. else if (*p == 't' && (*(p + 1) == ';' || *(p + 1) == 0))
  5094. rs->supports_vCont.t = 1;
  5095. else if (*p == 'r' && (*(p + 1) == ';' || *(p + 1) == 0))
  5096. rs->supports_vCont.r = 1;
  5097. p = strchr (p, ';');
  5098. }
  5099. /* If c, and C are not all supported, we can't use vCont. Clearing
  5100. BUF will make packet_ok disable the packet. */
  5101. if (!support_c || !support_C)
  5102. buf[0] = 0;
  5103. }
  5104. packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCont]);
  5105. rs->supports_vCont_probed = true;
  5106. }
  5107. /* Helper function for building "vCont" resumptions. Write a
  5108. resumption to P. ENDP points to one-passed-the-end of the buffer
  5109. we're allowed to write to. Returns BUF+CHARACTERS_WRITTEN. The
  5110. thread to be resumed is PTID; STEP and SIGGNAL indicate whether the
  5111. resumed thread should be single-stepped and/or signalled. If PTID
  5112. equals minus_one_ptid, then all threads are resumed; if PTID
  5113. represents a process, then all threads of the process are resumed;
  5114. the thread to be stepped and/or signalled is given in the global
  5115. INFERIOR_PTID. */
  5116. char *
  5117. remote_target::append_resumption (char *p, char *endp,
  5118. ptid_t ptid, int step, gdb_signal siggnal)
  5119. {
  5120. struct remote_state *rs = get_remote_state ();
  5121. if (step && siggnal != GDB_SIGNAL_0)
  5122. p += xsnprintf (p, endp - p, ";S%02x", siggnal);
  5123. else if (step
  5124. /* GDB is willing to range step. */
  5125. && use_range_stepping
  5126. /* Target supports range stepping. */
  5127. && rs->supports_vCont.r
  5128. /* We don't currently support range stepping multiple
  5129. threads with a wildcard (though the protocol allows it,
  5130. so stubs shouldn't make an active effort to forbid
  5131. it). */
  5132. && !(remote_multi_process_p (rs) && ptid.is_pid ()))
  5133. {
  5134. struct thread_info *tp;
  5135. if (ptid == minus_one_ptid)
  5136. {
  5137. /* If we don't know about the target thread's tid, then
  5138. we're resuming magic_null_ptid (see caller). */
  5139. tp = find_thread_ptid (this, magic_null_ptid);
  5140. }
  5141. else
  5142. tp = find_thread_ptid (this, ptid);
  5143. gdb_assert (tp != NULL);
  5144. if (tp->control.may_range_step)
  5145. {
  5146. int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
  5147. p += xsnprintf (p, endp - p, ";r%s,%s",
  5148. phex_nz (tp->control.step_range_start,
  5149. addr_size),
  5150. phex_nz (tp->control.step_range_end,
  5151. addr_size));
  5152. }
  5153. else
  5154. p += xsnprintf (p, endp - p, ";s");
  5155. }
  5156. else if (step)
  5157. p += xsnprintf (p, endp - p, ";s");
  5158. else if (siggnal != GDB_SIGNAL_0)
  5159. p += xsnprintf (p, endp - p, ";C%02x", siggnal);
  5160. else
  5161. p += xsnprintf (p, endp - p, ";c");
  5162. if (remote_multi_process_p (rs) && ptid.is_pid ())
  5163. {
  5164. ptid_t nptid;
  5165. /* All (-1) threads of process. */
  5166. nptid = ptid_t (ptid.pid (), -1);
  5167. p += xsnprintf (p, endp - p, ":");
  5168. p = write_ptid (p, endp, nptid);
  5169. }
  5170. else if (ptid != minus_one_ptid)
  5171. {
  5172. p += xsnprintf (p, endp - p, ":");
  5173. p = write_ptid (p, endp, ptid);
  5174. }
  5175. return p;
  5176. }
  5177. /* Clear the thread's private info on resume. */
  5178. static void
  5179. resume_clear_thread_private_info (struct thread_info *thread)
  5180. {
  5181. if (thread->priv != NULL)
  5182. {
  5183. remote_thread_info *priv = get_remote_thread_info (thread);
  5184. priv->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  5185. priv->watch_data_address = 0;
  5186. }
  5187. }
  5188. /* Append a vCont continue-with-signal action for threads that have a
  5189. non-zero stop signal. */
  5190. char *
  5191. remote_target::append_pending_thread_resumptions (char *p, char *endp,
  5192. ptid_t ptid)
  5193. {
  5194. for (thread_info *thread : all_non_exited_threads (this, ptid))
  5195. if (inferior_ptid != thread->ptid
  5196. && thread->stop_signal () != GDB_SIGNAL_0)
  5197. {
  5198. p = append_resumption (p, endp, thread->ptid,
  5199. 0, thread->stop_signal ());
  5200. thread->set_stop_signal (GDB_SIGNAL_0);
  5201. resume_clear_thread_private_info (thread);
  5202. }
  5203. return p;
  5204. }
  5205. /* Set the target running, using the packets that use Hc
  5206. (c/s/C/S). */
  5207. void
  5208. remote_target::remote_resume_with_hc (ptid_t ptid, int step,
  5209. gdb_signal siggnal)
  5210. {
  5211. struct remote_state *rs = get_remote_state ();
  5212. char *buf;
  5213. rs->last_sent_signal = siggnal;
  5214. rs->last_sent_step = step;
  5215. /* The c/s/C/S resume packets use Hc, so set the continue
  5216. thread. */
  5217. if (ptid == minus_one_ptid)
  5218. set_continue_thread (any_thread_ptid);
  5219. else
  5220. set_continue_thread (ptid);
  5221. for (thread_info *thread : all_non_exited_threads (this))
  5222. resume_clear_thread_private_info (thread);
  5223. buf = rs->buf.data ();
  5224. if (::execution_direction == EXEC_REVERSE)
  5225. {
  5226. /* We don't pass signals to the target in reverse exec mode. */
  5227. if (info_verbose && siggnal != GDB_SIGNAL_0)
  5228. warning (_(" - Can't pass signal %d to target in reverse: ignored."),
  5229. siggnal);
  5230. if (step && packet_support (PACKET_bs) == PACKET_DISABLE)
  5231. error (_("Remote reverse-step not supported."));
  5232. if (!step && packet_support (PACKET_bc) == PACKET_DISABLE)
  5233. error (_("Remote reverse-continue not supported."));
  5234. strcpy (buf, step ? "bs" : "bc");
  5235. }
  5236. else if (siggnal != GDB_SIGNAL_0)
  5237. {
  5238. buf[0] = step ? 'S' : 'C';
  5239. buf[1] = tohex (((int) siggnal >> 4) & 0xf);
  5240. buf[2] = tohex (((int) siggnal) & 0xf);
  5241. buf[3] = '\0';
  5242. }
  5243. else
  5244. strcpy (buf, step ? "s" : "c");
  5245. putpkt (buf);
  5246. }
  5247. /* Resume the remote inferior by using a "vCont" packet. The thread
  5248. to be resumed is PTID; STEP and SIGGNAL indicate whether the
  5249. resumed thread should be single-stepped and/or signalled. If PTID
  5250. equals minus_one_ptid, then all threads are resumed; the thread to
  5251. be stepped and/or signalled is given in the global INFERIOR_PTID.
  5252. This function returns non-zero iff it resumes the inferior.
  5253. This function issues a strict subset of all possible vCont commands
  5254. at the moment. */
  5255. int
  5256. remote_target::remote_resume_with_vcont (ptid_t ptid, int step,
  5257. enum gdb_signal siggnal)
  5258. {
  5259. struct remote_state *rs = get_remote_state ();
  5260. char *p;
  5261. char *endp;
  5262. /* No reverse execution actions defined for vCont. */
  5263. if (::execution_direction == EXEC_REVERSE)
  5264. return 0;
  5265. if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
  5266. remote_vcont_probe ();
  5267. if (packet_support (PACKET_vCont) == PACKET_DISABLE)
  5268. return 0;
  5269. p = rs->buf.data ();
  5270. endp = p + get_remote_packet_size ();
  5271. /* If we could generate a wider range of packets, we'd have to worry
  5272. about overflowing BUF. Should there be a generic
  5273. "multi-part-packet" packet? */
  5274. p += xsnprintf (p, endp - p, "vCont");
  5275. if (ptid == magic_null_ptid)
  5276. {
  5277. /* MAGIC_NULL_PTID means that we don't have any active threads,
  5278. so we don't have any TID numbers the inferior will
  5279. understand. Make sure to only send forms that do not specify
  5280. a TID. */
  5281. append_resumption (p, endp, minus_one_ptid, step, siggnal);
  5282. }
  5283. else if (ptid == minus_one_ptid || ptid.is_pid ())
  5284. {
  5285. /* Resume all threads (of all processes, or of a single
  5286. process), with preference for INFERIOR_PTID. This assumes
  5287. inferior_ptid belongs to the set of all threads we are about
  5288. to resume. */
  5289. if (step || siggnal != GDB_SIGNAL_0)
  5290. {
  5291. /* Step inferior_ptid, with or without signal. */
  5292. p = append_resumption (p, endp, inferior_ptid, step, siggnal);
  5293. }
  5294. /* Also pass down any pending signaled resumption for other
  5295. threads not the current. */
  5296. p = append_pending_thread_resumptions (p, endp, ptid);
  5297. /* And continue others without a signal. */
  5298. append_resumption (p, endp, ptid, /*step=*/ 0, GDB_SIGNAL_0);
  5299. }
  5300. else
  5301. {
  5302. /* Scheduler locking; resume only PTID. */
  5303. append_resumption (p, endp, ptid, step, siggnal);
  5304. }
  5305. gdb_assert (strlen (rs->buf.data ()) < get_remote_packet_size ());
  5306. putpkt (rs->buf);
  5307. if (target_is_non_stop_p ())
  5308. {
  5309. /* In non-stop, the stub replies to vCont with "OK". The stop
  5310. reply will be reported asynchronously by means of a `%Stop'
  5311. notification. */
  5312. getpkt (&rs->buf, 0);
  5313. if (strcmp (rs->buf.data (), "OK") != 0)
  5314. error (_("Unexpected vCont reply in non-stop mode: %s"),
  5315. rs->buf.data ());
  5316. }
  5317. return 1;
  5318. }
  5319. /* Tell the remote machine to resume. */
  5320. void
  5321. remote_target::resume (ptid_t ptid, int step, enum gdb_signal siggnal)
  5322. {
  5323. struct remote_state *rs = get_remote_state ();
  5324. /* When connected in non-stop mode, the core resumes threads
  5325. individually. Resuming remote threads directly in target_resume
  5326. would thus result in sending one packet per thread. Instead, to
  5327. minimize roundtrip latency, here we just store the resume
  5328. request (put the thread in RESUMED_PENDING_VCONT state); the actual remote
  5329. resumption will be done in remote_target::commit_resume, where we'll be
  5330. able to do vCont action coalescing. */
  5331. if (target_is_non_stop_p () && ::execution_direction != EXEC_REVERSE)
  5332. {
  5333. remote_thread_info *remote_thr;
  5334. if (minus_one_ptid == ptid || ptid.is_pid ())
  5335. remote_thr = get_remote_thread_info (this, inferior_ptid);
  5336. else
  5337. remote_thr = get_remote_thread_info (this, ptid);
  5338. /* We don't expect the core to ask to resume an already resumed (from
  5339. its point of view) thread. */
  5340. gdb_assert (remote_thr->get_resume_state () == resume_state::NOT_RESUMED);
  5341. remote_thr->set_resumed_pending_vcont (step, siggnal);
  5342. return;
  5343. }
  5344. /* In all-stop, we can't mark REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN
  5345. (explained in remote-notif.c:handle_notification) so
  5346. remote_notif_process is not called. We need find a place where
  5347. it is safe to start a 'vNotif' sequence. It is good to do it
  5348. before resuming inferior, because inferior was stopped and no RSP
  5349. traffic at that moment. */
  5350. if (!target_is_non_stop_p ())
  5351. remote_notif_process (rs->notif_state, &notif_client_stop);
  5352. rs->last_resume_exec_dir = ::execution_direction;
  5353. /* Prefer vCont, and fallback to s/c/S/C, which use Hc. */
  5354. if (!remote_resume_with_vcont (ptid, step, siggnal))
  5355. remote_resume_with_hc (ptid, step, siggnal);
  5356. /* Update resumed state tracked by the remote target. */
  5357. for (thread_info *tp : all_non_exited_threads (this, ptid))
  5358. get_remote_thread_info (tp)->set_resumed ();
  5359. /* We've just told the target to resume. The remote server will
  5360. wait for the inferior to stop, and then send a stop reply. In
  5361. the mean time, we can't start another command/query ourselves
  5362. because the stub wouldn't be ready to process it. This applies
  5363. only to the base all-stop protocol, however. In non-stop (which
  5364. only supports vCont), the stub replies with an "OK", and is
  5365. immediate able to process further serial input. */
  5366. if (!target_is_non_stop_p ())
  5367. rs->waiting_for_stop_reply = 1;
  5368. }
  5369. /* Private per-inferior info for target remote processes. */
  5370. struct remote_inferior : public private_inferior
  5371. {
  5372. /* Whether we can send a wildcard vCont for this process. */
  5373. bool may_wildcard_vcont = true;
  5374. };
  5375. /* Get the remote private inferior data associated to INF. */
  5376. static remote_inferior *
  5377. get_remote_inferior (inferior *inf)
  5378. {
  5379. if (inf->priv == NULL)
  5380. inf->priv.reset (new remote_inferior);
  5381. return static_cast<remote_inferior *> (inf->priv.get ());
  5382. }
  5383. /* Class used to track the construction of a vCont packet in the
  5384. outgoing packet buffer. This is used to send multiple vCont
  5385. packets if we have more actions than would fit a single packet. */
  5386. class vcont_builder
  5387. {
  5388. public:
  5389. explicit vcont_builder (remote_target *remote)
  5390. : m_remote (remote)
  5391. {
  5392. restart ();
  5393. }
  5394. void flush ();
  5395. void push_action (ptid_t ptid, bool step, gdb_signal siggnal);
  5396. private:
  5397. void restart ();
  5398. /* The remote target. */
  5399. remote_target *m_remote;
  5400. /* Pointer to the first action. P points here if no action has been
  5401. appended yet. */
  5402. char *m_first_action;
  5403. /* Where the next action will be appended. */
  5404. char *m_p;
  5405. /* The end of the buffer. Must never write past this. */
  5406. char *m_endp;
  5407. };
  5408. /* Prepare the outgoing buffer for a new vCont packet. */
  5409. void
  5410. vcont_builder::restart ()
  5411. {
  5412. struct remote_state *rs = m_remote->get_remote_state ();
  5413. m_p = rs->buf.data ();
  5414. m_endp = m_p + m_remote->get_remote_packet_size ();
  5415. m_p += xsnprintf (m_p, m_endp - m_p, "vCont");
  5416. m_first_action = m_p;
  5417. }
  5418. /* If the vCont packet being built has any action, send it to the
  5419. remote end. */
  5420. void
  5421. vcont_builder::flush ()
  5422. {
  5423. struct remote_state *rs;
  5424. if (m_p == m_first_action)
  5425. return;
  5426. rs = m_remote->get_remote_state ();
  5427. m_remote->putpkt (rs->buf);
  5428. m_remote->getpkt (&rs->buf, 0);
  5429. if (strcmp (rs->buf.data (), "OK") != 0)
  5430. error (_("Unexpected vCont reply in non-stop mode: %s"), rs->buf.data ());
  5431. }
  5432. /* The largest action is range-stepping, with its two addresses. This
  5433. is more than sufficient. If a new, bigger action is created, it'll
  5434. quickly trigger a failed assertion in append_resumption (and we'll
  5435. just bump this). */
  5436. #define MAX_ACTION_SIZE 200
  5437. /* Append a new vCont action in the outgoing packet being built. If
  5438. the action doesn't fit the packet along with previous actions, push
  5439. what we've got so far to the remote end and start over a new vCont
  5440. packet (with the new action). */
  5441. void
  5442. vcont_builder::push_action (ptid_t ptid, bool step, gdb_signal siggnal)
  5443. {
  5444. char buf[MAX_ACTION_SIZE + 1];
  5445. char *endp = m_remote->append_resumption (buf, buf + sizeof (buf),
  5446. ptid, step, siggnal);
  5447. /* Check whether this new action would fit in the vCont packet along
  5448. with previous actions. If not, send what we've got so far and
  5449. start a new vCont packet. */
  5450. size_t rsize = endp - buf;
  5451. if (rsize > m_endp - m_p)
  5452. {
  5453. flush ();
  5454. restart ();
  5455. /* Should now fit. */
  5456. gdb_assert (rsize <= m_endp - m_p);
  5457. }
  5458. memcpy (m_p, buf, rsize);
  5459. m_p += rsize;
  5460. *m_p = '\0';
  5461. }
  5462. /* to_commit_resume implementation. */
  5463. void
  5464. remote_target::commit_resumed ()
  5465. {
  5466. /* If connected in all-stop mode, we'd send the remote resume
  5467. request directly from remote_resume. Likewise if
  5468. reverse-debugging, as there are no defined vCont actions for
  5469. reverse execution. */
  5470. if (!target_is_non_stop_p () || ::execution_direction == EXEC_REVERSE)
  5471. return;
  5472. /* Try to send wildcard actions ("vCont;c" or "vCont;c:pPID.-1")
  5473. instead of resuming all threads of each process individually.
  5474. However, if any thread of a process must remain halted, we can't
  5475. send wildcard resumes and must send one action per thread.
  5476. Care must be taken to not resume threads/processes the server
  5477. side already told us are stopped, but the core doesn't know about
  5478. yet, because the events are still in the vStopped notification
  5479. queue. For example:
  5480. #1 => vCont s:p1.1;c
  5481. #2 <= OK
  5482. #3 <= %Stopped T05 p1.1
  5483. #4 => vStopped
  5484. #5 <= T05 p1.2
  5485. #6 => vStopped
  5486. #7 <= OK
  5487. #8 (infrun handles the stop for p1.1 and continues stepping)
  5488. #9 => vCont s:p1.1;c
  5489. The last vCont above would resume thread p1.2 by mistake, because
  5490. the server has no idea that the event for p1.2 had not been
  5491. handled yet.
  5492. The server side must similarly ignore resume actions for the
  5493. thread that has a pending %Stopped notification (and any other
  5494. threads with events pending), until GDB acks the notification
  5495. with vStopped. Otherwise, e.g., the following case is
  5496. mishandled:
  5497. #1 => g (or any other packet)
  5498. #2 <= [registers]
  5499. #3 <= %Stopped T05 p1.2
  5500. #4 => vCont s:p1.1;c
  5501. #5 <= OK
  5502. Above, the server must not resume thread p1.2. GDB can't know
  5503. that p1.2 stopped until it acks the %Stopped notification, and
  5504. since from GDB's perspective all threads should be running, it
  5505. sends a "c" action.
  5506. Finally, special care must also be given to handling fork/vfork
  5507. events. A (v)fork event actually tells us that two processes
  5508. stopped -- the parent and the child. Until we follow the fork,
  5509. we must not resume the child. Therefore, if we have a pending
  5510. fork follow, we must not send a global wildcard resume action
  5511. (vCont;c). We can still send process-wide wildcards though. */
  5512. /* Start by assuming a global wildcard (vCont;c) is possible. */
  5513. bool may_global_wildcard_vcont = true;
  5514. /* And assume every process is individually wildcard-able too. */
  5515. for (inferior *inf : all_non_exited_inferiors (this))
  5516. {
  5517. remote_inferior *priv = get_remote_inferior (inf);
  5518. priv->may_wildcard_vcont = true;
  5519. }
  5520. /* Check for any pending events (not reported or processed yet) and
  5521. disable process and global wildcard resumes appropriately. */
  5522. check_pending_events_prevent_wildcard_vcont (&may_global_wildcard_vcont);
  5523. bool any_pending_vcont_resume = false;
  5524. for (thread_info *tp : all_non_exited_threads (this))
  5525. {
  5526. remote_thread_info *priv = get_remote_thread_info (tp);
  5527. /* If a thread of a process is not meant to be resumed, then we
  5528. can't wildcard that process. */
  5529. if (priv->get_resume_state () == resume_state::NOT_RESUMED)
  5530. {
  5531. get_remote_inferior (tp->inf)->may_wildcard_vcont = false;
  5532. /* And if we can't wildcard a process, we can't wildcard
  5533. everything either. */
  5534. may_global_wildcard_vcont = false;
  5535. continue;
  5536. }
  5537. if (priv->get_resume_state () == resume_state::RESUMED_PENDING_VCONT)
  5538. any_pending_vcont_resume = true;
  5539. /* If a thread is the parent of an unfollowed fork, then we
  5540. can't do a global wildcard, as that would resume the fork
  5541. child. */
  5542. if (thread_pending_fork_status (tp) != nullptr)
  5543. may_global_wildcard_vcont = false;
  5544. }
  5545. /* We didn't have any resumed thread pending a vCont resume, so nothing to
  5546. do. */
  5547. if (!any_pending_vcont_resume)
  5548. return;
  5549. /* Now let's build the vCont packet(s). Actions must be appended
  5550. from narrower to wider scopes (thread -> process -> global). If
  5551. we end up with too many actions for a single packet vcont_builder
  5552. flushes the current vCont packet to the remote side and starts a
  5553. new one. */
  5554. struct vcont_builder vcont_builder (this);
  5555. /* Threads first. */
  5556. for (thread_info *tp : all_non_exited_threads (this))
  5557. {
  5558. remote_thread_info *remote_thr = get_remote_thread_info (tp);
  5559. /* If the thread was previously vCont-resumed, no need to send a specific
  5560. action for it. If we didn't receive a resume request for it, don't
  5561. send an action for it either. */
  5562. if (remote_thr->get_resume_state () != resume_state::RESUMED_PENDING_VCONT)
  5563. continue;
  5564. gdb_assert (!thread_is_in_step_over_chain (tp));
  5565. /* We should never be commit-resuming a thread that has a stop reply.
  5566. Otherwise, we would end up reporting a stop event for a thread while
  5567. it is running on the remote target. */
  5568. remote_state *rs = get_remote_state ();
  5569. for (const auto &stop_reply : rs->stop_reply_queue)
  5570. gdb_assert (stop_reply->ptid != tp->ptid);
  5571. const resumed_pending_vcont_info &info
  5572. = remote_thr->resumed_pending_vcont_info ();
  5573. /* Check if we need to send a specific action for this thread. If not,
  5574. it will be included in a wildcard resume instead. */
  5575. if (info.step || info.sig != GDB_SIGNAL_0
  5576. || !get_remote_inferior (tp->inf)->may_wildcard_vcont)
  5577. vcont_builder.push_action (tp->ptid, info.step, info.sig);
  5578. remote_thr->set_resumed ();
  5579. }
  5580. /* Now check whether we can send any process-wide wildcard. This is
  5581. to avoid sending a global wildcard in the case nothing is
  5582. supposed to be resumed. */
  5583. bool any_process_wildcard = false;
  5584. for (inferior *inf : all_non_exited_inferiors (this))
  5585. {
  5586. if (get_remote_inferior (inf)->may_wildcard_vcont)
  5587. {
  5588. any_process_wildcard = true;
  5589. break;
  5590. }
  5591. }
  5592. if (any_process_wildcard)
  5593. {
  5594. /* If all processes are wildcard-able, then send a single "c"
  5595. action, otherwise, send an "all (-1) threads of process"
  5596. continue action for each running process, if any. */
  5597. if (may_global_wildcard_vcont)
  5598. {
  5599. vcont_builder.push_action (minus_one_ptid,
  5600. false, GDB_SIGNAL_0);
  5601. }
  5602. else
  5603. {
  5604. for (inferior *inf : all_non_exited_inferiors (this))
  5605. {
  5606. if (get_remote_inferior (inf)->may_wildcard_vcont)
  5607. {
  5608. vcont_builder.push_action (ptid_t (inf->pid),
  5609. false, GDB_SIGNAL_0);
  5610. }
  5611. }
  5612. }
  5613. }
  5614. vcont_builder.flush ();
  5615. }
  5616. /* Implementation of target_has_pending_events. */
  5617. bool
  5618. remote_target::has_pending_events ()
  5619. {
  5620. if (target_can_async_p ())
  5621. {
  5622. remote_state *rs = get_remote_state ();
  5623. if (async_event_handler_marked (rs->remote_async_inferior_event_token))
  5624. return true;
  5625. /* Note that BUFCNT can be negative, indicating sticky
  5626. error. */
  5627. if (rs->remote_desc->bufcnt != 0)
  5628. return true;
  5629. }
  5630. return false;
  5631. }
  5632. /* Non-stop version of target_stop. Uses `vCont;t' to stop a remote
  5633. thread, all threads of a remote process, or all threads of all
  5634. processes. */
  5635. void
  5636. remote_target::remote_stop_ns (ptid_t ptid)
  5637. {
  5638. struct remote_state *rs = get_remote_state ();
  5639. char *p = rs->buf.data ();
  5640. char *endp = p + get_remote_packet_size ();
  5641. /* If any thread that needs to stop was resumed but pending a vCont
  5642. resume, generate a phony stop_reply. However, first check
  5643. whether the thread wasn't resumed with a signal. Generating a
  5644. phony stop in that case would result in losing the signal. */
  5645. bool needs_commit = false;
  5646. for (thread_info *tp : all_non_exited_threads (this, ptid))
  5647. {
  5648. remote_thread_info *remote_thr = get_remote_thread_info (tp);
  5649. if (remote_thr->get_resume_state ()
  5650. == resume_state::RESUMED_PENDING_VCONT)
  5651. {
  5652. const resumed_pending_vcont_info &info
  5653. = remote_thr->resumed_pending_vcont_info ();
  5654. if (info.sig != GDB_SIGNAL_0)
  5655. {
  5656. /* This signal must be forwarded to the inferior. We
  5657. could commit-resume just this thread, but its simpler
  5658. to just commit-resume everything. */
  5659. needs_commit = true;
  5660. break;
  5661. }
  5662. }
  5663. }
  5664. if (needs_commit)
  5665. commit_resumed ();
  5666. else
  5667. for (thread_info *tp : all_non_exited_threads (this, ptid))
  5668. {
  5669. remote_thread_info *remote_thr = get_remote_thread_info (tp);
  5670. if (remote_thr->get_resume_state ()
  5671. == resume_state::RESUMED_PENDING_VCONT)
  5672. {
  5673. remote_debug_printf ("Enqueueing phony stop reply for thread pending "
  5674. "vCont-resume (%d, %ld, %s)", tp->ptid.pid(),
  5675. tp->ptid.lwp (),
  5676. pulongest (tp->ptid.tid ()));
  5677. /* Check that the thread wasn't resumed with a signal.
  5678. Generating a phony stop would result in losing the
  5679. signal. */
  5680. const resumed_pending_vcont_info &info
  5681. = remote_thr->resumed_pending_vcont_info ();
  5682. gdb_assert (info.sig == GDB_SIGNAL_0);
  5683. stop_reply *sr = new stop_reply ();
  5684. sr->ptid = tp->ptid;
  5685. sr->rs = rs;
  5686. sr->ws.set_stopped (GDB_SIGNAL_0);
  5687. sr->arch = tp->inf->gdbarch;
  5688. sr->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  5689. sr->watch_data_address = 0;
  5690. sr->core = 0;
  5691. this->push_stop_reply (sr);
  5692. /* Pretend that this thread was actually resumed on the
  5693. remote target, then stopped. If we leave it in the
  5694. RESUMED_PENDING_VCONT state and the commit_resumed
  5695. method is called while the stop reply is still in the
  5696. queue, we'll end up reporting a stop event to the core
  5697. for that thread while it is running on the remote
  5698. target... that would be bad. */
  5699. remote_thr->set_resumed ();
  5700. }
  5701. }
  5702. /* FIXME: This supports_vCont_probed check is a workaround until
  5703. packet_support is per-connection. */
  5704. if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN
  5705. || !rs->supports_vCont_probed)
  5706. remote_vcont_probe ();
  5707. if (!rs->supports_vCont.t)
  5708. error (_("Remote server does not support stopping threads"));
  5709. if (ptid == minus_one_ptid
  5710. || (!remote_multi_process_p (rs) && ptid.is_pid ()))
  5711. p += xsnprintf (p, endp - p, "vCont;t");
  5712. else
  5713. {
  5714. ptid_t nptid;
  5715. p += xsnprintf (p, endp - p, "vCont;t:");
  5716. if (ptid.is_pid ())
  5717. /* All (-1) threads of process. */
  5718. nptid = ptid_t (ptid.pid (), -1);
  5719. else
  5720. {
  5721. /* Small optimization: if we already have a stop reply for
  5722. this thread, no use in telling the stub we want this
  5723. stopped. */
  5724. if (peek_stop_reply (ptid))
  5725. return;
  5726. nptid = ptid;
  5727. }
  5728. write_ptid (p, endp, nptid);
  5729. }
  5730. /* In non-stop, we get an immediate OK reply. The stop reply will
  5731. come in asynchronously by notification. */
  5732. putpkt (rs->buf);
  5733. getpkt (&rs->buf, 0);
  5734. if (strcmp (rs->buf.data (), "OK") != 0)
  5735. error (_("Stopping %s failed: %s"), target_pid_to_str (ptid).c_str (),
  5736. rs->buf.data ());
  5737. }
  5738. /* All-stop version of target_interrupt. Sends a break or a ^C to
  5739. interrupt the remote target. It is undefined which thread of which
  5740. process reports the interrupt. */
  5741. void
  5742. remote_target::remote_interrupt_as ()
  5743. {
  5744. struct remote_state *rs = get_remote_state ();
  5745. rs->ctrlc_pending_p = 1;
  5746. /* If the inferior is stopped already, but the core didn't know
  5747. about it yet, just ignore the request. The pending stop events
  5748. will be collected in remote_wait. */
  5749. if (stop_reply_queue_length () > 0)
  5750. return;
  5751. /* Send interrupt_sequence to remote target. */
  5752. send_interrupt_sequence ();
  5753. }
  5754. /* Non-stop version of target_interrupt. Uses `vCtrlC' to interrupt
  5755. the remote target. It is undefined which thread of which process
  5756. reports the interrupt. Throws an error if the packet is not
  5757. supported by the server. */
  5758. void
  5759. remote_target::remote_interrupt_ns ()
  5760. {
  5761. struct remote_state *rs = get_remote_state ();
  5762. char *p = rs->buf.data ();
  5763. char *endp = p + get_remote_packet_size ();
  5764. xsnprintf (p, endp - p, "vCtrlC");
  5765. /* In non-stop, we get an immediate OK reply. The stop reply will
  5766. come in asynchronously by notification. */
  5767. putpkt (rs->buf);
  5768. getpkt (&rs->buf, 0);
  5769. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vCtrlC]))
  5770. {
  5771. case PACKET_OK:
  5772. break;
  5773. case PACKET_UNKNOWN:
  5774. error (_("No support for interrupting the remote target."));
  5775. case PACKET_ERROR:
  5776. error (_("Interrupting target failed: %s"), rs->buf.data ());
  5777. }
  5778. }
  5779. /* Implement the to_stop function for the remote targets. */
  5780. void
  5781. remote_target::stop (ptid_t ptid)
  5782. {
  5783. REMOTE_SCOPED_DEBUG_ENTER_EXIT;
  5784. if (target_is_non_stop_p ())
  5785. remote_stop_ns (ptid);
  5786. else
  5787. {
  5788. /* We don't currently have a way to transparently pause the
  5789. remote target in all-stop mode. Interrupt it instead. */
  5790. remote_interrupt_as ();
  5791. }
  5792. }
  5793. /* Implement the to_interrupt function for the remote targets. */
  5794. void
  5795. remote_target::interrupt ()
  5796. {
  5797. REMOTE_SCOPED_DEBUG_ENTER_EXIT;
  5798. if (target_is_non_stop_p ())
  5799. remote_interrupt_ns ();
  5800. else
  5801. remote_interrupt_as ();
  5802. }
  5803. /* Implement the to_pass_ctrlc function for the remote targets. */
  5804. void
  5805. remote_target::pass_ctrlc ()
  5806. {
  5807. REMOTE_SCOPED_DEBUG_ENTER_EXIT;
  5808. struct remote_state *rs = get_remote_state ();
  5809. /* If we're starting up, we're not fully synced yet. Quit
  5810. immediately. */
  5811. if (rs->starting_up)
  5812. quit ();
  5813. /* If ^C has already been sent once, offer to disconnect. */
  5814. else if (rs->ctrlc_pending_p)
  5815. interrupt_query ();
  5816. else
  5817. target_interrupt ();
  5818. }
  5819. /* Ask the user what to do when an interrupt is received. */
  5820. void
  5821. remote_target::interrupt_query ()
  5822. {
  5823. struct remote_state *rs = get_remote_state ();
  5824. if (rs->waiting_for_stop_reply && rs->ctrlc_pending_p)
  5825. {
  5826. if (query (_("The target is not responding to interrupt requests.\n"
  5827. "Stop debugging it? ")))
  5828. {
  5829. remote_unpush_target (this);
  5830. throw_error (TARGET_CLOSE_ERROR, _("Disconnected from target."));
  5831. }
  5832. }
  5833. else
  5834. {
  5835. if (query (_("Interrupted while waiting for the program.\n"
  5836. "Give up waiting? ")))
  5837. quit ();
  5838. }
  5839. }
  5840. /* Enable/disable target terminal ownership. Most targets can use
  5841. terminal groups to control terminal ownership. Remote targets are
  5842. different in that explicit transfer of ownership to/from GDB/target
  5843. is required. */
  5844. void
  5845. remote_target::terminal_inferior ()
  5846. {
  5847. /* NOTE: At this point we could also register our selves as the
  5848. recipient of all input. Any characters typed could then be
  5849. passed on down to the target. */
  5850. }
  5851. void
  5852. remote_target::terminal_ours ()
  5853. {
  5854. }
  5855. static void
  5856. remote_console_output (const char *msg)
  5857. {
  5858. const char *p;
  5859. for (p = msg; p[0] && p[1]; p += 2)
  5860. {
  5861. char tb[2];
  5862. char c = fromhex (p[0]) * 16 + fromhex (p[1]);
  5863. tb[0] = c;
  5864. tb[1] = 0;
  5865. gdb_stdtarg->puts (tb);
  5866. }
  5867. gdb_stdtarg->flush ();
  5868. }
  5869. /* Return the length of the stop reply queue. */
  5870. int
  5871. remote_target::stop_reply_queue_length ()
  5872. {
  5873. remote_state *rs = get_remote_state ();
  5874. return rs->stop_reply_queue.size ();
  5875. }
  5876. static void
  5877. remote_notif_stop_parse (remote_target *remote,
  5878. struct notif_client *self, const char *buf,
  5879. struct notif_event *event)
  5880. {
  5881. remote->remote_parse_stop_reply (buf, (struct stop_reply *) event);
  5882. }
  5883. static void
  5884. remote_notif_stop_ack (remote_target *remote,
  5885. struct notif_client *self, const char *buf,
  5886. struct notif_event *event)
  5887. {
  5888. struct stop_reply *stop_reply = (struct stop_reply *) event;
  5889. /* acknowledge */
  5890. putpkt (remote, self->ack_command);
  5891. /* Kind can be TARGET_WAITKIND_IGNORE if we have meanwhile discarded
  5892. the notification. It was left in the queue because we need to
  5893. acknowledge it and pull the rest of the notifications out. */
  5894. if (stop_reply->ws.kind () != TARGET_WAITKIND_IGNORE)
  5895. remote->push_stop_reply (stop_reply);
  5896. }
  5897. static int
  5898. remote_notif_stop_can_get_pending_events (remote_target *remote,
  5899. struct notif_client *self)
  5900. {
  5901. /* We can't get pending events in remote_notif_process for
  5902. notification stop, and we have to do this in remote_wait_ns
  5903. instead. If we fetch all queued events from stub, remote stub
  5904. may exit and we have no chance to process them back in
  5905. remote_wait_ns. */
  5906. remote_state *rs = remote->get_remote_state ();
  5907. mark_async_event_handler (rs->remote_async_inferior_event_token);
  5908. return 0;
  5909. }
  5910. stop_reply::~stop_reply ()
  5911. {
  5912. for (cached_reg_t &reg : regcache)
  5913. xfree (reg.data);
  5914. }
  5915. static notif_event_up
  5916. remote_notif_stop_alloc_reply ()
  5917. {
  5918. return notif_event_up (new struct stop_reply ());
  5919. }
  5920. /* A client of notification Stop. */
  5921. struct notif_client notif_client_stop =
  5922. {
  5923. "Stop",
  5924. "vStopped",
  5925. remote_notif_stop_parse,
  5926. remote_notif_stop_ack,
  5927. remote_notif_stop_can_get_pending_events,
  5928. remote_notif_stop_alloc_reply,
  5929. REMOTE_NOTIF_STOP,
  5930. };
  5931. /* If CONTEXT contains any fork child threads that have not been
  5932. reported yet, remove them from the CONTEXT list. If such a
  5933. thread exists it is because we are stopped at a fork catchpoint
  5934. and have not yet called follow_fork, which will set up the
  5935. host-side data structures for the new process. */
  5936. void
  5937. remote_target::remove_new_fork_children (threads_listing_context *context)
  5938. {
  5939. struct notif_client *notif = &notif_client_stop;
  5940. /* For any threads stopped at a fork event, remove the corresponding
  5941. fork child threads from the CONTEXT list. */
  5942. for (thread_info *thread : all_non_exited_threads (this))
  5943. {
  5944. const target_waitstatus *ws = thread_pending_fork_status (thread);
  5945. if (ws == nullptr)
  5946. continue;
  5947. context->remove_thread (ws->child_ptid ());
  5948. }
  5949. /* Check for any pending fork events (not reported or processed yet)
  5950. in process PID and remove those fork child threads from the
  5951. CONTEXT list as well. */
  5952. remote_notif_get_pending_events (notif);
  5953. for (auto &event : get_remote_state ()->stop_reply_queue)
  5954. if (event->ws.kind () == TARGET_WAITKIND_FORKED
  5955. || event->ws.kind () == TARGET_WAITKIND_VFORKED)
  5956. context->remove_thread (event->ws.child_ptid ());
  5957. else if (event->ws.kind () == TARGET_WAITKIND_THREAD_EXITED)
  5958. context->remove_thread (event->ptid);
  5959. }
  5960. /* Check whether any event pending in the vStopped queue would prevent a
  5961. global or process wildcard vCont action. Set *may_global_wildcard to
  5962. false if we can't do a global wildcard (vCont;c), and clear the event
  5963. inferior's may_wildcard_vcont flag if we can't do a process-wide
  5964. wildcard resume (vCont;c:pPID.-1). */
  5965. void
  5966. remote_target::check_pending_events_prevent_wildcard_vcont
  5967. (bool *may_global_wildcard)
  5968. {
  5969. struct notif_client *notif = &notif_client_stop;
  5970. remote_notif_get_pending_events (notif);
  5971. for (auto &event : get_remote_state ()->stop_reply_queue)
  5972. {
  5973. if (event->ws.kind () == TARGET_WAITKIND_NO_RESUMED
  5974. || event->ws.kind () == TARGET_WAITKIND_NO_HISTORY)
  5975. continue;
  5976. if (event->ws.kind () == TARGET_WAITKIND_FORKED
  5977. || event->ws.kind () == TARGET_WAITKIND_VFORKED)
  5978. *may_global_wildcard = false;
  5979. /* This may be the first time we heard about this process.
  5980. Regardless, we must not do a global wildcard resume, otherwise
  5981. we'd resume this process too. */
  5982. *may_global_wildcard = false;
  5983. if (event->ptid != null_ptid)
  5984. {
  5985. inferior *inf = find_inferior_ptid (this, event->ptid);
  5986. if (inf != NULL)
  5987. get_remote_inferior (inf)->may_wildcard_vcont = false;
  5988. }
  5989. }
  5990. }
  5991. /* Discard all pending stop replies of inferior INF. */
  5992. void
  5993. remote_target::discard_pending_stop_replies (struct inferior *inf)
  5994. {
  5995. struct stop_reply *reply;
  5996. struct remote_state *rs = get_remote_state ();
  5997. struct remote_notif_state *rns = rs->notif_state;
  5998. /* This function can be notified when an inferior exists. When the
  5999. target is not remote, the notification state is NULL. */
  6000. if (rs->remote_desc == NULL)
  6001. return;
  6002. reply = (struct stop_reply *) rns->pending_event[notif_client_stop.id];
  6003. /* Discard the in-flight notification. */
  6004. if (reply != NULL && reply->ptid.pid () == inf->pid)
  6005. {
  6006. /* Leave the notification pending, since the server expects that
  6007. we acknowledge it with vStopped. But clear its contents, so
  6008. that later on when we acknowledge it, we also discard it. */
  6009. remote_debug_printf
  6010. ("discarding in-flight notification: ptid: %s, ws: %s\n",
  6011. reply->ptid.to_string().c_str(),
  6012. reply->ws.to_string ().c_str ());
  6013. reply->ws.set_ignore ();
  6014. }
  6015. /* Discard the stop replies we have already pulled with
  6016. vStopped. */
  6017. auto iter = std::remove_if (rs->stop_reply_queue.begin (),
  6018. rs->stop_reply_queue.end (),
  6019. [=] (const stop_reply_up &event)
  6020. {
  6021. return event->ptid.pid () == inf->pid;
  6022. });
  6023. for (auto it = iter; it != rs->stop_reply_queue.end (); ++it)
  6024. remote_debug_printf
  6025. ("discarding queued stop reply: ptid: %s, ws: %s\n",
  6026. reply->ptid.to_string().c_str(),
  6027. reply->ws.to_string ().c_str ());
  6028. rs->stop_reply_queue.erase (iter, rs->stop_reply_queue.end ());
  6029. }
  6030. /* Discard the stop replies for RS in stop_reply_queue. */
  6031. void
  6032. remote_target::discard_pending_stop_replies_in_queue ()
  6033. {
  6034. remote_state *rs = get_remote_state ();
  6035. /* Discard the stop replies we have already pulled with
  6036. vStopped. */
  6037. auto iter = std::remove_if (rs->stop_reply_queue.begin (),
  6038. rs->stop_reply_queue.end (),
  6039. [=] (const stop_reply_up &event)
  6040. {
  6041. return event->rs == rs;
  6042. });
  6043. rs->stop_reply_queue.erase (iter, rs->stop_reply_queue.end ());
  6044. }
  6045. /* Remove the first reply in 'stop_reply_queue' which matches
  6046. PTID. */
  6047. struct stop_reply *
  6048. remote_target::remote_notif_remove_queued_reply (ptid_t ptid)
  6049. {
  6050. remote_state *rs = get_remote_state ();
  6051. auto iter = std::find_if (rs->stop_reply_queue.begin (),
  6052. rs->stop_reply_queue.end (),
  6053. [=] (const stop_reply_up &event)
  6054. {
  6055. return event->ptid.matches (ptid);
  6056. });
  6057. struct stop_reply *result;
  6058. if (iter == rs->stop_reply_queue.end ())
  6059. result = nullptr;
  6060. else
  6061. {
  6062. result = iter->release ();
  6063. rs->stop_reply_queue.erase (iter);
  6064. }
  6065. if (notif_debug)
  6066. gdb_printf (gdb_stdlog,
  6067. "notif: discard queued event: 'Stop' in %s\n",
  6068. ptid.to_string ().c_str ());
  6069. return result;
  6070. }
  6071. /* Look for a queued stop reply belonging to PTID. If one is found,
  6072. remove it from the queue, and return it. Returns NULL if none is
  6073. found. If there are still queued events left to process, tell the
  6074. event loop to get back to target_wait soon. */
  6075. struct stop_reply *
  6076. remote_target::queued_stop_reply (ptid_t ptid)
  6077. {
  6078. remote_state *rs = get_remote_state ();
  6079. struct stop_reply *r = remote_notif_remove_queued_reply (ptid);
  6080. if (!rs->stop_reply_queue.empty () && target_can_async_p ())
  6081. {
  6082. /* There's still at least an event left. */
  6083. mark_async_event_handler (rs->remote_async_inferior_event_token);
  6084. }
  6085. return r;
  6086. }
  6087. /* Push a fully parsed stop reply in the stop reply queue. Since we
  6088. know that we now have at least one queued event left to pass to the
  6089. core side, tell the event loop to get back to target_wait soon. */
  6090. void
  6091. remote_target::push_stop_reply (struct stop_reply *new_event)
  6092. {
  6093. remote_state *rs = get_remote_state ();
  6094. rs->stop_reply_queue.push_back (stop_reply_up (new_event));
  6095. if (notif_debug)
  6096. gdb_printf (gdb_stdlog,
  6097. "notif: push 'Stop' %s to queue %d\n",
  6098. new_event->ptid.to_string ().c_str (),
  6099. int (rs->stop_reply_queue.size ()));
  6100. /* Mark the pending event queue only if async mode is currently enabled.
  6101. If async mode is not currently enabled, then, if it later becomes
  6102. enabled, and there are events in this queue, we will mark the event
  6103. token at that point, see remote_target::async. */
  6104. if (target_is_async_p ())
  6105. mark_async_event_handler (rs->remote_async_inferior_event_token);
  6106. }
  6107. /* Returns true if we have a stop reply for PTID. */
  6108. int
  6109. remote_target::peek_stop_reply (ptid_t ptid)
  6110. {
  6111. remote_state *rs = get_remote_state ();
  6112. for (auto &event : rs->stop_reply_queue)
  6113. if (ptid == event->ptid
  6114. && event->ws.kind () == TARGET_WAITKIND_STOPPED)
  6115. return 1;
  6116. return 0;
  6117. }
  6118. /* Helper for remote_parse_stop_reply. Return nonzero if the substring
  6119. starting with P and ending with PEND matches PREFIX. */
  6120. static int
  6121. strprefix (const char *p, const char *pend, const char *prefix)
  6122. {
  6123. for ( ; p < pend; p++, prefix++)
  6124. if (*p != *prefix)
  6125. return 0;
  6126. return *prefix == '\0';
  6127. }
  6128. /* Parse the stop reply in BUF. Either the function succeeds, and the
  6129. result is stored in EVENT, or throws an error. */
  6130. void
  6131. remote_target::remote_parse_stop_reply (const char *buf, stop_reply *event)
  6132. {
  6133. remote_arch_state *rsa = NULL;
  6134. ULONGEST addr;
  6135. const char *p;
  6136. int skipregs = 0;
  6137. event->ptid = null_ptid;
  6138. event->rs = get_remote_state ();
  6139. event->ws.set_ignore ();
  6140. event->stop_reason = TARGET_STOPPED_BY_NO_REASON;
  6141. event->regcache.clear ();
  6142. event->core = -1;
  6143. switch (buf[0])
  6144. {
  6145. case 'T': /* Status with PC, SP, FP, ... */
  6146. /* Expedited reply, containing Signal, {regno, reg} repeat. */
  6147. /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
  6148. ss = signal number
  6149. n... = register number
  6150. r... = register contents
  6151. */
  6152. p = &buf[3]; /* after Txx */
  6153. while (*p)
  6154. {
  6155. const char *p1;
  6156. int fieldsize;
  6157. p1 = strchr (p, ':');
  6158. if (p1 == NULL)
  6159. error (_("Malformed packet(a) (missing colon): %s\n\
  6160. Packet: '%s'\n"),
  6161. p, buf);
  6162. if (p == p1)
  6163. error (_("Malformed packet(a) (missing register number): %s\n\
  6164. Packet: '%s'\n"),
  6165. p, buf);
  6166. /* Some "registers" are actually extended stop information.
  6167. Note if you're adding a new entry here: GDB 7.9 and
  6168. earlier assume that all register "numbers" that start
  6169. with an hex digit are real register numbers. Make sure
  6170. the server only sends such a packet if it knows the
  6171. client understands it. */
  6172. if (strprefix (p, p1, "thread"))
  6173. event->ptid = read_ptid (++p1, &p);
  6174. else if (strprefix (p, p1, "syscall_entry"))
  6175. {
  6176. ULONGEST sysno;
  6177. p = unpack_varlen_hex (++p1, &sysno);
  6178. event->ws.set_syscall_entry ((int) sysno);
  6179. }
  6180. else if (strprefix (p, p1, "syscall_return"))
  6181. {
  6182. ULONGEST sysno;
  6183. p = unpack_varlen_hex (++p1, &sysno);
  6184. event->ws.set_syscall_return ((int) sysno);
  6185. }
  6186. else if (strprefix (p, p1, "watch")
  6187. || strprefix (p, p1, "rwatch")
  6188. || strprefix (p, p1, "awatch"))
  6189. {
  6190. event->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
  6191. p = unpack_varlen_hex (++p1, &addr);
  6192. event->watch_data_address = (CORE_ADDR) addr;
  6193. }
  6194. else if (strprefix (p, p1, "swbreak"))
  6195. {
  6196. event->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
  6197. /* Make sure the stub doesn't forget to indicate support
  6198. with qSupported. */
  6199. if (packet_support (PACKET_swbreak_feature) != PACKET_ENABLE)
  6200. error (_("Unexpected swbreak stop reason"));
  6201. /* The value part is documented as "must be empty",
  6202. though we ignore it, in case we ever decide to make
  6203. use of it in a backward compatible way. */
  6204. p = strchrnul (p1 + 1, ';');
  6205. }
  6206. else if (strprefix (p, p1, "hwbreak"))
  6207. {
  6208. event->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
  6209. /* Make sure the stub doesn't forget to indicate support
  6210. with qSupported. */
  6211. if (packet_support (PACKET_hwbreak_feature) != PACKET_ENABLE)
  6212. error (_("Unexpected hwbreak stop reason"));
  6213. /* See above. */
  6214. p = strchrnul (p1 + 1, ';');
  6215. }
  6216. else if (strprefix (p, p1, "library"))
  6217. {
  6218. event->ws.set_loaded ();
  6219. p = strchrnul (p1 + 1, ';');
  6220. }
  6221. else if (strprefix (p, p1, "replaylog"))
  6222. {
  6223. event->ws.set_no_history ();
  6224. /* p1 will indicate "begin" or "end", but it makes
  6225. no difference for now, so ignore it. */
  6226. p = strchrnul (p1 + 1, ';');
  6227. }
  6228. else if (strprefix (p, p1, "core"))
  6229. {
  6230. ULONGEST c;
  6231. p = unpack_varlen_hex (++p1, &c);
  6232. event->core = c;
  6233. }
  6234. else if (strprefix (p, p1, "fork"))
  6235. event->ws.set_forked (read_ptid (++p1, &p));
  6236. else if (strprefix (p, p1, "vfork"))
  6237. event->ws.set_vforked (read_ptid (++p1, &p));
  6238. else if (strprefix (p, p1, "vforkdone"))
  6239. {
  6240. event->ws.set_vfork_done ();
  6241. p = strchrnul (p1 + 1, ';');
  6242. }
  6243. else if (strprefix (p, p1, "exec"))
  6244. {
  6245. ULONGEST ignored;
  6246. int pathlen;
  6247. /* Determine the length of the execd pathname. */
  6248. p = unpack_varlen_hex (++p1, &ignored);
  6249. pathlen = (p - p1) / 2;
  6250. /* Save the pathname for event reporting and for
  6251. the next run command. */
  6252. gdb::unique_xmalloc_ptr<char> pathname
  6253. ((char *) xmalloc (pathlen + 1));
  6254. hex2bin (p1, (gdb_byte *) pathname.get (), pathlen);
  6255. pathname.get ()[pathlen] = '\0';
  6256. /* This is freed during event handling. */
  6257. event->ws.set_execd (std::move (pathname));
  6258. /* Skip the registers included in this packet, since
  6259. they may be for an architecture different from the
  6260. one used by the original program. */
  6261. skipregs = 1;
  6262. }
  6263. else if (strprefix (p, p1, "create"))
  6264. {
  6265. event->ws.set_thread_created ();
  6266. p = strchrnul (p1 + 1, ';');
  6267. }
  6268. else
  6269. {
  6270. ULONGEST pnum;
  6271. const char *p_temp;
  6272. if (skipregs)
  6273. {
  6274. p = strchrnul (p1 + 1, ';');
  6275. p++;
  6276. continue;
  6277. }
  6278. /* Maybe a real ``P'' register number. */
  6279. p_temp = unpack_varlen_hex (p, &pnum);
  6280. /* If the first invalid character is the colon, we got a
  6281. register number. Otherwise, it's an unknown stop
  6282. reason. */
  6283. if (p_temp == p1)
  6284. {
  6285. /* If we haven't parsed the event's thread yet, find
  6286. it now, in order to find the architecture of the
  6287. reported expedited registers. */
  6288. if (event->ptid == null_ptid)
  6289. {
  6290. /* If there is no thread-id information then leave
  6291. the event->ptid as null_ptid. Later in
  6292. process_stop_reply we will pick a suitable
  6293. thread. */
  6294. const char *thr = strstr (p1 + 1, ";thread:");
  6295. if (thr != NULL)
  6296. event->ptid = read_ptid (thr + strlen (";thread:"),
  6297. NULL);
  6298. }
  6299. if (rsa == NULL)
  6300. {
  6301. inferior *inf
  6302. = (event->ptid == null_ptid
  6303. ? NULL
  6304. : find_inferior_ptid (this, event->ptid));
  6305. /* If this is the first time we learn anything
  6306. about this process, skip the registers
  6307. included in this packet, since we don't yet
  6308. know which architecture to use to parse them.
  6309. We'll determine the architecture later when
  6310. we process the stop reply and retrieve the
  6311. target description, via
  6312. remote_notice_new_inferior ->
  6313. post_create_inferior. */
  6314. if (inf == NULL)
  6315. {
  6316. p = strchrnul (p1 + 1, ';');
  6317. p++;
  6318. continue;
  6319. }
  6320. event->arch = inf->gdbarch;
  6321. rsa = event->rs->get_remote_arch_state (event->arch);
  6322. }
  6323. packet_reg *reg
  6324. = packet_reg_from_pnum (event->arch, rsa, pnum);
  6325. cached_reg_t cached_reg;
  6326. if (reg == NULL)
  6327. error (_("Remote sent bad register number %s: %s\n\
  6328. Packet: '%s'\n"),
  6329. hex_string (pnum), p, buf);
  6330. cached_reg.num = reg->regnum;
  6331. cached_reg.data = (gdb_byte *)
  6332. xmalloc (register_size (event->arch, reg->regnum));
  6333. p = p1 + 1;
  6334. fieldsize = hex2bin (p, cached_reg.data,
  6335. register_size (event->arch, reg->regnum));
  6336. p += 2 * fieldsize;
  6337. if (fieldsize < register_size (event->arch, reg->regnum))
  6338. warning (_("Remote reply is too short: %s"), buf);
  6339. event->regcache.push_back (cached_reg);
  6340. }
  6341. else
  6342. {
  6343. /* Not a number. Silently skip unknown optional
  6344. info. */
  6345. p = strchrnul (p1 + 1, ';');
  6346. }
  6347. }
  6348. if (*p != ';')
  6349. error (_("Remote register badly formatted: %s\nhere: %s"),
  6350. buf, p);
  6351. ++p;
  6352. }
  6353. if (event->ws.kind () != TARGET_WAITKIND_IGNORE)
  6354. break;
  6355. /* fall through */
  6356. case 'S': /* Old style status, just signal only. */
  6357. {
  6358. int sig;
  6359. sig = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
  6360. if (GDB_SIGNAL_FIRST <= sig && sig < GDB_SIGNAL_LAST)
  6361. event->ws.set_stopped ((enum gdb_signal) sig);
  6362. else
  6363. event->ws.set_stopped (GDB_SIGNAL_UNKNOWN);
  6364. }
  6365. break;
  6366. case 'w': /* Thread exited. */
  6367. {
  6368. ULONGEST value;
  6369. p = unpack_varlen_hex (&buf[1], &value);
  6370. event->ws.set_thread_exited (value);
  6371. if (*p != ';')
  6372. error (_("stop reply packet badly formatted: %s"), buf);
  6373. event->ptid = read_ptid (++p, NULL);
  6374. break;
  6375. }
  6376. case 'W': /* Target exited. */
  6377. case 'X':
  6378. {
  6379. ULONGEST value;
  6380. /* GDB used to accept only 2 hex chars here. Stubs should
  6381. only send more if they detect GDB supports multi-process
  6382. support. */
  6383. p = unpack_varlen_hex (&buf[1], &value);
  6384. if (buf[0] == 'W')
  6385. {
  6386. /* The remote process exited. */
  6387. event->ws.set_exited (value);
  6388. }
  6389. else
  6390. {
  6391. /* The remote process exited with a signal. */
  6392. if (GDB_SIGNAL_FIRST <= value && value < GDB_SIGNAL_LAST)
  6393. event->ws.set_signalled ((enum gdb_signal) value);
  6394. else
  6395. event->ws.set_signalled (GDB_SIGNAL_UNKNOWN);
  6396. }
  6397. /* If no process is specified, return null_ptid, and let the
  6398. caller figure out the right process to use. */
  6399. int pid = 0;
  6400. if (*p == '\0')
  6401. ;
  6402. else if (*p == ';')
  6403. {
  6404. p++;
  6405. if (*p == '\0')
  6406. ;
  6407. else if (startswith (p, "process:"))
  6408. {
  6409. ULONGEST upid;
  6410. p += sizeof ("process:") - 1;
  6411. unpack_varlen_hex (p, &upid);
  6412. pid = upid;
  6413. }
  6414. else
  6415. error (_("unknown stop reply packet: %s"), buf);
  6416. }
  6417. else
  6418. error (_("unknown stop reply packet: %s"), buf);
  6419. event->ptid = ptid_t (pid);
  6420. }
  6421. break;
  6422. case 'N':
  6423. event->ws.set_no_resumed ();
  6424. event->ptid = minus_one_ptid;
  6425. break;
  6426. }
  6427. }
  6428. /* When the stub wants to tell GDB about a new notification reply, it
  6429. sends a notification (%Stop, for example). Those can come it at
  6430. any time, hence, we have to make sure that any pending
  6431. putpkt/getpkt sequence we're making is finished, before querying
  6432. the stub for more events with the corresponding ack command
  6433. (vStopped, for example). E.g., if we started a vStopped sequence
  6434. immediately upon receiving the notification, something like this
  6435. could happen:
  6436. 1.1) --> Hg 1
  6437. 1.2) <-- OK
  6438. 1.3) --> g
  6439. 1.4) <-- %Stop
  6440. 1.5) --> vStopped
  6441. 1.6) <-- (registers reply to step #1.3)
  6442. Obviously, the reply in step #1.6 would be unexpected to a vStopped
  6443. query.
  6444. To solve this, whenever we parse a %Stop notification successfully,
  6445. we mark the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN, and carry on
  6446. doing whatever we were doing:
  6447. 2.1) --> Hg 1
  6448. 2.2) <-- OK
  6449. 2.3) --> g
  6450. 2.4) <-- %Stop
  6451. <GDB marks the REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN>
  6452. 2.5) <-- (registers reply to step #2.3)
  6453. Eventually after step #2.5, we return to the event loop, which
  6454. notices there's an event on the
  6455. REMOTE_ASYNC_GET_PENDING_EVENTS_TOKEN event and calls the
  6456. associated callback --- the function below. At this point, we're
  6457. always safe to start a vStopped sequence. :
  6458. 2.6) --> vStopped
  6459. 2.7) <-- T05 thread:2
  6460. 2.8) --> vStopped
  6461. 2.9) --> OK
  6462. */
  6463. void
  6464. remote_target::remote_notif_get_pending_events (notif_client *nc)
  6465. {
  6466. struct remote_state *rs = get_remote_state ();
  6467. if (rs->notif_state->pending_event[nc->id] != NULL)
  6468. {
  6469. if (notif_debug)
  6470. gdb_printf (gdb_stdlog,
  6471. "notif: process: '%s' ack pending event\n",
  6472. nc->name);
  6473. /* acknowledge */
  6474. nc->ack (this, nc, rs->buf.data (),
  6475. rs->notif_state->pending_event[nc->id]);
  6476. rs->notif_state->pending_event[nc->id] = NULL;
  6477. while (1)
  6478. {
  6479. getpkt (&rs->buf, 0);
  6480. if (strcmp (rs->buf.data (), "OK") == 0)
  6481. break;
  6482. else
  6483. remote_notif_ack (this, nc, rs->buf.data ());
  6484. }
  6485. }
  6486. else
  6487. {
  6488. if (notif_debug)
  6489. gdb_printf (gdb_stdlog,
  6490. "notif: process: '%s' no pending reply\n",
  6491. nc->name);
  6492. }
  6493. }
  6494. /* Wrapper around remote_target::remote_notif_get_pending_events to
  6495. avoid having to export the whole remote_target class. */
  6496. void
  6497. remote_notif_get_pending_events (remote_target *remote, notif_client *nc)
  6498. {
  6499. remote->remote_notif_get_pending_events (nc);
  6500. }
  6501. /* Called from process_stop_reply when the stop packet we are responding
  6502. to didn't include a process-id or thread-id. STATUS is the stop event
  6503. we are responding to.
  6504. It is the task of this function to select a suitable thread (or process)
  6505. and return its ptid, this is the thread (or process) we will assume the
  6506. stop event came from.
  6507. In some cases there isn't really any choice about which thread (or
  6508. process) is selected, a basic remote with a single process containing a
  6509. single thread might choose not to send any process-id or thread-id in
  6510. its stop packets, this function will select and return the one and only
  6511. thread.
  6512. However, if a target supports multiple threads (or processes) and still
  6513. doesn't include a thread-id (or process-id) in its stop packet then
  6514. first, this is a badly behaving target, and second, we're going to have
  6515. to select a thread (or process) at random and use that. This function
  6516. will print a warning to the user if it detects that there is the
  6517. possibility that GDB is guessing which thread (or process) to
  6518. report.
  6519. Note that this is called before GDB fetches the updated thread list from the
  6520. target. So it's possible for the stop reply to be ambiguous and for GDB to
  6521. not realize it. For example, if there's initially one thread, the target
  6522. spawns a second thread, and then sends a stop reply without an id that
  6523. concerns the first thread. GDB will assume the stop reply is about the
  6524. first thread - the only thread it knows about - without printing a warning.
  6525. Anyway, if the remote meant for the stop reply to be about the second thread,
  6526. then it would be really broken, because GDB doesn't know about that thread
  6527. yet. */
  6528. ptid_t
  6529. remote_target::select_thread_for_ambiguous_stop_reply
  6530. (const target_waitstatus &status)
  6531. {
  6532. REMOTE_SCOPED_DEBUG_ENTER_EXIT;
  6533. /* Some stop events apply to all threads in an inferior, while others
  6534. only apply to a single thread. */
  6535. bool process_wide_stop
  6536. = (status.kind () == TARGET_WAITKIND_EXITED
  6537. || status.kind () == TARGET_WAITKIND_SIGNALLED);
  6538. remote_debug_printf ("process_wide_stop = %d", process_wide_stop);
  6539. thread_info *first_resumed_thread = nullptr;
  6540. bool ambiguous = false;
  6541. /* Consider all non-exited threads of the target, find the first resumed
  6542. one. */
  6543. for (thread_info *thr : all_non_exited_threads (this))
  6544. {
  6545. remote_thread_info *remote_thr = get_remote_thread_info (thr);
  6546. if (remote_thr->get_resume_state () != resume_state::RESUMED)
  6547. continue;
  6548. if (first_resumed_thread == nullptr)
  6549. first_resumed_thread = thr;
  6550. else if (!process_wide_stop
  6551. || first_resumed_thread->ptid.pid () != thr->ptid.pid ())
  6552. ambiguous = true;
  6553. }
  6554. gdb_assert (first_resumed_thread != nullptr);
  6555. remote_debug_printf ("first resumed thread is %s",
  6556. pid_to_str (first_resumed_thread->ptid).c_str ());
  6557. remote_debug_printf ("is this guess ambiguous? = %d", ambiguous);
  6558. /* Warn if the remote target is sending ambiguous stop replies. */
  6559. if (ambiguous)
  6560. {
  6561. static bool warned = false;
  6562. if (!warned)
  6563. {
  6564. /* If you are seeing this warning then the remote target has
  6565. stopped without specifying a thread-id, but the target
  6566. does have multiple threads (or inferiors), and so GDB is
  6567. having to guess which thread stopped.
  6568. Examples of what might cause this are the target sending
  6569. and 'S' stop packet, or a 'T' stop packet and not
  6570. including a thread-id.
  6571. Additionally, the target might send a 'W' or 'X packet
  6572. without including a process-id, when the target has
  6573. multiple running inferiors. */
  6574. if (process_wide_stop)
  6575. warning (_("multi-inferior target stopped without "
  6576. "sending a process-id, using first "
  6577. "non-exited inferior"));
  6578. else
  6579. warning (_("multi-threaded target stopped without "
  6580. "sending a thread-id, using first "
  6581. "non-exited thread"));
  6582. warned = true;
  6583. }
  6584. }
  6585. /* If this is a stop for all threads then don't use a particular threads
  6586. ptid, instead create a new ptid where only the pid field is set. */
  6587. if (process_wide_stop)
  6588. return ptid_t (first_resumed_thread->ptid.pid ());
  6589. else
  6590. return first_resumed_thread->ptid;
  6591. }
  6592. /* Called when it is decided that STOP_REPLY holds the info of the
  6593. event that is to be returned to the core. This function always
  6594. destroys STOP_REPLY. */
  6595. ptid_t
  6596. remote_target::process_stop_reply (struct stop_reply *stop_reply,
  6597. struct target_waitstatus *status)
  6598. {
  6599. *status = stop_reply->ws;
  6600. ptid_t ptid = stop_reply->ptid;
  6601. /* If no thread/process was reported by the stub then select a suitable
  6602. thread/process. */
  6603. if (ptid == null_ptid)
  6604. ptid = select_thread_for_ambiguous_stop_reply (*status);
  6605. gdb_assert (ptid != null_ptid);
  6606. if (status->kind () != TARGET_WAITKIND_EXITED
  6607. && status->kind () != TARGET_WAITKIND_SIGNALLED
  6608. && status->kind () != TARGET_WAITKIND_NO_RESUMED)
  6609. {
  6610. /* Expedited registers. */
  6611. if (!stop_reply->regcache.empty ())
  6612. {
  6613. struct regcache *regcache
  6614. = get_thread_arch_regcache (this, ptid, stop_reply->arch);
  6615. for (cached_reg_t &reg : stop_reply->regcache)
  6616. {
  6617. regcache->raw_supply (reg.num, reg.data);
  6618. xfree (reg.data);
  6619. }
  6620. stop_reply->regcache.clear ();
  6621. }
  6622. remote_notice_new_inferior (ptid, false);
  6623. remote_thread_info *remote_thr = get_remote_thread_info (this, ptid);
  6624. remote_thr->core = stop_reply->core;
  6625. remote_thr->stop_reason = stop_reply->stop_reason;
  6626. remote_thr->watch_data_address = stop_reply->watch_data_address;
  6627. if (target_is_non_stop_p ())
  6628. {
  6629. /* If the target works in non-stop mode, a stop-reply indicates that
  6630. only this thread stopped. */
  6631. remote_thr->set_not_resumed ();
  6632. }
  6633. else
  6634. {
  6635. /* If the target works in all-stop mode, a stop-reply indicates that
  6636. all the target's threads stopped. */
  6637. for (thread_info *tp : all_non_exited_threads (this))
  6638. get_remote_thread_info (tp)->set_not_resumed ();
  6639. }
  6640. }
  6641. delete stop_reply;
  6642. return ptid;
  6643. }
  6644. /* The non-stop mode version of target_wait. */
  6645. ptid_t
  6646. remote_target::wait_ns (ptid_t ptid, struct target_waitstatus *status,
  6647. target_wait_flags options)
  6648. {
  6649. struct remote_state *rs = get_remote_state ();
  6650. struct stop_reply *stop_reply;
  6651. int ret;
  6652. int is_notif = 0;
  6653. /* If in non-stop mode, get out of getpkt even if a
  6654. notification is received. */
  6655. ret = getpkt_or_notif_sane (&rs->buf, 0 /* forever */, &is_notif);
  6656. while (1)
  6657. {
  6658. if (ret != -1 && !is_notif)
  6659. switch (rs->buf[0])
  6660. {
  6661. case 'E': /* Error of some sort. */
  6662. /* We're out of sync with the target now. Did it continue
  6663. or not? We can't tell which thread it was in non-stop,
  6664. so just ignore this. */
  6665. warning (_("Remote failure reply: %s"), rs->buf.data ());
  6666. break;
  6667. case 'O': /* Console output. */
  6668. remote_console_output (&rs->buf[1]);
  6669. break;
  6670. default:
  6671. warning (_("Invalid remote reply: %s"), rs->buf.data ());
  6672. break;
  6673. }
  6674. /* Acknowledge a pending stop reply that may have arrived in the
  6675. mean time. */
  6676. if (rs->notif_state->pending_event[notif_client_stop.id] != NULL)
  6677. remote_notif_get_pending_events (&notif_client_stop);
  6678. /* If indeed we noticed a stop reply, we're done. */
  6679. stop_reply = queued_stop_reply (ptid);
  6680. if (stop_reply != NULL)
  6681. return process_stop_reply (stop_reply, status);
  6682. /* Still no event. If we're just polling for an event, then
  6683. return to the event loop. */
  6684. if (options & TARGET_WNOHANG)
  6685. {
  6686. status->set_ignore ();
  6687. return minus_one_ptid;
  6688. }
  6689. /* Otherwise do a blocking wait. */
  6690. ret = getpkt_or_notif_sane (&rs->buf, 1 /* forever */, &is_notif);
  6691. }
  6692. }
  6693. /* Return the first resumed thread. */
  6694. static ptid_t
  6695. first_remote_resumed_thread (remote_target *target)
  6696. {
  6697. for (thread_info *tp : all_non_exited_threads (target, minus_one_ptid))
  6698. if (tp->resumed ())
  6699. return tp->ptid;
  6700. return null_ptid;
  6701. }
  6702. /* Wait until the remote machine stops, then return, storing status in
  6703. STATUS just as `wait' would. */
  6704. ptid_t
  6705. remote_target::wait_as (ptid_t ptid, target_waitstatus *status,
  6706. target_wait_flags options)
  6707. {
  6708. struct remote_state *rs = get_remote_state ();
  6709. ptid_t event_ptid = null_ptid;
  6710. char *buf;
  6711. struct stop_reply *stop_reply;
  6712. again:
  6713. status->set_ignore ();
  6714. stop_reply = queued_stop_reply (ptid);
  6715. if (stop_reply != NULL)
  6716. {
  6717. /* None of the paths that push a stop reply onto the queue should
  6718. have set the waiting_for_stop_reply flag. */
  6719. gdb_assert (!rs->waiting_for_stop_reply);
  6720. event_ptid = process_stop_reply (stop_reply, status);
  6721. }
  6722. else
  6723. {
  6724. int forever = ((options & TARGET_WNOHANG) == 0
  6725. && rs->wait_forever_enabled_p);
  6726. if (!rs->waiting_for_stop_reply)
  6727. {
  6728. status->set_no_resumed ();
  6729. return minus_one_ptid;
  6730. }
  6731. /* FIXME: cagney/1999-09-27: If we're in async mode we should
  6732. _never_ wait for ever -> test on target_is_async_p().
  6733. However, before we do that we need to ensure that the caller
  6734. knows how to take the target into/out of async mode. */
  6735. int is_notif;
  6736. int ret = getpkt_or_notif_sane (&rs->buf, forever, &is_notif);
  6737. /* GDB gets a notification. Return to core as this event is
  6738. not interesting. */
  6739. if (ret != -1 && is_notif)
  6740. return minus_one_ptid;
  6741. if (ret == -1 && (options & TARGET_WNOHANG) != 0)
  6742. return minus_one_ptid;
  6743. buf = rs->buf.data ();
  6744. /* Assume that the target has acknowledged Ctrl-C unless we receive
  6745. an 'F' or 'O' packet. */
  6746. if (buf[0] != 'F' && buf[0] != 'O')
  6747. rs->ctrlc_pending_p = 0;
  6748. switch (buf[0])
  6749. {
  6750. case 'E': /* Error of some sort. */
  6751. /* We're out of sync with the target now. Did it continue or
  6752. not? Not is more likely, so report a stop. */
  6753. rs->waiting_for_stop_reply = 0;
  6754. warning (_("Remote failure reply: %s"), buf);
  6755. status->set_stopped (GDB_SIGNAL_0);
  6756. break;
  6757. case 'F': /* File-I/O request. */
  6758. /* GDB may access the inferior memory while handling the File-I/O
  6759. request, but we don't want GDB accessing memory while waiting
  6760. for a stop reply. See the comments in putpkt_binary. Set
  6761. waiting_for_stop_reply to 0 temporarily. */
  6762. rs->waiting_for_stop_reply = 0;
  6763. remote_fileio_request (this, buf, rs->ctrlc_pending_p);
  6764. rs->ctrlc_pending_p = 0;
  6765. /* GDB handled the File-I/O request, and the target is running
  6766. again. Keep waiting for events. */
  6767. rs->waiting_for_stop_reply = 1;
  6768. break;
  6769. case 'N': case 'T': case 'S': case 'X': case 'W':
  6770. {
  6771. /* There is a stop reply to handle. */
  6772. rs->waiting_for_stop_reply = 0;
  6773. stop_reply
  6774. = (struct stop_reply *) remote_notif_parse (this,
  6775. &notif_client_stop,
  6776. rs->buf.data ());
  6777. event_ptid = process_stop_reply (stop_reply, status);
  6778. break;
  6779. }
  6780. case 'O': /* Console output. */
  6781. remote_console_output (buf + 1);
  6782. break;
  6783. case '\0':
  6784. if (rs->last_sent_signal != GDB_SIGNAL_0)
  6785. {
  6786. /* Zero length reply means that we tried 'S' or 'C' and the
  6787. remote system doesn't support it. */
  6788. target_terminal::ours_for_output ();
  6789. gdb_printf
  6790. ("Can't send signals to this remote system. %s not sent.\n",
  6791. gdb_signal_to_name (rs->last_sent_signal));
  6792. rs->last_sent_signal = GDB_SIGNAL_0;
  6793. target_terminal::inferior ();
  6794. strcpy (buf, rs->last_sent_step ? "s" : "c");
  6795. putpkt (buf);
  6796. break;
  6797. }
  6798. /* fallthrough */
  6799. default:
  6800. warning (_("Invalid remote reply: %s"), buf);
  6801. break;
  6802. }
  6803. }
  6804. if (status->kind () == TARGET_WAITKIND_NO_RESUMED)
  6805. return minus_one_ptid;
  6806. else if (status->kind () == TARGET_WAITKIND_IGNORE)
  6807. {
  6808. /* Nothing interesting happened. If we're doing a non-blocking
  6809. poll, we're done. Otherwise, go back to waiting. */
  6810. if (options & TARGET_WNOHANG)
  6811. return minus_one_ptid;
  6812. else
  6813. goto again;
  6814. }
  6815. else if (status->kind () != TARGET_WAITKIND_EXITED
  6816. && status->kind () != TARGET_WAITKIND_SIGNALLED)
  6817. {
  6818. if (event_ptid != null_ptid)
  6819. record_currthread (rs, event_ptid);
  6820. else
  6821. event_ptid = first_remote_resumed_thread (this);
  6822. }
  6823. else
  6824. {
  6825. /* A process exit. Invalidate our notion of current thread. */
  6826. record_currthread (rs, minus_one_ptid);
  6827. /* It's possible that the packet did not include a pid. */
  6828. if (event_ptid == null_ptid)
  6829. event_ptid = first_remote_resumed_thread (this);
  6830. /* EVENT_PTID could still be NULL_PTID. Double-check. */
  6831. if (event_ptid == null_ptid)
  6832. event_ptid = magic_null_ptid;
  6833. }
  6834. return event_ptid;
  6835. }
  6836. /* Wait until the remote machine stops, then return, storing status in
  6837. STATUS just as `wait' would. */
  6838. ptid_t
  6839. remote_target::wait (ptid_t ptid, struct target_waitstatus *status,
  6840. target_wait_flags options)
  6841. {
  6842. REMOTE_SCOPED_DEBUG_ENTER_EXIT;
  6843. remote_state *rs = get_remote_state ();
  6844. /* Start by clearing the flag that asks for our wait method to be called,
  6845. we'll mark it again at the end if needed. If the target is not in
  6846. async mode then the async token should not be marked. */
  6847. if (target_is_async_p ())
  6848. clear_async_event_handler (rs->remote_async_inferior_event_token);
  6849. else
  6850. gdb_assert (!async_event_handler_marked
  6851. (rs->remote_async_inferior_event_token));
  6852. ptid_t event_ptid;
  6853. if (target_is_non_stop_p ())
  6854. event_ptid = wait_ns (ptid, status, options);
  6855. else
  6856. event_ptid = wait_as (ptid, status, options);
  6857. if (target_is_async_p ())
  6858. {
  6859. /* If there are events left in the queue, or unacknowledged
  6860. notifications, then tell the event loop to call us again. */
  6861. if (!rs->stop_reply_queue.empty ()
  6862. || rs->notif_state->pending_event[notif_client_stop.id] != nullptr)
  6863. mark_async_event_handler (rs->remote_async_inferior_event_token);
  6864. }
  6865. return event_ptid;
  6866. }
  6867. /* Fetch a single register using a 'p' packet. */
  6868. int
  6869. remote_target::fetch_register_using_p (struct regcache *regcache,
  6870. packet_reg *reg)
  6871. {
  6872. struct gdbarch *gdbarch = regcache->arch ();
  6873. struct remote_state *rs = get_remote_state ();
  6874. char *buf, *p;
  6875. gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
  6876. int i;
  6877. if (packet_support (PACKET_p) == PACKET_DISABLE)
  6878. return 0;
  6879. if (reg->pnum == -1)
  6880. return 0;
  6881. p = rs->buf.data ();
  6882. *p++ = 'p';
  6883. p += hexnumstr (p, reg->pnum);
  6884. *p++ = '\0';
  6885. putpkt (rs->buf);
  6886. getpkt (&rs->buf, 0);
  6887. buf = rs->buf.data ();
  6888. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_p]))
  6889. {
  6890. case PACKET_OK:
  6891. break;
  6892. case PACKET_UNKNOWN:
  6893. return 0;
  6894. case PACKET_ERROR:
  6895. error (_("Could not fetch register \"%s\"; remote failure reply '%s'"),
  6896. gdbarch_register_name (regcache->arch (),
  6897. reg->regnum),
  6898. buf);
  6899. }
  6900. /* If this register is unfetchable, tell the regcache. */
  6901. if (buf[0] == 'x')
  6902. {
  6903. regcache->raw_supply (reg->regnum, NULL);
  6904. return 1;
  6905. }
  6906. /* Otherwise, parse and supply the value. */
  6907. p = buf;
  6908. i = 0;
  6909. while (p[0] != 0)
  6910. {
  6911. if (p[1] == 0)
  6912. error (_("fetch_register_using_p: early buf termination"));
  6913. regp[i++] = fromhex (p[0]) * 16 + fromhex (p[1]);
  6914. p += 2;
  6915. }
  6916. regcache->raw_supply (reg->regnum, regp);
  6917. return 1;
  6918. }
  6919. /* Fetch the registers included in the target's 'g' packet. */
  6920. int
  6921. remote_target::send_g_packet ()
  6922. {
  6923. struct remote_state *rs = get_remote_state ();
  6924. int buf_len;
  6925. xsnprintf (rs->buf.data (), get_remote_packet_size (), "g");
  6926. putpkt (rs->buf);
  6927. getpkt (&rs->buf, 0);
  6928. if (packet_check_result (rs->buf) == PACKET_ERROR)
  6929. error (_("Could not read registers; remote failure reply '%s'"),
  6930. rs->buf.data ());
  6931. /* We can get out of synch in various cases. If the first character
  6932. in the buffer is not a hex character, assume that has happened
  6933. and try to fetch another packet to read. */
  6934. while ((rs->buf[0] < '0' || rs->buf[0] > '9')
  6935. && (rs->buf[0] < 'A' || rs->buf[0] > 'F')
  6936. && (rs->buf[0] < 'a' || rs->buf[0] > 'f')
  6937. && rs->buf[0] != 'x') /* New: unavailable register value. */
  6938. {
  6939. remote_debug_printf ("Bad register packet; fetching a new packet");
  6940. getpkt (&rs->buf, 0);
  6941. }
  6942. buf_len = strlen (rs->buf.data ());
  6943. /* Sanity check the received packet. */
  6944. if (buf_len % 2 != 0)
  6945. error (_("Remote 'g' packet reply is of odd length: %s"), rs->buf.data ());
  6946. return buf_len / 2;
  6947. }
  6948. void
  6949. remote_target::process_g_packet (struct regcache *regcache)
  6950. {
  6951. struct gdbarch *gdbarch = regcache->arch ();
  6952. struct remote_state *rs = get_remote_state ();
  6953. remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
  6954. int i, buf_len;
  6955. char *p;
  6956. char *regs;
  6957. buf_len = strlen (rs->buf.data ());
  6958. /* Further sanity checks, with knowledge of the architecture. */
  6959. if (buf_len > 2 * rsa->sizeof_g_packet)
  6960. error (_("Remote 'g' packet reply is too long (expected %ld bytes, got %d "
  6961. "bytes): %s"),
  6962. rsa->sizeof_g_packet, buf_len / 2,
  6963. rs->buf.data ());
  6964. /* Save the size of the packet sent to us by the target. It is used
  6965. as a heuristic when determining the max size of packets that the
  6966. target can safely receive. */
  6967. if (rsa->actual_register_packet_size == 0)
  6968. rsa->actual_register_packet_size = buf_len;
  6969. /* If this is smaller than we guessed the 'g' packet would be,
  6970. update our records. A 'g' reply that doesn't include a register's
  6971. value implies either that the register is not available, or that
  6972. the 'p' packet must be used. */
  6973. if (buf_len < 2 * rsa->sizeof_g_packet)
  6974. {
  6975. long sizeof_g_packet = buf_len / 2;
  6976. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  6977. {
  6978. long offset = rsa->regs[i].offset;
  6979. long reg_size = register_size (gdbarch, i);
  6980. if (rsa->regs[i].pnum == -1)
  6981. continue;
  6982. if (offset >= sizeof_g_packet)
  6983. rsa->regs[i].in_g_packet = 0;
  6984. else if (offset + reg_size > sizeof_g_packet)
  6985. error (_("Truncated register %d in remote 'g' packet"), i);
  6986. else
  6987. rsa->regs[i].in_g_packet = 1;
  6988. }
  6989. /* Looks valid enough, we can assume this is the correct length
  6990. for a 'g' packet. It's important not to adjust
  6991. rsa->sizeof_g_packet if we have truncated registers otherwise
  6992. this "if" won't be run the next time the method is called
  6993. with a packet of the same size and one of the internal errors
  6994. below will trigger instead. */
  6995. rsa->sizeof_g_packet = sizeof_g_packet;
  6996. }
  6997. regs = (char *) alloca (rsa->sizeof_g_packet);
  6998. /* Unimplemented registers read as all bits zero. */
  6999. memset (regs, 0, rsa->sizeof_g_packet);
  7000. /* Reply describes registers byte by byte, each byte encoded as two
  7001. hex characters. Suck them all up, then supply them to the
  7002. register cacheing/storage mechanism. */
  7003. p = rs->buf.data ();
  7004. for (i = 0; i < rsa->sizeof_g_packet; i++)
  7005. {
  7006. if (p[0] == 0 || p[1] == 0)
  7007. /* This shouldn't happen - we adjusted sizeof_g_packet above. */
  7008. internal_error (__FILE__, __LINE__,
  7009. _("unexpected end of 'g' packet reply"));
  7010. if (p[0] == 'x' && p[1] == 'x')
  7011. regs[i] = 0; /* 'x' */
  7012. else
  7013. regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  7014. p += 2;
  7015. }
  7016. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  7017. {
  7018. struct packet_reg *r = &rsa->regs[i];
  7019. long reg_size = register_size (gdbarch, i);
  7020. if (r->in_g_packet)
  7021. {
  7022. if ((r->offset + reg_size) * 2 > strlen (rs->buf.data ()))
  7023. /* This shouldn't happen - we adjusted in_g_packet above. */
  7024. internal_error (__FILE__, __LINE__,
  7025. _("unexpected end of 'g' packet reply"));
  7026. else if (rs->buf[r->offset * 2] == 'x')
  7027. {
  7028. gdb_assert (r->offset * 2 < strlen (rs->buf.data ()));
  7029. /* The register isn't available, mark it as such (at
  7030. the same time setting the value to zero). */
  7031. regcache->raw_supply (r->regnum, NULL);
  7032. }
  7033. else
  7034. regcache->raw_supply (r->regnum, regs + r->offset);
  7035. }
  7036. }
  7037. }
  7038. void
  7039. remote_target::fetch_registers_using_g (struct regcache *regcache)
  7040. {
  7041. send_g_packet ();
  7042. process_g_packet (regcache);
  7043. }
  7044. /* Make the remote selected traceframe match GDB's selected
  7045. traceframe. */
  7046. void
  7047. remote_target::set_remote_traceframe ()
  7048. {
  7049. int newnum;
  7050. struct remote_state *rs = get_remote_state ();
  7051. if (rs->remote_traceframe_number == get_traceframe_number ())
  7052. return;
  7053. /* Avoid recursion, remote_trace_find calls us again. */
  7054. rs->remote_traceframe_number = get_traceframe_number ();
  7055. newnum = target_trace_find (tfind_number,
  7056. get_traceframe_number (), 0, 0, NULL);
  7057. /* Should not happen. If it does, all bets are off. */
  7058. if (newnum != get_traceframe_number ())
  7059. warning (_("could not set remote traceframe"));
  7060. }
  7061. void
  7062. remote_target::fetch_registers (struct regcache *regcache, int regnum)
  7063. {
  7064. struct gdbarch *gdbarch = regcache->arch ();
  7065. struct remote_state *rs = get_remote_state ();
  7066. remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
  7067. int i;
  7068. set_remote_traceframe ();
  7069. set_general_thread (regcache->ptid ());
  7070. if (regnum >= 0)
  7071. {
  7072. packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
  7073. gdb_assert (reg != NULL);
  7074. /* If this register might be in the 'g' packet, try that first -
  7075. we are likely to read more than one register. If this is the
  7076. first 'g' packet, we might be overly optimistic about its
  7077. contents, so fall back to 'p'. */
  7078. if (reg->in_g_packet)
  7079. {
  7080. fetch_registers_using_g (regcache);
  7081. if (reg->in_g_packet)
  7082. return;
  7083. }
  7084. if (fetch_register_using_p (regcache, reg))
  7085. return;
  7086. /* This register is not available. */
  7087. regcache->raw_supply (reg->regnum, NULL);
  7088. return;
  7089. }
  7090. fetch_registers_using_g (regcache);
  7091. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  7092. if (!rsa->regs[i].in_g_packet)
  7093. if (!fetch_register_using_p (regcache, &rsa->regs[i]))
  7094. {
  7095. /* This register is not available. */
  7096. regcache->raw_supply (i, NULL);
  7097. }
  7098. }
  7099. /* Prepare to store registers. Since we may send them all (using a
  7100. 'G' request), we have to read out the ones we don't want to change
  7101. first. */
  7102. void
  7103. remote_target::prepare_to_store (struct regcache *regcache)
  7104. {
  7105. struct remote_state *rs = get_remote_state ();
  7106. remote_arch_state *rsa = rs->get_remote_arch_state (regcache->arch ());
  7107. int i;
  7108. /* Make sure the entire registers array is valid. */
  7109. switch (packet_support (PACKET_P))
  7110. {
  7111. case PACKET_DISABLE:
  7112. case PACKET_SUPPORT_UNKNOWN:
  7113. /* Make sure all the necessary registers are cached. */
  7114. for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
  7115. if (rsa->regs[i].in_g_packet)
  7116. regcache->raw_update (rsa->regs[i].regnum);
  7117. break;
  7118. case PACKET_ENABLE:
  7119. break;
  7120. }
  7121. }
  7122. /* Helper: Attempt to store REGNUM using the P packet. Return fail IFF
  7123. packet was not recognized. */
  7124. int
  7125. remote_target::store_register_using_P (const struct regcache *regcache,
  7126. packet_reg *reg)
  7127. {
  7128. struct gdbarch *gdbarch = regcache->arch ();
  7129. struct remote_state *rs = get_remote_state ();
  7130. /* Try storing a single register. */
  7131. char *buf = rs->buf.data ();
  7132. gdb_byte *regp = (gdb_byte *) alloca (register_size (gdbarch, reg->regnum));
  7133. char *p;
  7134. if (packet_support (PACKET_P) == PACKET_DISABLE)
  7135. return 0;
  7136. if (reg->pnum == -1)
  7137. return 0;
  7138. xsnprintf (buf, get_remote_packet_size (), "P%s=", phex_nz (reg->pnum, 0));
  7139. p = buf + strlen (buf);
  7140. regcache->raw_collect (reg->regnum, regp);
  7141. bin2hex (regp, p, register_size (gdbarch, reg->regnum));
  7142. putpkt (rs->buf);
  7143. getpkt (&rs->buf, 0);
  7144. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_P]))
  7145. {
  7146. case PACKET_OK:
  7147. return 1;
  7148. case PACKET_ERROR:
  7149. error (_("Could not write register \"%s\"; remote failure reply '%s'"),
  7150. gdbarch_register_name (gdbarch, reg->regnum), rs->buf.data ());
  7151. case PACKET_UNKNOWN:
  7152. return 0;
  7153. default:
  7154. internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
  7155. }
  7156. }
  7157. /* Store register REGNUM, or all registers if REGNUM == -1, from the
  7158. contents of the register cache buffer. FIXME: ignores errors. */
  7159. void
  7160. remote_target::store_registers_using_G (const struct regcache *regcache)
  7161. {
  7162. struct remote_state *rs = get_remote_state ();
  7163. remote_arch_state *rsa = rs->get_remote_arch_state (regcache->arch ());
  7164. gdb_byte *regs;
  7165. char *p;
  7166. /* Extract all the registers in the regcache copying them into a
  7167. local buffer. */
  7168. {
  7169. int i;
  7170. regs = (gdb_byte *) alloca (rsa->sizeof_g_packet);
  7171. memset (regs, 0, rsa->sizeof_g_packet);
  7172. for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
  7173. {
  7174. struct packet_reg *r = &rsa->regs[i];
  7175. if (r->in_g_packet)
  7176. regcache->raw_collect (r->regnum, regs + r->offset);
  7177. }
  7178. }
  7179. /* Command describes registers byte by byte,
  7180. each byte encoded as two hex characters. */
  7181. p = rs->buf.data ();
  7182. *p++ = 'G';
  7183. bin2hex (regs, p, rsa->sizeof_g_packet);
  7184. putpkt (rs->buf);
  7185. getpkt (&rs->buf, 0);
  7186. if (packet_check_result (rs->buf) == PACKET_ERROR)
  7187. error (_("Could not write registers; remote failure reply '%s'"),
  7188. rs->buf.data ());
  7189. }
  7190. /* Store register REGNUM, or all registers if REGNUM == -1, from the contents
  7191. of the register cache buffer. FIXME: ignores errors. */
  7192. void
  7193. remote_target::store_registers (struct regcache *regcache, int regnum)
  7194. {
  7195. struct gdbarch *gdbarch = regcache->arch ();
  7196. struct remote_state *rs = get_remote_state ();
  7197. remote_arch_state *rsa = rs->get_remote_arch_state (gdbarch);
  7198. int i;
  7199. set_remote_traceframe ();
  7200. set_general_thread (regcache->ptid ());
  7201. if (regnum >= 0)
  7202. {
  7203. packet_reg *reg = packet_reg_from_regnum (gdbarch, rsa, regnum);
  7204. gdb_assert (reg != NULL);
  7205. /* Always prefer to store registers using the 'P' packet if
  7206. possible; we often change only a small number of registers.
  7207. Sometimes we change a larger number; we'd need help from a
  7208. higher layer to know to use 'G'. */
  7209. if (store_register_using_P (regcache, reg))
  7210. return;
  7211. /* For now, don't complain if we have no way to write the
  7212. register. GDB loses track of unavailable registers too
  7213. easily. Some day, this may be an error. We don't have
  7214. any way to read the register, either... */
  7215. if (!reg->in_g_packet)
  7216. return;
  7217. store_registers_using_G (regcache);
  7218. return;
  7219. }
  7220. store_registers_using_G (regcache);
  7221. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  7222. if (!rsa->regs[i].in_g_packet)
  7223. if (!store_register_using_P (regcache, &rsa->regs[i]))
  7224. /* See above for why we do not issue an error here. */
  7225. continue;
  7226. }
  7227. /* Return the number of hex digits in num. */
  7228. static int
  7229. hexnumlen (ULONGEST num)
  7230. {
  7231. int i;
  7232. for (i = 0; num != 0; i++)
  7233. num >>= 4;
  7234. return std::max (i, 1);
  7235. }
  7236. /* Set BUF to the minimum number of hex digits representing NUM. */
  7237. static int
  7238. hexnumstr (char *buf, ULONGEST num)
  7239. {
  7240. int len = hexnumlen (num);
  7241. return hexnumnstr (buf, num, len);
  7242. }
  7243. /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
  7244. static int
  7245. hexnumnstr (char *buf, ULONGEST num, int width)
  7246. {
  7247. int i;
  7248. buf[width] = '\0';
  7249. for (i = width - 1; i >= 0; i--)
  7250. {
  7251. buf[i] = "0123456789abcdef"[(num & 0xf)];
  7252. num >>= 4;
  7253. }
  7254. return width;
  7255. }
  7256. /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
  7257. static CORE_ADDR
  7258. remote_address_masked (CORE_ADDR addr)
  7259. {
  7260. unsigned int address_size = remote_address_size;
  7261. /* If "remoteaddresssize" was not set, default to target address size. */
  7262. if (!address_size)
  7263. address_size = gdbarch_addr_bit (target_gdbarch ());
  7264. if (address_size > 0
  7265. && address_size < (sizeof (ULONGEST) * 8))
  7266. {
  7267. /* Only create a mask when that mask can safely be constructed
  7268. in a ULONGEST variable. */
  7269. ULONGEST mask = 1;
  7270. mask = (mask << address_size) - 1;
  7271. addr &= mask;
  7272. }
  7273. return addr;
  7274. }
  7275. /* Determine whether the remote target supports binary downloading.
  7276. This is accomplished by sending a no-op memory write of zero length
  7277. to the target at the specified address. It does not suffice to send
  7278. the whole packet, since many stubs strip the eighth bit and
  7279. subsequently compute a wrong checksum, which causes real havoc with
  7280. remote_write_bytes.
  7281. NOTE: This can still lose if the serial line is not eight-bit
  7282. clean. In cases like this, the user should clear "remote
  7283. X-packet". */
  7284. void
  7285. remote_target::check_binary_download (CORE_ADDR addr)
  7286. {
  7287. struct remote_state *rs = get_remote_state ();
  7288. switch (packet_support (PACKET_X))
  7289. {
  7290. case PACKET_DISABLE:
  7291. break;
  7292. case PACKET_ENABLE:
  7293. break;
  7294. case PACKET_SUPPORT_UNKNOWN:
  7295. {
  7296. char *p;
  7297. p = rs->buf.data ();
  7298. *p++ = 'X';
  7299. p += hexnumstr (p, (ULONGEST) addr);
  7300. *p++ = ',';
  7301. p += hexnumstr (p, (ULONGEST) 0);
  7302. *p++ = ':';
  7303. *p = '\0';
  7304. putpkt_binary (rs->buf.data (), (int) (p - rs->buf.data ()));
  7305. getpkt (&rs->buf, 0);
  7306. if (rs->buf[0] == '\0')
  7307. {
  7308. remote_debug_printf ("binary downloading NOT supported by target");
  7309. remote_protocol_packets[PACKET_X].support = PACKET_DISABLE;
  7310. }
  7311. else
  7312. {
  7313. remote_debug_printf ("binary downloading supported by target");
  7314. remote_protocol_packets[PACKET_X].support = PACKET_ENABLE;
  7315. }
  7316. break;
  7317. }
  7318. }
  7319. }
  7320. /* Helper function to resize the payload in order to try to get a good
  7321. alignment. We try to write an amount of data such that the next write will
  7322. start on an address aligned on REMOTE_ALIGN_WRITES. */
  7323. static int
  7324. align_for_efficient_write (int todo, CORE_ADDR memaddr)
  7325. {
  7326. return ((memaddr + todo) & ~(REMOTE_ALIGN_WRITES - 1)) - memaddr;
  7327. }
  7328. /* Write memory data directly to the remote machine.
  7329. This does not inform the data cache; the data cache uses this.
  7330. HEADER is the starting part of the packet.
  7331. MEMADDR is the address in the remote memory space.
  7332. MYADDR is the address of the buffer in our space.
  7333. LEN_UNITS is the number of addressable units to write.
  7334. UNIT_SIZE is the length in bytes of an addressable unit.
  7335. PACKET_FORMAT should be either 'X' or 'M', and indicates if we
  7336. should send data as binary ('X'), or hex-encoded ('M').
  7337. The function creates packet of the form
  7338. <HEADER><ADDRESS>,<LENGTH>:<DATA>
  7339. where encoding of <DATA> is terminated by PACKET_FORMAT.
  7340. If USE_LENGTH is 0, then the <LENGTH> field and the preceding comma
  7341. are omitted.
  7342. Return the transferred status, error or OK (an
  7343. 'enum target_xfer_status' value). Save the number of addressable units
  7344. transferred in *XFERED_LEN_UNITS. Only transfer a single packet.
  7345. On a platform with an addressable memory size of 2 bytes (UNIT_SIZE == 2), an
  7346. exchange between gdb and the stub could look like (?? in place of the
  7347. checksum):
  7348. -> $m1000,4#??
  7349. <- aaaabbbbccccdddd
  7350. -> $M1000,3:eeeeffffeeee#??
  7351. <- OK
  7352. -> $m1000,4#??
  7353. <- eeeeffffeeeedddd */
  7354. target_xfer_status
  7355. remote_target::remote_write_bytes_aux (const char *header, CORE_ADDR memaddr,
  7356. const gdb_byte *myaddr,
  7357. ULONGEST len_units,
  7358. int unit_size,
  7359. ULONGEST *xfered_len_units,
  7360. char packet_format, int use_length)
  7361. {
  7362. struct remote_state *rs = get_remote_state ();
  7363. char *p;
  7364. char *plen = NULL;
  7365. int plenlen = 0;
  7366. int todo_units;
  7367. int units_written;
  7368. int payload_capacity_bytes;
  7369. int payload_length_bytes;
  7370. if (packet_format != 'X' && packet_format != 'M')
  7371. internal_error (__FILE__, __LINE__,
  7372. _("remote_write_bytes_aux: bad packet format"));
  7373. if (len_units == 0)
  7374. return TARGET_XFER_EOF;
  7375. payload_capacity_bytes = get_memory_write_packet_size ();
  7376. /* The packet buffer will be large enough for the payload;
  7377. get_memory_packet_size ensures this. */
  7378. rs->buf[0] = '\0';
  7379. /* Compute the size of the actual payload by subtracting out the
  7380. packet header and footer overhead: "$M<memaddr>,<len>:...#nn". */
  7381. payload_capacity_bytes -= strlen ("$,:#NN");
  7382. if (!use_length)
  7383. /* The comma won't be used. */
  7384. payload_capacity_bytes += 1;
  7385. payload_capacity_bytes -= strlen (header);
  7386. payload_capacity_bytes -= hexnumlen (memaddr);
  7387. /* Construct the packet excluding the data: "<header><memaddr>,<len>:". */
  7388. strcat (rs->buf.data (), header);
  7389. p = rs->buf.data () + strlen (header);
  7390. /* Compute a best guess of the number of bytes actually transfered. */
  7391. if (packet_format == 'X')
  7392. {
  7393. /* Best guess at number of bytes that will fit. */
  7394. todo_units = std::min (len_units,
  7395. (ULONGEST) payload_capacity_bytes / unit_size);
  7396. if (use_length)
  7397. payload_capacity_bytes -= hexnumlen (todo_units);
  7398. todo_units = std::min (todo_units, payload_capacity_bytes / unit_size);
  7399. }
  7400. else
  7401. {
  7402. /* Number of bytes that will fit. */
  7403. todo_units
  7404. = std::min (len_units,
  7405. (ULONGEST) (payload_capacity_bytes / unit_size) / 2);
  7406. if (use_length)
  7407. payload_capacity_bytes -= hexnumlen (todo_units);
  7408. todo_units = std::min (todo_units,
  7409. (payload_capacity_bytes / unit_size) / 2);
  7410. }
  7411. if (todo_units <= 0)
  7412. internal_error (__FILE__, __LINE__,
  7413. _("minimum packet size too small to write data"));
  7414. /* If we already need another packet, then try to align the end
  7415. of this packet to a useful boundary. */
  7416. if (todo_units > 2 * REMOTE_ALIGN_WRITES && todo_units < len_units)
  7417. todo_units = align_for_efficient_write (todo_units, memaddr);
  7418. /* Append "<memaddr>". */
  7419. memaddr = remote_address_masked (memaddr);
  7420. p += hexnumstr (p, (ULONGEST) memaddr);
  7421. if (use_length)
  7422. {
  7423. /* Append ",". */
  7424. *p++ = ',';
  7425. /* Append the length and retain its location and size. It may need to be
  7426. adjusted once the packet body has been created. */
  7427. plen = p;
  7428. plenlen = hexnumstr (p, (ULONGEST) todo_units);
  7429. p += plenlen;
  7430. }
  7431. /* Append ":". */
  7432. *p++ = ':';
  7433. *p = '\0';
  7434. /* Append the packet body. */
  7435. if (packet_format == 'X')
  7436. {
  7437. /* Binary mode. Send target system values byte by byte, in
  7438. increasing byte addresses. Only escape certain critical
  7439. characters. */
  7440. payload_length_bytes =
  7441. remote_escape_output (myaddr, todo_units, unit_size, (gdb_byte *) p,
  7442. &units_written, payload_capacity_bytes);
  7443. /* If not all TODO units fit, then we'll need another packet. Make
  7444. a second try to keep the end of the packet aligned. Don't do
  7445. this if the packet is tiny. */
  7446. if (units_written < todo_units && units_written > 2 * REMOTE_ALIGN_WRITES)
  7447. {
  7448. int new_todo_units;
  7449. new_todo_units = align_for_efficient_write (units_written, memaddr);
  7450. if (new_todo_units != units_written)
  7451. payload_length_bytes =
  7452. remote_escape_output (myaddr, new_todo_units, unit_size,
  7453. (gdb_byte *) p, &units_written,
  7454. payload_capacity_bytes);
  7455. }
  7456. p += payload_length_bytes;
  7457. if (use_length && units_written < todo_units)
  7458. {
  7459. /* Escape chars have filled up the buffer prematurely,
  7460. and we have actually sent fewer units than planned.
  7461. Fix-up the length field of the packet. Use the same
  7462. number of characters as before. */
  7463. plen += hexnumnstr (plen, (ULONGEST) units_written,
  7464. plenlen);
  7465. *plen = ':'; /* overwrite \0 from hexnumnstr() */
  7466. }
  7467. }
  7468. else
  7469. {
  7470. /* Normal mode: Send target system values byte by byte, in
  7471. increasing byte addresses. Each byte is encoded as a two hex
  7472. value. */
  7473. p += 2 * bin2hex (myaddr, p, todo_units * unit_size);
  7474. units_written = todo_units;
  7475. }
  7476. putpkt_binary (rs->buf.data (), (int) (p - rs->buf.data ()));
  7477. getpkt (&rs->buf, 0);
  7478. if (rs->buf[0] == 'E')
  7479. return TARGET_XFER_E_IO;
  7480. /* Return UNITS_WRITTEN, not TODO_UNITS, in case escape chars caused us to
  7481. send fewer units than we'd planned. */
  7482. *xfered_len_units = (ULONGEST) units_written;
  7483. return (*xfered_len_units != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
  7484. }
  7485. /* Write memory data directly to the remote machine.
  7486. This does not inform the data cache; the data cache uses this.
  7487. MEMADDR is the address in the remote memory space.
  7488. MYADDR is the address of the buffer in our space.
  7489. LEN is the number of bytes.
  7490. Return the transferred status, error or OK (an
  7491. 'enum target_xfer_status' value). Save the number of bytes
  7492. transferred in *XFERED_LEN. Only transfer a single packet. */
  7493. target_xfer_status
  7494. remote_target::remote_write_bytes (CORE_ADDR memaddr, const gdb_byte *myaddr,
  7495. ULONGEST len, int unit_size,
  7496. ULONGEST *xfered_len)
  7497. {
  7498. const char *packet_format = NULL;
  7499. /* Check whether the target supports binary download. */
  7500. check_binary_download (memaddr);
  7501. switch (packet_support (PACKET_X))
  7502. {
  7503. case PACKET_ENABLE:
  7504. packet_format = "X";
  7505. break;
  7506. case PACKET_DISABLE:
  7507. packet_format = "M";
  7508. break;
  7509. case PACKET_SUPPORT_UNKNOWN:
  7510. internal_error (__FILE__, __LINE__,
  7511. _("remote_write_bytes: bad internal state"));
  7512. default:
  7513. internal_error (__FILE__, __LINE__, _("bad switch"));
  7514. }
  7515. return remote_write_bytes_aux (packet_format,
  7516. memaddr, myaddr, len, unit_size, xfered_len,
  7517. packet_format[0], 1);
  7518. }
  7519. /* Read memory data directly from the remote machine.
  7520. This does not use the data cache; the data cache uses this.
  7521. MEMADDR is the address in the remote memory space.
  7522. MYADDR is the address of the buffer in our space.
  7523. LEN_UNITS is the number of addressable memory units to read..
  7524. UNIT_SIZE is the length in bytes of an addressable unit.
  7525. Return the transferred status, error or OK (an
  7526. 'enum target_xfer_status' value). Save the number of bytes
  7527. transferred in *XFERED_LEN_UNITS.
  7528. See the comment of remote_write_bytes_aux for an example of
  7529. memory read/write exchange between gdb and the stub. */
  7530. target_xfer_status
  7531. remote_target::remote_read_bytes_1 (CORE_ADDR memaddr, gdb_byte *myaddr,
  7532. ULONGEST len_units,
  7533. int unit_size, ULONGEST *xfered_len_units)
  7534. {
  7535. struct remote_state *rs = get_remote_state ();
  7536. int buf_size_bytes; /* Max size of packet output buffer. */
  7537. char *p;
  7538. int todo_units;
  7539. int decoded_bytes;
  7540. buf_size_bytes = get_memory_read_packet_size ();
  7541. /* The packet buffer will be large enough for the payload;
  7542. get_memory_packet_size ensures this. */
  7543. /* Number of units that will fit. */
  7544. todo_units = std::min (len_units,
  7545. (ULONGEST) (buf_size_bytes / unit_size) / 2);
  7546. /* Construct "m"<memaddr>","<len>". */
  7547. memaddr = remote_address_masked (memaddr);
  7548. p = rs->buf.data ();
  7549. *p++ = 'm';
  7550. p += hexnumstr (p, (ULONGEST) memaddr);
  7551. *p++ = ',';
  7552. p += hexnumstr (p, (ULONGEST) todo_units);
  7553. *p = '\0';
  7554. putpkt (rs->buf);
  7555. getpkt (&rs->buf, 0);
  7556. if (rs->buf[0] == 'E'
  7557. && isxdigit (rs->buf[1]) && isxdigit (rs->buf[2])
  7558. && rs->buf[3] == '\0')
  7559. return TARGET_XFER_E_IO;
  7560. /* Reply describes memory byte by byte, each byte encoded as two hex
  7561. characters. */
  7562. p = rs->buf.data ();
  7563. decoded_bytes = hex2bin (p, myaddr, todo_units * unit_size);
  7564. /* Return what we have. Let higher layers handle partial reads. */
  7565. *xfered_len_units = (ULONGEST) (decoded_bytes / unit_size);
  7566. return (*xfered_len_units != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
  7567. }
  7568. /* Using the set of read-only target sections of remote, read live
  7569. read-only memory.
  7570. For interface/parameters/return description see target.h,
  7571. to_xfer_partial. */
  7572. target_xfer_status
  7573. remote_target::remote_xfer_live_readonly_partial (gdb_byte *readbuf,
  7574. ULONGEST memaddr,
  7575. ULONGEST len,
  7576. int unit_size,
  7577. ULONGEST *xfered_len)
  7578. {
  7579. const struct target_section *secp;
  7580. secp = target_section_by_addr (this, memaddr);
  7581. if (secp != NULL
  7582. && (bfd_section_flags (secp->the_bfd_section) & SEC_READONLY))
  7583. {
  7584. ULONGEST memend = memaddr + len;
  7585. const target_section_table *table = target_get_section_table (this);
  7586. for (const target_section &p : *table)
  7587. {
  7588. if (memaddr >= p.addr)
  7589. {
  7590. if (memend <= p.endaddr)
  7591. {
  7592. /* Entire transfer is within this section. */
  7593. return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
  7594. xfered_len);
  7595. }
  7596. else if (memaddr >= p.endaddr)
  7597. {
  7598. /* This section ends before the transfer starts. */
  7599. continue;
  7600. }
  7601. else
  7602. {
  7603. /* This section overlaps the transfer. Just do half. */
  7604. len = p.endaddr - memaddr;
  7605. return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
  7606. xfered_len);
  7607. }
  7608. }
  7609. }
  7610. }
  7611. return TARGET_XFER_EOF;
  7612. }
  7613. /* Similar to remote_read_bytes_1, but it reads from the remote stub
  7614. first if the requested memory is unavailable in traceframe.
  7615. Otherwise, fall back to remote_read_bytes_1. */
  7616. target_xfer_status
  7617. remote_target::remote_read_bytes (CORE_ADDR memaddr,
  7618. gdb_byte *myaddr, ULONGEST len, int unit_size,
  7619. ULONGEST *xfered_len)
  7620. {
  7621. if (len == 0)
  7622. return TARGET_XFER_EOF;
  7623. if (get_traceframe_number () != -1)
  7624. {
  7625. std::vector<mem_range> available;
  7626. /* If we fail to get the set of available memory, then the
  7627. target does not support querying traceframe info, and so we
  7628. attempt reading from the traceframe anyway (assuming the
  7629. target implements the old QTro packet then). */
  7630. if (traceframe_available_memory (&available, memaddr, len))
  7631. {
  7632. if (available.empty () || available[0].start != memaddr)
  7633. {
  7634. enum target_xfer_status res;
  7635. /* Don't read into the traceframe's available
  7636. memory. */
  7637. if (!available.empty ())
  7638. {
  7639. LONGEST oldlen = len;
  7640. len = available[0].start - memaddr;
  7641. gdb_assert (len <= oldlen);
  7642. }
  7643. /* This goes through the topmost target again. */
  7644. res = remote_xfer_live_readonly_partial (myaddr, memaddr,
  7645. len, unit_size, xfered_len);
  7646. if (res == TARGET_XFER_OK)
  7647. return TARGET_XFER_OK;
  7648. else
  7649. {
  7650. /* No use trying further, we know some memory starting
  7651. at MEMADDR isn't available. */
  7652. *xfered_len = len;
  7653. return (*xfered_len != 0) ?
  7654. TARGET_XFER_UNAVAILABLE : TARGET_XFER_EOF;
  7655. }
  7656. }
  7657. /* Don't try to read more than how much is available, in
  7658. case the target implements the deprecated QTro packet to
  7659. cater for older GDBs (the target's knowledge of read-only
  7660. sections may be outdated by now). */
  7661. len = available[0].length;
  7662. }
  7663. }
  7664. return remote_read_bytes_1 (memaddr, myaddr, len, unit_size, xfered_len);
  7665. }
  7666. /* Sends a packet with content determined by the printf format string
  7667. FORMAT and the remaining arguments, then gets the reply. Returns
  7668. whether the packet was a success, a failure, or unknown. */
  7669. packet_result
  7670. remote_target::remote_send_printf (const char *format, ...)
  7671. {
  7672. struct remote_state *rs = get_remote_state ();
  7673. int max_size = get_remote_packet_size ();
  7674. va_list ap;
  7675. va_start (ap, format);
  7676. rs->buf[0] = '\0';
  7677. int size = vsnprintf (rs->buf.data (), max_size, format, ap);
  7678. va_end (ap);
  7679. if (size >= max_size)
  7680. internal_error (__FILE__, __LINE__, _("Too long remote packet."));
  7681. if (putpkt (rs->buf) < 0)
  7682. error (_("Communication problem with target."));
  7683. rs->buf[0] = '\0';
  7684. getpkt (&rs->buf, 0);
  7685. return packet_check_result (rs->buf);
  7686. }
  7687. /* Flash writing can take quite some time. We'll set
  7688. effectively infinite timeout for flash operations.
  7689. In future, we'll need to decide on a better approach. */
  7690. static const int remote_flash_timeout = 1000;
  7691. void
  7692. remote_target::flash_erase (ULONGEST address, LONGEST length)
  7693. {
  7694. int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
  7695. enum packet_result ret;
  7696. scoped_restore restore_timeout
  7697. = make_scoped_restore (&remote_timeout, remote_flash_timeout);
  7698. ret = remote_send_printf ("vFlashErase:%s,%s",
  7699. phex (address, addr_size),
  7700. phex (length, 4));
  7701. switch (ret)
  7702. {
  7703. case PACKET_UNKNOWN:
  7704. error (_("Remote target does not support flash erase"));
  7705. case PACKET_ERROR:
  7706. error (_("Error erasing flash with vFlashErase packet"));
  7707. default:
  7708. break;
  7709. }
  7710. }
  7711. target_xfer_status
  7712. remote_target::remote_flash_write (ULONGEST address,
  7713. ULONGEST length, ULONGEST *xfered_len,
  7714. const gdb_byte *data)
  7715. {
  7716. scoped_restore restore_timeout
  7717. = make_scoped_restore (&remote_timeout, remote_flash_timeout);
  7718. return remote_write_bytes_aux ("vFlashWrite:", address, data, length, 1,
  7719. xfered_len,'X', 0);
  7720. }
  7721. void
  7722. remote_target::flash_done ()
  7723. {
  7724. int ret;
  7725. scoped_restore restore_timeout
  7726. = make_scoped_restore (&remote_timeout, remote_flash_timeout);
  7727. ret = remote_send_printf ("vFlashDone");
  7728. switch (ret)
  7729. {
  7730. case PACKET_UNKNOWN:
  7731. error (_("Remote target does not support vFlashDone"));
  7732. case PACKET_ERROR:
  7733. error (_("Error finishing flash operation"));
  7734. default:
  7735. break;
  7736. }
  7737. }
  7738. /* Stuff for dealing with the packets which are part of this protocol.
  7739. See comment at top of file for details. */
  7740. /* Close/unpush the remote target, and throw a TARGET_CLOSE_ERROR
  7741. error to higher layers. Called when a serial error is detected.
  7742. The exception message is STRING, followed by a colon and a blank,
  7743. the system error message for errno at function entry and final dot
  7744. for output compatibility with throw_perror_with_name. */
  7745. static void
  7746. unpush_and_perror (remote_target *target, const char *string)
  7747. {
  7748. int saved_errno = errno;
  7749. remote_unpush_target (target);
  7750. throw_error (TARGET_CLOSE_ERROR, "%s: %s.", string,
  7751. safe_strerror (saved_errno));
  7752. }
  7753. /* Read a single character from the remote end. The current quit
  7754. handler is overridden to avoid quitting in the middle of packet
  7755. sequence, as that would break communication with the remote server.
  7756. See remote_serial_quit_handler for more detail. */
  7757. int
  7758. remote_target::readchar (int timeout)
  7759. {
  7760. int ch;
  7761. struct remote_state *rs = get_remote_state ();
  7762. {
  7763. scoped_restore restore_quit_target
  7764. = make_scoped_restore (&curr_quit_handler_target, this);
  7765. scoped_restore restore_quit
  7766. = make_scoped_restore (&quit_handler, ::remote_serial_quit_handler);
  7767. rs->got_ctrlc_during_io = 0;
  7768. ch = serial_readchar (rs->remote_desc, timeout);
  7769. if (rs->got_ctrlc_during_io)
  7770. set_quit_flag ();
  7771. }
  7772. if (ch >= 0)
  7773. return ch;
  7774. switch ((enum serial_rc) ch)
  7775. {
  7776. case SERIAL_EOF:
  7777. remote_unpush_target (this);
  7778. throw_error (TARGET_CLOSE_ERROR, _("Remote connection closed"));
  7779. /* no return */
  7780. case SERIAL_ERROR:
  7781. unpush_and_perror (this, _("Remote communication error. "
  7782. "Target disconnected."));
  7783. /* no return */
  7784. case SERIAL_TIMEOUT:
  7785. break;
  7786. }
  7787. return ch;
  7788. }
  7789. /* Wrapper for serial_write that closes the target and throws if
  7790. writing fails. The current quit handler is overridden to avoid
  7791. quitting in the middle of packet sequence, as that would break
  7792. communication with the remote server. See
  7793. remote_serial_quit_handler for more detail. */
  7794. void
  7795. remote_target::remote_serial_write (const char *str, int len)
  7796. {
  7797. struct remote_state *rs = get_remote_state ();
  7798. scoped_restore restore_quit_target
  7799. = make_scoped_restore (&curr_quit_handler_target, this);
  7800. scoped_restore restore_quit
  7801. = make_scoped_restore (&quit_handler, ::remote_serial_quit_handler);
  7802. rs->got_ctrlc_during_io = 0;
  7803. if (serial_write (rs->remote_desc, str, len))
  7804. {
  7805. unpush_and_perror (this, _("Remote communication error. "
  7806. "Target disconnected."));
  7807. }
  7808. if (rs->got_ctrlc_during_io)
  7809. set_quit_flag ();
  7810. }
  7811. /* Return a string representing an escaped version of BUF, of len N.
  7812. E.g. \n is converted to \\n, \t to \\t, etc. */
  7813. static std::string
  7814. escape_buffer (const char *buf, int n)
  7815. {
  7816. string_file stb;
  7817. stb.putstrn (buf, n, '\\');
  7818. return stb.release ();
  7819. }
  7820. int
  7821. remote_target::putpkt (const char *buf)
  7822. {
  7823. return putpkt_binary (buf, strlen (buf));
  7824. }
  7825. /* Wrapper around remote_target::putpkt to avoid exporting
  7826. remote_target. */
  7827. int
  7828. putpkt (remote_target *remote, const char *buf)
  7829. {
  7830. return remote->putpkt (buf);
  7831. }
  7832. /* Send a packet to the remote machine, with error checking. The data
  7833. of the packet is in BUF. The string in BUF can be at most
  7834. get_remote_packet_size () - 5 to account for the $, # and checksum,
  7835. and for a possible /0 if we are debugging (remote_debug) and want
  7836. to print the sent packet as a string. */
  7837. int
  7838. remote_target::putpkt_binary (const char *buf, int cnt)
  7839. {
  7840. struct remote_state *rs = get_remote_state ();
  7841. int i;
  7842. unsigned char csum = 0;
  7843. gdb::def_vector<char> data (cnt + 6);
  7844. char *buf2 = data.data ();
  7845. int ch;
  7846. int tcount = 0;
  7847. char *p;
  7848. /* Catch cases like trying to read memory or listing threads while
  7849. we're waiting for a stop reply. The remote server wouldn't be
  7850. ready to handle this request, so we'd hang and timeout. We don't
  7851. have to worry about this in synchronous mode, because in that
  7852. case it's not possible to issue a command while the target is
  7853. running. This is not a problem in non-stop mode, because in that
  7854. case, the stub is always ready to process serial input. */
  7855. if (!target_is_non_stop_p ()
  7856. && target_is_async_p ()
  7857. && rs->waiting_for_stop_reply)
  7858. {
  7859. error (_("Cannot execute this command while the target is running.\n"
  7860. "Use the \"interrupt\" command to stop the target\n"
  7861. "and then try again."));
  7862. }
  7863. /* Copy the packet into buffer BUF2, encapsulating it
  7864. and giving it a checksum. */
  7865. p = buf2;
  7866. *p++ = '$';
  7867. for (i = 0; i < cnt; i++)
  7868. {
  7869. csum += buf[i];
  7870. *p++ = buf[i];
  7871. }
  7872. *p++ = '#';
  7873. *p++ = tohex ((csum >> 4) & 0xf);
  7874. *p++ = tohex (csum & 0xf);
  7875. /* Send it over and over until we get a positive ack. */
  7876. while (1)
  7877. {
  7878. if (remote_debug)
  7879. {
  7880. *p = '\0';
  7881. int len = (int) (p - buf2);
  7882. int max_chars;
  7883. if (remote_packet_max_chars < 0)
  7884. max_chars = len;
  7885. else
  7886. max_chars = remote_packet_max_chars;
  7887. std::string str
  7888. = escape_buffer (buf2, std::min (len, max_chars));
  7889. if (len > max_chars)
  7890. remote_debug_printf_nofunc
  7891. ("Sending packet: %s [%d bytes omitted]", str.c_str (),
  7892. len - max_chars);
  7893. else
  7894. remote_debug_printf_nofunc ("Sending packet: %s", str.c_str ());
  7895. }
  7896. remote_serial_write (buf2, p - buf2);
  7897. /* If this is a no acks version of the remote protocol, send the
  7898. packet and move on. */
  7899. if (rs->noack_mode)
  7900. break;
  7901. /* Read until either a timeout occurs (-2) or '+' is read.
  7902. Handle any notification that arrives in the mean time. */
  7903. while (1)
  7904. {
  7905. ch = readchar (remote_timeout);
  7906. switch (ch)
  7907. {
  7908. case '+':
  7909. remote_debug_printf_nofunc ("Received Ack");
  7910. return 1;
  7911. case '-':
  7912. remote_debug_printf_nofunc ("Received Nak");
  7913. /* FALLTHROUGH */
  7914. case SERIAL_TIMEOUT:
  7915. tcount++;
  7916. if (tcount > 3)
  7917. return 0;
  7918. break; /* Retransmit buffer. */
  7919. case '$':
  7920. {
  7921. remote_debug_printf ("Packet instead of Ack, ignoring it");
  7922. /* It's probably an old response sent because an ACK
  7923. was lost. Gobble up the packet and ack it so it
  7924. doesn't get retransmitted when we resend this
  7925. packet. */
  7926. skip_frame ();
  7927. remote_serial_write ("+", 1);
  7928. continue; /* Now, go look for +. */
  7929. }
  7930. case '%':
  7931. {
  7932. int val;
  7933. /* If we got a notification, handle it, and go back to looking
  7934. for an ack. */
  7935. /* We've found the start of a notification. Now
  7936. collect the data. */
  7937. val = read_frame (&rs->buf);
  7938. if (val >= 0)
  7939. {
  7940. remote_debug_printf_nofunc
  7941. (" Notification received: %s",
  7942. escape_buffer (rs->buf.data (), val).c_str ());
  7943. handle_notification (rs->notif_state, rs->buf.data ());
  7944. /* We're in sync now, rewait for the ack. */
  7945. tcount = 0;
  7946. }
  7947. else
  7948. remote_debug_printf_nofunc ("Junk: %c%s", ch & 0177,
  7949. rs->buf.data ());
  7950. continue;
  7951. }
  7952. /* fall-through */
  7953. default:
  7954. remote_debug_printf_nofunc ("Junk: %c%s", ch & 0177,
  7955. rs->buf.data ());
  7956. continue;
  7957. }
  7958. break; /* Here to retransmit. */
  7959. }
  7960. #if 0
  7961. /* This is wrong. If doing a long backtrace, the user should be
  7962. able to get out next time we call QUIT, without anything as
  7963. violent as interrupt_query. If we want to provide a way out of
  7964. here without getting to the next QUIT, it should be based on
  7965. hitting ^C twice as in remote_wait. */
  7966. if (quit_flag)
  7967. {
  7968. quit_flag = 0;
  7969. interrupt_query ();
  7970. }
  7971. #endif
  7972. }
  7973. return 0;
  7974. }
  7975. /* Come here after finding the start of a frame when we expected an
  7976. ack. Do our best to discard the rest of this packet. */
  7977. void
  7978. remote_target::skip_frame ()
  7979. {
  7980. int c;
  7981. while (1)
  7982. {
  7983. c = readchar (remote_timeout);
  7984. switch (c)
  7985. {
  7986. case SERIAL_TIMEOUT:
  7987. /* Nothing we can do. */
  7988. return;
  7989. case '#':
  7990. /* Discard the two bytes of checksum and stop. */
  7991. c = readchar (remote_timeout);
  7992. if (c >= 0)
  7993. c = readchar (remote_timeout);
  7994. return;
  7995. case '*': /* Run length encoding. */
  7996. /* Discard the repeat count. */
  7997. c = readchar (remote_timeout);
  7998. if (c < 0)
  7999. return;
  8000. break;
  8001. default:
  8002. /* A regular character. */
  8003. break;
  8004. }
  8005. }
  8006. }
  8007. /* Come here after finding the start of the frame. Collect the rest
  8008. into *BUF, verifying the checksum, length, and handling run-length
  8009. compression. NUL terminate the buffer. If there is not enough room,
  8010. expand *BUF.
  8011. Returns -1 on error, number of characters in buffer (ignoring the
  8012. trailing NULL) on success. (could be extended to return one of the
  8013. SERIAL status indications). */
  8014. long
  8015. remote_target::read_frame (gdb::char_vector *buf_p)
  8016. {
  8017. unsigned char csum;
  8018. long bc;
  8019. int c;
  8020. char *buf = buf_p->data ();
  8021. struct remote_state *rs = get_remote_state ();
  8022. csum = 0;
  8023. bc = 0;
  8024. while (1)
  8025. {
  8026. c = readchar (remote_timeout);
  8027. switch (c)
  8028. {
  8029. case SERIAL_TIMEOUT:
  8030. remote_debug_printf ("Timeout in mid-packet, retrying");
  8031. return -1;
  8032. case '$':
  8033. remote_debug_printf ("Saw new packet start in middle of old one");
  8034. return -1; /* Start a new packet, count retries. */
  8035. case '#':
  8036. {
  8037. unsigned char pktcsum;
  8038. int check_0 = 0;
  8039. int check_1 = 0;
  8040. buf[bc] = '\0';
  8041. check_0 = readchar (remote_timeout);
  8042. if (check_0 >= 0)
  8043. check_1 = readchar (remote_timeout);
  8044. if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
  8045. {
  8046. remote_debug_printf ("Timeout in checksum, retrying");
  8047. return -1;
  8048. }
  8049. else if (check_0 < 0 || check_1 < 0)
  8050. {
  8051. remote_debug_printf ("Communication error in checksum");
  8052. return -1;
  8053. }
  8054. /* Don't recompute the checksum; with no ack packets we
  8055. don't have any way to indicate a packet retransmission
  8056. is necessary. */
  8057. if (rs->noack_mode)
  8058. return bc;
  8059. pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
  8060. if (csum == pktcsum)
  8061. return bc;
  8062. remote_debug_printf
  8063. ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s",
  8064. pktcsum, csum, escape_buffer (buf, bc).c_str ());
  8065. /* Number of characters in buffer ignoring trailing
  8066. NULL. */
  8067. return -1;
  8068. }
  8069. case '*': /* Run length encoding. */
  8070. {
  8071. int repeat;
  8072. csum += c;
  8073. c = readchar (remote_timeout);
  8074. csum += c;
  8075. repeat = c - ' ' + 3; /* Compute repeat count. */
  8076. /* The character before ``*'' is repeated. */
  8077. if (repeat > 0 && repeat <= 255 && bc > 0)
  8078. {
  8079. if (bc + repeat - 1 >= buf_p->size () - 1)
  8080. {
  8081. /* Make some more room in the buffer. */
  8082. buf_p->resize (buf_p->size () + repeat);
  8083. buf = buf_p->data ();
  8084. }
  8085. memset (&buf[bc], buf[bc - 1], repeat);
  8086. bc += repeat;
  8087. continue;
  8088. }
  8089. buf[bc] = '\0';
  8090. gdb_printf (_("Invalid run length encoding: %s\n"), buf);
  8091. return -1;
  8092. }
  8093. default:
  8094. if (bc >= buf_p->size () - 1)
  8095. {
  8096. /* Make some more room in the buffer. */
  8097. buf_p->resize (buf_p->size () * 2);
  8098. buf = buf_p->data ();
  8099. }
  8100. buf[bc++] = c;
  8101. csum += c;
  8102. continue;
  8103. }
  8104. }
  8105. }
  8106. /* Set this to the maximum number of seconds to wait instead of waiting forever
  8107. in target_wait(). If this timer times out, then it generates an error and
  8108. the command is aborted. This replaces most of the need for timeouts in the
  8109. GDB test suite, and makes it possible to distinguish between a hung target
  8110. and one with slow communications. */
  8111. static int watchdog = 0;
  8112. static void
  8113. show_watchdog (struct ui_file *file, int from_tty,
  8114. struct cmd_list_element *c, const char *value)
  8115. {
  8116. gdb_printf (file, _("Watchdog timer is %s.\n"), value);
  8117. }
  8118. /* Read a packet from the remote machine, with error checking, and
  8119. store it in *BUF. Resize *BUF if necessary to hold the result. If
  8120. FOREVER, wait forever rather than timing out; this is used (in
  8121. synchronous mode) to wait for a target that is is executing user
  8122. code to stop. */
  8123. /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
  8124. don't have to change all the calls to getpkt to deal with the
  8125. return value, because at the moment I don't know what the right
  8126. thing to do it for those. */
  8127. void
  8128. remote_target::getpkt (gdb::char_vector *buf, int forever)
  8129. {
  8130. getpkt_sane (buf, forever);
  8131. }
  8132. /* Read a packet from the remote machine, with error checking, and
  8133. store it in *BUF. Resize *BUF if necessary to hold the result. If
  8134. FOREVER, wait forever rather than timing out; this is used (in
  8135. synchronous mode) to wait for a target that is is executing user
  8136. code to stop. If FOREVER == 0, this function is allowed to time
  8137. out gracefully and return an indication of this to the caller.
  8138. Otherwise return the number of bytes read. If EXPECTING_NOTIF,
  8139. consider receiving a notification enough reason to return to the
  8140. caller. *IS_NOTIF is an output boolean that indicates whether *BUF
  8141. holds a notification or not (a regular packet). */
  8142. int
  8143. remote_target::getpkt_or_notif_sane_1 (gdb::char_vector *buf,
  8144. int forever, int expecting_notif,
  8145. int *is_notif)
  8146. {
  8147. struct remote_state *rs = get_remote_state ();
  8148. int c;
  8149. int tries;
  8150. int timeout;
  8151. int val = -1;
  8152. strcpy (buf->data (), "timeout");
  8153. if (forever)
  8154. timeout = watchdog > 0 ? watchdog : -1;
  8155. else if (expecting_notif)
  8156. timeout = 0; /* There should already be a char in the buffer. If
  8157. not, bail out. */
  8158. else
  8159. timeout = remote_timeout;
  8160. #define MAX_TRIES 3
  8161. /* Process any number of notifications, and then return when
  8162. we get a packet. */
  8163. for (;;)
  8164. {
  8165. /* If we get a timeout or bad checksum, retry up to MAX_TRIES
  8166. times. */
  8167. for (tries = 1; tries <= MAX_TRIES; tries++)
  8168. {
  8169. /* This can loop forever if the remote side sends us
  8170. characters continuously, but if it pauses, we'll get
  8171. SERIAL_TIMEOUT from readchar because of timeout. Then
  8172. we'll count that as a retry.
  8173. Note that even when forever is set, we will only wait
  8174. forever prior to the start of a packet. After that, we
  8175. expect characters to arrive at a brisk pace. They should
  8176. show up within remote_timeout intervals. */
  8177. do
  8178. c = readchar (timeout);
  8179. while (c != SERIAL_TIMEOUT && c != '$' && c != '%');
  8180. if (c == SERIAL_TIMEOUT)
  8181. {
  8182. if (expecting_notif)
  8183. return -1; /* Don't complain, it's normal to not get
  8184. anything in this case. */
  8185. if (forever) /* Watchdog went off? Kill the target. */
  8186. {
  8187. remote_unpush_target (this);
  8188. throw_error (TARGET_CLOSE_ERROR,
  8189. _("Watchdog timeout has expired. "
  8190. "Target detached."));
  8191. }
  8192. remote_debug_printf ("Timed out.");
  8193. }
  8194. else
  8195. {
  8196. /* We've found the start of a packet or notification.
  8197. Now collect the data. */
  8198. val = read_frame (buf);
  8199. if (val >= 0)
  8200. break;
  8201. }
  8202. remote_serial_write ("-", 1);
  8203. }
  8204. if (tries > MAX_TRIES)
  8205. {
  8206. /* We have tried hard enough, and just can't receive the
  8207. packet/notification. Give up. */
  8208. gdb_printf (_("Ignoring packet error, continuing...\n"));
  8209. /* Skip the ack char if we're in no-ack mode. */
  8210. if (!rs->noack_mode)
  8211. remote_serial_write ("+", 1);
  8212. return -1;
  8213. }
  8214. /* If we got an ordinary packet, return that to our caller. */
  8215. if (c == '$')
  8216. {
  8217. if (remote_debug)
  8218. {
  8219. int max_chars;
  8220. if (remote_packet_max_chars < 0)
  8221. max_chars = val;
  8222. else
  8223. max_chars = remote_packet_max_chars;
  8224. std::string str
  8225. = escape_buffer (buf->data (),
  8226. std::min (val, max_chars));
  8227. if (val > max_chars)
  8228. remote_debug_printf_nofunc
  8229. ("Packet received: %s [%d bytes omitted]", str.c_str (),
  8230. val - max_chars);
  8231. else
  8232. remote_debug_printf_nofunc ("Packet received: %s",
  8233. str.c_str ());
  8234. }
  8235. /* Skip the ack char if we're in no-ack mode. */
  8236. if (!rs->noack_mode)
  8237. remote_serial_write ("+", 1);
  8238. if (is_notif != NULL)
  8239. *is_notif = 0;
  8240. return val;
  8241. }
  8242. /* If we got a notification, handle it, and go back to looking
  8243. for a packet. */
  8244. else
  8245. {
  8246. gdb_assert (c == '%');
  8247. remote_debug_printf_nofunc
  8248. (" Notification received: %s",
  8249. escape_buffer (buf->data (), val).c_str ());
  8250. if (is_notif != NULL)
  8251. *is_notif = 1;
  8252. handle_notification (rs->notif_state, buf->data ());
  8253. /* Notifications require no acknowledgement. */
  8254. if (expecting_notif)
  8255. return val;
  8256. }
  8257. }
  8258. }
  8259. int
  8260. remote_target::getpkt_sane (gdb::char_vector *buf, int forever)
  8261. {
  8262. return getpkt_or_notif_sane_1 (buf, forever, 0, NULL);
  8263. }
  8264. int
  8265. remote_target::getpkt_or_notif_sane (gdb::char_vector *buf, int forever,
  8266. int *is_notif)
  8267. {
  8268. return getpkt_or_notif_sane_1 (buf, forever, 1, is_notif);
  8269. }
  8270. /* Kill any new fork children of inferior INF that haven't been
  8271. processed by follow_fork. */
  8272. void
  8273. remote_target::kill_new_fork_children (inferior *inf)
  8274. {
  8275. remote_state *rs = get_remote_state ();
  8276. struct notif_client *notif = &notif_client_stop;
  8277. /* Kill the fork child threads of any threads in inferior INF that are stopped
  8278. at a fork event. */
  8279. for (thread_info *thread : inf->non_exited_threads ())
  8280. {
  8281. const target_waitstatus *ws = thread_pending_fork_status (thread);
  8282. if (ws == nullptr)
  8283. continue;
  8284. int child_pid = ws->child_ptid ().pid ();
  8285. int res = remote_vkill (child_pid);
  8286. if (res != 0)
  8287. error (_("Can't kill fork child process %d"), child_pid);
  8288. }
  8289. /* Check for any pending fork events (not reported or processed yet)
  8290. in inferior INF and kill those fork child threads as well. */
  8291. remote_notif_get_pending_events (notif);
  8292. for (auto &event : rs->stop_reply_queue)
  8293. {
  8294. if (event->ptid.pid () != inf->pid)
  8295. continue;
  8296. if (!is_fork_status (event->ws.kind ()))
  8297. continue;
  8298. int child_pid = event->ws.child_ptid ().pid ();
  8299. int res = remote_vkill (child_pid);
  8300. if (res != 0)
  8301. error (_("Can't kill fork child process %d"), child_pid);
  8302. }
  8303. }
  8304. /* Target hook to kill the current inferior. */
  8305. void
  8306. remote_target::kill ()
  8307. {
  8308. int res = -1;
  8309. inferior *inf = find_inferior_pid (this, inferior_ptid.pid ());
  8310. struct remote_state *rs = get_remote_state ();
  8311. gdb_assert (inf != nullptr);
  8312. if (packet_support (PACKET_vKill) != PACKET_DISABLE)
  8313. {
  8314. /* If we're stopped while forking and we haven't followed yet,
  8315. kill the child task. We need to do this before killing the
  8316. parent task because if this is a vfork then the parent will
  8317. be sleeping. */
  8318. kill_new_fork_children (inf);
  8319. res = remote_vkill (inf->pid);
  8320. if (res == 0)
  8321. {
  8322. target_mourn_inferior (inferior_ptid);
  8323. return;
  8324. }
  8325. }
  8326. /* If we are in 'target remote' mode and we are killing the only
  8327. inferior, then we will tell gdbserver to exit and unpush the
  8328. target. */
  8329. if (res == -1 && !remote_multi_process_p (rs)
  8330. && number_of_live_inferiors (this) == 1)
  8331. {
  8332. remote_kill_k ();
  8333. /* We've killed the remote end, we get to mourn it. If we are
  8334. not in extended mode, mourning the inferior also unpushes
  8335. remote_ops from the target stack, which closes the remote
  8336. connection. */
  8337. target_mourn_inferior (inferior_ptid);
  8338. return;
  8339. }
  8340. error (_("Can't kill process"));
  8341. }
  8342. /* Send a kill request to the target using the 'vKill' packet. */
  8343. int
  8344. remote_target::remote_vkill (int pid)
  8345. {
  8346. if (packet_support (PACKET_vKill) == PACKET_DISABLE)
  8347. return -1;
  8348. remote_state *rs = get_remote_state ();
  8349. /* Tell the remote target to detach. */
  8350. xsnprintf (rs->buf.data (), get_remote_packet_size (), "vKill;%x", pid);
  8351. putpkt (rs->buf);
  8352. getpkt (&rs->buf, 0);
  8353. switch (packet_ok (rs->buf,
  8354. &remote_protocol_packets[PACKET_vKill]))
  8355. {
  8356. case PACKET_OK:
  8357. return 0;
  8358. case PACKET_ERROR:
  8359. return 1;
  8360. case PACKET_UNKNOWN:
  8361. return -1;
  8362. default:
  8363. internal_error (__FILE__, __LINE__, _("Bad result from packet_ok"));
  8364. }
  8365. }
  8366. /* Send a kill request to the target using the 'k' packet. */
  8367. void
  8368. remote_target::remote_kill_k ()
  8369. {
  8370. /* Catch errors so the user can quit from gdb even when we
  8371. aren't on speaking terms with the remote system. */
  8372. try
  8373. {
  8374. putpkt ("k");
  8375. }
  8376. catch (const gdb_exception_error &ex)
  8377. {
  8378. if (ex.error == TARGET_CLOSE_ERROR)
  8379. {
  8380. /* If we got an (EOF) error that caused the target
  8381. to go away, then we're done, that's what we wanted.
  8382. "k" is susceptible to cause a premature EOF, given
  8383. that the remote server isn't actually required to
  8384. reply to "k", and it can happen that it doesn't
  8385. even get to reply ACK to the "k". */
  8386. return;
  8387. }
  8388. /* Otherwise, something went wrong. We didn't actually kill
  8389. the target. Just propagate the exception, and let the
  8390. user or higher layers decide what to do. */
  8391. throw;
  8392. }
  8393. }
  8394. void
  8395. remote_target::mourn_inferior ()
  8396. {
  8397. struct remote_state *rs = get_remote_state ();
  8398. /* We're no longer interested in notification events of an inferior
  8399. that exited or was killed/detached. */
  8400. discard_pending_stop_replies (current_inferior ());
  8401. /* In 'target remote' mode with one inferior, we close the connection. */
  8402. if (!rs->extended && number_of_live_inferiors (this) <= 1)
  8403. {
  8404. remote_unpush_target (this);
  8405. return;
  8406. }
  8407. /* In case we got here due to an error, but we're going to stay
  8408. connected. */
  8409. rs->waiting_for_stop_reply = 0;
  8410. /* If the current general thread belonged to the process we just
  8411. detached from or has exited, the remote side current general
  8412. thread becomes undefined. Considering a case like this:
  8413. - We just got here due to a detach.
  8414. - The process that we're detaching from happens to immediately
  8415. report a global breakpoint being hit in non-stop mode, in the
  8416. same thread we had selected before.
  8417. - GDB attaches to this process again.
  8418. - This event happens to be the next event we handle.
  8419. GDB would consider that the current general thread didn't need to
  8420. be set on the stub side (with Hg), since for all it knew,
  8421. GENERAL_THREAD hadn't changed.
  8422. Notice that although in all-stop mode, the remote server always
  8423. sets the current thread to the thread reporting the stop event,
  8424. that doesn't happen in non-stop mode; in non-stop, the stub *must
  8425. not* change the current thread when reporting a breakpoint hit,
  8426. due to the decoupling of event reporting and event handling.
  8427. To keep things simple, we always invalidate our notion of the
  8428. current thread. */
  8429. record_currthread (rs, minus_one_ptid);
  8430. /* Call common code to mark the inferior as not running. */
  8431. generic_mourn_inferior ();
  8432. }
  8433. bool
  8434. extended_remote_target::supports_disable_randomization ()
  8435. {
  8436. return packet_support (PACKET_QDisableRandomization) == PACKET_ENABLE;
  8437. }
  8438. void
  8439. remote_target::extended_remote_disable_randomization (int val)
  8440. {
  8441. struct remote_state *rs = get_remote_state ();
  8442. char *reply;
  8443. xsnprintf (rs->buf.data (), get_remote_packet_size (),
  8444. "QDisableRandomization:%x", val);
  8445. putpkt (rs->buf);
  8446. reply = remote_get_noisy_reply ();
  8447. if (*reply == '\0')
  8448. error (_("Target does not support QDisableRandomization."));
  8449. if (strcmp (reply, "OK") != 0)
  8450. error (_("Bogus QDisableRandomization reply from target: %s"), reply);
  8451. }
  8452. int
  8453. remote_target::extended_remote_run (const std::string &args)
  8454. {
  8455. struct remote_state *rs = get_remote_state ();
  8456. int len;
  8457. const char *remote_exec_file = get_remote_exec_file ();
  8458. /* If the user has disabled vRun support, or we have detected that
  8459. support is not available, do not try it. */
  8460. if (packet_support (PACKET_vRun) == PACKET_DISABLE)
  8461. return -1;
  8462. strcpy (rs->buf.data (), "vRun;");
  8463. len = strlen (rs->buf.data ());
  8464. if (strlen (remote_exec_file) * 2 + len >= get_remote_packet_size ())
  8465. error (_("Remote file name too long for run packet"));
  8466. len += 2 * bin2hex ((gdb_byte *) remote_exec_file, rs->buf.data () + len,
  8467. strlen (remote_exec_file));
  8468. if (!args.empty ())
  8469. {
  8470. int i;
  8471. gdb_argv argv (args.c_str ());
  8472. for (i = 0; argv[i] != NULL; i++)
  8473. {
  8474. if (strlen (argv[i]) * 2 + 1 + len >= get_remote_packet_size ())
  8475. error (_("Argument list too long for run packet"));
  8476. rs->buf[len++] = ';';
  8477. len += 2 * bin2hex ((gdb_byte *) argv[i], rs->buf.data () + len,
  8478. strlen (argv[i]));
  8479. }
  8480. }
  8481. rs->buf[len++] = '\0';
  8482. putpkt (rs->buf);
  8483. getpkt (&rs->buf, 0);
  8484. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_vRun]))
  8485. {
  8486. case PACKET_OK:
  8487. /* We have a wait response. All is well. */
  8488. return 0;
  8489. case PACKET_UNKNOWN:
  8490. return -1;
  8491. case PACKET_ERROR:
  8492. if (remote_exec_file[0] == '\0')
  8493. error (_("Running the default executable on the remote target failed; "
  8494. "try \"set remote exec-file\"?"));
  8495. else
  8496. error (_("Running \"%s\" on the remote target failed"),
  8497. remote_exec_file);
  8498. default:
  8499. gdb_assert_not_reached ("bad switch");
  8500. }
  8501. }
  8502. /* Helper function to send set/unset environment packets. ACTION is
  8503. either "set" or "unset". PACKET is either "QEnvironmentHexEncoded"
  8504. or "QEnvironmentUnsetVariable". VALUE is the variable to be
  8505. sent. */
  8506. void
  8507. remote_target::send_environment_packet (const char *action,
  8508. const char *packet,
  8509. const char *value)
  8510. {
  8511. remote_state *rs = get_remote_state ();
  8512. /* Convert the environment variable to an hex string, which
  8513. is the best format to be transmitted over the wire. */
  8514. std::string encoded_value = bin2hex ((const gdb_byte *) value,
  8515. strlen (value));
  8516. xsnprintf (rs->buf.data (), get_remote_packet_size (),
  8517. "%s:%s", packet, encoded_value.c_str ());
  8518. putpkt (rs->buf);
  8519. getpkt (&rs->buf, 0);
  8520. if (strcmp (rs->buf.data (), "OK") != 0)
  8521. warning (_("Unable to %s environment variable '%s' on remote."),
  8522. action, value);
  8523. }
  8524. /* Helper function to handle the QEnvironment* packets. */
  8525. void
  8526. remote_target::extended_remote_environment_support ()
  8527. {
  8528. remote_state *rs = get_remote_state ();
  8529. if (packet_support (PACKET_QEnvironmentReset) != PACKET_DISABLE)
  8530. {
  8531. putpkt ("QEnvironmentReset");
  8532. getpkt (&rs->buf, 0);
  8533. if (strcmp (rs->buf.data (), "OK") != 0)
  8534. warning (_("Unable to reset environment on remote."));
  8535. }
  8536. gdb_environ *e = &current_inferior ()->environment;
  8537. if (packet_support (PACKET_QEnvironmentHexEncoded) != PACKET_DISABLE)
  8538. for (const std::string &el : e->user_set_env ())
  8539. send_environment_packet ("set", "QEnvironmentHexEncoded",
  8540. el.c_str ());
  8541. if (packet_support (PACKET_QEnvironmentUnset) != PACKET_DISABLE)
  8542. for (const std::string &el : e->user_unset_env ())
  8543. send_environment_packet ("unset", "QEnvironmentUnset", el.c_str ());
  8544. }
  8545. /* Helper function to set the current working directory for the
  8546. inferior in the remote target. */
  8547. void
  8548. remote_target::extended_remote_set_inferior_cwd ()
  8549. {
  8550. if (packet_support (PACKET_QSetWorkingDir) != PACKET_DISABLE)
  8551. {
  8552. const std::string &inferior_cwd = current_inferior ()->cwd ();
  8553. remote_state *rs = get_remote_state ();
  8554. if (!inferior_cwd.empty ())
  8555. {
  8556. std::string hexpath
  8557. = bin2hex ((const gdb_byte *) inferior_cwd.data (),
  8558. inferior_cwd.size ());
  8559. xsnprintf (rs->buf.data (), get_remote_packet_size (),
  8560. "QSetWorkingDir:%s", hexpath.c_str ());
  8561. }
  8562. else
  8563. {
  8564. /* An empty inferior_cwd means that the user wants us to
  8565. reset the remote server's inferior's cwd. */
  8566. xsnprintf (rs->buf.data (), get_remote_packet_size (),
  8567. "QSetWorkingDir:");
  8568. }
  8569. putpkt (rs->buf);
  8570. getpkt (&rs->buf, 0);
  8571. if (packet_ok (rs->buf,
  8572. &remote_protocol_packets[PACKET_QSetWorkingDir])
  8573. != PACKET_OK)
  8574. error (_("\
  8575. Remote replied unexpectedly while setting the inferior's working\n\
  8576. directory: %s"),
  8577. rs->buf.data ());
  8578. }
  8579. }
  8580. /* In the extended protocol we want to be able to do things like
  8581. "run" and have them basically work as expected. So we need
  8582. a special create_inferior function. We support changing the
  8583. executable file and the command line arguments, but not the
  8584. environment. */
  8585. void
  8586. extended_remote_target::create_inferior (const char *exec_file,
  8587. const std::string &args,
  8588. char **env, int from_tty)
  8589. {
  8590. int run_worked;
  8591. char *stop_reply;
  8592. struct remote_state *rs = get_remote_state ();
  8593. const char *remote_exec_file = get_remote_exec_file ();
  8594. /* If running asynchronously, register the target file descriptor
  8595. with the event loop. */
  8596. if (target_can_async_p ())
  8597. target_async (1);
  8598. /* Disable address space randomization if requested (and supported). */
  8599. if (supports_disable_randomization ())
  8600. extended_remote_disable_randomization (disable_randomization);
  8601. /* If startup-with-shell is on, we inform gdbserver to start the
  8602. remote inferior using a shell. */
  8603. if (packet_support (PACKET_QStartupWithShell) != PACKET_DISABLE)
  8604. {
  8605. xsnprintf (rs->buf.data (), get_remote_packet_size (),
  8606. "QStartupWithShell:%d", startup_with_shell ? 1 : 0);
  8607. putpkt (rs->buf);
  8608. getpkt (&rs->buf, 0);
  8609. if (strcmp (rs->buf.data (), "OK") != 0)
  8610. error (_("\
  8611. Remote replied unexpectedly while setting startup-with-shell: %s"),
  8612. rs->buf.data ());
  8613. }
  8614. extended_remote_environment_support ();
  8615. extended_remote_set_inferior_cwd ();
  8616. /* Now restart the remote server. */
  8617. run_worked = extended_remote_run (args) != -1;
  8618. if (!run_worked)
  8619. {
  8620. /* vRun was not supported. Fail if we need it to do what the
  8621. user requested. */
  8622. if (remote_exec_file[0])
  8623. error (_("Remote target does not support \"set remote exec-file\""));
  8624. if (!args.empty ())
  8625. error (_("Remote target does not support \"set args\" or run ARGS"));
  8626. /* Fall back to "R". */
  8627. extended_remote_restart ();
  8628. }
  8629. /* vRun's success return is a stop reply. */
  8630. stop_reply = run_worked ? rs->buf.data () : NULL;
  8631. add_current_inferior_and_thread (stop_reply);
  8632. /* Get updated offsets, if the stub uses qOffsets. */
  8633. get_offsets ();
  8634. }
  8635. /* Given a location's target info BP_TGT and the packet buffer BUF, output
  8636. the list of conditions (in agent expression bytecode format), if any, the
  8637. target needs to evaluate. The output is placed into the packet buffer
  8638. started from BUF and ended at BUF_END. */
  8639. static int
  8640. remote_add_target_side_condition (struct gdbarch *gdbarch,
  8641. struct bp_target_info *bp_tgt, char *buf,
  8642. char *buf_end)
  8643. {
  8644. if (bp_tgt->conditions.empty ())
  8645. return 0;
  8646. buf += strlen (buf);
  8647. xsnprintf (buf, buf_end - buf, "%s", ";");
  8648. buf++;
  8649. /* Send conditions to the target. */
  8650. for (agent_expr *aexpr : bp_tgt->conditions)
  8651. {
  8652. xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len);
  8653. buf += strlen (buf);
  8654. for (int i = 0; i < aexpr->len; ++i)
  8655. buf = pack_hex_byte (buf, aexpr->buf[i]);
  8656. *buf = '\0';
  8657. }
  8658. return 0;
  8659. }
  8660. static void
  8661. remote_add_target_side_commands (struct gdbarch *gdbarch,
  8662. struct bp_target_info *bp_tgt, char *buf)
  8663. {
  8664. if (bp_tgt->tcommands.empty ())
  8665. return;
  8666. buf += strlen (buf);
  8667. sprintf (buf, ";cmds:%x,", bp_tgt->persist);
  8668. buf += strlen (buf);
  8669. /* Concatenate all the agent expressions that are commands into the
  8670. cmds parameter. */
  8671. for (agent_expr *aexpr : bp_tgt->tcommands)
  8672. {
  8673. sprintf (buf, "X%x,", aexpr->len);
  8674. buf += strlen (buf);
  8675. for (int i = 0; i < aexpr->len; ++i)
  8676. buf = pack_hex_byte (buf, aexpr->buf[i]);
  8677. *buf = '\0';
  8678. }
  8679. }
  8680. /* Insert a breakpoint. On targets that have software breakpoint
  8681. support, we ask the remote target to do the work; on targets
  8682. which don't, we insert a traditional memory breakpoint. */
  8683. int
  8684. remote_target::insert_breakpoint (struct gdbarch *gdbarch,
  8685. struct bp_target_info *bp_tgt)
  8686. {
  8687. /* Try the "Z" s/w breakpoint packet if it is not already disabled.
  8688. If it succeeds, then set the support to PACKET_ENABLE. If it
  8689. fails, and the user has explicitly requested the Z support then
  8690. report an error, otherwise, mark it disabled and go on. */
  8691. if (packet_support (PACKET_Z0) != PACKET_DISABLE)
  8692. {
  8693. CORE_ADDR addr = bp_tgt->reqstd_address;
  8694. struct remote_state *rs;
  8695. char *p, *endbuf;
  8696. /* Make sure the remote is pointing at the right process, if
  8697. necessary. */
  8698. if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
  8699. set_general_process ();
  8700. rs = get_remote_state ();
  8701. p = rs->buf.data ();
  8702. endbuf = p + get_remote_packet_size ();
  8703. *(p++) = 'Z';
  8704. *(p++) = '0';
  8705. *(p++) = ',';
  8706. addr = (ULONGEST) remote_address_masked (addr);
  8707. p += hexnumstr (p, addr);
  8708. xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
  8709. if (supports_evaluation_of_breakpoint_conditions ())
  8710. remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
  8711. if (can_run_breakpoint_commands ())
  8712. remote_add_target_side_commands (gdbarch, bp_tgt, p);
  8713. putpkt (rs->buf);
  8714. getpkt (&rs->buf, 0);
  8715. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0]))
  8716. {
  8717. case PACKET_ERROR:
  8718. return -1;
  8719. case PACKET_OK:
  8720. return 0;
  8721. case PACKET_UNKNOWN:
  8722. break;
  8723. }
  8724. }
  8725. /* If this breakpoint has target-side commands but this stub doesn't
  8726. support Z0 packets, throw error. */
  8727. if (!bp_tgt->tcommands.empty ())
  8728. throw_error (NOT_SUPPORTED_ERROR, _("\
  8729. Target doesn't support breakpoints that have target side commands."));
  8730. return memory_insert_breakpoint (this, gdbarch, bp_tgt);
  8731. }
  8732. int
  8733. remote_target::remove_breakpoint (struct gdbarch *gdbarch,
  8734. struct bp_target_info *bp_tgt,
  8735. enum remove_bp_reason reason)
  8736. {
  8737. CORE_ADDR addr = bp_tgt->placed_address;
  8738. struct remote_state *rs = get_remote_state ();
  8739. if (packet_support (PACKET_Z0) != PACKET_DISABLE)
  8740. {
  8741. char *p = rs->buf.data ();
  8742. char *endbuf = p + get_remote_packet_size ();
  8743. /* Make sure the remote is pointing at the right process, if
  8744. necessary. */
  8745. if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
  8746. set_general_process ();
  8747. *(p++) = 'z';
  8748. *(p++) = '0';
  8749. *(p++) = ',';
  8750. addr = (ULONGEST) remote_address_masked (bp_tgt->placed_address);
  8751. p += hexnumstr (p, addr);
  8752. xsnprintf (p, endbuf - p, ",%d", bp_tgt->kind);
  8753. putpkt (rs->buf);
  8754. getpkt (&rs->buf, 0);
  8755. return (rs->buf[0] == 'E');
  8756. }
  8757. return memory_remove_breakpoint (this, gdbarch, bp_tgt, reason);
  8758. }
  8759. static enum Z_packet_type
  8760. watchpoint_to_Z_packet (int type)
  8761. {
  8762. switch (type)
  8763. {
  8764. case hw_write:
  8765. return Z_PACKET_WRITE_WP;
  8766. break;
  8767. case hw_read:
  8768. return Z_PACKET_READ_WP;
  8769. break;
  8770. case hw_access:
  8771. return Z_PACKET_ACCESS_WP;
  8772. break;
  8773. default:
  8774. internal_error (__FILE__, __LINE__,
  8775. _("hw_bp_to_z: bad watchpoint type %d"), type);
  8776. }
  8777. }
  8778. int
  8779. remote_target::insert_watchpoint (CORE_ADDR addr, int len,
  8780. enum target_hw_bp_type type, struct expression *cond)
  8781. {
  8782. struct remote_state *rs = get_remote_state ();
  8783. char *endbuf = rs->buf.data () + get_remote_packet_size ();
  8784. char *p;
  8785. enum Z_packet_type packet = watchpoint_to_Z_packet (type);
  8786. if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
  8787. return 1;
  8788. /* Make sure the remote is pointing at the right process, if
  8789. necessary. */
  8790. if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
  8791. set_general_process ();
  8792. xsnprintf (rs->buf.data (), endbuf - rs->buf.data (), "Z%x,", packet);
  8793. p = strchr (rs->buf.data (), '\0');
  8794. addr = remote_address_masked (addr);
  8795. p += hexnumstr (p, (ULONGEST) addr);
  8796. xsnprintf (p, endbuf - p, ",%x", len);
  8797. putpkt (rs->buf);
  8798. getpkt (&rs->buf, 0);
  8799. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
  8800. {
  8801. case PACKET_ERROR:
  8802. return -1;
  8803. case PACKET_UNKNOWN:
  8804. return 1;
  8805. case PACKET_OK:
  8806. return 0;
  8807. }
  8808. internal_error (__FILE__, __LINE__,
  8809. _("remote_insert_watchpoint: reached end of function"));
  8810. }
  8811. bool
  8812. remote_target::watchpoint_addr_within_range (CORE_ADDR addr,
  8813. CORE_ADDR start, int length)
  8814. {
  8815. CORE_ADDR diff = remote_address_masked (addr - start);
  8816. return diff < length;
  8817. }
  8818. int
  8819. remote_target::remove_watchpoint (CORE_ADDR addr, int len,
  8820. enum target_hw_bp_type type, struct expression *cond)
  8821. {
  8822. struct remote_state *rs = get_remote_state ();
  8823. char *endbuf = rs->buf.data () + get_remote_packet_size ();
  8824. char *p;
  8825. enum Z_packet_type packet = watchpoint_to_Z_packet (type);
  8826. if (packet_support (PACKET_Z0 + packet) == PACKET_DISABLE)
  8827. return -1;
  8828. /* Make sure the remote is pointing at the right process, if
  8829. necessary. */
  8830. if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
  8831. set_general_process ();
  8832. xsnprintf (rs->buf.data (), endbuf - rs->buf.data (), "z%x,", packet);
  8833. p = strchr (rs->buf.data (), '\0');
  8834. addr = remote_address_masked (addr);
  8835. p += hexnumstr (p, (ULONGEST) addr);
  8836. xsnprintf (p, endbuf - p, ",%x", len);
  8837. putpkt (rs->buf);
  8838. getpkt (&rs->buf, 0);
  8839. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z0 + packet]))
  8840. {
  8841. case PACKET_ERROR:
  8842. case PACKET_UNKNOWN:
  8843. return -1;
  8844. case PACKET_OK:
  8845. return 0;
  8846. }
  8847. internal_error (__FILE__, __LINE__,
  8848. _("remote_remove_watchpoint: reached end of function"));
  8849. }
  8850. static int remote_hw_watchpoint_limit = -1;
  8851. static int remote_hw_watchpoint_length_limit = -1;
  8852. static int remote_hw_breakpoint_limit = -1;
  8853. int
  8854. remote_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
  8855. {
  8856. if (remote_hw_watchpoint_length_limit == 0)
  8857. return 0;
  8858. else if (remote_hw_watchpoint_length_limit < 0)
  8859. return 1;
  8860. else if (len <= remote_hw_watchpoint_length_limit)
  8861. return 1;
  8862. else
  8863. return 0;
  8864. }
  8865. int
  8866. remote_target::can_use_hw_breakpoint (enum bptype type, int cnt, int ot)
  8867. {
  8868. if (type == bp_hardware_breakpoint)
  8869. {
  8870. if (remote_hw_breakpoint_limit == 0)
  8871. return 0;
  8872. else if (remote_hw_breakpoint_limit < 0)
  8873. return 1;
  8874. else if (cnt <= remote_hw_breakpoint_limit)
  8875. return 1;
  8876. }
  8877. else
  8878. {
  8879. if (remote_hw_watchpoint_limit == 0)
  8880. return 0;
  8881. else if (remote_hw_watchpoint_limit < 0)
  8882. return 1;
  8883. else if (ot)
  8884. return -1;
  8885. else if (cnt <= remote_hw_watchpoint_limit)
  8886. return 1;
  8887. }
  8888. return -1;
  8889. }
  8890. /* The to_stopped_by_sw_breakpoint method of target remote. */
  8891. bool
  8892. remote_target::stopped_by_sw_breakpoint ()
  8893. {
  8894. struct thread_info *thread = inferior_thread ();
  8895. return (thread->priv != NULL
  8896. && (get_remote_thread_info (thread)->stop_reason
  8897. == TARGET_STOPPED_BY_SW_BREAKPOINT));
  8898. }
  8899. /* The to_supports_stopped_by_sw_breakpoint method of target
  8900. remote. */
  8901. bool
  8902. remote_target::supports_stopped_by_sw_breakpoint ()
  8903. {
  8904. return (packet_support (PACKET_swbreak_feature) == PACKET_ENABLE);
  8905. }
  8906. /* The to_stopped_by_hw_breakpoint method of target remote. */
  8907. bool
  8908. remote_target::stopped_by_hw_breakpoint ()
  8909. {
  8910. struct thread_info *thread = inferior_thread ();
  8911. return (thread->priv != NULL
  8912. && (get_remote_thread_info (thread)->stop_reason
  8913. == TARGET_STOPPED_BY_HW_BREAKPOINT));
  8914. }
  8915. /* The to_supports_stopped_by_hw_breakpoint method of target
  8916. remote. */
  8917. bool
  8918. remote_target::supports_stopped_by_hw_breakpoint ()
  8919. {
  8920. return (packet_support (PACKET_hwbreak_feature) == PACKET_ENABLE);
  8921. }
  8922. bool
  8923. remote_target::stopped_by_watchpoint ()
  8924. {
  8925. struct thread_info *thread = inferior_thread ();
  8926. return (thread->priv != NULL
  8927. && (get_remote_thread_info (thread)->stop_reason
  8928. == TARGET_STOPPED_BY_WATCHPOINT));
  8929. }
  8930. bool
  8931. remote_target::stopped_data_address (CORE_ADDR *addr_p)
  8932. {
  8933. struct thread_info *thread = inferior_thread ();
  8934. if (thread->priv != NULL
  8935. && (get_remote_thread_info (thread)->stop_reason
  8936. == TARGET_STOPPED_BY_WATCHPOINT))
  8937. {
  8938. *addr_p = get_remote_thread_info (thread)->watch_data_address;
  8939. return true;
  8940. }
  8941. return false;
  8942. }
  8943. int
  8944. remote_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
  8945. struct bp_target_info *bp_tgt)
  8946. {
  8947. CORE_ADDR addr = bp_tgt->reqstd_address;
  8948. struct remote_state *rs;
  8949. char *p, *endbuf;
  8950. char *message;
  8951. if (packet_support (PACKET_Z1) == PACKET_DISABLE)
  8952. return -1;
  8953. /* Make sure the remote is pointing at the right process, if
  8954. necessary. */
  8955. if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
  8956. set_general_process ();
  8957. rs = get_remote_state ();
  8958. p = rs->buf.data ();
  8959. endbuf = p + get_remote_packet_size ();
  8960. *(p++) = 'Z';
  8961. *(p++) = '1';
  8962. *(p++) = ',';
  8963. addr = remote_address_masked (addr);
  8964. p += hexnumstr (p, (ULONGEST) addr);
  8965. xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
  8966. if (supports_evaluation_of_breakpoint_conditions ())
  8967. remote_add_target_side_condition (gdbarch, bp_tgt, p, endbuf);
  8968. if (can_run_breakpoint_commands ())
  8969. remote_add_target_side_commands (gdbarch, bp_tgt, p);
  8970. putpkt (rs->buf);
  8971. getpkt (&rs->buf, 0);
  8972. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
  8973. {
  8974. case PACKET_ERROR:
  8975. if (rs->buf[1] == '.')
  8976. {
  8977. message = strchr (&rs->buf[2], '.');
  8978. if (message)
  8979. error (_("Remote failure reply: %s"), message + 1);
  8980. }
  8981. return -1;
  8982. case PACKET_UNKNOWN:
  8983. return -1;
  8984. case PACKET_OK:
  8985. return 0;
  8986. }
  8987. internal_error (__FILE__, __LINE__,
  8988. _("remote_insert_hw_breakpoint: reached end of function"));
  8989. }
  8990. int
  8991. remote_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
  8992. struct bp_target_info *bp_tgt)
  8993. {
  8994. CORE_ADDR addr;
  8995. struct remote_state *rs = get_remote_state ();
  8996. char *p = rs->buf.data ();
  8997. char *endbuf = p + get_remote_packet_size ();
  8998. if (packet_support (PACKET_Z1) == PACKET_DISABLE)
  8999. return -1;
  9000. /* Make sure the remote is pointing at the right process, if
  9001. necessary. */
  9002. if (!gdbarch_has_global_breakpoints (target_gdbarch ()))
  9003. set_general_process ();
  9004. *(p++) = 'z';
  9005. *(p++) = '1';
  9006. *(p++) = ',';
  9007. addr = remote_address_masked (bp_tgt->placed_address);
  9008. p += hexnumstr (p, (ULONGEST) addr);
  9009. xsnprintf (p, endbuf - p, ",%x", bp_tgt->kind);
  9010. putpkt (rs->buf);
  9011. getpkt (&rs->buf, 0);
  9012. switch (packet_ok (rs->buf, &remote_protocol_packets[PACKET_Z1]))
  9013. {
  9014. case PACKET_ERROR:
  9015. case PACKET_UNKNOWN:
  9016. return -1;
  9017. case PACKET_OK:
  9018. return 0;
  9019. }
  9020. internal_error (__FILE__, __LINE__,
  9021. _("remote_remove_hw_breakpoint: reached end of function"));
  9022. }
  9023. /* Verify memory using the "qCRC:" request. */
  9024. int
  9025. remote_target::verify_memory (const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
  9026. {
  9027. struct remote_state *rs = get_remote_state ();
  9028. unsigned long host_crc, target_crc;
  9029. char *tmp;
  9030. /* It doesn't make sense to use qCRC if the remote target is
  9031. connected but not running. */
  9032. if (target_has_execution ()
  9033. && packet_support (PACKET_qCRC) != PACKET_DISABLE)
  9034. {
  9035. enum packet_result result;
  9036. /* Make sure the remote is pointing at the right process. */
  9037. set_general_process ();
  9038. /* FIXME: assumes lma can fit into long. */
  9039. xsnprintf (rs->buf.data (), get_remote_packet_size (), "qCRC:%lx,%lx",
  9040. (long) lma, (long) size);
  9041. putpkt (rs->buf);
  9042. /* Be clever; compute the host_crc before waiting for target
  9043. reply. */
  9044. host_crc = xcrc32 (data, size, 0xffffffff);
  9045. getpkt (&rs->buf, 0);
  9046. result = packet_ok (rs->buf,
  9047. &remote_protocol_packets[PACKET_qCRC]);
  9048. if (result == PACKET_ERROR)
  9049. return -1;
  9050. else if (result == PACKET_OK)
  9051. {
  9052. for (target_crc = 0, tmp = &rs->buf[1]; *tmp; tmp++)
  9053. target_crc = target_crc * 16 + fromhex (*tmp);
  9054. return (host_crc == target_crc);
  9055. }
  9056. }
  9057. return simple_verify_memory (this, data, lma, size);
  9058. }
  9059. /* compare-sections command
  9060. With no arguments, compares each loadable section in the exec bfd
  9061. with the same memory range on the target, and reports mismatches.
  9062. Useful for verifying the image on the target against the exec file. */
  9063. static void
  9064. compare_sections_command (const char *args, int from_tty)
  9065. {
  9066. asection *s;
  9067. const char *sectname;
  9068. bfd_size_type size;
  9069. bfd_vma lma;
  9070. int matched = 0;
  9071. int mismatched = 0;
  9072. int res;
  9073. int read_only = 0;
  9074. if (!current_program_space->exec_bfd ())
  9075. error (_("command cannot be used without an exec file"));
  9076. if (args != NULL && strcmp (args, "-r") == 0)
  9077. {
  9078. read_only = 1;
  9079. args = NULL;
  9080. }
  9081. for (s = current_program_space->exec_bfd ()->sections; s; s = s->next)
  9082. {
  9083. if (!(s->flags & SEC_LOAD))
  9084. continue; /* Skip non-loadable section. */
  9085. if (read_only && (s->flags & SEC_READONLY) == 0)
  9086. continue; /* Skip writeable sections */
  9087. size = bfd_section_size (s);
  9088. if (size == 0)
  9089. continue; /* Skip zero-length section. */
  9090. sectname = bfd_section_name (s);
  9091. if (args && strcmp (args, sectname) != 0)
  9092. continue; /* Not the section selected by user. */
  9093. matched = 1; /* Do this section. */
  9094. lma = s->lma;
  9095. gdb::byte_vector sectdata (size);
  9096. bfd_get_section_contents (current_program_space->exec_bfd (), s,
  9097. sectdata.data (), 0, size);
  9098. res = target_verify_memory (sectdata.data (), lma, size);
  9099. if (res == -1)
  9100. error (_("target memory fault, section %s, range %s -- %s"), sectname,
  9101. paddress (target_gdbarch (), lma),
  9102. paddress (target_gdbarch (), lma + size));
  9103. gdb_printf ("Section %s, range %s -- %s: ", sectname,
  9104. paddress (target_gdbarch (), lma),
  9105. paddress (target_gdbarch (), lma + size));
  9106. if (res)
  9107. gdb_printf ("matched.\n");
  9108. else
  9109. {
  9110. gdb_printf ("MIS-MATCHED!\n");
  9111. mismatched++;
  9112. }
  9113. }
  9114. if (mismatched > 0)
  9115. warning (_("One or more sections of the target image does not match\n\
  9116. the loaded file\n"));
  9117. if (args && !matched)
  9118. gdb_printf (_("No loaded section named '%s'.\n"), args);
  9119. }
  9120. /* Write LEN bytes from WRITEBUF into OBJECT_NAME/ANNEX at OFFSET
  9121. into remote target. The number of bytes written to the remote
  9122. target is returned, or -1 for error. */
  9123. target_xfer_status
  9124. remote_target::remote_write_qxfer (const char *object_name,
  9125. const char *annex, const gdb_byte *writebuf,
  9126. ULONGEST offset, LONGEST len,
  9127. ULONGEST *xfered_len,
  9128. struct packet_config *packet)
  9129. {
  9130. int i, buf_len;
  9131. ULONGEST n;
  9132. struct remote_state *rs = get_remote_state ();
  9133. int max_size = get_memory_write_packet_size ();
  9134. if (packet_config_support (packet) == PACKET_DISABLE)
  9135. return TARGET_XFER_E_IO;
  9136. /* Insert header. */
  9137. i = snprintf (rs->buf.data (), max_size,
  9138. "qXfer:%s:write:%s:%s:",
  9139. object_name, annex ? annex : "",
  9140. phex_nz (offset, sizeof offset));
  9141. max_size -= (i + 1);
  9142. /* Escape as much data as fits into rs->buf. */
  9143. buf_len = remote_escape_output
  9144. (writebuf, len, 1, (gdb_byte *) rs->buf.data () + i, &max_size, max_size);
  9145. if (putpkt_binary (rs->buf.data (), i + buf_len) < 0
  9146. || getpkt_sane (&rs->buf, 0) < 0
  9147. || packet_ok (rs->buf, packet) != PACKET_OK)
  9148. return TARGET_XFER_E_IO;
  9149. unpack_varlen_hex (rs->buf.data (), &n);
  9150. *xfered_len = n;
  9151. return (*xfered_len != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
  9152. }
  9153. /* Read OBJECT_NAME/ANNEX from the remote target using a qXfer packet.
  9154. Data at OFFSET, of up to LEN bytes, is read into READBUF; the
  9155. number of bytes read is returned, or 0 for EOF, or -1 for error.
  9156. The number of bytes read may be less than LEN without indicating an
  9157. EOF. PACKET is checked and updated to indicate whether the remote
  9158. target supports this object. */
  9159. target_xfer_status
  9160. remote_target::remote_read_qxfer (const char *object_name,
  9161. const char *annex,
  9162. gdb_byte *readbuf, ULONGEST offset,
  9163. LONGEST len,
  9164. ULONGEST *xfered_len,
  9165. struct packet_config *packet)
  9166. {
  9167. struct remote_state *rs = get_remote_state ();
  9168. LONGEST i, n, packet_len;
  9169. if (packet_config_support (packet) == PACKET_DISABLE)
  9170. return TARGET_XFER_E_IO;
  9171. /* Check whether we've cached an end-of-object packet that matches
  9172. this request. */
  9173. if (rs->finished_object)
  9174. {
  9175. if (strcmp (object_name, rs->finished_object) == 0
  9176. && strcmp (annex ? annex : "", rs->finished_annex) == 0
  9177. && offset == rs->finished_offset)
  9178. return TARGET_XFER_EOF;
  9179. /* Otherwise, we're now reading something different. Discard
  9180. the cache. */
  9181. xfree (rs->finished_object);
  9182. xfree (rs->finished_annex);
  9183. rs->finished_object = NULL;
  9184. rs->finished_annex = NULL;
  9185. }
  9186. /* Request only enough to fit in a single packet. The actual data
  9187. may not, since we don't know how much of it will need to be escaped;
  9188. the target is free to respond with slightly less data. We subtract
  9189. five to account for the response type and the protocol frame. */
  9190. n = std::min<LONGEST> (get_remote_packet_size () - 5, len);
  9191. snprintf (rs->buf.data (), get_remote_packet_size () - 4,
  9192. "qXfer:%s:read:%s:%s,%s",
  9193. object_name, annex ? annex : "",
  9194. phex_nz (offset, sizeof offset),
  9195. phex_nz (n, sizeof n));
  9196. i = putpkt (rs->buf);
  9197. if (i < 0)
  9198. return TARGET_XFER_E_IO;
  9199. rs->buf[0] = '\0';
  9200. packet_len = getpkt_sane (&rs->buf, 0);
  9201. if (packet_len < 0 || packet_ok (rs->buf, packet) != PACKET_OK)
  9202. return TARGET_XFER_E_IO;
  9203. if (rs->buf[0] != 'l' && rs->buf[0] != 'm')
  9204. error (_("Unknown remote qXfer reply: %s"), rs->buf.data ());
  9205. /* 'm' means there is (or at least might be) more data after this
  9206. batch. That does not make sense unless there's at least one byte
  9207. of data in this reply. */
  9208. if (rs->buf[0] == 'm' && packet_len == 1)
  9209. error (_("Remote qXfer reply contained no data."));
  9210. /* Got some data. */
  9211. i = remote_unescape_input ((gdb_byte *) rs->buf.data () + 1,
  9212. packet_len - 1, readbuf, n);
  9213. /* 'l' is an EOF marker, possibly including a final block of data,
  9214. or possibly empty. If we have the final block of a non-empty
  9215. object, record this fact to bypass a subsequent partial read. */
  9216. if (rs->buf[0] == 'l' && offset + i > 0)
  9217. {
  9218. rs->finished_object = xstrdup (object_name);
  9219. rs->finished_annex = xstrdup (annex ? annex : "");
  9220. rs->finished_offset = offset + i;
  9221. }
  9222. if (i == 0)
  9223. return TARGET_XFER_EOF;
  9224. else
  9225. {
  9226. *xfered_len = i;
  9227. return TARGET_XFER_OK;
  9228. }
  9229. }
  9230. enum target_xfer_status
  9231. remote_target::xfer_partial (enum target_object object,
  9232. const char *annex, gdb_byte *readbuf,
  9233. const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  9234. ULONGEST *xfered_len)
  9235. {
  9236. struct remote_state *rs;
  9237. int i;
  9238. char *p2;
  9239. char query_type;
  9240. int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
  9241. set_remote_traceframe ();
  9242. set_general_thread (inferior_ptid);
  9243. rs = get_remote_state ();
  9244. /* Handle memory using the standard memory routines. */
  9245. if (object == TARGET_OBJECT_MEMORY)
  9246. {
  9247. /* If the remote target is connected but not running, we should
  9248. pass this request down to a lower stratum (e.g. the executable
  9249. file). */
  9250. if (!target_has_execution ())
  9251. return TARGET_XFER_EOF;
  9252. if (writebuf != NULL)
  9253. return remote_write_bytes (offset, writebuf, len, unit_size,
  9254. xfered_len);
  9255. else
  9256. return remote_read_bytes (offset, readbuf, len, unit_size,
  9257. xfered_len);
  9258. }
  9259. /* Handle extra signal info using qxfer packets. */
  9260. if (object == TARGET_OBJECT_SIGNAL_INFO)
  9261. {
  9262. if (readbuf)
  9263. return remote_read_qxfer ("siginfo", annex, readbuf, offset, len,
  9264. xfered_len, &remote_protocol_packets
  9265. [PACKET_qXfer_siginfo_read]);
  9266. else
  9267. return remote_write_qxfer ("siginfo", annex,
  9268. writebuf, offset, len, xfered_len,
  9269. &remote_protocol_packets
  9270. [PACKET_qXfer_siginfo_write]);
  9271. }
  9272. if (object == TARGET_OBJECT_STATIC_TRACE_DATA)
  9273. {
  9274. if (readbuf)
  9275. return remote_read_qxfer ("statictrace", annex,
  9276. readbuf, offset, len, xfered_len,
  9277. &remote_protocol_packets
  9278. [PACKET_qXfer_statictrace_read]);
  9279. else
  9280. return TARGET_XFER_E_IO;
  9281. }
  9282. /* Only handle flash writes. */
  9283. if (writebuf != NULL)
  9284. {
  9285. switch (object)
  9286. {
  9287. case TARGET_OBJECT_FLASH:
  9288. return remote_flash_write (offset, len, xfered_len,
  9289. writebuf);
  9290. default:
  9291. return TARGET_XFER_E_IO;
  9292. }
  9293. }
  9294. /* Map pre-existing objects onto letters. DO NOT do this for new
  9295. objects!!! Instead specify new query packets. */
  9296. switch (object)
  9297. {
  9298. case TARGET_OBJECT_AVR:
  9299. query_type = 'R';
  9300. break;
  9301. case TARGET_OBJECT_AUXV:
  9302. gdb_assert (annex == NULL);
  9303. return remote_read_qxfer ("auxv", annex, readbuf, offset, len,
  9304. xfered_len,
  9305. &remote_protocol_packets[PACKET_qXfer_auxv]);
  9306. case TARGET_OBJECT_AVAILABLE_FEATURES:
  9307. return remote_read_qxfer
  9308. ("features", annex, readbuf, offset, len, xfered_len,
  9309. &remote_protocol_packets[PACKET_qXfer_features]);
  9310. case TARGET_OBJECT_LIBRARIES:
  9311. return remote_read_qxfer
  9312. ("libraries", annex, readbuf, offset, len, xfered_len,
  9313. &remote_protocol_packets[PACKET_qXfer_libraries]);
  9314. case TARGET_OBJECT_LIBRARIES_SVR4:
  9315. return remote_read_qxfer
  9316. ("libraries-svr4", annex, readbuf, offset, len, xfered_len,
  9317. &remote_protocol_packets[PACKET_qXfer_libraries_svr4]);
  9318. case TARGET_OBJECT_MEMORY_MAP:
  9319. gdb_assert (annex == NULL);
  9320. return remote_read_qxfer ("memory-map", annex, readbuf, offset, len,
  9321. xfered_len,
  9322. &remote_protocol_packets[PACKET_qXfer_memory_map]);
  9323. case TARGET_OBJECT_OSDATA:
  9324. /* Should only get here if we're connected. */
  9325. gdb_assert (rs->remote_desc);
  9326. return remote_read_qxfer
  9327. ("osdata", annex, readbuf, offset, len, xfered_len,
  9328. &remote_protocol_packets[PACKET_qXfer_osdata]);
  9329. case TARGET_OBJECT_THREADS:
  9330. gdb_assert (annex == NULL);
  9331. return remote_read_qxfer ("threads", annex, readbuf, offset, len,
  9332. xfered_len,
  9333. &remote_protocol_packets[PACKET_qXfer_threads]);
  9334. case TARGET_OBJECT_TRACEFRAME_INFO:
  9335. gdb_assert (annex == NULL);
  9336. return remote_read_qxfer
  9337. ("traceframe-info", annex, readbuf, offset, len, xfered_len,
  9338. &remote_protocol_packets[PACKET_qXfer_traceframe_info]);
  9339. case TARGET_OBJECT_FDPIC:
  9340. return remote_read_qxfer ("fdpic", annex, readbuf, offset, len,
  9341. xfered_len,
  9342. &remote_protocol_packets[PACKET_qXfer_fdpic]);
  9343. case TARGET_OBJECT_OPENVMS_UIB:
  9344. return remote_read_qxfer ("uib", annex, readbuf, offset, len,
  9345. xfered_len,
  9346. &remote_protocol_packets[PACKET_qXfer_uib]);
  9347. case TARGET_OBJECT_BTRACE:
  9348. return remote_read_qxfer ("btrace", annex, readbuf, offset, len,
  9349. xfered_len,
  9350. &remote_protocol_packets[PACKET_qXfer_btrace]);
  9351. case TARGET_OBJECT_BTRACE_CONF:
  9352. return remote_read_qxfer ("btrace-conf", annex, readbuf, offset,
  9353. len, xfered_len,
  9354. &remote_protocol_packets[PACKET_qXfer_btrace_conf]);
  9355. case TARGET_OBJECT_EXEC_FILE:
  9356. return remote_read_qxfer ("exec-file", annex, readbuf, offset,
  9357. len, xfered_len,
  9358. &remote_protocol_packets[PACKET_qXfer_exec_file]);
  9359. default:
  9360. return TARGET_XFER_E_IO;
  9361. }
  9362. /* Minimum outbuf size is get_remote_packet_size (). If LEN is not
  9363. large enough let the caller deal with it. */
  9364. if (len < get_remote_packet_size ())
  9365. return TARGET_XFER_E_IO;
  9366. len = get_remote_packet_size ();
  9367. /* Except for querying the minimum buffer size, target must be open. */
  9368. if (!rs->remote_desc)
  9369. error (_("remote query is only available after target open"));
  9370. gdb_assert (annex != NULL);
  9371. gdb_assert (readbuf != NULL);
  9372. p2 = rs->buf.data ();
  9373. *p2++ = 'q';
  9374. *p2++ = query_type;
  9375. /* We used one buffer char for the remote protocol q command and
  9376. another for the query type. As the remote protocol encapsulation
  9377. uses 4 chars plus one extra in case we are debugging
  9378. (remote_debug), we have PBUFZIZ - 7 left to pack the query
  9379. string. */
  9380. i = 0;
  9381. while (annex[i] && (i < (get_remote_packet_size () - 8)))
  9382. {
  9383. /* Bad caller may have sent forbidden characters. */
  9384. gdb_assert (isprint (annex[i]) && annex[i] != '$' && annex[i] != '#');
  9385. *p2++ = annex[i];
  9386. i++;
  9387. }
  9388. *p2 = '\0';
  9389. gdb_assert (annex[i] == '\0');
  9390. i = putpkt (rs->buf);
  9391. if (i < 0)
  9392. return TARGET_XFER_E_IO;
  9393. getpkt (&rs->buf, 0);
  9394. strcpy ((char *) readbuf, rs->buf.data ());
  9395. *xfered_len = strlen ((char *) readbuf);
  9396. return (*xfered_len != 0) ? TARGET_XFER_OK : TARGET_XFER_EOF;
  9397. }
  9398. /* Implementation of to_get_memory_xfer_limit. */
  9399. ULONGEST
  9400. remote_target::get_memory_xfer_limit ()
  9401. {
  9402. return get_memory_write_packet_size ();
  9403. }
  9404. int
  9405. remote_target::search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
  9406. const gdb_byte *pattern, ULONGEST pattern_len,
  9407. CORE_ADDR *found_addrp)
  9408. {
  9409. int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
  9410. struct remote_state *rs = get_remote_state ();
  9411. int max_size = get_memory_write_packet_size ();
  9412. struct packet_config *packet =
  9413. &remote_protocol_packets[PACKET_qSearch_memory];
  9414. /* Number of packet bytes used to encode the pattern;
  9415. this could be more than PATTERN_LEN due to escape characters. */
  9416. int escaped_pattern_len;
  9417. /* Amount of pattern that was encodable in the packet. */
  9418. int used_pattern_len;
  9419. int i;
  9420. int found;
  9421. ULONGEST found_addr;
  9422. auto read_memory = [=] (CORE_ADDR addr, gdb_byte *result, size_t len)
  9423. {
  9424. return (target_read (this, TARGET_OBJECT_MEMORY, NULL, result, addr, len)
  9425. == len);
  9426. };
  9427. /* Don't go to the target if we don't have to. This is done before
  9428. checking packet_config_support to avoid the possibility that a
  9429. success for this edge case means the facility works in
  9430. general. */
  9431. if (pattern_len > search_space_len)
  9432. return 0;
  9433. if (pattern_len == 0)
  9434. {
  9435. *found_addrp = start_addr;
  9436. return 1;
  9437. }
  9438. /* If we already know the packet isn't supported, fall back to the simple
  9439. way of searching memory. */
  9440. if (packet_config_support (packet) == PACKET_DISABLE)
  9441. {
  9442. /* Target doesn't provided special support, fall back and use the
  9443. standard support (copy memory and do the search here). */
  9444. return simple_search_memory (read_memory, start_addr, search_space_len,
  9445. pattern, pattern_len, found_addrp);
  9446. }
  9447. /* Make sure the remote is pointing at the right process. */
  9448. set_general_process ();
  9449. /* Insert header. */
  9450. i = snprintf (rs->buf.data (), max_size,
  9451. "qSearch:memory:%s;%s;",
  9452. phex_nz (start_addr, addr_size),
  9453. phex_nz (search_space_len, sizeof (search_space_len)));
  9454. max_size -= (i + 1);
  9455. /* Escape as much data as fits into rs->buf. */
  9456. escaped_pattern_len =
  9457. remote_escape_output (pattern, pattern_len, 1,
  9458. (gdb_byte *) rs->buf.data () + i,
  9459. &used_pattern_len, max_size);
  9460. /* Bail if the pattern is too large. */
  9461. if (used_pattern_len != pattern_len)
  9462. error (_("Pattern is too large to transmit to remote target."));
  9463. if (putpkt_binary (rs->buf.data (), i + escaped_pattern_len) < 0
  9464. || getpkt_sane (&rs->buf, 0) < 0
  9465. || packet_ok (rs->buf, packet) != PACKET_OK)
  9466. {
  9467. /* The request may not have worked because the command is not
  9468. supported. If so, fall back to the simple way. */
  9469. if (packet_config_support (packet) == PACKET_DISABLE)
  9470. {
  9471. return simple_search_memory (read_memory, start_addr, search_space_len,
  9472. pattern, pattern_len, found_addrp);
  9473. }
  9474. return -1;
  9475. }
  9476. if (rs->buf[0] == '0')
  9477. found = 0;
  9478. else if (rs->buf[0] == '1')
  9479. {
  9480. found = 1;
  9481. if (rs->buf[1] != ',')
  9482. error (_("Unknown qSearch:memory reply: %s"), rs->buf.data ());
  9483. unpack_varlen_hex (&rs->buf[2], &found_addr);
  9484. *found_addrp = found_addr;
  9485. }
  9486. else
  9487. error (_("Unknown qSearch:memory reply: %s"), rs->buf.data ());
  9488. return found;
  9489. }
  9490. void
  9491. remote_target::rcmd (const char *command, struct ui_file *outbuf)
  9492. {
  9493. struct remote_state *rs = get_remote_state ();
  9494. char *p = rs->buf.data ();
  9495. if (!rs->remote_desc)
  9496. error (_("remote rcmd is only available after target open"));
  9497. /* Send a NULL command across as an empty command. */
  9498. if (command == NULL)
  9499. command = "";
  9500. /* The query prefix. */
  9501. strcpy (rs->buf.data (), "qRcmd,");
  9502. p = strchr (rs->buf.data (), '\0');
  9503. if ((strlen (rs->buf.data ()) + strlen (command) * 2 + 8/*misc*/)
  9504. > get_remote_packet_size ())
  9505. error (_("\"monitor\" command ``%s'' is too long."), command);
  9506. /* Encode the actual command. */
  9507. bin2hex ((const gdb_byte *) command, p, strlen (command));
  9508. if (putpkt (rs->buf) < 0)
  9509. error (_("Communication problem with target."));
  9510. /* get/display the response */
  9511. while (1)
  9512. {
  9513. char *buf;
  9514. /* XXX - see also remote_get_noisy_reply(). */
  9515. QUIT; /* Allow user to bail out with ^C. */
  9516. rs->buf[0] = '\0';
  9517. if (getpkt_sane (&rs->buf, 0) == -1)
  9518. {
  9519. /* Timeout. Continue to (try to) read responses.
  9520. This is better than stopping with an error, assuming the stub
  9521. is still executing the (long) monitor command.
  9522. If needed, the user can interrupt gdb using C-c, obtaining
  9523. an effect similar to stop on timeout. */
  9524. continue;
  9525. }
  9526. buf = rs->buf.data ();
  9527. if (buf[0] == '\0')
  9528. error (_("Target does not support this command."));
  9529. if (buf[0] == 'O' && buf[1] != 'K')
  9530. {
  9531. remote_console_output (buf + 1); /* 'O' message from stub. */
  9532. continue;
  9533. }
  9534. if (strcmp (buf, "OK") == 0)
  9535. break;
  9536. if (strlen (buf) == 3 && buf[0] == 'E'
  9537. && isxdigit (buf[1]) && isxdigit (buf[2]))
  9538. {
  9539. error (_("Protocol error with Rcmd"));
  9540. }
  9541. for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
  9542. {
  9543. char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
  9544. gdb_putc (c, outbuf);
  9545. }
  9546. break;
  9547. }
  9548. }
  9549. std::vector<mem_region>
  9550. remote_target::memory_map ()
  9551. {
  9552. std::vector<mem_region> result;
  9553. gdb::optional<gdb::char_vector> text
  9554. = target_read_stralloc (current_inferior ()->top_target (),
  9555. TARGET_OBJECT_MEMORY_MAP, NULL);
  9556. if (text)
  9557. result = parse_memory_map (text->data ());
  9558. return result;
  9559. }
  9560. /* Set of callbacks used to implement the 'maint packet' command. */
  9561. struct cli_packet_command_callbacks : public send_remote_packet_callbacks
  9562. {
  9563. /* Called before the packet is sent. BUF is the packet content before
  9564. the protocol specific prefix, suffix, and escaping is added. */
  9565. void sending (gdb::array_view<const char> &buf) override
  9566. {
  9567. gdb_puts ("sending: ");
  9568. print_packet (buf);
  9569. gdb_puts ("\n");
  9570. }
  9571. /* Called with BUF, the reply from the remote target. */
  9572. void received (gdb::array_view<const char> &buf) override
  9573. {
  9574. gdb_puts ("received: \"");
  9575. print_packet (buf);
  9576. gdb_puts ("\"\n");
  9577. }
  9578. private:
  9579. /* Print BUF o gdb_stdout. Any non-printable bytes in BUF are printed as
  9580. '\x??' with '??' replaced by the hexadecimal value of the byte. */
  9581. static void
  9582. print_packet (gdb::array_view<const char> &buf)
  9583. {
  9584. string_file stb;
  9585. for (int i = 0; i < buf.size (); ++i)
  9586. {
  9587. gdb_byte c = buf[i];
  9588. if (isprint (c))
  9589. gdb_putc (c, &stb);
  9590. else
  9591. gdb_printf (&stb, "\\x%02x", (unsigned char) c);
  9592. }
  9593. gdb_puts (stb.string ().c_str ());
  9594. }
  9595. };
  9596. /* See remote.h. */
  9597. void
  9598. send_remote_packet (gdb::array_view<const char> &buf,
  9599. send_remote_packet_callbacks *callbacks)
  9600. {
  9601. if (buf.size () == 0 || buf.data ()[0] == '\0')
  9602. error (_("a remote packet must not be empty"));
  9603. remote_target *remote = get_current_remote_target ();
  9604. if (remote == nullptr)
  9605. error (_("packets can only be sent to a remote target"));
  9606. callbacks->sending (buf);
  9607. remote->putpkt_binary (buf.data (), buf.size ());
  9608. remote_state *rs = remote->get_remote_state ();
  9609. int bytes = remote->getpkt_sane (&rs->buf, 0);
  9610. if (bytes < 0)
  9611. error (_("error while fetching packet from remote target"));
  9612. gdb::array_view<const char> view (&rs->buf[0], bytes);
  9613. callbacks->received (view);
  9614. }
  9615. /* Entry point for the 'maint packet' command. */
  9616. static void
  9617. cli_packet_command (const char *args, int from_tty)
  9618. {
  9619. cli_packet_command_callbacks cb;
  9620. gdb::array_view<const char> view
  9621. = gdb::make_array_view (args, args == nullptr ? 0 : strlen (args));
  9622. send_remote_packet (view, &cb);
  9623. }
  9624. #if 0
  9625. /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------- */
  9626. static void display_thread_info (struct gdb_ext_thread_info *info);
  9627. static void threadset_test_cmd (char *cmd, int tty);
  9628. static void threadalive_test (char *cmd, int tty);
  9629. static void threadlist_test_cmd (char *cmd, int tty);
  9630. int get_and_display_threadinfo (threadref *ref);
  9631. static void threadinfo_test_cmd (char *cmd, int tty);
  9632. static int thread_display_step (threadref *ref, void *context);
  9633. static void threadlist_update_test_cmd (char *cmd, int tty);
  9634. static void init_remote_threadtests (void);
  9635. #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid. */
  9636. static void
  9637. threadset_test_cmd (const char *cmd, int tty)
  9638. {
  9639. int sample_thread = SAMPLE_THREAD;
  9640. gdb_printf (_("Remote threadset test\n"));
  9641. set_general_thread (sample_thread);
  9642. }
  9643. static void
  9644. threadalive_test (const char *cmd, int tty)
  9645. {
  9646. int sample_thread = SAMPLE_THREAD;
  9647. int pid = inferior_ptid.pid ();
  9648. ptid_t ptid = ptid_t (pid, sample_thread, 0);
  9649. if (remote_thread_alive (ptid))
  9650. gdb_printf ("PASS: Thread alive test\n");
  9651. else
  9652. gdb_printf ("FAIL: Thread alive test\n");
  9653. }
  9654. void output_threadid (char *title, threadref *ref);
  9655. void
  9656. output_threadid (char *title, threadref *ref)
  9657. {
  9658. char hexid[20];
  9659. pack_threadid (&hexid[0], ref); /* Convert thread id into hex. */
  9660. hexid[16] = 0;
  9661. gdb_printf ("%s %s\n", title, (&hexid[0]));
  9662. }
  9663. static void
  9664. threadlist_test_cmd (const char *cmd, int tty)
  9665. {
  9666. int startflag = 1;
  9667. threadref nextthread;
  9668. int done, result_count;
  9669. threadref threadlist[3];
  9670. gdb_printf ("Remote Threadlist test\n");
  9671. if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
  9672. &result_count, &threadlist[0]))
  9673. gdb_printf ("FAIL: threadlist test\n");
  9674. else
  9675. {
  9676. threadref *scan = threadlist;
  9677. threadref *limit = scan + result_count;
  9678. while (scan < limit)
  9679. output_threadid (" thread ", scan++);
  9680. }
  9681. }
  9682. void
  9683. display_thread_info (struct gdb_ext_thread_info *info)
  9684. {
  9685. output_threadid ("Threadid: ", &info->threadid);
  9686. gdb_printf ("Name: %s\n ", info->shortname);
  9687. gdb_printf ("State: %s\n", info->display);
  9688. gdb_printf ("other: %s\n\n", info->more_display);
  9689. }
  9690. int
  9691. get_and_display_threadinfo (threadref *ref)
  9692. {
  9693. int result;
  9694. int set;
  9695. struct gdb_ext_thread_info threadinfo;
  9696. set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
  9697. | TAG_MOREDISPLAY | TAG_DISPLAY;
  9698. if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
  9699. display_thread_info (&threadinfo);
  9700. return result;
  9701. }
  9702. static void
  9703. threadinfo_test_cmd (const char *cmd, int tty)
  9704. {
  9705. int athread = SAMPLE_THREAD;
  9706. threadref thread;
  9707. int set;
  9708. int_to_threadref (&thread, athread);
  9709. gdb_printf ("Remote Threadinfo test\n");
  9710. if (!get_and_display_threadinfo (&thread))
  9711. gdb_printf ("FAIL cannot get thread info\n");
  9712. }
  9713. static int
  9714. thread_display_step (threadref *ref, void *context)
  9715. {
  9716. /* output_threadid(" threadstep ",ref); *//* simple test */
  9717. return get_and_display_threadinfo (ref);
  9718. }
  9719. static void
  9720. threadlist_update_test_cmd (const char *cmd, int tty)
  9721. {
  9722. gdb_printf ("Remote Threadlist update test\n");
  9723. remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
  9724. }
  9725. static void
  9726. init_remote_threadtests (void)
  9727. {
  9728. add_com ("tlist", class_obscure, threadlist_test_cmd,
  9729. _("Fetch and print the remote list of "
  9730. "thread identifiers, one pkt only."));
  9731. add_com ("tinfo", class_obscure, threadinfo_test_cmd,
  9732. _("Fetch and display info about one thread."));
  9733. add_com ("tset", class_obscure, threadset_test_cmd,
  9734. _("Test setting to a different thread."));
  9735. add_com ("tupd", class_obscure, threadlist_update_test_cmd,
  9736. _("Iterate through updating all remote thread info."));
  9737. add_com ("talive", class_obscure, threadalive_test,
  9738. _("Remote thread alive test."));
  9739. }
  9740. #endif /* 0 */
  9741. /* Convert a thread ID to a string. */
  9742. std::string
  9743. remote_target::pid_to_str (ptid_t ptid)
  9744. {
  9745. struct remote_state *rs = get_remote_state ();
  9746. if (ptid == null_ptid)
  9747. return normal_pid_to_str (ptid);
  9748. else if (ptid.is_pid ())
  9749. {
  9750. /* Printing an inferior target id. */
  9751. /* When multi-process extensions are off, there's no way in the
  9752. remote protocol to know the remote process id, if there's any
  9753. at all. There's one exception --- when we're connected with
  9754. target extended-remote, and we manually attached to a process
  9755. with "attach PID". We don't record anywhere a flag that
  9756. allows us to distinguish that case from the case of
  9757. connecting with extended-remote and the stub already being
  9758. attached to a process, and reporting yes to qAttached, hence
  9759. no smart special casing here. */
  9760. if (!remote_multi_process_p (rs))
  9761. return "Remote target";
  9762. return normal_pid_to_str (ptid);
  9763. }
  9764. else
  9765. {
  9766. if (magic_null_ptid == ptid)
  9767. return "Thread <main>";
  9768. else if (remote_multi_process_p (rs))
  9769. if (ptid.lwp () == 0)
  9770. return normal_pid_to_str (ptid);
  9771. else
  9772. return string_printf ("Thread %d.%ld",
  9773. ptid.pid (), ptid.lwp ());
  9774. else
  9775. return string_printf ("Thread %ld", ptid.lwp ());
  9776. }
  9777. }
  9778. /* Get the address of the thread local variable in OBJFILE which is
  9779. stored at OFFSET within the thread local storage for thread PTID. */
  9780. CORE_ADDR
  9781. remote_target::get_thread_local_address (ptid_t ptid, CORE_ADDR lm,
  9782. CORE_ADDR offset)
  9783. {
  9784. if (packet_support (PACKET_qGetTLSAddr) != PACKET_DISABLE)
  9785. {
  9786. struct remote_state *rs = get_remote_state ();
  9787. char *p = rs->buf.data ();
  9788. char *endp = p + get_remote_packet_size ();
  9789. enum packet_result result;
  9790. strcpy (p, "qGetTLSAddr:");
  9791. p += strlen (p);
  9792. p = write_ptid (p, endp, ptid);
  9793. *p++ = ',';
  9794. p += hexnumstr (p, offset);
  9795. *p++ = ',';
  9796. p += hexnumstr (p, lm);
  9797. *p++ = '\0';
  9798. putpkt (rs->buf);
  9799. getpkt (&rs->buf, 0);
  9800. result = packet_ok (rs->buf,
  9801. &remote_protocol_packets[PACKET_qGetTLSAddr]);
  9802. if (result == PACKET_OK)
  9803. {
  9804. ULONGEST addr;
  9805. unpack_varlen_hex (rs->buf.data (), &addr);
  9806. return addr;
  9807. }
  9808. else if (result == PACKET_UNKNOWN)
  9809. throw_error (TLS_GENERIC_ERROR,
  9810. _("Remote target doesn't support qGetTLSAddr packet"));
  9811. else
  9812. throw_error (TLS_GENERIC_ERROR,
  9813. _("Remote target failed to process qGetTLSAddr request"));
  9814. }
  9815. else
  9816. throw_error (TLS_GENERIC_ERROR,
  9817. _("TLS not supported or disabled on this target"));
  9818. /* Not reached. */
  9819. return 0;
  9820. }
  9821. /* Provide thread local base, i.e. Thread Information Block address.
  9822. Returns 1 if ptid is found and thread_local_base is non zero. */
  9823. bool
  9824. remote_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr)
  9825. {
  9826. if (packet_support (PACKET_qGetTIBAddr) != PACKET_DISABLE)
  9827. {
  9828. struct remote_state *rs = get_remote_state ();
  9829. char *p = rs->buf.data ();
  9830. char *endp = p + get_remote_packet_size ();
  9831. enum packet_result result;
  9832. strcpy (p, "qGetTIBAddr:");
  9833. p += strlen (p);
  9834. p = write_ptid (p, endp, ptid);
  9835. *p++ = '\0';
  9836. putpkt (rs->buf);
  9837. getpkt (&rs->buf, 0);
  9838. result = packet_ok (rs->buf,
  9839. &remote_protocol_packets[PACKET_qGetTIBAddr]);
  9840. if (result == PACKET_OK)
  9841. {
  9842. ULONGEST val;
  9843. unpack_varlen_hex (rs->buf.data (), &val);
  9844. if (addr)
  9845. *addr = (CORE_ADDR) val;
  9846. return true;
  9847. }
  9848. else if (result == PACKET_UNKNOWN)
  9849. error (_("Remote target doesn't support qGetTIBAddr packet"));
  9850. else
  9851. error (_("Remote target failed to process qGetTIBAddr request"));
  9852. }
  9853. else
  9854. error (_("qGetTIBAddr not supported or disabled on this target"));
  9855. /* Not reached. */
  9856. return false;
  9857. }
  9858. /* Support for inferring a target description based on the current
  9859. architecture and the size of a 'g' packet. While the 'g' packet
  9860. can have any size (since optional registers can be left off the
  9861. end), some sizes are easily recognizable given knowledge of the
  9862. approximate architecture. */
  9863. struct remote_g_packet_guess
  9864. {
  9865. remote_g_packet_guess (int bytes_, const struct target_desc *tdesc_)
  9866. : bytes (bytes_),
  9867. tdesc (tdesc_)
  9868. {
  9869. }
  9870. int bytes;
  9871. const struct target_desc *tdesc;
  9872. };
  9873. struct remote_g_packet_data : public allocate_on_obstack
  9874. {
  9875. std::vector<remote_g_packet_guess> guesses;
  9876. };
  9877. static struct gdbarch_data *remote_g_packet_data_handle;
  9878. static void *
  9879. remote_g_packet_data_init (struct obstack *obstack)
  9880. {
  9881. return new (obstack) remote_g_packet_data;
  9882. }
  9883. void
  9884. register_remote_g_packet_guess (struct gdbarch *gdbarch, int bytes,
  9885. const struct target_desc *tdesc)
  9886. {
  9887. struct remote_g_packet_data *data
  9888. = ((struct remote_g_packet_data *)
  9889. gdbarch_data (gdbarch, remote_g_packet_data_handle));
  9890. gdb_assert (tdesc != NULL);
  9891. for (const remote_g_packet_guess &guess : data->guesses)
  9892. if (guess.bytes == bytes)
  9893. internal_error (__FILE__, __LINE__,
  9894. _("Duplicate g packet description added for size %d"),
  9895. bytes);
  9896. data->guesses.emplace_back (bytes, tdesc);
  9897. }
  9898. /* Return true if remote_read_description would do anything on this target
  9899. and architecture, false otherwise. */
  9900. static bool
  9901. remote_read_description_p (struct target_ops *target)
  9902. {
  9903. struct remote_g_packet_data *data
  9904. = ((struct remote_g_packet_data *)
  9905. gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
  9906. return !data->guesses.empty ();
  9907. }
  9908. const struct target_desc *
  9909. remote_target::read_description ()
  9910. {
  9911. struct remote_g_packet_data *data
  9912. = ((struct remote_g_packet_data *)
  9913. gdbarch_data (target_gdbarch (), remote_g_packet_data_handle));
  9914. /* Do not try this during initial connection, when we do not know
  9915. whether there is a running but stopped thread. */
  9916. if (!target_has_execution () || inferior_ptid == null_ptid)
  9917. return beneath ()->read_description ();
  9918. if (!data->guesses.empty ())
  9919. {
  9920. int bytes = send_g_packet ();
  9921. for (const remote_g_packet_guess &guess : data->guesses)
  9922. if (guess.bytes == bytes)
  9923. return guess.tdesc;
  9924. /* We discard the g packet. A minor optimization would be to
  9925. hold on to it, and fill the register cache once we have selected
  9926. an architecture, but it's too tricky to do safely. */
  9927. }
  9928. return beneath ()->read_description ();
  9929. }
  9930. /* Remote file transfer support. This is host-initiated I/O, not
  9931. target-initiated; for target-initiated, see remote-fileio.c. */
  9932. /* If *LEFT is at least the length of STRING, copy STRING to
  9933. *BUFFER, update *BUFFER to point to the new end of the buffer, and
  9934. decrease *LEFT. Otherwise raise an error. */
  9935. static void
  9936. remote_buffer_add_string (char **buffer, int *left, const char *string)
  9937. {
  9938. int len = strlen (string);
  9939. if (len > *left)
  9940. error (_("Packet too long for target."));
  9941. memcpy (*buffer, string, len);
  9942. *buffer += len;
  9943. *left -= len;
  9944. /* NUL-terminate the buffer as a convenience, if there is
  9945. room. */
  9946. if (*left)
  9947. **buffer = '\0';
  9948. }
  9949. /* If *LEFT is large enough, hex encode LEN bytes from BYTES into
  9950. *BUFFER, update *BUFFER to point to the new end of the buffer, and
  9951. decrease *LEFT. Otherwise raise an error. */
  9952. static void
  9953. remote_buffer_add_bytes (char **buffer, int *left, const gdb_byte *bytes,
  9954. int len)
  9955. {
  9956. if (2 * len > *left)
  9957. error (_("Packet too long for target."));
  9958. bin2hex (bytes, *buffer, len);
  9959. *buffer += 2 * len;
  9960. *left -= 2 * len;
  9961. /* NUL-terminate the buffer as a convenience, if there is
  9962. room. */
  9963. if (*left)
  9964. **buffer = '\0';
  9965. }
  9966. /* If *LEFT is large enough, convert VALUE to hex and add it to
  9967. *BUFFER, update *BUFFER to point to the new end of the buffer, and
  9968. decrease *LEFT. Otherwise raise an error. */
  9969. static void
  9970. remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
  9971. {
  9972. int len = hexnumlen (value);
  9973. if (len > *left)
  9974. error (_("Packet too long for target."));
  9975. hexnumstr (*buffer, value);
  9976. *buffer += len;
  9977. *left -= len;
  9978. /* NUL-terminate the buffer as a convenience, if there is
  9979. room. */
  9980. if (*left)
  9981. **buffer = '\0';
  9982. }
  9983. /* Parse an I/O result packet from BUFFER. Set RETCODE to the return
  9984. value, *REMOTE_ERRNO to the remote error number or zero if none
  9985. was included, and *ATTACHMENT to point to the start of the annex
  9986. if any. The length of the packet isn't needed here; there may
  9987. be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
  9988. Return 0 if the packet could be parsed, -1 if it could not. If
  9989. -1 is returned, the other variables may not be initialized. */
  9990. static int
  9991. remote_hostio_parse_result (const char *buffer, int *retcode,
  9992. int *remote_errno, const char **attachment)
  9993. {
  9994. char *p, *p2;
  9995. *remote_errno = 0;
  9996. *attachment = NULL;
  9997. if (buffer[0] != 'F')
  9998. return -1;
  9999. errno = 0;
  10000. *retcode = strtol (&buffer[1], &p, 16);
  10001. if (errno != 0 || p == &buffer[1])
  10002. return -1;
  10003. /* Check for ",errno". */
  10004. if (*p == ',')
  10005. {
  10006. errno = 0;
  10007. *remote_errno = strtol (p + 1, &p2, 16);
  10008. if (errno != 0 || p + 1 == p2)
  10009. return -1;
  10010. p = p2;
  10011. }
  10012. /* Check for ";attachment". If there is no attachment, the
  10013. packet should end here. */
  10014. if (*p == ';')
  10015. {
  10016. *attachment = p + 1;
  10017. return 0;
  10018. }
  10019. else if (*p == '\0')
  10020. return 0;
  10021. else
  10022. return -1;
  10023. }
  10024. /* Send a prepared I/O packet to the target and read its response.
  10025. The prepared packet is in the global RS->BUF before this function
  10026. is called, and the answer is there when we return.
  10027. COMMAND_BYTES is the length of the request to send, which may include
  10028. binary data. WHICH_PACKET is the packet configuration to check
  10029. before attempting a packet. If an error occurs, *REMOTE_ERRNO
  10030. is set to the error number and -1 is returned. Otherwise the value
  10031. returned by the function is returned.
  10032. ATTACHMENT and ATTACHMENT_LEN should be non-NULL if and only if an
  10033. attachment is expected; an error will be reported if there's a
  10034. mismatch. If one is found, *ATTACHMENT will be set to point into
  10035. the packet buffer and *ATTACHMENT_LEN will be set to the
  10036. attachment's length. */
  10037. int
  10038. remote_target::remote_hostio_send_command (int command_bytes, int which_packet,
  10039. int *remote_errno, const char **attachment,
  10040. int *attachment_len)
  10041. {
  10042. struct remote_state *rs = get_remote_state ();
  10043. int ret, bytes_read;
  10044. const char *attachment_tmp;
  10045. if (packet_support (which_packet) == PACKET_DISABLE)
  10046. {
  10047. *remote_errno = FILEIO_ENOSYS;
  10048. return -1;
  10049. }
  10050. putpkt_binary (rs->buf.data (), command_bytes);
  10051. bytes_read = getpkt_sane (&rs->buf, 0);
  10052. /* If it timed out, something is wrong. Don't try to parse the
  10053. buffer. */
  10054. if (bytes_read < 0)
  10055. {
  10056. *remote_errno = FILEIO_EINVAL;
  10057. return -1;
  10058. }
  10059. switch (packet_ok (rs->buf, &remote_protocol_packets[which_packet]))
  10060. {
  10061. case PACKET_ERROR:
  10062. *remote_errno = FILEIO_EINVAL;
  10063. return -1;
  10064. case PACKET_UNKNOWN:
  10065. *remote_errno = FILEIO_ENOSYS;
  10066. return -1;
  10067. case PACKET_OK:
  10068. break;
  10069. }
  10070. if (remote_hostio_parse_result (rs->buf.data (), &ret, remote_errno,
  10071. &attachment_tmp))
  10072. {
  10073. *remote_errno = FILEIO_EINVAL;
  10074. return -1;
  10075. }
  10076. /* Make sure we saw an attachment if and only if we expected one. */
  10077. if ((attachment_tmp == NULL && attachment != NULL)
  10078. || (attachment_tmp != NULL && attachment == NULL))
  10079. {
  10080. *remote_errno = FILEIO_EINVAL;
  10081. return -1;
  10082. }
  10083. /* If an attachment was found, it must point into the packet buffer;
  10084. work out how many bytes there were. */
  10085. if (attachment_tmp != NULL)
  10086. {
  10087. *attachment = attachment_tmp;
  10088. *attachment_len = bytes_read - (*attachment - rs->buf.data ());
  10089. }
  10090. return ret;
  10091. }
  10092. /* See declaration.h. */
  10093. void
  10094. readahead_cache::invalidate ()
  10095. {
  10096. this->fd = -1;
  10097. }
  10098. /* See declaration.h. */
  10099. void
  10100. readahead_cache::invalidate_fd (int fd)
  10101. {
  10102. if (this->fd == fd)
  10103. this->fd = -1;
  10104. }
  10105. /* Set the filesystem remote_hostio functions that take FILENAME
  10106. arguments will use. Return 0 on success, or -1 if an error
  10107. occurs (and set *REMOTE_ERRNO). */
  10108. int
  10109. remote_target::remote_hostio_set_filesystem (struct inferior *inf,
  10110. int *remote_errno)
  10111. {
  10112. struct remote_state *rs = get_remote_state ();
  10113. int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
  10114. char *p = rs->buf.data ();
  10115. int left = get_remote_packet_size () - 1;
  10116. char arg[9];
  10117. int ret;
  10118. if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
  10119. return 0;
  10120. if (rs->fs_pid != -1 && required_pid == rs->fs_pid)
  10121. return 0;
  10122. remote_buffer_add_string (&p, &left, "vFile:setfs:");
  10123. xsnprintf (arg, sizeof (arg), "%x", required_pid);
  10124. remote_buffer_add_string (&p, &left, arg);
  10125. ret = remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_setfs,
  10126. remote_errno, NULL, NULL);
  10127. if (packet_support (PACKET_vFile_setfs) == PACKET_DISABLE)
  10128. return 0;
  10129. if (ret == 0)
  10130. rs->fs_pid = required_pid;
  10131. return ret;
  10132. }
  10133. /* Implementation of to_fileio_open. */
  10134. int
  10135. remote_target::remote_hostio_open (inferior *inf, const char *filename,
  10136. int flags, int mode, int warn_if_slow,
  10137. int *remote_errno)
  10138. {
  10139. struct remote_state *rs = get_remote_state ();
  10140. char *p = rs->buf.data ();
  10141. int left = get_remote_packet_size () - 1;
  10142. if (warn_if_slow)
  10143. {
  10144. static int warning_issued = 0;
  10145. gdb_printf (_("Reading %s from remote target...\n"),
  10146. filename);
  10147. if (!warning_issued)
  10148. {
  10149. warning (_("File transfers from remote targets can be slow."
  10150. " Use \"set sysroot\" to access files locally"
  10151. " instead."));
  10152. warning_issued = 1;
  10153. }
  10154. }
  10155. if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
  10156. return -1;
  10157. remote_buffer_add_string (&p, &left, "vFile:open:");
  10158. remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
  10159. strlen (filename));
  10160. remote_buffer_add_string (&p, &left, ",");
  10161. remote_buffer_add_int (&p, &left, flags);
  10162. remote_buffer_add_string (&p, &left, ",");
  10163. remote_buffer_add_int (&p, &left, mode);
  10164. return remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_open,
  10165. remote_errno, NULL, NULL);
  10166. }
  10167. int
  10168. remote_target::fileio_open (struct inferior *inf, const char *filename,
  10169. int flags, int mode, int warn_if_slow,
  10170. int *remote_errno)
  10171. {
  10172. return remote_hostio_open (inf, filename, flags, mode, warn_if_slow,
  10173. remote_errno);
  10174. }
  10175. /* Implementation of to_fileio_pwrite. */
  10176. int
  10177. remote_target::remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
  10178. ULONGEST offset, int *remote_errno)
  10179. {
  10180. struct remote_state *rs = get_remote_state ();
  10181. char *p = rs->buf.data ();
  10182. int left = get_remote_packet_size ();
  10183. int out_len;
  10184. rs->readahead_cache.invalidate_fd (fd);
  10185. remote_buffer_add_string (&p, &left, "vFile:pwrite:");
  10186. remote_buffer_add_int (&p, &left, fd);
  10187. remote_buffer_add_string (&p, &left, ",");
  10188. remote_buffer_add_int (&p, &left, offset);
  10189. remote_buffer_add_string (&p, &left, ",");
  10190. p += remote_escape_output (write_buf, len, 1, (gdb_byte *) p, &out_len,
  10191. (get_remote_packet_size ()
  10192. - (p - rs->buf.data ())));
  10193. return remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_pwrite,
  10194. remote_errno, NULL, NULL);
  10195. }
  10196. int
  10197. remote_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
  10198. ULONGEST offset, int *remote_errno)
  10199. {
  10200. return remote_hostio_pwrite (fd, write_buf, len, offset, remote_errno);
  10201. }
  10202. /* Helper for the implementation of to_fileio_pread. Read the file
  10203. from the remote side with vFile:pread. */
  10204. int
  10205. remote_target::remote_hostio_pread_vFile (int fd, gdb_byte *read_buf, int len,
  10206. ULONGEST offset, int *remote_errno)
  10207. {
  10208. struct remote_state *rs = get_remote_state ();
  10209. char *p = rs->buf.data ();
  10210. const char *attachment;
  10211. int left = get_remote_packet_size ();
  10212. int ret, attachment_len;
  10213. int read_len;
  10214. remote_buffer_add_string (&p, &left, "vFile:pread:");
  10215. remote_buffer_add_int (&p, &left, fd);
  10216. remote_buffer_add_string (&p, &left, ",");
  10217. remote_buffer_add_int (&p, &left, len);
  10218. remote_buffer_add_string (&p, &left, ",");
  10219. remote_buffer_add_int (&p, &left, offset);
  10220. ret = remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_pread,
  10221. remote_errno, &attachment,
  10222. &attachment_len);
  10223. if (ret < 0)
  10224. return ret;
  10225. read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
  10226. read_buf, len);
  10227. if (read_len != ret)
  10228. error (_("Read returned %d, but %d bytes."), ret, (int) read_len);
  10229. return ret;
  10230. }
  10231. /* See declaration.h. */
  10232. int
  10233. readahead_cache::pread (int fd, gdb_byte *read_buf, size_t len,
  10234. ULONGEST offset)
  10235. {
  10236. if (this->fd == fd
  10237. && this->offset <= offset
  10238. && offset < this->offset + this->bufsize)
  10239. {
  10240. ULONGEST max = this->offset + this->bufsize;
  10241. if (offset + len > max)
  10242. len = max - offset;
  10243. memcpy (read_buf, this->buf + offset - this->offset, len);
  10244. return len;
  10245. }
  10246. return 0;
  10247. }
  10248. /* Implementation of to_fileio_pread. */
  10249. int
  10250. remote_target::remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
  10251. ULONGEST offset, int *remote_errno)
  10252. {
  10253. int ret;
  10254. struct remote_state *rs = get_remote_state ();
  10255. readahead_cache *cache = &rs->readahead_cache;
  10256. ret = cache->pread (fd, read_buf, len, offset);
  10257. if (ret > 0)
  10258. {
  10259. cache->hit_count++;
  10260. remote_debug_printf ("readahead cache hit %s",
  10261. pulongest (cache->hit_count));
  10262. return ret;
  10263. }
  10264. cache->miss_count++;
  10265. remote_debug_printf ("readahead cache miss %s",
  10266. pulongest (cache->miss_count));
  10267. cache->fd = fd;
  10268. cache->offset = offset;
  10269. cache->bufsize = get_remote_packet_size ();
  10270. cache->buf = (gdb_byte *) xrealloc (cache->buf, cache->bufsize);
  10271. ret = remote_hostio_pread_vFile (cache->fd, cache->buf, cache->bufsize,
  10272. cache->offset, remote_errno);
  10273. if (ret <= 0)
  10274. {
  10275. cache->invalidate_fd (fd);
  10276. return ret;
  10277. }
  10278. cache->bufsize = ret;
  10279. return cache->pread (fd, read_buf, len, offset);
  10280. }
  10281. int
  10282. remote_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
  10283. ULONGEST offset, int *remote_errno)
  10284. {
  10285. return remote_hostio_pread (fd, read_buf, len, offset, remote_errno);
  10286. }
  10287. /* Implementation of to_fileio_close. */
  10288. int
  10289. remote_target::remote_hostio_close (int fd, int *remote_errno)
  10290. {
  10291. struct remote_state *rs = get_remote_state ();
  10292. char *p = rs->buf.data ();
  10293. int left = get_remote_packet_size () - 1;
  10294. rs->readahead_cache.invalidate_fd (fd);
  10295. remote_buffer_add_string (&p, &left, "vFile:close:");
  10296. remote_buffer_add_int (&p, &left, fd);
  10297. return remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_close,
  10298. remote_errno, NULL, NULL);
  10299. }
  10300. int
  10301. remote_target::fileio_close (int fd, int *remote_errno)
  10302. {
  10303. return remote_hostio_close (fd, remote_errno);
  10304. }
  10305. /* Implementation of to_fileio_unlink. */
  10306. int
  10307. remote_target::remote_hostio_unlink (inferior *inf, const char *filename,
  10308. int *remote_errno)
  10309. {
  10310. struct remote_state *rs = get_remote_state ();
  10311. char *p = rs->buf.data ();
  10312. int left = get_remote_packet_size () - 1;
  10313. if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
  10314. return -1;
  10315. remote_buffer_add_string (&p, &left, "vFile:unlink:");
  10316. remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
  10317. strlen (filename));
  10318. return remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_unlink,
  10319. remote_errno, NULL, NULL);
  10320. }
  10321. int
  10322. remote_target::fileio_unlink (struct inferior *inf, const char *filename,
  10323. int *remote_errno)
  10324. {
  10325. return remote_hostio_unlink (inf, filename, remote_errno);
  10326. }
  10327. /* Implementation of to_fileio_readlink. */
  10328. gdb::optional<std::string>
  10329. remote_target::fileio_readlink (struct inferior *inf, const char *filename,
  10330. int *remote_errno)
  10331. {
  10332. struct remote_state *rs = get_remote_state ();
  10333. char *p = rs->buf.data ();
  10334. const char *attachment;
  10335. int left = get_remote_packet_size ();
  10336. int len, attachment_len;
  10337. int read_len;
  10338. if (remote_hostio_set_filesystem (inf, remote_errno) != 0)
  10339. return {};
  10340. remote_buffer_add_string (&p, &left, "vFile:readlink:");
  10341. remote_buffer_add_bytes (&p, &left, (const gdb_byte *) filename,
  10342. strlen (filename));
  10343. len = remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_readlink,
  10344. remote_errno, &attachment,
  10345. &attachment_len);
  10346. if (len < 0)
  10347. return {};
  10348. std::string ret (len, '\0');
  10349. read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
  10350. (gdb_byte *) &ret[0], len);
  10351. if (read_len != len)
  10352. error (_("Readlink returned %d, but %d bytes."), len, read_len);
  10353. return ret;
  10354. }
  10355. /* Implementation of to_fileio_fstat. */
  10356. int
  10357. remote_target::fileio_fstat (int fd, struct stat *st, int *remote_errno)
  10358. {
  10359. struct remote_state *rs = get_remote_state ();
  10360. char *p = rs->buf.data ();
  10361. int left = get_remote_packet_size ();
  10362. int attachment_len, ret;
  10363. const char *attachment;
  10364. struct fio_stat fst;
  10365. int read_len;
  10366. remote_buffer_add_string (&p, &left, "vFile:fstat:");
  10367. remote_buffer_add_int (&p, &left, fd);
  10368. ret = remote_hostio_send_command (p - rs->buf.data (), PACKET_vFile_fstat,
  10369. remote_errno, &attachment,
  10370. &attachment_len);
  10371. if (ret < 0)
  10372. {
  10373. if (*remote_errno != FILEIO_ENOSYS)
  10374. return ret;
  10375. /* Strictly we should return -1, ENOSYS here, but when
  10376. "set sysroot remote:" was implemented in August 2008
  10377. BFD's need for a stat function was sidestepped with
  10378. this hack. This was not remedied until March 2015
  10379. so we retain the previous behavior to avoid breaking
  10380. compatibility.
  10381. Note that the memset is a March 2015 addition; older
  10382. GDBs set st_size *and nothing else* so the structure
  10383. would have garbage in all other fields. This might
  10384. break something but retaining the previous behavior
  10385. here would be just too wrong. */
  10386. memset (st, 0, sizeof (struct stat));
  10387. st->st_size = INT_MAX;
  10388. return 0;
  10389. }
  10390. read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
  10391. (gdb_byte *) &fst, sizeof (fst));
  10392. if (read_len != ret)
  10393. error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
  10394. if (read_len != sizeof (fst))
  10395. error (_("vFile:fstat returned %d bytes, but expecting %d."),
  10396. read_len, (int) sizeof (fst));
  10397. remote_fileio_to_host_stat (&fst, st);
  10398. return 0;
  10399. }
  10400. /* Implementation of to_filesystem_is_local. */
  10401. bool
  10402. remote_target::filesystem_is_local ()
  10403. {
  10404. /* Valgrind GDB presents itself as a remote target but works
  10405. on the local filesystem: it does not implement remote get
  10406. and users are not expected to set a sysroot. To handle
  10407. this case we treat the remote filesystem as local if the
  10408. sysroot is exactly TARGET_SYSROOT_PREFIX and if the stub
  10409. does not support vFile:open. */
  10410. if (gdb_sysroot == TARGET_SYSROOT_PREFIX)
  10411. {
  10412. enum packet_support ps = packet_support (PACKET_vFile_open);
  10413. if (ps == PACKET_SUPPORT_UNKNOWN)
  10414. {
  10415. int fd, remote_errno;
  10416. /* Try opening a file to probe support. The supplied
  10417. filename is irrelevant, we only care about whether
  10418. the stub recognizes the packet or not. */
  10419. fd = remote_hostio_open (NULL, "just probing",
  10420. FILEIO_O_RDONLY, 0700, 0,
  10421. &remote_errno);
  10422. if (fd >= 0)
  10423. remote_hostio_close (fd, &remote_errno);
  10424. ps = packet_support (PACKET_vFile_open);
  10425. }
  10426. if (ps == PACKET_DISABLE)
  10427. {
  10428. static int warning_issued = 0;
  10429. if (!warning_issued)
  10430. {
  10431. warning (_("remote target does not support file"
  10432. " transfer, attempting to access files"
  10433. " from local filesystem."));
  10434. warning_issued = 1;
  10435. }
  10436. return true;
  10437. }
  10438. }
  10439. return false;
  10440. }
  10441. static int
  10442. remote_fileio_errno_to_host (int errnum)
  10443. {
  10444. switch (errnum)
  10445. {
  10446. case FILEIO_EPERM:
  10447. return EPERM;
  10448. case FILEIO_ENOENT:
  10449. return ENOENT;
  10450. case FILEIO_EINTR:
  10451. return EINTR;
  10452. case FILEIO_EIO:
  10453. return EIO;
  10454. case FILEIO_EBADF:
  10455. return EBADF;
  10456. case FILEIO_EACCES:
  10457. return EACCES;
  10458. case FILEIO_EFAULT:
  10459. return EFAULT;
  10460. case FILEIO_EBUSY:
  10461. return EBUSY;
  10462. case FILEIO_EEXIST:
  10463. return EEXIST;
  10464. case FILEIO_ENODEV:
  10465. return ENODEV;
  10466. case FILEIO_ENOTDIR:
  10467. return ENOTDIR;
  10468. case FILEIO_EISDIR:
  10469. return EISDIR;
  10470. case FILEIO_EINVAL:
  10471. return EINVAL;
  10472. case FILEIO_ENFILE:
  10473. return ENFILE;
  10474. case FILEIO_EMFILE:
  10475. return EMFILE;
  10476. case FILEIO_EFBIG:
  10477. return EFBIG;
  10478. case FILEIO_ENOSPC:
  10479. return ENOSPC;
  10480. case FILEIO_ESPIPE:
  10481. return ESPIPE;
  10482. case FILEIO_EROFS:
  10483. return EROFS;
  10484. case FILEIO_ENOSYS:
  10485. return ENOSYS;
  10486. case FILEIO_ENAMETOOLONG:
  10487. return ENAMETOOLONG;
  10488. }
  10489. return -1;
  10490. }
  10491. static char *
  10492. remote_hostio_error (int errnum)
  10493. {
  10494. int host_error = remote_fileio_errno_to_host (errnum);
  10495. if (host_error == -1)
  10496. error (_("Unknown remote I/O error %d"), errnum);
  10497. else
  10498. error (_("Remote I/O error: %s"), safe_strerror (host_error));
  10499. }
  10500. /* A RAII wrapper around a remote file descriptor. */
  10501. class scoped_remote_fd
  10502. {
  10503. public:
  10504. scoped_remote_fd (remote_target *remote, int fd)
  10505. : m_remote (remote), m_fd (fd)
  10506. {
  10507. }
  10508. ~scoped_remote_fd ()
  10509. {
  10510. if (m_fd != -1)
  10511. {
  10512. try
  10513. {
  10514. int remote_errno;
  10515. m_remote->remote_hostio_close (m_fd, &remote_errno);
  10516. }
  10517. catch (...)
  10518. {
  10519. /* Swallow exception before it escapes the dtor. If
  10520. something goes wrong, likely the connection is gone,
  10521. and there's nothing else that can be done. */
  10522. }
  10523. }
  10524. }
  10525. DISABLE_COPY_AND_ASSIGN (scoped_remote_fd);
  10526. /* Release ownership of the file descriptor, and return it. */
  10527. ATTRIBUTE_UNUSED_RESULT int release () noexcept
  10528. {
  10529. int fd = m_fd;
  10530. m_fd = -1;
  10531. return fd;
  10532. }
  10533. /* Return the owned file descriptor. */
  10534. int get () const noexcept
  10535. {
  10536. return m_fd;
  10537. }
  10538. private:
  10539. /* The remote target. */
  10540. remote_target *m_remote;
  10541. /* The owned remote I/O file descriptor. */
  10542. int m_fd;
  10543. };
  10544. void
  10545. remote_file_put (const char *local_file, const char *remote_file, int from_tty)
  10546. {
  10547. remote_target *remote = get_current_remote_target ();
  10548. if (remote == nullptr)
  10549. error (_("command can only be used with remote target"));
  10550. remote->remote_file_put (local_file, remote_file, from_tty);
  10551. }
  10552. void
  10553. remote_target::remote_file_put (const char *local_file, const char *remote_file,
  10554. int from_tty)
  10555. {
  10556. int retcode, remote_errno, bytes, io_size;
  10557. int bytes_in_buffer;
  10558. int saw_eof;
  10559. ULONGEST offset;
  10560. gdb_file_up file = gdb_fopen_cloexec (local_file, "rb");
  10561. if (file == NULL)
  10562. perror_with_name (local_file);
  10563. scoped_remote_fd fd
  10564. (this, remote_hostio_open (NULL,
  10565. remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
  10566. | FILEIO_O_TRUNC),
  10567. 0700, 0, &remote_errno));
  10568. if (fd.get () == -1)
  10569. remote_hostio_error (remote_errno);
  10570. /* Send up to this many bytes at once. They won't all fit in the
  10571. remote packet limit, so we'll transfer slightly fewer. */
  10572. io_size = get_remote_packet_size ();
  10573. gdb::byte_vector buffer (io_size);
  10574. bytes_in_buffer = 0;
  10575. saw_eof = 0;
  10576. offset = 0;
  10577. while (bytes_in_buffer || !saw_eof)
  10578. {
  10579. if (!saw_eof)
  10580. {
  10581. bytes = fread (buffer.data () + bytes_in_buffer, 1,
  10582. io_size - bytes_in_buffer,
  10583. file.get ());
  10584. if (bytes == 0)
  10585. {
  10586. if (ferror (file.get ()))
  10587. error (_("Error reading %s."), local_file);
  10588. else
  10589. {
  10590. /* EOF. Unless there is something still in the
  10591. buffer from the last iteration, we are done. */
  10592. saw_eof = 1;
  10593. if (bytes_in_buffer == 0)
  10594. break;
  10595. }
  10596. }
  10597. }
  10598. else
  10599. bytes = 0;
  10600. bytes += bytes_in_buffer;
  10601. bytes_in_buffer = 0;
  10602. retcode = remote_hostio_pwrite (fd.get (), buffer.data (), bytes,
  10603. offset, &remote_errno);
  10604. if (retcode < 0)
  10605. remote_hostio_error (remote_errno);
  10606. else if (retcode == 0)
  10607. error (_("Remote write of %d bytes returned 0!"), bytes);
  10608. else if (retcode < bytes)
  10609. {
  10610. /* Short write. Save the rest of the read data for the next
  10611. write. */
  10612. bytes_in_buffer = bytes - retcode;
  10613. memmove (buffer.data (), buffer.data () + retcode, bytes_in_buffer);
  10614. }
  10615. offset += retcode;
  10616. }
  10617. if (remote_hostio_close (fd.release (), &remote_errno))
  10618. remote_hostio_error (remote_errno);
  10619. if (from_tty)
  10620. gdb_printf (_("Successfully sent file \"%s\".\n"), local_file);
  10621. }
  10622. void
  10623. remote_file_get (const char *remote_file, const char *local_file, int from_tty)
  10624. {
  10625. remote_target *remote = get_current_remote_target ();
  10626. if (remote == nullptr)
  10627. error (_("command can only be used with remote target"));
  10628. remote->remote_file_get (remote_file, local_file, from_tty);
  10629. }
  10630. void
  10631. remote_target::remote_file_get (const char *remote_file, const char *local_file,
  10632. int from_tty)
  10633. {
  10634. int remote_errno, bytes, io_size;
  10635. ULONGEST offset;
  10636. scoped_remote_fd fd
  10637. (this, remote_hostio_open (NULL,
  10638. remote_file, FILEIO_O_RDONLY, 0, 0,
  10639. &remote_errno));
  10640. if (fd.get () == -1)
  10641. remote_hostio_error (remote_errno);
  10642. gdb_file_up file = gdb_fopen_cloexec (local_file, "wb");
  10643. if (file == NULL)
  10644. perror_with_name (local_file);
  10645. /* Send up to this many bytes at once. They won't all fit in the
  10646. remote packet limit, so we'll transfer slightly fewer. */
  10647. io_size = get_remote_packet_size ();
  10648. gdb::byte_vector buffer (io_size);
  10649. offset = 0;
  10650. while (1)
  10651. {
  10652. bytes = remote_hostio_pread (fd.get (), buffer.data (), io_size, offset,
  10653. &remote_errno);
  10654. if (bytes == 0)
  10655. /* Success, but no bytes, means end-of-file. */
  10656. break;
  10657. if (bytes == -1)
  10658. remote_hostio_error (remote_errno);
  10659. offset += bytes;
  10660. bytes = fwrite (buffer.data (), 1, bytes, file.get ());
  10661. if (bytes == 0)
  10662. perror_with_name (local_file);
  10663. }
  10664. if (remote_hostio_close (fd.release (), &remote_errno))
  10665. remote_hostio_error (remote_errno);
  10666. if (from_tty)
  10667. gdb_printf (_("Successfully fetched file \"%s\".\n"), remote_file);
  10668. }
  10669. void
  10670. remote_file_delete (const char *remote_file, int from_tty)
  10671. {
  10672. remote_target *remote = get_current_remote_target ();
  10673. if (remote == nullptr)
  10674. error (_("command can only be used with remote target"));
  10675. remote->remote_file_delete (remote_file, from_tty);
  10676. }
  10677. void
  10678. remote_target::remote_file_delete (const char *remote_file, int from_tty)
  10679. {
  10680. int retcode, remote_errno;
  10681. retcode = remote_hostio_unlink (NULL, remote_file, &remote_errno);
  10682. if (retcode == -1)
  10683. remote_hostio_error (remote_errno);
  10684. if (from_tty)
  10685. gdb_printf (_("Successfully deleted file \"%s\".\n"), remote_file);
  10686. }
  10687. static void
  10688. remote_put_command (const char *args, int from_tty)
  10689. {
  10690. if (args == NULL)
  10691. error_no_arg (_("file to put"));
  10692. gdb_argv argv (args);
  10693. if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
  10694. error (_("Invalid parameters to remote put"));
  10695. remote_file_put (argv[0], argv[1], from_tty);
  10696. }
  10697. static void
  10698. remote_get_command (const char *args, int from_tty)
  10699. {
  10700. if (args == NULL)
  10701. error_no_arg (_("file to get"));
  10702. gdb_argv argv (args);
  10703. if (argv[0] == NULL || argv[1] == NULL || argv[2] != NULL)
  10704. error (_("Invalid parameters to remote get"));
  10705. remote_file_get (argv[0], argv[1], from_tty);
  10706. }
  10707. static void
  10708. remote_delete_command (const char *args, int from_tty)
  10709. {
  10710. if (args == NULL)
  10711. error_no_arg (_("file to delete"));
  10712. gdb_argv argv (args);
  10713. if (argv[0] == NULL || argv[1] != NULL)
  10714. error (_("Invalid parameters to remote delete"));
  10715. remote_file_delete (argv[0], from_tty);
  10716. }
  10717. bool
  10718. remote_target::can_execute_reverse ()
  10719. {
  10720. if (packet_support (PACKET_bs) == PACKET_ENABLE
  10721. || packet_support (PACKET_bc) == PACKET_ENABLE)
  10722. return true;
  10723. else
  10724. return false;
  10725. }
  10726. bool
  10727. remote_target::supports_non_stop ()
  10728. {
  10729. return true;
  10730. }
  10731. bool
  10732. remote_target::supports_disable_randomization ()
  10733. {
  10734. /* Only supported in extended mode. */
  10735. return false;
  10736. }
  10737. bool
  10738. remote_target::supports_multi_process ()
  10739. {
  10740. struct remote_state *rs = get_remote_state ();
  10741. return remote_multi_process_p (rs);
  10742. }
  10743. static int
  10744. remote_supports_cond_tracepoints ()
  10745. {
  10746. return packet_support (PACKET_ConditionalTracepoints) == PACKET_ENABLE;
  10747. }
  10748. bool
  10749. remote_target::supports_evaluation_of_breakpoint_conditions ()
  10750. {
  10751. return packet_support (PACKET_ConditionalBreakpoints) == PACKET_ENABLE;
  10752. }
  10753. static int
  10754. remote_supports_fast_tracepoints ()
  10755. {
  10756. return packet_support (PACKET_FastTracepoints) == PACKET_ENABLE;
  10757. }
  10758. static int
  10759. remote_supports_static_tracepoints ()
  10760. {
  10761. return packet_support (PACKET_StaticTracepoints) == PACKET_ENABLE;
  10762. }
  10763. static int
  10764. remote_supports_install_in_trace ()
  10765. {
  10766. return packet_support (PACKET_InstallInTrace) == PACKET_ENABLE;
  10767. }
  10768. bool
  10769. remote_target::supports_enable_disable_tracepoint ()
  10770. {
  10771. return (packet_support (PACKET_EnableDisableTracepoints_feature)
  10772. == PACKET_ENABLE);
  10773. }
  10774. bool
  10775. remote_target::supports_string_tracing ()
  10776. {
  10777. return packet_support (PACKET_tracenz_feature) == PACKET_ENABLE;
  10778. }
  10779. bool
  10780. remote_target::can_run_breakpoint_commands ()
  10781. {
  10782. return packet_support (PACKET_BreakpointCommands) == PACKET_ENABLE;
  10783. }
  10784. void
  10785. remote_target::trace_init ()
  10786. {
  10787. struct remote_state *rs = get_remote_state ();
  10788. putpkt ("QTinit");
  10789. remote_get_noisy_reply ();
  10790. if (strcmp (rs->buf.data (), "OK") != 0)
  10791. error (_("Target does not support this command."));
  10792. }
  10793. /* Recursive routine to walk through command list including loops, and
  10794. download packets for each command. */
  10795. void
  10796. remote_target::remote_download_command_source (int num, ULONGEST addr,
  10797. struct command_line *cmds)
  10798. {
  10799. struct remote_state *rs = get_remote_state ();
  10800. struct command_line *cmd;
  10801. for (cmd = cmds; cmd; cmd = cmd->next)
  10802. {
  10803. QUIT; /* Allow user to bail out with ^C. */
  10804. strcpy (rs->buf.data (), "QTDPsrc:");
  10805. encode_source_string (num, addr, "cmd", cmd->line,
  10806. rs->buf.data () + strlen (rs->buf.data ()),
  10807. rs->buf.size () - strlen (rs->buf.data ()));
  10808. putpkt (rs->buf);
  10809. remote_get_noisy_reply ();
  10810. if (strcmp (rs->buf.data (), "OK"))
  10811. warning (_("Target does not support source download."));
  10812. if (cmd->control_type == while_control
  10813. || cmd->control_type == while_stepping_control)
  10814. {
  10815. remote_download_command_source (num, addr, cmd->body_list_0.get ());
  10816. QUIT; /* Allow user to bail out with ^C. */
  10817. strcpy (rs->buf.data (), "QTDPsrc:");
  10818. encode_source_string (num, addr, "cmd", "end",
  10819. rs->buf.data () + strlen (rs->buf.data ()),
  10820. rs->buf.size () - strlen (rs->buf.data ()));
  10821. putpkt (rs->buf);
  10822. remote_get_noisy_reply ();
  10823. if (strcmp (rs->buf.data (), "OK"))
  10824. warning (_("Target does not support source download."));
  10825. }
  10826. }
  10827. }
  10828. void
  10829. remote_target::download_tracepoint (struct bp_location *loc)
  10830. {
  10831. CORE_ADDR tpaddr;
  10832. char addrbuf[40];
  10833. std::vector<std::string> tdp_actions;
  10834. std::vector<std::string> stepping_actions;
  10835. char *pkt;
  10836. struct breakpoint *b = loc->owner;
  10837. struct tracepoint *t = (struct tracepoint *) b;
  10838. struct remote_state *rs = get_remote_state ();
  10839. int ret;
  10840. const char *err_msg = _("Tracepoint packet too large for target.");
  10841. size_t size_left;
  10842. /* We use a buffer other than rs->buf because we'll build strings
  10843. across multiple statements, and other statements in between could
  10844. modify rs->buf. */
  10845. gdb::char_vector buf (get_remote_packet_size ());
  10846. encode_actions_rsp (loc, &tdp_actions, &stepping_actions);
  10847. tpaddr = loc->address;
  10848. strcpy (addrbuf, phex (tpaddr, sizeof (CORE_ADDR)));
  10849. ret = snprintf (buf.data (), buf.size (), "QTDP:%x:%s:%c:%lx:%x",
  10850. b->number, addrbuf, /* address */
  10851. (b->enable_state == bp_enabled ? 'E' : 'D'),
  10852. t->step_count, t->pass_count);
  10853. if (ret < 0 || ret >= buf.size ())
  10854. error ("%s", err_msg);
  10855. /* Fast tracepoints are mostly handled by the target, but we can
  10856. tell the target how big of an instruction block should be moved
  10857. around. */
  10858. if (b->type == bp_fast_tracepoint)
  10859. {
  10860. /* Only test for support at download time; we may not know
  10861. target capabilities at definition time. */
  10862. if (remote_supports_fast_tracepoints ())
  10863. {
  10864. if (gdbarch_fast_tracepoint_valid_at (loc->gdbarch, tpaddr,
  10865. NULL))
  10866. {
  10867. size_left = buf.size () - strlen (buf.data ());
  10868. ret = snprintf (buf.data () + strlen (buf.data ()),
  10869. size_left, ":F%x",
  10870. gdb_insn_length (loc->gdbarch, tpaddr));
  10871. if (ret < 0 || ret >= size_left)
  10872. error ("%s", err_msg);
  10873. }
  10874. else
  10875. /* If it passed validation at definition but fails now,
  10876. something is very wrong. */
  10877. internal_error (__FILE__, __LINE__,
  10878. _("Fast tracepoint not "
  10879. "valid during download"));
  10880. }
  10881. else
  10882. /* Fast tracepoints are functionally identical to regular
  10883. tracepoints, so don't take lack of support as a reason to
  10884. give up on the trace run. */
  10885. warning (_("Target does not support fast tracepoints, "
  10886. "downloading %d as regular tracepoint"), b->number);
  10887. }
  10888. else if (b->type == bp_static_tracepoint)
  10889. {
  10890. /* Only test for support at download time; we may not know
  10891. target capabilities at definition time. */
  10892. if (remote_supports_static_tracepoints ())
  10893. {
  10894. struct static_tracepoint_marker marker;
  10895. if (target_static_tracepoint_marker_at (tpaddr, &marker))
  10896. {
  10897. size_left = buf.size () - strlen (buf.data ());
  10898. ret = snprintf (buf.data () + strlen (buf.data ()),
  10899. size_left, ":S");
  10900. if (ret < 0 || ret >= size_left)
  10901. error ("%s", err_msg);
  10902. }
  10903. else
  10904. error (_("Static tracepoint not valid during download"));
  10905. }
  10906. else
  10907. /* Fast tracepoints are functionally identical to regular
  10908. tracepoints, so don't take lack of support as a reason
  10909. to give up on the trace run. */
  10910. error (_("Target does not support static tracepoints"));
  10911. }
  10912. /* If the tracepoint has a conditional, make it into an agent
  10913. expression and append to the definition. */
  10914. if (loc->cond)
  10915. {
  10916. /* Only test support at download time, we may not know target
  10917. capabilities at definition time. */
  10918. if (remote_supports_cond_tracepoints ())
  10919. {
  10920. agent_expr_up aexpr = gen_eval_for_expr (tpaddr,
  10921. loc->cond.get ());
  10922. size_left = buf.size () - strlen (buf.data ());
  10923. ret = snprintf (buf.data () + strlen (buf.data ()),
  10924. size_left, ":X%x,", aexpr->len);
  10925. if (ret < 0 || ret >= size_left)
  10926. error ("%s", err_msg);
  10927. size_left = buf.size () - strlen (buf.data ());
  10928. /* Two bytes to encode each aexpr byte, plus the terminating
  10929. null byte. */
  10930. if (aexpr->len * 2 + 1 > size_left)
  10931. error ("%s", err_msg);
  10932. pkt = buf.data () + strlen (buf.data ());
  10933. for (int ndx = 0; ndx < aexpr->len; ++ndx)
  10934. pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
  10935. *pkt = '\0';
  10936. }
  10937. else
  10938. warning (_("Target does not support conditional tracepoints, "
  10939. "ignoring tp %d cond"), b->number);
  10940. }
  10941. if (b->commands || !default_collect.empty ())
  10942. {
  10943. size_left = buf.size () - strlen (buf.data ());
  10944. ret = snprintf (buf.data () + strlen (buf.data ()),
  10945. size_left, "-");
  10946. if (ret < 0 || ret >= size_left)
  10947. error ("%s", err_msg);
  10948. }
  10949. putpkt (buf.data ());
  10950. remote_get_noisy_reply ();
  10951. if (strcmp (rs->buf.data (), "OK"))
  10952. error (_("Target does not support tracepoints."));
  10953. /* do_single_steps (t); */
  10954. for (auto action_it = tdp_actions.begin ();
  10955. action_it != tdp_actions.end (); action_it++)
  10956. {
  10957. QUIT; /* Allow user to bail out with ^C. */
  10958. bool has_more = ((action_it + 1) != tdp_actions.end ()
  10959. || !stepping_actions.empty ());
  10960. ret = snprintf (buf.data (), buf.size (), "QTDP:-%x:%s:%s%c",
  10961. b->number, addrbuf, /* address */
  10962. action_it->c_str (),
  10963. has_more ? '-' : 0);
  10964. if (ret < 0 || ret >= buf.size ())
  10965. error ("%s", err_msg);
  10966. putpkt (buf.data ());
  10967. remote_get_noisy_reply ();
  10968. if (strcmp (rs->buf.data (), "OK"))
  10969. error (_("Error on target while setting tracepoints."));
  10970. }
  10971. for (auto action_it = stepping_actions.begin ();
  10972. action_it != stepping_actions.end (); action_it++)
  10973. {
  10974. QUIT; /* Allow user to bail out with ^C. */
  10975. bool is_first = action_it == stepping_actions.begin ();
  10976. bool has_more = (action_it + 1) != stepping_actions.end ();
  10977. ret = snprintf (buf.data (), buf.size (), "QTDP:-%x:%s:%s%s%s",
  10978. b->number, addrbuf, /* address */
  10979. is_first ? "S" : "",
  10980. action_it->c_str (),
  10981. has_more ? "-" : "");
  10982. if (ret < 0 || ret >= buf.size ())
  10983. error ("%s", err_msg);
  10984. putpkt (buf.data ());
  10985. remote_get_noisy_reply ();
  10986. if (strcmp (rs->buf.data (), "OK"))
  10987. error (_("Error on target while setting tracepoints."));
  10988. }
  10989. if (packet_support (PACKET_TracepointSource) == PACKET_ENABLE)
  10990. {
  10991. if (b->location != NULL)
  10992. {
  10993. ret = snprintf (buf.data (), buf.size (), "QTDPsrc:");
  10994. if (ret < 0 || ret >= buf.size ())
  10995. error ("%s", err_msg);
  10996. encode_source_string (b->number, loc->address, "at",
  10997. event_location_to_string (b->location.get ()),
  10998. buf.data () + strlen (buf.data ()),
  10999. buf.size () - strlen (buf.data ()));
  11000. putpkt (buf.data ());
  11001. remote_get_noisy_reply ();
  11002. if (strcmp (rs->buf.data (), "OK"))
  11003. warning (_("Target does not support source download."));
  11004. }
  11005. if (b->cond_string)
  11006. {
  11007. ret = snprintf (buf.data (), buf.size (), "QTDPsrc:");
  11008. if (ret < 0 || ret >= buf.size ())
  11009. error ("%s", err_msg);
  11010. encode_source_string (b->number, loc->address,
  11011. "cond", b->cond_string.get (),
  11012. buf.data () + strlen (buf.data ()),
  11013. buf.size () - strlen (buf.data ()));
  11014. putpkt (buf.data ());
  11015. remote_get_noisy_reply ();
  11016. if (strcmp (rs->buf.data (), "OK"))
  11017. warning (_("Target does not support source download."));
  11018. }
  11019. remote_download_command_source (b->number, loc->address,
  11020. breakpoint_commands (b));
  11021. }
  11022. }
  11023. bool
  11024. remote_target::can_download_tracepoint ()
  11025. {
  11026. struct remote_state *rs = get_remote_state ();
  11027. struct trace_status *ts;
  11028. int status;
  11029. /* Don't try to install tracepoints until we've relocated our
  11030. symbols, and fetched and merged the target's tracepoint list with
  11031. ours. */
  11032. if (rs->starting_up)
  11033. return false;
  11034. ts = current_trace_status ();
  11035. status = get_trace_status (ts);
  11036. if (status == -1 || !ts->running_known || !ts->running)
  11037. return false;
  11038. /* If we are in a tracing experiment, but remote stub doesn't support
  11039. installing tracepoint in trace, we have to return. */
  11040. if (!remote_supports_install_in_trace ())
  11041. return false;
  11042. return true;
  11043. }
  11044. void
  11045. remote_target::download_trace_state_variable (const trace_state_variable &tsv)
  11046. {
  11047. struct remote_state *rs = get_remote_state ();
  11048. char *p;
  11049. xsnprintf (rs->buf.data (), get_remote_packet_size (), "QTDV:%x:%s:%x:",
  11050. tsv.number, phex ((ULONGEST) tsv.initial_value, 8),
  11051. tsv.builtin);
  11052. p = rs->buf.data () + strlen (rs->buf.data ());
  11053. if ((p - rs->buf.data ()) + tsv.name.length () * 2
  11054. >= get_remote_packet_size ())
  11055. error (_("Trace state variable name too long for tsv definition packet"));
  11056. p += 2 * bin2hex ((gdb_byte *) (tsv.name.data ()), p, tsv.name.length ());
  11057. *p++ = '\0';
  11058. putpkt (rs->buf);
  11059. remote_get_noisy_reply ();
  11060. if (rs->buf[0] == '\0')
  11061. error (_("Target does not support this command."));
  11062. if (strcmp (rs->buf.data (), "OK") != 0)
  11063. error (_("Error on target while downloading trace state variable."));
  11064. }
  11065. void
  11066. remote_target::enable_tracepoint (struct bp_location *location)
  11067. {
  11068. struct remote_state *rs = get_remote_state ();
  11069. xsnprintf (rs->buf.data (), get_remote_packet_size (), "QTEnable:%x:%s",
  11070. location->owner->number,
  11071. phex (location->address, sizeof (CORE_ADDR)));
  11072. putpkt (rs->buf);
  11073. remote_get_noisy_reply ();
  11074. if (rs->buf[0] == '\0')
  11075. error (_("Target does not support enabling tracepoints while a trace run is ongoing."));
  11076. if (strcmp (rs->buf.data (), "OK") != 0)
  11077. error (_("Error on target while enabling tracepoint."));
  11078. }
  11079. void
  11080. remote_target::disable_tracepoint (struct bp_location *location)
  11081. {
  11082. struct remote_state *rs = get_remote_state ();
  11083. xsnprintf (rs->buf.data (), get_remote_packet_size (), "QTDisable:%x:%s",
  11084. location->owner->number,
  11085. phex (location->address, sizeof (CORE_ADDR)));
  11086. putpkt (rs->buf);
  11087. remote_get_noisy_reply ();
  11088. if (rs->buf[0] == '\0')
  11089. error (_("Target does not support disabling tracepoints while a trace run is ongoing."));
  11090. if (strcmp (rs->buf.data (), "OK") != 0)
  11091. error (_("Error on target while disabling tracepoint."));
  11092. }
  11093. void
  11094. remote_target::trace_set_readonly_regions ()
  11095. {
  11096. asection *s;
  11097. bfd_size_type size;
  11098. bfd_vma vma;
  11099. int anysecs = 0;
  11100. int offset = 0;
  11101. if (!current_program_space->exec_bfd ())
  11102. return; /* No information to give. */
  11103. struct remote_state *rs = get_remote_state ();
  11104. strcpy (rs->buf.data (), "QTro");
  11105. offset = strlen (rs->buf.data ());
  11106. for (s = current_program_space->exec_bfd ()->sections; s; s = s->next)
  11107. {
  11108. char tmp1[40], tmp2[40];
  11109. int sec_length;
  11110. if ((s->flags & SEC_LOAD) == 0 ||
  11111. /* (s->flags & SEC_CODE) == 0 || */
  11112. (s->flags & SEC_READONLY) == 0)
  11113. continue;
  11114. anysecs = 1;
  11115. vma = bfd_section_vma (s);
  11116. size = bfd_section_size (s);
  11117. sprintf_vma (tmp1, vma);
  11118. sprintf_vma (tmp2, vma + size);
  11119. sec_length = 1 + strlen (tmp1) + 1 + strlen (tmp2);
  11120. if (offset + sec_length + 1 > rs->buf.size ())
  11121. {
  11122. if (packet_support (PACKET_qXfer_traceframe_info) != PACKET_ENABLE)
  11123. warning (_("\
  11124. Too many sections for read-only sections definition packet."));
  11125. break;
  11126. }
  11127. xsnprintf (rs->buf.data () + offset, rs->buf.size () - offset, ":%s,%s",
  11128. tmp1, tmp2);
  11129. offset += sec_length;
  11130. }
  11131. if (anysecs)
  11132. {
  11133. putpkt (rs->buf);
  11134. getpkt (&rs->buf, 0);
  11135. }
  11136. }
  11137. void
  11138. remote_target::trace_start ()
  11139. {
  11140. struct remote_state *rs = get_remote_state ();
  11141. putpkt ("QTStart");
  11142. remote_get_noisy_reply ();
  11143. if (rs->buf[0] == '\0')
  11144. error (_("Target does not support this command."));
  11145. if (strcmp (rs->buf.data (), "OK") != 0)
  11146. error (_("Bogus reply from target: %s"), rs->buf.data ());
  11147. }
  11148. int
  11149. remote_target::get_trace_status (struct trace_status *ts)
  11150. {
  11151. /* Initialize it just to avoid a GCC false warning. */
  11152. char *p = NULL;
  11153. enum packet_result result;
  11154. struct remote_state *rs = get_remote_state ();
  11155. if (packet_support (PACKET_qTStatus) == PACKET_DISABLE)
  11156. return -1;
  11157. /* FIXME we need to get register block size some other way. */
  11158. trace_regblock_size
  11159. = rs->get_remote_arch_state (target_gdbarch ())->sizeof_g_packet;
  11160. putpkt ("qTStatus");
  11161. try
  11162. {
  11163. p = remote_get_noisy_reply ();
  11164. }
  11165. catch (const gdb_exception_error &ex)
  11166. {
  11167. if (ex.error != TARGET_CLOSE_ERROR)
  11168. {
  11169. exception_fprintf (gdb_stderr, ex, "qTStatus: ");
  11170. return -1;
  11171. }
  11172. throw;
  11173. }
  11174. result = packet_ok (p, &remote_protocol_packets[PACKET_qTStatus]);
  11175. /* If the remote target doesn't do tracing, flag it. */
  11176. if (result == PACKET_UNKNOWN)
  11177. return -1;
  11178. /* We're working with a live target. */
  11179. ts->filename = NULL;
  11180. if (*p++ != 'T')
  11181. error (_("Bogus trace status reply from target: %s"), rs->buf.data ());
  11182. /* Function 'parse_trace_status' sets default value of each field of
  11183. 'ts' at first, so we don't have to do it here. */
  11184. parse_trace_status (p, ts);
  11185. return ts->running;
  11186. }
  11187. void
  11188. remote_target::get_tracepoint_status (struct breakpoint *bp,
  11189. struct uploaded_tp *utp)
  11190. {
  11191. struct remote_state *rs = get_remote_state ();
  11192. char *reply;
  11193. struct tracepoint *tp = (struct tracepoint *) bp;
  11194. size_t size = get_remote_packet_size ();
  11195. if (tp)
  11196. {
  11197. tp->hit_count = 0;
  11198. tp->traceframe_usage = 0;
  11199. for (bp_location *loc : tp->locations ())
  11200. {
  11201. /* If the tracepoint was never downloaded, don't go asking for
  11202. any status. */
  11203. if (tp->number_on_target == 0)
  11204. continue;
  11205. xsnprintf (rs->buf.data (), size, "qTP:%x:%s", tp->number_on_target,
  11206. phex_nz (loc->address, 0));
  11207. putpkt (rs->buf);
  11208. reply = remote_get_noisy_reply ();
  11209. if (reply && *reply)
  11210. {
  11211. if (*reply == 'V')
  11212. parse_tracepoint_status (reply + 1, bp, utp);
  11213. }
  11214. }
  11215. }
  11216. else if (utp)
  11217. {
  11218. utp->hit_count = 0;
  11219. utp->traceframe_usage = 0;
  11220. xsnprintf (rs->buf.data (), size, "qTP:%x:%s", utp->number,
  11221. phex_nz (utp->addr, 0));
  11222. putpkt (rs->buf);
  11223. reply = remote_get_noisy_reply ();
  11224. if (reply && *reply)
  11225. {
  11226. if (*reply == 'V')
  11227. parse_tracepoint_status (reply + 1, bp, utp);
  11228. }
  11229. }
  11230. }
  11231. void
  11232. remote_target::trace_stop ()
  11233. {
  11234. struct remote_state *rs = get_remote_state ();
  11235. putpkt ("QTStop");
  11236. remote_get_noisy_reply ();
  11237. if (rs->buf[0] == '\0')
  11238. error (_("Target does not support this command."));
  11239. if (strcmp (rs->buf.data (), "OK") != 0)
  11240. error (_("Bogus reply from target: %s"), rs->buf.data ());
  11241. }
  11242. int
  11243. remote_target::trace_find (enum trace_find_type type, int num,
  11244. CORE_ADDR addr1, CORE_ADDR addr2,
  11245. int *tpp)
  11246. {
  11247. struct remote_state *rs = get_remote_state ();
  11248. char *endbuf = rs->buf.data () + get_remote_packet_size ();
  11249. char *p, *reply;
  11250. int target_frameno = -1, target_tracept = -1;
  11251. /* Lookups other than by absolute frame number depend on the current
  11252. trace selected, so make sure it is correct on the remote end
  11253. first. */
  11254. if (type != tfind_number)
  11255. set_remote_traceframe ();
  11256. p = rs->buf.data ();
  11257. strcpy (p, "QTFrame:");
  11258. p = strchr (p, '\0');
  11259. switch (type)
  11260. {
  11261. case tfind_number:
  11262. xsnprintf (p, endbuf - p, "%x", num);
  11263. break;
  11264. case tfind_pc:
  11265. xsnprintf (p, endbuf - p, "pc:%s", phex_nz (addr1, 0));
  11266. break;
  11267. case tfind_tp:
  11268. xsnprintf (p, endbuf - p, "tdp:%x", num);
  11269. break;
  11270. case tfind_range:
  11271. xsnprintf (p, endbuf - p, "range:%s:%s", phex_nz (addr1, 0),
  11272. phex_nz (addr2, 0));
  11273. break;
  11274. case tfind_outside:
  11275. xsnprintf (p, endbuf - p, "outside:%s:%s", phex_nz (addr1, 0),
  11276. phex_nz (addr2, 0));
  11277. break;
  11278. default:
  11279. error (_("Unknown trace find type %d"), type);
  11280. }
  11281. putpkt (rs->buf);
  11282. reply = remote_get_noisy_reply ();
  11283. if (*reply == '\0')
  11284. error (_("Target does not support this command."));
  11285. while (reply && *reply)
  11286. switch (*reply)
  11287. {
  11288. case 'F':
  11289. p = ++reply;
  11290. target_frameno = (int) strtol (p, &reply, 16);
  11291. if (reply == p)
  11292. error (_("Unable to parse trace frame number"));
  11293. /* Don't update our remote traceframe number cache on failure
  11294. to select a remote traceframe. */
  11295. if (target_frameno == -1)
  11296. return -1;
  11297. break;
  11298. case 'T':
  11299. p = ++reply;
  11300. target_tracept = (int) strtol (p, &reply, 16);
  11301. if (reply == p)
  11302. error (_("Unable to parse tracepoint number"));
  11303. break;
  11304. case 'O': /* "OK"? */
  11305. if (reply[1] == 'K' && reply[2] == '\0')
  11306. reply += 2;
  11307. else
  11308. error (_("Bogus reply from target: %s"), reply);
  11309. break;
  11310. default:
  11311. error (_("Bogus reply from target: %s"), reply);
  11312. }
  11313. if (tpp)
  11314. *tpp = target_tracept;
  11315. rs->remote_traceframe_number = target_frameno;
  11316. return target_frameno;
  11317. }
  11318. bool
  11319. remote_target::get_trace_state_variable_value (int tsvnum, LONGEST *val)
  11320. {
  11321. struct remote_state *rs = get_remote_state ();
  11322. char *reply;
  11323. ULONGEST uval;
  11324. set_remote_traceframe ();
  11325. xsnprintf (rs->buf.data (), get_remote_packet_size (), "qTV:%x", tsvnum);
  11326. putpkt (rs->buf);
  11327. reply = remote_get_noisy_reply ();
  11328. if (reply && *reply)
  11329. {
  11330. if (*reply == 'V')
  11331. {
  11332. unpack_varlen_hex (reply + 1, &uval);
  11333. *val = (LONGEST) uval;
  11334. return true;
  11335. }
  11336. }
  11337. return false;
  11338. }
  11339. int
  11340. remote_target::save_trace_data (const char *filename)
  11341. {
  11342. struct remote_state *rs = get_remote_state ();
  11343. char *p, *reply;
  11344. p = rs->buf.data ();
  11345. strcpy (p, "QTSave:");
  11346. p += strlen (p);
  11347. if ((p - rs->buf.data ()) + strlen (filename) * 2
  11348. >= get_remote_packet_size ())
  11349. error (_("Remote file name too long for trace save packet"));
  11350. p += 2 * bin2hex ((gdb_byte *) filename, p, strlen (filename));
  11351. *p++ = '\0';
  11352. putpkt (rs->buf);
  11353. reply = remote_get_noisy_reply ();
  11354. if (*reply == '\0')
  11355. error (_("Target does not support this command."));
  11356. if (strcmp (reply, "OK") != 0)
  11357. error (_("Bogus reply from target: %s"), reply);
  11358. return 0;
  11359. }
  11360. /* This is basically a memory transfer, but needs to be its own packet
  11361. because we don't know how the target actually organizes its trace
  11362. memory, plus we want to be able to ask for as much as possible, but
  11363. not be unhappy if we don't get as much as we ask for. */
  11364. LONGEST
  11365. remote_target::get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
  11366. {
  11367. struct remote_state *rs = get_remote_state ();
  11368. char *reply;
  11369. char *p;
  11370. int rslt;
  11371. p = rs->buf.data ();
  11372. strcpy (p, "qTBuffer:");
  11373. p += strlen (p);
  11374. p += hexnumstr (p, offset);
  11375. *p++ = ',';
  11376. p += hexnumstr (p, len);
  11377. *p++ = '\0';
  11378. putpkt (rs->buf);
  11379. reply = remote_get_noisy_reply ();
  11380. if (reply && *reply)
  11381. {
  11382. /* 'l' by itself means we're at the end of the buffer and
  11383. there is nothing more to get. */
  11384. if (*reply == 'l')
  11385. return 0;
  11386. /* Convert the reply into binary. Limit the number of bytes to
  11387. convert according to our passed-in buffer size, rather than
  11388. what was returned in the packet; if the target is
  11389. unexpectedly generous and gives us a bigger reply than we
  11390. asked for, we don't want to crash. */
  11391. rslt = hex2bin (reply, buf, len);
  11392. return rslt;
  11393. }
  11394. /* Something went wrong, flag as an error. */
  11395. return -1;
  11396. }
  11397. void
  11398. remote_target::set_disconnected_tracing (int val)
  11399. {
  11400. struct remote_state *rs = get_remote_state ();
  11401. if (packet_support (PACKET_DisconnectedTracing_feature) == PACKET_ENABLE)
  11402. {
  11403. char *reply;
  11404. xsnprintf (rs->buf.data (), get_remote_packet_size (),
  11405. "QTDisconnected:%x", val);
  11406. putpkt (rs->buf);
  11407. reply = remote_get_noisy_reply ();
  11408. if (*reply == '\0')
  11409. error (_("Target does not support this command."));
  11410. if (strcmp (reply, "OK") != 0)
  11411. error (_("Bogus reply from target: %s"), reply);
  11412. }
  11413. else if (val)
  11414. warning (_("Target does not support disconnected tracing."));
  11415. }
  11416. int
  11417. remote_target::core_of_thread (ptid_t ptid)
  11418. {
  11419. thread_info *info = find_thread_ptid (this, ptid);
  11420. if (info != NULL && info->priv != NULL)
  11421. return get_remote_thread_info (info)->core;
  11422. return -1;
  11423. }
  11424. void
  11425. remote_target::set_circular_trace_buffer (int val)
  11426. {
  11427. struct remote_state *rs = get_remote_state ();
  11428. char *reply;
  11429. xsnprintf (rs->buf.data (), get_remote_packet_size (),
  11430. "QTBuffer:circular:%x", val);
  11431. putpkt (rs->buf);
  11432. reply = remote_get_noisy_reply ();
  11433. if (*reply == '\0')
  11434. error (_("Target does not support this command."));
  11435. if (strcmp (reply, "OK") != 0)
  11436. error (_("Bogus reply from target: %s"), reply);
  11437. }
  11438. traceframe_info_up
  11439. remote_target::traceframe_info ()
  11440. {
  11441. gdb::optional<gdb::char_vector> text
  11442. = target_read_stralloc (current_inferior ()->top_target (),
  11443. TARGET_OBJECT_TRACEFRAME_INFO,
  11444. NULL);
  11445. if (text)
  11446. return parse_traceframe_info (text->data ());
  11447. return NULL;
  11448. }
  11449. /* Handle the qTMinFTPILen packet. Returns the minimum length of
  11450. instruction on which a fast tracepoint may be placed. Returns -1
  11451. if the packet is not supported, and 0 if the minimum instruction
  11452. length is unknown. */
  11453. int
  11454. remote_target::get_min_fast_tracepoint_insn_len ()
  11455. {
  11456. struct remote_state *rs = get_remote_state ();
  11457. char *reply;
  11458. /* If we're not debugging a process yet, the IPA can't be
  11459. loaded. */
  11460. if (!target_has_execution ())
  11461. return 0;
  11462. /* Make sure the remote is pointing at the right process. */
  11463. set_general_process ();
  11464. xsnprintf (rs->buf.data (), get_remote_packet_size (), "qTMinFTPILen");
  11465. putpkt (rs->buf);
  11466. reply = remote_get_noisy_reply ();
  11467. if (*reply == '\0')
  11468. return -1;
  11469. else
  11470. {
  11471. ULONGEST min_insn_len;
  11472. unpack_varlen_hex (reply, &min_insn_len);
  11473. return (int) min_insn_len;
  11474. }
  11475. }
  11476. void
  11477. remote_target::set_trace_buffer_size (LONGEST val)
  11478. {
  11479. if (packet_support (PACKET_QTBuffer_size) != PACKET_DISABLE)
  11480. {
  11481. struct remote_state *rs = get_remote_state ();
  11482. char *buf = rs->buf.data ();
  11483. char *endbuf = buf + get_remote_packet_size ();
  11484. enum packet_result result;
  11485. gdb_assert (val >= 0 || val == -1);
  11486. buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:");
  11487. /* Send -1 as literal "-1" to avoid host size dependency. */
  11488. if (val < 0)
  11489. {
  11490. *buf++ = '-';
  11491. buf += hexnumstr (buf, (ULONGEST) -val);
  11492. }
  11493. else
  11494. buf += hexnumstr (buf, (ULONGEST) val);
  11495. putpkt (rs->buf);
  11496. remote_get_noisy_reply ();
  11497. result = packet_ok (rs->buf,
  11498. &remote_protocol_packets[PACKET_QTBuffer_size]);
  11499. if (result != PACKET_OK)
  11500. warning (_("Bogus reply from target: %s"), rs->buf.data ());
  11501. }
  11502. }
  11503. bool
  11504. remote_target::set_trace_notes (const char *user, const char *notes,
  11505. const char *stop_notes)
  11506. {
  11507. struct remote_state *rs = get_remote_state ();
  11508. char *reply;
  11509. char *buf = rs->buf.data ();
  11510. char *endbuf = buf + get_remote_packet_size ();
  11511. int nbytes;
  11512. buf += xsnprintf (buf, endbuf - buf, "QTNotes:");
  11513. if (user)
  11514. {
  11515. buf += xsnprintf (buf, endbuf - buf, "user:");
  11516. nbytes = bin2hex ((gdb_byte *) user, buf, strlen (user));
  11517. buf += 2 * nbytes;
  11518. *buf++ = ';';
  11519. }
  11520. if (notes)
  11521. {
  11522. buf += xsnprintf (buf, endbuf - buf, "notes:");
  11523. nbytes = bin2hex ((gdb_byte *) notes, buf, strlen (notes));
  11524. buf += 2 * nbytes;
  11525. *buf++ = ';';
  11526. }
  11527. if (stop_notes)
  11528. {
  11529. buf += xsnprintf (buf, endbuf - buf, "tstop:");
  11530. nbytes = bin2hex ((gdb_byte *) stop_notes, buf, strlen (stop_notes));
  11531. buf += 2 * nbytes;
  11532. *buf++ = ';';
  11533. }
  11534. /* Ensure the buffer is terminated. */
  11535. *buf = '\0';
  11536. putpkt (rs->buf);
  11537. reply = remote_get_noisy_reply ();
  11538. if (*reply == '\0')
  11539. return false;
  11540. if (strcmp (reply, "OK") != 0)
  11541. error (_("Bogus reply from target: %s"), reply);
  11542. return true;
  11543. }
  11544. bool
  11545. remote_target::use_agent (bool use)
  11546. {
  11547. if (packet_support (PACKET_QAgent) != PACKET_DISABLE)
  11548. {
  11549. struct remote_state *rs = get_remote_state ();
  11550. /* If the stub supports QAgent. */
  11551. xsnprintf (rs->buf.data (), get_remote_packet_size (), "QAgent:%d", use);
  11552. putpkt (rs->buf);
  11553. getpkt (&rs->buf, 0);
  11554. if (strcmp (rs->buf.data (), "OK") == 0)
  11555. {
  11556. ::use_agent = use;
  11557. return true;
  11558. }
  11559. }
  11560. return false;
  11561. }
  11562. bool
  11563. remote_target::can_use_agent ()
  11564. {
  11565. return (packet_support (PACKET_QAgent) != PACKET_DISABLE);
  11566. }
  11567. struct btrace_target_info
  11568. {
  11569. /* The ptid of the traced thread. */
  11570. ptid_t ptid;
  11571. /* The obtained branch trace configuration. */
  11572. struct btrace_config conf;
  11573. };
  11574. /* Reset our idea of our target's btrace configuration. */
  11575. static void
  11576. remote_btrace_reset (remote_state *rs)
  11577. {
  11578. memset (&rs->btrace_config, 0, sizeof (rs->btrace_config));
  11579. }
  11580. /* Synchronize the configuration with the target. */
  11581. void
  11582. remote_target::btrace_sync_conf (const btrace_config *conf)
  11583. {
  11584. struct packet_config *packet;
  11585. struct remote_state *rs;
  11586. char *buf, *pos, *endbuf;
  11587. rs = get_remote_state ();
  11588. buf = rs->buf.data ();
  11589. endbuf = buf + get_remote_packet_size ();
  11590. packet = &remote_protocol_packets[PACKET_Qbtrace_conf_bts_size];
  11591. if (packet_config_support (packet) == PACKET_ENABLE
  11592. && conf->bts.size != rs->btrace_config.bts.size)
  11593. {
  11594. pos = buf;
  11595. pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
  11596. conf->bts.size);
  11597. putpkt (buf);
  11598. getpkt (&rs->buf, 0);
  11599. if (packet_ok (buf, packet) == PACKET_ERROR)
  11600. {
  11601. if (buf[0] == 'E' && buf[1] == '.')
  11602. error (_("Failed to configure the BTS buffer size: %s"), buf + 2);
  11603. else
  11604. error (_("Failed to configure the BTS buffer size."));
  11605. }
  11606. rs->btrace_config.bts.size = conf->bts.size;
  11607. }
  11608. packet = &remote_protocol_packets[PACKET_Qbtrace_conf_pt_size];
  11609. if (packet_config_support (packet) == PACKET_ENABLE
  11610. && conf->pt.size != rs->btrace_config.pt.size)
  11611. {
  11612. pos = buf;
  11613. pos += xsnprintf (pos, endbuf - pos, "%s=0x%x", packet->name,
  11614. conf->pt.size);
  11615. putpkt (buf);
  11616. getpkt (&rs->buf, 0);
  11617. if (packet_ok (buf, packet) == PACKET_ERROR)
  11618. {
  11619. if (buf[0] == 'E' && buf[1] == '.')
  11620. error (_("Failed to configure the trace buffer size: %s"), buf + 2);
  11621. else
  11622. error (_("Failed to configure the trace buffer size."));
  11623. }
  11624. rs->btrace_config.pt.size = conf->pt.size;
  11625. }
  11626. }
  11627. /* Read TP's btrace configuration from the target and store it into CONF. */
  11628. static void
  11629. btrace_read_config (thread_info *tp, struct btrace_config *conf)
  11630. {
  11631. /* target_read_stralloc relies on INFERIOR_PTID. */
  11632. scoped_restore_current_thread restore_thread;
  11633. switch_to_thread (tp);
  11634. gdb::optional<gdb::char_vector> xml
  11635. = target_read_stralloc (current_inferior ()->top_target (),
  11636. TARGET_OBJECT_BTRACE_CONF, "");
  11637. if (xml)
  11638. parse_xml_btrace_conf (conf, xml->data ());
  11639. }
  11640. /* Maybe reopen target btrace. */
  11641. void
  11642. remote_target::remote_btrace_maybe_reopen ()
  11643. {
  11644. struct remote_state *rs = get_remote_state ();
  11645. int btrace_target_pushed = 0;
  11646. #if !defined (HAVE_LIBIPT)
  11647. int warned = 0;
  11648. #endif
  11649. /* Don't bother walking the entirety of the remote thread list when
  11650. we know the feature isn't supported by the remote. */
  11651. if (packet_support (PACKET_qXfer_btrace_conf) != PACKET_ENABLE)
  11652. return;
  11653. for (thread_info *tp : all_non_exited_threads (this))
  11654. {
  11655. memset (&rs->btrace_config, 0x00, sizeof (struct btrace_config));
  11656. btrace_read_config (tp, &rs->btrace_config);
  11657. if (rs->btrace_config.format == BTRACE_FORMAT_NONE)
  11658. continue;
  11659. #if !defined (HAVE_LIBIPT)
  11660. if (rs->btrace_config.format == BTRACE_FORMAT_PT)
  11661. {
  11662. if (!warned)
  11663. {
  11664. warned = 1;
  11665. warning (_("Target is recording using Intel Processor Trace "
  11666. "but support was disabled at compile time."));
  11667. }
  11668. continue;
  11669. }
  11670. #endif /* !defined (HAVE_LIBIPT) */
  11671. /* Push target, once, but before anything else happens. This way our
  11672. changes to the threads will be cleaned up by unpushing the target
  11673. in case btrace_read_config () throws. */
  11674. if (!btrace_target_pushed)
  11675. {
  11676. btrace_target_pushed = 1;
  11677. record_btrace_push_target ();
  11678. gdb_printf (_("Target is recording using %s.\n"),
  11679. btrace_format_string (rs->btrace_config.format));
  11680. }
  11681. tp->btrace.target = XCNEW (struct btrace_target_info);
  11682. tp->btrace.target->ptid = tp->ptid;
  11683. tp->btrace.target->conf = rs->btrace_config;
  11684. }
  11685. }
  11686. /* Enable branch tracing. */
  11687. struct btrace_target_info *
  11688. remote_target::enable_btrace (thread_info *tp,
  11689. const struct btrace_config *conf)
  11690. {
  11691. struct btrace_target_info *tinfo = NULL;
  11692. struct packet_config *packet = NULL;
  11693. struct remote_state *rs = get_remote_state ();
  11694. char *buf = rs->buf.data ();
  11695. char *endbuf = buf + get_remote_packet_size ();
  11696. switch (conf->format)
  11697. {
  11698. case BTRACE_FORMAT_BTS:
  11699. packet = &remote_protocol_packets[PACKET_Qbtrace_bts];
  11700. break;
  11701. case BTRACE_FORMAT_PT:
  11702. packet = &remote_protocol_packets[PACKET_Qbtrace_pt];
  11703. break;
  11704. }
  11705. if (packet == NULL || packet_config_support (packet) != PACKET_ENABLE)
  11706. error (_("Target does not support branch tracing."));
  11707. btrace_sync_conf (conf);
  11708. ptid_t ptid = tp->ptid;
  11709. set_general_thread (ptid);
  11710. buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
  11711. putpkt (rs->buf);
  11712. getpkt (&rs->buf, 0);
  11713. if (packet_ok (rs->buf, packet) == PACKET_ERROR)
  11714. {
  11715. if (rs->buf[0] == 'E' && rs->buf[1] == '.')
  11716. error (_("Could not enable branch tracing for %s: %s"),
  11717. target_pid_to_str (ptid).c_str (), &rs->buf[2]);
  11718. else
  11719. error (_("Could not enable branch tracing for %s."),
  11720. target_pid_to_str (ptid).c_str ());
  11721. }
  11722. tinfo = XCNEW (struct btrace_target_info);
  11723. tinfo->ptid = ptid;
  11724. /* If we fail to read the configuration, we lose some information, but the
  11725. tracing itself is not impacted. */
  11726. try
  11727. {
  11728. btrace_read_config (tp, &tinfo->conf);
  11729. }
  11730. catch (const gdb_exception_error &err)
  11731. {
  11732. if (err.message != NULL)
  11733. warning ("%s", err.what ());
  11734. }
  11735. return tinfo;
  11736. }
  11737. /* Disable branch tracing. */
  11738. void
  11739. remote_target::disable_btrace (struct btrace_target_info *tinfo)
  11740. {
  11741. struct packet_config *packet = &remote_protocol_packets[PACKET_Qbtrace_off];
  11742. struct remote_state *rs = get_remote_state ();
  11743. char *buf = rs->buf.data ();
  11744. char *endbuf = buf + get_remote_packet_size ();
  11745. if (packet_config_support (packet) != PACKET_ENABLE)
  11746. error (_("Target does not support branch tracing."));
  11747. set_general_thread (tinfo->ptid);
  11748. buf += xsnprintf (buf, endbuf - buf, "%s", packet->name);
  11749. putpkt (rs->buf);
  11750. getpkt (&rs->buf, 0);
  11751. if (packet_ok (rs->buf, packet) == PACKET_ERROR)
  11752. {
  11753. if (rs->buf[0] == 'E' && rs->buf[1] == '.')
  11754. error (_("Could not disable branch tracing for %s: %s"),
  11755. target_pid_to_str (tinfo->ptid).c_str (), &rs->buf[2]);
  11756. else
  11757. error (_("Could not disable branch tracing for %s."),
  11758. target_pid_to_str (tinfo->ptid).c_str ());
  11759. }
  11760. xfree (tinfo);
  11761. }
  11762. /* Teardown branch tracing. */
  11763. void
  11764. remote_target::teardown_btrace (struct btrace_target_info *tinfo)
  11765. {
  11766. /* We must not talk to the target during teardown. */
  11767. xfree (tinfo);
  11768. }
  11769. /* Read the branch trace. */
  11770. enum btrace_error
  11771. remote_target::read_btrace (struct btrace_data *btrace,
  11772. struct btrace_target_info *tinfo,
  11773. enum btrace_read_type type)
  11774. {
  11775. struct packet_config *packet = &remote_protocol_packets[PACKET_qXfer_btrace];
  11776. const char *annex;
  11777. if (packet_config_support (packet) != PACKET_ENABLE)
  11778. error (_("Target does not support branch tracing."));
  11779. #if !defined(HAVE_LIBEXPAT)
  11780. error (_("Cannot process branch tracing result. XML parsing not supported."));
  11781. #endif
  11782. switch (type)
  11783. {
  11784. case BTRACE_READ_ALL:
  11785. annex = "all";
  11786. break;
  11787. case BTRACE_READ_NEW:
  11788. annex = "new";
  11789. break;
  11790. case BTRACE_READ_DELTA:
  11791. annex = "delta";
  11792. break;
  11793. default:
  11794. internal_error (__FILE__, __LINE__,
  11795. _("Bad branch tracing read type: %u."),
  11796. (unsigned int) type);
  11797. }
  11798. gdb::optional<gdb::char_vector> xml
  11799. = target_read_stralloc (current_inferior ()->top_target (),
  11800. TARGET_OBJECT_BTRACE, annex);
  11801. if (!xml)
  11802. return BTRACE_ERR_UNKNOWN;
  11803. parse_xml_btrace (btrace, xml->data ());
  11804. return BTRACE_ERR_NONE;
  11805. }
  11806. const struct btrace_config *
  11807. remote_target::btrace_conf (const struct btrace_target_info *tinfo)
  11808. {
  11809. return &tinfo->conf;
  11810. }
  11811. bool
  11812. remote_target::augmented_libraries_svr4_read ()
  11813. {
  11814. return (packet_support (PACKET_augmented_libraries_svr4_read_feature)
  11815. == PACKET_ENABLE);
  11816. }
  11817. /* Implementation of to_load. */
  11818. void
  11819. remote_target::load (const char *name, int from_tty)
  11820. {
  11821. generic_load (name, from_tty);
  11822. }
  11823. /* Accepts an integer PID; returns a string representing a file that
  11824. can be opened on the remote side to get the symbols for the child
  11825. process. Returns NULL if the operation is not supported. */
  11826. char *
  11827. remote_target::pid_to_exec_file (int pid)
  11828. {
  11829. static gdb::optional<gdb::char_vector> filename;
  11830. char *annex = NULL;
  11831. if (packet_support (PACKET_qXfer_exec_file) != PACKET_ENABLE)
  11832. return NULL;
  11833. inferior *inf = find_inferior_pid (this, pid);
  11834. if (inf == NULL)
  11835. internal_error (__FILE__, __LINE__,
  11836. _("not currently attached to process %d"), pid);
  11837. if (!inf->fake_pid_p)
  11838. {
  11839. const int annex_size = 9;
  11840. annex = (char *) alloca (annex_size);
  11841. xsnprintf (annex, annex_size, "%x", pid);
  11842. }
  11843. filename = target_read_stralloc (current_inferior ()->top_target (),
  11844. TARGET_OBJECT_EXEC_FILE, annex);
  11845. return filename ? filename->data () : nullptr;
  11846. }
  11847. /* Implement the to_can_do_single_step target_ops method. */
  11848. int
  11849. remote_target::can_do_single_step ()
  11850. {
  11851. /* We can only tell whether target supports single step or not by
  11852. supported s and S vCont actions if the stub supports vContSupported
  11853. feature. If the stub doesn't support vContSupported feature,
  11854. we have conservatively to think target doesn't supports single
  11855. step. */
  11856. if (packet_support (PACKET_vContSupported) == PACKET_ENABLE)
  11857. {
  11858. struct remote_state *rs = get_remote_state ();
  11859. if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
  11860. remote_vcont_probe ();
  11861. return rs->supports_vCont.s && rs->supports_vCont.S;
  11862. }
  11863. else
  11864. return 0;
  11865. }
  11866. /* Implementation of the to_execution_direction method for the remote
  11867. target. */
  11868. enum exec_direction_kind
  11869. remote_target::execution_direction ()
  11870. {
  11871. struct remote_state *rs = get_remote_state ();
  11872. return rs->last_resume_exec_dir;
  11873. }
  11874. /* Return pointer to the thread_info struct which corresponds to
  11875. THREAD_HANDLE (having length HANDLE_LEN). */
  11876. thread_info *
  11877. remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle,
  11878. int handle_len,
  11879. inferior *inf)
  11880. {
  11881. for (thread_info *tp : all_non_exited_threads (this))
  11882. {
  11883. remote_thread_info *priv = get_remote_thread_info (tp);
  11884. if (tp->inf == inf && priv != NULL)
  11885. {
  11886. if (handle_len != priv->thread_handle.size ())
  11887. error (_("Thread handle size mismatch: %d vs %zu (from remote)"),
  11888. handle_len, priv->thread_handle.size ());
  11889. if (memcmp (thread_handle, priv->thread_handle.data (),
  11890. handle_len) == 0)
  11891. return tp;
  11892. }
  11893. }
  11894. return NULL;
  11895. }
  11896. gdb::byte_vector
  11897. remote_target::thread_info_to_thread_handle (struct thread_info *tp)
  11898. {
  11899. remote_thread_info *priv = get_remote_thread_info (tp);
  11900. return priv->thread_handle;
  11901. }
  11902. bool
  11903. remote_target::can_async_p ()
  11904. {
  11905. /* This flag should be checked in the common target.c code. */
  11906. gdb_assert (target_async_permitted);
  11907. /* We're async whenever the serial device can. */
  11908. struct remote_state *rs = get_remote_state ();
  11909. return serial_can_async_p (rs->remote_desc);
  11910. }
  11911. bool
  11912. remote_target::is_async_p ()
  11913. {
  11914. /* We're async whenever the serial device is. */
  11915. struct remote_state *rs = get_remote_state ();
  11916. return serial_is_async_p (rs->remote_desc);
  11917. }
  11918. /* Pass the SERIAL event on and up to the client. One day this code
  11919. will be able to delay notifying the client of an event until the
  11920. point where an entire packet has been received. */
  11921. static serial_event_ftype remote_async_serial_handler;
  11922. static void
  11923. remote_async_serial_handler (struct serial *scb, void *context)
  11924. {
  11925. /* Don't propogate error information up to the client. Instead let
  11926. the client find out about the error by querying the target. */
  11927. inferior_event_handler (INF_REG_EVENT);
  11928. }
  11929. static void
  11930. remote_async_inferior_event_handler (gdb_client_data data)
  11931. {
  11932. inferior_event_handler (INF_REG_EVENT);
  11933. }
  11934. int
  11935. remote_target::async_wait_fd ()
  11936. {
  11937. struct remote_state *rs = get_remote_state ();
  11938. return rs->remote_desc->fd;
  11939. }
  11940. void
  11941. remote_target::async (int enable)
  11942. {
  11943. struct remote_state *rs = get_remote_state ();
  11944. if (enable)
  11945. {
  11946. serial_async (rs->remote_desc, remote_async_serial_handler, rs);
  11947. /* If there are pending events in the stop reply queue tell the
  11948. event loop to process them. */
  11949. if (!rs->stop_reply_queue.empty ())
  11950. mark_async_event_handler (rs->remote_async_inferior_event_token);
  11951. /* For simplicity, below we clear the pending events token
  11952. without remembering whether it is marked, so here we always
  11953. mark it. If there's actually no pending notification to
  11954. process, this ends up being a no-op (other than a spurious
  11955. event-loop wakeup). */
  11956. if (target_is_non_stop_p ())
  11957. mark_async_event_handler (rs->notif_state->get_pending_events_token);
  11958. }
  11959. else
  11960. {
  11961. serial_async (rs->remote_desc, NULL, NULL);
  11962. /* If the core is disabling async, it doesn't want to be
  11963. disturbed with target events. Clear all async event sources
  11964. too. */
  11965. clear_async_event_handler (rs->remote_async_inferior_event_token);
  11966. if (target_is_non_stop_p ())
  11967. clear_async_event_handler (rs->notif_state->get_pending_events_token);
  11968. }
  11969. }
  11970. /* Implementation of the to_thread_events method. */
  11971. void
  11972. remote_target::thread_events (int enable)
  11973. {
  11974. struct remote_state *rs = get_remote_state ();
  11975. size_t size = get_remote_packet_size ();
  11976. if (packet_support (PACKET_QThreadEvents) == PACKET_DISABLE)
  11977. return;
  11978. xsnprintf (rs->buf.data (), size, "QThreadEvents:%x", enable ? 1 : 0);
  11979. putpkt (rs->buf);
  11980. getpkt (&rs->buf, 0);
  11981. switch (packet_ok (rs->buf,
  11982. &remote_protocol_packets[PACKET_QThreadEvents]))
  11983. {
  11984. case PACKET_OK:
  11985. if (strcmp (rs->buf.data (), "OK") != 0)
  11986. error (_("Remote refused setting thread events: %s"), rs->buf.data ());
  11987. break;
  11988. case PACKET_ERROR:
  11989. warning (_("Remote failure reply: %s"), rs->buf.data ());
  11990. break;
  11991. case PACKET_UNKNOWN:
  11992. break;
  11993. }
  11994. }
  11995. static void
  11996. show_remote_cmd (const char *args, int from_tty)
  11997. {
  11998. /* We can't just use cmd_show_list here, because we want to skip
  11999. the redundant "show remote Z-packet" and the legacy aliases. */
  12000. struct cmd_list_element *list = remote_show_cmdlist;
  12001. struct ui_out *uiout = current_uiout;
  12002. ui_out_emit_tuple tuple_emitter (uiout, "showlist");
  12003. for (; list != NULL; list = list->next)
  12004. if (strcmp (list->name, "Z-packet") == 0)
  12005. continue;
  12006. else if (list->type == not_set_cmd)
  12007. /* Alias commands are exactly like the original, except they
  12008. don't have the normal type. */
  12009. continue;
  12010. else
  12011. {
  12012. ui_out_emit_tuple option_emitter (uiout, "option");
  12013. uiout->field_string ("name", list->name);
  12014. uiout->text (": ");
  12015. if (list->type == show_cmd)
  12016. do_show_command (NULL, from_tty, list);
  12017. else
  12018. cmd_func (list, NULL, from_tty);
  12019. }
  12020. }
  12021. /* Function to be called whenever a new objfile (shlib) is detected. */
  12022. static void
  12023. remote_new_objfile (struct objfile *objfile)
  12024. {
  12025. remote_target *remote = get_current_remote_target ();
  12026. /* First, check whether the current inferior's process target is a remote
  12027. target. */
  12028. if (remote == nullptr)
  12029. return;
  12030. /* When we are attaching or handling a fork child and the shared library
  12031. subsystem reads the list of loaded libraries, we receive new objfile
  12032. events in between each found library. The libraries are read in an
  12033. undefined order, so if we gave the remote side a chance to look up
  12034. symbols between each objfile, we might give it an inconsistent picture
  12035. of the inferior. It could appear that a library A appears loaded but
  12036. a library B does not, even though library A requires library B. That
  12037. would present a state that couldn't normally exist in the inferior.
  12038. So, skip these events, we'll give the remote a chance to look up symbols
  12039. once all the loaded libraries and their symbols are known to GDB. */
  12040. if (current_inferior ()->in_initial_library_scan)
  12041. return;
  12042. remote->remote_check_symbols ();
  12043. }
  12044. /* Pull all the tracepoints defined on the target and create local
  12045. data structures representing them. We don't want to create real
  12046. tracepoints yet, we don't want to mess up the user's existing
  12047. collection. */
  12048. int
  12049. remote_target::upload_tracepoints (struct uploaded_tp **utpp)
  12050. {
  12051. struct remote_state *rs = get_remote_state ();
  12052. char *p;
  12053. /* Ask for a first packet of tracepoint definition. */
  12054. putpkt ("qTfP");
  12055. getpkt (&rs->buf, 0);
  12056. p = rs->buf.data ();
  12057. while (*p && *p != 'l')
  12058. {
  12059. parse_tracepoint_definition (p, utpp);
  12060. /* Ask for another packet of tracepoint definition. */
  12061. putpkt ("qTsP");
  12062. getpkt (&rs->buf, 0);
  12063. p = rs->buf.data ();
  12064. }
  12065. return 0;
  12066. }
  12067. int
  12068. remote_target::upload_trace_state_variables (struct uploaded_tsv **utsvp)
  12069. {
  12070. struct remote_state *rs = get_remote_state ();
  12071. char *p;
  12072. /* Ask for a first packet of variable definition. */
  12073. putpkt ("qTfV");
  12074. getpkt (&rs->buf, 0);
  12075. p = rs->buf.data ();
  12076. while (*p && *p != 'l')
  12077. {
  12078. parse_tsv_definition (p, utsvp);
  12079. /* Ask for another packet of variable definition. */
  12080. putpkt ("qTsV");
  12081. getpkt (&rs->buf, 0);
  12082. p = rs->buf.data ();
  12083. }
  12084. return 0;
  12085. }
  12086. /* The "set/show range-stepping" show hook. */
  12087. static void
  12088. show_range_stepping (struct ui_file *file, int from_tty,
  12089. struct cmd_list_element *c,
  12090. const char *value)
  12091. {
  12092. gdb_printf (file,
  12093. _("Debugger's willingness to use range stepping "
  12094. "is %s.\n"), value);
  12095. }
  12096. /* Return true if the vCont;r action is supported by the remote
  12097. stub. */
  12098. bool
  12099. remote_target::vcont_r_supported ()
  12100. {
  12101. if (packet_support (PACKET_vCont) == PACKET_SUPPORT_UNKNOWN)
  12102. remote_vcont_probe ();
  12103. return (packet_support (PACKET_vCont) == PACKET_ENABLE
  12104. && get_remote_state ()->supports_vCont.r);
  12105. }
  12106. /* The "set/show range-stepping" set hook. */
  12107. static void
  12108. set_range_stepping (const char *ignore_args, int from_tty,
  12109. struct cmd_list_element *c)
  12110. {
  12111. /* When enabling, check whether range stepping is actually supported
  12112. by the target, and warn if not. */
  12113. if (use_range_stepping)
  12114. {
  12115. remote_target *remote = get_current_remote_target ();
  12116. if (remote == NULL
  12117. || !remote->vcont_r_supported ())
  12118. warning (_("Range stepping is not supported by the current target"));
  12119. }
  12120. }
  12121. static void
  12122. show_remote_debug (struct ui_file *file, int from_tty,
  12123. struct cmd_list_element *c, const char *value)
  12124. {
  12125. gdb_printf (file, _("Debugging of remote protocol is %s.\n"),
  12126. value);
  12127. }
  12128. static void
  12129. show_remote_timeout (struct ui_file *file, int from_tty,
  12130. struct cmd_list_element *c, const char *value)
  12131. {
  12132. gdb_printf (file,
  12133. _("Timeout limit to wait for target to respond is %s.\n"),
  12134. value);
  12135. }
  12136. /* Implement the "supports_memory_tagging" target_ops method. */
  12137. bool
  12138. remote_target::supports_memory_tagging ()
  12139. {
  12140. return remote_memory_tagging_p ();
  12141. }
  12142. /* Create the qMemTags packet given ADDRESS, LEN and TYPE. */
  12143. static void
  12144. create_fetch_memtags_request (gdb::char_vector &packet, CORE_ADDR address,
  12145. size_t len, int type)
  12146. {
  12147. int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
  12148. std::string request = string_printf ("qMemTags:%s,%s:%s",
  12149. phex_nz (address, addr_size),
  12150. phex_nz (len, sizeof (len)),
  12151. phex_nz (type, sizeof (type)));
  12152. strcpy (packet.data (), request.c_str ());
  12153. }
  12154. /* Parse the qMemTags packet reply into TAGS.
  12155. Return true if successful, false otherwise. */
  12156. static bool
  12157. parse_fetch_memtags_reply (const gdb::char_vector &reply,
  12158. gdb::byte_vector &tags)
  12159. {
  12160. if (reply.empty () || reply[0] == 'E' || reply[0] != 'm')
  12161. return false;
  12162. /* Copy the tag data. */
  12163. tags = hex2bin (reply.data () + 1);
  12164. return true;
  12165. }
  12166. /* Create the QMemTags packet given ADDRESS, LEN, TYPE and TAGS. */
  12167. static void
  12168. create_store_memtags_request (gdb::char_vector &packet, CORE_ADDR address,
  12169. size_t len, int type,
  12170. const gdb::byte_vector &tags)
  12171. {
  12172. int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
  12173. /* Put together the main packet, address and length. */
  12174. std::string request = string_printf ("QMemTags:%s,%s:%s:",
  12175. phex_nz (address, addr_size),
  12176. phex_nz (len, sizeof (len)),
  12177. phex_nz (type, sizeof (type)));
  12178. request += bin2hex (tags.data (), tags.size ());
  12179. /* Check if we have exceeded the maximum packet size. */
  12180. if (packet.size () < request.length ())
  12181. error (_("Contents too big for packet QMemTags."));
  12182. strcpy (packet.data (), request.c_str ());
  12183. }
  12184. /* Implement the "fetch_memtags" target_ops method. */
  12185. bool
  12186. remote_target::fetch_memtags (CORE_ADDR address, size_t len,
  12187. gdb::byte_vector &tags, int type)
  12188. {
  12189. /* Make sure the qMemTags packet is supported. */
  12190. if (!remote_memory_tagging_p ())
  12191. gdb_assert_not_reached ("remote fetch_memtags called with packet disabled");
  12192. struct remote_state *rs = get_remote_state ();
  12193. create_fetch_memtags_request (rs->buf, address, len, type);
  12194. putpkt (rs->buf);
  12195. getpkt (&rs->buf, 0);
  12196. return parse_fetch_memtags_reply (rs->buf, tags);
  12197. }
  12198. /* Implement the "store_memtags" target_ops method. */
  12199. bool
  12200. remote_target::store_memtags (CORE_ADDR address, size_t len,
  12201. const gdb::byte_vector &tags, int type)
  12202. {
  12203. /* Make sure the QMemTags packet is supported. */
  12204. if (!remote_memory_tagging_p ())
  12205. gdb_assert_not_reached ("remote store_memtags called with packet disabled");
  12206. struct remote_state *rs = get_remote_state ();
  12207. create_store_memtags_request (rs->buf, address, len, type, tags);
  12208. putpkt (rs->buf);
  12209. getpkt (&rs->buf, 0);
  12210. /* Verify if the request was successful. */
  12211. return packet_check_result (rs->buf.data ()) == PACKET_OK;
  12212. }
  12213. /* Return true if remote target T is non-stop. */
  12214. bool
  12215. remote_target_is_non_stop_p (remote_target *t)
  12216. {
  12217. scoped_restore_current_thread restore_thread;
  12218. switch_to_target_no_thread (t);
  12219. return target_is_non_stop_p ();
  12220. }
  12221. #if GDB_SELF_TEST
  12222. namespace selftests {
  12223. static void
  12224. test_memory_tagging_functions ()
  12225. {
  12226. remote_target remote;
  12227. struct packet_config *config
  12228. = &remote_protocol_packets[PACKET_memory_tagging_feature];
  12229. scoped_restore restore_memtag_support_
  12230. = make_scoped_restore (&config->support);
  12231. /* Test memory tagging packet support. */
  12232. config->support = PACKET_SUPPORT_UNKNOWN;
  12233. SELF_CHECK (remote.supports_memory_tagging () == false);
  12234. config->support = PACKET_DISABLE;
  12235. SELF_CHECK (remote.supports_memory_tagging () == false);
  12236. config->support = PACKET_ENABLE;
  12237. SELF_CHECK (remote.supports_memory_tagging () == true);
  12238. /* Setup testing. */
  12239. gdb::char_vector packet;
  12240. gdb::byte_vector tags, bv;
  12241. std::string expected, reply;
  12242. packet.resize (32000);
  12243. /* Test creating a qMemTags request. */
  12244. expected = "qMemTags:0,0:0";
  12245. create_fetch_memtags_request (packet, 0x0, 0x0, 0);
  12246. SELF_CHECK (strcmp (packet.data (), expected.c_str ()) == 0);
  12247. expected = "qMemTags:deadbeef,10:1";
  12248. create_fetch_memtags_request (packet, 0xdeadbeef, 16, 1);
  12249. SELF_CHECK (strcmp (packet.data (), expected.c_str ()) == 0);
  12250. /* Test parsing a qMemTags reply. */
  12251. /* Error reply, tags vector unmodified. */
  12252. reply = "E00";
  12253. strcpy (packet.data (), reply.c_str ());
  12254. tags.resize (0);
  12255. SELF_CHECK (parse_fetch_memtags_reply (packet, tags) == false);
  12256. SELF_CHECK (tags.size () == 0);
  12257. /* Valid reply, tags vector updated. */
  12258. tags.resize (0);
  12259. bv.resize (0);
  12260. for (int i = 0; i < 5; i++)
  12261. bv.push_back (i);
  12262. reply = "m" + bin2hex (bv.data (), bv.size ());
  12263. strcpy (packet.data (), reply.c_str ());
  12264. SELF_CHECK (parse_fetch_memtags_reply (packet, tags) == true);
  12265. SELF_CHECK (tags.size () == 5);
  12266. for (int i = 0; i < 5; i++)
  12267. SELF_CHECK (tags[i] == i);
  12268. /* Test creating a QMemTags request. */
  12269. /* Empty tag data. */
  12270. tags.resize (0);
  12271. expected = "QMemTags:0,0:0:";
  12272. create_store_memtags_request (packet, 0x0, 0x0, 0, tags);
  12273. SELF_CHECK (memcmp (packet.data (), expected.c_str (),
  12274. expected.length ()) == 0);
  12275. /* Non-empty tag data. */
  12276. tags.resize (0);
  12277. for (int i = 0; i < 5; i++)
  12278. tags.push_back (i);
  12279. expected = "QMemTags:deadbeef,ff:1:0001020304";
  12280. create_store_memtags_request (packet, 0xdeadbeef, 255, 1, tags);
  12281. SELF_CHECK (memcmp (packet.data (), expected.c_str (),
  12282. expected.length ()) == 0);
  12283. }
  12284. } // namespace selftests
  12285. #endif /* GDB_SELF_TEST */
  12286. void _initialize_remote ();
  12287. void
  12288. _initialize_remote ()
  12289. {
  12290. /* architecture specific data */
  12291. remote_g_packet_data_handle =
  12292. gdbarch_data_register_pre_init (remote_g_packet_data_init);
  12293. add_target (remote_target_info, remote_target::open);
  12294. add_target (extended_remote_target_info, extended_remote_target::open);
  12295. /* Hook into new objfile notification. */
  12296. gdb::observers::new_objfile.attach (remote_new_objfile, "remote");
  12297. #if 0
  12298. init_remote_threadtests ();
  12299. #endif
  12300. /* set/show remote ... */
  12301. add_basic_prefix_cmd ("remote", class_maintenance, _("\
  12302. Remote protocol specific variables.\n\
  12303. Configure various remote-protocol specific variables such as\n\
  12304. the packets being used."),
  12305. &remote_set_cmdlist,
  12306. 0 /* allow-unknown */, &setlist);
  12307. add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, _("\
  12308. Remote protocol specific variables.\n\
  12309. Configure various remote-protocol specific variables such as\n\
  12310. the packets being used."),
  12311. &remote_show_cmdlist,
  12312. 0 /* allow-unknown */, &showlist);
  12313. add_cmd ("compare-sections", class_obscure, compare_sections_command, _("\
  12314. Compare section data on target to the exec file.\n\
  12315. Argument is a single section name (default: all loaded sections).\n\
  12316. To compare only read-only loaded sections, specify the -r option."),
  12317. &cmdlist);
  12318. add_cmd ("packet", class_maintenance, cli_packet_command, _("\
  12319. Send an arbitrary packet to a remote target.\n\
  12320. maintenance packet TEXT\n\
  12321. If GDB is talking to an inferior via the GDB serial protocol, then\n\
  12322. this command sends the string TEXT to the inferior, and displays the\n\
  12323. response packet. GDB supplies the initial `$' character, and the\n\
  12324. terminating `#' character and checksum."),
  12325. &maintenancelist);
  12326. set_show_commands remotebreak_cmds
  12327. = add_setshow_boolean_cmd ("remotebreak", no_class, &remote_break, _("\
  12328. Set whether to send break if interrupted."), _("\
  12329. Show whether to send break if interrupted."), _("\
  12330. If set, a break, instead of a cntrl-c, is sent to the remote target."),
  12331. set_remotebreak, show_remotebreak,
  12332. &setlist, &showlist);
  12333. deprecate_cmd (remotebreak_cmds.set, "set remote interrupt-sequence");
  12334. deprecate_cmd (remotebreak_cmds.show, "show remote interrupt-sequence");
  12335. add_setshow_enum_cmd ("interrupt-sequence", class_support,
  12336. interrupt_sequence_modes, &interrupt_sequence_mode,
  12337. _("\
  12338. Set interrupt sequence to remote target."), _("\
  12339. Show interrupt sequence to remote target."), _("\
  12340. Valid value is \"Ctrl-C\", \"BREAK\" or \"BREAK-g\". The default is \"Ctrl-C\"."),
  12341. NULL, show_interrupt_sequence,
  12342. &remote_set_cmdlist,
  12343. &remote_show_cmdlist);
  12344. add_setshow_boolean_cmd ("interrupt-on-connect", class_support,
  12345. &interrupt_on_connect, _("\
  12346. Set whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
  12347. Show whether interrupt-sequence is sent to remote target when gdb connects to."), _("\
  12348. If set, interrupt sequence is sent to remote target."),
  12349. NULL, NULL,
  12350. &remote_set_cmdlist, &remote_show_cmdlist);
  12351. /* Install commands for configuring memory read/write packets. */
  12352. add_cmd ("remotewritesize", no_class, set_memory_write_packet_size, _("\
  12353. Set the maximum number of bytes per memory write packet (deprecated)."),
  12354. &setlist);
  12355. add_cmd ("remotewritesize", no_class, show_memory_write_packet_size, _("\
  12356. Show the maximum number of bytes per memory write packet (deprecated)."),
  12357. &showlist);
  12358. add_cmd ("memory-write-packet-size", no_class,
  12359. set_memory_write_packet_size, _("\
  12360. Set the maximum number of bytes per memory-write packet.\n\
  12361. Specify the number of bytes in a packet or 0 (zero) for the\n\
  12362. default packet size. The actual limit is further reduced\n\
  12363. dependent on the target. Specify ``fixed'' to disable the\n\
  12364. further restriction and ``limit'' to enable that restriction."),
  12365. &remote_set_cmdlist);
  12366. add_cmd ("memory-read-packet-size", no_class,
  12367. set_memory_read_packet_size, _("\
  12368. Set the maximum number of bytes per memory-read packet.\n\
  12369. Specify the number of bytes in a packet or 0 (zero) for the\n\
  12370. default packet size. The actual limit is further reduced\n\
  12371. dependent on the target. Specify ``fixed'' to disable the\n\
  12372. further restriction and ``limit'' to enable that restriction."),
  12373. &remote_set_cmdlist);
  12374. add_cmd ("memory-write-packet-size", no_class,
  12375. show_memory_write_packet_size,
  12376. _("Show the maximum number of bytes per memory-write packet."),
  12377. &remote_show_cmdlist);
  12378. add_cmd ("memory-read-packet-size", no_class,
  12379. show_memory_read_packet_size,
  12380. _("Show the maximum number of bytes per memory-read packet."),
  12381. &remote_show_cmdlist);
  12382. add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-limit", no_class,
  12383. &remote_hw_watchpoint_limit, _("\
  12384. Set the maximum number of target hardware watchpoints."), _("\
  12385. Show the maximum number of target hardware watchpoints."), _("\
  12386. Specify \"unlimited\" for unlimited hardware watchpoints."),
  12387. NULL, show_hardware_watchpoint_limit,
  12388. &remote_set_cmdlist,
  12389. &remote_show_cmdlist);
  12390. add_setshow_zuinteger_unlimited_cmd ("hardware-watchpoint-length-limit",
  12391. no_class,
  12392. &remote_hw_watchpoint_length_limit, _("\
  12393. Set the maximum length (in bytes) of a target hardware watchpoint."), _("\
  12394. Show the maximum length (in bytes) of a target hardware watchpoint."), _("\
  12395. Specify \"unlimited\" to allow watchpoints of unlimited size."),
  12396. NULL, show_hardware_watchpoint_length_limit,
  12397. &remote_set_cmdlist, &remote_show_cmdlist);
  12398. add_setshow_zuinteger_unlimited_cmd ("hardware-breakpoint-limit", no_class,
  12399. &remote_hw_breakpoint_limit, _("\
  12400. Set the maximum number of target hardware breakpoints."), _("\
  12401. Show the maximum number of target hardware breakpoints."), _("\
  12402. Specify \"unlimited\" for unlimited hardware breakpoints."),
  12403. NULL, show_hardware_breakpoint_limit,
  12404. &remote_set_cmdlist, &remote_show_cmdlist);
  12405. add_setshow_zuinteger_cmd ("remoteaddresssize", class_obscure,
  12406. &remote_address_size, _("\
  12407. Set the maximum size of the address (in bits) in a memory packet."), _("\
  12408. Show the maximum size of the address (in bits) in a memory packet."), NULL,
  12409. NULL,
  12410. NULL, /* FIXME: i18n: */
  12411. &setlist, &showlist);
  12412. init_all_packet_configs ();
  12413. add_packet_config_cmd (&remote_protocol_packets[PACKET_X],
  12414. "X", "binary-download", 1);
  12415. add_packet_config_cmd (&remote_protocol_packets[PACKET_vCont],
  12416. "vCont", "verbose-resume", 0);
  12417. add_packet_config_cmd (&remote_protocol_packets[PACKET_QPassSignals],
  12418. "QPassSignals", "pass-signals", 0);
  12419. add_packet_config_cmd (&remote_protocol_packets[PACKET_QCatchSyscalls],
  12420. "QCatchSyscalls", "catch-syscalls", 0);
  12421. add_packet_config_cmd (&remote_protocol_packets[PACKET_QProgramSignals],
  12422. "QProgramSignals", "program-signals", 0);
  12423. add_packet_config_cmd (&remote_protocol_packets[PACKET_QSetWorkingDir],
  12424. "QSetWorkingDir", "set-working-dir", 0);
  12425. add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartupWithShell],
  12426. "QStartupWithShell", "startup-with-shell", 0);
  12427. add_packet_config_cmd (&remote_protocol_packets
  12428. [PACKET_QEnvironmentHexEncoded],
  12429. "QEnvironmentHexEncoded", "environment-hex-encoded",
  12430. 0);
  12431. add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentReset],
  12432. "QEnvironmentReset", "environment-reset",
  12433. 0);
  12434. add_packet_config_cmd (&remote_protocol_packets[PACKET_QEnvironmentUnset],
  12435. "QEnvironmentUnset", "environment-unset",
  12436. 0);
  12437. add_packet_config_cmd (&remote_protocol_packets[PACKET_qSymbol],
  12438. "qSymbol", "symbol-lookup", 0);
  12439. add_packet_config_cmd (&remote_protocol_packets[PACKET_P],
  12440. "P", "set-register", 1);
  12441. add_packet_config_cmd (&remote_protocol_packets[PACKET_p],
  12442. "p", "fetch-register", 1);
  12443. add_packet_config_cmd (&remote_protocol_packets[PACKET_Z0],
  12444. "Z0", "software-breakpoint", 0);
  12445. add_packet_config_cmd (&remote_protocol_packets[PACKET_Z1],
  12446. "Z1", "hardware-breakpoint", 0);
  12447. add_packet_config_cmd (&remote_protocol_packets[PACKET_Z2],
  12448. "Z2", "write-watchpoint", 0);
  12449. add_packet_config_cmd (&remote_protocol_packets[PACKET_Z3],
  12450. "Z3", "read-watchpoint", 0);
  12451. add_packet_config_cmd (&remote_protocol_packets[PACKET_Z4],
  12452. "Z4", "access-watchpoint", 0);
  12453. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_auxv],
  12454. "qXfer:auxv:read", "read-aux-vector", 0);
  12455. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_exec_file],
  12456. "qXfer:exec-file:read", "pid-to-exec-file", 0);
  12457. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_features],
  12458. "qXfer:features:read", "target-features", 0);
  12459. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries],
  12460. "qXfer:libraries:read", "library-info", 0);
  12461. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_libraries_svr4],
  12462. "qXfer:libraries-svr4:read", "library-info-svr4", 0);
  12463. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_memory_map],
  12464. "qXfer:memory-map:read", "memory-map", 0);
  12465. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_osdata],
  12466. "qXfer:osdata:read", "osdata", 0);
  12467. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_threads],
  12468. "qXfer:threads:read", "threads", 0);
  12469. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_read],
  12470. "qXfer:siginfo:read", "read-siginfo-object", 0);
  12471. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_siginfo_write],
  12472. "qXfer:siginfo:write", "write-siginfo-object", 0);
  12473. add_packet_config_cmd
  12474. (&remote_protocol_packets[PACKET_qXfer_traceframe_info],
  12475. "qXfer:traceframe-info:read", "traceframe-info", 0);
  12476. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_uib],
  12477. "qXfer:uib:read", "unwind-info-block", 0);
  12478. add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTLSAddr],
  12479. "qGetTLSAddr", "get-thread-local-storage-address",
  12480. 0);
  12481. add_packet_config_cmd (&remote_protocol_packets[PACKET_qGetTIBAddr],
  12482. "qGetTIBAddr", "get-thread-information-block-address",
  12483. 0);
  12484. add_packet_config_cmd (&remote_protocol_packets[PACKET_bc],
  12485. "bc", "reverse-continue", 0);
  12486. add_packet_config_cmd (&remote_protocol_packets[PACKET_bs],
  12487. "bs", "reverse-step", 0);
  12488. add_packet_config_cmd (&remote_protocol_packets[PACKET_qSupported],
  12489. "qSupported", "supported-packets", 0);
  12490. add_packet_config_cmd (&remote_protocol_packets[PACKET_qSearch_memory],
  12491. "qSearch:memory", "search-memory", 0);
  12492. add_packet_config_cmd (&remote_protocol_packets[PACKET_qTStatus],
  12493. "qTStatus", "trace-status", 0);
  12494. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_setfs],
  12495. "vFile:setfs", "hostio-setfs", 0);
  12496. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_open],
  12497. "vFile:open", "hostio-open", 0);
  12498. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pread],
  12499. "vFile:pread", "hostio-pread", 0);
  12500. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_pwrite],
  12501. "vFile:pwrite", "hostio-pwrite", 0);
  12502. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_close],
  12503. "vFile:close", "hostio-close", 0);
  12504. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_unlink],
  12505. "vFile:unlink", "hostio-unlink", 0);
  12506. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
  12507. "vFile:readlink", "hostio-readlink", 0);
  12508. add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
  12509. "vFile:fstat", "hostio-fstat", 0);
  12510. add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
  12511. "vAttach", "attach", 0);
  12512. add_packet_config_cmd (&remote_protocol_packets[PACKET_vRun],
  12513. "vRun", "run", 0);
  12514. add_packet_config_cmd (&remote_protocol_packets[PACKET_QStartNoAckMode],
  12515. "QStartNoAckMode", "noack", 0);
  12516. add_packet_config_cmd (&remote_protocol_packets[PACKET_vKill],
  12517. "vKill", "kill", 0);
  12518. add_packet_config_cmd (&remote_protocol_packets[PACKET_qAttached],
  12519. "qAttached", "query-attached", 0);
  12520. add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalTracepoints],
  12521. "ConditionalTracepoints",
  12522. "conditional-tracepoints", 0);
  12523. add_packet_config_cmd (&remote_protocol_packets[PACKET_ConditionalBreakpoints],
  12524. "ConditionalBreakpoints",
  12525. "conditional-breakpoints", 0);
  12526. add_packet_config_cmd (&remote_protocol_packets[PACKET_BreakpointCommands],
  12527. "BreakpointCommands",
  12528. "breakpoint-commands", 0);
  12529. add_packet_config_cmd (&remote_protocol_packets[PACKET_FastTracepoints],
  12530. "FastTracepoints", "fast-tracepoints", 0);
  12531. add_packet_config_cmd (&remote_protocol_packets[PACKET_TracepointSource],
  12532. "TracepointSource", "TracepointSource", 0);
  12533. add_packet_config_cmd (&remote_protocol_packets[PACKET_QAllow],
  12534. "QAllow", "allow", 0);
  12535. add_packet_config_cmd (&remote_protocol_packets[PACKET_StaticTracepoints],
  12536. "StaticTracepoints", "static-tracepoints", 0);
  12537. add_packet_config_cmd (&remote_protocol_packets[PACKET_InstallInTrace],
  12538. "InstallInTrace", "install-in-trace", 0);
  12539. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_statictrace_read],
  12540. "qXfer:statictrace:read", "read-sdata-object", 0);
  12541. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_fdpic],
  12542. "qXfer:fdpic:read", "read-fdpic-loadmap", 0);
  12543. add_packet_config_cmd (&remote_protocol_packets[PACKET_QDisableRandomization],
  12544. "QDisableRandomization", "disable-randomization", 0);
  12545. add_packet_config_cmd (&remote_protocol_packets[PACKET_QAgent],
  12546. "QAgent", "agent", 0);
  12547. add_packet_config_cmd (&remote_protocol_packets[PACKET_QTBuffer_size],
  12548. "QTBuffer:size", "trace-buffer-size", 0);
  12549. add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_off],
  12550. "Qbtrace:off", "disable-btrace", 0);
  12551. add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_bts],
  12552. "Qbtrace:bts", "enable-btrace-bts", 0);
  12553. add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_pt],
  12554. "Qbtrace:pt", "enable-btrace-pt", 0);
  12555. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace],
  12556. "qXfer:btrace", "read-btrace", 0);
  12557. add_packet_config_cmd (&remote_protocol_packets[PACKET_qXfer_btrace_conf],
  12558. "qXfer:btrace-conf", "read-btrace-conf", 0);
  12559. add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_bts_size],
  12560. "Qbtrace-conf:bts:size", "btrace-conf-bts-size", 0);
  12561. add_packet_config_cmd (&remote_protocol_packets[PACKET_multiprocess_feature],
  12562. "multiprocess-feature", "multiprocess-feature", 0);
  12563. add_packet_config_cmd (&remote_protocol_packets[PACKET_swbreak_feature],
  12564. "swbreak-feature", "swbreak-feature", 0);
  12565. add_packet_config_cmd (&remote_protocol_packets[PACKET_hwbreak_feature],
  12566. "hwbreak-feature", "hwbreak-feature", 0);
  12567. add_packet_config_cmd (&remote_protocol_packets[PACKET_fork_event_feature],
  12568. "fork-event-feature", "fork-event-feature", 0);
  12569. add_packet_config_cmd (&remote_protocol_packets[PACKET_vfork_event_feature],
  12570. "vfork-event-feature", "vfork-event-feature", 0);
  12571. add_packet_config_cmd (&remote_protocol_packets[PACKET_Qbtrace_conf_pt_size],
  12572. "Qbtrace-conf:pt:size", "btrace-conf-pt-size", 0);
  12573. add_packet_config_cmd (&remote_protocol_packets[PACKET_vContSupported],
  12574. "vContSupported", "verbose-resume-supported", 0);
  12575. add_packet_config_cmd (&remote_protocol_packets[PACKET_exec_event_feature],
  12576. "exec-event-feature", "exec-event-feature", 0);
  12577. add_packet_config_cmd (&remote_protocol_packets[PACKET_vCtrlC],
  12578. "vCtrlC", "ctrl-c", 0);
  12579. add_packet_config_cmd (&remote_protocol_packets[PACKET_QThreadEvents],
  12580. "QThreadEvents", "thread-events", 0);
  12581. add_packet_config_cmd (&remote_protocol_packets[PACKET_no_resumed],
  12582. "N stop reply", "no-resumed-stop-reply", 0);
  12583. add_packet_config_cmd (&remote_protocol_packets[PACKET_memory_tagging_feature],
  12584. "memory-tagging-feature", "memory-tagging-feature", 0);
  12585. /* Assert that we've registered "set remote foo-packet" commands
  12586. for all packet configs. */
  12587. {
  12588. int i;
  12589. for (i = 0; i < PACKET_MAX; i++)
  12590. {
  12591. /* Ideally all configs would have a command associated. Some
  12592. still don't though. */
  12593. int excepted;
  12594. switch (i)
  12595. {
  12596. case PACKET_QNonStop:
  12597. case PACKET_EnableDisableTracepoints_feature:
  12598. case PACKET_tracenz_feature:
  12599. case PACKET_DisconnectedTracing_feature:
  12600. case PACKET_augmented_libraries_svr4_read_feature:
  12601. case PACKET_qCRC:
  12602. /* Additions to this list need to be well justified:
  12603. pre-existing packets are OK; new packets are not. */
  12604. excepted = 1;
  12605. break;
  12606. default:
  12607. excepted = 0;
  12608. break;
  12609. }
  12610. /* This catches both forgetting to add a config command, and
  12611. forgetting to remove a packet from the exception list. */
  12612. gdb_assert (excepted == (remote_protocol_packets[i].name == NULL));
  12613. }
  12614. }
  12615. /* Keep the old ``set remote Z-packet ...'' working. Each individual
  12616. Z sub-packet has its own set and show commands, but users may
  12617. have sets to this variable in their .gdbinit files (or in their
  12618. documentation). */
  12619. add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
  12620. &remote_Z_packet_detect, _("\
  12621. Set use of remote protocol `Z' packets."), _("\
  12622. Show use of remote protocol `Z' packets."), _("\
  12623. When set, GDB will attempt to use the remote breakpoint and watchpoint\n\
  12624. packets."),
  12625. set_remote_protocol_Z_packet_cmd,
  12626. show_remote_protocol_Z_packet_cmd,
  12627. /* FIXME: i18n: Use of remote protocol
  12628. `Z' packets is %s. */
  12629. &remote_set_cmdlist, &remote_show_cmdlist);
  12630. add_basic_prefix_cmd ("remote", class_files, _("\
  12631. Manipulate files on the remote system.\n\
  12632. Transfer files to and from the remote target system."),
  12633. &remote_cmdlist,
  12634. 0 /* allow-unknown */, &cmdlist);
  12635. add_cmd ("put", class_files, remote_put_command,
  12636. _("Copy a local file to the remote system."),
  12637. &remote_cmdlist);
  12638. add_cmd ("get", class_files, remote_get_command,
  12639. _("Copy a remote file to the local system."),
  12640. &remote_cmdlist);
  12641. add_cmd ("delete", class_files, remote_delete_command,
  12642. _("Delete a remote file."),
  12643. &remote_cmdlist);
  12644. add_setshow_string_noescape_cmd ("exec-file", class_files,
  12645. &remote_exec_file_var, _("\
  12646. Set the remote pathname for \"run\"."), _("\
  12647. Show the remote pathname for \"run\"."), NULL,
  12648. set_remote_exec_file,
  12649. show_remote_exec_file,
  12650. &remote_set_cmdlist,
  12651. &remote_show_cmdlist);
  12652. add_setshow_boolean_cmd ("range-stepping", class_run,
  12653. &use_range_stepping, _("\
  12654. Enable or disable range stepping."), _("\
  12655. Show whether target-assisted range stepping is enabled."), _("\
  12656. If on, and the target supports it, when stepping a source line, GDB\n\
  12657. tells the target to step the corresponding range of addresses itself instead\n\
  12658. of issuing multiple single-steps. This speeds up source level\n\
  12659. stepping. If off, GDB always issues single-steps, even if range\n\
  12660. stepping is supported by the target. The default is on."),
  12661. set_range_stepping,
  12662. show_range_stepping,
  12663. &setlist,
  12664. &showlist);
  12665. add_setshow_zinteger_cmd ("watchdog", class_maintenance, &watchdog, _("\
  12666. Set watchdog timer."), _("\
  12667. Show watchdog timer."), _("\
  12668. When non-zero, this timeout is used instead of waiting forever for a target\n\
  12669. to finish a low-level step or continue operation. If the specified amount\n\
  12670. of time passes without a response from the target, an error occurs."),
  12671. NULL,
  12672. show_watchdog,
  12673. &setlist, &showlist);
  12674. add_setshow_zuinteger_unlimited_cmd ("remote-packet-max-chars", no_class,
  12675. &remote_packet_max_chars, _("\
  12676. Set the maximum number of characters to display for each remote packet."), _("\
  12677. Show the maximum number of characters to display for each remote packet."), _("\
  12678. Specify \"unlimited\" to display all the characters."),
  12679. NULL, show_remote_packet_max_chars,
  12680. &setdebuglist, &showdebuglist);
  12681. add_setshow_boolean_cmd ("remote", no_class, &remote_debug,
  12682. _("Set debugging of remote protocol."),
  12683. _("Show debugging of remote protocol."),
  12684. _("\
  12685. When enabled, each packet sent or received with the remote target\n\
  12686. is displayed."),
  12687. NULL,
  12688. show_remote_debug,
  12689. &setdebuglist, &showdebuglist);
  12690. add_setshow_zuinteger_unlimited_cmd ("remotetimeout", no_class,
  12691. &remote_timeout, _("\
  12692. Set timeout limit to wait for target to respond."), _("\
  12693. Show timeout limit to wait for target to respond."), _("\
  12694. This value is used to set the time limit for gdb to wait for a response\n\
  12695. from the target."),
  12696. NULL,
  12697. show_remote_timeout,
  12698. &setlist, &showlist);
  12699. /* Eventually initialize fileio. See fileio.c */
  12700. initialize_remote_fileio (&remote_set_cmdlist, &remote_show_cmdlist);
  12701. #if GDB_SELF_TEST
  12702. selftests::register_test ("remote_memory_tagging",
  12703. selftests::test_memory_tagging_functions);
  12704. #endif
  12705. }