gdbstub.c 88 KB

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