2
0

gdbstub.c 70 KB

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