You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gdbstub.c 86KB

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