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

gdbstub.c 86KB

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