target.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675
  1. /* Select target systems and architectures at runtime for GDB.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "target.h"
  17. #include "target-dcache.h"
  18. #include "gdbcmd.h"
  19. #include "symtab.h"
  20. #include "inferior.h"
  21. #include "infrun.h"
  22. #include "observable.h"
  23. #include "bfd.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "dcache.h"
  27. #include <signal.h>
  28. #include "regcache.h"
  29. #include "gdbcore.h"
  30. #include "target-descriptions.h"
  31. #include "gdbthread.h"
  32. #include "solib.h"
  33. #include "exec.h"
  34. #include "inline-frame.h"
  35. #include "tracepoint.h"
  36. #include "gdb/fileio.h"
  37. #include "gdbsupport/agent.h"
  38. #include "auxv.h"
  39. #include "target-debug.h"
  40. #include "top.h"
  41. #include "event-top.h"
  42. #include <algorithm>
  43. #include "gdbsupport/byte-vector.h"
  44. #include "gdbsupport/search.h"
  45. #include "terminal.h"
  46. #include <unordered_map>
  47. #include "target-connection.h"
  48. #include "valprint.h"
  49. #include "cli/cli-decode.h"
  50. static void generic_tls_error (void) ATTRIBUTE_NORETURN;
  51. static void default_terminal_info (struct target_ops *, const char *, int);
  52. static int default_watchpoint_addr_within_range (struct target_ops *,
  53. CORE_ADDR, CORE_ADDR, int);
  54. static int default_region_ok_for_hw_watchpoint (struct target_ops *,
  55. CORE_ADDR, int);
  56. static void default_rcmd (struct target_ops *, const char *, struct ui_file *);
  57. static ptid_t default_get_ada_task_ptid (struct target_ops *self,
  58. long lwp, ULONGEST tid);
  59. static void default_mourn_inferior (struct target_ops *self);
  60. static int default_search_memory (struct target_ops *ops,
  61. CORE_ADDR start_addr,
  62. ULONGEST search_space_len,
  63. const gdb_byte *pattern,
  64. ULONGEST pattern_len,
  65. CORE_ADDR *found_addrp);
  66. static int default_verify_memory (struct target_ops *self,
  67. const gdb_byte *data,
  68. CORE_ADDR memaddr, ULONGEST size);
  69. static void tcomplain (void) ATTRIBUTE_NORETURN;
  70. static struct target_ops *find_default_run_target (const char *);
  71. static int dummy_find_memory_regions (struct target_ops *self,
  72. find_memory_region_ftype ignore1,
  73. void *ignore2);
  74. static gdb::unique_xmalloc_ptr<char> dummy_make_corefile_notes
  75. (struct target_ops *self, bfd *ignore1, int *ignore2);
  76. static std::string default_pid_to_str (struct target_ops *ops, ptid_t ptid);
  77. static enum exec_direction_kind default_execution_direction
  78. (struct target_ops *self);
  79. /* Mapping between target_info objects (which have address identity)
  80. and corresponding open/factory function/callback. Each add_target
  81. call adds one entry to this map, and registers a "target
  82. TARGET_NAME" command that when invoked calls the factory registered
  83. here. The target_info object is associated with the command via
  84. the command's context. */
  85. static std::unordered_map<const target_info *, target_open_ftype *>
  86. target_factories;
  87. /* The singleton debug target. */
  88. static struct target_ops *the_debug_target;
  89. /* Command list for target. */
  90. static struct cmd_list_element *targetlist = NULL;
  91. /* True if we should trust readonly sections from the
  92. executable when reading memory. */
  93. static bool trust_readonly = false;
  94. /* Nonzero if we should show true memory content including
  95. memory breakpoint inserted by gdb. */
  96. static int show_memory_breakpoints = 0;
  97. /* These globals control whether GDB attempts to perform these
  98. operations; they are useful for targets that need to prevent
  99. inadvertent disruption, such as in non-stop mode. */
  100. bool may_write_registers = true;
  101. bool may_write_memory = true;
  102. bool may_insert_breakpoints = true;
  103. bool may_insert_tracepoints = true;
  104. bool may_insert_fast_tracepoints = true;
  105. bool may_stop = true;
  106. /* Non-zero if we want to see trace of target level stuff. */
  107. static unsigned int targetdebug = 0;
  108. static void
  109. set_targetdebug (const char *args, int from_tty, struct cmd_list_element *c)
  110. {
  111. if (targetdebug)
  112. current_inferior ()->push_target (the_debug_target);
  113. else
  114. current_inferior ()->unpush_target (the_debug_target);
  115. }
  116. static void
  117. show_targetdebug (struct ui_file *file, int from_tty,
  118. struct cmd_list_element *c, const char *value)
  119. {
  120. gdb_printf (file, _("Target debugging is %s.\n"), value);
  121. }
  122. int
  123. target_has_memory ()
  124. {
  125. for (target_ops *t = current_inferior ()->top_target ();
  126. t != NULL;
  127. t = t->beneath ())
  128. if (t->has_memory ())
  129. return 1;
  130. return 0;
  131. }
  132. int
  133. target_has_stack ()
  134. {
  135. for (target_ops *t = current_inferior ()->top_target ();
  136. t != NULL;
  137. t = t->beneath ())
  138. if (t->has_stack ())
  139. return 1;
  140. return 0;
  141. }
  142. int
  143. target_has_registers ()
  144. {
  145. for (target_ops *t = current_inferior ()->top_target ();
  146. t != NULL;
  147. t = t->beneath ())
  148. if (t->has_registers ())
  149. return 1;
  150. return 0;
  151. }
  152. bool
  153. target_has_execution (inferior *inf)
  154. {
  155. if (inf == nullptr)
  156. inf = current_inferior ();
  157. for (target_ops *t = inf->top_target ();
  158. t != nullptr;
  159. t = inf->find_target_beneath (t))
  160. if (t->has_execution (inf))
  161. return true;
  162. return false;
  163. }
  164. const char *
  165. target_shortname ()
  166. {
  167. return current_inferior ()->top_target ()->shortname ();
  168. }
  169. /* See target.h. */
  170. bool
  171. target_attach_no_wait ()
  172. {
  173. return current_inferior ()->top_target ()->attach_no_wait ();
  174. }
  175. /* See target.h. */
  176. void
  177. target_post_attach (int pid)
  178. {
  179. return current_inferior ()->top_target ()->post_attach (pid);
  180. }
  181. /* See target.h. */
  182. void
  183. target_prepare_to_store (regcache *regcache)
  184. {
  185. return current_inferior ()->top_target ()->prepare_to_store (regcache);
  186. }
  187. /* See target.h. */
  188. bool
  189. target_supports_enable_disable_tracepoint ()
  190. {
  191. target_ops *target = current_inferior ()->top_target ();
  192. return target->supports_enable_disable_tracepoint ();
  193. }
  194. bool
  195. target_supports_string_tracing ()
  196. {
  197. return current_inferior ()->top_target ()->supports_string_tracing ();
  198. }
  199. /* See target.h. */
  200. bool
  201. target_supports_evaluation_of_breakpoint_conditions ()
  202. {
  203. target_ops *target = current_inferior ()->top_target ();
  204. return target->supports_evaluation_of_breakpoint_conditions ();
  205. }
  206. /* See target.h. */
  207. bool
  208. target_supports_dumpcore ()
  209. {
  210. return current_inferior ()->top_target ()->supports_dumpcore ();
  211. }
  212. /* See target.h. */
  213. void
  214. target_dumpcore (const char *filename)
  215. {
  216. return current_inferior ()->top_target ()->dumpcore (filename);
  217. }
  218. /* See target.h. */
  219. bool
  220. target_can_run_breakpoint_commands ()
  221. {
  222. return current_inferior ()->top_target ()->can_run_breakpoint_commands ();
  223. }
  224. /* See target.h. */
  225. void
  226. target_files_info ()
  227. {
  228. return current_inferior ()->top_target ()->files_info ();
  229. }
  230. /* See target.h. */
  231. int
  232. target_insert_fork_catchpoint (int pid)
  233. {
  234. return current_inferior ()->top_target ()->insert_fork_catchpoint (pid);
  235. }
  236. /* See target.h. */
  237. int
  238. target_remove_fork_catchpoint (int pid)
  239. {
  240. return current_inferior ()->top_target ()->remove_fork_catchpoint (pid);
  241. }
  242. /* See target.h. */
  243. int
  244. target_insert_vfork_catchpoint (int pid)
  245. {
  246. return current_inferior ()->top_target ()->insert_vfork_catchpoint (pid);
  247. }
  248. /* See target.h. */
  249. int
  250. target_remove_vfork_catchpoint (int pid)
  251. {
  252. return current_inferior ()->top_target ()->remove_vfork_catchpoint (pid);
  253. }
  254. /* See target.h. */
  255. int
  256. target_insert_exec_catchpoint (int pid)
  257. {
  258. return current_inferior ()->top_target ()->insert_exec_catchpoint (pid);
  259. }
  260. /* See target.h. */
  261. int
  262. target_remove_exec_catchpoint (int pid)
  263. {
  264. return current_inferior ()->top_target ()->remove_exec_catchpoint (pid);
  265. }
  266. /* See target.h. */
  267. int
  268. target_set_syscall_catchpoint (int pid, bool needed, int any_count,
  269. gdb::array_view<const int> syscall_counts)
  270. {
  271. target_ops *target = current_inferior ()->top_target ();
  272. return target->set_syscall_catchpoint (pid, needed, any_count,
  273. syscall_counts);
  274. }
  275. /* See target.h. */
  276. void
  277. target_rcmd (const char *command, struct ui_file *outbuf)
  278. {
  279. return current_inferior ()->top_target ()->rcmd (command, outbuf);
  280. }
  281. /* See target.h. */
  282. bool
  283. target_can_lock_scheduler ()
  284. {
  285. target_ops *target = current_inferior ()->top_target ();
  286. return (target->get_thread_control_capabilities ()& tc_schedlock) != 0;
  287. }
  288. /* See target.h. */
  289. bool
  290. target_can_async_p ()
  291. {
  292. return target_can_async_p (current_inferior ()->top_target ());
  293. }
  294. /* See target.h. */
  295. bool
  296. target_can_async_p (struct target_ops *target)
  297. {
  298. if (!target_async_permitted)
  299. return false;
  300. return target->can_async_p ();
  301. }
  302. /* See target.h. */
  303. bool
  304. target_is_async_p ()
  305. {
  306. bool result = current_inferior ()->top_target ()->is_async_p ();
  307. gdb_assert (target_async_permitted || !result);
  308. return result;
  309. }
  310. exec_direction_kind
  311. target_execution_direction ()
  312. {
  313. return current_inferior ()->top_target ()->execution_direction ();
  314. }
  315. /* See target.h. */
  316. const char *
  317. target_extra_thread_info (thread_info *tp)
  318. {
  319. return current_inferior ()->top_target ()->extra_thread_info (tp);
  320. }
  321. /* See target.h. */
  322. char *
  323. target_pid_to_exec_file (int pid)
  324. {
  325. return current_inferior ()->top_target ()->pid_to_exec_file (pid);
  326. }
  327. /* See target.h. */
  328. gdbarch *
  329. target_thread_architecture (ptid_t ptid)
  330. {
  331. return current_inferior ()->top_target ()->thread_architecture (ptid);
  332. }
  333. /* See target.h. */
  334. int
  335. target_find_memory_regions (find_memory_region_ftype func, void *data)
  336. {
  337. return current_inferior ()->top_target ()->find_memory_regions (func, data);
  338. }
  339. /* See target.h. */
  340. gdb::unique_xmalloc_ptr<char>
  341. target_make_corefile_notes (bfd *bfd, int *size_p)
  342. {
  343. return current_inferior ()->top_target ()->make_corefile_notes (bfd, size_p);
  344. }
  345. gdb_byte *
  346. target_get_bookmark (const char *args, int from_tty)
  347. {
  348. return current_inferior ()->top_target ()->get_bookmark (args, from_tty);
  349. }
  350. void
  351. target_goto_bookmark (const gdb_byte *arg, int from_tty)
  352. {
  353. return current_inferior ()->top_target ()->goto_bookmark (arg, from_tty);
  354. }
  355. /* See target.h. */
  356. bool
  357. target_stopped_by_watchpoint ()
  358. {
  359. return current_inferior ()->top_target ()->stopped_by_watchpoint ();
  360. }
  361. /* See target.h. */
  362. bool
  363. target_stopped_by_sw_breakpoint ()
  364. {
  365. return current_inferior ()->top_target ()->stopped_by_sw_breakpoint ();
  366. }
  367. bool
  368. target_supports_stopped_by_sw_breakpoint ()
  369. {
  370. target_ops *target = current_inferior ()->top_target ();
  371. return target->supports_stopped_by_sw_breakpoint ();
  372. }
  373. bool
  374. target_stopped_by_hw_breakpoint ()
  375. {
  376. return current_inferior ()->top_target ()->stopped_by_hw_breakpoint ();
  377. }
  378. bool
  379. target_supports_stopped_by_hw_breakpoint ()
  380. {
  381. target_ops *target = current_inferior ()->top_target ();
  382. return target->supports_stopped_by_hw_breakpoint ();
  383. }
  384. /* See target.h. */
  385. bool
  386. target_have_steppable_watchpoint ()
  387. {
  388. return current_inferior ()->top_target ()->have_steppable_watchpoint ();
  389. }
  390. /* See target.h. */
  391. int
  392. target_can_use_hardware_watchpoint (bptype type, int cnt, int othertype)
  393. {
  394. target_ops *target = current_inferior ()->top_target ();
  395. return target->can_use_hw_breakpoint (type, cnt, othertype);
  396. }
  397. /* See target.h. */
  398. int
  399. target_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
  400. {
  401. target_ops *target = current_inferior ()->top_target ();
  402. return target->region_ok_for_hw_watchpoint (addr, len);
  403. }
  404. int
  405. target_can_do_single_step ()
  406. {
  407. return current_inferior ()->top_target ()->can_do_single_step ();
  408. }
  409. /* See target.h. */
  410. int
  411. target_insert_watchpoint (CORE_ADDR addr, int len, target_hw_bp_type type,
  412. expression *cond)
  413. {
  414. target_ops *target = current_inferior ()->top_target ();
  415. return target->insert_watchpoint (addr, len, type, cond);
  416. }
  417. /* See target.h. */
  418. int
  419. target_remove_watchpoint (CORE_ADDR addr, int len, target_hw_bp_type type,
  420. expression *cond)
  421. {
  422. target_ops *target = current_inferior ()->top_target ();
  423. return target->remove_watchpoint (addr, len, type, cond);
  424. }
  425. /* See target.h. */
  426. int
  427. target_insert_hw_breakpoint (gdbarch *gdbarch, bp_target_info *bp_tgt)
  428. {
  429. target_ops *target = current_inferior ()->top_target ();
  430. return target->insert_hw_breakpoint (gdbarch, bp_tgt);
  431. }
  432. /* See target.h. */
  433. int
  434. target_remove_hw_breakpoint (gdbarch *gdbarch, bp_target_info *bp_tgt)
  435. {
  436. target_ops *target = current_inferior ()->top_target ();
  437. return target->remove_hw_breakpoint (gdbarch, bp_tgt);
  438. }
  439. /* See target.h. */
  440. bool
  441. target_can_accel_watchpoint_condition (CORE_ADDR addr, int len, int type,
  442. expression *cond)
  443. {
  444. target_ops *target = current_inferior ()->top_target ();
  445. return target->can_accel_watchpoint_condition (addr, len, type, cond);
  446. }
  447. /* See target.h. */
  448. bool
  449. target_can_execute_reverse ()
  450. {
  451. return current_inferior ()->top_target ()->can_execute_reverse ();
  452. }
  453. ptid_t
  454. target_get_ada_task_ptid (long lwp, ULONGEST tid)
  455. {
  456. return current_inferior ()->top_target ()->get_ada_task_ptid (lwp, tid);
  457. }
  458. bool
  459. target_filesystem_is_local ()
  460. {
  461. return current_inferior ()->top_target ()->filesystem_is_local ();
  462. }
  463. void
  464. target_trace_init ()
  465. {
  466. return current_inferior ()->top_target ()->trace_init ();
  467. }
  468. void
  469. target_download_tracepoint (bp_location *location)
  470. {
  471. return current_inferior ()->top_target ()->download_tracepoint (location);
  472. }
  473. bool
  474. target_can_download_tracepoint ()
  475. {
  476. return current_inferior ()->top_target ()->can_download_tracepoint ();
  477. }
  478. void
  479. target_download_trace_state_variable (const trace_state_variable &tsv)
  480. {
  481. target_ops *target = current_inferior ()->top_target ();
  482. return target->download_trace_state_variable (tsv);
  483. }
  484. void
  485. target_enable_tracepoint (bp_location *loc)
  486. {
  487. return current_inferior ()->top_target ()->enable_tracepoint (loc);
  488. }
  489. void
  490. target_disable_tracepoint (bp_location *loc)
  491. {
  492. return current_inferior ()->top_target ()->disable_tracepoint (loc);
  493. }
  494. void
  495. target_trace_start ()
  496. {
  497. return current_inferior ()->top_target ()->trace_start ();
  498. }
  499. void
  500. target_trace_set_readonly_regions ()
  501. {
  502. return current_inferior ()->top_target ()->trace_set_readonly_regions ();
  503. }
  504. int
  505. target_get_trace_status (trace_status *ts)
  506. {
  507. return current_inferior ()->top_target ()->get_trace_status (ts);
  508. }
  509. void
  510. target_get_tracepoint_status (breakpoint *tp, uploaded_tp *utp)
  511. {
  512. return current_inferior ()->top_target ()->get_tracepoint_status (tp, utp);
  513. }
  514. void
  515. target_trace_stop ()
  516. {
  517. return current_inferior ()->top_target ()->trace_stop ();
  518. }
  519. int
  520. target_trace_find (trace_find_type type, int num,
  521. CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
  522. {
  523. target_ops *target = current_inferior ()->top_target ();
  524. return target->trace_find (type, num, addr1, addr2, tpp);
  525. }
  526. bool
  527. target_get_trace_state_variable_value (int tsv, LONGEST *val)
  528. {
  529. target_ops *target = current_inferior ()->top_target ();
  530. return target->get_trace_state_variable_value (tsv, val);
  531. }
  532. int
  533. target_save_trace_data (const char *filename)
  534. {
  535. return current_inferior ()->top_target ()->save_trace_data (filename);
  536. }
  537. int
  538. target_upload_tracepoints (uploaded_tp **utpp)
  539. {
  540. return current_inferior ()->top_target ()->upload_tracepoints (utpp);
  541. }
  542. int
  543. target_upload_trace_state_variables (uploaded_tsv **utsvp)
  544. {
  545. target_ops *target = current_inferior ()->top_target ();
  546. return target->upload_trace_state_variables (utsvp);
  547. }
  548. LONGEST
  549. target_get_raw_trace_data (gdb_byte *buf, ULONGEST offset, LONGEST len)
  550. {
  551. target_ops *target = current_inferior ()->top_target ();
  552. return target->get_raw_trace_data (buf, offset, len);
  553. }
  554. int
  555. target_get_min_fast_tracepoint_insn_len ()
  556. {
  557. target_ops *target = current_inferior ()->top_target ();
  558. return target->get_min_fast_tracepoint_insn_len ();
  559. }
  560. void
  561. target_set_disconnected_tracing (int val)
  562. {
  563. return current_inferior ()->top_target ()->set_disconnected_tracing (val);
  564. }
  565. void
  566. target_set_circular_trace_buffer (int val)
  567. {
  568. return current_inferior ()->top_target ()->set_circular_trace_buffer (val);
  569. }
  570. void
  571. target_set_trace_buffer_size (LONGEST val)
  572. {
  573. return current_inferior ()->top_target ()->set_trace_buffer_size (val);
  574. }
  575. bool
  576. target_set_trace_notes (const char *user, const char *notes,
  577. const char *stopnotes)
  578. {
  579. target_ops *target = current_inferior ()->top_target ();
  580. return target->set_trace_notes (user, notes, stopnotes);
  581. }
  582. bool
  583. target_get_tib_address (ptid_t ptid, CORE_ADDR *addr)
  584. {
  585. return current_inferior ()->top_target ()->get_tib_address (ptid, addr);
  586. }
  587. void
  588. target_set_permissions ()
  589. {
  590. return current_inferior ()->top_target ()->set_permissions ();
  591. }
  592. bool
  593. target_static_tracepoint_marker_at (CORE_ADDR addr,
  594. static_tracepoint_marker *marker)
  595. {
  596. target_ops *target = current_inferior ()->top_target ();
  597. return target->static_tracepoint_marker_at (addr, marker);
  598. }
  599. std::vector<static_tracepoint_marker>
  600. target_static_tracepoint_markers_by_strid (const char *marker_id)
  601. {
  602. target_ops *target = current_inferior ()->top_target ();
  603. return target->static_tracepoint_markers_by_strid (marker_id);
  604. }
  605. traceframe_info_up
  606. target_traceframe_info ()
  607. {
  608. return current_inferior ()->top_target ()->traceframe_info ();
  609. }
  610. bool
  611. target_use_agent (bool use)
  612. {
  613. return current_inferior ()->top_target ()->use_agent (use);
  614. }
  615. bool
  616. target_can_use_agent ()
  617. {
  618. return current_inferior ()->top_target ()->can_use_agent ();
  619. }
  620. bool
  621. target_augmented_libraries_svr4_read ()
  622. {
  623. return current_inferior ()->top_target ()->augmented_libraries_svr4_read ();
  624. }
  625. bool
  626. target_supports_memory_tagging ()
  627. {
  628. return current_inferior ()->top_target ()->supports_memory_tagging ();
  629. }
  630. bool
  631. target_fetch_memtags (CORE_ADDR address, size_t len, gdb::byte_vector &tags,
  632. int type)
  633. {
  634. return current_inferior ()->top_target ()->fetch_memtags (address, len, tags, type);
  635. }
  636. bool
  637. target_store_memtags (CORE_ADDR address, size_t len,
  638. const gdb::byte_vector &tags, int type)
  639. {
  640. return current_inferior ()->top_target ()->store_memtags (address, len, tags, type);
  641. }
  642. void
  643. target_log_command (const char *p)
  644. {
  645. return current_inferior ()->top_target ()->log_command (p);
  646. }
  647. /* This is used to implement the various target commands. */
  648. static void
  649. open_target (const char *args, int from_tty, struct cmd_list_element *command)
  650. {
  651. auto *ti = static_cast<target_info *> (command->context ());
  652. target_open_ftype *func = target_factories[ti];
  653. if (targetdebug)
  654. gdb_printf (gdb_stdlog, "-> %s->open (...)\n",
  655. ti->shortname);
  656. func (args, from_tty);
  657. if (targetdebug)
  658. gdb_printf (gdb_stdlog, "<- %s->open (%s, %d)\n",
  659. ti->shortname, args, from_tty);
  660. }
  661. /* See target.h. */
  662. void
  663. add_target (const target_info &t, target_open_ftype *func,
  664. completer_ftype *completer)
  665. {
  666. struct cmd_list_element *c;
  667. auto &func_slot = target_factories[&t];
  668. if (func_slot != nullptr)
  669. internal_error (__FILE__, __LINE__,
  670. _("target already added (\"%s\")."), t.shortname);
  671. func_slot = func;
  672. if (targetlist == NULL)
  673. add_basic_prefix_cmd ("target", class_run, _("\
  674. Connect to a target machine or process.\n\
  675. The first argument is the type or protocol of the target machine.\n\
  676. Remaining arguments are interpreted by the target protocol. For more\n\
  677. information on the arguments for a particular protocol, type\n\
  678. `help target ' followed by the protocol name."),
  679. &targetlist, 0, &cmdlist);
  680. c = add_cmd (t.shortname, no_class, t.doc, &targetlist);
  681. c->set_context ((void *) &t);
  682. c->func = open_target;
  683. if (completer != NULL)
  684. set_cmd_completer (c, completer);
  685. }
  686. /* See target.h. */
  687. void
  688. add_deprecated_target_alias (const target_info &tinfo, const char *alias)
  689. {
  690. struct cmd_list_element *c;
  691. /* If we use add_alias_cmd, here, we do not get the deprecated warning,
  692. see PR cli/15104. */
  693. c = add_cmd (alias, no_class, tinfo.doc, &targetlist);
  694. c->func = open_target;
  695. c->set_context ((void *) &tinfo);
  696. gdb::unique_xmalloc_ptr<char> alt
  697. = xstrprintf ("target %s", tinfo.shortname);
  698. deprecate_cmd (c, alt.release ());
  699. }
  700. /* Stub functions */
  701. void
  702. target_kill (void)
  703. {
  704. current_inferior ()->top_target ()->kill ();
  705. }
  706. void
  707. target_load (const char *arg, int from_tty)
  708. {
  709. target_dcache_invalidate ();
  710. current_inferior ()->top_target ()->load (arg, from_tty);
  711. }
  712. /* Define it. */
  713. target_terminal_state target_terminal::m_terminal_state
  714. = target_terminal_state::is_ours;
  715. /* See target/target.h. */
  716. void
  717. target_terminal::init (void)
  718. {
  719. current_inferior ()->top_target ()->terminal_init ();
  720. m_terminal_state = target_terminal_state::is_ours;
  721. }
  722. /* See target/target.h. */
  723. void
  724. target_terminal::inferior (void)
  725. {
  726. struct ui *ui = current_ui;
  727. /* A background resume (``run&'') should leave GDB in control of the
  728. terminal. */
  729. if (ui->prompt_state != PROMPT_BLOCKED)
  730. return;
  731. /* Since we always run the inferior in the main console (unless "set
  732. inferior-tty" is in effect), when some UI other than the main one
  733. calls target_terminal::inferior, then we leave the main UI's
  734. terminal settings as is. */
  735. if (ui != main_ui)
  736. return;
  737. /* If GDB is resuming the inferior in the foreground, install
  738. inferior's terminal modes. */
  739. struct inferior *inf = current_inferior ();
  740. if (inf->terminal_state != target_terminal_state::is_inferior)
  741. {
  742. current_inferior ()->top_target ()->terminal_inferior ();
  743. inf->terminal_state = target_terminal_state::is_inferior;
  744. }
  745. m_terminal_state = target_terminal_state::is_inferior;
  746. /* If the user hit C-c before, pretend that it was hit right
  747. here. */
  748. if (check_quit_flag ())
  749. target_pass_ctrlc ();
  750. }
  751. /* See target/target.h. */
  752. void
  753. target_terminal::restore_inferior (void)
  754. {
  755. struct ui *ui = current_ui;
  756. /* See target_terminal::inferior(). */
  757. if (ui->prompt_state != PROMPT_BLOCKED || ui != main_ui)
  758. return;
  759. /* Restore the terminal settings of inferiors that were in the
  760. foreground but are now ours_for_output due to a temporary
  761. target_target::ours_for_output() call. */
  762. {
  763. scoped_restore_current_inferior restore_inferior;
  764. for (::inferior *inf : all_inferiors ())
  765. {
  766. if (inf->terminal_state == target_terminal_state::is_ours_for_output)
  767. {
  768. set_current_inferior (inf);
  769. current_inferior ()->top_target ()->terminal_inferior ();
  770. inf->terminal_state = target_terminal_state::is_inferior;
  771. }
  772. }
  773. }
  774. m_terminal_state = target_terminal_state::is_inferior;
  775. /* If the user hit C-c before, pretend that it was hit right
  776. here. */
  777. if (check_quit_flag ())
  778. target_pass_ctrlc ();
  779. }
  780. /* Switch terminal state to DESIRED_STATE, either is_ours, or
  781. is_ours_for_output. */
  782. static void
  783. target_terminal_is_ours_kind (target_terminal_state desired_state)
  784. {
  785. scoped_restore_current_inferior restore_inferior;
  786. /* Must do this in two passes. First, have all inferiors save the
  787. current terminal settings. Then, after all inferiors have add a
  788. chance to safely save the terminal settings, restore GDB's
  789. terminal settings. */
  790. for (inferior *inf : all_inferiors ())
  791. {
  792. if (inf->terminal_state == target_terminal_state::is_inferior)
  793. {
  794. set_current_inferior (inf);
  795. current_inferior ()->top_target ()->terminal_save_inferior ();
  796. }
  797. }
  798. for (inferior *inf : all_inferiors ())
  799. {
  800. /* Note we don't check is_inferior here like above because we
  801. need to handle 'is_ours_for_output -> is_ours' too. Careful
  802. to never transition from 'is_ours' to 'is_ours_for_output',
  803. though. */
  804. if (inf->terminal_state != target_terminal_state::is_ours
  805. && inf->terminal_state != desired_state)
  806. {
  807. set_current_inferior (inf);
  808. if (desired_state == target_terminal_state::is_ours)
  809. current_inferior ()->top_target ()->terminal_ours ();
  810. else if (desired_state == target_terminal_state::is_ours_for_output)
  811. current_inferior ()->top_target ()->terminal_ours_for_output ();
  812. else
  813. gdb_assert_not_reached ("unhandled desired state");
  814. inf->terminal_state = desired_state;
  815. }
  816. }
  817. }
  818. /* See target/target.h. */
  819. void
  820. target_terminal::ours ()
  821. {
  822. struct ui *ui = current_ui;
  823. /* See target_terminal::inferior. */
  824. if (ui != main_ui)
  825. return;
  826. if (m_terminal_state == target_terminal_state::is_ours)
  827. return;
  828. target_terminal_is_ours_kind (target_terminal_state::is_ours);
  829. m_terminal_state = target_terminal_state::is_ours;
  830. }
  831. /* See target/target.h. */
  832. void
  833. target_terminal::ours_for_output ()
  834. {
  835. struct ui *ui = current_ui;
  836. /* See target_terminal::inferior. */
  837. if (ui != main_ui)
  838. return;
  839. if (!target_terminal::is_inferior ())
  840. return;
  841. target_terminal_is_ours_kind (target_terminal_state::is_ours_for_output);
  842. target_terminal::m_terminal_state = target_terminal_state::is_ours_for_output;
  843. }
  844. /* See target/target.h. */
  845. void
  846. target_terminal::info (const char *arg, int from_tty)
  847. {
  848. current_inferior ()->top_target ()->terminal_info (arg, from_tty);
  849. }
  850. /* See target.h. */
  851. bool
  852. target_supports_terminal_ours (void)
  853. {
  854. /* The current top target is the target at the top of the target
  855. stack of the current inferior. While normally there's always an
  856. inferior, we must check for nullptr here because we can get here
  857. very early during startup, before the initial inferior is first
  858. created. */
  859. inferior *inf = current_inferior ();
  860. if (inf == nullptr)
  861. return false;
  862. return inf->top_target ()->supports_terminal_ours ();
  863. }
  864. static void
  865. tcomplain (void)
  866. {
  867. error (_("You can't do that when your target is `%s'"),
  868. current_inferior ()->top_target ()->shortname ());
  869. }
  870. void
  871. noprocess (void)
  872. {
  873. error (_("You can't do that without a process to debug."));
  874. }
  875. static void
  876. default_terminal_info (struct target_ops *self, const char *args, int from_tty)
  877. {
  878. gdb_printf (_("No saved terminal information.\n"));
  879. }
  880. /* A default implementation for the to_get_ada_task_ptid target method.
  881. This function builds the PTID by using both LWP and TID as part of
  882. the PTID lwp and tid elements. The pid used is the pid of the
  883. inferior_ptid. */
  884. static ptid_t
  885. default_get_ada_task_ptid (struct target_ops *self, long lwp, ULONGEST tid)
  886. {
  887. return ptid_t (inferior_ptid.pid (), lwp, tid);
  888. }
  889. static enum exec_direction_kind
  890. default_execution_direction (struct target_ops *self)
  891. {
  892. if (!target_can_execute_reverse ())
  893. return EXEC_FORWARD;
  894. else if (!target_can_async_p ())
  895. return EXEC_FORWARD;
  896. else
  897. gdb_assert_not_reached ("\
  898. to_execution_direction must be implemented for reverse async");
  899. }
  900. /* See target.h. */
  901. void
  902. decref_target (target_ops *t)
  903. {
  904. t->decref ();
  905. if (t->refcount () == 0)
  906. {
  907. if (t->stratum () == process_stratum)
  908. connection_list_remove (as_process_stratum_target (t));
  909. target_close (t);
  910. }
  911. }
  912. /* See target.h. */
  913. void
  914. target_stack::push (target_ops *t)
  915. {
  916. t->incref ();
  917. strata stratum = t->stratum ();
  918. if (stratum == process_stratum)
  919. connection_list_add (as_process_stratum_target (t));
  920. /* If there's already a target at this stratum, remove it. */
  921. if (m_stack[stratum] != NULL)
  922. unpush (m_stack[stratum]);
  923. /* Now add the new one. */
  924. m_stack[stratum] = t;
  925. if (m_top < stratum)
  926. m_top = stratum;
  927. }
  928. /* See target.h. */
  929. bool
  930. target_stack::unpush (target_ops *t)
  931. {
  932. gdb_assert (t != NULL);
  933. strata stratum = t->stratum ();
  934. if (stratum == dummy_stratum)
  935. internal_error (__FILE__, __LINE__,
  936. _("Attempt to unpush the dummy target"));
  937. /* Look for the specified target. Note that a target can only occur
  938. once in the target stack. */
  939. if (m_stack[stratum] != t)
  940. {
  941. /* If T wasn't pushed, quit. Only open targets should be
  942. closed. */
  943. return false;
  944. }
  945. /* Unchain the target. */
  946. m_stack[stratum] = NULL;
  947. if (m_top == stratum)
  948. m_top = this->find_beneath (t)->stratum ();
  949. /* Finally close the target, if there are no inferiors
  950. referencing this target still. Note we do this after unchaining,
  951. so any target method calls from within the target_close
  952. implementation don't end up in T anymore. Do leave the target
  953. open if we have are other inferiors referencing this target
  954. still. */
  955. decref_target (t);
  956. return true;
  957. }
  958. /* Unpush TARGET and assert that it worked. */
  959. static void
  960. unpush_target_and_assert (struct target_ops *target)
  961. {
  962. if (!current_inferior ()->unpush_target (target))
  963. {
  964. gdb_printf (gdb_stderr,
  965. "pop_all_targets couldn't find target %s\n",
  966. target->shortname ());
  967. internal_error (__FILE__, __LINE__,
  968. _("failed internal consistency check"));
  969. }
  970. }
  971. void
  972. pop_all_targets_above (enum strata above_stratum)
  973. {
  974. while ((int) (current_inferior ()->top_target ()->stratum ())
  975. > (int) above_stratum)
  976. unpush_target_and_assert (current_inferior ()->top_target ());
  977. }
  978. /* See target.h. */
  979. void
  980. pop_all_targets_at_and_above (enum strata stratum)
  981. {
  982. while ((int) (current_inferior ()->top_target ()->stratum ())
  983. >= (int) stratum)
  984. unpush_target_and_assert (current_inferior ()->top_target ());
  985. }
  986. void
  987. pop_all_targets (void)
  988. {
  989. pop_all_targets_above (dummy_stratum);
  990. }
  991. void
  992. target_unpusher::operator() (struct target_ops *ops) const
  993. {
  994. current_inferior ()->unpush_target (ops);
  995. }
  996. /* Default implementation of to_get_thread_local_address. */
  997. static void
  998. generic_tls_error (void)
  999. {
  1000. throw_error (TLS_GENERIC_ERROR,
  1001. _("Cannot find thread-local variables on this target"));
  1002. }
  1003. /* Using the objfile specified in OBJFILE, find the address for the
  1004. current thread's thread-local storage with offset OFFSET. */
  1005. CORE_ADDR
  1006. target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
  1007. {
  1008. volatile CORE_ADDR addr = 0;
  1009. struct target_ops *target = current_inferior ()->top_target ();
  1010. struct gdbarch *gdbarch = target_gdbarch ();
  1011. /* If OBJFILE is a separate debug object file, look for the
  1012. original object file. */
  1013. if (objfile->separate_debug_objfile_backlink != NULL)
  1014. objfile = objfile->separate_debug_objfile_backlink;
  1015. if (gdbarch_fetch_tls_load_module_address_p (gdbarch))
  1016. {
  1017. ptid_t ptid = inferior_ptid;
  1018. try
  1019. {
  1020. CORE_ADDR lm_addr;
  1021. /* Fetch the load module address for this objfile. */
  1022. lm_addr = gdbarch_fetch_tls_load_module_address (gdbarch,
  1023. objfile);
  1024. if (gdbarch_get_thread_local_address_p (gdbarch))
  1025. addr = gdbarch_get_thread_local_address (gdbarch, ptid, lm_addr,
  1026. offset);
  1027. else
  1028. addr = target->get_thread_local_address (ptid, lm_addr, offset);
  1029. }
  1030. /* If an error occurred, print TLS related messages here. Otherwise,
  1031. throw the error to some higher catcher. */
  1032. catch (const gdb_exception &ex)
  1033. {
  1034. int objfile_is_library = (objfile->flags & OBJF_SHARED);
  1035. switch (ex.error)
  1036. {
  1037. case TLS_NO_LIBRARY_SUPPORT_ERROR:
  1038. error (_("Cannot find thread-local variables "
  1039. "in this thread library."));
  1040. break;
  1041. case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
  1042. if (objfile_is_library)
  1043. error (_("Cannot find shared library `%s' in dynamic"
  1044. " linker's load module list"), objfile_name (objfile));
  1045. else
  1046. error (_("Cannot find executable file `%s' in dynamic"
  1047. " linker's load module list"), objfile_name (objfile));
  1048. break;
  1049. case TLS_NOT_ALLOCATED_YET_ERROR:
  1050. if (objfile_is_library)
  1051. error (_("The inferior has not yet allocated storage for"
  1052. " thread-local variables in\n"
  1053. "the shared library `%s'\n"
  1054. "for %s"),
  1055. objfile_name (objfile),
  1056. target_pid_to_str (ptid).c_str ());
  1057. else
  1058. error (_("The inferior has not yet allocated storage for"
  1059. " thread-local variables in\n"
  1060. "the executable `%s'\n"
  1061. "for %s"),
  1062. objfile_name (objfile),
  1063. target_pid_to_str (ptid).c_str ());
  1064. break;
  1065. case TLS_GENERIC_ERROR:
  1066. if (objfile_is_library)
  1067. error (_("Cannot find thread-local storage for %s, "
  1068. "shared library %s:\n%s"),
  1069. target_pid_to_str (ptid).c_str (),
  1070. objfile_name (objfile), ex.what ());
  1071. else
  1072. error (_("Cannot find thread-local storage for %s, "
  1073. "executable file %s:\n%s"),
  1074. target_pid_to_str (ptid).c_str (),
  1075. objfile_name (objfile), ex.what ());
  1076. break;
  1077. default:
  1078. throw;
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. else
  1084. error (_("Cannot find thread-local variables on this target"));
  1085. return addr;
  1086. }
  1087. const char *
  1088. target_xfer_status_to_string (enum target_xfer_status status)
  1089. {
  1090. #define CASE(X) case X: return #X
  1091. switch (status)
  1092. {
  1093. CASE(TARGET_XFER_E_IO);
  1094. CASE(TARGET_XFER_UNAVAILABLE);
  1095. default:
  1096. return "<unknown>";
  1097. }
  1098. #undef CASE
  1099. };
  1100. /* See target.h. */
  1101. gdb::unique_xmalloc_ptr<char>
  1102. target_read_string (CORE_ADDR memaddr, int len, int *bytes_read)
  1103. {
  1104. gdb::unique_xmalloc_ptr<gdb_byte> buffer;
  1105. int ignore;
  1106. if (bytes_read == nullptr)
  1107. bytes_read = &ignore;
  1108. /* Note that the endian-ness does not matter here. */
  1109. int errcode = read_string (memaddr, -1, 1, len, BFD_ENDIAN_LITTLE,
  1110. &buffer, bytes_read);
  1111. if (errcode != 0)
  1112. return {};
  1113. return gdb::unique_xmalloc_ptr<char> ((char *) buffer.release ());
  1114. }
  1115. const target_section_table *
  1116. target_get_section_table (struct target_ops *target)
  1117. {
  1118. return target->get_section_table ();
  1119. }
  1120. /* Find a section containing ADDR. */
  1121. const struct target_section *
  1122. target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
  1123. {
  1124. const target_section_table *table = target_get_section_table (target);
  1125. if (table == NULL)
  1126. return NULL;
  1127. for (const target_section &secp : *table)
  1128. {
  1129. if (addr >= secp.addr && addr < secp.endaddr)
  1130. return &secp;
  1131. }
  1132. return NULL;
  1133. }
  1134. /* See target.h. */
  1135. const target_section_table *
  1136. default_get_section_table ()
  1137. {
  1138. return &current_program_space->target_sections ();
  1139. }
  1140. /* Helper for the memory xfer routines. Checks the attributes of the
  1141. memory region of MEMADDR against the read or write being attempted.
  1142. If the access is permitted returns true, otherwise returns false.
  1143. REGION_P is an optional output parameter. If not-NULL, it is
  1144. filled with a pointer to the memory region of MEMADDR. REG_LEN
  1145. returns LEN trimmed to the end of the region. This is how much the
  1146. caller can continue requesting, if the access is permitted. A
  1147. single xfer request must not straddle memory region boundaries. */
  1148. static int
  1149. memory_xfer_check_region (gdb_byte *readbuf, const gdb_byte *writebuf,
  1150. ULONGEST memaddr, ULONGEST len, ULONGEST *reg_len,
  1151. struct mem_region **region_p)
  1152. {
  1153. struct mem_region *region;
  1154. region = lookup_mem_region (memaddr);
  1155. if (region_p != NULL)
  1156. *region_p = region;
  1157. switch (region->attrib.mode)
  1158. {
  1159. case MEM_RO:
  1160. if (writebuf != NULL)
  1161. return 0;
  1162. break;
  1163. case MEM_WO:
  1164. if (readbuf != NULL)
  1165. return 0;
  1166. break;
  1167. case MEM_FLASH:
  1168. /* We only support writing to flash during "load" for now. */
  1169. if (writebuf != NULL)
  1170. error (_("Writing to flash memory forbidden in this context"));
  1171. break;
  1172. case MEM_NONE:
  1173. return 0;
  1174. }
  1175. /* region->hi == 0 means there's no upper bound. */
  1176. if (memaddr + len < region->hi || region->hi == 0)
  1177. *reg_len = len;
  1178. else
  1179. *reg_len = region->hi - memaddr;
  1180. return 1;
  1181. }
  1182. /* Read memory from more than one valid target. A core file, for
  1183. instance, could have some of memory but delegate other bits to
  1184. the target below it. So, we must manually try all targets. */
  1185. enum target_xfer_status
  1186. raw_memory_xfer_partial (struct target_ops *ops, gdb_byte *readbuf,
  1187. const gdb_byte *writebuf, ULONGEST memaddr, LONGEST len,
  1188. ULONGEST *xfered_len)
  1189. {
  1190. enum target_xfer_status res;
  1191. do
  1192. {
  1193. res = ops->xfer_partial (TARGET_OBJECT_MEMORY, NULL,
  1194. readbuf, writebuf, memaddr, len,
  1195. xfered_len);
  1196. if (res == TARGET_XFER_OK)
  1197. break;
  1198. /* Stop if the target reports that the memory is not available. */
  1199. if (res == TARGET_XFER_UNAVAILABLE)
  1200. break;
  1201. /* Don't continue past targets which have all the memory.
  1202. At one time, this code was necessary to read data from
  1203. executables / shared libraries when data for the requested
  1204. addresses weren't available in the core file. But now the
  1205. core target handles this case itself. */
  1206. if (ops->has_all_memory ())
  1207. break;
  1208. ops = ops->beneath ();
  1209. }
  1210. while (ops != NULL);
  1211. /* The cache works at the raw memory level. Make sure the cache
  1212. gets updated with raw contents no matter what kind of memory
  1213. object was originally being written. Note we do write-through
  1214. first, so that if it fails, we don't write to the cache contents
  1215. that never made it to the target. */
  1216. if (writebuf != NULL
  1217. && inferior_ptid != null_ptid
  1218. && target_dcache_init_p ()
  1219. && (stack_cache_enabled_p () || code_cache_enabled_p ()))
  1220. {
  1221. DCACHE *dcache = target_dcache_get ();
  1222. /* Note that writing to an area of memory which wasn't present
  1223. in the cache doesn't cause it to be loaded in. */
  1224. dcache_update (dcache, res, memaddr, writebuf, *xfered_len);
  1225. }
  1226. return res;
  1227. }
  1228. /* Perform a partial memory transfer.
  1229. For docs see target.h, to_xfer_partial. */
  1230. static enum target_xfer_status
  1231. memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
  1232. gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST memaddr,
  1233. ULONGEST len, ULONGEST *xfered_len)
  1234. {
  1235. enum target_xfer_status res;
  1236. ULONGEST reg_len;
  1237. struct mem_region *region;
  1238. struct inferior *inf;
  1239. /* For accesses to unmapped overlay sections, read directly from
  1240. files. Must do this first, as MEMADDR may need adjustment. */
  1241. if (readbuf != NULL && overlay_debugging)
  1242. {
  1243. struct obj_section *section = find_pc_overlay (memaddr);
  1244. if (pc_in_unmapped_range (memaddr, section))
  1245. {
  1246. const target_section_table *table = target_get_section_table (ops);
  1247. const char *section_name = section->the_bfd_section->name;
  1248. memaddr = overlay_mapped_address (memaddr, section);
  1249. auto match_cb = [=] (const struct target_section *s)
  1250. {
  1251. return (strcmp (section_name, s->the_bfd_section->name) == 0);
  1252. };
  1253. return section_table_xfer_memory_partial (readbuf, writebuf,
  1254. memaddr, len, xfered_len,
  1255. *table, match_cb);
  1256. }
  1257. }
  1258. /* Try the executable files, if "trust-readonly-sections" is set. */
  1259. if (readbuf != NULL && trust_readonly)
  1260. {
  1261. const struct target_section *secp
  1262. = target_section_by_addr (ops, memaddr);
  1263. if (secp != NULL
  1264. && (bfd_section_flags (secp->the_bfd_section) & SEC_READONLY))
  1265. {
  1266. const target_section_table *table = target_get_section_table (ops);
  1267. return section_table_xfer_memory_partial (readbuf, writebuf,
  1268. memaddr, len, xfered_len,
  1269. *table);
  1270. }
  1271. }
  1272. /* Try GDB's internal data cache. */
  1273. if (!memory_xfer_check_region (readbuf, writebuf, memaddr, len, &reg_len,
  1274. &region))
  1275. return TARGET_XFER_E_IO;
  1276. if (inferior_ptid != null_ptid)
  1277. inf = current_inferior ();
  1278. else
  1279. inf = NULL;
  1280. if (inf != NULL
  1281. && readbuf != NULL
  1282. /* The dcache reads whole cache lines; that doesn't play well
  1283. with reading from a trace buffer, because reading outside of
  1284. the collected memory range fails. */
  1285. && get_traceframe_number () == -1
  1286. && (region->attrib.cache
  1287. || (stack_cache_enabled_p () && object == TARGET_OBJECT_STACK_MEMORY)
  1288. || (code_cache_enabled_p () && object == TARGET_OBJECT_CODE_MEMORY)))
  1289. {
  1290. DCACHE *dcache = target_dcache_get_or_init ();
  1291. return dcache_read_memory_partial (ops, dcache, memaddr, readbuf,
  1292. reg_len, xfered_len);
  1293. }
  1294. /* If none of those methods found the memory we wanted, fall back
  1295. to a target partial transfer. Normally a single call to
  1296. to_xfer_partial is enough; if it doesn't recognize an object
  1297. it will call the to_xfer_partial of the next target down.
  1298. But for memory this won't do. Memory is the only target
  1299. object which can be read from more than one valid target.
  1300. A core file, for instance, could have some of memory but
  1301. delegate other bits to the target below it. So, we must
  1302. manually try all targets. */
  1303. res = raw_memory_xfer_partial (ops, readbuf, writebuf, memaddr, reg_len,
  1304. xfered_len);
  1305. /* If we still haven't got anything, return the last error. We
  1306. give up. */
  1307. return res;
  1308. }
  1309. /* Perform a partial memory transfer. For docs see target.h,
  1310. to_xfer_partial. */
  1311. static enum target_xfer_status
  1312. memory_xfer_partial (struct target_ops *ops, enum target_object object,
  1313. gdb_byte *readbuf, const gdb_byte *writebuf,
  1314. ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
  1315. {
  1316. enum target_xfer_status res;
  1317. /* Zero length requests are ok and require no work. */
  1318. if (len == 0)
  1319. return TARGET_XFER_EOF;
  1320. memaddr = address_significant (target_gdbarch (), memaddr);
  1321. /* Fill in READBUF with breakpoint shadows, or WRITEBUF with
  1322. breakpoint insns, thus hiding out from higher layers whether
  1323. there are software breakpoints inserted in the code stream. */
  1324. if (readbuf != NULL)
  1325. {
  1326. res = memory_xfer_partial_1 (ops, object, readbuf, NULL, memaddr, len,
  1327. xfered_len);
  1328. if (res == TARGET_XFER_OK && !show_memory_breakpoints)
  1329. breakpoint_xfer_memory (readbuf, NULL, NULL, memaddr, *xfered_len);
  1330. }
  1331. else
  1332. {
  1333. /* A large write request is likely to be partially satisfied
  1334. by memory_xfer_partial_1. We will continually malloc
  1335. and free a copy of the entire write request for breakpoint
  1336. shadow handling even though we only end up writing a small
  1337. subset of it. Cap writes to a limit specified by the target
  1338. to mitigate this. */
  1339. len = std::min (ops->get_memory_xfer_limit (), len);
  1340. gdb::byte_vector buf (writebuf, writebuf + len);
  1341. breakpoint_xfer_memory (NULL, buf.data (), writebuf, memaddr, len);
  1342. res = memory_xfer_partial_1 (ops, object, NULL, buf.data (), memaddr, len,
  1343. xfered_len);
  1344. }
  1345. return res;
  1346. }
  1347. scoped_restore_tmpl<int>
  1348. make_scoped_restore_show_memory_breakpoints (int show)
  1349. {
  1350. return make_scoped_restore (&show_memory_breakpoints, show);
  1351. }
  1352. /* For docs see target.h, to_xfer_partial. */
  1353. enum target_xfer_status
  1354. target_xfer_partial (struct target_ops *ops,
  1355. enum target_object object, const char *annex,
  1356. gdb_byte *readbuf, const gdb_byte *writebuf,
  1357. ULONGEST offset, ULONGEST len,
  1358. ULONGEST *xfered_len)
  1359. {
  1360. enum target_xfer_status retval;
  1361. /* Transfer is done when LEN is zero. */
  1362. if (len == 0)
  1363. return TARGET_XFER_EOF;
  1364. if (writebuf && !may_write_memory)
  1365. error (_("Writing to memory is not allowed (addr %s, len %s)"),
  1366. core_addr_to_string_nz (offset), plongest (len));
  1367. *xfered_len = 0;
  1368. /* If this is a memory transfer, let the memory-specific code
  1369. have a look at it instead. Memory transfers are more
  1370. complicated. */
  1371. if (object == TARGET_OBJECT_MEMORY || object == TARGET_OBJECT_STACK_MEMORY
  1372. || object == TARGET_OBJECT_CODE_MEMORY)
  1373. retval = memory_xfer_partial (ops, object, readbuf,
  1374. writebuf, offset, len, xfered_len);
  1375. else if (object == TARGET_OBJECT_RAW_MEMORY)
  1376. {
  1377. /* Skip/avoid accessing the target if the memory region
  1378. attributes block the access. Check this here instead of in
  1379. raw_memory_xfer_partial as otherwise we'd end up checking
  1380. this twice in the case of the memory_xfer_partial path is
  1381. taken; once before checking the dcache, and another in the
  1382. tail call to raw_memory_xfer_partial. */
  1383. if (!memory_xfer_check_region (readbuf, writebuf, offset, len, &len,
  1384. NULL))
  1385. return TARGET_XFER_E_IO;
  1386. /* Request the normal memory object from other layers. */
  1387. retval = raw_memory_xfer_partial (ops, readbuf, writebuf, offset, len,
  1388. xfered_len);
  1389. }
  1390. else
  1391. retval = ops->xfer_partial (object, annex, readbuf,
  1392. writebuf, offset, len, xfered_len);
  1393. if (targetdebug)
  1394. {
  1395. const unsigned char *myaddr = NULL;
  1396. gdb_printf (gdb_stdlog,
  1397. "%s:target_xfer_partial "
  1398. "(%d, %s, %s, %s, %s, %s) = %d, %s",
  1399. ops->shortname (),
  1400. (int) object,
  1401. (annex ? annex : "(null)"),
  1402. host_address_to_string (readbuf),
  1403. host_address_to_string (writebuf),
  1404. core_addr_to_string_nz (offset),
  1405. pulongest (len), retval,
  1406. pulongest (*xfered_len));
  1407. if (readbuf)
  1408. myaddr = readbuf;
  1409. if (writebuf)
  1410. myaddr = writebuf;
  1411. if (retval == TARGET_XFER_OK && myaddr != NULL)
  1412. {
  1413. int i;
  1414. gdb_puts (", bytes =", gdb_stdlog);
  1415. for (i = 0; i < *xfered_len; i++)
  1416. {
  1417. if ((((intptr_t) &(myaddr[i])) & 0xf) == 0)
  1418. {
  1419. if (targetdebug < 2 && i > 0)
  1420. {
  1421. gdb_printf (gdb_stdlog, " ...");
  1422. break;
  1423. }
  1424. gdb_printf (gdb_stdlog, "\n");
  1425. }
  1426. gdb_printf (gdb_stdlog, " %02x", myaddr[i] & 0xff);
  1427. }
  1428. }
  1429. gdb_putc ('\n', gdb_stdlog);
  1430. }
  1431. /* Check implementations of to_xfer_partial update *XFERED_LEN
  1432. properly. Do assertion after printing debug messages, so that we
  1433. can find more clues on assertion failure from debugging messages. */
  1434. if (retval == TARGET_XFER_OK || retval == TARGET_XFER_UNAVAILABLE)
  1435. gdb_assert (*xfered_len > 0);
  1436. return retval;
  1437. }
  1438. /* Read LEN bytes of target memory at address MEMADDR, placing the
  1439. results in GDB's memory at MYADDR. Returns either 0 for success or
  1440. -1 if any error occurs.
  1441. If an error occurs, no guarantee is made about the contents of the data at
  1442. MYADDR. In particular, the caller should not depend upon partial reads
  1443. filling the buffer with good data. There is no way for the caller to know
  1444. how much good data might have been transfered anyway. Callers that can
  1445. deal with partial reads should call target_read (which will retry until
  1446. it makes no progress, and then return how much was transferred). */
  1447. int
  1448. target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  1449. {
  1450. if (target_read (current_inferior ()->top_target (),
  1451. TARGET_OBJECT_MEMORY, NULL,
  1452. myaddr, memaddr, len) == len)
  1453. return 0;
  1454. else
  1455. return -1;
  1456. }
  1457. /* See target/target.h. */
  1458. int
  1459. target_read_uint32 (CORE_ADDR memaddr, uint32_t *result)
  1460. {
  1461. gdb_byte buf[4];
  1462. int r;
  1463. r = target_read_memory (memaddr, buf, sizeof buf);
  1464. if (r != 0)
  1465. return r;
  1466. *result = extract_unsigned_integer (buf, sizeof buf,
  1467. gdbarch_byte_order (target_gdbarch ()));
  1468. return 0;
  1469. }
  1470. /* Like target_read_memory, but specify explicitly that this is a read
  1471. from the target's raw memory. That is, this read bypasses the
  1472. dcache, breakpoint shadowing, etc. */
  1473. int
  1474. target_read_raw_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  1475. {
  1476. if (target_read (current_inferior ()->top_target (),
  1477. TARGET_OBJECT_RAW_MEMORY, NULL,
  1478. myaddr, memaddr, len) == len)
  1479. return 0;
  1480. else
  1481. return -1;
  1482. }
  1483. /* Like target_read_memory, but specify explicitly that this is a read from
  1484. the target's stack. This may trigger different cache behavior. */
  1485. int
  1486. target_read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  1487. {
  1488. if (target_read (current_inferior ()->top_target (),
  1489. TARGET_OBJECT_STACK_MEMORY, NULL,
  1490. myaddr, memaddr, len) == len)
  1491. return 0;
  1492. else
  1493. return -1;
  1494. }
  1495. /* Like target_read_memory, but specify explicitly that this is a read from
  1496. the target's code. This may trigger different cache behavior. */
  1497. int
  1498. target_read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
  1499. {
  1500. if (target_read (current_inferior ()->top_target (),
  1501. TARGET_OBJECT_CODE_MEMORY, NULL,
  1502. myaddr, memaddr, len) == len)
  1503. return 0;
  1504. else
  1505. return -1;
  1506. }
  1507. /* Write LEN bytes from MYADDR to target memory at address MEMADDR.
  1508. Returns either 0 for success or -1 if any error occurs. If an
  1509. error occurs, no guarantee is made about how much data got written.
  1510. Callers that can deal with partial writes should call
  1511. target_write. */
  1512. int
  1513. target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
  1514. {
  1515. if (target_write (current_inferior ()->top_target (),
  1516. TARGET_OBJECT_MEMORY, NULL,
  1517. myaddr, memaddr, len) == len)
  1518. return 0;
  1519. else
  1520. return -1;
  1521. }
  1522. /* Write LEN bytes from MYADDR to target raw memory at address
  1523. MEMADDR. Returns either 0 for success or -1 if any error occurs.
  1524. If an error occurs, no guarantee is made about how much data got
  1525. written. Callers that can deal with partial writes should call
  1526. target_write. */
  1527. int
  1528. target_write_raw_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
  1529. {
  1530. if (target_write (current_inferior ()->top_target (),
  1531. TARGET_OBJECT_RAW_MEMORY, NULL,
  1532. myaddr, memaddr, len) == len)
  1533. return 0;
  1534. else
  1535. return -1;
  1536. }
  1537. /* Fetch the target's memory map. */
  1538. std::vector<mem_region>
  1539. target_memory_map (void)
  1540. {
  1541. target_ops *target = current_inferior ()->top_target ();
  1542. std::vector<mem_region> result = target->memory_map ();
  1543. if (result.empty ())
  1544. return result;
  1545. std::sort (result.begin (), result.end ());
  1546. /* Check that regions do not overlap. Simultaneously assign
  1547. a numbering for the "mem" commands to use to refer to
  1548. each region. */
  1549. mem_region *last_one = NULL;
  1550. for (size_t ix = 0; ix < result.size (); ix++)
  1551. {
  1552. mem_region *this_one = &result[ix];
  1553. this_one->number = ix;
  1554. if (last_one != NULL && last_one->hi > this_one->lo)
  1555. {
  1556. warning (_("Overlapping regions in memory map: ignoring"));
  1557. return std::vector<mem_region> ();
  1558. }
  1559. last_one = this_one;
  1560. }
  1561. return result;
  1562. }
  1563. void
  1564. target_flash_erase (ULONGEST address, LONGEST length)
  1565. {
  1566. current_inferior ()->top_target ()->flash_erase (address, length);
  1567. }
  1568. void
  1569. target_flash_done (void)
  1570. {
  1571. current_inferior ()->top_target ()->flash_done ();
  1572. }
  1573. static void
  1574. show_trust_readonly (struct ui_file *file, int from_tty,
  1575. struct cmd_list_element *c, const char *value)
  1576. {
  1577. gdb_printf (file,
  1578. _("Mode for reading from readonly sections is %s.\n"),
  1579. value);
  1580. }
  1581. /* Target vector read/write partial wrapper functions. */
  1582. static enum target_xfer_status
  1583. target_read_partial (struct target_ops *ops,
  1584. enum target_object object,
  1585. const char *annex, gdb_byte *buf,
  1586. ULONGEST offset, ULONGEST len,
  1587. ULONGEST *xfered_len)
  1588. {
  1589. return target_xfer_partial (ops, object, annex, buf, NULL, offset, len,
  1590. xfered_len);
  1591. }
  1592. static enum target_xfer_status
  1593. target_write_partial (struct target_ops *ops,
  1594. enum target_object object,
  1595. const char *annex, const gdb_byte *buf,
  1596. ULONGEST offset, LONGEST len, ULONGEST *xfered_len)
  1597. {
  1598. return target_xfer_partial (ops, object, annex, NULL, buf, offset, len,
  1599. xfered_len);
  1600. }
  1601. /* Wrappers to perform the full transfer. */
  1602. /* For docs on target_read see target.h. */
  1603. LONGEST
  1604. target_read (struct target_ops *ops,
  1605. enum target_object object,
  1606. const char *annex, gdb_byte *buf,
  1607. ULONGEST offset, LONGEST len)
  1608. {
  1609. LONGEST xfered_total = 0;
  1610. int unit_size = 1;
  1611. /* If we are reading from a memory object, find the length of an addressable
  1612. unit for that architecture. */
  1613. if (object == TARGET_OBJECT_MEMORY
  1614. || object == TARGET_OBJECT_STACK_MEMORY
  1615. || object == TARGET_OBJECT_CODE_MEMORY
  1616. || object == TARGET_OBJECT_RAW_MEMORY)
  1617. unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
  1618. while (xfered_total < len)
  1619. {
  1620. ULONGEST xfered_partial;
  1621. enum target_xfer_status status;
  1622. status = target_read_partial (ops, object, annex,
  1623. buf + xfered_total * unit_size,
  1624. offset + xfered_total, len - xfered_total,
  1625. &xfered_partial);
  1626. /* Call an observer, notifying them of the xfer progress? */
  1627. if (status == TARGET_XFER_EOF)
  1628. return xfered_total;
  1629. else if (status == TARGET_XFER_OK)
  1630. {
  1631. xfered_total += xfered_partial;
  1632. QUIT;
  1633. }
  1634. else
  1635. return TARGET_XFER_E_IO;
  1636. }
  1637. return len;
  1638. }
  1639. /* Assuming that the entire [begin, end) range of memory cannot be
  1640. read, try to read whatever subrange is possible to read.
  1641. The function returns, in RESULT, either zero or one memory block.
  1642. If there's a readable subrange at the beginning, it is completely
  1643. read and returned. Any further readable subrange will not be read.
  1644. Otherwise, if there's a readable subrange at the end, it will be
  1645. completely read and returned. Any readable subranges before it
  1646. (obviously, not starting at the beginning), will be ignored. In
  1647. other cases -- either no readable subrange, or readable subrange(s)
  1648. that is neither at the beginning, or end, nothing is returned.
  1649. The purpose of this function is to handle a read across a boundary
  1650. of accessible memory in a case when memory map is not available.
  1651. The above restrictions are fine for this case, but will give
  1652. incorrect results if the memory is 'patchy'. However, supporting
  1653. 'patchy' memory would require trying to read every single byte,
  1654. and it seems unacceptable solution. Explicit memory map is
  1655. recommended for this case -- and target_read_memory_robust will
  1656. take care of reading multiple ranges then. */
  1657. static void
  1658. read_whatever_is_readable (struct target_ops *ops,
  1659. const ULONGEST begin, const ULONGEST end,
  1660. int unit_size,
  1661. std::vector<memory_read_result> *result)
  1662. {
  1663. ULONGEST current_begin = begin;
  1664. ULONGEST current_end = end;
  1665. int forward;
  1666. ULONGEST xfered_len;
  1667. /* If we previously failed to read 1 byte, nothing can be done here. */
  1668. if (end - begin <= 1)
  1669. return;
  1670. gdb::unique_xmalloc_ptr<gdb_byte> buf ((gdb_byte *) xmalloc (end - begin));
  1671. /* Check that either first or the last byte is readable, and give up
  1672. if not. This heuristic is meant to permit reading accessible memory
  1673. at the boundary of accessible region. */
  1674. if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
  1675. buf.get (), begin, 1, &xfered_len) == TARGET_XFER_OK)
  1676. {
  1677. forward = 1;
  1678. ++current_begin;
  1679. }
  1680. else if (target_read_partial (ops, TARGET_OBJECT_MEMORY, NULL,
  1681. buf.get () + (end - begin) - 1, end - 1, 1,
  1682. &xfered_len) == TARGET_XFER_OK)
  1683. {
  1684. forward = 0;
  1685. --current_end;
  1686. }
  1687. else
  1688. return;
  1689. /* Loop invariant is that the [current_begin, current_end) was previously
  1690. found to be not readable as a whole.
  1691. Note loop condition -- if the range has 1 byte, we can't divide the range
  1692. so there's no point trying further. */
  1693. while (current_end - current_begin > 1)
  1694. {
  1695. ULONGEST first_half_begin, first_half_end;
  1696. ULONGEST second_half_begin, second_half_end;
  1697. LONGEST xfer;
  1698. ULONGEST middle = current_begin + (current_end - current_begin) / 2;
  1699. if (forward)
  1700. {
  1701. first_half_begin = current_begin;
  1702. first_half_end = middle;
  1703. second_half_begin = middle;
  1704. second_half_end = current_end;
  1705. }
  1706. else
  1707. {
  1708. first_half_begin = middle;
  1709. first_half_end = current_end;
  1710. second_half_begin = current_begin;
  1711. second_half_end = middle;
  1712. }
  1713. xfer = target_read (ops, TARGET_OBJECT_MEMORY, NULL,
  1714. buf.get () + (first_half_begin - begin) * unit_size,
  1715. first_half_begin,
  1716. first_half_end - first_half_begin);
  1717. if (xfer == first_half_end - first_half_begin)
  1718. {
  1719. /* This half reads up fine. So, the error must be in the
  1720. other half. */
  1721. current_begin = second_half_begin;
  1722. current_end = second_half_end;
  1723. }
  1724. else
  1725. {
  1726. /* This half is not readable. Because we've tried one byte, we
  1727. know some part of this half if actually readable. Go to the next
  1728. iteration to divide again and try to read.
  1729. We don't handle the other half, because this function only tries
  1730. to read a single readable subrange. */
  1731. current_begin = first_half_begin;
  1732. current_end = first_half_end;
  1733. }
  1734. }
  1735. if (forward)
  1736. {
  1737. /* The [begin, current_begin) range has been read. */
  1738. result->emplace_back (begin, current_end, std::move (buf));
  1739. }
  1740. else
  1741. {
  1742. /* The [current_end, end) range has been read. */
  1743. LONGEST region_len = end - current_end;
  1744. gdb::unique_xmalloc_ptr<gdb_byte> data
  1745. ((gdb_byte *) xmalloc (region_len * unit_size));
  1746. memcpy (data.get (), buf.get () + (current_end - begin) * unit_size,
  1747. region_len * unit_size);
  1748. result->emplace_back (current_end, end, std::move (data));
  1749. }
  1750. }
  1751. std::vector<memory_read_result>
  1752. read_memory_robust (struct target_ops *ops,
  1753. const ULONGEST offset, const LONGEST len)
  1754. {
  1755. std::vector<memory_read_result> result;
  1756. int unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
  1757. LONGEST xfered_total = 0;
  1758. while (xfered_total < len)
  1759. {
  1760. struct mem_region *region = lookup_mem_region (offset + xfered_total);
  1761. LONGEST region_len;
  1762. /* If there is no explicit region, a fake one should be created. */
  1763. gdb_assert (region);
  1764. if (region->hi == 0)
  1765. region_len = len - xfered_total;
  1766. else
  1767. region_len = region->hi - offset;
  1768. if (region->attrib.mode == MEM_NONE || region->attrib.mode == MEM_WO)
  1769. {
  1770. /* Cannot read this region. Note that we can end up here only
  1771. if the region is explicitly marked inaccessible, or
  1772. 'inaccessible-by-default' is in effect. */
  1773. xfered_total += region_len;
  1774. }
  1775. else
  1776. {
  1777. LONGEST to_read = std::min (len - xfered_total, region_len);
  1778. gdb::unique_xmalloc_ptr<gdb_byte> buffer
  1779. ((gdb_byte *) xmalloc (to_read * unit_size));
  1780. LONGEST xfered_partial =
  1781. target_read (ops, TARGET_OBJECT_MEMORY, NULL, buffer.get (),
  1782. offset + xfered_total, to_read);
  1783. /* Call an observer, notifying them of the xfer progress? */
  1784. if (xfered_partial <= 0)
  1785. {
  1786. /* Got an error reading full chunk. See if maybe we can read
  1787. some subrange. */
  1788. read_whatever_is_readable (ops, offset + xfered_total,
  1789. offset + xfered_total + to_read,
  1790. unit_size, &result);
  1791. xfered_total += to_read;
  1792. }
  1793. else
  1794. {
  1795. result.emplace_back (offset + xfered_total,
  1796. offset + xfered_total + xfered_partial,
  1797. std::move (buffer));
  1798. xfered_total += xfered_partial;
  1799. }
  1800. QUIT;
  1801. }
  1802. }
  1803. return result;
  1804. }
  1805. /* An alternative to target_write with progress callbacks. */
  1806. LONGEST
  1807. target_write_with_progress (struct target_ops *ops,
  1808. enum target_object object,
  1809. const char *annex, const gdb_byte *buf,
  1810. ULONGEST offset, LONGEST len,
  1811. void (*progress) (ULONGEST, void *), void *baton)
  1812. {
  1813. LONGEST xfered_total = 0;
  1814. int unit_size = 1;
  1815. /* If we are writing to a memory object, find the length of an addressable
  1816. unit for that architecture. */
  1817. if (object == TARGET_OBJECT_MEMORY
  1818. || object == TARGET_OBJECT_STACK_MEMORY
  1819. || object == TARGET_OBJECT_CODE_MEMORY
  1820. || object == TARGET_OBJECT_RAW_MEMORY)
  1821. unit_size = gdbarch_addressable_memory_unit_size (target_gdbarch ());
  1822. /* Give the progress callback a chance to set up. */
  1823. if (progress)
  1824. (*progress) (0, baton);
  1825. while (xfered_total < len)
  1826. {
  1827. ULONGEST xfered_partial;
  1828. enum target_xfer_status status;
  1829. status = target_write_partial (ops, object, annex,
  1830. buf + xfered_total * unit_size,
  1831. offset + xfered_total, len - xfered_total,
  1832. &xfered_partial);
  1833. if (status != TARGET_XFER_OK)
  1834. return status == TARGET_XFER_EOF ? xfered_total : TARGET_XFER_E_IO;
  1835. if (progress)
  1836. (*progress) (xfered_partial, baton);
  1837. xfered_total += xfered_partial;
  1838. QUIT;
  1839. }
  1840. return len;
  1841. }
  1842. /* For docs on target_write see target.h. */
  1843. LONGEST
  1844. target_write (struct target_ops *ops,
  1845. enum target_object object,
  1846. const char *annex, const gdb_byte *buf,
  1847. ULONGEST offset, LONGEST len)
  1848. {
  1849. return target_write_with_progress (ops, object, annex, buf, offset, len,
  1850. NULL, NULL);
  1851. }
  1852. /* Help for target_read_alloc and target_read_stralloc. See their comments
  1853. for details. */
  1854. template <typename T>
  1855. gdb::optional<gdb::def_vector<T>>
  1856. target_read_alloc_1 (struct target_ops *ops, enum target_object object,
  1857. const char *annex)
  1858. {
  1859. gdb::def_vector<T> buf;
  1860. size_t buf_pos = 0;
  1861. const int chunk = 4096;
  1862. /* This function does not have a length parameter; it reads the
  1863. entire OBJECT). Also, it doesn't support objects fetched partly
  1864. from one target and partly from another (in a different stratum,
  1865. e.g. a core file and an executable). Both reasons make it
  1866. unsuitable for reading memory. */
  1867. gdb_assert (object != TARGET_OBJECT_MEMORY);
  1868. /* Start by reading up to 4K at a time. The target will throttle
  1869. this number down if necessary. */
  1870. while (1)
  1871. {
  1872. ULONGEST xfered_len;
  1873. enum target_xfer_status status;
  1874. buf.resize (buf_pos + chunk);
  1875. status = target_read_partial (ops, object, annex,
  1876. (gdb_byte *) &buf[buf_pos],
  1877. buf_pos, chunk,
  1878. &xfered_len);
  1879. if (status == TARGET_XFER_EOF)
  1880. {
  1881. /* Read all there was. */
  1882. buf.resize (buf_pos);
  1883. return buf;
  1884. }
  1885. else if (status != TARGET_XFER_OK)
  1886. {
  1887. /* An error occurred. */
  1888. return {};
  1889. }
  1890. buf_pos += xfered_len;
  1891. QUIT;
  1892. }
  1893. }
  1894. /* See target.h */
  1895. gdb::optional<gdb::byte_vector>
  1896. target_read_alloc (struct target_ops *ops, enum target_object object,
  1897. const char *annex)
  1898. {
  1899. return target_read_alloc_1<gdb_byte> (ops, object, annex);
  1900. }
  1901. /* See target.h. */
  1902. gdb::optional<gdb::char_vector>
  1903. target_read_stralloc (struct target_ops *ops, enum target_object object,
  1904. const char *annex)
  1905. {
  1906. gdb::optional<gdb::char_vector> buf
  1907. = target_read_alloc_1<char> (ops, object, annex);
  1908. if (!buf)
  1909. return {};
  1910. if (buf->empty () || buf->back () != '\0')
  1911. buf->push_back ('\0');
  1912. /* Check for embedded NUL bytes; but allow trailing NULs. */
  1913. for (auto it = std::find (buf->begin (), buf->end (), '\0');
  1914. it != buf->end (); it++)
  1915. if (*it != '\0')
  1916. {
  1917. warning (_("target object %d, annex %s, "
  1918. "contained unexpected null characters"),
  1919. (int) object, annex ? annex : "(none)");
  1920. break;
  1921. }
  1922. return buf;
  1923. }
  1924. /* Memory transfer methods. */
  1925. void
  1926. get_target_memory (struct target_ops *ops, CORE_ADDR addr, gdb_byte *buf,
  1927. LONGEST len)
  1928. {
  1929. /* This method is used to read from an alternate, non-current
  1930. target. This read must bypass the overlay support (as symbols
  1931. don't match this target), and GDB's internal cache (wrong cache
  1932. for this target). */
  1933. if (target_read (ops, TARGET_OBJECT_RAW_MEMORY, NULL, buf, addr, len)
  1934. != len)
  1935. memory_error (TARGET_XFER_E_IO, addr);
  1936. }
  1937. ULONGEST
  1938. get_target_memory_unsigned (struct target_ops *ops, CORE_ADDR addr,
  1939. int len, enum bfd_endian byte_order)
  1940. {
  1941. gdb_byte buf[sizeof (ULONGEST)];
  1942. gdb_assert (len <= sizeof (buf));
  1943. get_target_memory (ops, addr, buf, len);
  1944. return extract_unsigned_integer (buf, len, byte_order);
  1945. }
  1946. /* See target.h. */
  1947. int
  1948. target_insert_breakpoint (struct gdbarch *gdbarch,
  1949. struct bp_target_info *bp_tgt)
  1950. {
  1951. if (!may_insert_breakpoints)
  1952. {
  1953. warning (_("May not insert breakpoints"));
  1954. return 1;
  1955. }
  1956. target_ops *target = current_inferior ()->top_target ();
  1957. return target->insert_breakpoint (gdbarch, bp_tgt);
  1958. }
  1959. /* See target.h. */
  1960. int
  1961. target_remove_breakpoint (struct gdbarch *gdbarch,
  1962. struct bp_target_info *bp_tgt,
  1963. enum remove_bp_reason reason)
  1964. {
  1965. /* This is kind of a weird case to handle, but the permission might
  1966. have been changed after breakpoints were inserted - in which case
  1967. we should just take the user literally and assume that any
  1968. breakpoints should be left in place. */
  1969. if (!may_insert_breakpoints)
  1970. {
  1971. warning (_("May not remove breakpoints"));
  1972. return 1;
  1973. }
  1974. target_ops *target = current_inferior ()->top_target ();
  1975. return target->remove_breakpoint (gdbarch, bp_tgt, reason);
  1976. }
  1977. static void
  1978. info_target_command (const char *args, int from_tty)
  1979. {
  1980. int has_all_mem = 0;
  1981. if (current_program_space->symfile_object_file != NULL)
  1982. {
  1983. objfile *objf = current_program_space->symfile_object_file;
  1984. gdb_printf (_("Symbols from \"%s\".\n"),
  1985. objfile_name (objf));
  1986. }
  1987. for (target_ops *t = current_inferior ()->top_target ();
  1988. t != NULL;
  1989. t = t->beneath ())
  1990. {
  1991. if (!t->has_memory ())
  1992. continue;
  1993. if ((int) (t->stratum ()) <= (int) dummy_stratum)
  1994. continue;
  1995. if (has_all_mem)
  1996. gdb_printf (_("\tWhile running this, "
  1997. "GDB does not access memory from...\n"));
  1998. gdb_printf ("%s:\n", t->longname ());
  1999. t->files_info ();
  2000. has_all_mem = t->has_all_memory ();
  2001. }
  2002. }
  2003. /* This function is called before any new inferior is created, e.g.
  2004. by running a program, attaching, or connecting to a target.
  2005. It cleans up any state from previous invocations which might
  2006. change between runs. This is a subset of what target_preopen
  2007. resets (things which might change between targets). */
  2008. void
  2009. target_pre_inferior (int from_tty)
  2010. {
  2011. /* Clear out solib state. Otherwise the solib state of the previous
  2012. inferior might have survived and is entirely wrong for the new
  2013. target. This has been observed on GNU/Linux using glibc 2.3. How
  2014. to reproduce:
  2015. bash$ ./foo&
  2016. [1] 4711
  2017. bash$ ./foo&
  2018. [1] 4712
  2019. bash$ gdb ./foo
  2020. [...]
  2021. (gdb) attach 4711
  2022. (gdb) detach
  2023. (gdb) attach 4712
  2024. Cannot access memory at address 0xdeadbeef
  2025. */
  2026. /* In some OSs, the shared library list is the same/global/shared
  2027. across inferiors. If code is shared between processes, so are
  2028. memory regions and features. */
  2029. if (!gdbarch_has_global_solist (target_gdbarch ()))
  2030. {
  2031. no_shared_libraries (NULL, from_tty);
  2032. invalidate_target_mem_regions ();
  2033. target_clear_description ();
  2034. }
  2035. /* attach_flag may be set if the previous process associated with
  2036. the inferior was attached to. */
  2037. current_inferior ()->attach_flag = 0;
  2038. current_inferior ()->highest_thread_num = 0;
  2039. agent_capability_invalidate ();
  2040. }
  2041. /* This is to be called by the open routine before it does
  2042. anything. */
  2043. void
  2044. target_preopen (int from_tty)
  2045. {
  2046. dont_repeat ();
  2047. if (current_inferior ()->pid != 0)
  2048. {
  2049. if (!from_tty
  2050. || !target_has_execution ()
  2051. || query (_("A program is being debugged already. Kill it? ")))
  2052. {
  2053. /* Core inferiors actually should be detached, not
  2054. killed. */
  2055. if (target_has_execution ())
  2056. target_kill ();
  2057. else
  2058. target_detach (current_inferior (), 0);
  2059. }
  2060. else
  2061. error (_("Program not killed."));
  2062. }
  2063. /* Calling target_kill may remove the target from the stack. But if
  2064. it doesn't (which seems like a win for UDI), remove it now. */
  2065. /* Leave the exec target, though. The user may be switching from a
  2066. live process to a core of the same program. */
  2067. pop_all_targets_above (file_stratum);
  2068. target_pre_inferior (from_tty);
  2069. }
  2070. /* See target.h. */
  2071. void
  2072. target_detach (inferior *inf, int from_tty)
  2073. {
  2074. /* After we have detached, we will clear the register cache for this inferior
  2075. by calling registers_changed_ptid. We must save the pid_ptid before
  2076. detaching, as the target detach method will clear inf->pid. */
  2077. ptid_t save_pid_ptid = ptid_t (inf->pid);
  2078. /* As long as some to_detach implementations rely on the current_inferior
  2079. (either directly, or indirectly, like through target_gdbarch or by
  2080. reading memory), INF needs to be the current inferior. When that
  2081. requirement will become no longer true, then we can remove this
  2082. assertion. */
  2083. gdb_assert (inf == current_inferior ());
  2084. prepare_for_detach ();
  2085. /* Hold a strong reference because detaching may unpush the
  2086. target. */
  2087. auto proc_target_ref = target_ops_ref::new_reference (inf->process_target ());
  2088. current_inferior ()->top_target ()->detach (inf, from_tty);
  2089. process_stratum_target *proc_target
  2090. = as_process_stratum_target (proc_target_ref.get ());
  2091. registers_changed_ptid (proc_target, save_pid_ptid);
  2092. /* We have to ensure we have no frame cache left. Normally,
  2093. registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when
  2094. inferior_ptid matches save_pid_ptid, but in our case, it does not
  2095. call it, as inferior_ptid has been reset. */
  2096. reinit_frame_cache ();
  2097. }
  2098. void
  2099. target_disconnect (const char *args, int from_tty)
  2100. {
  2101. /* If we're in breakpoints-always-inserted mode or if breakpoints
  2102. are global across processes, we have to remove them before
  2103. disconnecting. */
  2104. remove_breakpoints ();
  2105. current_inferior ()->top_target ()->disconnect (args, from_tty);
  2106. }
  2107. /* See target/target.h. */
  2108. ptid_t
  2109. target_wait (ptid_t ptid, struct target_waitstatus *status,
  2110. target_wait_flags options)
  2111. {
  2112. target_ops *target = current_inferior ()->top_target ();
  2113. process_stratum_target *proc_target = current_inferior ()->process_target ();
  2114. gdb_assert (!proc_target->commit_resumed_state);
  2115. if (!target_can_async_p (target))
  2116. gdb_assert ((options & TARGET_WNOHANG) == 0);
  2117. try
  2118. {
  2119. gdb::observers::target_pre_wait.notify (ptid);
  2120. ptid_t event_ptid = target->wait (ptid, status, options);
  2121. gdb::observers::target_post_wait.notify (event_ptid);
  2122. return event_ptid;
  2123. }
  2124. catch (...)
  2125. {
  2126. gdb::observers::target_post_wait.notify (null_ptid);
  2127. throw;
  2128. }
  2129. }
  2130. /* See target.h. */
  2131. ptid_t
  2132. default_target_wait (struct target_ops *ops,
  2133. ptid_t ptid, struct target_waitstatus *status,
  2134. target_wait_flags options)
  2135. {
  2136. status->set_ignore ();
  2137. return minus_one_ptid;
  2138. }
  2139. std::string
  2140. target_pid_to_str (ptid_t ptid)
  2141. {
  2142. return current_inferior ()->top_target ()->pid_to_str (ptid);
  2143. }
  2144. const char *
  2145. target_thread_name (struct thread_info *info)
  2146. {
  2147. gdb_assert (info->inf == current_inferior ());
  2148. return current_inferior ()->top_target ()->thread_name (info);
  2149. }
  2150. struct thread_info *
  2151. target_thread_handle_to_thread_info (const gdb_byte *thread_handle,
  2152. int handle_len,
  2153. struct inferior *inf)
  2154. {
  2155. target_ops *target = current_inferior ()->top_target ();
  2156. return target->thread_handle_to_thread_info (thread_handle, handle_len, inf);
  2157. }
  2158. /* See target.h. */
  2159. gdb::byte_vector
  2160. target_thread_info_to_thread_handle (struct thread_info *tip)
  2161. {
  2162. target_ops *target = current_inferior ()->top_target ();
  2163. return target->thread_info_to_thread_handle (tip);
  2164. }
  2165. void
  2166. target_resume (ptid_t ptid, int step, enum gdb_signal signal)
  2167. {
  2168. process_stratum_target *curr_target = current_inferior ()->process_target ();
  2169. gdb_assert (!curr_target->commit_resumed_state);
  2170. target_dcache_invalidate ();
  2171. current_inferior ()->top_target ()->resume (ptid, step, signal);
  2172. registers_changed_ptid (curr_target, ptid);
  2173. /* We only set the internal executing state here. The user/frontend
  2174. running state is set at a higher level. This also clears the
  2175. thread's stop_pc as side effect. */
  2176. set_executing (curr_target, ptid, true);
  2177. clear_inline_frame_state (curr_target, ptid);
  2178. if (target_can_async_p ())
  2179. target_async (1);
  2180. }
  2181. /* See target.h. */
  2182. void
  2183. target_commit_resumed ()
  2184. {
  2185. gdb_assert (current_inferior ()->process_target ()->commit_resumed_state);
  2186. current_inferior ()->top_target ()->commit_resumed ();
  2187. }
  2188. /* See target.h. */
  2189. bool
  2190. target_has_pending_events ()
  2191. {
  2192. return current_inferior ()->top_target ()->has_pending_events ();
  2193. }
  2194. void
  2195. target_pass_signals (gdb::array_view<const unsigned char> pass_signals)
  2196. {
  2197. current_inferior ()->top_target ()->pass_signals (pass_signals);
  2198. }
  2199. void
  2200. target_program_signals (gdb::array_view<const unsigned char> program_signals)
  2201. {
  2202. current_inferior ()->top_target ()->program_signals (program_signals);
  2203. }
  2204. static void
  2205. default_follow_fork (struct target_ops *self, inferior *child_inf,
  2206. ptid_t child_ptid, target_waitkind fork_kind,
  2207. bool follow_child, bool detach_fork)
  2208. {
  2209. /* Some target returned a fork event, but did not know how to follow it. */
  2210. internal_error (__FILE__, __LINE__,
  2211. _("could not find a target to follow fork"));
  2212. }
  2213. /* See target.h. */
  2214. void
  2215. target_follow_fork (inferior *child_inf, ptid_t child_ptid,
  2216. target_waitkind fork_kind, bool follow_child,
  2217. bool detach_fork)
  2218. {
  2219. target_ops *target = current_inferior ()->top_target ();
  2220. /* Check consistency between CHILD_INF, CHILD_PTID, FOLLOW_CHILD and
  2221. DETACH_FORK. */
  2222. if (child_inf != nullptr)
  2223. {
  2224. gdb_assert (follow_child || !detach_fork);
  2225. gdb_assert (child_inf->pid == child_ptid.pid ());
  2226. }
  2227. else
  2228. gdb_assert (!follow_child && detach_fork);
  2229. return target->follow_fork (child_inf, child_ptid, fork_kind, follow_child,
  2230. detach_fork);
  2231. }
  2232. /* See target.h. */
  2233. void
  2234. target_follow_exec (inferior *follow_inf, ptid_t ptid,
  2235. const char *execd_pathname)
  2236. {
  2237. current_inferior ()->top_target ()->follow_exec (follow_inf, ptid,
  2238. execd_pathname);
  2239. }
  2240. static void
  2241. default_mourn_inferior (struct target_ops *self)
  2242. {
  2243. internal_error (__FILE__, __LINE__,
  2244. _("could not find a target to follow mourn inferior"));
  2245. }
  2246. void
  2247. target_mourn_inferior (ptid_t ptid)
  2248. {
  2249. gdb_assert (ptid.pid () == inferior_ptid.pid ());
  2250. current_inferior ()->top_target ()->mourn_inferior ();
  2251. /* We no longer need to keep handles on any of the object files.
  2252. Make sure to release them to avoid unnecessarily locking any
  2253. of them while we're not actually debugging. */
  2254. bfd_cache_close_all ();
  2255. }
  2256. /* Look for a target which can describe architectural features, starting
  2257. from TARGET. If we find one, return its description. */
  2258. const struct target_desc *
  2259. target_read_description (struct target_ops *target)
  2260. {
  2261. return target->read_description ();
  2262. }
  2263. /* Default implementation of memory-searching. */
  2264. static int
  2265. default_search_memory (struct target_ops *self,
  2266. CORE_ADDR start_addr, ULONGEST search_space_len,
  2267. const gdb_byte *pattern, ULONGEST pattern_len,
  2268. CORE_ADDR *found_addrp)
  2269. {
  2270. auto read_memory = [=] (CORE_ADDR addr, gdb_byte *result, size_t len)
  2271. {
  2272. return target_read (current_inferior ()->top_target (),
  2273. TARGET_OBJECT_MEMORY, NULL,
  2274. result, addr, len) == len;
  2275. };
  2276. /* Start over from the top of the target stack. */
  2277. return simple_search_memory (read_memory, start_addr, search_space_len,
  2278. pattern, pattern_len, found_addrp);
  2279. }
  2280. /* Search SEARCH_SPACE_LEN bytes beginning at START_ADDR for the
  2281. sequence of bytes in PATTERN with length PATTERN_LEN.
  2282. The result is 1 if found, 0 if not found, and -1 if there was an error
  2283. requiring halting of the search (e.g. memory read error).
  2284. If the pattern is found the address is recorded in FOUND_ADDRP. */
  2285. int
  2286. target_search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
  2287. const gdb_byte *pattern, ULONGEST pattern_len,
  2288. CORE_ADDR *found_addrp)
  2289. {
  2290. target_ops *target = current_inferior ()->top_target ();
  2291. return target->search_memory (start_addr, search_space_len, pattern,
  2292. pattern_len, found_addrp);
  2293. }
  2294. /* Look through the currently pushed targets. If none of them will
  2295. be able to restart the currently running process, issue an error
  2296. message. */
  2297. void
  2298. target_require_runnable (void)
  2299. {
  2300. for (target_ops *t = current_inferior ()->top_target ();
  2301. t != NULL;
  2302. t = t->beneath ())
  2303. {
  2304. /* If this target knows how to create a new program, then
  2305. assume we will still be able to after killing the current
  2306. one. Either killing and mourning will not pop T, or else
  2307. find_default_run_target will find it again. */
  2308. if (t->can_create_inferior ())
  2309. return;
  2310. /* Do not worry about targets at certain strata that can not
  2311. create inferiors. Assume they will be pushed again if
  2312. necessary, and continue to the process_stratum. */
  2313. if (t->stratum () > process_stratum)
  2314. continue;
  2315. error (_("The \"%s\" target does not support \"run\". "
  2316. "Try \"help target\" or \"continue\"."),
  2317. t->shortname ());
  2318. }
  2319. /* This function is only called if the target is running. In that
  2320. case there should have been a process_stratum target and it
  2321. should either know how to create inferiors, or not... */
  2322. internal_error (__FILE__, __LINE__, _("No targets found"));
  2323. }
  2324. /* Whether GDB is allowed to fall back to the default run target for
  2325. "run", "attach", etc. when no target is connected yet. */
  2326. static bool auto_connect_native_target = true;
  2327. static void
  2328. show_auto_connect_native_target (struct ui_file *file, int from_tty,
  2329. struct cmd_list_element *c, const char *value)
  2330. {
  2331. gdb_printf (file,
  2332. _("Whether GDB may automatically connect to the "
  2333. "native target is %s.\n"),
  2334. value);
  2335. }
  2336. /* A pointer to the target that can respond to "run" or "attach".
  2337. Native targets are always singletons and instantiated early at GDB
  2338. startup. */
  2339. static target_ops *the_native_target;
  2340. /* See target.h. */
  2341. void
  2342. set_native_target (target_ops *target)
  2343. {
  2344. if (the_native_target != NULL)
  2345. internal_error (__FILE__, __LINE__,
  2346. _("native target already set (\"%s\")."),
  2347. the_native_target->longname ());
  2348. the_native_target = target;
  2349. }
  2350. /* See target.h. */
  2351. target_ops *
  2352. get_native_target ()
  2353. {
  2354. return the_native_target;
  2355. }
  2356. /* Look through the list of possible targets for a target that can
  2357. execute a run or attach command without any other data. This is
  2358. used to locate the default process stratum.
  2359. If DO_MESG is not NULL, the result is always valid (error() is
  2360. called for errors); else, return NULL on error. */
  2361. static struct target_ops *
  2362. find_default_run_target (const char *do_mesg)
  2363. {
  2364. if (auto_connect_native_target && the_native_target != NULL)
  2365. return the_native_target;
  2366. if (do_mesg != NULL)
  2367. error (_("Don't know how to %s. Try \"help target\"."), do_mesg);
  2368. return NULL;
  2369. }
  2370. /* See target.h. */
  2371. struct target_ops *
  2372. find_attach_target (void)
  2373. {
  2374. /* If a target on the current stack can attach, use it. */
  2375. for (target_ops *t = current_inferior ()->top_target ();
  2376. t != NULL;
  2377. t = t->beneath ())
  2378. {
  2379. if (t->can_attach ())
  2380. return t;
  2381. }
  2382. /* Otherwise, use the default run target for attaching. */
  2383. return find_default_run_target ("attach");
  2384. }
  2385. /* See target.h. */
  2386. struct target_ops *
  2387. find_run_target (void)
  2388. {
  2389. /* If a target on the current stack can run, use it. */
  2390. for (target_ops *t = current_inferior ()->top_target ();
  2391. t != NULL;
  2392. t = t->beneath ())
  2393. {
  2394. if (t->can_create_inferior ())
  2395. return t;
  2396. }
  2397. /* Otherwise, use the default run target. */
  2398. return find_default_run_target ("run");
  2399. }
  2400. bool
  2401. target_ops::info_proc (const char *args, enum info_proc_what what)
  2402. {
  2403. return false;
  2404. }
  2405. /* Implement the "info proc" command. */
  2406. int
  2407. target_info_proc (const char *args, enum info_proc_what what)
  2408. {
  2409. struct target_ops *t;
  2410. /* If we're already connected to something that can get us OS
  2411. related data, use it. Otherwise, try using the native
  2412. target. */
  2413. t = find_target_at (process_stratum);
  2414. if (t == NULL)
  2415. t = find_default_run_target (NULL);
  2416. for (; t != NULL; t = t->beneath ())
  2417. {
  2418. if (t->info_proc (args, what))
  2419. {
  2420. if (targetdebug)
  2421. gdb_printf (gdb_stdlog,
  2422. "target_info_proc (\"%s\", %d)\n", args, what);
  2423. return 1;
  2424. }
  2425. }
  2426. return 0;
  2427. }
  2428. static int
  2429. find_default_supports_disable_randomization (struct target_ops *self)
  2430. {
  2431. struct target_ops *t;
  2432. t = find_default_run_target (NULL);
  2433. if (t != NULL)
  2434. return t->supports_disable_randomization ();
  2435. return 0;
  2436. }
  2437. int
  2438. target_supports_disable_randomization (void)
  2439. {
  2440. return current_inferior ()->top_target ()->supports_disable_randomization ();
  2441. }
  2442. /* See target/target.h. */
  2443. int
  2444. target_supports_multi_process (void)
  2445. {
  2446. return current_inferior ()->top_target ()->supports_multi_process ();
  2447. }
  2448. /* See target.h. */
  2449. gdb::optional<gdb::char_vector>
  2450. target_get_osdata (const char *type)
  2451. {
  2452. struct target_ops *t;
  2453. /* If we're already connected to something that can get us OS
  2454. related data, use it. Otherwise, try using the native
  2455. target. */
  2456. t = find_target_at (process_stratum);
  2457. if (t == NULL)
  2458. t = find_default_run_target ("get OS data");
  2459. if (!t)
  2460. return {};
  2461. return target_read_stralloc (t, TARGET_OBJECT_OSDATA, type);
  2462. }
  2463. /* Determine the current address space of thread PTID. */
  2464. struct address_space *
  2465. target_thread_address_space (ptid_t ptid)
  2466. {
  2467. struct address_space *aspace;
  2468. aspace = current_inferior ()->top_target ()->thread_address_space (ptid);
  2469. gdb_assert (aspace != NULL);
  2470. return aspace;
  2471. }
  2472. /* See target.h. */
  2473. target_ops *
  2474. target_ops::beneath () const
  2475. {
  2476. return current_inferior ()->find_target_beneath (this);
  2477. }
  2478. void
  2479. target_ops::close ()
  2480. {
  2481. }
  2482. bool
  2483. target_ops::can_attach ()
  2484. {
  2485. return 0;
  2486. }
  2487. void
  2488. target_ops::attach (const char *, int)
  2489. {
  2490. gdb_assert_not_reached ("target_ops::attach called");
  2491. }
  2492. bool
  2493. target_ops::can_create_inferior ()
  2494. {
  2495. return 0;
  2496. }
  2497. void
  2498. target_ops::create_inferior (const char *, const std::string &,
  2499. char **, int)
  2500. {
  2501. gdb_assert_not_reached ("target_ops::create_inferior called");
  2502. }
  2503. bool
  2504. target_ops::can_run ()
  2505. {
  2506. return false;
  2507. }
  2508. int
  2509. target_can_run ()
  2510. {
  2511. for (target_ops *t = current_inferior ()->top_target ();
  2512. t != NULL;
  2513. t = t->beneath ())
  2514. {
  2515. if (t->can_run ())
  2516. return 1;
  2517. }
  2518. return 0;
  2519. }
  2520. /* Target file operations. */
  2521. static struct target_ops *
  2522. default_fileio_target (void)
  2523. {
  2524. struct target_ops *t;
  2525. /* If we're already connected to something that can perform
  2526. file I/O, use it. Otherwise, try using the native target. */
  2527. t = find_target_at (process_stratum);
  2528. if (t != NULL)
  2529. return t;
  2530. return find_default_run_target ("file I/O");
  2531. }
  2532. /* File handle for target file operations. */
  2533. struct fileio_fh_t
  2534. {
  2535. /* The target on which this file is open. NULL if the target is
  2536. meanwhile closed while the handle is open. */
  2537. target_ops *target;
  2538. /* The file descriptor on the target. */
  2539. int target_fd;
  2540. /* Check whether this fileio_fh_t represents a closed file. */
  2541. bool is_closed ()
  2542. {
  2543. return target_fd < 0;
  2544. }
  2545. };
  2546. /* Vector of currently open file handles. The value returned by
  2547. target_fileio_open and passed as the FD argument to other
  2548. target_fileio_* functions is an index into this vector. This
  2549. vector's entries are never freed; instead, files are marked as
  2550. closed, and the handle becomes available for reuse. */
  2551. static std::vector<fileio_fh_t> fileio_fhandles;
  2552. /* Index into fileio_fhandles of the lowest handle that might be
  2553. closed. This permits handle reuse without searching the whole
  2554. list each time a new file is opened. */
  2555. static int lowest_closed_fd;
  2556. /* See target.h. */
  2557. void
  2558. fileio_handles_invalidate_target (target_ops *targ)
  2559. {
  2560. for (fileio_fh_t &fh : fileio_fhandles)
  2561. if (fh.target == targ)
  2562. fh.target = NULL;
  2563. }
  2564. /* Acquire a target fileio file descriptor. */
  2565. static int
  2566. acquire_fileio_fd (target_ops *target, int target_fd)
  2567. {
  2568. /* Search for closed handles to reuse. */
  2569. for (; lowest_closed_fd < fileio_fhandles.size (); lowest_closed_fd++)
  2570. {
  2571. fileio_fh_t &fh = fileio_fhandles[lowest_closed_fd];
  2572. if (fh.is_closed ())
  2573. break;
  2574. }
  2575. /* Push a new handle if no closed handles were found. */
  2576. if (lowest_closed_fd == fileio_fhandles.size ())
  2577. fileio_fhandles.push_back (fileio_fh_t {target, target_fd});
  2578. else
  2579. fileio_fhandles[lowest_closed_fd] = {target, target_fd};
  2580. /* Should no longer be marked closed. */
  2581. gdb_assert (!fileio_fhandles[lowest_closed_fd].is_closed ());
  2582. /* Return its index, and start the next lookup at
  2583. the next index. */
  2584. return lowest_closed_fd++;
  2585. }
  2586. /* Release a target fileio file descriptor. */
  2587. static void
  2588. release_fileio_fd (int fd, fileio_fh_t *fh)
  2589. {
  2590. fh->target_fd = -1;
  2591. lowest_closed_fd = std::min (lowest_closed_fd, fd);
  2592. }
  2593. /* Return a pointer to the fileio_fhandle_t corresponding to FD. */
  2594. static fileio_fh_t *
  2595. fileio_fd_to_fh (int fd)
  2596. {
  2597. return &fileio_fhandles[fd];
  2598. }
  2599. /* Default implementations of file i/o methods. We don't want these
  2600. to delegate automatically, because we need to know which target
  2601. supported the method, in order to call it directly from within
  2602. pread/pwrite, etc. */
  2603. int
  2604. target_ops::fileio_open (struct inferior *inf, const char *filename,
  2605. int flags, int mode, int warn_if_slow,
  2606. int *target_errno)
  2607. {
  2608. *target_errno = FILEIO_ENOSYS;
  2609. return -1;
  2610. }
  2611. int
  2612. target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
  2613. ULONGEST offset, int *target_errno)
  2614. {
  2615. *target_errno = FILEIO_ENOSYS;
  2616. return -1;
  2617. }
  2618. int
  2619. target_ops::fileio_pread (int fd, gdb_byte *read_buf, int len,
  2620. ULONGEST offset, int *target_errno)
  2621. {
  2622. *target_errno = FILEIO_ENOSYS;
  2623. return -1;
  2624. }
  2625. int
  2626. target_ops::fileio_fstat (int fd, struct stat *sb, int *target_errno)
  2627. {
  2628. *target_errno = FILEIO_ENOSYS;
  2629. return -1;
  2630. }
  2631. int
  2632. target_ops::fileio_close (int fd, int *target_errno)
  2633. {
  2634. *target_errno = FILEIO_ENOSYS;
  2635. return -1;
  2636. }
  2637. int
  2638. target_ops::fileio_unlink (struct inferior *inf, const char *filename,
  2639. int *target_errno)
  2640. {
  2641. *target_errno = FILEIO_ENOSYS;
  2642. return -1;
  2643. }
  2644. gdb::optional<std::string>
  2645. target_ops::fileio_readlink (struct inferior *inf, const char *filename,
  2646. int *target_errno)
  2647. {
  2648. *target_errno = FILEIO_ENOSYS;
  2649. return {};
  2650. }
  2651. /* See target.h. */
  2652. int
  2653. target_fileio_open (struct inferior *inf, const char *filename,
  2654. int flags, int mode, bool warn_if_slow, int *target_errno)
  2655. {
  2656. for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
  2657. {
  2658. int fd = t->fileio_open (inf, filename, flags, mode,
  2659. warn_if_slow, target_errno);
  2660. if (fd == -1 && *target_errno == FILEIO_ENOSYS)
  2661. continue;
  2662. if (fd < 0)
  2663. fd = -1;
  2664. else
  2665. fd = acquire_fileio_fd (t, fd);
  2666. if (targetdebug)
  2667. gdb_printf (gdb_stdlog,
  2668. "target_fileio_open (%d,%s,0x%x,0%o,%d)"
  2669. " = %d (%d)\n",
  2670. inf == NULL ? 0 : inf->num,
  2671. filename, flags, mode,
  2672. warn_if_slow, fd,
  2673. fd != -1 ? 0 : *target_errno);
  2674. return fd;
  2675. }
  2676. *target_errno = FILEIO_ENOSYS;
  2677. return -1;
  2678. }
  2679. /* See target.h. */
  2680. int
  2681. target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
  2682. ULONGEST offset, int *target_errno)
  2683. {
  2684. fileio_fh_t *fh = fileio_fd_to_fh (fd);
  2685. int ret = -1;
  2686. if (fh->is_closed ())
  2687. *target_errno = EBADF;
  2688. else if (fh->target == NULL)
  2689. *target_errno = EIO;
  2690. else
  2691. ret = fh->target->fileio_pwrite (fh->target_fd, write_buf,
  2692. len, offset, target_errno);
  2693. if (targetdebug)
  2694. gdb_printf (gdb_stdlog,
  2695. "target_fileio_pwrite (%d,...,%d,%s) "
  2696. "= %d (%d)\n",
  2697. fd, len, pulongest (offset),
  2698. ret, ret != -1 ? 0 : *target_errno);
  2699. return ret;
  2700. }
  2701. /* See target.h. */
  2702. int
  2703. target_fileio_pread (int fd, gdb_byte *read_buf, int len,
  2704. ULONGEST offset, int *target_errno)
  2705. {
  2706. fileio_fh_t *fh = fileio_fd_to_fh (fd);
  2707. int ret = -1;
  2708. if (fh->is_closed ())
  2709. *target_errno = EBADF;
  2710. else if (fh->target == NULL)
  2711. *target_errno = EIO;
  2712. else
  2713. ret = fh->target->fileio_pread (fh->target_fd, read_buf,
  2714. len, offset, target_errno);
  2715. if (targetdebug)
  2716. gdb_printf (gdb_stdlog,
  2717. "target_fileio_pread (%d,...,%d,%s) "
  2718. "= %d (%d)\n",
  2719. fd, len, pulongest (offset),
  2720. ret, ret != -1 ? 0 : *target_errno);
  2721. return ret;
  2722. }
  2723. /* See target.h. */
  2724. int
  2725. target_fileio_fstat (int fd, struct stat *sb, int *target_errno)
  2726. {
  2727. fileio_fh_t *fh = fileio_fd_to_fh (fd);
  2728. int ret = -1;
  2729. if (fh->is_closed ())
  2730. *target_errno = EBADF;
  2731. else if (fh->target == NULL)
  2732. *target_errno = EIO;
  2733. else
  2734. ret = fh->target->fileio_fstat (fh->target_fd, sb, target_errno);
  2735. if (targetdebug)
  2736. gdb_printf (gdb_stdlog,
  2737. "target_fileio_fstat (%d) = %d (%d)\n",
  2738. fd, ret, ret != -1 ? 0 : *target_errno);
  2739. return ret;
  2740. }
  2741. /* See target.h. */
  2742. int
  2743. target_fileio_close (int fd, int *target_errno)
  2744. {
  2745. fileio_fh_t *fh = fileio_fd_to_fh (fd);
  2746. int ret = -1;
  2747. if (fh->is_closed ())
  2748. *target_errno = EBADF;
  2749. else
  2750. {
  2751. if (fh->target != NULL)
  2752. ret = fh->target->fileio_close (fh->target_fd,
  2753. target_errno);
  2754. else
  2755. ret = 0;
  2756. release_fileio_fd (fd, fh);
  2757. }
  2758. if (targetdebug)
  2759. gdb_printf (gdb_stdlog,
  2760. "target_fileio_close (%d) = %d (%d)\n",
  2761. fd, ret, ret != -1 ? 0 : *target_errno);
  2762. return ret;
  2763. }
  2764. /* See target.h. */
  2765. int
  2766. target_fileio_unlink (struct inferior *inf, const char *filename,
  2767. int *target_errno)
  2768. {
  2769. for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
  2770. {
  2771. int ret = t->fileio_unlink (inf, filename, target_errno);
  2772. if (ret == -1 && *target_errno == FILEIO_ENOSYS)
  2773. continue;
  2774. if (targetdebug)
  2775. gdb_printf (gdb_stdlog,
  2776. "target_fileio_unlink (%d,%s)"
  2777. " = %d (%d)\n",
  2778. inf == NULL ? 0 : inf->num, filename,
  2779. ret, ret != -1 ? 0 : *target_errno);
  2780. return ret;
  2781. }
  2782. *target_errno = FILEIO_ENOSYS;
  2783. return -1;
  2784. }
  2785. /* See target.h. */
  2786. gdb::optional<std::string>
  2787. target_fileio_readlink (struct inferior *inf, const char *filename,
  2788. int *target_errno)
  2789. {
  2790. for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
  2791. {
  2792. gdb::optional<std::string> ret
  2793. = t->fileio_readlink (inf, filename, target_errno);
  2794. if (!ret.has_value () && *target_errno == FILEIO_ENOSYS)
  2795. continue;
  2796. if (targetdebug)
  2797. gdb_printf (gdb_stdlog,
  2798. "target_fileio_readlink (%d,%s)"
  2799. " = %s (%d)\n",
  2800. inf == NULL ? 0 : inf->num,
  2801. filename, ret ? ret->c_str () : "(nil)",
  2802. ret ? 0 : *target_errno);
  2803. return ret;
  2804. }
  2805. *target_errno = FILEIO_ENOSYS;
  2806. return {};
  2807. }
  2808. /* Like scoped_fd, but specific to target fileio. */
  2809. class scoped_target_fd
  2810. {
  2811. public:
  2812. explicit scoped_target_fd (int fd) noexcept
  2813. : m_fd (fd)
  2814. {
  2815. }
  2816. ~scoped_target_fd ()
  2817. {
  2818. if (m_fd >= 0)
  2819. {
  2820. int target_errno;
  2821. target_fileio_close (m_fd, &target_errno);
  2822. }
  2823. }
  2824. DISABLE_COPY_AND_ASSIGN (scoped_target_fd);
  2825. int get () const noexcept
  2826. {
  2827. return m_fd;
  2828. }
  2829. private:
  2830. int m_fd;
  2831. };
  2832. /* Read target file FILENAME, in the filesystem as seen by INF. If
  2833. INF is NULL, use the filesystem seen by the debugger (GDB or, for
  2834. remote targets, the remote stub). Store the result in *BUF_P and
  2835. return the size of the transferred data. PADDING additional bytes
  2836. are available in *BUF_P. This is a helper function for
  2837. target_fileio_read_alloc; see the declaration of that function for
  2838. more information. */
  2839. static LONGEST
  2840. target_fileio_read_alloc_1 (struct inferior *inf, const char *filename,
  2841. gdb_byte **buf_p, int padding)
  2842. {
  2843. size_t buf_alloc, buf_pos;
  2844. gdb_byte *buf;
  2845. LONGEST n;
  2846. int target_errno;
  2847. scoped_target_fd fd (target_fileio_open (inf, filename, FILEIO_O_RDONLY,
  2848. 0700, false, &target_errno));
  2849. if (fd.get () == -1)
  2850. return -1;
  2851. /* Start by reading up to 4K at a time. The target will throttle
  2852. this number down if necessary. */
  2853. buf_alloc = 4096;
  2854. buf = (gdb_byte *) xmalloc (buf_alloc);
  2855. buf_pos = 0;
  2856. while (1)
  2857. {
  2858. n = target_fileio_pread (fd.get (), &buf[buf_pos],
  2859. buf_alloc - buf_pos - padding, buf_pos,
  2860. &target_errno);
  2861. if (n < 0)
  2862. {
  2863. /* An error occurred. */
  2864. xfree (buf);
  2865. return -1;
  2866. }
  2867. else if (n == 0)
  2868. {
  2869. /* Read all there was. */
  2870. if (buf_pos == 0)
  2871. xfree (buf);
  2872. else
  2873. *buf_p = buf;
  2874. return buf_pos;
  2875. }
  2876. buf_pos += n;
  2877. /* If the buffer is filling up, expand it. */
  2878. if (buf_alloc < buf_pos * 2)
  2879. {
  2880. buf_alloc *= 2;
  2881. buf = (gdb_byte *) xrealloc (buf, buf_alloc);
  2882. }
  2883. QUIT;
  2884. }
  2885. }
  2886. /* See target.h. */
  2887. LONGEST
  2888. target_fileio_read_alloc (struct inferior *inf, const char *filename,
  2889. gdb_byte **buf_p)
  2890. {
  2891. return target_fileio_read_alloc_1 (inf, filename, buf_p, 0);
  2892. }
  2893. /* See target.h. */
  2894. gdb::unique_xmalloc_ptr<char>
  2895. target_fileio_read_stralloc (struct inferior *inf, const char *filename)
  2896. {
  2897. gdb_byte *buffer;
  2898. char *bufstr;
  2899. LONGEST i, transferred;
  2900. transferred = target_fileio_read_alloc_1 (inf, filename, &buffer, 1);
  2901. bufstr = (char *) buffer;
  2902. if (transferred < 0)
  2903. return gdb::unique_xmalloc_ptr<char> (nullptr);
  2904. if (transferred == 0)
  2905. return make_unique_xstrdup ("");
  2906. bufstr[transferred] = 0;
  2907. /* Check for embedded NUL bytes; but allow trailing NULs. */
  2908. for (i = strlen (bufstr); i < transferred; i++)
  2909. if (bufstr[i] != 0)
  2910. {
  2911. warning (_("target file %s "
  2912. "contained unexpected null characters"),
  2913. filename);
  2914. break;
  2915. }
  2916. return gdb::unique_xmalloc_ptr<char> (bufstr);
  2917. }
  2918. static int
  2919. default_region_ok_for_hw_watchpoint (struct target_ops *self,
  2920. CORE_ADDR addr, int len)
  2921. {
  2922. return (len <= gdbarch_ptr_bit (target_gdbarch ()) / TARGET_CHAR_BIT);
  2923. }
  2924. static int
  2925. default_watchpoint_addr_within_range (struct target_ops *target,
  2926. CORE_ADDR addr,
  2927. CORE_ADDR start, int length)
  2928. {
  2929. return addr >= start && addr < start + length;
  2930. }
  2931. /* See target.h. */
  2932. target_ops *
  2933. target_stack::find_beneath (const target_ops *t) const
  2934. {
  2935. /* Look for a non-empty slot at stratum levels beneath T's. */
  2936. for (int stratum = t->stratum () - 1; stratum >= 0; --stratum)
  2937. if (m_stack[stratum] != NULL)
  2938. return m_stack[stratum];
  2939. return NULL;
  2940. }
  2941. /* See target.h. */
  2942. struct target_ops *
  2943. find_target_at (enum strata stratum)
  2944. {
  2945. return current_inferior ()->target_at (stratum);
  2946. }
  2947. /* See target.h */
  2948. void
  2949. target_announce_detach (int from_tty)
  2950. {
  2951. pid_t pid;
  2952. const char *exec_file;
  2953. if (!from_tty)
  2954. return;
  2955. pid = inferior_ptid.pid ();
  2956. exec_file = get_exec_file (0);
  2957. if (exec_file == nullptr)
  2958. gdb_printf ("Detaching from pid %s\n",
  2959. target_pid_to_str (ptid_t (pid)).c_str ());
  2960. else
  2961. gdb_printf (_("Detaching from program: %s, %s\n"), exec_file,
  2962. target_pid_to_str (ptid_t (pid)).c_str ());
  2963. }
  2964. /* See target.h */
  2965. void
  2966. target_announce_attach (int from_tty, int pid)
  2967. {
  2968. if (!from_tty)
  2969. return;
  2970. const char *exec_file = get_exec_file (0);
  2971. if (exec_file != nullptr)
  2972. gdb_printf ("Attaching to program: %s, %s\n", exec_file,
  2973. target_pid_to_str (ptid_t (pid)).c_str ());
  2974. else
  2975. gdb_printf ("Attaching to %s\n",
  2976. target_pid_to_str (ptid_t (pid)).c_str ());
  2977. }
  2978. /* The inferior process has died. Long live the inferior! */
  2979. void
  2980. generic_mourn_inferior (void)
  2981. {
  2982. inferior *inf = current_inferior ();
  2983. switch_to_no_thread ();
  2984. /* Mark breakpoints uninserted in case something tries to delete a
  2985. breakpoint while we delete the inferior's threads (which would
  2986. fail, since the inferior is long gone). */
  2987. mark_breakpoints_out ();
  2988. if (inf->pid != 0)
  2989. exit_inferior (inf);
  2990. /* Note this wipes step-resume breakpoints, so needs to be done
  2991. after exit_inferior, which ends up referencing the step-resume
  2992. breakpoints through clear_thread_inferior_resources. */
  2993. breakpoint_init_inferior (inf_exited);
  2994. registers_changed ();
  2995. reopen_exec_file ();
  2996. reinit_frame_cache ();
  2997. if (deprecated_detach_hook)
  2998. deprecated_detach_hook ();
  2999. }
  3000. /* Convert a normal process ID to a string. Returns the string in a
  3001. static buffer. */
  3002. std::string
  3003. normal_pid_to_str (ptid_t ptid)
  3004. {
  3005. return string_printf ("process %d", ptid.pid ());
  3006. }
  3007. static std::string
  3008. default_pid_to_str (struct target_ops *ops, ptid_t ptid)
  3009. {
  3010. return normal_pid_to_str (ptid);
  3011. }
  3012. /* Error-catcher for target_find_memory_regions. */
  3013. static int
  3014. dummy_find_memory_regions (struct target_ops *self,
  3015. find_memory_region_ftype ignore1, void *ignore2)
  3016. {
  3017. error (_("Command not implemented for this target."));
  3018. return 0;
  3019. }
  3020. /* Error-catcher for target_make_corefile_notes. */
  3021. static gdb::unique_xmalloc_ptr<char>
  3022. dummy_make_corefile_notes (struct target_ops *self,
  3023. bfd *ignore1, int *ignore2)
  3024. {
  3025. error (_("Command not implemented for this target."));
  3026. return NULL;
  3027. }
  3028. #include "target-delegates.c"
  3029. /* The initial current target, so that there is always a semi-valid
  3030. current target. */
  3031. static dummy_target the_dummy_target;
  3032. /* See target.h. */
  3033. target_ops *
  3034. get_dummy_target ()
  3035. {
  3036. return &the_dummy_target;
  3037. }
  3038. static const target_info dummy_target_info = {
  3039. "None",
  3040. N_("None"),
  3041. ""
  3042. };
  3043. strata
  3044. dummy_target::stratum () const
  3045. {
  3046. return dummy_stratum;
  3047. }
  3048. strata
  3049. debug_target::stratum () const
  3050. {
  3051. return debug_stratum;
  3052. }
  3053. const target_info &
  3054. dummy_target::info () const
  3055. {
  3056. return dummy_target_info;
  3057. }
  3058. const target_info &
  3059. debug_target::info () const
  3060. {
  3061. return beneath ()->info ();
  3062. }
  3063. void
  3064. target_close (struct target_ops *targ)
  3065. {
  3066. for (inferior *inf : all_inferiors ())
  3067. gdb_assert (!inf->target_is_pushed (targ));
  3068. fileio_handles_invalidate_target (targ);
  3069. targ->close ();
  3070. if (targetdebug)
  3071. gdb_printf (gdb_stdlog, "target_close ()\n");
  3072. }
  3073. int
  3074. target_thread_alive (ptid_t ptid)
  3075. {
  3076. return current_inferior ()->top_target ()->thread_alive (ptid);
  3077. }
  3078. void
  3079. target_update_thread_list (void)
  3080. {
  3081. current_inferior ()->top_target ()->update_thread_list ();
  3082. }
  3083. void
  3084. target_stop (ptid_t ptid)
  3085. {
  3086. process_stratum_target *proc_target = current_inferior ()->process_target ();
  3087. gdb_assert (!proc_target->commit_resumed_state);
  3088. if (!may_stop)
  3089. {
  3090. warning (_("May not interrupt or stop the target, ignoring attempt"));
  3091. return;
  3092. }
  3093. current_inferior ()->top_target ()->stop (ptid);
  3094. }
  3095. void
  3096. target_interrupt ()
  3097. {
  3098. if (!may_stop)
  3099. {
  3100. warning (_("May not interrupt or stop the target, ignoring attempt"));
  3101. return;
  3102. }
  3103. current_inferior ()->top_target ()->interrupt ();
  3104. }
  3105. /* See target.h. */
  3106. void
  3107. target_pass_ctrlc (void)
  3108. {
  3109. /* Pass the Ctrl-C to the first target that has a thread
  3110. running. */
  3111. for (inferior *inf : all_inferiors ())
  3112. {
  3113. target_ops *proc_target = inf->process_target ();
  3114. if (proc_target == NULL)
  3115. continue;
  3116. for (thread_info *thr : inf->non_exited_threads ())
  3117. {
  3118. /* A thread can be THREAD_STOPPED and executing, while
  3119. running an infcall. */
  3120. if (thr->state == THREAD_RUNNING || thr->executing ())
  3121. {
  3122. /* We can get here quite deep in target layers. Avoid
  3123. switching thread context or anything that would
  3124. communicate with the target (e.g., to fetch
  3125. registers), or flushing e.g., the frame cache. We
  3126. just switch inferior in order to be able to call
  3127. through the target_stack. */
  3128. scoped_restore_current_inferior restore_inferior;
  3129. set_current_inferior (inf);
  3130. current_inferior ()->top_target ()->pass_ctrlc ();
  3131. return;
  3132. }
  3133. }
  3134. }
  3135. }
  3136. /* See target.h. */
  3137. void
  3138. default_target_pass_ctrlc (struct target_ops *ops)
  3139. {
  3140. target_interrupt ();
  3141. }
  3142. /* See target/target.h. */
  3143. void
  3144. target_stop_and_wait (ptid_t ptid)
  3145. {
  3146. struct target_waitstatus status;
  3147. bool was_non_stop = non_stop;
  3148. non_stop = true;
  3149. target_stop (ptid);
  3150. target_wait (ptid, &status, 0);
  3151. non_stop = was_non_stop;
  3152. }
  3153. /* See target/target.h. */
  3154. void
  3155. target_continue_no_signal (ptid_t ptid)
  3156. {
  3157. target_resume (ptid, 0, GDB_SIGNAL_0);
  3158. }
  3159. /* See target/target.h. */
  3160. void
  3161. target_continue (ptid_t ptid, enum gdb_signal signal)
  3162. {
  3163. target_resume (ptid, 0, signal);
  3164. }
  3165. /* Concatenate ELEM to LIST, a comma-separated list. */
  3166. static void
  3167. str_comma_list_concat_elem (std::string *list, const char *elem)
  3168. {
  3169. if (!list->empty ())
  3170. list->append (", ");
  3171. list->append (elem);
  3172. }
  3173. /* Helper for target_options_to_string. If OPT is present in
  3174. TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
  3175. OPT is removed from TARGET_OPTIONS. */
  3176. static void
  3177. do_option (target_wait_flags *target_options, std::string *ret,
  3178. target_wait_flag opt, const char *opt_str)
  3179. {
  3180. if ((*target_options & opt) != 0)
  3181. {
  3182. str_comma_list_concat_elem (ret, opt_str);
  3183. *target_options &= ~opt;
  3184. }
  3185. }
  3186. /* See target.h. */
  3187. std::string
  3188. target_options_to_string (target_wait_flags target_options)
  3189. {
  3190. std::string ret;
  3191. #define DO_TARG_OPTION(OPT) \
  3192. do_option (&target_options, &ret, OPT, #OPT)
  3193. DO_TARG_OPTION (TARGET_WNOHANG);
  3194. if (target_options != 0)
  3195. str_comma_list_concat_elem (&ret, "unknown???");
  3196. return ret;
  3197. }
  3198. void
  3199. target_fetch_registers (struct regcache *regcache, int regno)
  3200. {
  3201. current_inferior ()->top_target ()->fetch_registers (regcache, regno);
  3202. if (targetdebug)
  3203. regcache->debug_print_register ("target_fetch_registers", regno);
  3204. }
  3205. void
  3206. target_store_registers (struct regcache *regcache, int regno)
  3207. {
  3208. if (!may_write_registers)
  3209. error (_("Writing to registers is not allowed (regno %d)"), regno);
  3210. current_inferior ()->top_target ()->store_registers (regcache, regno);
  3211. if (targetdebug)
  3212. {
  3213. regcache->debug_print_register ("target_store_registers", regno);
  3214. }
  3215. }
  3216. int
  3217. target_core_of_thread (ptid_t ptid)
  3218. {
  3219. return current_inferior ()->top_target ()->core_of_thread (ptid);
  3220. }
  3221. int
  3222. simple_verify_memory (struct target_ops *ops,
  3223. const gdb_byte *data, CORE_ADDR lma, ULONGEST size)
  3224. {
  3225. LONGEST total_xfered = 0;
  3226. while (total_xfered < size)
  3227. {
  3228. ULONGEST xfered_len;
  3229. enum target_xfer_status status;
  3230. gdb_byte buf[1024];
  3231. ULONGEST howmuch = std::min<ULONGEST> (sizeof (buf), size - total_xfered);
  3232. status = target_xfer_partial (ops, TARGET_OBJECT_MEMORY, NULL,
  3233. buf, NULL, lma + total_xfered, howmuch,
  3234. &xfered_len);
  3235. if (status == TARGET_XFER_OK
  3236. && memcmp (data + total_xfered, buf, xfered_len) == 0)
  3237. {
  3238. total_xfered += xfered_len;
  3239. QUIT;
  3240. }
  3241. else
  3242. return 0;
  3243. }
  3244. return 1;
  3245. }
  3246. /* Default implementation of memory verification. */
  3247. static int
  3248. default_verify_memory (struct target_ops *self,
  3249. const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
  3250. {
  3251. /* Start over from the top of the target stack. */
  3252. return simple_verify_memory (current_inferior ()->top_target (),
  3253. data, memaddr, size);
  3254. }
  3255. int
  3256. target_verify_memory (const gdb_byte *data, CORE_ADDR memaddr, ULONGEST size)
  3257. {
  3258. target_ops *target = current_inferior ()->top_target ();
  3259. return target->verify_memory (data, memaddr, size);
  3260. }
  3261. /* The documentation for this function is in its prototype declaration in
  3262. target.h. */
  3263. int
  3264. target_insert_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
  3265. enum target_hw_bp_type rw)
  3266. {
  3267. target_ops *target = current_inferior ()->top_target ();
  3268. return target->insert_mask_watchpoint (addr, mask, rw);
  3269. }
  3270. /* The documentation for this function is in its prototype declaration in
  3271. target.h. */
  3272. int
  3273. target_remove_mask_watchpoint (CORE_ADDR addr, CORE_ADDR mask,
  3274. enum target_hw_bp_type rw)
  3275. {
  3276. target_ops *target = current_inferior ()->top_target ();
  3277. return target->remove_mask_watchpoint (addr, mask, rw);
  3278. }
  3279. /* The documentation for this function is in its prototype declaration
  3280. in target.h. */
  3281. int
  3282. target_masked_watch_num_registers (CORE_ADDR addr, CORE_ADDR mask)
  3283. {
  3284. target_ops *target = current_inferior ()->top_target ();
  3285. return target->masked_watch_num_registers (addr, mask);
  3286. }
  3287. /* The documentation for this function is in its prototype declaration
  3288. in target.h. */
  3289. int
  3290. target_ranged_break_num_registers (void)
  3291. {
  3292. return current_inferior ()->top_target ()->ranged_break_num_registers ();
  3293. }
  3294. /* See target.h. */
  3295. struct btrace_target_info *
  3296. target_enable_btrace (thread_info *tp, const struct btrace_config *conf)
  3297. {
  3298. return current_inferior ()->top_target ()->enable_btrace (tp, conf);
  3299. }
  3300. /* See target.h. */
  3301. void
  3302. target_disable_btrace (struct btrace_target_info *btinfo)
  3303. {
  3304. current_inferior ()->top_target ()->disable_btrace (btinfo);
  3305. }
  3306. /* See target.h. */
  3307. void
  3308. target_teardown_btrace (struct btrace_target_info *btinfo)
  3309. {
  3310. current_inferior ()->top_target ()->teardown_btrace (btinfo);
  3311. }
  3312. /* See target.h. */
  3313. enum btrace_error
  3314. target_read_btrace (struct btrace_data *btrace,
  3315. struct btrace_target_info *btinfo,
  3316. enum btrace_read_type type)
  3317. {
  3318. target_ops *target = current_inferior ()->top_target ();
  3319. return target->read_btrace (btrace, btinfo, type);
  3320. }
  3321. /* See target.h. */
  3322. const struct btrace_config *
  3323. target_btrace_conf (const struct btrace_target_info *btinfo)
  3324. {
  3325. return current_inferior ()->top_target ()->btrace_conf (btinfo);
  3326. }
  3327. /* See target.h. */
  3328. void
  3329. target_stop_recording (void)
  3330. {
  3331. current_inferior ()->top_target ()->stop_recording ();
  3332. }
  3333. /* See target.h. */
  3334. void
  3335. target_save_record (const char *filename)
  3336. {
  3337. current_inferior ()->top_target ()->save_record (filename);
  3338. }
  3339. /* See target.h. */
  3340. int
  3341. target_supports_delete_record ()
  3342. {
  3343. return current_inferior ()->top_target ()->supports_delete_record ();
  3344. }
  3345. /* See target.h. */
  3346. void
  3347. target_delete_record (void)
  3348. {
  3349. current_inferior ()->top_target ()->delete_record ();
  3350. }
  3351. /* See target.h. */
  3352. enum record_method
  3353. target_record_method (ptid_t ptid)
  3354. {
  3355. return current_inferior ()->top_target ()->record_method (ptid);
  3356. }
  3357. /* See target.h. */
  3358. int
  3359. target_record_is_replaying (ptid_t ptid)
  3360. {
  3361. return current_inferior ()->top_target ()->record_is_replaying (ptid);
  3362. }
  3363. /* See target.h. */
  3364. int
  3365. target_record_will_replay (ptid_t ptid, int dir)
  3366. {
  3367. return current_inferior ()->top_target ()->record_will_replay (ptid, dir);
  3368. }
  3369. /* See target.h. */
  3370. void
  3371. target_record_stop_replaying (void)
  3372. {
  3373. current_inferior ()->top_target ()->record_stop_replaying ();
  3374. }
  3375. /* See target.h. */
  3376. void
  3377. target_goto_record_begin (void)
  3378. {
  3379. current_inferior ()->top_target ()->goto_record_begin ();
  3380. }
  3381. /* See target.h. */
  3382. void
  3383. target_goto_record_end (void)
  3384. {
  3385. current_inferior ()->top_target ()->goto_record_end ();
  3386. }
  3387. /* See target.h. */
  3388. void
  3389. target_goto_record (ULONGEST insn)
  3390. {
  3391. current_inferior ()->top_target ()->goto_record (insn);
  3392. }
  3393. /* See target.h. */
  3394. void
  3395. target_insn_history (int size, gdb_disassembly_flags flags)
  3396. {
  3397. current_inferior ()->top_target ()->insn_history (size, flags);
  3398. }
  3399. /* See target.h. */
  3400. void
  3401. target_insn_history_from (ULONGEST from, int size,
  3402. gdb_disassembly_flags flags)
  3403. {
  3404. current_inferior ()->top_target ()->insn_history_from (from, size, flags);
  3405. }
  3406. /* See target.h. */
  3407. void
  3408. target_insn_history_range (ULONGEST begin, ULONGEST end,
  3409. gdb_disassembly_flags flags)
  3410. {
  3411. current_inferior ()->top_target ()->insn_history_range (begin, end, flags);
  3412. }
  3413. /* See target.h. */
  3414. void
  3415. target_call_history (int size, record_print_flags flags)
  3416. {
  3417. current_inferior ()->top_target ()->call_history (size, flags);
  3418. }
  3419. /* See target.h. */
  3420. void
  3421. target_call_history_from (ULONGEST begin, int size, record_print_flags flags)
  3422. {
  3423. current_inferior ()->top_target ()->call_history_from (begin, size, flags);
  3424. }
  3425. /* See target.h. */
  3426. void
  3427. target_call_history_range (ULONGEST begin, ULONGEST end, record_print_flags flags)
  3428. {
  3429. current_inferior ()->top_target ()->call_history_range (begin, end, flags);
  3430. }
  3431. /* See target.h. */
  3432. const struct frame_unwind *
  3433. target_get_unwinder (void)
  3434. {
  3435. return current_inferior ()->top_target ()->get_unwinder ();
  3436. }
  3437. /* See target.h. */
  3438. const struct frame_unwind *
  3439. target_get_tailcall_unwinder (void)
  3440. {
  3441. return current_inferior ()->top_target ()->get_tailcall_unwinder ();
  3442. }
  3443. /* See target.h. */
  3444. void
  3445. target_prepare_to_generate_core (void)
  3446. {
  3447. current_inferior ()->top_target ()->prepare_to_generate_core ();
  3448. }
  3449. /* See target.h. */
  3450. void
  3451. target_done_generating_core (void)
  3452. {
  3453. current_inferior ()->top_target ()->done_generating_core ();
  3454. }
  3455. static char targ_desc[] =
  3456. "Names of targets and files being debugged.\nShows the entire \
  3457. stack of targets currently in use (including the exec-file,\n\
  3458. core-file, and process, if any), as well as the symbol file name.";
  3459. static void
  3460. default_rcmd (struct target_ops *self, const char *command,
  3461. struct ui_file *output)
  3462. {
  3463. error (_("\"monitor\" command not supported by this target."));
  3464. }
  3465. static void
  3466. do_monitor_command (const char *cmd, int from_tty)
  3467. {
  3468. target_rcmd (cmd, gdb_stdtarg);
  3469. }
  3470. /* Erases all the memory regions marked as flash. CMD and FROM_TTY are
  3471. ignored. */
  3472. void
  3473. flash_erase_command (const char *cmd, int from_tty)
  3474. {
  3475. /* Used to communicate termination of flash operations to the target. */
  3476. bool found_flash_region = false;
  3477. struct gdbarch *gdbarch = target_gdbarch ();
  3478. std::vector<mem_region> mem_regions = target_memory_map ();
  3479. /* Iterate over all memory regions. */
  3480. for (const mem_region &m : mem_regions)
  3481. {
  3482. /* Is this a flash memory region? */
  3483. if (m.attrib.mode == MEM_FLASH)
  3484. {
  3485. found_flash_region = true;
  3486. target_flash_erase (m.lo, m.hi - m.lo);
  3487. ui_out_emit_tuple tuple_emitter (current_uiout, "erased-regions");
  3488. current_uiout->message (_("Erasing flash memory region at address "));
  3489. current_uiout->field_core_addr ("address", gdbarch, m.lo);
  3490. current_uiout->message (", size = ");
  3491. current_uiout->field_string ("size", hex_string (m.hi - m.lo));
  3492. current_uiout->message ("\n");
  3493. }
  3494. }
  3495. /* Did we do any flash operations? If so, we need to finalize them. */
  3496. if (found_flash_region)
  3497. target_flash_done ();
  3498. else
  3499. current_uiout->message (_("No flash memory regions found.\n"));
  3500. }
  3501. /* Print the name of each layers of our target stack. */
  3502. static void
  3503. maintenance_print_target_stack (const char *cmd, int from_tty)
  3504. {
  3505. gdb_printf (_("The current target stack is:\n"));
  3506. for (target_ops *t = current_inferior ()->top_target ();
  3507. t != NULL;
  3508. t = t->beneath ())
  3509. {
  3510. if (t->stratum () == debug_stratum)
  3511. continue;
  3512. gdb_printf (" - %s (%s)\n", t->shortname (), t->longname ());
  3513. }
  3514. }
  3515. /* See target.h. */
  3516. void
  3517. target_async (int enable)
  3518. {
  3519. /* If we are trying to enable async mode then it must be the case that
  3520. async mode is possible for this target. */
  3521. gdb_assert (!enable || target_can_async_p ());
  3522. infrun_async (enable);
  3523. current_inferior ()->top_target ()->async (enable);
  3524. }
  3525. /* See target.h. */
  3526. void
  3527. target_thread_events (int enable)
  3528. {
  3529. current_inferior ()->top_target ()->thread_events (enable);
  3530. }
  3531. /* Controls if targets can report that they can/are async. This is
  3532. just for maintainers to use when debugging gdb. */
  3533. bool target_async_permitted = true;
  3534. static void
  3535. set_maint_target_async (bool permitted)
  3536. {
  3537. if (have_live_inferiors ())
  3538. error (_("Cannot change this setting while the inferior is running."));
  3539. target_async_permitted = permitted;
  3540. }
  3541. static bool
  3542. get_maint_target_async ()
  3543. {
  3544. return target_async_permitted;
  3545. }
  3546. static void
  3547. show_maint_target_async (ui_file *file, int from_tty,
  3548. cmd_list_element *c, const char *value)
  3549. {
  3550. gdb_printf (file,
  3551. _("Controlling the inferior in "
  3552. "asynchronous mode is %s.\n"), value);
  3553. }
  3554. /* Return true if the target operates in non-stop mode even with "set
  3555. non-stop off". */
  3556. static int
  3557. target_always_non_stop_p (void)
  3558. {
  3559. return current_inferior ()->top_target ()->always_non_stop_p ();
  3560. }
  3561. /* See target.h. */
  3562. bool
  3563. target_is_non_stop_p ()
  3564. {
  3565. return ((non_stop
  3566. || target_non_stop_enabled == AUTO_BOOLEAN_TRUE
  3567. || (target_non_stop_enabled == AUTO_BOOLEAN_AUTO
  3568. && target_always_non_stop_p ()))
  3569. && target_can_async_p ());
  3570. }
  3571. /* See target.h. */
  3572. bool
  3573. exists_non_stop_target ()
  3574. {
  3575. if (target_is_non_stop_p ())
  3576. return true;
  3577. scoped_restore_current_thread restore_thread;
  3578. for (inferior *inf : all_inferiors ())
  3579. {
  3580. switch_to_inferior_no_thread (inf);
  3581. if (target_is_non_stop_p ())
  3582. return true;
  3583. }
  3584. return false;
  3585. }
  3586. /* Controls if targets can report that they always run in non-stop
  3587. mode. This is just for maintainers to use when debugging gdb. */
  3588. enum auto_boolean target_non_stop_enabled = AUTO_BOOLEAN_AUTO;
  3589. /* Set callback for maint target-non-stop setting. */
  3590. static void
  3591. set_maint_target_non_stop (auto_boolean enabled)
  3592. {
  3593. if (have_live_inferiors ())
  3594. error (_("Cannot change this setting while the inferior is running."));
  3595. target_non_stop_enabled = enabled;
  3596. }
  3597. /* Get callback for maint target-non-stop setting. */
  3598. static auto_boolean
  3599. get_maint_target_non_stop ()
  3600. {
  3601. return target_non_stop_enabled;
  3602. }
  3603. static void
  3604. show_maint_target_non_stop (ui_file *file, int from_tty,
  3605. cmd_list_element *c, const char *value)
  3606. {
  3607. if (target_non_stop_enabled == AUTO_BOOLEAN_AUTO)
  3608. gdb_printf (file,
  3609. _("Whether the target is always in non-stop mode "
  3610. "is %s (currently %s).\n"), value,
  3611. target_always_non_stop_p () ? "on" : "off");
  3612. else
  3613. gdb_printf (file,
  3614. _("Whether the target is always in non-stop mode "
  3615. "is %s.\n"), value);
  3616. }
  3617. /* Temporary copies of permission settings. */
  3618. static bool may_write_registers_1 = true;
  3619. static bool may_write_memory_1 = true;
  3620. static bool may_insert_breakpoints_1 = true;
  3621. static bool may_insert_tracepoints_1 = true;
  3622. static bool may_insert_fast_tracepoints_1 = true;
  3623. static bool may_stop_1 = true;
  3624. /* Make the user-set values match the real values again. */
  3625. void
  3626. update_target_permissions (void)
  3627. {
  3628. may_write_registers_1 = may_write_registers;
  3629. may_write_memory_1 = may_write_memory;
  3630. may_insert_breakpoints_1 = may_insert_breakpoints;
  3631. may_insert_tracepoints_1 = may_insert_tracepoints;
  3632. may_insert_fast_tracepoints_1 = may_insert_fast_tracepoints;
  3633. may_stop_1 = may_stop;
  3634. }
  3635. /* The one function handles (most of) the permission flags in the same
  3636. way. */
  3637. static void
  3638. set_target_permissions (const char *args, int from_tty,
  3639. struct cmd_list_element *c)
  3640. {
  3641. if (target_has_execution ())
  3642. {
  3643. update_target_permissions ();
  3644. error (_("Cannot change this setting while the inferior is running."));
  3645. }
  3646. /* Make the real values match the user-changed values. */
  3647. may_write_registers = may_write_registers_1;
  3648. may_insert_breakpoints = may_insert_breakpoints_1;
  3649. may_insert_tracepoints = may_insert_tracepoints_1;
  3650. may_insert_fast_tracepoints = may_insert_fast_tracepoints_1;
  3651. may_stop = may_stop_1;
  3652. update_observer_mode ();
  3653. }
  3654. /* Set memory write permission independently of observer mode. */
  3655. static void
  3656. set_write_memory_permission (const char *args, int from_tty,
  3657. struct cmd_list_element *c)
  3658. {
  3659. /* Make the real values match the user-changed values. */
  3660. may_write_memory = may_write_memory_1;
  3661. update_observer_mode ();
  3662. }
  3663. void _initialize_target ();
  3664. void
  3665. _initialize_target ()
  3666. {
  3667. the_debug_target = new debug_target ();
  3668. add_info ("target", info_target_command, targ_desc);
  3669. add_info ("files", info_target_command, targ_desc);
  3670. add_setshow_zuinteger_cmd ("target", class_maintenance, &targetdebug, _("\
  3671. Set target debugging."), _("\
  3672. Show target debugging."), _("\
  3673. When non-zero, target debugging is enabled. Higher numbers are more\n\
  3674. verbose."),
  3675. set_targetdebug,
  3676. show_targetdebug,
  3677. &setdebuglist, &showdebuglist);
  3678. add_setshow_boolean_cmd ("trust-readonly-sections", class_support,
  3679. &trust_readonly, _("\
  3680. Set mode for reading from readonly sections."), _("\
  3681. Show mode for reading from readonly sections."), _("\
  3682. When this mode is on, memory reads from readonly sections (such as .text)\n\
  3683. will be read from the object file instead of from the target. This will\n\
  3684. result in significant performance improvement for remote targets."),
  3685. NULL,
  3686. show_trust_readonly,
  3687. &setlist, &showlist);
  3688. add_com ("monitor", class_obscure, do_monitor_command,
  3689. _("Send a command to the remote monitor (remote targets only)."));
  3690. add_cmd ("target-stack", class_maintenance, maintenance_print_target_stack,
  3691. _("Print the name of each layer of the internal target stack."),
  3692. &maintenanceprintlist);
  3693. add_setshow_boolean_cmd ("target-async", no_class,
  3694. _("\
  3695. Set whether gdb controls the inferior in asynchronous mode."), _("\
  3696. Show whether gdb controls the inferior in asynchronous mode."), _("\
  3697. Tells gdb whether to control the inferior in asynchronous mode."),
  3698. set_maint_target_async,
  3699. get_maint_target_async,
  3700. show_maint_target_async,
  3701. &maintenance_set_cmdlist,
  3702. &maintenance_show_cmdlist);
  3703. add_setshow_auto_boolean_cmd ("target-non-stop", no_class,
  3704. _("\
  3705. Set whether gdb always controls the inferior in non-stop mode."), _("\
  3706. Show whether gdb always controls the inferior in non-stop mode."), _("\
  3707. Tells gdb whether to control the inferior in non-stop mode."),
  3708. set_maint_target_non_stop,
  3709. get_maint_target_non_stop,
  3710. show_maint_target_non_stop,
  3711. &maintenance_set_cmdlist,
  3712. &maintenance_show_cmdlist);
  3713. add_setshow_boolean_cmd ("may-write-registers", class_support,
  3714. &may_write_registers_1, _("\
  3715. Set permission to write into registers."), _("\
  3716. Show permission to write into registers."), _("\
  3717. When this permission is on, GDB may write into the target's registers.\n\
  3718. Otherwise, any sort of write attempt will result in an error."),
  3719. set_target_permissions, NULL,
  3720. &setlist, &showlist);
  3721. add_setshow_boolean_cmd ("may-write-memory", class_support,
  3722. &may_write_memory_1, _("\
  3723. Set permission to write into target memory."), _("\
  3724. Show permission to write into target memory."), _("\
  3725. When this permission is on, GDB may write into the target's memory.\n\
  3726. Otherwise, any sort of write attempt will result in an error."),
  3727. set_write_memory_permission, NULL,
  3728. &setlist, &showlist);
  3729. add_setshow_boolean_cmd ("may-insert-breakpoints", class_support,
  3730. &may_insert_breakpoints_1, _("\
  3731. Set permission to insert breakpoints in the target."), _("\
  3732. Show permission to insert breakpoints in the target."), _("\
  3733. When this permission is on, GDB may insert breakpoints in the program.\n\
  3734. Otherwise, any sort of insertion attempt will result in an error."),
  3735. set_target_permissions, NULL,
  3736. &setlist, &showlist);
  3737. add_setshow_boolean_cmd ("may-insert-tracepoints", class_support,
  3738. &may_insert_tracepoints_1, _("\
  3739. Set permission to insert tracepoints in the target."), _("\
  3740. Show permission to insert tracepoints in the target."), _("\
  3741. When this permission is on, GDB may insert tracepoints in the program.\n\
  3742. Otherwise, any sort of insertion attempt will result in an error."),
  3743. set_target_permissions, NULL,
  3744. &setlist, &showlist);
  3745. add_setshow_boolean_cmd ("may-insert-fast-tracepoints", class_support,
  3746. &may_insert_fast_tracepoints_1, _("\
  3747. Set permission to insert fast tracepoints in the target."), _("\
  3748. Show permission to insert fast tracepoints in the target."), _("\
  3749. When this permission is on, GDB may insert fast tracepoints.\n\
  3750. Otherwise, any sort of insertion attempt will result in an error."),
  3751. set_target_permissions, NULL,
  3752. &setlist, &showlist);
  3753. add_setshow_boolean_cmd ("may-interrupt", class_support,
  3754. &may_stop_1, _("\
  3755. Set permission to interrupt or signal the target."), _("\
  3756. Show permission to interrupt or signal the target."), _("\
  3757. When this permission is on, GDB may interrupt/stop the target's execution.\n\
  3758. Otherwise, any attempt to interrupt or stop will be ignored."),
  3759. set_target_permissions, NULL,
  3760. &setlist, &showlist);
  3761. add_com ("flash-erase", no_class, flash_erase_command,
  3762. _("Erase all flash memory regions."));
  3763. add_setshow_boolean_cmd ("auto-connect-native-target", class_support,
  3764. &auto_connect_native_target, _("\
  3765. Set whether GDB may automatically connect to the native target."), _("\
  3766. Show whether GDB may automatically connect to the native target."), _("\
  3767. When on, and GDB is not connected to a target yet, GDB\n\
  3768. attempts \"run\" and other commands with the native target."),
  3769. NULL, show_auto_connect_native_target,
  3770. &setlist, &showlist);
  3771. }