2
0

gdbstub.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054
  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 "config.h"
  20. #include "qemu-common.h"
  21. #ifdef CONFIG_USER_ONLY
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include "qemu.h"
  30. #else
  31. #include "monitor/monitor.h"
  32. #include "char/char.h"
  33. #include "sysemu/sysemu.h"
  34. #include "exec/gdbstub.h"
  35. #endif
  36. #define MAX_PACKET_LENGTH 4096
  37. #include "cpu.h"
  38. #include "qemu/sockets.h"
  39. #include "sysemu/kvm.h"
  40. #include "qemu/bitops.h"
  41. #ifndef TARGET_CPU_MEMORY_RW_DEBUG
  42. static inline int target_memory_rw_debug(CPUArchState *env, target_ulong addr,
  43. uint8_t *buf, int len, int is_write)
  44. {
  45. return cpu_memory_rw_debug(env, addr, buf, len, is_write);
  46. }
  47. #else
  48. /* target_memory_rw_debug() defined in cpu.h */
  49. #endif
  50. enum {
  51. GDB_SIGNAL_0 = 0,
  52. GDB_SIGNAL_INT = 2,
  53. GDB_SIGNAL_QUIT = 3,
  54. GDB_SIGNAL_TRAP = 5,
  55. GDB_SIGNAL_ABRT = 6,
  56. GDB_SIGNAL_ALRM = 14,
  57. GDB_SIGNAL_IO = 23,
  58. GDB_SIGNAL_XCPU = 24,
  59. GDB_SIGNAL_UNKNOWN = 143
  60. };
  61. #ifdef CONFIG_USER_ONLY
  62. /* Map target signal numbers to GDB protocol signal numbers and vice
  63. * versa. For user emulation's currently supported systems, we can
  64. * assume most signals are defined.
  65. */
  66. static int gdb_signal_table[] = {
  67. 0,
  68. TARGET_SIGHUP,
  69. TARGET_SIGINT,
  70. TARGET_SIGQUIT,
  71. TARGET_SIGILL,
  72. TARGET_SIGTRAP,
  73. TARGET_SIGABRT,
  74. -1, /* SIGEMT */
  75. TARGET_SIGFPE,
  76. TARGET_SIGKILL,
  77. TARGET_SIGBUS,
  78. TARGET_SIGSEGV,
  79. TARGET_SIGSYS,
  80. TARGET_SIGPIPE,
  81. TARGET_SIGALRM,
  82. TARGET_SIGTERM,
  83. TARGET_SIGURG,
  84. TARGET_SIGSTOP,
  85. TARGET_SIGTSTP,
  86. TARGET_SIGCONT,
  87. TARGET_SIGCHLD,
  88. TARGET_SIGTTIN,
  89. TARGET_SIGTTOU,
  90. TARGET_SIGIO,
  91. TARGET_SIGXCPU,
  92. TARGET_SIGXFSZ,
  93. TARGET_SIGVTALRM,
  94. TARGET_SIGPROF,
  95. TARGET_SIGWINCH,
  96. -1, /* SIGLOST */
  97. TARGET_SIGUSR1,
  98. TARGET_SIGUSR2,
  99. #ifdef TARGET_SIGPWR
  100. TARGET_SIGPWR,
  101. #else
  102. -1,
  103. #endif
  104. -1, /* SIGPOLL */
  105. -1,
  106. -1,
  107. -1,
  108. -1,
  109. -1,
  110. -1,
  111. -1,
  112. -1,
  113. -1,
  114. -1,
  115. -1,
  116. #ifdef __SIGRTMIN
  117. __SIGRTMIN + 1,
  118. __SIGRTMIN + 2,
  119. __SIGRTMIN + 3,
  120. __SIGRTMIN + 4,
  121. __SIGRTMIN + 5,
  122. __SIGRTMIN + 6,
  123. __SIGRTMIN + 7,
  124. __SIGRTMIN + 8,
  125. __SIGRTMIN + 9,
  126. __SIGRTMIN + 10,
  127. __SIGRTMIN + 11,
  128. __SIGRTMIN + 12,
  129. __SIGRTMIN + 13,
  130. __SIGRTMIN + 14,
  131. __SIGRTMIN + 15,
  132. __SIGRTMIN + 16,
  133. __SIGRTMIN + 17,
  134. __SIGRTMIN + 18,
  135. __SIGRTMIN + 19,
  136. __SIGRTMIN + 20,
  137. __SIGRTMIN + 21,
  138. __SIGRTMIN + 22,
  139. __SIGRTMIN + 23,
  140. __SIGRTMIN + 24,
  141. __SIGRTMIN + 25,
  142. __SIGRTMIN + 26,
  143. __SIGRTMIN + 27,
  144. __SIGRTMIN + 28,
  145. __SIGRTMIN + 29,
  146. __SIGRTMIN + 30,
  147. __SIGRTMIN + 31,
  148. -1, /* SIGCANCEL */
  149. __SIGRTMIN,
  150. __SIGRTMIN + 32,
  151. __SIGRTMIN + 33,
  152. __SIGRTMIN + 34,
  153. __SIGRTMIN + 35,
  154. __SIGRTMIN + 36,
  155. __SIGRTMIN + 37,
  156. __SIGRTMIN + 38,
  157. __SIGRTMIN + 39,
  158. __SIGRTMIN + 40,
  159. __SIGRTMIN + 41,
  160. __SIGRTMIN + 42,
  161. __SIGRTMIN + 43,
  162. __SIGRTMIN + 44,
  163. __SIGRTMIN + 45,
  164. __SIGRTMIN + 46,
  165. __SIGRTMIN + 47,
  166. __SIGRTMIN + 48,
  167. __SIGRTMIN + 49,
  168. __SIGRTMIN + 50,
  169. __SIGRTMIN + 51,
  170. __SIGRTMIN + 52,
  171. __SIGRTMIN + 53,
  172. __SIGRTMIN + 54,
  173. __SIGRTMIN + 55,
  174. __SIGRTMIN + 56,
  175. __SIGRTMIN + 57,
  176. __SIGRTMIN + 58,
  177. __SIGRTMIN + 59,
  178. __SIGRTMIN + 60,
  179. __SIGRTMIN + 61,
  180. __SIGRTMIN + 62,
  181. __SIGRTMIN + 63,
  182. __SIGRTMIN + 64,
  183. __SIGRTMIN + 65,
  184. __SIGRTMIN + 66,
  185. __SIGRTMIN + 67,
  186. __SIGRTMIN + 68,
  187. __SIGRTMIN + 69,
  188. __SIGRTMIN + 70,
  189. __SIGRTMIN + 71,
  190. __SIGRTMIN + 72,
  191. __SIGRTMIN + 73,
  192. __SIGRTMIN + 74,
  193. __SIGRTMIN + 75,
  194. __SIGRTMIN + 76,
  195. __SIGRTMIN + 77,
  196. __SIGRTMIN + 78,
  197. __SIGRTMIN + 79,
  198. __SIGRTMIN + 80,
  199. __SIGRTMIN + 81,
  200. __SIGRTMIN + 82,
  201. __SIGRTMIN + 83,
  202. __SIGRTMIN + 84,
  203. __SIGRTMIN + 85,
  204. __SIGRTMIN + 86,
  205. __SIGRTMIN + 87,
  206. __SIGRTMIN + 88,
  207. __SIGRTMIN + 89,
  208. __SIGRTMIN + 90,
  209. __SIGRTMIN + 91,
  210. __SIGRTMIN + 92,
  211. __SIGRTMIN + 93,
  212. __SIGRTMIN + 94,
  213. __SIGRTMIN + 95,
  214. -1, /* SIGINFO */
  215. -1, /* UNKNOWN */
  216. -1, /* DEFAULT */
  217. -1,
  218. -1,
  219. -1,
  220. -1,
  221. -1,
  222. -1
  223. #endif
  224. };
  225. #else
  226. /* In system mode we only need SIGINT and SIGTRAP; other signals
  227. are not yet supported. */
  228. enum {
  229. TARGET_SIGINT = 2,
  230. TARGET_SIGTRAP = 5
  231. };
  232. static int gdb_signal_table[] = {
  233. -1,
  234. -1,
  235. TARGET_SIGINT,
  236. -1,
  237. -1,
  238. TARGET_SIGTRAP
  239. };
  240. #endif
  241. #ifdef CONFIG_USER_ONLY
  242. static int target_signal_to_gdb (int sig)
  243. {
  244. int i;
  245. for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
  246. if (gdb_signal_table[i] == sig)
  247. return i;
  248. return GDB_SIGNAL_UNKNOWN;
  249. }
  250. #endif
  251. static int gdb_signal_to_target (int sig)
  252. {
  253. if (sig < ARRAY_SIZE (gdb_signal_table))
  254. return gdb_signal_table[sig];
  255. else
  256. return -1;
  257. }
  258. //#define DEBUG_GDB
  259. typedef struct GDBRegisterState {
  260. int base_reg;
  261. int num_regs;
  262. gdb_reg_cb get_reg;
  263. gdb_reg_cb set_reg;
  264. const char *xml;
  265. struct GDBRegisterState *next;
  266. } GDBRegisterState;
  267. enum RSState {
  268. RS_INACTIVE,
  269. RS_IDLE,
  270. RS_GETLINE,
  271. RS_CHKSUM1,
  272. RS_CHKSUM2,
  273. };
  274. typedef struct GDBState {
  275. CPUArchState *c_cpu; /* current CPU for step/continue ops */
  276. CPUArchState *g_cpu; /* current CPU for other ops */
  277. CPUArchState *query_cpu; /* for q{f|s}ThreadInfo */
  278. enum RSState state; /* parsing state */
  279. char line_buf[MAX_PACKET_LENGTH];
  280. int line_buf_index;
  281. int line_csum;
  282. uint8_t last_packet[MAX_PACKET_LENGTH + 4];
  283. int last_packet_len;
  284. int signal;
  285. #ifdef CONFIG_USER_ONLY
  286. int fd;
  287. int running_state;
  288. #else
  289. CharDriverState *chr;
  290. CharDriverState *mon_chr;
  291. #endif
  292. char syscall_buf[256];
  293. gdb_syscall_complete_cb current_syscall_cb;
  294. } GDBState;
  295. /* By default use no IRQs and no timers while single stepping so as to
  296. * make single stepping like an ICE HW step.
  297. */
  298. static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
  299. static GDBState *gdbserver_state;
  300. /* This is an ugly hack to cope with both new and old gdb.
  301. If gdb sends qXfer:features:read then assume we're talking to a newish
  302. gdb that understands target descriptions. */
  303. static int gdb_has_xml;
  304. #ifdef CONFIG_USER_ONLY
  305. /* XXX: This is not thread safe. Do we care? */
  306. static int gdbserver_fd = -1;
  307. static int get_char(GDBState *s)
  308. {
  309. uint8_t ch;
  310. int ret;
  311. for(;;) {
  312. ret = qemu_recv(s->fd, &ch, 1, 0);
  313. if (ret < 0) {
  314. if (errno == ECONNRESET)
  315. s->fd = -1;
  316. if (errno != EINTR && errno != EAGAIN)
  317. return -1;
  318. } else if (ret == 0) {
  319. close(s->fd);
  320. s->fd = -1;
  321. return -1;
  322. } else {
  323. break;
  324. }
  325. }
  326. return ch;
  327. }
  328. #endif
  329. static enum {
  330. GDB_SYS_UNKNOWN,
  331. GDB_SYS_ENABLED,
  332. GDB_SYS_DISABLED,
  333. } gdb_syscall_mode;
  334. /* If gdb is connected when the first semihosting syscall occurs then use
  335. remote gdb syscalls. Otherwise use native file IO. */
  336. int use_gdb_syscalls(void)
  337. {
  338. if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
  339. gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
  340. : GDB_SYS_DISABLED);
  341. }
  342. return gdb_syscall_mode == GDB_SYS_ENABLED;
  343. }
  344. /* Resume execution. */
  345. static inline void gdb_continue(GDBState *s)
  346. {
  347. #ifdef CONFIG_USER_ONLY
  348. s->running_state = 1;
  349. #else
  350. vm_start();
  351. #endif
  352. }
  353. static void put_buffer(GDBState *s, const uint8_t *buf, int len)
  354. {
  355. #ifdef CONFIG_USER_ONLY
  356. int ret;
  357. while (len > 0) {
  358. ret = send(s->fd, buf, len, 0);
  359. if (ret < 0) {
  360. if (errno != EINTR && errno != EAGAIN)
  361. return;
  362. } else {
  363. buf += ret;
  364. len -= ret;
  365. }
  366. }
  367. #else
  368. qemu_chr_fe_write(s->chr, buf, len);
  369. #endif
  370. }
  371. static inline int fromhex(int v)
  372. {
  373. if (v >= '0' && v <= '9')
  374. return v - '0';
  375. else if (v >= 'A' && v <= 'F')
  376. return v - 'A' + 10;
  377. else if (v >= 'a' && v <= 'f')
  378. return v - 'a' + 10;
  379. else
  380. return 0;
  381. }
  382. static inline int tohex(int v)
  383. {
  384. if (v < 10)
  385. return v + '0';
  386. else
  387. return v - 10 + 'a';
  388. }
  389. static void memtohex(char *buf, const uint8_t *mem, int len)
  390. {
  391. int i, c;
  392. char *q;
  393. q = buf;
  394. for(i = 0; i < len; i++) {
  395. c = mem[i];
  396. *q++ = tohex(c >> 4);
  397. *q++ = tohex(c & 0xf);
  398. }
  399. *q = '\0';
  400. }
  401. static void hextomem(uint8_t *mem, const char *buf, int len)
  402. {
  403. int i;
  404. for(i = 0; i < len; i++) {
  405. mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
  406. buf += 2;
  407. }
  408. }
  409. /* return -1 if error, 0 if OK */
  410. static int put_packet_binary(GDBState *s, const char *buf, int len)
  411. {
  412. int csum, i;
  413. uint8_t *p;
  414. for(;;) {
  415. p = s->last_packet;
  416. *(p++) = '$';
  417. memcpy(p, buf, len);
  418. p += len;
  419. csum = 0;
  420. for(i = 0; i < len; i++) {
  421. csum += buf[i];
  422. }
  423. *(p++) = '#';
  424. *(p++) = tohex((csum >> 4) & 0xf);
  425. *(p++) = tohex((csum) & 0xf);
  426. s->last_packet_len = p - s->last_packet;
  427. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  428. #ifdef CONFIG_USER_ONLY
  429. i = get_char(s);
  430. if (i < 0)
  431. return -1;
  432. if (i == '+')
  433. break;
  434. #else
  435. break;
  436. #endif
  437. }
  438. return 0;
  439. }
  440. /* return -1 if error, 0 if OK */
  441. static int put_packet(GDBState *s, const char *buf)
  442. {
  443. #ifdef DEBUG_GDB
  444. printf("reply='%s'\n", buf);
  445. #endif
  446. return put_packet_binary(s, buf, strlen(buf));
  447. }
  448. /* The GDB remote protocol transfers values in target byte order. This means
  449. we can use the raw memory access routines to access the value buffer.
  450. Conveniently, these also handle the case where the buffer is mis-aligned.
  451. */
  452. #define GET_REG8(val) do { \
  453. stb_p(mem_buf, val); \
  454. return 1; \
  455. } while(0)
  456. #define GET_REG16(val) do { \
  457. stw_p(mem_buf, val); \
  458. return 2; \
  459. } while(0)
  460. #define GET_REG32(val) do { \
  461. stl_p(mem_buf, val); \
  462. return 4; \
  463. } while(0)
  464. #define GET_REG64(val) do { \
  465. stq_p(mem_buf, val); \
  466. return 8; \
  467. } while(0)
  468. #if TARGET_LONG_BITS == 64
  469. #define GET_REGL(val) GET_REG64(val)
  470. #define ldtul_p(addr) ldq_p(addr)
  471. #else
  472. #define GET_REGL(val) GET_REG32(val)
  473. #define ldtul_p(addr) ldl_p(addr)
  474. #endif
  475. #if defined(TARGET_I386)
  476. #ifdef TARGET_X86_64
  477. static const int gpr_map[16] = {
  478. R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
  479. 8, 9, 10, 11, 12, 13, 14, 15
  480. };
  481. #else
  482. #define gpr_map gpr_map32
  483. #endif
  484. static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
  485. #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
  486. #define IDX_IP_REG CPU_NB_REGS
  487. #define IDX_FLAGS_REG (IDX_IP_REG + 1)
  488. #define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
  489. #define IDX_FP_REGS (IDX_SEG_REGS + 6)
  490. #define IDX_XMM_REGS (IDX_FP_REGS + 16)
  491. #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
  492. static int cpu_gdb_read_register(CPUX86State *env, uint8_t *mem_buf, int n)
  493. {
  494. if (n < CPU_NB_REGS) {
  495. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  496. GET_REG64(env->regs[gpr_map[n]]);
  497. } else if (n < CPU_NB_REGS32) {
  498. GET_REG32(env->regs[gpr_map32[n]]);
  499. }
  500. } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
  501. #ifdef USE_X86LDOUBLE
  502. /* FIXME: byteswap float values - after fixing fpregs layout. */
  503. memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
  504. #else
  505. memset(mem_buf, 0, 10);
  506. #endif
  507. return 10;
  508. } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
  509. n -= IDX_XMM_REGS;
  510. if (n < CPU_NB_REGS32 ||
  511. (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
  512. stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
  513. stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
  514. return 16;
  515. }
  516. } else {
  517. switch (n) {
  518. case IDX_IP_REG:
  519. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  520. GET_REG64(env->eip);
  521. } else {
  522. GET_REG32(env->eip);
  523. }
  524. case IDX_FLAGS_REG: GET_REG32(env->eflags);
  525. case IDX_SEG_REGS: GET_REG32(env->segs[R_CS].selector);
  526. case IDX_SEG_REGS + 1: GET_REG32(env->segs[R_SS].selector);
  527. case IDX_SEG_REGS + 2: GET_REG32(env->segs[R_DS].selector);
  528. case IDX_SEG_REGS + 3: GET_REG32(env->segs[R_ES].selector);
  529. case IDX_SEG_REGS + 4: GET_REG32(env->segs[R_FS].selector);
  530. case IDX_SEG_REGS + 5: GET_REG32(env->segs[R_GS].selector);
  531. case IDX_FP_REGS + 8: GET_REG32(env->fpuc);
  532. case IDX_FP_REGS + 9: GET_REG32((env->fpus & ~0x3800) |
  533. (env->fpstt & 0x7) << 11);
  534. case IDX_FP_REGS + 10: GET_REG32(0); /* ftag */
  535. case IDX_FP_REGS + 11: GET_REG32(0); /* fiseg */
  536. case IDX_FP_REGS + 12: GET_REG32(0); /* fioff */
  537. case IDX_FP_REGS + 13: GET_REG32(0); /* foseg */
  538. case IDX_FP_REGS + 14: GET_REG32(0); /* fooff */
  539. case IDX_FP_REGS + 15: GET_REG32(0); /* fop */
  540. case IDX_MXCSR_REG: GET_REG32(env->mxcsr);
  541. }
  542. }
  543. return 0;
  544. }
  545. static int cpu_x86_gdb_load_seg(CPUX86State *env, int sreg, uint8_t *mem_buf)
  546. {
  547. uint16_t selector = ldl_p(mem_buf);
  548. if (selector != env->segs[sreg].selector) {
  549. #if defined(CONFIG_USER_ONLY)
  550. cpu_x86_load_seg(env, sreg, selector);
  551. #else
  552. unsigned int limit, flags;
  553. target_ulong base;
  554. if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
  555. base = selector << 4;
  556. limit = 0xffff;
  557. flags = 0;
  558. } else {
  559. if (!cpu_x86_get_descr_debug(env, selector, &base, &limit, &flags))
  560. return 4;
  561. }
  562. cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
  563. #endif
  564. }
  565. return 4;
  566. }
  567. static int cpu_gdb_write_register(CPUX86State *env, uint8_t *mem_buf, int n)
  568. {
  569. uint32_t tmp;
  570. if (n < CPU_NB_REGS) {
  571. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  572. env->regs[gpr_map[n]] = ldtul_p(mem_buf);
  573. return sizeof(target_ulong);
  574. } else if (n < CPU_NB_REGS32) {
  575. n = gpr_map32[n];
  576. env->regs[n] &= ~0xffffffffUL;
  577. env->regs[n] |= (uint32_t)ldl_p(mem_buf);
  578. return 4;
  579. }
  580. } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
  581. #ifdef USE_X86LDOUBLE
  582. /* FIXME: byteswap float values - after fixing fpregs layout. */
  583. memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
  584. #endif
  585. return 10;
  586. } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
  587. n -= IDX_XMM_REGS;
  588. if (n < CPU_NB_REGS32 ||
  589. (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
  590. env->xmm_regs[n].XMM_Q(0) = ldq_p(mem_buf);
  591. env->xmm_regs[n].XMM_Q(1) = ldq_p(mem_buf + 8);
  592. return 16;
  593. }
  594. } else {
  595. switch (n) {
  596. case IDX_IP_REG:
  597. if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
  598. env->eip = ldq_p(mem_buf);
  599. return 8;
  600. } else {
  601. env->eip &= ~0xffffffffUL;
  602. env->eip |= (uint32_t)ldl_p(mem_buf);
  603. return 4;
  604. }
  605. case IDX_FLAGS_REG:
  606. env->eflags = ldl_p(mem_buf);
  607. return 4;
  608. case IDX_SEG_REGS: return cpu_x86_gdb_load_seg(env, R_CS, mem_buf);
  609. case IDX_SEG_REGS + 1: return cpu_x86_gdb_load_seg(env, R_SS, mem_buf);
  610. case IDX_SEG_REGS + 2: return cpu_x86_gdb_load_seg(env, R_DS, mem_buf);
  611. case IDX_SEG_REGS + 3: return cpu_x86_gdb_load_seg(env, R_ES, mem_buf);
  612. case IDX_SEG_REGS + 4: return cpu_x86_gdb_load_seg(env, R_FS, mem_buf);
  613. case IDX_SEG_REGS + 5: return cpu_x86_gdb_load_seg(env, R_GS, mem_buf);
  614. case IDX_FP_REGS + 8:
  615. env->fpuc = ldl_p(mem_buf);
  616. return 4;
  617. case IDX_FP_REGS + 9:
  618. tmp = ldl_p(mem_buf);
  619. env->fpstt = (tmp >> 11) & 7;
  620. env->fpus = tmp & ~0x3800;
  621. return 4;
  622. case IDX_FP_REGS + 10: /* ftag */ return 4;
  623. case IDX_FP_REGS + 11: /* fiseg */ return 4;
  624. case IDX_FP_REGS + 12: /* fioff */ return 4;
  625. case IDX_FP_REGS + 13: /* foseg */ return 4;
  626. case IDX_FP_REGS + 14: /* fooff */ return 4;
  627. case IDX_FP_REGS + 15: /* fop */ return 4;
  628. case IDX_MXCSR_REG:
  629. env->mxcsr = ldl_p(mem_buf);
  630. return 4;
  631. }
  632. }
  633. /* Unrecognised register. */
  634. return 0;
  635. }
  636. #elif defined (TARGET_PPC)
  637. /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
  638. expects whatever the target description contains. Due to a
  639. historical mishap the FP registers appear in between core integer
  640. regs and PC, MSR, CR, and so forth. We hack round this by giving the
  641. FP regs zero size when talking to a newer gdb. */
  642. #define NUM_CORE_REGS 71
  643. #if defined (TARGET_PPC64)
  644. #define GDB_CORE_XML "power64-core.xml"
  645. #else
  646. #define GDB_CORE_XML "power-core.xml"
  647. #endif
  648. static int cpu_gdb_read_register(CPUPPCState *env, uint8_t *mem_buf, int n)
  649. {
  650. if (n < 32) {
  651. /* gprs */
  652. GET_REGL(env->gpr[n]);
  653. } else if (n < 64) {
  654. /* fprs */
  655. if (gdb_has_xml)
  656. return 0;
  657. stfq_p(mem_buf, env->fpr[n-32]);
  658. return 8;
  659. } else {
  660. switch (n) {
  661. case 64: GET_REGL(env->nip);
  662. case 65: GET_REGL(env->msr);
  663. case 66:
  664. {
  665. uint32_t cr = 0;
  666. int i;
  667. for (i = 0; i < 8; i++)
  668. cr |= env->crf[i] << (32 - ((i + 1) * 4));
  669. GET_REG32(cr);
  670. }
  671. case 67: GET_REGL(env->lr);
  672. case 68: GET_REGL(env->ctr);
  673. case 69: GET_REGL(env->xer);
  674. case 70:
  675. {
  676. if (gdb_has_xml)
  677. return 0;
  678. GET_REG32(env->fpscr);
  679. }
  680. }
  681. }
  682. return 0;
  683. }
  684. static int cpu_gdb_write_register(CPUPPCState *env, uint8_t *mem_buf, int n)
  685. {
  686. if (n < 32) {
  687. /* gprs */
  688. env->gpr[n] = ldtul_p(mem_buf);
  689. return sizeof(target_ulong);
  690. } else if (n < 64) {
  691. /* fprs */
  692. if (gdb_has_xml)
  693. return 0;
  694. env->fpr[n-32] = ldfq_p(mem_buf);
  695. return 8;
  696. } else {
  697. switch (n) {
  698. case 64:
  699. env->nip = ldtul_p(mem_buf);
  700. return sizeof(target_ulong);
  701. case 65:
  702. ppc_store_msr(env, ldtul_p(mem_buf));
  703. return sizeof(target_ulong);
  704. case 66:
  705. {
  706. uint32_t cr = ldl_p(mem_buf);
  707. int i;
  708. for (i = 0; i < 8; i++)
  709. env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
  710. return 4;
  711. }
  712. case 67:
  713. env->lr = ldtul_p(mem_buf);
  714. return sizeof(target_ulong);
  715. case 68:
  716. env->ctr = ldtul_p(mem_buf);
  717. return sizeof(target_ulong);
  718. case 69:
  719. env->xer = ldtul_p(mem_buf);
  720. return sizeof(target_ulong);
  721. case 70:
  722. /* fpscr */
  723. if (gdb_has_xml)
  724. return 0;
  725. return 4;
  726. }
  727. }
  728. return 0;
  729. }
  730. #elif defined (TARGET_SPARC)
  731. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  732. #define NUM_CORE_REGS 86
  733. #else
  734. #define NUM_CORE_REGS 72
  735. #endif
  736. #ifdef TARGET_ABI32
  737. #define GET_REGA(val) GET_REG32(val)
  738. #else
  739. #define GET_REGA(val) GET_REGL(val)
  740. #endif
  741. static int cpu_gdb_read_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
  742. {
  743. if (n < 8) {
  744. /* g0..g7 */
  745. GET_REGA(env->gregs[n]);
  746. }
  747. if (n < 32) {
  748. /* register window */
  749. GET_REGA(env->regwptr[n - 8]);
  750. }
  751. #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
  752. if (n < 64) {
  753. /* fprs */
  754. if (n & 1) {
  755. GET_REG32(env->fpr[(n - 32) / 2].l.lower);
  756. } else {
  757. GET_REG32(env->fpr[(n - 32) / 2].l.upper);
  758. }
  759. }
  760. /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
  761. switch (n) {
  762. case 64: GET_REGA(env->y);
  763. case 65: GET_REGA(cpu_get_psr(env));
  764. case 66: GET_REGA(env->wim);
  765. case 67: GET_REGA(env->tbr);
  766. case 68: GET_REGA(env->pc);
  767. case 69: GET_REGA(env->npc);
  768. case 70: GET_REGA(env->fsr);
  769. case 71: GET_REGA(0); /* csr */
  770. default: GET_REGA(0);
  771. }
  772. #else
  773. if (n < 64) {
  774. /* f0-f31 */
  775. if (n & 1) {
  776. GET_REG32(env->fpr[(n - 32) / 2].l.lower);
  777. } else {
  778. GET_REG32(env->fpr[(n - 32) / 2].l.upper);
  779. }
  780. }
  781. if (n < 80) {
  782. /* f32-f62 (double width, even numbers only) */
  783. GET_REG64(env->fpr[(n - 32) / 2].ll);
  784. }
  785. switch (n) {
  786. case 80: GET_REGL(env->pc);
  787. case 81: GET_REGL(env->npc);
  788. case 82: GET_REGL((cpu_get_ccr(env) << 32) |
  789. ((env->asi & 0xff) << 24) |
  790. ((env->pstate & 0xfff) << 8) |
  791. cpu_get_cwp64(env));
  792. case 83: GET_REGL(env->fsr);
  793. case 84: GET_REGL(env->fprs);
  794. case 85: GET_REGL(env->y);
  795. }
  796. #endif
  797. return 0;
  798. }
  799. static int cpu_gdb_write_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
  800. {
  801. #if defined(TARGET_ABI32)
  802. abi_ulong tmp;
  803. tmp = ldl_p(mem_buf);
  804. #else
  805. target_ulong tmp;
  806. tmp = ldtul_p(mem_buf);
  807. #endif
  808. if (n < 8) {
  809. /* g0..g7 */
  810. env->gregs[n] = tmp;
  811. } else if (n < 32) {
  812. /* register window */
  813. env->regwptr[n - 8] = tmp;
  814. }
  815. #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
  816. else if (n < 64) {
  817. /* fprs */
  818. /* f0-f31 */
  819. if (n & 1) {
  820. env->fpr[(n - 32) / 2].l.lower = tmp;
  821. } else {
  822. env->fpr[(n - 32) / 2].l.upper = tmp;
  823. }
  824. } else {
  825. /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
  826. switch (n) {
  827. case 64: env->y = tmp; break;
  828. case 65: cpu_put_psr(env, tmp); break;
  829. case 66: env->wim = tmp; break;
  830. case 67: env->tbr = tmp; break;
  831. case 68: env->pc = tmp; break;
  832. case 69: env->npc = tmp; break;
  833. case 70: env->fsr = tmp; break;
  834. default: return 0;
  835. }
  836. }
  837. return 4;
  838. #else
  839. else if (n < 64) {
  840. /* f0-f31 */
  841. tmp = ldl_p(mem_buf);
  842. if (n & 1) {
  843. env->fpr[(n - 32) / 2].l.lower = tmp;
  844. } else {
  845. env->fpr[(n - 32) / 2].l.upper = tmp;
  846. }
  847. return 4;
  848. } else if (n < 80) {
  849. /* f32-f62 (double width, even numbers only) */
  850. env->fpr[(n - 32) / 2].ll = tmp;
  851. } else {
  852. switch (n) {
  853. case 80: env->pc = tmp; break;
  854. case 81: env->npc = tmp; break;
  855. case 82:
  856. cpu_put_ccr(env, tmp >> 32);
  857. env->asi = (tmp >> 24) & 0xff;
  858. env->pstate = (tmp >> 8) & 0xfff;
  859. cpu_put_cwp64(env, tmp & 0xff);
  860. break;
  861. case 83: env->fsr = tmp; break;
  862. case 84: env->fprs = tmp; break;
  863. case 85: env->y = tmp; break;
  864. default: return 0;
  865. }
  866. }
  867. return 8;
  868. #endif
  869. }
  870. #elif defined (TARGET_ARM)
  871. /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
  872. whatever the target description contains. Due to a historical mishap
  873. the FPA registers appear in between core integer regs and the CPSR.
  874. We hack round this by giving the FPA regs zero size when talking to a
  875. newer gdb. */
  876. #define NUM_CORE_REGS 26
  877. #define GDB_CORE_XML "arm-core.xml"
  878. static int cpu_gdb_read_register(CPUARMState *env, uint8_t *mem_buf, int n)
  879. {
  880. if (n < 16) {
  881. /* Core integer register. */
  882. GET_REG32(env->regs[n]);
  883. }
  884. if (n < 24) {
  885. /* FPA registers. */
  886. if (gdb_has_xml)
  887. return 0;
  888. memset(mem_buf, 0, 12);
  889. return 12;
  890. }
  891. switch (n) {
  892. case 24:
  893. /* FPA status register. */
  894. if (gdb_has_xml)
  895. return 0;
  896. GET_REG32(0);
  897. case 25:
  898. /* CPSR */
  899. GET_REG32(cpsr_read(env));
  900. }
  901. /* Unknown register. */
  902. return 0;
  903. }
  904. static int cpu_gdb_write_register(CPUARMState *env, uint8_t *mem_buf, int n)
  905. {
  906. uint32_t tmp;
  907. tmp = ldl_p(mem_buf);
  908. /* Mask out low bit of PC to workaround gdb bugs. This will probably
  909. cause problems if we ever implement the Jazelle DBX extensions. */
  910. if (n == 15)
  911. tmp &= ~1;
  912. if (n < 16) {
  913. /* Core integer register. */
  914. env->regs[n] = tmp;
  915. return 4;
  916. }
  917. if (n < 24) { /* 16-23 */
  918. /* FPA registers (ignored). */
  919. if (gdb_has_xml)
  920. return 0;
  921. return 12;
  922. }
  923. switch (n) {
  924. case 24:
  925. /* FPA status register (ignored). */
  926. if (gdb_has_xml)
  927. return 0;
  928. return 4;
  929. case 25:
  930. /* CPSR */
  931. cpsr_write (env, tmp, 0xffffffff);
  932. return 4;
  933. }
  934. /* Unknown register. */
  935. return 0;
  936. }
  937. #elif defined (TARGET_M68K)
  938. #define NUM_CORE_REGS 18
  939. #define GDB_CORE_XML "cf-core.xml"
  940. static int cpu_gdb_read_register(CPUM68KState *env, uint8_t *mem_buf, int n)
  941. {
  942. if (n < 8) {
  943. /* D0-D7 */
  944. GET_REG32(env->dregs[n]);
  945. } else if (n < 16) {
  946. /* A0-A7 */
  947. GET_REG32(env->aregs[n - 8]);
  948. } else {
  949. switch (n) {
  950. case 16: GET_REG32(env->sr);
  951. case 17: GET_REG32(env->pc);
  952. }
  953. }
  954. /* FP registers not included here because they vary between
  955. ColdFire and m68k. Use XML bits for these. */
  956. return 0;
  957. }
  958. static int cpu_gdb_write_register(CPUM68KState *env, uint8_t *mem_buf, int n)
  959. {
  960. uint32_t tmp;
  961. tmp = ldl_p(mem_buf);
  962. if (n < 8) {
  963. /* D0-D7 */
  964. env->dregs[n] = tmp;
  965. } else if (n < 16) {
  966. /* A0-A7 */
  967. env->aregs[n - 8] = tmp;
  968. } else {
  969. switch (n) {
  970. case 16: env->sr = tmp; break;
  971. case 17: env->pc = tmp; break;
  972. default: return 0;
  973. }
  974. }
  975. return 4;
  976. }
  977. #elif defined (TARGET_MIPS)
  978. #define NUM_CORE_REGS 73
  979. static int cpu_gdb_read_register(CPUMIPSState *env, uint8_t *mem_buf, int n)
  980. {
  981. if (n < 32) {
  982. GET_REGL(env->active_tc.gpr[n]);
  983. }
  984. if (env->CP0_Config1 & (1 << CP0C1_FP)) {
  985. if (n >= 38 && n < 70) {
  986. if (env->CP0_Status & (1 << CP0St_FR))
  987. GET_REGL(env->active_fpu.fpr[n - 38].d);
  988. else
  989. GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
  990. }
  991. switch (n) {
  992. case 70: GET_REGL((int32_t)env->active_fpu.fcr31);
  993. case 71: GET_REGL((int32_t)env->active_fpu.fcr0);
  994. }
  995. }
  996. switch (n) {
  997. case 32: GET_REGL((int32_t)env->CP0_Status);
  998. case 33: GET_REGL(env->active_tc.LO[0]);
  999. case 34: GET_REGL(env->active_tc.HI[0]);
  1000. case 35: GET_REGL(env->CP0_BadVAddr);
  1001. case 36: GET_REGL((int32_t)env->CP0_Cause);
  1002. case 37: GET_REGL(env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16));
  1003. case 72: GET_REGL(0); /* fp */
  1004. case 89: GET_REGL((int32_t)env->CP0_PRid);
  1005. }
  1006. if (n >= 73 && n <= 88) {
  1007. /* 16 embedded regs. */
  1008. GET_REGL(0);
  1009. }
  1010. return 0;
  1011. }
  1012. /* convert MIPS rounding mode in FCR31 to IEEE library */
  1013. static unsigned int ieee_rm[] =
  1014. {
  1015. float_round_nearest_even,
  1016. float_round_to_zero,
  1017. float_round_up,
  1018. float_round_down
  1019. };
  1020. #define RESTORE_ROUNDING_MODE \
  1021. set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
  1022. static int cpu_gdb_write_register(CPUMIPSState *env, uint8_t *mem_buf, int n)
  1023. {
  1024. target_ulong tmp;
  1025. tmp = ldtul_p(mem_buf);
  1026. if (n < 32) {
  1027. env->active_tc.gpr[n] = tmp;
  1028. return sizeof(target_ulong);
  1029. }
  1030. if (env->CP0_Config1 & (1 << CP0C1_FP)
  1031. && n >= 38 && n < 73) {
  1032. if (n < 70) {
  1033. if (env->CP0_Status & (1 << CP0St_FR))
  1034. env->active_fpu.fpr[n - 38].d = tmp;
  1035. else
  1036. env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
  1037. }
  1038. switch (n) {
  1039. case 70:
  1040. env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
  1041. /* set rounding mode */
  1042. RESTORE_ROUNDING_MODE;
  1043. break;
  1044. case 71: env->active_fpu.fcr0 = tmp; break;
  1045. }
  1046. return sizeof(target_ulong);
  1047. }
  1048. switch (n) {
  1049. case 32: env->CP0_Status = tmp; break;
  1050. case 33: env->active_tc.LO[0] = tmp; break;
  1051. case 34: env->active_tc.HI[0] = tmp; break;
  1052. case 35: env->CP0_BadVAddr = tmp; break;
  1053. case 36: env->CP0_Cause = tmp; break;
  1054. case 37:
  1055. env->active_tc.PC = tmp & ~(target_ulong)1;
  1056. if (tmp & 1) {
  1057. env->hflags |= MIPS_HFLAG_M16;
  1058. } else {
  1059. env->hflags &= ~(MIPS_HFLAG_M16);
  1060. }
  1061. break;
  1062. case 72: /* fp, ignored */ break;
  1063. default:
  1064. if (n > 89)
  1065. return 0;
  1066. /* Other registers are readonly. Ignore writes. */
  1067. break;
  1068. }
  1069. return sizeof(target_ulong);
  1070. }
  1071. #elif defined(TARGET_OPENRISC)
  1072. #define NUM_CORE_REGS (32 + 3)
  1073. static int cpu_gdb_read_register(CPUOpenRISCState *env, uint8_t *mem_buf, int n)
  1074. {
  1075. if (n < 32) {
  1076. GET_REG32(env->gpr[n]);
  1077. } else {
  1078. switch (n) {
  1079. case 32: /* PPC */
  1080. GET_REG32(env->ppc);
  1081. break;
  1082. case 33: /* NPC */
  1083. GET_REG32(env->npc);
  1084. break;
  1085. case 34: /* SR */
  1086. GET_REG32(env->sr);
  1087. break;
  1088. default:
  1089. break;
  1090. }
  1091. }
  1092. return 0;
  1093. }
  1094. static int cpu_gdb_write_register(CPUOpenRISCState *env,
  1095. uint8_t *mem_buf, int n)
  1096. {
  1097. uint32_t tmp;
  1098. if (n > NUM_CORE_REGS) {
  1099. return 0;
  1100. }
  1101. tmp = ldl_p(mem_buf);
  1102. if (n < 32) {
  1103. env->gpr[n] = tmp;
  1104. } else {
  1105. switch (n) {
  1106. case 32: /* PPC */
  1107. env->ppc = tmp;
  1108. break;
  1109. case 33: /* NPC */
  1110. env->npc = tmp;
  1111. break;
  1112. case 34: /* SR */
  1113. env->sr = tmp;
  1114. break;
  1115. default:
  1116. break;
  1117. }
  1118. }
  1119. return 4;
  1120. }
  1121. #elif defined (TARGET_SH4)
  1122. /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
  1123. /* FIXME: We should use XML for this. */
  1124. #define NUM_CORE_REGS 59
  1125. static int cpu_gdb_read_register(CPUSH4State *env, uint8_t *mem_buf, int n)
  1126. {
  1127. switch (n) {
  1128. case 0 ... 7:
  1129. if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
  1130. GET_REGL(env->gregs[n + 16]);
  1131. } else {
  1132. GET_REGL(env->gregs[n]);
  1133. }
  1134. case 8 ... 15:
  1135. GET_REGL(env->gregs[n]);
  1136. case 16:
  1137. GET_REGL(env->pc);
  1138. case 17:
  1139. GET_REGL(env->pr);
  1140. case 18:
  1141. GET_REGL(env->gbr);
  1142. case 19:
  1143. GET_REGL(env->vbr);
  1144. case 20:
  1145. GET_REGL(env->mach);
  1146. case 21:
  1147. GET_REGL(env->macl);
  1148. case 22:
  1149. GET_REGL(env->sr);
  1150. case 23:
  1151. GET_REGL(env->fpul);
  1152. case 24:
  1153. GET_REGL(env->fpscr);
  1154. case 25 ... 40:
  1155. if (env->fpscr & FPSCR_FR) {
  1156. stfl_p(mem_buf, env->fregs[n - 9]);
  1157. } else {
  1158. stfl_p(mem_buf, env->fregs[n - 25]);
  1159. }
  1160. return 4;
  1161. case 41:
  1162. GET_REGL(env->ssr);
  1163. case 42:
  1164. GET_REGL(env->spc);
  1165. case 43 ... 50:
  1166. GET_REGL(env->gregs[n - 43]);
  1167. case 51 ... 58:
  1168. GET_REGL(env->gregs[n - (51 - 16)]);
  1169. }
  1170. return 0;
  1171. }
  1172. static int cpu_gdb_write_register(CPUSH4State *env, uint8_t *mem_buf, int n)
  1173. {
  1174. switch (n) {
  1175. case 0 ... 7:
  1176. if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
  1177. env->gregs[n + 16] = ldl_p(mem_buf);
  1178. } else {
  1179. env->gregs[n] = ldl_p(mem_buf);
  1180. }
  1181. break;
  1182. case 8 ... 15:
  1183. env->gregs[n] = ldl_p(mem_buf);
  1184. break;
  1185. case 16:
  1186. env->pc = ldl_p(mem_buf);
  1187. break;
  1188. case 17:
  1189. env->pr = ldl_p(mem_buf);
  1190. break;
  1191. case 18:
  1192. env->gbr = ldl_p(mem_buf);
  1193. break;
  1194. case 19:
  1195. env->vbr = ldl_p(mem_buf);
  1196. break;
  1197. case 20:
  1198. env->mach = ldl_p(mem_buf);
  1199. break;
  1200. case 21:
  1201. env->macl = ldl_p(mem_buf);
  1202. break;
  1203. case 22:
  1204. env->sr = ldl_p(mem_buf);
  1205. break;
  1206. case 23:
  1207. env->fpul = ldl_p(mem_buf);
  1208. break;
  1209. case 24:
  1210. env->fpscr = ldl_p(mem_buf);
  1211. break;
  1212. case 25 ... 40:
  1213. if (env->fpscr & FPSCR_FR) {
  1214. env->fregs[n - 9] = ldfl_p(mem_buf);
  1215. } else {
  1216. env->fregs[n - 25] = ldfl_p(mem_buf);
  1217. }
  1218. break;
  1219. case 41:
  1220. env->ssr = ldl_p(mem_buf);
  1221. break;
  1222. case 42:
  1223. env->spc = ldl_p(mem_buf);
  1224. break;
  1225. case 43 ... 50:
  1226. env->gregs[n - 43] = ldl_p(mem_buf);
  1227. break;
  1228. case 51 ... 58:
  1229. env->gregs[n - (51 - 16)] = ldl_p(mem_buf);
  1230. break;
  1231. default: return 0;
  1232. }
  1233. return 4;
  1234. }
  1235. #elif defined (TARGET_MICROBLAZE)
  1236. #define NUM_CORE_REGS (32 + 5)
  1237. static int cpu_gdb_read_register(CPUMBState *env, uint8_t *mem_buf, int n)
  1238. {
  1239. if (n < 32) {
  1240. GET_REG32(env->regs[n]);
  1241. } else {
  1242. GET_REG32(env->sregs[n - 32]);
  1243. }
  1244. return 0;
  1245. }
  1246. static int cpu_gdb_write_register(CPUMBState *env, uint8_t *mem_buf, int n)
  1247. {
  1248. uint32_t tmp;
  1249. if (n > NUM_CORE_REGS)
  1250. return 0;
  1251. tmp = ldl_p(mem_buf);
  1252. if (n < 32) {
  1253. env->regs[n] = tmp;
  1254. } else {
  1255. env->sregs[n - 32] = tmp;
  1256. }
  1257. return 4;
  1258. }
  1259. #elif defined (TARGET_CRIS)
  1260. #define NUM_CORE_REGS 49
  1261. static int
  1262. read_register_crisv10(CPUCRISState *env, uint8_t *mem_buf, int n)
  1263. {
  1264. if (n < 15) {
  1265. GET_REG32(env->regs[n]);
  1266. }
  1267. if (n == 15) {
  1268. GET_REG32(env->pc);
  1269. }
  1270. if (n < 32) {
  1271. switch (n) {
  1272. case 16:
  1273. GET_REG8(env->pregs[n - 16]);
  1274. break;
  1275. case 17:
  1276. GET_REG8(env->pregs[n - 16]);
  1277. break;
  1278. case 20:
  1279. case 21:
  1280. GET_REG16(env->pregs[n - 16]);
  1281. break;
  1282. default:
  1283. if (n >= 23) {
  1284. GET_REG32(env->pregs[n - 16]);
  1285. }
  1286. break;
  1287. }
  1288. }
  1289. return 0;
  1290. }
  1291. static int cpu_gdb_read_register(CPUCRISState *env, uint8_t *mem_buf, int n)
  1292. {
  1293. uint8_t srs;
  1294. if (env->pregs[PR_VR] < 32)
  1295. return read_register_crisv10(env, mem_buf, n);
  1296. srs = env->pregs[PR_SRS];
  1297. if (n < 16) {
  1298. GET_REG32(env->regs[n]);
  1299. }
  1300. if (n >= 21 && n < 32) {
  1301. GET_REG32(env->pregs[n - 16]);
  1302. }
  1303. if (n >= 33 && n < 49) {
  1304. GET_REG32(env->sregs[srs][n - 33]);
  1305. }
  1306. switch (n) {
  1307. case 16: GET_REG8(env->pregs[0]);
  1308. case 17: GET_REG8(env->pregs[1]);
  1309. case 18: GET_REG32(env->pregs[2]);
  1310. case 19: GET_REG8(srs);
  1311. case 20: GET_REG16(env->pregs[4]);
  1312. case 32: GET_REG32(env->pc);
  1313. }
  1314. return 0;
  1315. }
  1316. static int cpu_gdb_write_register(CPUCRISState *env, uint8_t *mem_buf, int n)
  1317. {
  1318. uint32_t tmp;
  1319. if (n > 49)
  1320. return 0;
  1321. tmp = ldl_p(mem_buf);
  1322. if (n < 16) {
  1323. env->regs[n] = tmp;
  1324. }
  1325. if (n >= 21 && n < 32) {
  1326. env->pregs[n - 16] = tmp;
  1327. }
  1328. /* FIXME: Should support function regs be writable? */
  1329. switch (n) {
  1330. case 16: return 1;
  1331. case 17: return 1;
  1332. case 18: env->pregs[PR_PID] = tmp; break;
  1333. case 19: return 1;
  1334. case 20: return 2;
  1335. case 32: env->pc = tmp; break;
  1336. }
  1337. return 4;
  1338. }
  1339. #elif defined (TARGET_ALPHA)
  1340. #define NUM_CORE_REGS 67
  1341. static int cpu_gdb_read_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
  1342. {
  1343. uint64_t val;
  1344. CPU_DoubleU d;
  1345. switch (n) {
  1346. case 0 ... 30:
  1347. val = env->ir[n];
  1348. break;
  1349. case 32 ... 62:
  1350. d.d = env->fir[n - 32];
  1351. val = d.ll;
  1352. break;
  1353. case 63:
  1354. val = cpu_alpha_load_fpcr(env);
  1355. break;
  1356. case 64:
  1357. val = env->pc;
  1358. break;
  1359. case 66:
  1360. val = env->unique;
  1361. break;
  1362. case 31:
  1363. case 65:
  1364. /* 31 really is the zero register; 65 is unassigned in the
  1365. gdb protocol, but is still required to occupy 8 bytes. */
  1366. val = 0;
  1367. break;
  1368. default:
  1369. return 0;
  1370. }
  1371. GET_REGL(val);
  1372. }
  1373. static int cpu_gdb_write_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
  1374. {
  1375. target_ulong tmp = ldtul_p(mem_buf);
  1376. CPU_DoubleU d;
  1377. switch (n) {
  1378. case 0 ... 30:
  1379. env->ir[n] = tmp;
  1380. break;
  1381. case 32 ... 62:
  1382. d.ll = tmp;
  1383. env->fir[n - 32] = d.d;
  1384. break;
  1385. case 63:
  1386. cpu_alpha_store_fpcr(env, tmp);
  1387. break;
  1388. case 64:
  1389. env->pc = tmp;
  1390. break;
  1391. case 66:
  1392. env->unique = tmp;
  1393. break;
  1394. case 31:
  1395. case 65:
  1396. /* 31 really is the zero register; 65 is unassigned in the
  1397. gdb protocol, but is still required to occupy 8 bytes. */
  1398. break;
  1399. default:
  1400. return 0;
  1401. }
  1402. return 8;
  1403. }
  1404. #elif defined (TARGET_S390X)
  1405. #define NUM_CORE_REGS S390_NUM_REGS
  1406. static int cpu_gdb_read_register(CPUS390XState *env, uint8_t *mem_buf, int n)
  1407. {
  1408. uint64_t val;
  1409. int cc_op;
  1410. switch (n) {
  1411. case S390_PSWM_REGNUM:
  1412. cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
  1413. val = deposit64(env->psw.mask, 44, 2, cc_op);
  1414. GET_REGL(val);
  1415. break;
  1416. case S390_PSWA_REGNUM:
  1417. GET_REGL(env->psw.addr);
  1418. break;
  1419. case S390_R0_REGNUM ... S390_R15_REGNUM:
  1420. GET_REGL(env->regs[n-S390_R0_REGNUM]);
  1421. break;
  1422. case S390_A0_REGNUM ... S390_A15_REGNUM:
  1423. GET_REG32(env->aregs[n-S390_A0_REGNUM]);
  1424. break;
  1425. case S390_FPC_REGNUM:
  1426. GET_REG32(env->fpc);
  1427. break;
  1428. case S390_F0_REGNUM ... S390_F15_REGNUM:
  1429. GET_REG64(env->fregs[n-S390_F0_REGNUM].ll);
  1430. break;
  1431. }
  1432. return 0;
  1433. }
  1434. static int cpu_gdb_write_register(CPUS390XState *env, uint8_t *mem_buf, int n)
  1435. {
  1436. target_ulong tmpl;
  1437. uint32_t tmp32;
  1438. int r = 8;
  1439. tmpl = ldtul_p(mem_buf);
  1440. tmp32 = ldl_p(mem_buf);
  1441. switch (n) {
  1442. case S390_PSWM_REGNUM:
  1443. env->psw.mask = tmpl;
  1444. env->cc_op = extract64(tmpl, 44, 2);
  1445. break;
  1446. case S390_PSWA_REGNUM:
  1447. env->psw.addr = tmpl;
  1448. break;
  1449. case S390_R0_REGNUM ... S390_R15_REGNUM:
  1450. env->regs[n-S390_R0_REGNUM] = tmpl;
  1451. break;
  1452. case S390_A0_REGNUM ... S390_A15_REGNUM:
  1453. env->aregs[n-S390_A0_REGNUM] = tmp32;
  1454. r = 4;
  1455. break;
  1456. case S390_FPC_REGNUM:
  1457. env->fpc = tmp32;
  1458. r = 4;
  1459. break;
  1460. case S390_F0_REGNUM ... S390_F15_REGNUM:
  1461. env->fregs[n-S390_F0_REGNUM].ll = tmpl;
  1462. break;
  1463. default:
  1464. return 0;
  1465. }
  1466. return r;
  1467. }
  1468. #elif defined (TARGET_LM32)
  1469. #include "hw/lm32_pic.h"
  1470. #define NUM_CORE_REGS (32 + 7)
  1471. static int cpu_gdb_read_register(CPULM32State *env, uint8_t *mem_buf, int n)
  1472. {
  1473. if (n < 32) {
  1474. GET_REG32(env->regs[n]);
  1475. } else {
  1476. switch (n) {
  1477. case 32:
  1478. GET_REG32(env->pc);
  1479. break;
  1480. /* FIXME: put in right exception ID */
  1481. case 33:
  1482. GET_REG32(0);
  1483. break;
  1484. case 34:
  1485. GET_REG32(env->eba);
  1486. break;
  1487. case 35:
  1488. GET_REG32(env->deba);
  1489. break;
  1490. case 36:
  1491. GET_REG32(env->ie);
  1492. break;
  1493. case 37:
  1494. GET_REG32(lm32_pic_get_im(env->pic_state));
  1495. break;
  1496. case 38:
  1497. GET_REG32(lm32_pic_get_ip(env->pic_state));
  1498. break;
  1499. }
  1500. }
  1501. return 0;
  1502. }
  1503. static int cpu_gdb_write_register(CPULM32State *env, uint8_t *mem_buf, int n)
  1504. {
  1505. uint32_t tmp;
  1506. if (n > NUM_CORE_REGS) {
  1507. return 0;
  1508. }
  1509. tmp = ldl_p(mem_buf);
  1510. if (n < 32) {
  1511. env->regs[n] = tmp;
  1512. } else {
  1513. switch (n) {
  1514. case 32:
  1515. env->pc = tmp;
  1516. break;
  1517. case 34:
  1518. env->eba = tmp;
  1519. break;
  1520. case 35:
  1521. env->deba = tmp;
  1522. break;
  1523. case 36:
  1524. env->ie = tmp;
  1525. break;
  1526. case 37:
  1527. lm32_pic_set_im(env->pic_state, tmp);
  1528. break;
  1529. case 38:
  1530. lm32_pic_set_ip(env->pic_state, tmp);
  1531. break;
  1532. }
  1533. }
  1534. return 4;
  1535. }
  1536. #elif defined(TARGET_XTENSA)
  1537. /* Use num_core_regs to see only non-privileged registers in an unmodified gdb.
  1538. * Use num_regs to see all registers. gdb modification is required for that:
  1539. * reset bit 0 in the 'flags' field of the registers definitions in the
  1540. * gdb/xtensa-config.c inside gdb source tree or inside gdb overlay.
  1541. */
  1542. #define NUM_CORE_REGS (env->config->gdb_regmap.num_regs)
  1543. #define num_g_regs NUM_CORE_REGS
  1544. static int cpu_gdb_read_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
  1545. {
  1546. const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
  1547. if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
  1548. return 0;
  1549. }
  1550. switch (reg->type) {
  1551. case 9: /*pc*/
  1552. GET_REG32(env->pc);
  1553. break;
  1554. case 1: /*ar*/
  1555. xtensa_sync_phys_from_window(env);
  1556. GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]);
  1557. break;
  1558. case 2: /*SR*/
  1559. GET_REG32(env->sregs[reg->targno & 0xff]);
  1560. break;
  1561. case 3: /*UR*/
  1562. GET_REG32(env->uregs[reg->targno & 0xff]);
  1563. break;
  1564. case 4: /*f*/
  1565. GET_REG32(float32_val(env->fregs[reg->targno & 0x0f]));
  1566. break;
  1567. case 8: /*a*/
  1568. GET_REG32(env->regs[reg->targno & 0x0f]);
  1569. break;
  1570. default:
  1571. qemu_log("%s from reg %d of unsupported type %d\n",
  1572. __func__, n, reg->type);
  1573. return 0;
  1574. }
  1575. }
  1576. static int cpu_gdb_write_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
  1577. {
  1578. uint32_t tmp;
  1579. const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
  1580. if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
  1581. return 0;
  1582. }
  1583. tmp = ldl_p(mem_buf);
  1584. switch (reg->type) {
  1585. case 9: /*pc*/
  1586. env->pc = tmp;
  1587. break;
  1588. case 1: /*ar*/
  1589. env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp;
  1590. xtensa_sync_window_from_phys(env);
  1591. break;
  1592. case 2: /*SR*/
  1593. env->sregs[reg->targno & 0xff] = tmp;
  1594. break;
  1595. case 3: /*UR*/
  1596. env->uregs[reg->targno & 0xff] = tmp;
  1597. break;
  1598. case 4: /*f*/
  1599. env->fregs[reg->targno & 0x0f] = make_float32(tmp);
  1600. break;
  1601. case 8: /*a*/
  1602. env->regs[reg->targno & 0x0f] = tmp;
  1603. break;
  1604. default:
  1605. qemu_log("%s to reg %d of unsupported type %d\n",
  1606. __func__, n, reg->type);
  1607. return 0;
  1608. }
  1609. return 4;
  1610. }
  1611. #else
  1612. #define NUM_CORE_REGS 0
  1613. static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n)
  1614. {
  1615. return 0;
  1616. }
  1617. static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n)
  1618. {
  1619. return 0;
  1620. }
  1621. #endif
  1622. #if !defined(TARGET_XTENSA)
  1623. static int num_g_regs = NUM_CORE_REGS;
  1624. #endif
  1625. #ifdef GDB_CORE_XML
  1626. /* Encode data using the encoding for 'x' packets. */
  1627. static int memtox(char *buf, const char *mem, int len)
  1628. {
  1629. char *p = buf;
  1630. char c;
  1631. while (len--) {
  1632. c = *(mem++);
  1633. switch (c) {
  1634. case '#': case '$': case '*': case '}':
  1635. *(p++) = '}';
  1636. *(p++) = c ^ 0x20;
  1637. break;
  1638. default:
  1639. *(p++) = c;
  1640. break;
  1641. }
  1642. }
  1643. return p - buf;
  1644. }
  1645. static const char *get_feature_xml(const char *p, const char **newp)
  1646. {
  1647. size_t len;
  1648. int i;
  1649. const char *name;
  1650. static char target_xml[1024];
  1651. len = 0;
  1652. while (p[len] && p[len] != ':')
  1653. len++;
  1654. *newp = p + len;
  1655. name = NULL;
  1656. if (strncmp(p, "target.xml", len) == 0) {
  1657. /* Generate the XML description for this CPU. */
  1658. if (!target_xml[0]) {
  1659. GDBRegisterState *r;
  1660. snprintf(target_xml, sizeof(target_xml),
  1661. "<?xml version=\"1.0\"?>"
  1662. "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
  1663. "<target>"
  1664. "<xi:include href=\"%s\"/>",
  1665. GDB_CORE_XML);
  1666. for (r = first_cpu->gdb_regs; r; r = r->next) {
  1667. pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
  1668. pstrcat(target_xml, sizeof(target_xml), r->xml);
  1669. pstrcat(target_xml, sizeof(target_xml), "\"/>");
  1670. }
  1671. pstrcat(target_xml, sizeof(target_xml), "</target>");
  1672. }
  1673. return target_xml;
  1674. }
  1675. for (i = 0; ; i++) {
  1676. name = xml_builtin[i][0];
  1677. if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
  1678. break;
  1679. }
  1680. return name ? xml_builtin[i][1] : NULL;
  1681. }
  1682. #endif
  1683. static int gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int reg)
  1684. {
  1685. GDBRegisterState *r;
  1686. if (reg < NUM_CORE_REGS)
  1687. return cpu_gdb_read_register(env, mem_buf, reg);
  1688. for (r = env->gdb_regs; r; r = r->next) {
  1689. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  1690. return r->get_reg(env, mem_buf, reg - r->base_reg);
  1691. }
  1692. }
  1693. return 0;
  1694. }
  1695. static int gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int reg)
  1696. {
  1697. GDBRegisterState *r;
  1698. if (reg < NUM_CORE_REGS)
  1699. return cpu_gdb_write_register(env, mem_buf, reg);
  1700. for (r = env->gdb_regs; r; r = r->next) {
  1701. if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
  1702. return r->set_reg(env, mem_buf, reg - r->base_reg);
  1703. }
  1704. }
  1705. return 0;
  1706. }
  1707. #if !defined(TARGET_XTENSA)
  1708. /* Register a supplemental set of CPU registers. If g_pos is nonzero it
  1709. specifies the first register number and these registers are included in
  1710. a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
  1711. gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
  1712. */
  1713. void gdb_register_coprocessor(CPUArchState * env,
  1714. gdb_reg_cb get_reg, gdb_reg_cb set_reg,
  1715. int num_regs, const char *xml, int g_pos)
  1716. {
  1717. GDBRegisterState *s;
  1718. GDBRegisterState **p;
  1719. static int last_reg = NUM_CORE_REGS;
  1720. p = &env->gdb_regs;
  1721. while (*p) {
  1722. /* Check for duplicates. */
  1723. if (strcmp((*p)->xml, xml) == 0)
  1724. return;
  1725. p = &(*p)->next;
  1726. }
  1727. s = g_new0(GDBRegisterState, 1);
  1728. s->base_reg = last_reg;
  1729. s->num_regs = num_regs;
  1730. s->get_reg = get_reg;
  1731. s->set_reg = set_reg;
  1732. s->xml = xml;
  1733. /* Add to end of list. */
  1734. last_reg += num_regs;
  1735. *p = s;
  1736. if (g_pos) {
  1737. if (g_pos != s->base_reg) {
  1738. fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
  1739. "Expected %d got %d\n", xml, g_pos, s->base_reg);
  1740. } else {
  1741. num_g_regs = last_reg;
  1742. }
  1743. }
  1744. }
  1745. #endif
  1746. #ifndef CONFIG_USER_ONLY
  1747. static const int xlat_gdb_type[] = {
  1748. [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
  1749. [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
  1750. [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
  1751. };
  1752. #endif
  1753. static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
  1754. {
  1755. CPUArchState *env;
  1756. int err = 0;
  1757. if (kvm_enabled())
  1758. return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  1759. switch (type) {
  1760. case GDB_BREAKPOINT_SW:
  1761. case GDB_BREAKPOINT_HW:
  1762. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1763. err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
  1764. if (err)
  1765. break;
  1766. }
  1767. return err;
  1768. #ifndef CONFIG_USER_ONLY
  1769. case GDB_WATCHPOINT_WRITE:
  1770. case GDB_WATCHPOINT_READ:
  1771. case GDB_WATCHPOINT_ACCESS:
  1772. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1773. err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
  1774. NULL);
  1775. if (err)
  1776. break;
  1777. }
  1778. return err;
  1779. #endif
  1780. default:
  1781. return -ENOSYS;
  1782. }
  1783. }
  1784. static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
  1785. {
  1786. CPUArchState *env;
  1787. int err = 0;
  1788. if (kvm_enabled())
  1789. return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
  1790. switch (type) {
  1791. case GDB_BREAKPOINT_SW:
  1792. case GDB_BREAKPOINT_HW:
  1793. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1794. err = cpu_breakpoint_remove(env, addr, BP_GDB);
  1795. if (err)
  1796. break;
  1797. }
  1798. return err;
  1799. #ifndef CONFIG_USER_ONLY
  1800. case GDB_WATCHPOINT_WRITE:
  1801. case GDB_WATCHPOINT_READ:
  1802. case GDB_WATCHPOINT_ACCESS:
  1803. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1804. err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
  1805. if (err)
  1806. break;
  1807. }
  1808. return err;
  1809. #endif
  1810. default:
  1811. return -ENOSYS;
  1812. }
  1813. }
  1814. static void gdb_breakpoint_remove_all(void)
  1815. {
  1816. CPUArchState *env;
  1817. if (kvm_enabled()) {
  1818. kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
  1819. return;
  1820. }
  1821. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1822. cpu_breakpoint_remove_all(env, BP_GDB);
  1823. #ifndef CONFIG_USER_ONLY
  1824. cpu_watchpoint_remove_all(env, BP_GDB);
  1825. #endif
  1826. }
  1827. }
  1828. static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
  1829. {
  1830. cpu_synchronize_state(s->c_cpu);
  1831. #if defined(TARGET_I386)
  1832. s->c_cpu->eip = pc;
  1833. #elif defined (TARGET_PPC)
  1834. s->c_cpu->nip = pc;
  1835. #elif defined (TARGET_SPARC)
  1836. s->c_cpu->pc = pc;
  1837. s->c_cpu->npc = pc + 4;
  1838. #elif defined (TARGET_ARM)
  1839. s->c_cpu->regs[15] = pc;
  1840. #elif defined (TARGET_SH4)
  1841. s->c_cpu->pc = pc;
  1842. #elif defined (TARGET_MIPS)
  1843. s->c_cpu->active_tc.PC = pc & ~(target_ulong)1;
  1844. if (pc & 1) {
  1845. s->c_cpu->hflags |= MIPS_HFLAG_M16;
  1846. } else {
  1847. s->c_cpu->hflags &= ~(MIPS_HFLAG_M16);
  1848. }
  1849. #elif defined (TARGET_MICROBLAZE)
  1850. s->c_cpu->sregs[SR_PC] = pc;
  1851. #elif defined(TARGET_OPENRISC)
  1852. s->c_cpu->pc = pc;
  1853. #elif defined (TARGET_CRIS)
  1854. s->c_cpu->pc = pc;
  1855. #elif defined (TARGET_ALPHA)
  1856. s->c_cpu->pc = pc;
  1857. #elif defined (TARGET_S390X)
  1858. s->c_cpu->psw.addr = pc;
  1859. #elif defined (TARGET_LM32)
  1860. s->c_cpu->pc = pc;
  1861. #elif defined(TARGET_XTENSA)
  1862. s->c_cpu->pc = pc;
  1863. #endif
  1864. }
  1865. static CPUArchState *find_cpu(uint32_t thread_id)
  1866. {
  1867. CPUArchState *env;
  1868. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  1869. if (cpu_index(env) == thread_id) {
  1870. return env;
  1871. }
  1872. }
  1873. return NULL;
  1874. }
  1875. static int gdb_handle_packet(GDBState *s, const char *line_buf)
  1876. {
  1877. CPUArchState *env;
  1878. const char *p;
  1879. uint32_t thread;
  1880. int ch, reg_size, type, res;
  1881. char buf[MAX_PACKET_LENGTH];
  1882. uint8_t mem_buf[MAX_PACKET_LENGTH];
  1883. uint8_t *registers;
  1884. target_ulong addr, len;
  1885. #ifdef DEBUG_GDB
  1886. printf("command='%s'\n", line_buf);
  1887. #endif
  1888. p = line_buf;
  1889. ch = *p++;
  1890. switch(ch) {
  1891. case '?':
  1892. /* TODO: Make this return the correct value for user-mode. */
  1893. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
  1894. cpu_index(s->c_cpu));
  1895. put_packet(s, buf);
  1896. /* Remove all the breakpoints when this query is issued,
  1897. * because gdb is doing and initial connect and the state
  1898. * should be cleaned up.
  1899. */
  1900. gdb_breakpoint_remove_all();
  1901. break;
  1902. case 'c':
  1903. if (*p != '\0') {
  1904. addr = strtoull(p, (char **)&p, 16);
  1905. gdb_set_cpu_pc(s, addr);
  1906. }
  1907. s->signal = 0;
  1908. gdb_continue(s);
  1909. return RS_IDLE;
  1910. case 'C':
  1911. s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
  1912. if (s->signal == -1)
  1913. s->signal = 0;
  1914. gdb_continue(s);
  1915. return RS_IDLE;
  1916. case 'v':
  1917. if (strncmp(p, "Cont", 4) == 0) {
  1918. int res_signal, res_thread;
  1919. p += 4;
  1920. if (*p == '?') {
  1921. put_packet(s, "vCont;c;C;s;S");
  1922. break;
  1923. }
  1924. res = 0;
  1925. res_signal = 0;
  1926. res_thread = 0;
  1927. while (*p) {
  1928. int action, signal;
  1929. if (*p++ != ';') {
  1930. res = 0;
  1931. break;
  1932. }
  1933. action = *p++;
  1934. signal = 0;
  1935. if (action == 'C' || action == 'S') {
  1936. signal = strtoul(p, (char **)&p, 16);
  1937. } else if (action != 'c' && action != 's') {
  1938. res = 0;
  1939. break;
  1940. }
  1941. thread = 0;
  1942. if (*p == ':') {
  1943. thread = strtoull(p+1, (char **)&p, 16);
  1944. }
  1945. action = tolower(action);
  1946. if (res == 0 || (res == 'c' && action == 's')) {
  1947. res = action;
  1948. res_signal = signal;
  1949. res_thread = thread;
  1950. }
  1951. }
  1952. if (res) {
  1953. if (res_thread != -1 && res_thread != 0) {
  1954. env = find_cpu(res_thread);
  1955. if (env == NULL) {
  1956. put_packet(s, "E22");
  1957. break;
  1958. }
  1959. s->c_cpu = env;
  1960. }
  1961. if (res == 's') {
  1962. cpu_single_step(s->c_cpu, sstep_flags);
  1963. }
  1964. s->signal = res_signal;
  1965. gdb_continue(s);
  1966. return RS_IDLE;
  1967. }
  1968. break;
  1969. } else {
  1970. goto unknown_command;
  1971. }
  1972. case 'k':
  1973. #ifdef CONFIG_USER_ONLY
  1974. /* Kill the target */
  1975. fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
  1976. exit(0);
  1977. #endif
  1978. case 'D':
  1979. /* Detach packet */
  1980. gdb_breakpoint_remove_all();
  1981. gdb_syscall_mode = GDB_SYS_DISABLED;
  1982. gdb_continue(s);
  1983. put_packet(s, "OK");
  1984. break;
  1985. case 's':
  1986. if (*p != '\0') {
  1987. addr = strtoull(p, (char **)&p, 16);
  1988. gdb_set_cpu_pc(s, addr);
  1989. }
  1990. cpu_single_step(s->c_cpu, sstep_flags);
  1991. gdb_continue(s);
  1992. return RS_IDLE;
  1993. case 'F':
  1994. {
  1995. target_ulong ret;
  1996. target_ulong err;
  1997. ret = strtoull(p, (char **)&p, 16);
  1998. if (*p == ',') {
  1999. p++;
  2000. err = strtoull(p, (char **)&p, 16);
  2001. } else {
  2002. err = 0;
  2003. }
  2004. if (*p == ',')
  2005. p++;
  2006. type = *p;
  2007. if (s->current_syscall_cb) {
  2008. s->current_syscall_cb(s->c_cpu, ret, err);
  2009. s->current_syscall_cb = NULL;
  2010. }
  2011. if (type == 'C') {
  2012. put_packet(s, "T02");
  2013. } else {
  2014. gdb_continue(s);
  2015. }
  2016. }
  2017. break;
  2018. case 'g':
  2019. cpu_synchronize_state(s->g_cpu);
  2020. env = s->g_cpu;
  2021. len = 0;
  2022. for (addr = 0; addr < num_g_regs; addr++) {
  2023. reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
  2024. len += reg_size;
  2025. }
  2026. memtohex(buf, mem_buf, len);
  2027. put_packet(s, buf);
  2028. break;
  2029. case 'G':
  2030. cpu_synchronize_state(s->g_cpu);
  2031. env = s->g_cpu;
  2032. registers = mem_buf;
  2033. len = strlen(p) / 2;
  2034. hextomem((uint8_t *)registers, p, len);
  2035. for (addr = 0; addr < num_g_regs && len > 0; addr++) {
  2036. reg_size = gdb_write_register(s->g_cpu, registers, addr);
  2037. len -= reg_size;
  2038. registers += reg_size;
  2039. }
  2040. put_packet(s, "OK");
  2041. break;
  2042. case 'm':
  2043. addr = strtoull(p, (char **)&p, 16);
  2044. if (*p == ',')
  2045. p++;
  2046. len = strtoull(p, NULL, 16);
  2047. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
  2048. put_packet (s, "E14");
  2049. } else {
  2050. memtohex(buf, mem_buf, len);
  2051. put_packet(s, buf);
  2052. }
  2053. break;
  2054. case 'M':
  2055. addr = strtoull(p, (char **)&p, 16);
  2056. if (*p == ',')
  2057. p++;
  2058. len = strtoull(p, (char **)&p, 16);
  2059. if (*p == ':')
  2060. p++;
  2061. hextomem(mem_buf, p, len);
  2062. if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0) {
  2063. put_packet(s, "E14");
  2064. } else {
  2065. put_packet(s, "OK");
  2066. }
  2067. break;
  2068. case 'p':
  2069. /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
  2070. This works, but can be very slow. Anything new enough to
  2071. understand XML also knows how to use this properly. */
  2072. if (!gdb_has_xml)
  2073. goto unknown_command;
  2074. addr = strtoull(p, (char **)&p, 16);
  2075. reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
  2076. if (reg_size) {
  2077. memtohex(buf, mem_buf, reg_size);
  2078. put_packet(s, buf);
  2079. } else {
  2080. put_packet(s, "E14");
  2081. }
  2082. break;
  2083. case 'P':
  2084. if (!gdb_has_xml)
  2085. goto unknown_command;
  2086. addr = strtoull(p, (char **)&p, 16);
  2087. if (*p == '=')
  2088. p++;
  2089. reg_size = strlen(p) / 2;
  2090. hextomem(mem_buf, p, reg_size);
  2091. gdb_write_register(s->g_cpu, mem_buf, addr);
  2092. put_packet(s, "OK");
  2093. break;
  2094. case 'Z':
  2095. case 'z':
  2096. type = strtoul(p, (char **)&p, 16);
  2097. if (*p == ',')
  2098. p++;
  2099. addr = strtoull(p, (char **)&p, 16);
  2100. if (*p == ',')
  2101. p++;
  2102. len = strtoull(p, (char **)&p, 16);
  2103. if (ch == 'Z')
  2104. res = gdb_breakpoint_insert(addr, len, type);
  2105. else
  2106. res = gdb_breakpoint_remove(addr, len, type);
  2107. if (res >= 0)
  2108. put_packet(s, "OK");
  2109. else if (res == -ENOSYS)
  2110. put_packet(s, "");
  2111. else
  2112. put_packet(s, "E22");
  2113. break;
  2114. case 'H':
  2115. type = *p++;
  2116. thread = strtoull(p, (char **)&p, 16);
  2117. if (thread == -1 || thread == 0) {
  2118. put_packet(s, "OK");
  2119. break;
  2120. }
  2121. env = find_cpu(thread);
  2122. if (env == NULL) {
  2123. put_packet(s, "E22");
  2124. break;
  2125. }
  2126. switch (type) {
  2127. case 'c':
  2128. s->c_cpu = env;
  2129. put_packet(s, "OK");
  2130. break;
  2131. case 'g':
  2132. s->g_cpu = env;
  2133. put_packet(s, "OK");
  2134. break;
  2135. default:
  2136. put_packet(s, "E22");
  2137. break;
  2138. }
  2139. break;
  2140. case 'T':
  2141. thread = strtoull(p, (char **)&p, 16);
  2142. env = find_cpu(thread);
  2143. if (env != NULL) {
  2144. put_packet(s, "OK");
  2145. } else {
  2146. put_packet(s, "E22");
  2147. }
  2148. break;
  2149. case 'q':
  2150. case 'Q':
  2151. /* parse any 'q' packets here */
  2152. if (!strcmp(p,"qemu.sstepbits")) {
  2153. /* Query Breakpoint bit definitions */
  2154. snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
  2155. SSTEP_ENABLE,
  2156. SSTEP_NOIRQ,
  2157. SSTEP_NOTIMER);
  2158. put_packet(s, buf);
  2159. break;
  2160. } else if (strncmp(p,"qemu.sstep",10) == 0) {
  2161. /* Display or change the sstep_flags */
  2162. p += 10;
  2163. if (*p != '=') {
  2164. /* Display current setting */
  2165. snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
  2166. put_packet(s, buf);
  2167. break;
  2168. }
  2169. p++;
  2170. type = strtoul(p, (char **)&p, 16);
  2171. sstep_flags = type;
  2172. put_packet(s, "OK");
  2173. break;
  2174. } else if (strcmp(p,"C") == 0) {
  2175. /* "Current thread" remains vague in the spec, so always return
  2176. * the first CPU (gdb returns the first thread). */
  2177. put_packet(s, "QC1");
  2178. break;
  2179. } else if (strcmp(p,"fThreadInfo") == 0) {
  2180. s->query_cpu = first_cpu;
  2181. goto report_cpuinfo;
  2182. } else if (strcmp(p,"sThreadInfo") == 0) {
  2183. report_cpuinfo:
  2184. if (s->query_cpu) {
  2185. snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
  2186. put_packet(s, buf);
  2187. s->query_cpu = s->query_cpu->next_cpu;
  2188. } else
  2189. put_packet(s, "l");
  2190. break;
  2191. } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
  2192. thread = strtoull(p+16, (char **)&p, 16);
  2193. env = find_cpu(thread);
  2194. if (env != NULL) {
  2195. CPUState *cpu = ENV_GET_CPU(env);
  2196. cpu_synchronize_state(env);
  2197. len = snprintf((char *)mem_buf, sizeof(mem_buf),
  2198. "CPU#%d [%s]", cpu->cpu_index,
  2199. env->halted ? "halted " : "running");
  2200. memtohex(buf, mem_buf, len);
  2201. put_packet(s, buf);
  2202. }
  2203. break;
  2204. }
  2205. #ifdef CONFIG_USER_ONLY
  2206. else if (strncmp(p, "Offsets", 7) == 0) {
  2207. TaskState *ts = s->c_cpu->opaque;
  2208. snprintf(buf, sizeof(buf),
  2209. "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
  2210. ";Bss=" TARGET_ABI_FMT_lx,
  2211. ts->info->code_offset,
  2212. ts->info->data_offset,
  2213. ts->info->data_offset);
  2214. put_packet(s, buf);
  2215. break;
  2216. }
  2217. #else /* !CONFIG_USER_ONLY */
  2218. else if (strncmp(p, "Rcmd,", 5) == 0) {
  2219. int len = strlen(p + 5);
  2220. if ((len % 2) != 0) {
  2221. put_packet(s, "E01");
  2222. break;
  2223. }
  2224. hextomem(mem_buf, p + 5, len);
  2225. len = len / 2;
  2226. mem_buf[len++] = 0;
  2227. qemu_chr_be_write(s->mon_chr, mem_buf, len);
  2228. put_packet(s, "OK");
  2229. break;
  2230. }
  2231. #endif /* !CONFIG_USER_ONLY */
  2232. if (strncmp(p, "Supported", 9) == 0) {
  2233. snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
  2234. #ifdef GDB_CORE_XML
  2235. pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
  2236. #endif
  2237. put_packet(s, buf);
  2238. break;
  2239. }
  2240. #ifdef GDB_CORE_XML
  2241. if (strncmp(p, "Xfer:features:read:", 19) == 0) {
  2242. const char *xml;
  2243. target_ulong total_len;
  2244. gdb_has_xml = 1;
  2245. p += 19;
  2246. xml = get_feature_xml(p, &p);
  2247. if (!xml) {
  2248. snprintf(buf, sizeof(buf), "E00");
  2249. put_packet(s, buf);
  2250. break;
  2251. }
  2252. if (*p == ':')
  2253. p++;
  2254. addr = strtoul(p, (char **)&p, 16);
  2255. if (*p == ',')
  2256. p++;
  2257. len = strtoul(p, (char **)&p, 16);
  2258. total_len = strlen(xml);
  2259. if (addr > total_len) {
  2260. snprintf(buf, sizeof(buf), "E00");
  2261. put_packet(s, buf);
  2262. break;
  2263. }
  2264. if (len > (MAX_PACKET_LENGTH - 5) / 2)
  2265. len = (MAX_PACKET_LENGTH - 5) / 2;
  2266. if (len < total_len - addr) {
  2267. buf[0] = 'm';
  2268. len = memtox(buf + 1, xml + addr, len);
  2269. } else {
  2270. buf[0] = 'l';
  2271. len = memtox(buf + 1, xml + addr, total_len - addr);
  2272. }
  2273. put_packet_binary(s, buf, len + 1);
  2274. break;
  2275. }
  2276. #endif
  2277. /* Unrecognised 'q' command. */
  2278. goto unknown_command;
  2279. default:
  2280. unknown_command:
  2281. /* put empty packet */
  2282. buf[0] = '\0';
  2283. put_packet(s, buf);
  2284. break;
  2285. }
  2286. return RS_IDLE;
  2287. }
  2288. void gdb_set_stop_cpu(CPUArchState *env)
  2289. {
  2290. gdbserver_state->c_cpu = env;
  2291. gdbserver_state->g_cpu = env;
  2292. }
  2293. #ifndef CONFIG_USER_ONLY
  2294. static void gdb_vm_state_change(void *opaque, int running, RunState state)
  2295. {
  2296. GDBState *s = gdbserver_state;
  2297. CPUArchState *env = s->c_cpu;
  2298. char buf[256];
  2299. const char *type;
  2300. int ret;
  2301. if (running || s->state == RS_INACTIVE) {
  2302. return;
  2303. }
  2304. /* Is there a GDB syscall waiting to be sent? */
  2305. if (s->current_syscall_cb) {
  2306. put_packet(s, s->syscall_buf);
  2307. return;
  2308. }
  2309. switch (state) {
  2310. case RUN_STATE_DEBUG:
  2311. if (env->watchpoint_hit) {
  2312. switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
  2313. case BP_MEM_READ:
  2314. type = "r";
  2315. break;
  2316. case BP_MEM_ACCESS:
  2317. type = "a";
  2318. break;
  2319. default:
  2320. type = "";
  2321. break;
  2322. }
  2323. snprintf(buf, sizeof(buf),
  2324. "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
  2325. GDB_SIGNAL_TRAP, cpu_index(env), type,
  2326. env->watchpoint_hit->vaddr);
  2327. env->watchpoint_hit = NULL;
  2328. goto send_packet;
  2329. }
  2330. tb_flush(env);
  2331. ret = GDB_SIGNAL_TRAP;
  2332. break;
  2333. case RUN_STATE_PAUSED:
  2334. ret = GDB_SIGNAL_INT;
  2335. break;
  2336. case RUN_STATE_SHUTDOWN:
  2337. ret = GDB_SIGNAL_QUIT;
  2338. break;
  2339. case RUN_STATE_IO_ERROR:
  2340. ret = GDB_SIGNAL_IO;
  2341. break;
  2342. case RUN_STATE_WATCHDOG:
  2343. ret = GDB_SIGNAL_ALRM;
  2344. break;
  2345. case RUN_STATE_INTERNAL_ERROR:
  2346. ret = GDB_SIGNAL_ABRT;
  2347. break;
  2348. case RUN_STATE_SAVE_VM:
  2349. case RUN_STATE_RESTORE_VM:
  2350. return;
  2351. case RUN_STATE_FINISH_MIGRATE:
  2352. ret = GDB_SIGNAL_XCPU;
  2353. break;
  2354. default:
  2355. ret = GDB_SIGNAL_UNKNOWN;
  2356. break;
  2357. }
  2358. snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(env));
  2359. send_packet:
  2360. put_packet(s, buf);
  2361. /* disable single step if it was enabled */
  2362. cpu_single_step(env, 0);
  2363. }
  2364. #endif
  2365. /* Send a gdb syscall request.
  2366. This accepts limited printf-style format specifiers, specifically:
  2367. %x - target_ulong argument printed in hex.
  2368. %lx - 64-bit argument printed in hex.
  2369. %s - string pointer (target_ulong) and length (int) pair. */
  2370. void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
  2371. {
  2372. va_list va;
  2373. char *p;
  2374. char *p_end;
  2375. target_ulong addr;
  2376. uint64_t i64;
  2377. GDBState *s;
  2378. s = gdbserver_state;
  2379. if (!s)
  2380. return;
  2381. s->current_syscall_cb = cb;
  2382. #ifndef CONFIG_USER_ONLY
  2383. vm_stop(RUN_STATE_DEBUG);
  2384. #endif
  2385. va_start(va, fmt);
  2386. p = s->syscall_buf;
  2387. p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
  2388. *(p++) = 'F';
  2389. while (*fmt) {
  2390. if (*fmt == '%') {
  2391. fmt++;
  2392. switch (*fmt++) {
  2393. case 'x':
  2394. addr = va_arg(va, target_ulong);
  2395. p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
  2396. break;
  2397. case 'l':
  2398. if (*(fmt++) != 'x')
  2399. goto bad_format;
  2400. i64 = va_arg(va, uint64_t);
  2401. p += snprintf(p, p_end - p, "%" PRIx64, i64);
  2402. break;
  2403. case 's':
  2404. addr = va_arg(va, target_ulong);
  2405. p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
  2406. addr, va_arg(va, int));
  2407. break;
  2408. default:
  2409. bad_format:
  2410. fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
  2411. fmt - 1);
  2412. break;
  2413. }
  2414. } else {
  2415. *(p++) = *(fmt++);
  2416. }
  2417. }
  2418. *p = 0;
  2419. va_end(va);
  2420. #ifdef CONFIG_USER_ONLY
  2421. put_packet(s, s->syscall_buf);
  2422. gdb_handlesig(s->c_cpu, 0);
  2423. #else
  2424. /* In this case wait to send the syscall packet until notification that
  2425. the CPU has stopped. This must be done because if the packet is sent
  2426. now the reply from the syscall request could be received while the CPU
  2427. is still in the running state, which can cause packets to be dropped
  2428. and state transition 'T' packets to be sent while the syscall is still
  2429. being processed. */
  2430. cpu_exit(s->c_cpu);
  2431. #endif
  2432. }
  2433. static void gdb_read_byte(GDBState *s, int ch)
  2434. {
  2435. int i, csum;
  2436. uint8_t reply;
  2437. #ifndef CONFIG_USER_ONLY
  2438. if (s->last_packet_len) {
  2439. /* Waiting for a response to the last packet. If we see the start
  2440. of a new command then abandon the previous response. */
  2441. if (ch == '-') {
  2442. #ifdef DEBUG_GDB
  2443. printf("Got NACK, retransmitting\n");
  2444. #endif
  2445. put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
  2446. }
  2447. #ifdef DEBUG_GDB
  2448. else if (ch == '+')
  2449. printf("Got ACK\n");
  2450. else
  2451. printf("Got '%c' when expecting ACK/NACK\n", ch);
  2452. #endif
  2453. if (ch == '+' || ch == '$')
  2454. s->last_packet_len = 0;
  2455. if (ch != '$')
  2456. return;
  2457. }
  2458. if (runstate_is_running()) {
  2459. /* when the CPU is running, we cannot do anything except stop
  2460. it when receiving a char */
  2461. vm_stop(RUN_STATE_PAUSED);
  2462. } else
  2463. #endif
  2464. {
  2465. switch(s->state) {
  2466. case RS_IDLE:
  2467. if (ch == '$') {
  2468. s->line_buf_index = 0;
  2469. s->state = RS_GETLINE;
  2470. }
  2471. break;
  2472. case RS_GETLINE:
  2473. if (ch == '#') {
  2474. s->state = RS_CHKSUM1;
  2475. } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
  2476. s->state = RS_IDLE;
  2477. } else {
  2478. s->line_buf[s->line_buf_index++] = ch;
  2479. }
  2480. break;
  2481. case RS_CHKSUM1:
  2482. s->line_buf[s->line_buf_index] = '\0';
  2483. s->line_csum = fromhex(ch) << 4;
  2484. s->state = RS_CHKSUM2;
  2485. break;
  2486. case RS_CHKSUM2:
  2487. s->line_csum |= fromhex(ch);
  2488. csum = 0;
  2489. for(i = 0; i < s->line_buf_index; i++) {
  2490. csum += s->line_buf[i];
  2491. }
  2492. if (s->line_csum != (csum & 0xff)) {
  2493. reply = '-';
  2494. put_buffer(s, &reply, 1);
  2495. s->state = RS_IDLE;
  2496. } else {
  2497. reply = '+';
  2498. put_buffer(s, &reply, 1);
  2499. s->state = gdb_handle_packet(s, s->line_buf);
  2500. }
  2501. break;
  2502. default:
  2503. abort();
  2504. }
  2505. }
  2506. }
  2507. /* Tell the remote gdb that the process has exited. */
  2508. void gdb_exit(CPUArchState *env, int code)
  2509. {
  2510. GDBState *s;
  2511. char buf[4];
  2512. s = gdbserver_state;
  2513. if (!s) {
  2514. return;
  2515. }
  2516. #ifdef CONFIG_USER_ONLY
  2517. if (gdbserver_fd < 0 || s->fd < 0) {
  2518. return;
  2519. }
  2520. #endif
  2521. snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
  2522. put_packet(s, buf);
  2523. #ifndef CONFIG_USER_ONLY
  2524. if (s->chr) {
  2525. qemu_chr_delete(s->chr);
  2526. }
  2527. #endif
  2528. }
  2529. #ifdef CONFIG_USER_ONLY
  2530. int
  2531. gdb_queuesig (void)
  2532. {
  2533. GDBState *s;
  2534. s = gdbserver_state;
  2535. if (gdbserver_fd < 0 || s->fd < 0)
  2536. return 0;
  2537. else
  2538. return 1;
  2539. }
  2540. int
  2541. gdb_handlesig (CPUArchState *env, int sig)
  2542. {
  2543. GDBState *s;
  2544. char buf[256];
  2545. int n;
  2546. s = gdbserver_state;
  2547. if (gdbserver_fd < 0 || s->fd < 0)
  2548. return sig;
  2549. /* disable single step if it was enabled */
  2550. cpu_single_step(env, 0);
  2551. tb_flush(env);
  2552. if (sig != 0)
  2553. {
  2554. snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
  2555. put_packet(s, buf);
  2556. }
  2557. /* put_packet() might have detected that the peer terminated the
  2558. connection. */
  2559. if (s->fd < 0)
  2560. return sig;
  2561. sig = 0;
  2562. s->state = RS_IDLE;
  2563. s->running_state = 0;
  2564. while (s->running_state == 0) {
  2565. n = read (s->fd, buf, 256);
  2566. if (n > 0)
  2567. {
  2568. int i;
  2569. for (i = 0; i < n; i++)
  2570. gdb_read_byte (s, buf[i]);
  2571. }
  2572. else if (n == 0 || errno != EAGAIN)
  2573. {
  2574. /* XXX: Connection closed. Should probably wait for another
  2575. connection before continuing. */
  2576. return sig;
  2577. }
  2578. }
  2579. sig = s->signal;
  2580. s->signal = 0;
  2581. return sig;
  2582. }
  2583. /* Tell the remote gdb that the process has exited due to SIG. */
  2584. void gdb_signalled(CPUArchState *env, int sig)
  2585. {
  2586. GDBState *s;
  2587. char buf[4];
  2588. s = gdbserver_state;
  2589. if (gdbserver_fd < 0 || s->fd < 0)
  2590. return;
  2591. snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
  2592. put_packet(s, buf);
  2593. }
  2594. static void gdb_accept(void)
  2595. {
  2596. GDBState *s;
  2597. struct sockaddr_in sockaddr;
  2598. socklen_t len;
  2599. int val, fd;
  2600. for(;;) {
  2601. len = sizeof(sockaddr);
  2602. fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
  2603. if (fd < 0 && errno != EINTR) {
  2604. perror("accept");
  2605. return;
  2606. } else if (fd >= 0) {
  2607. #ifndef _WIN32
  2608. fcntl(fd, F_SETFD, FD_CLOEXEC);
  2609. #endif
  2610. break;
  2611. }
  2612. }
  2613. /* set short latency */
  2614. val = 1;
  2615. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
  2616. s = g_malloc0(sizeof(GDBState));
  2617. s->c_cpu = first_cpu;
  2618. s->g_cpu = first_cpu;
  2619. s->fd = fd;
  2620. gdb_has_xml = 0;
  2621. gdbserver_state = s;
  2622. fcntl(fd, F_SETFL, O_NONBLOCK);
  2623. }
  2624. static int gdbserver_open(int port)
  2625. {
  2626. struct sockaddr_in sockaddr;
  2627. int fd, val, ret;
  2628. fd = socket(PF_INET, SOCK_STREAM, 0);
  2629. if (fd < 0) {
  2630. perror("socket");
  2631. return -1;
  2632. }
  2633. #ifndef _WIN32
  2634. fcntl(fd, F_SETFD, FD_CLOEXEC);
  2635. #endif
  2636. /* allow fast reuse */
  2637. val = 1;
  2638. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
  2639. sockaddr.sin_family = AF_INET;
  2640. sockaddr.sin_port = htons(port);
  2641. sockaddr.sin_addr.s_addr = 0;
  2642. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  2643. if (ret < 0) {
  2644. perror("bind");
  2645. close(fd);
  2646. return -1;
  2647. }
  2648. ret = listen(fd, 0);
  2649. if (ret < 0) {
  2650. perror("listen");
  2651. close(fd);
  2652. return -1;
  2653. }
  2654. return fd;
  2655. }
  2656. int gdbserver_start(int port)
  2657. {
  2658. gdbserver_fd = gdbserver_open(port);
  2659. if (gdbserver_fd < 0)
  2660. return -1;
  2661. /* accept connections */
  2662. gdb_accept();
  2663. return 0;
  2664. }
  2665. /* Disable gdb stub for child processes. */
  2666. void gdbserver_fork(CPUArchState *env)
  2667. {
  2668. GDBState *s = gdbserver_state;
  2669. if (gdbserver_fd < 0 || s->fd < 0)
  2670. return;
  2671. close(s->fd);
  2672. s->fd = -1;
  2673. cpu_breakpoint_remove_all(env, BP_GDB);
  2674. cpu_watchpoint_remove_all(env, BP_GDB);
  2675. }
  2676. #else
  2677. static int gdb_chr_can_receive(void *opaque)
  2678. {
  2679. /* We can handle an arbitrarily large amount of data.
  2680. Pick the maximum packet size, which is as good as anything. */
  2681. return MAX_PACKET_LENGTH;
  2682. }
  2683. static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
  2684. {
  2685. int i;
  2686. for (i = 0; i < size; i++) {
  2687. gdb_read_byte(gdbserver_state, buf[i]);
  2688. }
  2689. }
  2690. static void gdb_chr_event(void *opaque, int event)
  2691. {
  2692. switch (event) {
  2693. case CHR_EVENT_OPENED:
  2694. vm_stop(RUN_STATE_PAUSED);
  2695. gdb_has_xml = 0;
  2696. break;
  2697. default:
  2698. break;
  2699. }
  2700. }
  2701. static void gdb_monitor_output(GDBState *s, const char *msg, int len)
  2702. {
  2703. char buf[MAX_PACKET_LENGTH];
  2704. buf[0] = 'O';
  2705. if (len > (MAX_PACKET_LENGTH/2) - 1)
  2706. len = (MAX_PACKET_LENGTH/2) - 1;
  2707. memtohex(buf + 1, (uint8_t *)msg, len);
  2708. put_packet(s, buf);
  2709. }
  2710. static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
  2711. {
  2712. const char *p = (const char *)buf;
  2713. int max_sz;
  2714. max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
  2715. for (;;) {
  2716. if (len <= max_sz) {
  2717. gdb_monitor_output(gdbserver_state, p, len);
  2718. break;
  2719. }
  2720. gdb_monitor_output(gdbserver_state, p, max_sz);
  2721. p += max_sz;
  2722. len -= max_sz;
  2723. }
  2724. return len;
  2725. }
  2726. #ifndef _WIN32
  2727. static void gdb_sigterm_handler(int signal)
  2728. {
  2729. if (runstate_is_running()) {
  2730. vm_stop(RUN_STATE_PAUSED);
  2731. }
  2732. }
  2733. #endif
  2734. int gdbserver_start(const char *device)
  2735. {
  2736. GDBState *s;
  2737. char gdbstub_device_name[128];
  2738. CharDriverState *chr = NULL;
  2739. CharDriverState *mon_chr;
  2740. if (!device)
  2741. return -1;
  2742. if (strcmp(device, "none") != 0) {
  2743. if (strstart(device, "tcp:", NULL)) {
  2744. /* enforce required TCP attributes */
  2745. snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
  2746. "%s,nowait,nodelay,server", device);
  2747. device = gdbstub_device_name;
  2748. }
  2749. #ifndef _WIN32
  2750. else if (strcmp(device, "stdio") == 0) {
  2751. struct sigaction act;
  2752. memset(&act, 0, sizeof(act));
  2753. act.sa_handler = gdb_sigterm_handler;
  2754. sigaction(SIGINT, &act, NULL);
  2755. }
  2756. #endif
  2757. chr = qemu_chr_new("gdb", device, NULL);
  2758. if (!chr)
  2759. return -1;
  2760. qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
  2761. gdb_chr_event, NULL);
  2762. }
  2763. s = gdbserver_state;
  2764. if (!s) {
  2765. s = g_malloc0(sizeof(GDBState));
  2766. gdbserver_state = s;
  2767. qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
  2768. /* Initialize a monitor terminal for gdb */
  2769. mon_chr = g_malloc0(sizeof(*mon_chr));
  2770. mon_chr->chr_write = gdb_monitor_write;
  2771. monitor_init(mon_chr, 0);
  2772. } else {
  2773. if (s->chr)
  2774. qemu_chr_delete(s->chr);
  2775. mon_chr = s->mon_chr;
  2776. memset(s, 0, sizeof(GDBState));
  2777. }
  2778. s->c_cpu = first_cpu;
  2779. s->g_cpu = first_cpu;
  2780. s->chr = chr;
  2781. s->state = chr ? RS_IDLE : RS_INACTIVE;
  2782. s->mon_chr = mon_chr;
  2783. s->current_syscall_cb = NULL;
  2784. return 0;
  2785. }
  2786. #endif