2
0

gdbstub.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  1. /*
  2. * gdb server stub
  3. *
  4. * This implements a subset of the remote protocol as described in:
  5. *
  6. * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
  7. *
  8. * Copyright (c) 2003-2005 Fabrice Bellard
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * SPDX-License-Identifier: LGPL-2.0+
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu-common.h"
  27. #include "qapi/error.h"
  28. #include "qemu/error-report.h"
  29. #include "qemu/ctype.h"
  30. #include "qemu/cutils.h"
  31. #include "qemu/module.h"
  32. #include "trace/trace-root.h"
  33. #include "exec/gdbstub.h"
  34. #ifdef CONFIG_USER_ONLY
  35. #include "qemu.h"
  36. #else
  37. #include "monitor/monitor.h"
  38. #include "chardev/char.h"
  39. #include "chardev/char-fe.h"
  40. #include "hw/cpu/cluster.h"
  41. #include "hw/boards.h"
  42. #endif
  43. #define MAX_PACKET_LENGTH 4096
  44. #include "qemu/sockets.h"
  45. #include "sysemu/hw_accel.h"
  46. #include "sysemu/kvm.h"
  47. #include "sysemu/runstate.h"
  48. #include "semihosting/semihost.h"
  49. #include "exec/exec-all.h"
  50. #include "sysemu/replay.h"
  51. #ifdef CONFIG_USER_ONLY
  52. #define GDB_ATTACHED "0"
  53. #else
  54. #define GDB_ATTACHED "1"
  55. #endif
  56. #ifndef CONFIG_USER_ONLY
  57. static int phy_memory_mode;
  58. #endif
  59. static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
  60. uint8_t *buf, int len, bool is_write)
  61. {
  62. CPUClass *cc;
  63. #ifndef CONFIG_USER_ONLY
  64. if (phy_memory_mode) {
  65. if (is_write) {
  66. cpu_physical_memory_write(addr, buf, len);
  67. } else {
  68. cpu_physical_memory_read(addr, buf, len);
  69. }
  70. return 0;
  71. }
  72. #endif
  73. cc = CPU_GET_CLASS(cpu);
  74. if (cc->memory_rw_debug) {
  75. return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
  76. }
  77. return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
  78. }
  79. /* Return the GDB index for a given vCPU state.
  80. *
  81. * For user mode this is simply the thread id. In system mode GDB
  82. * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
  83. */
  84. static inline int cpu_gdb_index(CPUState *cpu)
  85. {
  86. #if defined(CONFIG_USER_ONLY)
  87. TaskState *ts = (TaskState *) cpu->opaque;
  88. return ts->ts_tid;
  89. #else
  90. return cpu->cpu_index + 1;
  91. #endif
  92. }
  93. enum {
  94. GDB_SIGNAL_0 = 0,
  95. GDB_SIGNAL_INT = 2,
  96. GDB_SIGNAL_QUIT = 3,
  97. GDB_SIGNAL_TRAP = 5,
  98. GDB_SIGNAL_ABRT = 6,
  99. GDB_SIGNAL_ALRM = 14,
  100. GDB_SIGNAL_IO = 23,
  101. GDB_SIGNAL_XCPU = 24,
  102. GDB_SIGNAL_UNKNOWN = 143
  103. };
  104. #ifdef CONFIG_USER_ONLY
  105. /* Map target signal numbers to GDB protocol signal numbers and vice
  106. * versa. For user emulation's currently supported systems, we can
  107. * assume most signals are defined.
  108. */
  109. static int gdb_signal_table[] = {
  110. 0,
  111. TARGET_SIGHUP,
  112. TARGET_SIGINT,
  113. TARGET_SIGQUIT,
  114. TARGET_SIGILL,
  115. TARGET_SIGTRAP,
  116. TARGET_SIGABRT,
  117. -1, /* SIGEMT */
  118. TARGET_SIGFPE,
  119. TARGET_SIGKILL,
  120. TARGET_SIGBUS,
  121. TARGET_SIGSEGV,
  122. TARGET_SIGSYS,
  123. TARGET_SIGPIPE,
  124. TARGET_SIGALRM,
  125. TARGET_SIGTERM,
  126. TARGET_SIGURG,
  127. TARGET_SIGSTOP,
  128. TARGET_SIGTSTP,
  129. TARGET_SIGCONT,
  130. TARGET_SIGCHLD,
  131. TARGET_SIGTTIN,
  132. TARGET_SIGTTOU,
  133. TARGET_SIGIO,
  134. TARGET_SIGXCPU,
  135. TARGET_SIGXFSZ,
  136. TARGET_SIGVTALRM,
  137. TARGET_SIGPROF,
  138. TARGET_SIGWINCH,
  139. -1, /* SIGLOST */
  140. TARGET_SIGUSR1,
  141. TARGET_SIGUSR2,
  142. #ifdef TARGET_SIGPWR
  143. TARGET_SIGPWR,
  144. #else
  145. -1,
  146. #endif
  147. -1, /* SIGPOLL */
  148. -1,
  149. -1,
  150. -1,
  151. -1,
  152. -1,
  153. -1,
  154. -1,
  155. -1,
  156. -1,
  157. -1,
  158. -1,
  159. #ifdef __SIGRTMIN
  160. __SIGRTMIN + 1,
  161. __SIGRTMIN + 2,
  162. __SIGRTMIN + 3,
  163. __SIGRTMIN + 4,
  164. __SIGRTMIN + 5,
  165. __SIGRTMIN + 6,
  166. __SIGRTMIN + 7,
  167. __SIGRTMIN + 8,
  168. __SIGRTMIN + 9,
  169. __SIGRTMIN + 10,
  170. __SIGRTMIN + 11,
  171. __SIGRTMIN + 12,
  172. __SIGRTMIN + 13,
  173. __SIGRTMIN + 14,
  174. __SIGRTMIN + 15,
  175. __SIGRTMIN + 16,
  176. __SIGRTMIN + 17,
  177. __SIGRTMIN + 18,
  178. __SIGRTMIN + 19,
  179. __SIGRTMIN + 20,
  180. __SIGRTMIN + 21,
  181. __SIGRTMIN + 22,
  182. __SIGRTMIN + 23,
  183. __SIGRTMIN + 24,
  184. __SIGRTMIN + 25,
  185. __SIGRTMIN + 26,
  186. __SIGRTMIN + 27,
  187. __SIGRTMIN + 28,
  188. __SIGRTMIN + 29,
  189. __SIGRTMIN + 30,
  190. __SIGRTMIN + 31,
  191. -1, /* SIGCANCEL */
  192. __SIGRTMIN,
  193. __SIGRTMIN + 32,
  194. __SIGRTMIN + 33,
  195. __SIGRTMIN + 34,
  196. __SIGRTMIN + 35,
  197. __SIGRTMIN + 36,
  198. __SIGRTMIN + 37,
  199. __SIGRTMIN + 38,
  200. __SIGRTMIN + 39,
  201. __SIGRTMIN + 40,
  202. __SIGRTMIN + 41,
  203. __SIGRTMIN + 42,
  204. __SIGRTMIN + 43,
  205. __SIGRTMIN + 44,
  206. __SIGRTMIN + 45,
  207. __SIGRTMIN + 46,
  208. __SIGRTMIN + 47,
  209. __SIGRTMIN + 48,
  210. __SIGRTMIN + 49,
  211. __SIGRTMIN + 50,
  212. __SIGRTMIN + 51,
  213. __SIGRTMIN + 52,
  214. __SIGRTMIN + 53,
  215. __SIGRTMIN + 54,
  216. __SIGRTMIN + 55,
  217. __SIGRTMIN + 56,
  218. __SIGRTMIN + 57,
  219. __SIGRTMIN + 58,
  220. __SIGRTMIN + 59,
  221. __SIGRTMIN + 60,
  222. __SIGRTMIN + 61,
  223. __SIGRTMIN + 62,
  224. __SIGRTMIN + 63,
  225. __SIGRTMIN + 64,
  226. __SIGRTMIN + 65,
  227. __SIGRTMIN + 66,
  228. __SIGRTMIN + 67,
  229. __SIGRTMIN + 68,
  230. __SIGRTMIN + 69,
  231. __SIGRTMIN + 70,
  232. __SIGRTMIN + 71,
  233. __SIGRTMIN + 72,
  234. __SIGRTMIN + 73,
  235. __SIGRTMIN + 74,
  236. __SIGRTMIN + 75,
  237. __SIGRTMIN + 76,
  238. __SIGRTMIN + 77,
  239. __SIGRTMIN + 78,
  240. __SIGRTMIN + 79,
  241. __SIGRTMIN + 80,
  242. __SIGRTMIN + 81,
  243. __SIGRTMIN + 82,
  244. __SIGRTMIN + 83,
  245. __SIGRTMIN + 84,
  246. __SIGRTMIN + 85,
  247. __SIGRTMIN + 86,
  248. __SIGRTMIN + 87,
  249. __SIGRTMIN + 88,
  250. __SIGRTMIN + 89,
  251. __SIGRTMIN + 90,
  252. __SIGRTMIN + 91,
  253. __SIGRTMIN + 92,
  254. __SIGRTMIN + 93,
  255. __SIGRTMIN + 94,
  256. __SIGRTMIN + 95,
  257. -1, /* SIGINFO */
  258. -1, /* UNKNOWN */
  259. -1, /* DEFAULT */
  260. -1,
  261. -1,
  262. -1,
  263. -1,
  264. -1,
  265. -1
  266. #endif
  267. };
  268. #else
  269. /* In system mode we only need SIGINT and SIGTRAP; other signals
  270. are not yet supported. */
  271. enum {
  272. TARGET_SIGINT = 2,
  273. TARGET_SIGTRAP = 5
  274. };
  275. static int gdb_signal_table[] = {
  276. -1,
  277. -1,
  278. TARGET_SIGINT,
  279. -1,
  280. -1,
  281. TARGET_SIGTRAP
  282. };
  283. #endif
  284. #ifdef CONFIG_USER_ONLY
  285. static int target_signal_to_gdb (int sig)
  286. {
  287. int i;
  288. for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
  289. if (gdb_signal_table[i] == sig)
  290. return i;
  291. return GDB_SIGNAL_UNKNOWN;
  292. }
  293. #endif
  294. static int gdb_signal_to_target (int sig)
  295. {
  296. if (sig < ARRAY_SIZE (gdb_signal_table))
  297. return gdb_signal_table[sig];
  298. else
  299. return -1;
  300. }
  301. typedef struct GDBRegisterState {
  302. int base_reg;
  303. int num_regs;
  304. gdb_get_reg_cb get_reg;
  305. gdb_set_reg_cb set_reg;
  306. const char *xml;
  307. struct GDBRegisterState *next;
  308. } GDBRegisterState;
  309. typedef struct GDBProcess {
  310. uint32_t pid;
  311. bool attached;
  312. char target_xml[1024];
  313. } GDBProcess;
  314. enum RSState {
  315. RS_INACTIVE,
  316. RS_IDLE,
  317. RS_GETLINE,
  318. RS_GETLINE_ESC,
  319. RS_GETLINE_RLE,
  320. RS_CHKSUM1,
  321. RS_CHKSUM2,
  322. };
  323. typedef struct GDBState {
  324. bool init; /* have we been initialised? */
  325. CPUState *c_cpu; /* current CPU for step/continue ops */
  326. CPUState *g_cpu; /* current CPU for other ops */
  327. CPUState *query_cpu; /* for q{f|s}ThreadInfo */
  328. enum RSState state; /* parsing state */
  329. char line_buf[MAX_PACKET_LENGTH];
  330. int line_buf_index;
  331. int line_sum; /* running checksum */
  332. int line_csum; /* checksum at the end of the packet */
  333. GByteArray *last_packet;
  334. int signal;
  335. #ifdef CONFIG_USER_ONLY
  336. int fd;
  337. char *socket_path;
  338. int running_state;
  339. #else
  340. CharBackend chr;
  341. Chardev *mon_chr;
  342. #endif
  343. bool multiprocess;
  344. GDBProcess *processes;
  345. int process_num;
  346. char syscall_buf[256];
  347. gdb_syscall_complete_cb current_syscall_cb;
  348. GString *str_buf;
  349. GByteArray *mem_buf;
  350. } GDBState;
  351. /* By default use no IRQs and no timers while single stepping so as to
  352. * make single stepping like an ICE HW step.
  353. */
  354. static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
  355. /* Retrieves flags for single step mode. */
  356. static int get_sstep_flags(void)
  357. {
  358. /*
  359. * In replay mode all events written into the log should be replayed.
  360. * That is why NOIRQ flag is removed in this mode.
  361. */
  362. if (replay_mode != REPLAY_MODE_NONE) {
  363. return SSTEP_ENABLE;
  364. } else {
  365. return sstep_flags;
  366. }
  367. }
  368. static GDBState gdbserver_state;
  369. static void init_gdbserver_state(void)
  370. {
  371. g_assert(!gdbserver_state.init);
  372. memset(&gdbserver_state, 0, sizeof(GDBState));
  373. gdbserver_state.init = true;
  374. gdbserver_state.str_buf = g_string_new(NULL);
  375. gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
  376. gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
  377. }
  378. #ifndef CONFIG_USER_ONLY
  379. static void reset_gdbserver_state(void)
  380. {
  381. g_free(gdbserver_state.processes);
  382. gdbserver_state.processes = NULL;
  383. gdbserver_state.process_num = 0;
  384. }
  385. #endif
  386. bool gdb_has_xml;
  387. #ifdef CONFIG_USER_ONLY
  388. static int get_char(void)
  389. {
  390. uint8_t ch;
  391. int ret;
  392. for(;;) {
  393. ret = qemu_recv(gdbserver_state.fd, &ch, 1, 0);
  394. if (ret < 0) {
  395. if (errno == ECONNRESET)
  396. gdbserver_state.fd = -1;
  397. if (errno != EINTR)
  398. return -1;
  399. } else if (ret == 0) {
  400. close(gdbserver_state.fd);
  401. gdbserver_state.fd = -1;
  402. return -1;
  403. } else {
  404. break;
  405. }
  406. }
  407. return ch;
  408. }
  409. #endif
  410. static enum {
  411. GDB_SYS_UNKNOWN,
  412. GDB_SYS_ENABLED,
  413. GDB_SYS_DISABLED,
  414. } gdb_syscall_mode;
  415. /* Decide if either remote gdb syscalls or native file IO should be used. */
  416. int use_gdb_syscalls(void)
  417. {
  418. SemihostingTarget target = semihosting_get_target();
  419. if (target == SEMIHOSTING_TARGET_NATIVE) {
  420. /* -semihosting-config target=native */
  421. return false;
  422. } else if (target == SEMIHOSTING_TARGET_GDB) {
  423. /* -semihosting-config target=gdb */
  424. return true;
  425. }
  426. /* -semihosting-config target=auto */
  427. /* On the first call check if gdb is connected and remember. */
  428. if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
  429. gdb_syscall_mode = gdbserver_state.init ?
  430. GDB_SYS_ENABLED : GDB_SYS_DISABLED;
  431. }
  432. return gdb_syscall_mode == GDB_SYS_ENABLED;
  433. }
  434. static bool stub_can_reverse(void)
  435. {
  436. #ifdef CONFIG_USER_ONLY
  437. return false;
  438. #else
  439. return replay_mode == REPLAY_MODE_PLAY;
  440. #endif
  441. }
  442. /* Resume execution. */
  443. static inline void gdb_continue(void)
  444. {
  445. #ifdef CONFIG_USER_ONLY
  446. gdbserver_state.running_state = 1;
  447. trace_gdbstub_op_continue();
  448. #else
  449. if (!runstate_needs_reset()) {
  450. trace_gdbstub_op_continue();
  451. vm_start();
  452. }
  453. #endif
  454. }
  455. /*
  456. * Resume execution, per CPU actions. For user-mode emulation it's
  457. * equivalent to gdb_continue.
  458. */
  459. static int gdb_continue_partial(char *newstates)
  460. {
  461. CPUState *cpu;
  462. int res = 0;
  463. #ifdef CONFIG_USER_ONLY
  464. /*
  465. * This is not exactly accurate, but it's an improvement compared to the
  466. * previous situation, where only one CPU would be single-stepped.
  467. */
  468. CPU_FOREACH(cpu) {
  469. if (newstates[cpu->cpu_index] == 's') {
  470. trace_gdbstub_op_stepping(cpu->cpu_index);
  471. cpu_single_step(cpu, sstep_flags);
  472. }
  473. }
  474. gdbserver_state.running_state = 1;
  475. #else
  476. int flag = 0;
  477. if (!runstate_needs_reset()) {
  478. if (vm_prepare_start()) {
  479. return 0;
  480. }
  481. CPU_FOREACH(cpu) {
  482. switch (newstates[cpu->cpu_index]) {
  483. case 0:
  484. case 1:
  485. break; /* nothing to do here */
  486. case 's':
  487. trace_gdbstub_op_stepping(cpu->cpu_index);
  488. cpu_single_step(cpu, get_sstep_flags());
  489. cpu_resume(cpu);
  490. flag = 1;
  491. break;
  492. case 'c':
  493. trace_gdbstub_op_continue_cpu(cpu->cpu_index);
  494. cpu_resume(cpu);
  495. flag = 1;
  496. break;
  497. default:
  498. res = -1;
  499. break;
  500. }
  501. }
  502. }
  503. if (flag) {
  504. qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
  505. }
  506. #endif
  507. return res;
  508. }
  509. static void put_buffer(const uint8_t *buf, int len)
  510. {
  511. #ifdef CONFIG_USER_ONLY
  512. int ret;
  513. while (len > 0) {
  514. ret = send(gdbserver_state.fd, buf, len, 0);
  515. if (ret < 0) {
  516. if (errno != EINTR)
  517. return;
  518. } else {
  519. buf += ret;
  520. len -= ret;
  521. }
  522. }
  523. #else
  524. /* XXX this blocks entire thread. Rewrite to use
  525. * qemu_chr_fe_write and background I/O callbacks */
  526. qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len);
  527. #endif
  528. }
  529. static inline int fromhex(int v)
  530. {
  531. if (v >= '0' && v <= '9')
  532. return v - '0';
  533. else if (v >= 'A' && v <= 'F')
  534. return v - 'A' + 10;
  535. else if (v >= 'a' && v <= 'f')
  536. return v - 'a' + 10;
  537. else
  538. return 0;
  539. }
  540. static inline int tohex(int v)
  541. {
  542. if (v < 10)
  543. return v + '0';
  544. else
  545. return v - 10 + 'a';
  546. }
  547. /* writes 2*len+1 bytes in buf */
  548. static void memtohex(GString *buf, const uint8_t *mem, int len)
  549. {
  550. int i, c;
  551. for(i = 0; i < len; i++) {
  552. c = mem[i];
  553. g_string_append_c(buf, tohex(c >> 4));
  554. g_string_append_c(buf, tohex(c & 0xf));
  555. }
  556. g_string_append_c(buf, '\0');
  557. }
  558. static void hextomem(GByteArray *mem, const char *buf, int len)
  559. {
  560. int i;
  561. for(i = 0; i < len; i++) {
  562. guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
  563. g_byte_array_append(mem, &byte, 1);
  564. buf += 2;
  565. }
  566. }
  567. static void hexdump(const char *buf, int len,
  568. void (*trace_fn)(size_t ofs, char const *text))
  569. {
  570. char line_buffer[3 * 16 + 4 + 16 + 1];
  571. size_t i;
  572. for (i = 0; i < len || (i & 0xF); ++i) {
  573. size_t byte_ofs = i & 15;
  574. if (byte_ofs == 0) {
  575. memset(line_buffer, ' ', 3 * 16 + 4 + 16);
  576. line_buffer[3 * 16 + 4 + 16] = 0;
  577. }
  578. size_t col_group = (i >> 2) & 3;
  579. size_t hex_col = byte_ofs * 3 + col_group;
  580. size_t txt_col = 3 * 16 + 4 + byte_ofs;
  581. if (i < len) {
  582. char value = buf[i];
  583. line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
  584. line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
  585. line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
  586. ? value
  587. : '.';
  588. }
  589. if (byte_ofs == 0xF)
  590. trace_fn(i & -16, line_buffer);
  591. }
  592. }
  593. /* return -1 if error, 0 if OK */
  594. static int put_packet_binary(const char *buf, int len, bool dump)
  595. {
  596. int csum, i;
  597. uint8_t footer[3];
  598. if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
  599. hexdump(buf, len, trace_gdbstub_io_binaryreply);
  600. }
  601. for(;;) {
  602. g_byte_array_set_size(gdbserver_state.last_packet, 0);
  603. g_byte_array_append(gdbserver_state.last_packet,
  604. (const uint8_t *) "$", 1);
  605. g_byte_array_append(gdbserver_state.last_packet,
  606. (const uint8_t *) buf, len);
  607. csum = 0;
  608. for(i = 0; i < len; i++) {
  609. csum += buf[i];
  610. }
  611. footer[0] = '#';
  612. footer[1] = tohex((csum >> 4) & 0xf);
  613. footer[2] = tohex((csum) & 0xf);
  614. g_byte_array_append(gdbserver_state.last_packet, footer, 3);
  615. put_buffer(gdbserver_state.last_packet->data,
  616. gdbserver_state.last_packet->len);
  617. #ifdef CONFIG_USER_ONLY
  618. i = get_char();
  619. if (i < 0)
  620. return -1;
  621. if (i == '+')
  622. break;
  623. #else
  624. break;
  625. #endif
  626. }
  627. return 0;
  628. }
  629. /* return -1 if error, 0 if OK */
  630. static int put_packet(const char *buf)
  631. {
  632. trace_gdbstub_io_reply(buf);
  633. return put_packet_binary(buf, strlen(buf), false);
  634. }
  635. static void put_strbuf(void)
  636. {
  637. put_packet(gdbserver_state.str_buf->str);
  638. }
  639. /* Encode data using the encoding for 'x' packets. */
  640. static void memtox(GString *buf, const char *mem, int len)
  641. {
  642. char c;
  643. while (len--) {
  644. c = *(mem++);
  645. switch (c) {
  646. case '#': case '$': case '*': case '}':
  647. g_string_append_c(buf, '}');
  648. g_string_append_c(buf, c ^ 0x20);
  649. break;
  650. default:
  651. g_string_append_c(buf, c);
  652. break;
  653. }
  654. }
  655. }
  656. static uint32_t gdb_get_cpu_pid(CPUState *cpu)
  657. {
  658. /* TODO: In user mode, we should use the task state PID */
  659. if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
  660. /* Return the default process' PID */
  661. int index = gdbserver_state.process_num - 1;
  662. return gdbserver_state.processes[index].pid;
  663. }
  664. return cpu->cluster_index + 1;
  665. }
  666. static GDBProcess *gdb_get_process(uint32_t pid)
  667. {
  668. int i;
  669. if (!pid) {
  670. /* 0 means any process, we take the first one */
  671. return &gdbserver_state.processes[0];
  672. }
  673. for (i = 0; i < gdbserver_state.process_num; i++) {
  674. if (gdbserver_state.processes[i].pid == pid) {
  675. return &gdbserver_state.processes[i];
  676. }
  677. }
  678. return NULL;
  679. }
  680. static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
  681. {
  682. return gdb_get_process(gdb_get_cpu_pid(cpu));
  683. }
  684. static CPUState *find_cpu(uint32_t thread_id)
  685. {
  686. CPUState *cpu;
  687. CPU_FOREACH(cpu) {
  688. if (cpu_gdb_index(cpu) == thread_id) {
  689. return cpu;
  690. }
  691. }
  692. return NULL;
  693. }
  694. static CPUState *get_first_cpu_in_process(GDBProcess *process)
  695. {
  696. CPUState *cpu;
  697. CPU_FOREACH(cpu) {
  698. if (gdb_get_cpu_pid(cpu) == process->pid) {
  699. return cpu;
  700. }
  701. }
  702. return NULL;
  703. }
  704. static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
  705. {
  706. uint32_t pid = gdb_get_cpu_pid(cpu);
  707. cpu = CPU_NEXT(cpu);
  708. while (cpu) {
  709. if (gdb_get_cpu_pid(cpu) == pid) {
  710. break;
  711. }
  712. cpu = CPU_NEXT(cpu);
  713. }
  714. return cpu;
  715. }
  716. /* Return the cpu following @cpu, while ignoring unattached processes. */
  717. static CPUState *gdb_next_attached_cpu(CPUState *cpu)
  718. {
  719. cpu = CPU_NEXT(cpu);
  720. while (cpu) {
  721. if (gdb_get_cpu_process(cpu)->attached) {
  722. break;
  723. }
  724. cpu = CPU_NEXT(cpu);
  725. }
  726. return cpu;
  727. }
  728. /* Return the first attached cpu */
  729. static CPUState *gdb_first_attached_cpu(void)
  730. {
  731. CPUState *cpu = first_cpu;
  732. GDBProcess *process = gdb_get_cpu_process(cpu);
  733. if (!process->attached) {
  734. return gdb_next_attached_cpu(cpu);
  735. }
  736. return cpu;
  737. }
  738. static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
  739. {
  740. GDBProcess *process;
  741. CPUState *cpu;
  742. if (!pid && !tid) {
  743. /* 0 means any process/thread, we take the first attached one */
  744. return gdb_first_attached_cpu();
  745. } else if (pid && !tid) {
  746. /* any thread in a specific process */
  747. process = gdb_get_process(pid);
  748. if (process == NULL) {
  749. return NULL;
  750. }
  751. if (!process->attached) {
  752. return NULL;
  753. }
  754. return get_first_cpu_in_process(process);
  755. } else {
  756. /* a specific thread */
  757. cpu = find_cpu(tid);
  758. if (cpu == NULL) {
  759. return NULL;
  760. }
  761. process = gdb_get_cpu_process(cpu);
  762. if (pid && process->pid != pid) {
  763. return NULL;
  764. }
  765. if (!process->attached) {
  766. return NULL;
  767. }
  768. return cpu;
  769. }
  770. }
  771. static const char *get_feature_xml(const char *p, const char **newp,
  772. GDBProcess *process)
  773. {
  774. size_t len;
  775. int i;
  776. const char *name;
  777. CPUState *cpu = get_first_cpu_in_process(process);
  778. CPUClass *cc = CPU_GET_CLASS(cpu);
  779. len = 0;
  780. while (p[len] && p[len] != ':')
  781. len++;
  782. *newp = p + len;
  783. name = NULL;
  784. if (strncmp(p, "target.xml", len) == 0) {
  785. char *buf = process->target_xml;
  786. const size_t buf_sz = sizeof(process->target_xml);
  787. /* Generate the XML description for this CPU. */
  788. if (!buf[0]) {
  789. GDBRegisterState *r;
  790. pstrcat(buf, buf_sz,
  791. "<?xml version=\"1.0\"?>"
  792. "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
  793. "<target>");
  794. if (cc->gdb_arch_name) {
  795. gchar *arch = cc->gdb_arch_name(cpu);
  796. pstrcat(buf, buf_sz, "<architecture>");
  797. pstrcat(buf, buf_sz, arch);
  798. pstrcat(buf, buf_sz, "</architecture>");
  799. g_free(arch);
  800. }
  801. pstrcat(buf, buf_sz, "<xi:include href=\"");
  802. pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
  803. pstrcat(buf, buf_sz, "\"/>");
  804. for (r = cpu->gdb_regs; r; r = r->next) {
  805. pstrcat(buf, buf_sz, "<xi:include href=\"");
  806. pstrcat(buf, buf_sz, r->xml);
  807. pstrcat(buf, buf_sz, "\"/>");
  808. }
  809. pstrcat(buf, buf_sz, "</target>");
  810. }
  811. return buf;
  812. }
  813. if (cc->gdb_get_dynamic_xml) {
  814. char *xmlname = g_strndup(p, len);
  815. const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
  816. g_free(xmlname);
  817. if (xml) {
  818. return xml;
  819. }
  820. }
  821. for (i = 0; ; i++) {
  822. name = xml_builtin[i][0];
  823. if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
  824. break;
  825. }
  826. return name ? xml_builtin[i][1] : NULL;
  827. }
  828. static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
  829. {
  830. CPUClass *cc = CPU_GET_CLASS(cpu);
  831. CPUArchState *env = cpu->env_ptr;
  832. GDBRegisterState *r;
  833. if (reg < cc->gdb_num_core_regs) {
  834. return cc->gdb_read_register(cpu, buf, reg);
  835. }
  836. for (r = cpu->gdb_regs; r; r = r->next) {
  837. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  838. return r->get_reg(env, buf, reg - r->base_reg);
  839. }
  840. }
  841. return 0;
  842. }
  843. static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
  844. {
  845. CPUClass *cc = CPU_GET_CLASS(cpu);
  846. CPUArchState *env = cpu->env_ptr;
  847. GDBRegisterState *r;
  848. if (reg < cc->gdb_num_core_regs) {
  849. return cc->gdb_write_register(cpu, mem_buf, reg);
  850. }
  851. for (r = cpu->gdb_regs; r; r = r->next) {
  852. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  853. return r->set_reg(env, mem_buf, reg - r->base_reg);
  854. }
  855. }
  856. return 0;
  857. }
  858. /* Register a supplemental set of CPU registers. If g_pos is nonzero it
  859. specifies the first register number and these registers are included in
  860. a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
  861. gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
  862. */
  863. void gdb_register_coprocessor(CPUState *cpu,
  864. gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
  865. int num_regs, const char *xml, int g_pos)
  866. {
  867. GDBRegisterState *s;
  868. GDBRegisterState **p;
  869. p = &cpu->gdb_regs;
  870. while (*p) {
  871. /* Check for duplicates. */
  872. if (strcmp((*p)->xml, xml) == 0)
  873. return;
  874. p = &(*p)->next;
  875. }
  876. s = g_new0(GDBRegisterState, 1);
  877. s->base_reg = cpu->gdb_num_regs;
  878. s->num_regs = num_regs;
  879. s->get_reg = get_reg;
  880. s->set_reg = set_reg;
  881. s->xml = xml;
  882. /* Add to end of list. */
  883. cpu->gdb_num_regs += num_regs;
  884. *p = s;
  885. if (g_pos) {
  886. if (g_pos != s->base_reg) {
  887. error_report("Error: Bad gdb register numbering for '%s', "
  888. "expected %d got %d", xml, g_pos, s->base_reg);
  889. } else {
  890. cpu->gdb_num_g_regs = cpu->gdb_num_regs;
  891. }
  892. }
  893. }
  894. #ifndef CONFIG_USER_ONLY
  895. /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
  896. static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
  897. {
  898. static const int xlat[] = {
  899. [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
  900. [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
  901. [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
  902. };
  903. CPUClass *cc = CPU_GET_CLASS(cpu);
  904. int cputype = xlat[gdbtype];
  905. if (cc->gdb_stop_before_watchpoint) {
  906. cputype |= BP_STOP_BEFORE_ACCESS;
  907. }
  908. return cputype;
  909. }
  910. #endif
  911. static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
  912. {
  913. CPUState *cpu;
  914. int err = 0;
  915. if (kvm_enabled()) {
  916. return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
  917. }
  918. switch (type) {
  919. case GDB_BREAKPOINT_SW:
  920. case GDB_BREAKPOINT_HW:
  921. CPU_FOREACH(cpu) {
  922. err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
  923. if (err) {
  924. break;
  925. }
  926. }
  927. return err;
  928. #ifndef CONFIG_USER_ONLY
  929. case GDB_WATCHPOINT_WRITE:
  930. case GDB_WATCHPOINT_READ:
  931. case GDB_WATCHPOINT_ACCESS:
  932. CPU_FOREACH(cpu) {
  933. err = cpu_watchpoint_insert(cpu, addr, len,
  934. xlat_gdb_type(cpu, type), NULL);
  935. if (err) {
  936. break;
  937. }
  938. }
  939. return err;
  940. #endif
  941. default:
  942. return -ENOSYS;
  943. }
  944. }
  945. static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
  946. {
  947. CPUState *cpu;
  948. int err = 0;
  949. if (kvm_enabled()) {
  950. return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
  951. }
  952. switch (type) {
  953. case GDB_BREAKPOINT_SW:
  954. case GDB_BREAKPOINT_HW:
  955. CPU_FOREACH(cpu) {
  956. err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
  957. if (err) {
  958. break;
  959. }
  960. }
  961. return err;
  962. #ifndef CONFIG_USER_ONLY
  963. case GDB_WATCHPOINT_WRITE:
  964. case GDB_WATCHPOINT_READ:
  965. case GDB_WATCHPOINT_ACCESS:
  966. CPU_FOREACH(cpu) {
  967. err = cpu_watchpoint_remove(cpu, addr, len,
  968. xlat_gdb_type(cpu, type));
  969. if (err)
  970. break;
  971. }
  972. return err;
  973. #endif
  974. default:
  975. return -ENOSYS;
  976. }
  977. }
  978. static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
  979. {
  980. cpu_breakpoint_remove_all(cpu, BP_GDB);
  981. #ifndef CONFIG_USER_ONLY
  982. cpu_watchpoint_remove_all(cpu, BP_GDB);
  983. #endif
  984. }
  985. static void gdb_process_breakpoint_remove_all(GDBProcess *p)
  986. {
  987. CPUState *cpu = get_first_cpu_in_process(p);
  988. while (cpu) {
  989. gdb_cpu_breakpoint_remove_all(cpu);
  990. cpu = gdb_next_cpu_in_process(cpu);
  991. }
  992. }
  993. static void gdb_breakpoint_remove_all(void)
  994. {
  995. CPUState *cpu;
  996. if (kvm_enabled()) {
  997. kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
  998. return;
  999. }
  1000. CPU_FOREACH(cpu) {
  1001. gdb_cpu_breakpoint_remove_all(cpu);
  1002. }
  1003. }
  1004. static void gdb_set_cpu_pc(target_ulong pc)
  1005. {
  1006. CPUState *cpu = gdbserver_state.c_cpu;
  1007. cpu_synchronize_state(cpu);
  1008. cpu_set_pc(cpu, pc);
  1009. }
  1010. static void gdb_append_thread_id(CPUState *cpu, GString *buf)
  1011. {
  1012. if (gdbserver_state.multiprocess) {
  1013. g_string_append_printf(buf, "p%02x.%02x",
  1014. gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu));
  1015. } else {
  1016. g_string_append_printf(buf, "%02x", cpu_gdb_index(cpu));
  1017. }
  1018. }
  1019. typedef enum GDBThreadIdKind {
  1020. GDB_ONE_THREAD = 0,
  1021. GDB_ALL_THREADS, /* One process, all threads */
  1022. GDB_ALL_PROCESSES,
  1023. GDB_READ_THREAD_ERR
  1024. } GDBThreadIdKind;
  1025. static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
  1026. uint32_t *pid, uint32_t *tid)
  1027. {
  1028. unsigned long p, t;
  1029. int ret;
  1030. if (*buf == 'p') {
  1031. buf++;
  1032. ret = qemu_strtoul(buf, &buf, 16, &p);
  1033. if (ret) {
  1034. return GDB_READ_THREAD_ERR;
  1035. }
  1036. /* Skip '.' */
  1037. buf++;
  1038. } else {
  1039. p = 1;
  1040. }
  1041. ret = qemu_strtoul(buf, &buf, 16, &t);
  1042. if (ret) {
  1043. return GDB_READ_THREAD_ERR;
  1044. }
  1045. *end_buf = buf;
  1046. if (p == -1) {
  1047. return GDB_ALL_PROCESSES;
  1048. }
  1049. if (pid) {
  1050. *pid = p;
  1051. }
  1052. if (t == -1) {
  1053. return GDB_ALL_THREADS;
  1054. }
  1055. if (tid) {
  1056. *tid = t;
  1057. }
  1058. return GDB_ONE_THREAD;
  1059. }
  1060. /**
  1061. * gdb_handle_vcont - Parses and handles a vCont packet.
  1062. * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
  1063. * a format error, 0 on success.
  1064. */
  1065. static int gdb_handle_vcont(const char *p)
  1066. {
  1067. int res, signal = 0;
  1068. char cur_action;
  1069. char *newstates;
  1070. unsigned long tmp;
  1071. uint32_t pid, tid;
  1072. GDBProcess *process;
  1073. CPUState *cpu;
  1074. GDBThreadIdKind kind;
  1075. #ifdef CONFIG_USER_ONLY
  1076. int max_cpus = 1; /* global variable max_cpus exists only in system mode */
  1077. CPU_FOREACH(cpu) {
  1078. max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
  1079. }
  1080. #else
  1081. MachineState *ms = MACHINE(qdev_get_machine());
  1082. unsigned int max_cpus = ms->smp.max_cpus;
  1083. #endif
  1084. /* uninitialised CPUs stay 0 */
  1085. newstates = g_new0(char, max_cpus);
  1086. /* mark valid CPUs with 1 */
  1087. CPU_FOREACH(cpu) {
  1088. newstates[cpu->cpu_index] = 1;
  1089. }
  1090. /*
  1091. * res keeps track of what error we are returning, with -ENOTSUP meaning
  1092. * that the command is unknown or unsupported, thus returning an empty
  1093. * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
  1094. * or incorrect parameters passed.
  1095. */
  1096. res = 0;
  1097. while (*p) {
  1098. if (*p++ != ';') {
  1099. res = -ENOTSUP;
  1100. goto out;
  1101. }
  1102. cur_action = *p++;
  1103. if (cur_action == 'C' || cur_action == 'S') {
  1104. cur_action = qemu_tolower(cur_action);
  1105. res = qemu_strtoul(p, &p, 16, &tmp);
  1106. if (res) {
  1107. goto out;
  1108. }
  1109. signal = gdb_signal_to_target(tmp);
  1110. } else if (cur_action != 'c' && cur_action != 's') {
  1111. /* unknown/invalid/unsupported command */
  1112. res = -ENOTSUP;
  1113. goto out;
  1114. }
  1115. if (*p == '\0' || *p == ';') {
  1116. /*
  1117. * No thread specifier, action is on "all threads". The
  1118. * specification is unclear regarding the process to act on. We
  1119. * choose all processes.
  1120. */
  1121. kind = GDB_ALL_PROCESSES;
  1122. } else if (*p++ == ':') {
  1123. kind = read_thread_id(p, &p, &pid, &tid);
  1124. } else {
  1125. res = -ENOTSUP;
  1126. goto out;
  1127. }
  1128. switch (kind) {
  1129. case GDB_READ_THREAD_ERR:
  1130. res = -EINVAL;
  1131. goto out;
  1132. case GDB_ALL_PROCESSES:
  1133. cpu = gdb_first_attached_cpu();
  1134. while (cpu) {
  1135. if (newstates[cpu->cpu_index] == 1) {
  1136. newstates[cpu->cpu_index] = cur_action;
  1137. }
  1138. cpu = gdb_next_attached_cpu(cpu);
  1139. }
  1140. break;
  1141. case GDB_ALL_THREADS:
  1142. process = gdb_get_process(pid);
  1143. if (!process->attached) {
  1144. res = -EINVAL;
  1145. goto out;
  1146. }
  1147. cpu = get_first_cpu_in_process(process);
  1148. while (cpu) {
  1149. if (newstates[cpu->cpu_index] == 1) {
  1150. newstates[cpu->cpu_index] = cur_action;
  1151. }
  1152. cpu = gdb_next_cpu_in_process(cpu);
  1153. }
  1154. break;
  1155. case GDB_ONE_THREAD:
  1156. cpu = gdb_get_cpu(pid, tid);
  1157. /* invalid CPU/thread specified */
  1158. if (!cpu) {
  1159. res = -EINVAL;
  1160. goto out;
  1161. }
  1162. /* only use if no previous match occourred */
  1163. if (newstates[cpu->cpu_index] == 1) {
  1164. newstates[cpu->cpu_index] = cur_action;
  1165. }
  1166. break;
  1167. }
  1168. }
  1169. gdbserver_state.signal = signal;
  1170. gdb_continue_partial(newstates);
  1171. out:
  1172. g_free(newstates);
  1173. return res;
  1174. }
  1175. typedef union GdbCmdVariant {
  1176. const char *data;
  1177. uint8_t opcode;
  1178. unsigned long val_ul;
  1179. unsigned long long val_ull;
  1180. struct {
  1181. GDBThreadIdKind kind;
  1182. uint32_t pid;
  1183. uint32_t tid;
  1184. } thread_id;
  1185. } GdbCmdVariant;
  1186. #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
  1187. static const char *cmd_next_param(const char *param, const char delimiter)
  1188. {
  1189. static const char all_delimiters[] = ",;:=";
  1190. char curr_delimiters[2] = {0};
  1191. const char *delimiters;
  1192. if (delimiter == '?') {
  1193. delimiters = all_delimiters;
  1194. } else if (delimiter == '0') {
  1195. return strchr(param, '\0');
  1196. } else if (delimiter == '.' && *param) {
  1197. return param + 1;
  1198. } else {
  1199. curr_delimiters[0] = delimiter;
  1200. delimiters = curr_delimiters;
  1201. }
  1202. param += strcspn(param, delimiters);
  1203. if (*param) {
  1204. param++;
  1205. }
  1206. return param;
  1207. }
  1208. static int cmd_parse_params(const char *data, const char *schema,
  1209. GArray *params)
  1210. {
  1211. const char *curr_schema, *curr_data;
  1212. g_assert(schema);
  1213. g_assert(params->len == 0);
  1214. curr_schema = schema;
  1215. curr_data = data;
  1216. while (curr_schema[0] && curr_schema[1] && *curr_data) {
  1217. GdbCmdVariant this_param;
  1218. switch (curr_schema[0]) {
  1219. case 'l':
  1220. if (qemu_strtoul(curr_data, &curr_data, 16,
  1221. &this_param.val_ul)) {
  1222. return -EINVAL;
  1223. }
  1224. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  1225. g_array_append_val(params, this_param);
  1226. break;
  1227. case 'L':
  1228. if (qemu_strtou64(curr_data, &curr_data, 16,
  1229. (uint64_t *)&this_param.val_ull)) {
  1230. return -EINVAL;
  1231. }
  1232. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  1233. g_array_append_val(params, this_param);
  1234. break;
  1235. case 's':
  1236. this_param.data = curr_data;
  1237. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  1238. g_array_append_val(params, this_param);
  1239. break;
  1240. case 'o':
  1241. this_param.opcode = *(uint8_t *)curr_data;
  1242. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  1243. g_array_append_val(params, this_param);
  1244. break;
  1245. case 't':
  1246. this_param.thread_id.kind =
  1247. read_thread_id(curr_data, &curr_data,
  1248. &this_param.thread_id.pid,
  1249. &this_param.thread_id.tid);
  1250. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  1251. g_array_append_val(params, this_param);
  1252. break;
  1253. case '?':
  1254. curr_data = cmd_next_param(curr_data, curr_schema[1]);
  1255. break;
  1256. default:
  1257. return -EINVAL;
  1258. }
  1259. curr_schema += 2;
  1260. }
  1261. return 0;
  1262. }
  1263. typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
  1264. /*
  1265. * cmd_startswith -> cmd is compared using startswith
  1266. *
  1267. *
  1268. * schema definitions:
  1269. * Each schema parameter entry consists of 2 chars,
  1270. * the first char represents the parameter type handling
  1271. * the second char represents the delimiter for the next parameter
  1272. *
  1273. * Currently supported schema types:
  1274. * 'l' -> unsigned long (stored in .val_ul)
  1275. * 'L' -> unsigned long long (stored in .val_ull)
  1276. * 's' -> string (stored in .data)
  1277. * 'o' -> single char (stored in .opcode)
  1278. * 't' -> thread id (stored in .thread_id)
  1279. * '?' -> skip according to delimiter
  1280. *
  1281. * Currently supported delimiters:
  1282. * '?' -> Stop at any delimiter (",;:=\0")
  1283. * '0' -> Stop at "\0"
  1284. * '.' -> Skip 1 char unless reached "\0"
  1285. * Any other value is treated as the delimiter value itself
  1286. */
  1287. typedef struct GdbCmdParseEntry {
  1288. GdbCmdHandler handler;
  1289. const char *cmd;
  1290. bool cmd_startswith;
  1291. const char *schema;
  1292. } GdbCmdParseEntry;
  1293. static inline int startswith(const char *string, const char *pattern)
  1294. {
  1295. return !strncmp(string, pattern, strlen(pattern));
  1296. }
  1297. static int process_string_cmd(void *user_ctx, const char *data,
  1298. const GdbCmdParseEntry *cmds, int num_cmds)
  1299. {
  1300. int i;
  1301. g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
  1302. if (!cmds) {
  1303. return -1;
  1304. }
  1305. for (i = 0; i < num_cmds; i++) {
  1306. const GdbCmdParseEntry *cmd = &cmds[i];
  1307. g_assert(cmd->handler && cmd->cmd);
  1308. if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
  1309. (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
  1310. continue;
  1311. }
  1312. if (cmd->schema) {
  1313. if (cmd_parse_params(&data[strlen(cmd->cmd)],
  1314. cmd->schema, params)) {
  1315. return -1;
  1316. }
  1317. }
  1318. cmd->handler(params, user_ctx);
  1319. return 0;
  1320. }
  1321. return -1;
  1322. }
  1323. static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
  1324. {
  1325. if (!data) {
  1326. return;
  1327. }
  1328. g_string_set_size(gdbserver_state.str_buf, 0);
  1329. g_byte_array_set_size(gdbserver_state.mem_buf, 0);
  1330. /* In case there was an error during the command parsing we must
  1331. * send a NULL packet to indicate the command is not supported */
  1332. if (process_string_cmd(NULL, data, cmd, 1)) {
  1333. put_packet("");
  1334. }
  1335. }
  1336. static void handle_detach(GArray *params, void *user_ctx)
  1337. {
  1338. GDBProcess *process;
  1339. uint32_t pid = 1;
  1340. if (gdbserver_state.multiprocess) {
  1341. if (!params->len) {
  1342. put_packet("E22");
  1343. return;
  1344. }
  1345. pid = get_param(params, 0)->val_ul;
  1346. }
  1347. process = gdb_get_process(pid);
  1348. gdb_process_breakpoint_remove_all(process);
  1349. process->attached = false;
  1350. if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
  1351. gdbserver_state.c_cpu = gdb_first_attached_cpu();
  1352. }
  1353. if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
  1354. gdbserver_state.g_cpu = gdb_first_attached_cpu();
  1355. }
  1356. if (!gdbserver_state.c_cpu) {
  1357. /* No more process attached */
  1358. gdb_syscall_mode = GDB_SYS_DISABLED;
  1359. gdb_continue();
  1360. }
  1361. put_packet("OK");
  1362. }
  1363. static void handle_thread_alive(GArray *params, void *user_ctx)
  1364. {
  1365. CPUState *cpu;
  1366. if (!params->len) {
  1367. put_packet("E22");
  1368. return;
  1369. }
  1370. if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
  1371. put_packet("E22");
  1372. return;
  1373. }
  1374. cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
  1375. get_param(params, 0)->thread_id.tid);
  1376. if (!cpu) {
  1377. put_packet("E22");
  1378. return;
  1379. }
  1380. put_packet("OK");
  1381. }
  1382. static void handle_continue(GArray *params, void *user_ctx)
  1383. {
  1384. if (params->len) {
  1385. gdb_set_cpu_pc(get_param(params, 0)->val_ull);
  1386. }
  1387. gdbserver_state.signal = 0;
  1388. gdb_continue();
  1389. }
  1390. static void handle_cont_with_sig(GArray *params, void *user_ctx)
  1391. {
  1392. unsigned long signal = 0;
  1393. /*
  1394. * Note: C sig;[addr] is currently unsupported and we simply
  1395. * omit the addr parameter
  1396. */
  1397. if (params->len) {
  1398. signal = get_param(params, 0)->val_ul;
  1399. }
  1400. gdbserver_state.signal = gdb_signal_to_target(signal);
  1401. if (gdbserver_state.signal == -1) {
  1402. gdbserver_state.signal = 0;
  1403. }
  1404. gdb_continue();
  1405. }
  1406. static void handle_set_thread(GArray *params, void *user_ctx)
  1407. {
  1408. CPUState *cpu;
  1409. if (params->len != 2) {
  1410. put_packet("E22");
  1411. return;
  1412. }
  1413. if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
  1414. put_packet("E22");
  1415. return;
  1416. }
  1417. if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
  1418. put_packet("OK");
  1419. return;
  1420. }
  1421. cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
  1422. get_param(params, 1)->thread_id.tid);
  1423. if (!cpu) {
  1424. put_packet("E22");
  1425. return;
  1426. }
  1427. /*
  1428. * Note: This command is deprecated and modern gdb's will be using the
  1429. * vCont command instead.
  1430. */
  1431. switch (get_param(params, 0)->opcode) {
  1432. case 'c':
  1433. gdbserver_state.c_cpu = cpu;
  1434. put_packet("OK");
  1435. break;
  1436. case 'g':
  1437. gdbserver_state.g_cpu = cpu;
  1438. put_packet("OK");
  1439. break;
  1440. default:
  1441. put_packet("E22");
  1442. break;
  1443. }
  1444. }
  1445. static void handle_insert_bp(GArray *params, void *user_ctx)
  1446. {
  1447. int res;
  1448. if (params->len != 3) {
  1449. put_packet("E22");
  1450. return;
  1451. }
  1452. res = gdb_breakpoint_insert(get_param(params, 0)->val_ul,
  1453. get_param(params, 1)->val_ull,
  1454. get_param(params, 2)->val_ull);
  1455. if (res >= 0) {
  1456. put_packet("OK");
  1457. return;
  1458. } else if (res == -ENOSYS) {
  1459. put_packet("");
  1460. return;
  1461. }
  1462. put_packet("E22");
  1463. }
  1464. static void handle_remove_bp(GArray *params, void *user_ctx)
  1465. {
  1466. int res;
  1467. if (params->len != 3) {
  1468. put_packet("E22");
  1469. return;
  1470. }
  1471. res = gdb_breakpoint_remove(get_param(params, 0)->val_ul,
  1472. get_param(params, 1)->val_ull,
  1473. get_param(params, 2)->val_ull);
  1474. if (res >= 0) {
  1475. put_packet("OK");
  1476. return;
  1477. } else if (res == -ENOSYS) {
  1478. put_packet("");
  1479. return;
  1480. }
  1481. put_packet("E22");
  1482. }
  1483. /*
  1484. * handle_set/get_reg
  1485. *
  1486. * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
  1487. * This works, but can be very slow. Anything new enough to understand
  1488. * XML also knows how to use this properly. However to use this we
  1489. * need to define a local XML file as well as be talking to a
  1490. * reasonably modern gdb. Responding with an empty packet will cause
  1491. * the remote gdb to fallback to older methods.
  1492. */
  1493. static void handle_set_reg(GArray *params, void *user_ctx)
  1494. {
  1495. int reg_size;
  1496. if (!gdb_has_xml) {
  1497. put_packet("");
  1498. return;
  1499. }
  1500. if (params->len != 2) {
  1501. put_packet("E22");
  1502. return;
  1503. }
  1504. reg_size = strlen(get_param(params, 1)->data) / 2;
  1505. hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
  1506. gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
  1507. get_param(params, 0)->val_ull);
  1508. put_packet("OK");
  1509. }
  1510. static void handle_get_reg(GArray *params, void *user_ctx)
  1511. {
  1512. int reg_size;
  1513. if (!gdb_has_xml) {
  1514. put_packet("");
  1515. return;
  1516. }
  1517. if (!params->len) {
  1518. put_packet("E14");
  1519. return;
  1520. }
  1521. reg_size = gdb_read_register(gdbserver_state.g_cpu,
  1522. gdbserver_state.mem_buf,
  1523. get_param(params, 0)->val_ull);
  1524. if (!reg_size) {
  1525. put_packet("E14");
  1526. return;
  1527. } else {
  1528. g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
  1529. }
  1530. memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_size);
  1531. put_strbuf();
  1532. }
  1533. static void handle_write_mem(GArray *params, void *user_ctx)
  1534. {
  1535. if (params->len != 3) {
  1536. put_packet("E22");
  1537. return;
  1538. }
  1539. /* hextomem() reads 2*len bytes */
  1540. if (get_param(params, 1)->val_ull >
  1541. strlen(get_param(params, 2)->data) / 2) {
  1542. put_packet("E22");
  1543. return;
  1544. }
  1545. hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
  1546. get_param(params, 1)->val_ull);
  1547. if (target_memory_rw_debug(gdbserver_state.g_cpu,
  1548. get_param(params, 0)->val_ull,
  1549. gdbserver_state.mem_buf->data,
  1550. gdbserver_state.mem_buf->len, true)) {
  1551. put_packet("E14");
  1552. return;
  1553. }
  1554. put_packet("OK");
  1555. }
  1556. static void handle_read_mem(GArray *params, void *user_ctx)
  1557. {
  1558. if (params->len != 2) {
  1559. put_packet("E22");
  1560. return;
  1561. }
  1562. /* memtohex() doubles the required space */
  1563. if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
  1564. put_packet("E22");
  1565. return;
  1566. }
  1567. g_byte_array_set_size(gdbserver_state.mem_buf,
  1568. get_param(params, 1)->val_ull);
  1569. if (target_memory_rw_debug(gdbserver_state.g_cpu,
  1570. get_param(params, 0)->val_ull,
  1571. gdbserver_state.mem_buf->data,
  1572. gdbserver_state.mem_buf->len, false)) {
  1573. put_packet("E14");
  1574. return;
  1575. }
  1576. memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
  1577. gdbserver_state.mem_buf->len);
  1578. put_strbuf();
  1579. }
  1580. static void handle_write_all_regs(GArray *params, void *user_ctx)
  1581. {
  1582. target_ulong addr, len;
  1583. uint8_t *registers;
  1584. int reg_size;
  1585. if (!params->len) {
  1586. return;
  1587. }
  1588. cpu_synchronize_state(gdbserver_state.g_cpu);
  1589. len = strlen(get_param(params, 0)->data) / 2;
  1590. hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
  1591. registers = gdbserver_state.mem_buf->data;
  1592. for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
  1593. addr++) {
  1594. reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
  1595. len -= reg_size;
  1596. registers += reg_size;
  1597. }
  1598. put_packet("OK");
  1599. }
  1600. static void handle_read_all_regs(GArray *params, void *user_ctx)
  1601. {
  1602. target_ulong addr, len;
  1603. cpu_synchronize_state(gdbserver_state.g_cpu);
  1604. g_byte_array_set_size(gdbserver_state.mem_buf, 0);
  1605. len = 0;
  1606. for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
  1607. len += gdb_read_register(gdbserver_state.g_cpu,
  1608. gdbserver_state.mem_buf,
  1609. addr);
  1610. }
  1611. g_assert(len == gdbserver_state.mem_buf->len);
  1612. memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
  1613. put_strbuf();
  1614. }
  1615. static void handle_file_io(GArray *params, void *user_ctx)
  1616. {
  1617. if (params->len >= 1 && gdbserver_state.current_syscall_cb) {
  1618. target_ulong ret, err;
  1619. ret = (target_ulong)get_param(params, 0)->val_ull;
  1620. if (params->len >= 2) {
  1621. err = (target_ulong)get_param(params, 1)->val_ull;
  1622. } else {
  1623. err = 0;
  1624. }
  1625. gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
  1626. gdbserver_state.current_syscall_cb = NULL;
  1627. }
  1628. if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') {
  1629. put_packet("T02");
  1630. return;
  1631. }
  1632. gdb_continue();
  1633. }
  1634. static void handle_step(GArray *params, void *user_ctx)
  1635. {
  1636. if (params->len) {
  1637. gdb_set_cpu_pc((target_ulong)get_param(params, 0)->val_ull);
  1638. }
  1639. cpu_single_step(gdbserver_state.c_cpu, get_sstep_flags());
  1640. gdb_continue();
  1641. }
  1642. static void handle_backward(GArray *params, void *user_ctx)
  1643. {
  1644. if (!stub_can_reverse()) {
  1645. put_packet("E22");
  1646. }
  1647. if (params->len == 1) {
  1648. switch (get_param(params, 0)->opcode) {
  1649. case 's':
  1650. if (replay_reverse_step()) {
  1651. gdb_continue();
  1652. } else {
  1653. put_packet("E14");
  1654. }
  1655. return;
  1656. case 'c':
  1657. if (replay_reverse_continue()) {
  1658. gdb_continue();
  1659. } else {
  1660. put_packet("E14");
  1661. }
  1662. return;
  1663. }
  1664. }
  1665. /* Default invalid command */
  1666. put_packet("");
  1667. }
  1668. static void handle_v_cont_query(GArray *params, void *user_ctx)
  1669. {
  1670. put_packet("vCont;c;C;s;S");
  1671. }
  1672. static void handle_v_cont(GArray *params, void *user_ctx)
  1673. {
  1674. int res;
  1675. if (!params->len) {
  1676. return;
  1677. }
  1678. res = gdb_handle_vcont(get_param(params, 0)->data);
  1679. if ((res == -EINVAL) || (res == -ERANGE)) {
  1680. put_packet("E22");
  1681. } else if (res) {
  1682. put_packet("");
  1683. }
  1684. }
  1685. static void handle_v_attach(GArray *params, void *user_ctx)
  1686. {
  1687. GDBProcess *process;
  1688. CPUState *cpu;
  1689. g_string_assign(gdbserver_state.str_buf, "E22");
  1690. if (!params->len) {
  1691. goto cleanup;
  1692. }
  1693. process = gdb_get_process(get_param(params, 0)->val_ul);
  1694. if (!process) {
  1695. goto cleanup;
  1696. }
  1697. cpu = get_first_cpu_in_process(process);
  1698. if (!cpu) {
  1699. goto cleanup;
  1700. }
  1701. process->attached = true;
  1702. gdbserver_state.g_cpu = cpu;
  1703. gdbserver_state.c_cpu = cpu;
  1704. g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
  1705. gdb_append_thread_id(cpu, gdbserver_state.str_buf);
  1706. g_string_append_c(gdbserver_state.str_buf, ';');
  1707. cleanup:
  1708. put_strbuf();
  1709. }
  1710. static void handle_v_kill(GArray *params, void *user_ctx)
  1711. {
  1712. /* Kill the target */
  1713. put_packet("OK");
  1714. error_report("QEMU: Terminated via GDBstub");
  1715. gdb_exit(0);
  1716. exit(0);
  1717. }
  1718. static const GdbCmdParseEntry gdb_v_commands_table[] = {
  1719. /* Order is important if has same prefix */
  1720. {
  1721. .handler = handle_v_cont_query,
  1722. .cmd = "Cont?",
  1723. .cmd_startswith = 1
  1724. },
  1725. {
  1726. .handler = handle_v_cont,
  1727. .cmd = "Cont",
  1728. .cmd_startswith = 1,
  1729. .schema = "s0"
  1730. },
  1731. {
  1732. .handler = handle_v_attach,
  1733. .cmd = "Attach;",
  1734. .cmd_startswith = 1,
  1735. .schema = "l0"
  1736. },
  1737. {
  1738. .handler = handle_v_kill,
  1739. .cmd = "Kill;",
  1740. .cmd_startswith = 1
  1741. },
  1742. };
  1743. static void handle_v_commands(GArray *params, void *user_ctx)
  1744. {
  1745. if (!params->len) {
  1746. return;
  1747. }
  1748. if (process_string_cmd(NULL, get_param(params, 0)->data,
  1749. gdb_v_commands_table,
  1750. ARRAY_SIZE(gdb_v_commands_table))) {
  1751. put_packet("");
  1752. }
  1753. }
  1754. static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
  1755. {
  1756. g_string_printf(gdbserver_state.str_buf, "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
  1757. SSTEP_ENABLE, SSTEP_NOIRQ, SSTEP_NOTIMER);
  1758. put_strbuf();
  1759. }
  1760. static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
  1761. {
  1762. if (!params->len) {
  1763. return;
  1764. }
  1765. sstep_flags = get_param(params, 0)->val_ul;
  1766. put_packet("OK");
  1767. }
  1768. static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
  1769. {
  1770. g_string_printf(gdbserver_state.str_buf, "0x%x", sstep_flags);
  1771. put_strbuf();
  1772. }
  1773. static void handle_query_curr_tid(GArray *params, void *user_ctx)
  1774. {
  1775. CPUState *cpu;
  1776. GDBProcess *process;
  1777. /*
  1778. * "Current thread" remains vague in the spec, so always return
  1779. * the first thread of the current process (gdb returns the
  1780. * first thread).
  1781. */
  1782. process = gdb_get_cpu_process(gdbserver_state.g_cpu);
  1783. cpu = get_first_cpu_in_process(process);
  1784. g_string_assign(gdbserver_state.str_buf, "QC");
  1785. gdb_append_thread_id(cpu, gdbserver_state.str_buf);
  1786. put_strbuf();
  1787. }
  1788. static void handle_query_threads(GArray *params, void *user_ctx)
  1789. {
  1790. if (!gdbserver_state.query_cpu) {
  1791. put_packet("l");
  1792. return;
  1793. }
  1794. g_string_assign(gdbserver_state.str_buf, "m");
  1795. gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
  1796. put_strbuf();
  1797. gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
  1798. }
  1799. static void handle_query_first_threads(GArray *params, void *user_ctx)
  1800. {
  1801. gdbserver_state.query_cpu = gdb_first_attached_cpu();
  1802. handle_query_threads(params, user_ctx);
  1803. }
  1804. static void handle_query_thread_extra(GArray *params, void *user_ctx)
  1805. {
  1806. g_autoptr(GString) rs = g_string_new(NULL);
  1807. CPUState *cpu;
  1808. if (!params->len ||
  1809. get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
  1810. put_packet("E22");
  1811. return;
  1812. }
  1813. cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
  1814. get_param(params, 0)->thread_id.tid);
  1815. if (!cpu) {
  1816. return;
  1817. }
  1818. cpu_synchronize_state(cpu);
  1819. if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
  1820. /* Print the CPU model and name in multiprocess mode */
  1821. ObjectClass *oc = object_get_class(OBJECT(cpu));
  1822. const char *cpu_model = object_class_get_name(oc);
  1823. const char *cpu_name =
  1824. object_get_canonical_path_component(OBJECT(cpu));
  1825. g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
  1826. cpu->halted ? "halted " : "running");
  1827. } else {
  1828. g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
  1829. cpu->halted ? "halted " : "running");
  1830. }
  1831. trace_gdbstub_op_extra_info(rs->str);
  1832. memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
  1833. put_strbuf();
  1834. }
  1835. #ifdef CONFIG_USER_ONLY
  1836. static void handle_query_offsets(GArray *params, void *user_ctx)
  1837. {
  1838. TaskState *ts;
  1839. ts = gdbserver_state.c_cpu->opaque;
  1840. g_string_printf(gdbserver_state.str_buf,
  1841. "Text=" TARGET_ABI_FMT_lx
  1842. ";Data=" TARGET_ABI_FMT_lx
  1843. ";Bss=" TARGET_ABI_FMT_lx,
  1844. ts->info->code_offset,
  1845. ts->info->data_offset,
  1846. ts->info->data_offset);
  1847. put_strbuf();
  1848. }
  1849. #else
  1850. static void handle_query_rcmd(GArray *params, void *user_ctx)
  1851. {
  1852. const guint8 zero = 0;
  1853. int len;
  1854. if (!params->len) {
  1855. put_packet("E22");
  1856. return;
  1857. }
  1858. len = strlen(get_param(params, 0)->data);
  1859. if (len % 2) {
  1860. put_packet("E01");
  1861. return;
  1862. }
  1863. g_assert(gdbserver_state.mem_buf->len == 0);
  1864. len = len / 2;
  1865. hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
  1866. g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
  1867. qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data,
  1868. gdbserver_state.mem_buf->len);
  1869. put_packet("OK");
  1870. }
  1871. #endif
  1872. static void handle_query_supported(GArray *params, void *user_ctx)
  1873. {
  1874. CPUClass *cc;
  1875. g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
  1876. cc = CPU_GET_CLASS(first_cpu);
  1877. if (cc->gdb_core_xml_file) {
  1878. g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
  1879. }
  1880. if (stub_can_reverse()) {
  1881. g_string_append(gdbserver_state.str_buf,
  1882. ";ReverseStep+;ReverseContinue+");
  1883. }
  1884. #ifdef CONFIG_USER_ONLY
  1885. if (gdbserver_state.c_cpu->opaque) {
  1886. g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
  1887. }
  1888. #endif
  1889. if (params->len &&
  1890. strstr(get_param(params, 0)->data, "multiprocess+")) {
  1891. gdbserver_state.multiprocess = true;
  1892. }
  1893. g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
  1894. put_strbuf();
  1895. }
  1896. static void handle_query_xfer_features(GArray *params, void *user_ctx)
  1897. {
  1898. GDBProcess *process;
  1899. CPUClass *cc;
  1900. unsigned long len, total_len, addr;
  1901. const char *xml;
  1902. const char *p;
  1903. if (params->len < 3) {
  1904. put_packet("E22");
  1905. return;
  1906. }
  1907. process = gdb_get_cpu_process(gdbserver_state.g_cpu);
  1908. cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
  1909. if (!cc->gdb_core_xml_file) {
  1910. put_packet("");
  1911. return;
  1912. }
  1913. gdb_has_xml = true;
  1914. p = get_param(params, 0)->data;
  1915. xml = get_feature_xml(p, &p, process);
  1916. if (!xml) {
  1917. put_packet("E00");
  1918. return;
  1919. }
  1920. addr = get_param(params, 1)->val_ul;
  1921. len = get_param(params, 2)->val_ul;
  1922. total_len = strlen(xml);
  1923. if (addr > total_len) {
  1924. put_packet("E00");
  1925. return;
  1926. }
  1927. if (len > (MAX_PACKET_LENGTH - 5) / 2) {
  1928. len = (MAX_PACKET_LENGTH - 5) / 2;
  1929. }
  1930. if (len < total_len - addr) {
  1931. g_string_assign(gdbserver_state.str_buf, "m");
  1932. memtox(gdbserver_state.str_buf, xml + addr, len);
  1933. } else {
  1934. g_string_assign(gdbserver_state.str_buf, "l");
  1935. memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
  1936. }
  1937. put_packet_binary(gdbserver_state.str_buf->str,
  1938. gdbserver_state.str_buf->len, true);
  1939. }
  1940. #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
  1941. static void handle_query_xfer_auxv(GArray *params, void *user_ctx)
  1942. {
  1943. TaskState *ts;
  1944. unsigned long offset, len, saved_auxv, auxv_len;
  1945. if (params->len < 2) {
  1946. put_packet("E22");
  1947. return;
  1948. }
  1949. offset = get_param(params, 0)->val_ul;
  1950. len = get_param(params, 1)->val_ul;
  1951. ts = gdbserver_state.c_cpu->opaque;
  1952. saved_auxv = ts->info->saved_auxv;
  1953. auxv_len = ts->info->auxv_len;
  1954. if (offset >= auxv_len) {
  1955. put_packet("E00");
  1956. return;
  1957. }
  1958. if (len > (MAX_PACKET_LENGTH - 5) / 2) {
  1959. len = (MAX_PACKET_LENGTH - 5) / 2;
  1960. }
  1961. if (len < auxv_len - offset) {
  1962. g_string_assign(gdbserver_state.str_buf, "m");
  1963. } else {
  1964. g_string_assign(gdbserver_state.str_buf, "l");
  1965. len = auxv_len - offset;
  1966. }
  1967. g_byte_array_set_size(gdbserver_state.mem_buf, len);
  1968. if (target_memory_rw_debug(gdbserver_state.g_cpu, saved_auxv + offset,
  1969. gdbserver_state.mem_buf->data, len, false)) {
  1970. put_packet("E14");
  1971. return;
  1972. }
  1973. memtox(gdbserver_state.str_buf,
  1974. (const char *)gdbserver_state.mem_buf->data, len);
  1975. put_packet_binary(gdbserver_state.str_buf->str,
  1976. gdbserver_state.str_buf->len, true);
  1977. }
  1978. #endif
  1979. static void handle_query_attached(GArray *params, void *user_ctx)
  1980. {
  1981. put_packet(GDB_ATTACHED);
  1982. }
  1983. static void handle_query_qemu_supported(GArray *params, void *user_ctx)
  1984. {
  1985. g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
  1986. #ifndef CONFIG_USER_ONLY
  1987. g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
  1988. #endif
  1989. put_strbuf();
  1990. }
  1991. #ifndef CONFIG_USER_ONLY
  1992. static void handle_query_qemu_phy_mem_mode(GArray *params,
  1993. void *user_ctx)
  1994. {
  1995. g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
  1996. put_strbuf();
  1997. }
  1998. static void handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx)
  1999. {
  2000. if (!params->len) {
  2001. put_packet("E22");
  2002. return;
  2003. }
  2004. if (!get_param(params, 0)->val_ul) {
  2005. phy_memory_mode = 0;
  2006. } else {
  2007. phy_memory_mode = 1;
  2008. }
  2009. put_packet("OK");
  2010. }
  2011. #endif
  2012. static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
  2013. /* Order is important if has same prefix */
  2014. {
  2015. .handler = handle_query_qemu_sstepbits,
  2016. .cmd = "qemu.sstepbits",
  2017. },
  2018. {
  2019. .handler = handle_query_qemu_sstep,
  2020. .cmd = "qemu.sstep",
  2021. },
  2022. {
  2023. .handler = handle_set_qemu_sstep,
  2024. .cmd = "qemu.sstep=",
  2025. .cmd_startswith = 1,
  2026. .schema = "l0"
  2027. },
  2028. };
  2029. static const GdbCmdParseEntry gdb_gen_query_table[] = {
  2030. {
  2031. .handler = handle_query_curr_tid,
  2032. .cmd = "C",
  2033. },
  2034. {
  2035. .handler = handle_query_threads,
  2036. .cmd = "sThreadInfo",
  2037. },
  2038. {
  2039. .handler = handle_query_first_threads,
  2040. .cmd = "fThreadInfo",
  2041. },
  2042. {
  2043. .handler = handle_query_thread_extra,
  2044. .cmd = "ThreadExtraInfo,",
  2045. .cmd_startswith = 1,
  2046. .schema = "t0"
  2047. },
  2048. #ifdef CONFIG_USER_ONLY
  2049. {
  2050. .handler = handle_query_offsets,
  2051. .cmd = "Offsets",
  2052. },
  2053. #else
  2054. {
  2055. .handler = handle_query_rcmd,
  2056. .cmd = "Rcmd,",
  2057. .cmd_startswith = 1,
  2058. .schema = "s0"
  2059. },
  2060. #endif
  2061. {
  2062. .handler = handle_query_supported,
  2063. .cmd = "Supported:",
  2064. .cmd_startswith = 1,
  2065. .schema = "s0"
  2066. },
  2067. {
  2068. .handler = handle_query_supported,
  2069. .cmd = "Supported",
  2070. .schema = "s0"
  2071. },
  2072. {
  2073. .handler = handle_query_xfer_features,
  2074. .cmd = "Xfer:features:read:",
  2075. .cmd_startswith = 1,
  2076. .schema = "s:l,l0"
  2077. },
  2078. #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
  2079. {
  2080. .handler = handle_query_xfer_auxv,
  2081. .cmd = "Xfer:auxv:read::",
  2082. .cmd_startswith = 1,
  2083. .schema = "l,l0"
  2084. },
  2085. #endif
  2086. {
  2087. .handler = handle_query_attached,
  2088. .cmd = "Attached:",
  2089. .cmd_startswith = 1
  2090. },
  2091. {
  2092. .handler = handle_query_attached,
  2093. .cmd = "Attached",
  2094. },
  2095. {
  2096. .handler = handle_query_qemu_supported,
  2097. .cmd = "qemu.Supported",
  2098. },
  2099. #ifndef CONFIG_USER_ONLY
  2100. {
  2101. .handler = handle_query_qemu_phy_mem_mode,
  2102. .cmd = "qemu.PhyMemMode",
  2103. },
  2104. #endif
  2105. };
  2106. static const GdbCmdParseEntry gdb_gen_set_table[] = {
  2107. /* Order is important if has same prefix */
  2108. {
  2109. .handler = handle_set_qemu_sstep,
  2110. .cmd = "qemu.sstep:",
  2111. .cmd_startswith = 1,
  2112. .schema = "l0"
  2113. },
  2114. #ifndef CONFIG_USER_ONLY
  2115. {
  2116. .handler = handle_set_qemu_phy_mem_mode,
  2117. .cmd = "qemu.PhyMemMode:",
  2118. .cmd_startswith = 1,
  2119. .schema = "l0"
  2120. },
  2121. #endif
  2122. };
  2123. static void handle_gen_query(GArray *params, void *user_ctx)
  2124. {
  2125. if (!params->len) {
  2126. return;
  2127. }
  2128. if (!process_string_cmd(NULL, get_param(params, 0)->data,
  2129. gdb_gen_query_set_common_table,
  2130. ARRAY_SIZE(gdb_gen_query_set_common_table))) {
  2131. return;
  2132. }
  2133. if (process_string_cmd(NULL, get_param(params, 0)->data,
  2134. gdb_gen_query_table,
  2135. ARRAY_SIZE(gdb_gen_query_table))) {
  2136. put_packet("");
  2137. }
  2138. }
  2139. static void handle_gen_set(GArray *params, void *user_ctx)
  2140. {
  2141. if (!params->len) {
  2142. return;
  2143. }
  2144. if (!process_string_cmd(NULL, get_param(params, 0)->data,
  2145. gdb_gen_query_set_common_table,
  2146. ARRAY_SIZE(gdb_gen_query_set_common_table))) {
  2147. return;
  2148. }
  2149. if (process_string_cmd(NULL, get_param(params, 0)->data,
  2150. gdb_gen_set_table,
  2151. ARRAY_SIZE(gdb_gen_set_table))) {
  2152. put_packet("");
  2153. }
  2154. }
  2155. static void handle_target_halt(GArray *params, void *user_ctx)
  2156. {
  2157. g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
  2158. gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
  2159. g_string_append_c(gdbserver_state.str_buf, ';');
  2160. put_strbuf();
  2161. /*
  2162. * Remove all the breakpoints when this query is issued,
  2163. * because gdb is doing an initial connect and the state
  2164. * should be cleaned up.
  2165. */
  2166. gdb_breakpoint_remove_all();
  2167. }
  2168. static int gdb_handle_packet(const char *line_buf)
  2169. {
  2170. const GdbCmdParseEntry *cmd_parser = NULL;
  2171. trace_gdbstub_io_command(line_buf);
  2172. switch (line_buf[0]) {
  2173. case '!':
  2174. put_packet("OK");
  2175. break;
  2176. case '?':
  2177. {
  2178. static const GdbCmdParseEntry target_halted_cmd_desc = {
  2179. .handler = handle_target_halt,
  2180. .cmd = "?",
  2181. .cmd_startswith = 1
  2182. };
  2183. cmd_parser = &target_halted_cmd_desc;
  2184. }
  2185. break;
  2186. case 'c':
  2187. {
  2188. static const GdbCmdParseEntry continue_cmd_desc = {
  2189. .handler = handle_continue,
  2190. .cmd = "c",
  2191. .cmd_startswith = 1,
  2192. .schema = "L0"
  2193. };
  2194. cmd_parser = &continue_cmd_desc;
  2195. }
  2196. break;
  2197. case 'C':
  2198. {
  2199. static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
  2200. .handler = handle_cont_with_sig,
  2201. .cmd = "C",
  2202. .cmd_startswith = 1,
  2203. .schema = "l0"
  2204. };
  2205. cmd_parser = &cont_with_sig_cmd_desc;
  2206. }
  2207. break;
  2208. case 'v':
  2209. {
  2210. static const GdbCmdParseEntry v_cmd_desc = {
  2211. .handler = handle_v_commands,
  2212. .cmd = "v",
  2213. .cmd_startswith = 1,
  2214. .schema = "s0"
  2215. };
  2216. cmd_parser = &v_cmd_desc;
  2217. }
  2218. break;
  2219. case 'k':
  2220. /* Kill the target */
  2221. error_report("QEMU: Terminated via GDBstub");
  2222. gdb_exit(0);
  2223. exit(0);
  2224. case 'D':
  2225. {
  2226. static const GdbCmdParseEntry detach_cmd_desc = {
  2227. .handler = handle_detach,
  2228. .cmd = "D",
  2229. .cmd_startswith = 1,
  2230. .schema = "?.l0"
  2231. };
  2232. cmd_parser = &detach_cmd_desc;
  2233. }
  2234. break;
  2235. case 's':
  2236. {
  2237. static const GdbCmdParseEntry step_cmd_desc = {
  2238. .handler = handle_step,
  2239. .cmd = "s",
  2240. .cmd_startswith = 1,
  2241. .schema = "L0"
  2242. };
  2243. cmd_parser = &step_cmd_desc;
  2244. }
  2245. break;
  2246. case 'b':
  2247. {
  2248. static const GdbCmdParseEntry backward_cmd_desc = {
  2249. .handler = handle_backward,
  2250. .cmd = "b",
  2251. .cmd_startswith = 1,
  2252. .schema = "o0"
  2253. };
  2254. cmd_parser = &backward_cmd_desc;
  2255. }
  2256. break;
  2257. case 'F':
  2258. {
  2259. static const GdbCmdParseEntry file_io_cmd_desc = {
  2260. .handler = handle_file_io,
  2261. .cmd = "F",
  2262. .cmd_startswith = 1,
  2263. .schema = "L,L,o0"
  2264. };
  2265. cmd_parser = &file_io_cmd_desc;
  2266. }
  2267. break;
  2268. case 'g':
  2269. {
  2270. static const GdbCmdParseEntry read_all_regs_cmd_desc = {
  2271. .handler = handle_read_all_regs,
  2272. .cmd = "g",
  2273. .cmd_startswith = 1
  2274. };
  2275. cmd_parser = &read_all_regs_cmd_desc;
  2276. }
  2277. break;
  2278. case 'G':
  2279. {
  2280. static const GdbCmdParseEntry write_all_regs_cmd_desc = {
  2281. .handler = handle_write_all_regs,
  2282. .cmd = "G",
  2283. .cmd_startswith = 1,
  2284. .schema = "s0"
  2285. };
  2286. cmd_parser = &write_all_regs_cmd_desc;
  2287. }
  2288. break;
  2289. case 'm':
  2290. {
  2291. static const GdbCmdParseEntry read_mem_cmd_desc = {
  2292. .handler = handle_read_mem,
  2293. .cmd = "m",
  2294. .cmd_startswith = 1,
  2295. .schema = "L,L0"
  2296. };
  2297. cmd_parser = &read_mem_cmd_desc;
  2298. }
  2299. break;
  2300. case 'M':
  2301. {
  2302. static const GdbCmdParseEntry write_mem_cmd_desc = {
  2303. .handler = handle_write_mem,
  2304. .cmd = "M",
  2305. .cmd_startswith = 1,
  2306. .schema = "L,L:s0"
  2307. };
  2308. cmd_parser = &write_mem_cmd_desc;
  2309. }
  2310. break;
  2311. case 'p':
  2312. {
  2313. static const GdbCmdParseEntry get_reg_cmd_desc = {
  2314. .handler = handle_get_reg,
  2315. .cmd = "p",
  2316. .cmd_startswith = 1,
  2317. .schema = "L0"
  2318. };
  2319. cmd_parser = &get_reg_cmd_desc;
  2320. }
  2321. break;
  2322. case 'P':
  2323. {
  2324. static const GdbCmdParseEntry set_reg_cmd_desc = {
  2325. .handler = handle_set_reg,
  2326. .cmd = "P",
  2327. .cmd_startswith = 1,
  2328. .schema = "L?s0"
  2329. };
  2330. cmd_parser = &set_reg_cmd_desc;
  2331. }
  2332. break;
  2333. case 'Z':
  2334. {
  2335. static const GdbCmdParseEntry insert_bp_cmd_desc = {
  2336. .handler = handle_insert_bp,
  2337. .cmd = "Z",
  2338. .cmd_startswith = 1,
  2339. .schema = "l?L?L0"
  2340. };
  2341. cmd_parser = &insert_bp_cmd_desc;
  2342. }
  2343. break;
  2344. case 'z':
  2345. {
  2346. static const GdbCmdParseEntry remove_bp_cmd_desc = {
  2347. .handler = handle_remove_bp,
  2348. .cmd = "z",
  2349. .cmd_startswith = 1,
  2350. .schema = "l?L?L0"
  2351. };
  2352. cmd_parser = &remove_bp_cmd_desc;
  2353. }
  2354. break;
  2355. case 'H':
  2356. {
  2357. static const GdbCmdParseEntry set_thread_cmd_desc = {
  2358. .handler = handle_set_thread,
  2359. .cmd = "H",
  2360. .cmd_startswith = 1,
  2361. .schema = "o.t0"
  2362. };
  2363. cmd_parser = &set_thread_cmd_desc;
  2364. }
  2365. break;
  2366. case 'T':
  2367. {
  2368. static const GdbCmdParseEntry thread_alive_cmd_desc = {
  2369. .handler = handle_thread_alive,
  2370. .cmd = "T",
  2371. .cmd_startswith = 1,
  2372. .schema = "t0"
  2373. };
  2374. cmd_parser = &thread_alive_cmd_desc;
  2375. }
  2376. break;
  2377. case 'q':
  2378. {
  2379. static const GdbCmdParseEntry gen_query_cmd_desc = {
  2380. .handler = handle_gen_query,
  2381. .cmd = "q",
  2382. .cmd_startswith = 1,
  2383. .schema = "s0"
  2384. };
  2385. cmd_parser = &gen_query_cmd_desc;
  2386. }
  2387. break;
  2388. case 'Q':
  2389. {
  2390. static const GdbCmdParseEntry gen_set_cmd_desc = {
  2391. .handler = handle_gen_set,
  2392. .cmd = "Q",
  2393. .cmd_startswith = 1,
  2394. .schema = "s0"
  2395. };
  2396. cmd_parser = &gen_set_cmd_desc;
  2397. }
  2398. break;
  2399. default:
  2400. /* put empty packet */
  2401. put_packet("");
  2402. break;
  2403. }
  2404. if (cmd_parser) {
  2405. run_cmd_parser(line_buf, cmd_parser);
  2406. }
  2407. return RS_IDLE;
  2408. }
  2409. void gdb_set_stop_cpu(CPUState *cpu)
  2410. {
  2411. GDBProcess *p = gdb_get_cpu_process(cpu);
  2412. if (!p->attached) {
  2413. /*
  2414. * Having a stop CPU corresponding to a process that is not attached
  2415. * confuses GDB. So we ignore the request.
  2416. */
  2417. return;
  2418. }
  2419. gdbserver_state.c_cpu = cpu;
  2420. gdbserver_state.g_cpu = cpu;
  2421. }
  2422. #ifndef CONFIG_USER_ONLY
  2423. static void gdb_vm_state_change(void *opaque, bool running, RunState state)
  2424. {
  2425. CPUState *cpu = gdbserver_state.c_cpu;
  2426. g_autoptr(GString) buf = g_string_new(NULL);
  2427. g_autoptr(GString) tid = g_string_new(NULL);
  2428. const char *type;
  2429. int ret;
  2430. if (running || gdbserver_state.state == RS_INACTIVE) {
  2431. return;
  2432. }
  2433. /* Is there a GDB syscall waiting to be sent? */
  2434. if (gdbserver_state.current_syscall_cb) {
  2435. put_packet(gdbserver_state.syscall_buf);
  2436. return;
  2437. }
  2438. if (cpu == NULL) {
  2439. /* No process attached */
  2440. return;
  2441. }
  2442. gdb_append_thread_id(cpu, tid);
  2443. switch (state) {
  2444. case RUN_STATE_DEBUG:
  2445. if (cpu->watchpoint_hit) {
  2446. switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
  2447. case BP_MEM_READ:
  2448. type = "r";
  2449. break;
  2450. case BP_MEM_ACCESS:
  2451. type = "a";
  2452. break;
  2453. default:
  2454. type = "";
  2455. break;
  2456. }
  2457. trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
  2458. (target_ulong)cpu->watchpoint_hit->vaddr);
  2459. g_string_printf(buf, "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
  2460. GDB_SIGNAL_TRAP, tid->str, type,
  2461. (target_ulong)cpu->watchpoint_hit->vaddr);
  2462. cpu->watchpoint_hit = NULL;
  2463. goto send_packet;
  2464. } else {
  2465. trace_gdbstub_hit_break();
  2466. }
  2467. tb_flush(cpu);
  2468. ret = GDB_SIGNAL_TRAP;
  2469. break;
  2470. case RUN_STATE_PAUSED:
  2471. trace_gdbstub_hit_paused();
  2472. ret = GDB_SIGNAL_INT;
  2473. break;
  2474. case RUN_STATE_SHUTDOWN:
  2475. trace_gdbstub_hit_shutdown();
  2476. ret = GDB_SIGNAL_QUIT;
  2477. break;
  2478. case RUN_STATE_IO_ERROR:
  2479. trace_gdbstub_hit_io_error();
  2480. ret = GDB_SIGNAL_IO;
  2481. break;
  2482. case RUN_STATE_WATCHDOG:
  2483. trace_gdbstub_hit_watchdog();
  2484. ret = GDB_SIGNAL_ALRM;
  2485. break;
  2486. case RUN_STATE_INTERNAL_ERROR:
  2487. trace_gdbstub_hit_internal_error();
  2488. ret = GDB_SIGNAL_ABRT;
  2489. break;
  2490. case RUN_STATE_SAVE_VM:
  2491. case RUN_STATE_RESTORE_VM:
  2492. return;
  2493. case RUN_STATE_FINISH_MIGRATE:
  2494. ret = GDB_SIGNAL_XCPU;
  2495. break;
  2496. default:
  2497. trace_gdbstub_hit_unknown(state);
  2498. ret = GDB_SIGNAL_UNKNOWN;
  2499. break;
  2500. }
  2501. gdb_set_stop_cpu(cpu);
  2502. g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
  2503. send_packet:
  2504. put_packet(buf->str);
  2505. /* disable single step if it was enabled */
  2506. cpu_single_step(cpu, 0);
  2507. }
  2508. #endif
  2509. /* Send a gdb syscall request.
  2510. This accepts limited printf-style format specifiers, specifically:
  2511. %x - target_ulong argument printed in hex.
  2512. %lx - 64-bit argument printed in hex.
  2513. %s - string pointer (target_ulong) and length (int) pair. */
  2514. void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
  2515. {
  2516. char *p;
  2517. char *p_end;
  2518. target_ulong addr;
  2519. uint64_t i64;
  2520. if (!gdbserver_state.init) {
  2521. return;
  2522. }
  2523. gdbserver_state.current_syscall_cb = cb;
  2524. #ifndef CONFIG_USER_ONLY
  2525. vm_stop(RUN_STATE_DEBUG);
  2526. #endif
  2527. p = &gdbserver_state.syscall_buf[0];
  2528. p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
  2529. *(p++) = 'F';
  2530. while (*fmt) {
  2531. if (*fmt == '%') {
  2532. fmt++;
  2533. switch (*fmt++) {
  2534. case 'x':
  2535. addr = va_arg(va, target_ulong);
  2536. p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
  2537. break;
  2538. case 'l':
  2539. if (*(fmt++) != 'x')
  2540. goto bad_format;
  2541. i64 = va_arg(va, uint64_t);
  2542. p += snprintf(p, p_end - p, "%" PRIx64, i64);
  2543. break;
  2544. case 's':
  2545. addr = va_arg(va, target_ulong);
  2546. p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
  2547. addr, va_arg(va, int));
  2548. break;
  2549. default:
  2550. bad_format:
  2551. error_report("gdbstub: Bad syscall format string '%s'",
  2552. fmt - 1);
  2553. break;
  2554. }
  2555. } else {
  2556. *(p++) = *(fmt++);
  2557. }
  2558. }
  2559. *p = 0;
  2560. #ifdef CONFIG_USER_ONLY
  2561. put_packet(gdbserver_state.syscall_buf);
  2562. /* Return control to gdb for it to process the syscall request.
  2563. * Since the protocol requires that gdb hands control back to us
  2564. * using a "here are the results" F packet, we don't need to check
  2565. * gdb_handlesig's return value (which is the signal to deliver if
  2566. * execution was resumed via a continue packet).
  2567. */
  2568. gdb_handlesig(gdbserver_state.c_cpu, 0);
  2569. #else
  2570. /* In this case wait to send the syscall packet until notification that
  2571. the CPU has stopped. This must be done because if the packet is sent
  2572. now the reply from the syscall request could be received while the CPU
  2573. is still in the running state, which can cause packets to be dropped
  2574. and state transition 'T' packets to be sent while the syscall is still
  2575. being processed. */
  2576. qemu_cpu_kick(gdbserver_state.c_cpu);
  2577. #endif
  2578. }
  2579. void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
  2580. {
  2581. va_list va;
  2582. va_start(va, fmt);
  2583. gdb_do_syscallv(cb, fmt, va);
  2584. va_end(va);
  2585. }
  2586. static void gdb_read_byte(uint8_t ch)
  2587. {
  2588. uint8_t reply;
  2589. #ifndef CONFIG_USER_ONLY
  2590. if (gdbserver_state.last_packet->len) {
  2591. /* Waiting for a response to the last packet. If we see the start
  2592. of a new command then abandon the previous response. */
  2593. if (ch == '-') {
  2594. trace_gdbstub_err_got_nack();
  2595. put_buffer(gdbserver_state.last_packet->data,
  2596. gdbserver_state.last_packet->len);
  2597. } else if (ch == '+') {
  2598. trace_gdbstub_io_got_ack();
  2599. } else {
  2600. trace_gdbstub_io_got_unexpected(ch);
  2601. }
  2602. if (ch == '+' || ch == '$') {
  2603. g_byte_array_set_size(gdbserver_state.last_packet, 0);
  2604. }
  2605. if (ch != '$')
  2606. return;
  2607. }
  2608. if (runstate_is_running()) {
  2609. /* when the CPU is running, we cannot do anything except stop
  2610. it when receiving a char */
  2611. vm_stop(RUN_STATE_PAUSED);
  2612. } else
  2613. #endif
  2614. {
  2615. switch(gdbserver_state.state) {
  2616. case RS_IDLE:
  2617. if (ch == '$') {
  2618. /* start of command packet */
  2619. gdbserver_state.line_buf_index = 0;
  2620. gdbserver_state.line_sum = 0;
  2621. gdbserver_state.state = RS_GETLINE;
  2622. } else {
  2623. trace_gdbstub_err_garbage(ch);
  2624. }
  2625. break;
  2626. case RS_GETLINE:
  2627. if (ch == '}') {
  2628. /* start escape sequence */
  2629. gdbserver_state.state = RS_GETLINE_ESC;
  2630. gdbserver_state.line_sum += ch;
  2631. } else if (ch == '*') {
  2632. /* start run length encoding sequence */
  2633. gdbserver_state.state = RS_GETLINE_RLE;
  2634. gdbserver_state.line_sum += ch;
  2635. } else if (ch == '#') {
  2636. /* end of command, start of checksum*/
  2637. gdbserver_state.state = RS_CHKSUM1;
  2638. } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
  2639. trace_gdbstub_err_overrun();
  2640. gdbserver_state.state = RS_IDLE;
  2641. } else {
  2642. /* unescaped command character */
  2643. gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
  2644. gdbserver_state.line_sum += ch;
  2645. }
  2646. break;
  2647. case RS_GETLINE_ESC:
  2648. if (ch == '#') {
  2649. /* unexpected end of command in escape sequence */
  2650. gdbserver_state.state = RS_CHKSUM1;
  2651. } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
  2652. /* command buffer overrun */
  2653. trace_gdbstub_err_overrun();
  2654. gdbserver_state.state = RS_IDLE;
  2655. } else {
  2656. /* parse escaped character and leave escape state */
  2657. gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
  2658. gdbserver_state.line_sum += ch;
  2659. gdbserver_state.state = RS_GETLINE;
  2660. }
  2661. break;
  2662. case RS_GETLINE_RLE:
  2663. /*
  2664. * Run-length encoding is explained in "Debugging with GDB /
  2665. * Appendix E GDB Remote Serial Protocol / Overview".
  2666. */
  2667. if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
  2668. /* invalid RLE count encoding */
  2669. trace_gdbstub_err_invalid_repeat(ch);
  2670. gdbserver_state.state = RS_GETLINE;
  2671. } else {
  2672. /* decode repeat length */
  2673. int repeat = ch - ' ' + 3;
  2674. if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
  2675. /* that many repeats would overrun the command buffer */
  2676. trace_gdbstub_err_overrun();
  2677. gdbserver_state.state = RS_IDLE;
  2678. } else if (gdbserver_state.line_buf_index < 1) {
  2679. /* got a repeat but we have nothing to repeat */
  2680. trace_gdbstub_err_invalid_rle();
  2681. gdbserver_state.state = RS_GETLINE;
  2682. } else {
  2683. /* repeat the last character */
  2684. memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
  2685. gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
  2686. gdbserver_state.line_buf_index += repeat;
  2687. gdbserver_state.line_sum += ch;
  2688. gdbserver_state.state = RS_GETLINE;
  2689. }
  2690. }
  2691. break;
  2692. case RS_CHKSUM1:
  2693. /* get high hex digit of checksum */
  2694. if (!isxdigit(ch)) {
  2695. trace_gdbstub_err_checksum_invalid(ch);
  2696. gdbserver_state.state = RS_GETLINE;
  2697. break;
  2698. }
  2699. gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
  2700. gdbserver_state.line_csum = fromhex(ch) << 4;
  2701. gdbserver_state.state = RS_CHKSUM2;
  2702. break;
  2703. case RS_CHKSUM2:
  2704. /* get low hex digit of checksum */
  2705. if (!isxdigit(ch)) {
  2706. trace_gdbstub_err_checksum_invalid(ch);
  2707. gdbserver_state.state = RS_GETLINE;
  2708. break;
  2709. }
  2710. gdbserver_state.line_csum |= fromhex(ch);
  2711. if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
  2712. trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
  2713. /* send NAK reply */
  2714. reply = '-';
  2715. put_buffer(&reply, 1);
  2716. gdbserver_state.state = RS_IDLE;
  2717. } else {
  2718. /* send ACK reply */
  2719. reply = '+';
  2720. put_buffer(&reply, 1);
  2721. gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
  2722. }
  2723. break;
  2724. default:
  2725. abort();
  2726. }
  2727. }
  2728. }
  2729. /* Tell the remote gdb that the process has exited. */
  2730. void gdb_exit(int code)
  2731. {
  2732. char buf[4];
  2733. if (!gdbserver_state.init) {
  2734. return;
  2735. }
  2736. #ifdef CONFIG_USER_ONLY
  2737. if (gdbserver_state.socket_path) {
  2738. unlink(gdbserver_state.socket_path);
  2739. }
  2740. if (gdbserver_state.fd < 0) {
  2741. return;
  2742. }
  2743. #endif
  2744. trace_gdbstub_op_exiting((uint8_t)code);
  2745. snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
  2746. put_packet(buf);
  2747. #ifndef CONFIG_USER_ONLY
  2748. qemu_chr_fe_deinit(&gdbserver_state.chr, true);
  2749. #endif
  2750. }
  2751. /*
  2752. * Create the process that will contain all the "orphan" CPUs (that are not
  2753. * part of a CPU cluster). Note that if this process contains no CPUs, it won't
  2754. * be attachable and thus will be invisible to the user.
  2755. */
  2756. static void create_default_process(GDBState *s)
  2757. {
  2758. GDBProcess *process;
  2759. int max_pid = 0;
  2760. if (gdbserver_state.process_num) {
  2761. max_pid = s->processes[s->process_num - 1].pid;
  2762. }
  2763. s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
  2764. process = &s->processes[s->process_num - 1];
  2765. /* We need an available PID slot for this process */
  2766. assert(max_pid < UINT32_MAX);
  2767. process->pid = max_pid + 1;
  2768. process->attached = false;
  2769. process->target_xml[0] = '\0';
  2770. }
  2771. #ifdef CONFIG_USER_ONLY
  2772. int
  2773. gdb_handlesig(CPUState *cpu, int sig)
  2774. {
  2775. char buf[256];
  2776. int n;
  2777. if (!gdbserver_state.init || gdbserver_state.fd < 0) {
  2778. return sig;
  2779. }
  2780. /* disable single step if it was enabled */
  2781. cpu_single_step(cpu, 0);
  2782. tb_flush(cpu);
  2783. if (sig != 0) {
  2784. snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
  2785. put_packet(buf);
  2786. }
  2787. /* put_packet() might have detected that the peer terminated the
  2788. connection. */
  2789. if (gdbserver_state.fd < 0) {
  2790. return sig;
  2791. }
  2792. sig = 0;
  2793. gdbserver_state.state = RS_IDLE;
  2794. gdbserver_state.running_state = 0;
  2795. while (gdbserver_state.running_state == 0) {
  2796. n = read(gdbserver_state.fd, buf, 256);
  2797. if (n > 0) {
  2798. int i;
  2799. for (i = 0; i < n; i++) {
  2800. gdb_read_byte(buf[i]);
  2801. }
  2802. } else {
  2803. /* XXX: Connection closed. Should probably wait for another
  2804. connection before continuing. */
  2805. if (n == 0) {
  2806. close(gdbserver_state.fd);
  2807. }
  2808. gdbserver_state.fd = -1;
  2809. return sig;
  2810. }
  2811. }
  2812. sig = gdbserver_state.signal;
  2813. gdbserver_state.signal = 0;
  2814. return sig;
  2815. }
  2816. /* Tell the remote gdb that the process has exited due to SIG. */
  2817. void gdb_signalled(CPUArchState *env, int sig)
  2818. {
  2819. char buf[4];
  2820. if (!gdbserver_state.init || gdbserver_state.fd < 0) {
  2821. return;
  2822. }
  2823. snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
  2824. put_packet(buf);
  2825. }
  2826. static void gdb_accept_init(int fd)
  2827. {
  2828. init_gdbserver_state();
  2829. create_default_process(&gdbserver_state);
  2830. gdbserver_state.processes[0].attached = true;
  2831. gdbserver_state.c_cpu = gdb_first_attached_cpu();
  2832. gdbserver_state.g_cpu = gdbserver_state.c_cpu;
  2833. gdbserver_state.fd = fd;
  2834. gdb_has_xml = false;
  2835. }
  2836. static bool gdb_accept_socket(int gdb_fd)
  2837. {
  2838. int fd;
  2839. for(;;) {
  2840. fd = accept(gdb_fd, NULL, NULL);
  2841. if (fd < 0 && errno != EINTR) {
  2842. perror("accept socket");
  2843. return false;
  2844. } else if (fd >= 0) {
  2845. qemu_set_cloexec(fd);
  2846. break;
  2847. }
  2848. }
  2849. gdb_accept_init(fd);
  2850. return true;
  2851. }
  2852. static int gdbserver_open_socket(const char *path)
  2853. {
  2854. struct sockaddr_un sockaddr = {};
  2855. int fd, ret;
  2856. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  2857. if (fd < 0) {
  2858. perror("create socket");
  2859. return -1;
  2860. }
  2861. sockaddr.sun_family = AF_UNIX;
  2862. pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
  2863. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  2864. if (ret < 0) {
  2865. perror("bind socket");
  2866. close(fd);
  2867. return -1;
  2868. }
  2869. ret = listen(fd, 1);
  2870. if (ret < 0) {
  2871. perror("listen socket");
  2872. close(fd);
  2873. return -1;
  2874. }
  2875. return fd;
  2876. }
  2877. static bool gdb_accept_tcp(int gdb_fd)
  2878. {
  2879. struct sockaddr_in sockaddr = {};
  2880. socklen_t len;
  2881. int fd;
  2882. for(;;) {
  2883. len = sizeof(sockaddr);
  2884. fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
  2885. if (fd < 0 && errno != EINTR) {
  2886. perror("accept");
  2887. return false;
  2888. } else if (fd >= 0) {
  2889. qemu_set_cloexec(fd);
  2890. break;
  2891. }
  2892. }
  2893. /* set short latency */
  2894. if (socket_set_nodelay(fd)) {
  2895. perror("setsockopt");
  2896. close(fd);
  2897. return false;
  2898. }
  2899. gdb_accept_init(fd);
  2900. return true;
  2901. }
  2902. static int gdbserver_open_port(int port)
  2903. {
  2904. struct sockaddr_in sockaddr;
  2905. int fd, ret;
  2906. fd = socket(PF_INET, SOCK_STREAM, 0);
  2907. if (fd < 0) {
  2908. perror("socket");
  2909. return -1;
  2910. }
  2911. qemu_set_cloexec(fd);
  2912. socket_set_fast_reuse(fd);
  2913. sockaddr.sin_family = AF_INET;
  2914. sockaddr.sin_port = htons(port);
  2915. sockaddr.sin_addr.s_addr = 0;
  2916. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  2917. if (ret < 0) {
  2918. perror("bind");
  2919. close(fd);
  2920. return -1;
  2921. }
  2922. ret = listen(fd, 1);
  2923. if (ret < 0) {
  2924. perror("listen");
  2925. close(fd);
  2926. return -1;
  2927. }
  2928. return fd;
  2929. }
  2930. int gdbserver_start(const char *port_or_path)
  2931. {
  2932. int port = g_ascii_strtoull(port_or_path, NULL, 10);
  2933. int gdb_fd;
  2934. if (port > 0) {
  2935. gdb_fd = gdbserver_open_port(port);
  2936. } else {
  2937. gdb_fd = gdbserver_open_socket(port_or_path);
  2938. }
  2939. if (gdb_fd < 0) {
  2940. return -1;
  2941. }
  2942. if (port > 0 && gdb_accept_tcp(gdb_fd)) {
  2943. return 0;
  2944. } else if (gdb_accept_socket(gdb_fd)) {
  2945. gdbserver_state.socket_path = g_strdup(port_or_path);
  2946. return 0;
  2947. }
  2948. /* gone wrong */
  2949. close(gdb_fd);
  2950. return -1;
  2951. }
  2952. /* Disable gdb stub for child processes. */
  2953. void gdbserver_fork(CPUState *cpu)
  2954. {
  2955. if (!gdbserver_state.init || gdbserver_state.fd < 0) {
  2956. return;
  2957. }
  2958. close(gdbserver_state.fd);
  2959. gdbserver_state.fd = -1;
  2960. cpu_breakpoint_remove_all(cpu, BP_GDB);
  2961. cpu_watchpoint_remove_all(cpu, BP_GDB);
  2962. }
  2963. #else
  2964. static int gdb_chr_can_receive(void *opaque)
  2965. {
  2966. /* We can handle an arbitrarily large amount of data.
  2967. Pick the maximum packet size, which is as good as anything. */
  2968. return MAX_PACKET_LENGTH;
  2969. }
  2970. static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
  2971. {
  2972. int i;
  2973. for (i = 0; i < size; i++) {
  2974. gdb_read_byte(buf[i]);
  2975. }
  2976. }
  2977. static void gdb_chr_event(void *opaque, QEMUChrEvent event)
  2978. {
  2979. int i;
  2980. GDBState *s = (GDBState *) opaque;
  2981. switch (event) {
  2982. case CHR_EVENT_OPENED:
  2983. /* Start with first process attached, others detached */
  2984. for (i = 0; i < s->process_num; i++) {
  2985. s->processes[i].attached = !i;
  2986. }
  2987. s->c_cpu = gdb_first_attached_cpu();
  2988. s->g_cpu = s->c_cpu;
  2989. vm_stop(RUN_STATE_PAUSED);
  2990. replay_gdb_attached();
  2991. gdb_has_xml = false;
  2992. break;
  2993. default:
  2994. break;
  2995. }
  2996. }
  2997. static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
  2998. {
  2999. g_autoptr(GString) hex_buf = g_string_new("O");
  3000. memtohex(hex_buf, buf, len);
  3001. put_packet(hex_buf->str);
  3002. return len;
  3003. }
  3004. #ifndef _WIN32
  3005. static void gdb_sigterm_handler(int signal)
  3006. {
  3007. if (runstate_is_running()) {
  3008. vm_stop(RUN_STATE_PAUSED);
  3009. }
  3010. }
  3011. #endif
  3012. static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
  3013. bool *be_opened, Error **errp)
  3014. {
  3015. *be_opened = false;
  3016. }
  3017. static void char_gdb_class_init(ObjectClass *oc, void *data)
  3018. {
  3019. ChardevClass *cc = CHARDEV_CLASS(oc);
  3020. cc->internal = true;
  3021. cc->open = gdb_monitor_open;
  3022. cc->chr_write = gdb_monitor_write;
  3023. }
  3024. #define TYPE_CHARDEV_GDB "chardev-gdb"
  3025. static const TypeInfo char_gdb_type_info = {
  3026. .name = TYPE_CHARDEV_GDB,
  3027. .parent = TYPE_CHARDEV,
  3028. .class_init = char_gdb_class_init,
  3029. };
  3030. static int find_cpu_clusters(Object *child, void *opaque)
  3031. {
  3032. if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
  3033. GDBState *s = (GDBState *) opaque;
  3034. CPUClusterState *cluster = CPU_CLUSTER(child);
  3035. GDBProcess *process;
  3036. s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
  3037. process = &s->processes[s->process_num - 1];
  3038. /*
  3039. * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
  3040. * runtime, we enforce here that the machine does not use a cluster ID
  3041. * that would lead to PID 0.
  3042. */
  3043. assert(cluster->cluster_id != UINT32_MAX);
  3044. process->pid = cluster->cluster_id + 1;
  3045. process->attached = false;
  3046. process->target_xml[0] = '\0';
  3047. return 0;
  3048. }
  3049. return object_child_foreach(child, find_cpu_clusters, opaque);
  3050. }
  3051. static int pid_order(const void *a, const void *b)
  3052. {
  3053. GDBProcess *pa = (GDBProcess *) a;
  3054. GDBProcess *pb = (GDBProcess *) b;
  3055. if (pa->pid < pb->pid) {
  3056. return -1;
  3057. } else if (pa->pid > pb->pid) {
  3058. return 1;
  3059. } else {
  3060. return 0;
  3061. }
  3062. }
  3063. static void create_processes(GDBState *s)
  3064. {
  3065. object_child_foreach(object_get_root(), find_cpu_clusters, s);
  3066. if (gdbserver_state.processes) {
  3067. /* Sort by PID */
  3068. qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order);
  3069. }
  3070. create_default_process(s);
  3071. }
  3072. int gdbserver_start(const char *device)
  3073. {
  3074. trace_gdbstub_op_start(device);
  3075. char gdbstub_device_name[128];
  3076. Chardev *chr = NULL;
  3077. Chardev *mon_chr;
  3078. if (!first_cpu) {
  3079. error_report("gdbstub: meaningless to attach gdb to a "
  3080. "machine without any CPU.");
  3081. return -1;
  3082. }
  3083. if (!device)
  3084. return -1;
  3085. if (strcmp(device, "none") != 0) {
  3086. if (strstart(device, "tcp:", NULL)) {
  3087. /* enforce required TCP attributes */
  3088. snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
  3089. "%s,wait=off,nodelay=on,server=on", device);
  3090. device = gdbstub_device_name;
  3091. }
  3092. #ifndef _WIN32
  3093. else if (strcmp(device, "stdio") == 0) {
  3094. struct sigaction act;
  3095. memset(&act, 0, sizeof(act));
  3096. act.sa_handler = gdb_sigterm_handler;
  3097. sigaction(SIGINT, &act, NULL);
  3098. }
  3099. #endif
  3100. /*
  3101. * FIXME: it's a bit weird to allow using a mux chardev here
  3102. * and implicitly setup a monitor. We may want to break this.
  3103. */
  3104. chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
  3105. if (!chr)
  3106. return -1;
  3107. }
  3108. if (!gdbserver_state.init) {
  3109. init_gdbserver_state();
  3110. qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
  3111. /* Initialize a monitor terminal for gdb */
  3112. mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
  3113. NULL, NULL, &error_abort);
  3114. monitor_init_hmp(mon_chr, false, &error_abort);
  3115. } else {
  3116. qemu_chr_fe_deinit(&gdbserver_state.chr, true);
  3117. mon_chr = gdbserver_state.mon_chr;
  3118. reset_gdbserver_state();
  3119. }
  3120. create_processes(&gdbserver_state);
  3121. if (chr) {
  3122. qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort);
  3123. qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive,
  3124. gdb_chr_receive, gdb_chr_event,
  3125. NULL, &gdbserver_state, NULL, true);
  3126. }
  3127. gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
  3128. gdbserver_state.mon_chr = mon_chr;
  3129. gdbserver_state.current_syscall_cb = NULL;
  3130. return 0;
  3131. }
  3132. static void register_types(void)
  3133. {
  3134. type_register_static(&char_gdb_type_info);
  3135. }
  3136. type_init(register_types);
  3137. #endif