1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
|
/*
* A Win32 based proxy implementing the GBD remote protocol.
* This makes it possible to debug Wine (and any "emulated"
* program) under Linux using GDB.
*
* Copyright (c) Eric Pouech 2002-2004
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* Protocol specification can be found here:
* http://sources.redhat.com/gdb/onlinedocs/gdb/Maintenance-Commands.html
*/
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_SYS_POLL_H
# include <sys/poll.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_TCP_H
# include <netinet/tcp.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
/* if we don't have poll support on this system
* we won't provide gdb proxy support here...
*/
#ifdef HAVE_POLL
#include "debugger.h"
#include "windef.h"
#include "winbase.h"
#include "tlhelp32.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
struct gdb_context
{
/* gdb information */
int sock;
/* incoming buffer */
char* in_buf;
int in_buf_alloc;
int in_len;
/* split into individual packet */
char* in_packet;
int in_packet_len;
/* outgoing buffer */
char* out_buf;
int out_buf_alloc;
int out_len;
int out_curr_packet;
/* generic GDB thread information */
struct dbg_thread* exec_thread; /* thread used in step & continue */
struct dbg_thread* other_thread; /* thread to be used in any other operation */
/* current Win32 trap env */
unsigned last_sig;
BOOL in_trap;
dbg_ctx_t context;
/* Win32 information */
struct dbg_process* process;
/* Unix environment */
unsigned long wine_segs[3]; /* load addresses of the ELF wine exec segments (text, bss and data) */
};
static BOOL tgt_process_gdbproxy_read(HANDLE hProcess, const void* addr,
void* buffer, SIZE_T len, SIZE_T* rlen)
{
return ReadProcessMemory( hProcess, addr, buffer, len, rlen );
}
static BOOL tgt_process_gdbproxy_write(HANDLE hProcess, void* addr,
const void* buffer, SIZE_T len, SIZE_T* wlen)
{
return WriteProcessMemory( hProcess, addr, buffer, len, wlen );
}
static struct be_process_io be_process_gdbproxy_io =
{
NULL, /* we shouldn't use close_process() in gdbproxy */
tgt_process_gdbproxy_read,
tgt_process_gdbproxy_write
};
/* =============================================== *
* B A S I C M A N I P U L A T I O N S *
* =============================================== *
*/
static inline int hex_from0(char ch)
{
if (ch >= '0' && ch <= '9') return ch - '0';
if (ch >= 'A' && ch <= 'F') return ch - 'A' + 10;
if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
assert(0);
return 0;
}
static inline unsigned char hex_to0(int x)
{
assert(x >= 0 && x < 16);
return "0123456789abcdef"[x];
}
static int hex_to_int(const char* src, size_t len)
{
unsigned int returnval = 0;
while (len--)
{
returnval <<= 4;
returnval |= hex_from0(*src++);
}
return returnval;
}
static void hex_from(void* dst, const char* src, size_t len)
{
unsigned char *p = dst;
while (len--)
{
*p++ = (hex_from0(src[0]) << 4) | hex_from0(src[1]);
src += 2;
}
}
static void hex_to(char* dst, const void* src, size_t len)
{
const unsigned char *p = src;
while (len--)
{
*dst++ = hex_to0(*p >> 4);
*dst++ = hex_to0(*p & 0x0F);
p++;
}
}
static unsigned char checksum(const char* ptr, int len)
{
unsigned cksum = 0;
while (len-- > 0)
cksum += (unsigned char)*ptr++;
return cksum;
}
#ifdef __i386__
static const char target_xml[] = "";
#elif defined(__powerpc__)
static const char target_xml[] = "";
#elif defined(__x86_64__)
static const char target_xml[] = "";
#elif defined(__arm__)
static const char target_xml[] =
"l <target><architecture>arm</architecture>\n"
"<feature name=\"org.gnu.gdb.arm.core\">\n"
" <reg name=\"r0\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r1\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r2\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r3\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r4\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r5\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r6\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r7\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r8\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r9\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r10\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r11\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"r12\" bitsize=\"32\" type=\"uint32\"/>\n"
" <reg name=\"sp\" bitsize=\"32\" type=\"data_ptr\"/>\n"
" <reg name=\"lr\" bitsize=\"32\"/>\n"
" <reg name=\"pc\" bitsize=\"32\" type=\"code_ptr\"/>\n"
" <reg name=\"cpsr\" bitsize=\"32\"/>\n"
"</feature></target>\n";
#elif defined(__aarch64__)
static const char target_xml[] = "";
#else
# error Define the registers map for your CPU
#endif
static inline void* cpu_register_ptr(struct gdb_context *gdbctx,
dbg_ctx_t *ctx, unsigned idx)
{
assert(idx < gdbctx->process->be_cpu->gdb_num_regs);
return (char*)ctx + gdbctx->process->be_cpu->gdb_register_map[idx].ctx_offset;
}
static inline DWORD64 cpu_register(struct gdb_context *gdbctx,
dbg_ctx_t *ctx, unsigned idx)
{
switch (gdbctx->process->be_cpu->gdb_register_map[idx].ctx_length)
{
case 1: return *(BYTE*)cpu_register_ptr(gdbctx, ctx, idx);
case 2: return *(WORD*)cpu_register_ptr(gdbctx, ctx, idx);
case 4: return *(DWORD*)cpu_register_ptr(gdbctx, ctx, idx);
case 8: return *(DWORD64*)cpu_register_ptr(gdbctx, ctx, idx);
default:
ERR("got unexpected size: %u\n",
(unsigned)gdbctx->process->be_cpu->gdb_register_map[idx].ctx_length);
assert(0);
return 0;
}
}
static inline void cpu_register_hex_from(struct gdb_context *gdbctx,
dbg_ctx_t* ctx, unsigned idx, const char **phex)
{
const struct gdb_register *cpu_register_map = gdbctx->process->be_cpu->gdb_register_map;
if (cpu_register_map[idx].gdb_length == cpu_register_map[idx].ctx_length)
hex_from(cpu_register_ptr(gdbctx, ctx, idx), *phex, cpu_register_map[idx].gdb_length);
else
{
DWORD64 val = 0;
unsigned i;
BYTE b;
for (i = 0; i < cpu_register_map[idx].gdb_length; i++)
{
hex_from(&b, *phex, 1);
*phex += 2;
val += (DWORD64)b << (8 * i);
}
switch (cpu_register_map[idx].ctx_length)
{
case 1: *(BYTE*)cpu_register_ptr(gdbctx, ctx, idx) = (BYTE)val; break;
case 2: *(WORD*)cpu_register_ptr(gdbctx, ctx, idx) = (WORD)val; break;
case 4: *(DWORD*)cpu_register_ptr(gdbctx, ctx, idx) = (DWORD)val; break;
case 8: *(DWORD64*)cpu_register_ptr(gdbctx, ctx, idx) = val; break;
default: assert(0);
}
}
}
/* =============================================== *
* W I N 3 2 D E B U G I N T E R F A C E *
* =============================================== *
*/
static BOOL fetch_context(struct gdb_context *gdbctx, HANDLE h, dbg_ctx_t *ctx)
{
if (!gdbctx->process->be_cpu->get_context(h, ctx))
{
ERR("Failed to get context, error %u\n", GetLastError());
return FALSE;
}
return TRUE;
}
static BOOL handle_exception(struct gdb_context* gdbctx, EXCEPTION_DEBUG_INFO* exc)
{
EXCEPTION_RECORD* rec = &exc->ExceptionRecord;
BOOL ret = FALSE;
switch (rec->ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
case EXCEPTION_PRIV_INSTRUCTION:
case EXCEPTION_STACK_OVERFLOW:
case EXCEPTION_GUARD_PAGE:
gdbctx->last_sig = SIGSEGV;
ret = TRUE;
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
gdbctx->last_sig = SIGBUS;
ret = TRUE;
break;
case EXCEPTION_SINGLE_STEP:
/* fall through */
case EXCEPTION_BREAKPOINT:
gdbctx->last_sig = SIGTRAP;
ret = TRUE;
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
case EXCEPTION_FLT_INEXACT_RESULT:
case EXCEPTION_FLT_INVALID_OPERATION:
case EXCEPTION_FLT_OVERFLOW:
case EXCEPTION_FLT_STACK_CHECK:
case EXCEPTION_FLT_UNDERFLOW:
gdbctx->last_sig = SIGFPE;
ret = TRUE;
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_INT_OVERFLOW:
gdbctx->last_sig = SIGFPE;
ret = TRUE;
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
gdbctx->last_sig = SIGILL;
ret = TRUE;
break;
case CONTROL_C_EXIT:
gdbctx->last_sig = SIGINT;
ret = TRUE;
break;
case STATUS_POSSIBLE_DEADLOCK:
gdbctx->last_sig = SIGALRM;
ret = TRUE;
/* FIXME: we could also add here a O packet with additional information */
break;
case EXCEPTION_NAME_THREAD:
{
const THREADNAME_INFO *threadname = (const THREADNAME_INFO *)rec->ExceptionInformation;
struct dbg_thread *thread;
char name[9];
SIZE_T read;
if (threadname->dwThreadID == -1)
thread = dbg_curr_thread;
else
thread = dbg_get_thread(gdbctx->process, threadname->dwThreadID);
if (thread)
{
if (gdbctx->process->process_io->read( gdbctx->process->handle,
threadname->szName, name, sizeof(name), &read) && read == sizeof(name))
{
fprintf(stderr, "Thread ID=%04x renamed to \"%.9s\"\n",
threadname->dwThreadID, name);
}
}
else
ERR("Cannot set name of thread %04x\n", threadname->dwThreadID);
return DBG_CONTINUE;
}
case EXCEPTION_INVALID_HANDLE:
return DBG_CONTINUE;
default:
fprintf(stderr, "Unhandled exception code 0x%08x\n", rec->ExceptionCode);
gdbctx->last_sig = SIGABRT;
ret = TRUE;
break;
}
return ret;
}
static void handle_debug_event(struct gdb_context* gdbctx, DEBUG_EVENT* de)
{
union {
char bufferA[256];
WCHAR buffer[256];
} u;
dbg_curr_thread = dbg_get_thread(gdbctx->process, de->dwThreadId);
switch (de->dwDebugEventCode)
{
case CREATE_PROCESS_DEBUG_EVENT:
gdbctx->process = dbg_add_process(&be_process_gdbproxy_io, de->dwProcessId,
de->u.CreateProcessInfo.hProcess);
if (!gdbctx->process) break;
memory_get_string_indirect(gdbctx->process,
de->u.CreateProcessInfo.lpImageName,
de->u.CreateProcessInfo.fUnicode,
u.buffer, ARRAY_SIZE(u.buffer));
dbg_set_process_name(gdbctx->process, u.buffer);
fprintf(stderr, "%04x:%04x: create process '%s'/%p @%p (%u<%u>)\n",
de->dwProcessId, de->dwThreadId,
dbg_W2A(u.buffer, -1),
de->u.CreateProcessInfo.lpImageName,
de->u.CreateProcessInfo.lpStartAddress,
de->u.CreateProcessInfo.dwDebugInfoFileOffset,
de->u.CreateProcessInfo.nDebugInfoSize);
/* de->u.CreateProcessInfo.lpStartAddress; */
if (!dbg_init(gdbctx->process->handle, u.buffer, TRUE))
ERR("Couldn't initiate DbgHelp\n");
fprintf(stderr, "%04x:%04x: create thread I @%p\n", de->dwProcessId,
de->dwThreadId, de->u.CreateProcessInfo.lpStartAddress);
assert(dbg_curr_thread == NULL); /* shouldn't be there */
dbg_add_thread(gdbctx->process, de->dwThreadId,
de->u.CreateProcessInfo.hThread,
de->u.CreateProcessInfo.lpThreadLocalBase);
break;
case LOAD_DLL_DEBUG_EVENT:
assert(dbg_curr_thread);
memory_get_string_indirect(gdbctx->process,
de->u.LoadDll.lpImageName,
de->u.LoadDll.fUnicode,
u.buffer, ARRAY_SIZE(u.buffer));
fprintf(stderr, "%04x:%04x: loads DLL %s @%p (%u<%u>)\n",
de->dwProcessId, de->dwThreadId,
dbg_W2A(u.buffer, -1),
de->u.LoadDll.lpBaseOfDll,
de->u.LoadDll.dwDebugInfoFileOffset,
de->u.LoadDll.nDebugInfoSize);
dbg_load_module(gdbctx->process->handle, de->u.LoadDll.hFile, u.buffer,
(DWORD_PTR)de->u.LoadDll.lpBaseOfDll, 0);
break;
case UNLOAD_DLL_DEBUG_EVENT:
fprintf(stderr, "%08x:%08x: unload DLL @%p\n",
de->dwProcessId, de->dwThreadId, de->u.UnloadDll.lpBaseOfDll);
SymUnloadModule(gdbctx->process->handle,
(DWORD_PTR)de->u.UnloadDll.lpBaseOfDll);
break;
case EXCEPTION_DEBUG_EVENT:
assert(dbg_curr_thread);
TRACE("%08x:%08x: exception code=0x%08x\n", de->dwProcessId,
de->dwThreadId, de->u.Exception.ExceptionRecord.ExceptionCode);
if (fetch_context(gdbctx, dbg_curr_thread->handle, &gdbctx->context))
{
gdbctx->in_trap = handle_exception(gdbctx, &de->u.Exception);
}
break;
case CREATE_THREAD_DEBUG_EVENT:
fprintf(stderr, "%08x:%08x: create thread D @%p\n", de->dwProcessId,
de->dwThreadId, de->u.CreateThread.lpStartAddress);
dbg_add_thread(gdbctx->process,
de->dwThreadId,
de->u.CreateThread.hThread,
de->u.CreateThread.lpThreadLocalBase);
break;
case EXIT_THREAD_DEBUG_EVENT:
fprintf(stderr, "%08x:%08x: exit thread (%u)\n",
de->dwProcessId, de->dwThreadId, de->u.ExitThread.dwExitCode);
assert(dbg_curr_thread);
if (dbg_curr_thread == gdbctx->exec_thread) gdbctx->exec_thread = NULL;
if (dbg_curr_thread == gdbctx->other_thread) gdbctx->other_thread = NULL;
dbg_del_thread(dbg_curr_thread);
break;
case EXIT_PROCESS_DEBUG_EVENT:
fprintf(stderr, "%08x:%08x: exit process (%u)\n",
de->dwProcessId, de->dwThreadId, de->u.ExitProcess.dwExitCode);
dbg_del_process(gdbctx->process);
gdbctx->process = NULL;
/* now signal gdb that we're done */
gdbctx->last_sig = SIGTERM;
gdbctx->in_trap = TRUE;
break;
case OUTPUT_DEBUG_STRING_EVENT:
assert(dbg_curr_thread);
memory_get_string(gdbctx->process,
de->u.DebugString.lpDebugStringData, TRUE,
de->u.DebugString.fUnicode, u.bufferA, sizeof(u.bufferA));
fprintf(stderr, "%08x:%08x: output debug string (%s)\n",
de->dwProcessId, de->dwThreadId, debugstr_a(u.bufferA));
break;
case RIP_EVENT:
fprintf(stderr, "%08x:%08x: rip error=%u type=%u\n", de->dwProcessId,
de->dwThreadId, de->u.RipInfo.dwError, de->u.RipInfo.dwType);
break;
default:
FIXME("%08x:%08x: unknown event (%u)\n",
de->dwProcessId, de->dwThreadId, de->dwDebugEventCode);
}
}
static void resume_debuggee(struct gdb_context* gdbctx, DWORD cont)
{
if (dbg_curr_thread)
{
if (!gdbctx->process->be_cpu->set_context(dbg_curr_thread->handle, &gdbctx->context))
ERR("Failed to set context for thread %04x, error %u\n",
dbg_curr_thread->tid, GetLastError());
if (!ContinueDebugEvent(gdbctx->process->pid, dbg_curr_thread->tid, cont))
ERR("Failed to continue thread %04x, error %u\n",
dbg_curr_thread->tid, GetLastError());
}
else
ERR("Cannot find last thread\n");
}
static void resume_debuggee_thread(struct gdb_context* gdbctx, DWORD cont, unsigned int threadid)
{
if (dbg_curr_thread)
{
if(dbg_curr_thread->tid == threadid){
/* Windows debug and GDB don't seem to work well here, windows only likes ContinueDebugEvent being used on the reporter of the event */
if (!gdbctx->process->be_cpu->set_context(dbg_curr_thread->handle, &gdbctx->context))
ERR("Failed to set context for thread %04x, error %u\n",
dbg_curr_thread->tid, GetLastError());
if (!ContinueDebugEvent(gdbctx->process->pid, dbg_curr_thread->tid, cont))
ERR("Failed to continue thread %04x, error %u\n",
dbg_curr_thread->tid, GetLastError());
}
}
else
ERR("Cannot find last thread\n");
}
static BOOL check_for_interrupt(struct gdb_context* gdbctx)
{
struct pollfd pollfd;
int ret;
char pkt;
pollfd.fd = gdbctx->sock;
pollfd.events = POLLIN;
pollfd.revents = 0;
if ((ret = poll(&pollfd, 1, 0)) == 1) {
ret = read(gdbctx->sock, &pkt, 1);
if (ret != 1) {
ERR("read failed\n");
return FALSE;
}
if (pkt != '\003') {
ERR("Unexpected break packet %#02x\n", pkt);
return FALSE;
}
return TRUE;
} else if (ret == -1) {
ERR("poll failed\n");
}
return FALSE;
}
static void wait_for_debuggee(struct gdb_context* gdbctx)
{
DEBUG_EVENT de;
gdbctx->in_trap = FALSE;
for (;;)
{
if (!WaitForDebugEvent(&de, 10))
{
if (GetLastError() == ERROR_SEM_TIMEOUT)
{
if (check_for_interrupt(gdbctx)) {
if (!DebugBreakProcess(gdbctx->process->handle)) {
ERR("Failed to break into debugee\n");
break;
}
WaitForDebugEvent(&de, INFINITE);
} else {
continue;
}
} else {
break;
}
}
handle_debug_event(gdbctx, &de);
assert(!gdbctx->process ||
gdbctx->process->pid == 0 ||
de.dwProcessId == gdbctx->process->pid);
assert(!dbg_curr_thread || de.dwThreadId == dbg_curr_thread->tid);
if (gdbctx->in_trap) break;
ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
}
}
static void detach_debuggee(struct gdb_context* gdbctx, BOOL kill)
{
assert(gdbctx->process->be_cpu);
gdbctx->process->be_cpu->single_step(&gdbctx->context, FALSE);
resume_debuggee(gdbctx, DBG_CONTINUE);
if (!kill)
DebugActiveProcessStop(gdbctx->process->pid);
dbg_del_process(gdbctx->process);
gdbctx->process = NULL;
}
static void get_process_info(struct gdb_context* gdbctx, char* buffer, size_t len)
{
DWORD status;
if (!GetExitCodeProcess(gdbctx->process->handle, &status))
{
strcpy(buffer, "Unknown process");
return;
}
if (status == STILL_ACTIVE)
{
strcpy(buffer, "Running");
}
else
snprintf(buffer, len, "Terminated (%u)", status);
switch (GetPriorityClass(gdbctx->process->handle))
{
case 0: break;
#ifdef ABOVE_NORMAL_PRIORITY_CLASS
case ABOVE_NORMAL_PRIORITY_CLASS: strcat(buffer, ", above normal priority"); break;
#endif
#ifdef BELOW_NORMAL_PRIORITY_CLASS
case BELOW_NORMAL_PRIORITY_CLASS: strcat(buffer, ", below normal priotity"); break;
#endif
case HIGH_PRIORITY_CLASS: strcat(buffer, ", high priority"); break;
case IDLE_PRIORITY_CLASS: strcat(buffer, ", idle priority"); break;
case NORMAL_PRIORITY_CLASS: strcat(buffer, ", normal priority"); break;
case REALTIME_PRIORITY_CLASS: strcat(buffer, ", realtime priority"); break;
}
strcat(buffer, "\n");
}
static void get_thread_info(struct gdb_context* gdbctx, unsigned tid,
char* buffer, size_t len)
{
struct dbg_thread* thd;
DWORD status;
int prio;
/* FIXME: use the size of buffer */
thd = dbg_get_thread(gdbctx->process, tid);
if (thd == NULL)
{
strcpy(buffer, "No information");
return;
}
if (GetExitCodeThread(thd->handle, &status))
{
if (status == STILL_ACTIVE)
{
/* FIXME: this is a bit brutal... some nicer way shall be found */
switch (status = SuspendThread(thd->handle))
{
case -1: break;
case 0: strcpy(buffer, "Running"); break;
default: snprintf(buffer, len, "Suspended (%u)", status - 1);
}
ResumeThread(thd->handle);
}
else
snprintf(buffer, len, "Terminated (exit code = %u)", status);
}
else
{
strcpy(buffer, "Unknown threadID");
}
switch (prio = GetThreadPriority(thd->handle))
{
case THREAD_PRIORITY_ERROR_RETURN: break;
case THREAD_PRIORITY_ABOVE_NORMAL: strcat(buffer, ", priority +1 above normal"); break;
case THREAD_PRIORITY_BELOW_NORMAL: strcat(buffer, ", priority -1 below normal"); break;
case THREAD_PRIORITY_HIGHEST: strcat(buffer, ", priority +2 above normal"); break;
case THREAD_PRIORITY_LOWEST: strcat(buffer, ", priority -2 below normal"); break;
case THREAD_PRIORITY_IDLE: strcat(buffer, ", priority idle"); break;
case THREAD_PRIORITY_NORMAL: strcat(buffer, ", priority normal"); break;
case THREAD_PRIORITY_TIME_CRITICAL: strcat(buffer, ", priority time-critical"); break;
default: snprintf(buffer + strlen(buffer), len - strlen(buffer), ", priority = %d", prio);
}
assert(strlen(buffer) < len);
}
/* =============================================== *
* P A C K E T U T I L S *
* =============================================== *
*/
enum packet_return {packet_error = 0x00, packet_ok = 0x01, packet_done = 0x02,
packet_last_f = 0x80};
static char* packet_realloc(char* buf, int size)
{
if (!buf)
return HeapAlloc(GetProcessHeap(), 0, size);
return HeapReAlloc(GetProcessHeap(), 0, buf, size);
}
static void packet_reply_grow(struct gdb_context* gdbctx, size_t size)
{
if (gdbctx->out_buf_alloc < gdbctx->out_len + size)
{
gdbctx->out_buf_alloc = ((gdbctx->out_len + size) / 32 + 1) * 32;
gdbctx->out_buf = packet_realloc(gdbctx->out_buf, gdbctx->out_buf_alloc);
}
}
static void packet_reply_hex_to(struct gdb_context* gdbctx, const void* src, int len)
{
packet_reply_grow(gdbctx, len * 2);
hex_to(&gdbctx->out_buf[gdbctx->out_len], src, len);
gdbctx->out_len += len * 2;
}
static inline void packet_reply_hex_to_str(struct gdb_context* gdbctx, const char* src)
{
packet_reply_hex_to(gdbctx, src, strlen(src));
}
static void packet_reply_val(struct gdb_context* gdbctx, unsigned long val, int len)
{
int i, shift;
shift = (len - 1) * 8;
packet_reply_grow(gdbctx, len * 2);
for (i = 0; i < len; i++, shift -= 8)
{
gdbctx->out_buf[gdbctx->out_len++] = hex_to0((val >> (shift + 4)) & 0x0F);
gdbctx->out_buf[gdbctx->out_len++] = hex_to0((val >> shift ) & 0x0F);
}
}
static inline void packet_reply_add(struct gdb_context* gdbctx, const char* str)
{
int len = strlen(str);
packet_reply_grow(gdbctx, len);
memcpy(&gdbctx->out_buf[gdbctx->out_len], str, len);
gdbctx->out_len += len;
}
static void packet_reply_open(struct gdb_context* gdbctx)
{
assert(gdbctx->out_curr_packet == -1);
packet_reply_add(gdbctx, "$");
gdbctx->out_curr_packet = gdbctx->out_len;
}
static void packet_reply_close(struct gdb_context* gdbctx)
{
unsigned char cksum;
int plen;
plen = gdbctx->out_len - gdbctx->out_curr_packet;
packet_reply_add(gdbctx, "#");
cksum = checksum(&gdbctx->out_buf[gdbctx->out_curr_packet], plen);
packet_reply_hex_to(gdbctx, &cksum, 1);
gdbctx->out_curr_packet = -1;
}
static enum packet_return packet_reply(struct gdb_context* gdbctx, const char* packet)
{
packet_reply_open(gdbctx);
assert(strchr(packet, '$') == NULL && strchr(packet, '#') == NULL);
packet_reply_add(gdbctx, packet);
packet_reply_close(gdbctx);
return packet_done;
}
static enum packet_return packet_reply_error(struct gdb_context* gdbctx, int error)
{
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "E");
packet_reply_val(gdbctx, error, 1);
packet_reply_close(gdbctx);
return packet_done;
}
static inline void packet_reply_register_hex_to(struct gdb_context* gdbctx, unsigned idx)
{
const struct gdb_register *cpu_register_map = gdbctx->process->be_cpu->gdb_register_map;
if (cpu_register_map[idx].gdb_length == cpu_register_map[idx].ctx_length)
packet_reply_hex_to(gdbctx, cpu_register_ptr(gdbctx, &gdbctx->context, idx),
cpu_register_map[idx].gdb_length);
else
{
DWORD64 val = cpu_register(gdbctx, &gdbctx->context, idx);
unsigned i;
for (i = 0; i < cpu_register_map[idx].gdb_length; i++)
{
BYTE b = val;
packet_reply_hex_to(gdbctx, &b, 1);
val >>= 8;
}
}
}
/* =============================================== *
* P A C K E T H A N D L E R S *
* =============================================== *
*/
static enum packet_return packet_reply_status(struct gdb_context* gdbctx)
{
enum packet_return ret = packet_done;
packet_reply_open(gdbctx);
if (gdbctx->process != NULL)
{
unsigned char sig;
unsigned i;
packet_reply_add(gdbctx, "T");
sig = gdbctx->last_sig;
packet_reply_val(gdbctx, sig, 1);
packet_reply_add(gdbctx, "thread:");
packet_reply_val(gdbctx, dbg_curr_thread->tid, 4);
packet_reply_add(gdbctx, ";");
for (i = 0; i < gdbctx->process->be_cpu->gdb_num_regs; i++)
{
/* FIXME: this call will also grow the buffer...
* unneeded, but not harmful
*/
packet_reply_val(gdbctx, i, 1);
packet_reply_add(gdbctx, ":");
packet_reply_register_hex_to(gdbctx, i);
packet_reply_add(gdbctx, ";");
}
}
else
{
/* Try to put an exit code
* Cannot use GetExitCodeProcess, wouldn't fit in a 8 bit value, so
* just indicate the end of process and exit */
packet_reply_add(gdbctx, "W00");
/*if (!gdbctx->extended)*/ ret |= packet_last_f;
}
packet_reply_close(gdbctx);
return ret;
}
#if 0
static enum packet_return packet_extended(struct gdb_context* gdbctx)
{
gdbctx->extended = 1;
return packet_ok;
}
#endif
static enum packet_return packet_last_signal(struct gdb_context* gdbctx)
{
assert(gdbctx->in_packet_len == 0);
return packet_reply_status(gdbctx);
}
static enum packet_return packet_continue(struct gdb_context* gdbctx)
{
/* FIXME: add support for address in packet */
assert(gdbctx->in_packet_len == 0);
if (dbg_curr_thread != gdbctx->exec_thread && gdbctx->exec_thread)
FIXME("Can't continue thread %04x while on thread %04x\n",
gdbctx->exec_thread->tid, dbg_curr_thread->tid);
resume_debuggee(gdbctx, DBG_CONTINUE);
wait_for_debuggee(gdbctx);
return packet_reply_status(gdbctx);
}
static enum packet_return packet_verbose_cont(struct gdb_context* gdbctx)
{
int i;
int defaultAction = -1; /* magic non action */
unsigned char sig;
int actions =0;
int actionIndex[20]; /* allow for up to 20 actions */
int threadIndex[20];
int threadCount = 0;
unsigned int threadIDs[100]; /* TODO: Should make this dynamic */
unsigned int threadID = 0;
struct dbg_thread* thd;
/* OK we have vCont followed by..
* ? for query
* c for packet_continue
* Csig for packet_continue_signal
* s for step
* Ssig for step signal
* and then an optional thread ID at the end..
* *******************************************/
/* Query */
if (gdbctx->in_packet[4] == '?')
{
/*
Reply:
`vCont[;action]...'
The vCont packet is supported. Each action is a supported command in the vCont packet.
`'
The vCont packet is not supported. (this didn't seem to be obeyed!)
*/
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "vCont");
/* add all the supported actions to the reply (all of them for now) */
packet_reply_add(gdbctx, ";c");
packet_reply_add(gdbctx, ";C");
packet_reply_add(gdbctx, ";s");
packet_reply_add(gdbctx, ";S");
packet_reply_close(gdbctx);
return packet_done;
}
/* go through the packet and identify where all the actions start at */
for (i = 4; i < gdbctx->in_packet_len - 1; i++)
{
if (gdbctx->in_packet[i] == ';')
{
threadIndex[actions] = 0;
actionIndex[actions++] = i;
}
else if (gdbctx->in_packet[i] == ':')
{
threadIndex[actions - 1] = i;
}
}
/* now look up the default action */
for (i = 0 ; i < actions; i++)
{
if (threadIndex[i] == 0)
{
if (defaultAction != -1)
{
fprintf(stderr,"Too many default actions specified\n");
return packet_error;
}
defaultAction = i;
}
}
/* Now, I have this default action thing that needs to be applied to all non counted threads */
/* go through all the threads and stick their ids in the to be done list. */
LIST_FOR_EACH_ENTRY(thd, &gdbctx->process->threads, struct dbg_thread, entry)
{
threadIDs[threadCount++] = thd->tid;
/* check to see if we have more threads than I counted on, and tell the user what to do
* (they're running winedbg, so I'm sure they can fix the problem from the error message!) */
if (threadCount == 100)
{
fprintf(stderr, "Wow, that's a lot of threads, change threadIDs in wine/programs/winedbg/gdbproxy.c to be higher\n");
break;
}
}
/* Ok, now we have... actionIndex full of actions and we know what threads there are, so all
* that remains is to apply the actions to the threads and the default action to any threads
* left */
if (dbg_curr_thread != gdbctx->exec_thread && gdbctx->exec_thread)
FIXME("Can't continue thread %04x while on thread %04x\n",
gdbctx->exec_thread->tid, dbg_curr_thread->tid);
/* deal with the threaded stuff first */
for (i = 0; i < actions ; i++)
{
if (threadIndex[i] != 0)
{
int j, idLength = 0;
if (i < actions - 1)
{
idLength = (actionIndex[i+1] - threadIndex[i]) - 1;
}
else
{
idLength = (gdbctx->in_packet_len - threadIndex[i]) - 1;
}
threadID = hex_to_int(gdbctx->in_packet + threadIndex[i] + 1 , idLength);
/* process the action */
switch (gdbctx->in_packet[actionIndex[i] + 1])
{
case 's': /* step */
gdbctx->process->be_cpu->single_step(&gdbctx->context, TRUE);
/* fall through*/
case 'c': /* continue */
resume_debuggee_thread(gdbctx, DBG_CONTINUE, threadID);
break;
case 'S': /* step Sig, */
gdbctx->process->be_cpu->single_step(&gdbctx->context, TRUE);
/* fall through */
case 'C': /* continue sig */
hex_from(&sig, gdbctx->in_packet + actionIndex[i] + 2, 1);
/* cannot change signals on the fly */
TRACE("sigs: %u %u\n", sig, gdbctx->last_sig);
if (sig != gdbctx->last_sig)
return packet_error;
resume_debuggee_thread(gdbctx, DBG_EXCEPTION_NOT_HANDLED, threadID);
break;
}
for (j = 0 ; j < threadCount; j++)
{
if (threadIDs[j] == threadID)
{
threadIDs[j] = 0;
break;
}
}
}
} /* for i=0 ; i< actions */
/* now we have manage the default action */
if (defaultAction >= 0)
{
for (i = 0 ; i< threadCount; i++)
{
/* check to see if we've already done something to the thread*/
if (threadIDs[i] != 0)
{
/* if not apply the default action*/
threadID = threadIDs[i];
/* process the action (yes this is almost identical to the one above!) */
switch (gdbctx->in_packet[actionIndex[defaultAction] + 1])
{
case 's': /* step */
gdbctx->process->be_cpu->single_step(&gdbctx->context, TRUE);
/* fall through */
case 'c': /* continue */
resume_debuggee_thread(gdbctx, DBG_CONTINUE, threadID);
break;
case 'S':
gdbctx->process->be_cpu->single_step(&gdbctx->context, TRUE);
/* fall through */
case 'C': /* continue sig */
hex_from(&sig, gdbctx->in_packet + actionIndex[defaultAction] + 2, 1);
/* cannot change signals on the fly */
TRACE("sigs: %u %u\n", sig, gdbctx->last_sig);
if (sig != gdbctx->last_sig)
return packet_error;
resume_debuggee_thread(gdbctx, DBG_EXCEPTION_NOT_HANDLED, threadID);
break;
}
}
}
} /* if(defaultAction >=0) */
wait_for_debuggee(gdbctx);
if (gdbctx->process)
gdbctx->process->be_cpu->single_step(&gdbctx->context, FALSE);
return packet_reply_status(gdbctx);
}
static enum packet_return packet_verbose(struct gdb_context* gdbctx)
{
if (gdbctx->in_packet_len >= 4 && !memcmp(gdbctx->in_packet, "Cont", 4))
{
return packet_verbose_cont(gdbctx);
}
WARN("Unhandled verbose packet %s\n",
debugstr_an(gdbctx->in_packet, gdbctx->in_packet_len));
return packet_error;
}
static enum packet_return packet_continue_signal(struct gdb_context* gdbctx)
{
unsigned char sig;
/* FIXME: add support for address in packet */
assert(gdbctx->in_packet_len == 2);
if (dbg_curr_thread != gdbctx->exec_thread && gdbctx->exec_thread)
FIXME("Can't continue thread %04x while on thread %04x\n",
gdbctx->exec_thread->tid, dbg_curr_thread->tid);
hex_from(&sig, gdbctx->in_packet, 1);
/* cannot change signals on the fly */
TRACE("sigs: %u %u\n", sig, gdbctx->last_sig);
if (sig != gdbctx->last_sig)
return packet_error;
resume_debuggee(gdbctx, DBG_EXCEPTION_NOT_HANDLED);
wait_for_debuggee(gdbctx);
return packet_reply_status(gdbctx);
}
static enum packet_return packet_detach(struct gdb_context* gdbctx)
{
detach_debuggee(gdbctx, FALSE);
return packet_ok | packet_last_f;
}
static enum packet_return packet_read_registers(struct gdb_context* gdbctx)
{
int i;
dbg_ctx_t ctx;
assert(gdbctx->in_trap);
if (dbg_curr_thread != gdbctx->other_thread && gdbctx->other_thread)
{
if (!fetch_context(gdbctx, gdbctx->other_thread->handle, &ctx))
return packet_error;
}
packet_reply_open(gdbctx);
for (i = 0; i < gdbctx->process->be_cpu->gdb_num_regs; i++)
packet_reply_register_hex_to(gdbctx, i);
packet_reply_close(gdbctx);
return packet_done;
}
static enum packet_return packet_write_registers(struct gdb_context* gdbctx)
{
const size_t cpu_num_regs = gdbctx->process->be_cpu->gdb_num_regs;
unsigned i;
dbg_ctx_t ctx;
dbg_ctx_t *pctx = &gdbctx->context;
const char* ptr;
assert(gdbctx->in_trap);
if (dbg_curr_thread != gdbctx->other_thread && gdbctx->other_thread)
{
if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
return packet_error;
}
if (gdbctx->in_packet_len < cpu_num_regs * 2) return packet_error;
ptr = gdbctx->in_packet;
for (i = 0; i < cpu_num_regs; i++)
cpu_register_hex_from(gdbctx, pctx, i, &ptr);
if (pctx != &gdbctx->context &&
!gdbctx->process->be_cpu->set_context(gdbctx->other_thread->handle, pctx))
{
ERR("Failed to set context for tid %04x, error %u\n",
gdbctx->other_thread->tid, GetLastError());
return packet_error;
}
return packet_ok;
}
static enum packet_return packet_kill(struct gdb_context* gdbctx)
{
detach_debuggee(gdbctx, TRUE);
#if 0
if (!gdbctx->extended)
/* dunno whether GDB cares or not */
#endif
wait(NULL);
exit(0);
/* assume we can't really answer something here */
/* return packet_done; */
}
static enum packet_return packet_thread(struct gdb_context* gdbctx)
{
char* end;
unsigned thread;
switch (gdbctx->in_packet[0])
{
case 'c':
case 'g':
if (gdbctx->in_packet[1] == '-')
thread = -strtol(gdbctx->in_packet + 2, &end, 16);
else
thread = strtol(gdbctx->in_packet + 1, &end, 16);
if (end == NULL || end > gdbctx->in_packet + gdbctx->in_packet_len)
{
ERR("Failed to parse %s\n",
debugstr_an(gdbctx->in_packet, gdbctx->in_packet_len));
return packet_error;
}
if (gdbctx->in_packet[0] == 'c')
gdbctx->exec_thread = dbg_get_thread(gdbctx->process, thread);
else
gdbctx->other_thread = dbg_get_thread(gdbctx->process, thread);
return packet_ok;
default:
FIXME("Unknown thread sub-command %c\n", gdbctx->in_packet[0]);
return packet_error;
}
}
static enum packet_return packet_read_memory(struct gdb_context* gdbctx)
{
char *addr;
unsigned int len, blk_len, nread;
char buffer[32];
SIZE_T r = 0;
assert(gdbctx->in_trap);
/* FIXME:check in_packet_len for reading %p,%x */
if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2) return packet_error;
if (len <= 0) return packet_error;
TRACE("Read %u bytes at %p\n", len, addr);
for (nread = 0; nread < len; nread += r, addr += r)
{
blk_len = min(sizeof(buffer), len - nread);
if (!gdbctx->process->process_io->read(gdbctx->process->handle, addr,
buffer, blk_len, &r) || r == 0)
{
/* fail at first address, return error */
if (nread == 0) return packet_reply_error(gdbctx, EFAULT);
/* something has already been read, return partial information */
break;
}
if (nread == 0) packet_reply_open(gdbctx);
packet_reply_hex_to(gdbctx, buffer, r);
}
packet_reply_close(gdbctx);
return packet_done;
}
static enum packet_return packet_write_memory(struct gdb_context* gdbctx)
{
char* addr;
unsigned int len, blk_len;
char* ptr;
char buffer[32];
SIZE_T w;
assert(gdbctx->in_trap);
ptr = memchr(gdbctx->in_packet, ':', gdbctx->in_packet_len);
if (ptr == NULL)
{
ERR("Cannot find ':' in %s\n", debugstr_an(gdbctx->in_packet, gdbctx->in_packet_len));
return packet_error;
}
*ptr++ = '\0';
if (sscanf(gdbctx->in_packet, "%p,%x", &addr, &len) != 2)
{
ERR("Failed to parse %s\n", debugstr_a(gdbctx->in_packet));
return packet_error;
}
if (ptr - gdbctx->in_packet + len * 2 != gdbctx->in_packet_len)
{
ERR("Length %u does not match packet length %u\n",
(int)(ptr - gdbctx->in_packet) + len * 2, gdbctx->in_packet_len);
return packet_error;
}
TRACE("Write %u bytes at %p\n", len, addr);
while (len > 0)
{
blk_len = min(sizeof(buffer), len);
hex_from(buffer, ptr, blk_len);
if (!gdbctx->process->process_io->write(gdbctx->process->handle, addr, buffer, blk_len, &w) ||
w != blk_len)
break;
addr += blk_len;
len -= blk_len;
ptr += blk_len;
}
return packet_ok; /* FIXME: error while writing ? */
}
static enum packet_return packet_read_register(struct gdb_context* gdbctx)
{
unsigned reg;
dbg_ctx_t ctx;
dbg_ctx_t *pctx = &gdbctx->context;
assert(gdbctx->in_trap);
reg = hex_to_int(gdbctx->in_packet, gdbctx->in_packet_len);
if (reg >= gdbctx->process->be_cpu->gdb_num_regs)
{
WARN("Unhandled register %u\n", reg);
return packet_error;
}
if (dbg_curr_thread != gdbctx->other_thread && gdbctx->other_thread)
{
if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
return packet_error;
}
TRACE("%u => %s\n", reg, wine_dbgstr_longlong(cpu_register(gdbctx, pctx, reg)));
packet_reply_open(gdbctx);
packet_reply_register_hex_to(gdbctx, reg);
packet_reply_close(gdbctx);
return packet_done;
}
static enum packet_return packet_write_register(struct gdb_context* gdbctx)
{
unsigned reg;
char* ptr;
dbg_ctx_t ctx;
dbg_ctx_t *pctx = &gdbctx->context;
assert(gdbctx->in_trap);
reg = strtoul(gdbctx->in_packet, &ptr, 16);
if (ptr == NULL || reg >= gdbctx->process->be_cpu->gdb_num_regs || *ptr++ != '=')
{
WARN("Unhandled register %s\n",
debugstr_an(gdbctx->in_packet, gdbctx->in_packet_len));
/* FIXME: if just the reg is above cpu_num_regs, don't tell gdb
* it wouldn't matter too much, and it fakes our support for all regs
*/
return (ptr == NULL) ? packet_error : packet_ok;
}
TRACE("%u <= %s\n", reg,
debugstr_an(ptr, (int)(gdbctx->in_packet_len - (ptr - gdbctx->in_packet))));
if (dbg_curr_thread != gdbctx->other_thread && gdbctx->other_thread)
{
if (!fetch_context(gdbctx, gdbctx->other_thread->handle, pctx = &ctx))
return packet_error;
}
cpu_register_hex_from(gdbctx, pctx, reg, (const char**)&ptr);
if (pctx != &gdbctx->context &&
!gdbctx->process->be_cpu->set_context(gdbctx->other_thread->handle, pctx))
{
ERR("Failed to set context for tid %04x, error %u\n",
gdbctx->other_thread->tid, GetLastError());
return packet_error;
}
return packet_ok;
}
static void packet_query_monitor_wnd_helper(struct gdb_context* gdbctx, HWND hWnd, int indent)
{
char buffer[128];
char clsName[128];
char wndName[128];
HWND child;
do {
if (!GetClassNameA(hWnd, clsName, sizeof(clsName)))
strcpy(clsName, "-- Unknown --");
if (!GetWindowTextA(hWnd, wndName, sizeof(wndName)))
strcpy(wndName, "-- Empty --");
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "O");
snprintf(buffer, sizeof(buffer),
"%*s%04lx%*s%-17.17s %08x %0*lx %.14s\n",
indent, "", (ULONG_PTR)hWnd, 13 - indent, "",
clsName, GetWindowLongW(hWnd, GWL_STYLE),
ADDRWIDTH, (ULONG_PTR)GetWindowLongPtrW(hWnd, GWLP_WNDPROC),
wndName);
packet_reply_hex_to_str(gdbctx, buffer);
packet_reply_close(gdbctx);
if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
packet_query_monitor_wnd_helper(gdbctx, child, indent + 1);
} while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
}
static void packet_query_monitor_wnd(struct gdb_context* gdbctx, int len, const char* str)
{
char buffer[128];
/* we do the output in several 'O' packets, with the last one being just OK for
* marking the end of the output */
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "O");
snprintf(buffer, sizeof(buffer),
"%-16.16s %-17.17s %-8.8s %s\n",
"hwnd", "Class Name", " Style", " WndProc Text");
packet_reply_hex_to_str(gdbctx, buffer);
packet_reply_close(gdbctx);
/* FIXME: could also add a pmt to this command in str... */
packet_query_monitor_wnd_helper(gdbctx, GetDesktopWindow(), 0);
packet_reply(gdbctx, "OK");
}
static void packet_query_monitor_process(struct gdb_context* gdbctx, int len, const char* str)
{
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
char buffer[31+MAX_PATH];
char deco;
PROCESSENTRY32 entry;
BOOL ok;
if (snap == INVALID_HANDLE_VALUE)
return;
entry.dwSize = sizeof(entry);
ok = Process32First(snap, &entry);
/* we do the output in several 'O' packets, with the last one being just OK for
* marking the end of the output */
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "O");
snprintf(buffer, sizeof(buffer),
" %-8.8s %-8.8s %-8.8s %s\n",
"pid", "threads", "parent", "executable");
packet_reply_hex_to_str(gdbctx, buffer);
packet_reply_close(gdbctx);
while (ok)
{
deco = ' ';
if (entry.th32ProcessID == gdbctx->process->pid) deco = '>';
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "O");
snprintf(buffer, sizeof(buffer),
"%c%08x %-8d %08x '%s'\n",
deco, entry.th32ProcessID, entry.cntThreads,
entry.th32ParentProcessID, entry.szExeFile);
packet_reply_hex_to_str(gdbctx, buffer);
packet_reply_close(gdbctx);
ok = Process32Next(snap, &entry);
}
CloseHandle(snap);
packet_reply(gdbctx, "OK");
}
static void packet_query_monitor_mem(struct gdb_context* gdbctx, int len, const char* str)
{
MEMORY_BASIC_INFORMATION mbi;
char* addr = 0;
const char* state;
const char* type;
char prot[3+1];
char buffer[128];
/* we do the output in several 'O' packets, with the last one being just OK for
* marking the end of the output */
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "O");
packet_reply_hex_to_str(gdbctx, "Address Size State Type RWX\n");
packet_reply_close(gdbctx);
while (VirtualQueryEx(gdbctx->process->handle, addr, &mbi, sizeof(mbi)) >= sizeof(mbi))
{
switch (mbi.State)
{
case MEM_COMMIT: state = "commit "; break;
case MEM_FREE: state = "free "; break;
case MEM_RESERVE: state = "reserve"; break;
default: state = "??? "; break;
}
if (mbi.State != MEM_FREE)
{
switch (mbi.Type)
{
case MEM_IMAGE: type = "image "; break;
case MEM_MAPPED: type = "mapped "; break;
case MEM_PRIVATE: type = "private"; break;
case 0: type = " "; break;
default: type = "??? "; break;
}
memset(prot, ' ' , sizeof(prot)-1);
prot[sizeof(prot)-1] = '\0';
if (mbi.AllocationProtect & (PAGE_READONLY|PAGE_READWRITE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE))
prot[0] = 'R';
if (mbi.AllocationProtect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE))
prot[1] = 'W';
if (mbi.AllocationProtect & (PAGE_WRITECOPY|PAGE_EXECUTE_WRITECOPY))
prot[1] = 'C';
if (mbi.AllocationProtect & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE))
prot[2] = 'X';
}
else
{
type = "";
prot[0] = '\0';
}
packet_reply_open(gdbctx);
snprintf(buffer, sizeof(buffer), "%0*lx %0*lx %s %s %s\n",
(unsigned)sizeof(void*), (DWORD_PTR)addr,
(unsigned)sizeof(void*), mbi.RegionSize, state, type, prot);
packet_reply_add(gdbctx, "O");
packet_reply_hex_to_str(gdbctx, buffer);
packet_reply_close(gdbctx);
if (addr + mbi.RegionSize < addr) /* wrap around ? */
break;
addr += mbi.RegionSize;
}
packet_reply(gdbctx, "OK");
}
struct query_detail
{
int with_arg;
const char* name;
size_t len;
void (*handler)(struct gdb_context*, int, const char*);
} query_details[] =
{
{0, "wnd", 3, packet_query_monitor_wnd},
{0, "window", 6, packet_query_monitor_wnd},
{0, "proc", 4, packet_query_monitor_process},
{0, "process", 7, packet_query_monitor_process},
{0, "mem", 3, packet_query_monitor_mem},
{0, NULL, 0, NULL},
};
static enum packet_return packet_query_remote_command(struct gdb_context* gdbctx,
const char* hxcmd, size_t len)
{
char buffer[128];
struct query_detail* qd;
assert((len & 1) == 0 && len < 2 * sizeof(buffer));
len /= 2;
hex_from(buffer, hxcmd, len);
for (qd = query_details; qd->name != NULL; qd++)
{
if (len < qd->len || strncmp(buffer, qd->name, qd->len) != 0) continue;
if (!qd->with_arg && len != qd->len) continue;
(qd->handler)(gdbctx, len - qd->len, buffer + qd->len);
return packet_done;
}
return packet_reply_error(gdbctx, EINVAL);
}
static enum packet_return packet_query(struct gdb_context* gdbctx)
{
switch (gdbctx->in_packet[0])
{
case 'f':
if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
{
struct dbg_thread* thd;
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "m");
LIST_FOR_EACH_ENTRY(thd, &gdbctx->process->threads, struct dbg_thread, entry)
{
packet_reply_val(gdbctx, thd->tid, 4);
if (list_next(&gdbctx->process->threads, &thd->entry) != NULL)
packet_reply_add(gdbctx, ",");
}
packet_reply_close(gdbctx);
return packet_done;
}
else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
{
char result[128];
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "O");
get_process_info(gdbctx, result, sizeof(result));
packet_reply_hex_to_str(gdbctx, result);
packet_reply_close(gdbctx);
return packet_done;
}
break;
case 's':
if (strncmp(gdbctx->in_packet + 1, "ThreadInfo", gdbctx->in_packet_len - 1) == 0)
{
packet_reply(gdbctx, "l");
return packet_done;
}
else if (strncmp(gdbctx->in_packet + 1, "ProcessInfo", gdbctx->in_packet_len - 1) == 0)
{
packet_reply(gdbctx, "l");
return packet_done;
}
break;
case 'A':
if (strncmp(gdbctx->in_packet, "Attached", gdbctx->in_packet_len) == 0)
return packet_reply(gdbctx, "1");
break;
case 'C':
if (gdbctx->in_packet_len == 1)
{
struct dbg_thread* thd;
/* FIXME: doc says 16 bit val ??? */
/* grab first created thread, aka last in list */
assert(gdbctx->process && !list_empty(&gdbctx->process->threads));
thd = LIST_ENTRY(list_tail(&gdbctx->process->threads), struct dbg_thread, entry);
packet_reply_open(gdbctx);
packet_reply_add(gdbctx, "QC");
packet_reply_val(gdbctx, thd->tid, 4);
packet_reply_close(gdbctx);
return packet_done;
}
break;
case 'O':
if (strncmp(gdbctx->in_packet, "Offsets", gdbctx->in_packet_len) == 0)
{
char buf[64];
snprintf(buf, sizeof(buf),
"Text=%08lx;Data=%08lx;Bss=%08lx",
gdbctx->wine_segs[0], gdbctx->wine_segs[1],
gdbctx->wine_segs[2]);
return packet_reply(gdbctx, buf);
}
break;
case 'R':
if (gdbctx->in_packet_len > 5 && strncmp(gdbctx->in_packet, "Rcmd,", 5) == 0)
{
return packet_query_remote_command(gdbctx, gdbctx->in_packet + 5,
gdbctx->in_packet_len - 5);
}
break;
case 'S':
if (strncmp(gdbctx->in_packet, "Symbol::", gdbctx->in_packet_len) == 0)
return packet_ok;
if (strncmp(gdbctx->in_packet, "Supported", 9) == 0)
{
if (strlen(target_xml))
return packet_reply(gdbctx, "PacketSize=400;qXfer:features:read+");
else
{
/* no features supported */
packet_reply_open(gdbctx);
packet_reply_close(gdbctx);
return packet_done;
}
}
break;
case 'T':
if (gdbctx->in_packet_len > 15 &&
strncmp(gdbctx->in_packet, "ThreadExtraInfo", 15) == 0 &&
gdbctx->in_packet[15] == ',')
{
unsigned tid;
char* end;
char result[128];
tid = strtol(gdbctx->in_packet + 16, &end, 16);
if (end == NULL) break;
get_thread_info(gdbctx, tid, result, sizeof(result));
packet_reply_open(gdbctx);
packet_reply_hex_to_str(gdbctx, result);
packet_reply_close(gdbctx);
return packet_done;
}
if (strncmp(gdbctx->in_packet, "TStatus", 7) == 0)
{
/* Tracepoints not supported */
packet_reply_open(gdbctx);
packet_reply_close(gdbctx);
return packet_done;
}
break;
case 'X':
if (strlen(target_xml) && strncmp(gdbctx->in_packet, "Xfer:features:read:target.xml", 29) == 0)
return packet_reply(gdbctx, target_xml);
break;
}
ERR("Unhandled query %s\n", debugstr_an(gdbctx->in_packet, gdbctx->in_packet_len));
return packet_error;
}
static enum packet_return packet_step(struct gdb_context* gdbctx)
{
/* FIXME: add support for address in packet */
assert(gdbctx->in_packet_len == 0);
if (dbg_curr_thread != gdbctx->exec_thread && gdbctx->exec_thread)
FIXME("Can't single-step thread %04x while on thread %04x\n",
gdbctx->exec_thread->tid, dbg_curr_thread->tid);
gdbctx->process->be_cpu->single_step(&gdbctx->context, TRUE);
resume_debuggee(gdbctx, DBG_CONTINUE);
wait_for_debuggee(gdbctx);
gdbctx->process->be_cpu->single_step(&gdbctx->context, FALSE);
return packet_reply_status(gdbctx);
}
#if 0
static enum packet_return packet_step_signal(struct gdb_context* gdbctx)
{
unsigned char sig;
/* FIXME: add support for address in packet */
assert(gdbctx->in_packet_len == 2);
if (dbg_curr_thread->tid != gdbctx->exec_thread && gdbctx->exec_thread)
if (gdbctx->trace & GDBPXY_TRC_COMMAND_ERROR)
fprintf(stderr, "NIY: step/sig on %u, while last thread is %u\n",
gdbctx->exec_thread, DEBUG_CurrThread->tid);
hex_from(&sig, gdbctx->in_packet, 1);
/* cannot change signals on the fly */
if (gdbctx->trace & GDBPXY_TRC_COMMAND)
fprintf(stderr, "sigs: %u %u\n", sig, gdbctx->last_sig);
if (sig != gdbctx->last_sig)
return packet_error;
resume_debuggee(gdbctx, DBG_EXCEPTION_NOT_HANDLED);
wait_for_debuggee(gdbctx);
return packet_reply_status(gdbctx);
}
#endif
static enum packet_return packet_thread_alive(struct gdb_context* gdbctx)
{
char* end;
unsigned tid;
tid = strtol(gdbctx->in_packet, &end, 16);
if (tid == -1 || tid == 0)
return packet_reply_error(gdbctx, EINVAL);
if (dbg_get_thread(gdbctx->process, tid) != NULL)
return packet_ok;
return packet_reply_error(gdbctx, ESRCH);
}
/* =============================================== *
* P A C K E T I N F R A S T R U C T U R E *
* =============================================== *
*/
struct packet_entry
{
char key;
enum packet_return (*handler)(struct gdb_context* gdbctx);
};
static struct packet_entry packet_entries[] =
{
/*{'!', packet_extended}, */
{'?', packet_last_signal},
{'c', packet_continue},
{'C', packet_continue_signal},
{'D', packet_detach},
{'g', packet_read_registers},
{'G', packet_write_registers},
{'k', packet_kill},
{'H', packet_thread},
{'m', packet_read_memory},
{'M', packet_write_memory},
{'p', packet_read_register},
{'P', packet_write_register},
{'q', packet_query},
/* {'Q', packet_set}, */
/* {'R', packet,restart}, only in extended mode ! */
{'s', packet_step},
/*{'S', packet_step_signal}, hard(er) to implement */
{'T', packet_thread_alive},
{'v', packet_verbose},
};
static BOOL extract_packets(struct gdb_context* gdbctx)
{
char* end;
int plen;
unsigned char in_cksum, loc_cksum;
char* ptr;
enum packet_return ret = packet_error;
int num_packet = 0;
while ((ret & packet_last_f) == 0)
{
TRACE("Packet: %s\n", debugstr_an(gdbctx->in_buf, gdbctx->in_len));
ptr = memchr(gdbctx->in_buf, '$', gdbctx->in_len);
if (ptr == NULL) return FALSE;
if (ptr != gdbctx->in_buf)
{
int glen = ptr - gdbctx->in_buf; /* garbage len */
WARN("Removing garbage: %s\n", debugstr_an(gdbctx->in_buf, glen));
gdbctx->in_len -= glen;
memmove(gdbctx->in_buf, ptr, gdbctx->in_len);
}
end = memchr(gdbctx->in_buf + 1, '#', gdbctx->in_len);
if (end == NULL) return FALSE;
/* no checksum yet */
if (end + 3 > gdbctx->in_buf + gdbctx->in_len) return FALSE;
plen = end - gdbctx->in_buf - 1;
hex_from(&in_cksum, end + 1, 1);
loc_cksum = checksum(gdbctx->in_buf + 1, plen);
if (loc_cksum == in_cksum)
{
if (num_packet == 0) {
int i;
ret = packet_error;
write(gdbctx->sock, "+", 1);
assert(plen);
/* FIXME: should use bsearch if packet_entries was sorted */
for (i = 0; i < ARRAY_SIZE(packet_entries); i++)
{
if (packet_entries[i].key == gdbctx->in_buf[1]) break;
}
if (i == ARRAY_SIZE(packet_entries))
WARN("Unhandled packet %s\n", debugstr_an(&gdbctx->in_buf[1], plen));
else
{
gdbctx->in_packet = gdbctx->in_buf + 2;
gdbctx->in_packet_len = plen - 1;
ret = (packet_entries[i].handler)(gdbctx);
}
switch (ret & ~packet_last_f)
{
case packet_error: packet_reply(gdbctx, ""); break;
case packet_ok: packet_reply(gdbctx, "OK"); break;
case packet_done: break;
}
TRACE("Reply: %s\n", debugstr_an(gdbctx->out_buf, gdbctx->out_len));
i = write(gdbctx->sock, gdbctx->out_buf, gdbctx->out_len);
assert(i == gdbctx->out_len);
/* if this fails, we'll have to use POLLOUT...
*/
gdbctx->out_len = 0;
num_packet++;
}
else
{
/* FIXME: If we have more than one packet in our input buffer,
* it's very likely that we took too long to answer to a given packet
* and gdb is sending us the same packet again.
* So we simply drop the second packet. This will lower the risk of error,
* but there are still some race conditions here.
* A better fix (yet not perfect) would be to have two threads:
* - one managing the packets for gdb
* - the second one managing the commands...
* This would allow us to send the reply with the '+' character (Ack of
* the command) way sooner than we do now.
*/
ERR("Dropping packet; I was too slow to respond\n");
}
}
else
{
write(gdbctx->sock, "+", 1);
ERR("Dropping packet; invalid checksum %d <> %d\n", in_cksum, loc_cksum);
}
gdbctx->in_len -= plen + 4;
memmove(gdbctx->in_buf, end + 3, gdbctx->in_len);
}
return TRUE;
}
static int fetch_data(struct gdb_context* gdbctx)
{
int len, in_len = gdbctx->in_len;
assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
for (;;)
{
#define STEP 128
if (gdbctx->in_len + STEP > gdbctx->in_buf_alloc)
gdbctx->in_buf = packet_realloc(gdbctx->in_buf, gdbctx->in_buf_alloc += STEP);
#undef STEP
len = read(gdbctx->sock, gdbctx->in_buf + gdbctx->in_len, gdbctx->in_buf_alloc - gdbctx->in_len);
if (len <= 0) break;
gdbctx->in_len += len;
assert(gdbctx->in_len <= gdbctx->in_buf_alloc);
if (len < gdbctx->in_buf_alloc - gdbctx->in_len) break;
}
return gdbctx->in_len - in_len;
}
#define FLAG_NO_START 1
#define FLAG_WITH_XTERM 2
static BOOL gdb_exec(const char* wine_path, unsigned port, unsigned flags)
{
char buf[MAX_PATH];
int fd;
const char *gdb_path, *tmp_path;
FILE* f;
if (!(gdb_path = getenv("WINE_GDB"))) gdb_path = "gdb";
if (!(tmp_path = getenv("TMPDIR"))) tmp_path = "/tmp";
strcpy(buf, tmp_path);
strcat(buf, "/winegdb.XXXXXX");
fd = mkstemps(buf, 0);
if (fd == -1) return FALSE;
if ((f = fdopen(fd, "w+")) == NULL) return FALSE;
fprintf(f, "file \"%s\"\n", wine_path);
fprintf(f, "target remote localhost:%d\n", ntohs(port));
fprintf(f, "set prompt Wine-gdb>\\ \n");
/* gdb 5.1 seems to require it, won't hurt anyway */
fprintf(f, "sharedlibrary\n");
/* This is needed (but not a decent & final fix)
* Without this, gdb would skip our inter-DLL relay code (because
* we don't have any line number information for the relay code)
* With this, we will stop on first instruction of the stub, and
* reusing step, will get us through the relay stub at the actual
* function we're looking at.
*/
fprintf(f, "set step-mode on\n");
/* tell gdb to delete this file when done handling it... */
fprintf(f, "shell rm -f \"%s\"\n", buf);
fclose(f);
if (flags & FLAG_WITH_XTERM)
execlp("xterm", "xterm", "-e", gdb_path, "-x", buf, NULL);
else
execlp(gdb_path, gdb_path, "-x", buf, NULL);
assert(0); /* never reached */
return TRUE;
}
static BOOL gdb_startup(struct gdb_context* gdbctx, DEBUG_EVENT* de, unsigned flags, unsigned port)
{
int sock;
struct sockaddr_in s_addrs = {0};
socklen_t s_len = sizeof(s_addrs);
struct pollfd pollfd;
IMAGEHLP_MODULE64 imh_mod;
BOOL ret = FALSE;
/* step 1: create socket for gdb connection request */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
ERR("Failed to create socket: %s\n", strerror(errno));
return FALSE;
}
s_addrs.sin_family = AF_INET;
s_addrs.sin_addr.s_addr = INADDR_ANY;
s_addrs.sin_port = htons(port);
if (bind(sock, (struct sockaddr *)&s_addrs, sizeof(s_addrs)) == -1)
goto cleanup;
if (listen(sock, 1) == -1 || getsockname(sock, (struct sockaddr*)&s_addrs, &s_len) == -1)
goto cleanup;
/* step 2: do the process internal creation */
handle_debug_event(gdbctx, de);
/* step3: get the wine loader name */
if (!dbg_get_debuggee_info(gdbctx->process->handle, &imh_mod))
goto cleanup;
/* step 4: fire up gdb (if requested) */
if (flags & FLAG_NO_START)
fprintf(stderr, "target remote localhost:%d\n", ntohs(s_addrs.sin_port));
else
switch (fork())
{
case -1: /* error in parent... */
ERR("Failed to start gdb: fork: %s\n", strerror(errno));
goto cleanup;
default: /* in parent... success */
signal(SIGINT, SIG_IGN);
break;
case 0: /* in child... and alive */
gdb_exec(imh_mod.LoadedImageName, s_addrs.sin_port, flags);
/* if we're here, exec failed, so report failure */
goto cleanup;
}
/* step 5: wait for gdb to connect actually */
pollfd.fd = sock;
pollfd.events = POLLIN;
pollfd.revents = 0;
switch (poll(&pollfd, 1, -1))
{
case 1:
if (pollfd.revents & POLLIN)
{
int dummy = 1;
gdbctx->sock = accept(sock, (struct sockaddr*)&s_addrs, &s_len);
if (gdbctx->sock == -1)
break;
ret = TRUE;
TRACE("connected on %d\n", gdbctx->sock);
/* don't keep our small packets too long: send them ASAP back to GDB
* without this, GDB really crawls
*/
setsockopt(gdbctx->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&dummy, sizeof(dummy));
}
break;
case 0:
ERR("Timed out connecting to gdb\n");
break;
case -1:
ERR("Failed to connect to gdb: poll: %s\n", strerror(errno));
break;
default:
assert(0);
}
cleanup:
close(sock);
return ret;
}
static BOOL gdb_init_context(struct gdb_context* gdbctx, unsigned flags, unsigned port)
{
DEBUG_EVENT de;
int i;
gdbctx->sock = -1;
gdbctx->in_buf = NULL;
gdbctx->in_buf_alloc = 0;
gdbctx->in_len = 0;
gdbctx->out_buf = NULL;
gdbctx->out_buf_alloc = 0;
gdbctx->out_len = 0;
gdbctx->out_curr_packet = -1;
gdbctx->exec_thread = gdbctx->other_thread = NULL;
gdbctx->last_sig = 0;
gdbctx->in_trap = FALSE;
gdbctx->process = NULL;
for (i = 0; i < ARRAY_SIZE(gdbctx->wine_segs); i++)
gdbctx->wine_segs[i] = 0;
/* wait for first trap */
while (WaitForDebugEvent(&de, INFINITE))
{
if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
{
/* this should be the first event we get,
* and the only one of this type */
assert(gdbctx->process == NULL && de.dwProcessId == dbg_curr_pid);
/* gdbctx->dwProcessId = pid; */
if (!gdb_startup(gdbctx, &de, flags, port)) return FALSE;
assert(!gdbctx->in_trap);
}
else
{
handle_debug_event(gdbctx, &de);
if (gdbctx->in_trap) break;
}
ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
}
return TRUE;
}
static int gdb_remote(unsigned flags, unsigned port)
{
struct pollfd pollfd;
struct gdb_context gdbctx;
BOOL doLoop;
for (doLoop = gdb_init_context(&gdbctx, flags, port); doLoop;)
{
pollfd.fd = gdbctx.sock;
pollfd.events = POLLIN;
pollfd.revents = 0;
switch (poll(&pollfd, 1, -1))
{
case 1:
/* got something */
if (pollfd.revents & (POLLHUP | POLLERR))
{
ERR("gdb hung up\n");
/* kill also debuggee process - questionnable - */
detach_debuggee(&gdbctx, TRUE);
doLoop = FALSE;
break;
}
if ((pollfd.revents & POLLIN) && fetch_data(&gdbctx) > 0)
{
if (extract_packets(&gdbctx)) doLoop = FALSE;
}
break;
case 0:
/* timeout, should never happen (infinite timeout) */
break;
case -1:
ERR("poll failed: %s\n", strerror(errno));
doLoop = FALSE;
break;
}
}
wait(NULL);
return 0;
}
#endif
int gdb_main(int argc, char* argv[])
{
#ifdef HAVE_POLL
unsigned gdb_flags = 0, port = 0;
char *port_end;
argc--; argv++;
while (argc > 0 && argv[0][0] == '-')
{
if (strcmp(argv[0], "--no-start") == 0)
{
gdb_flags |= FLAG_NO_START;
argc--; argv++;
continue;
}
if (strcmp(argv[0], "--with-xterm") == 0)
{
gdb_flags |= FLAG_WITH_XTERM;
argc--; argv++;
continue;
}
if (strcmp(argv[0], "--port") == 0 && argc > 1)
{
port = strtoul(argv[1], &port_end, 10);
if (*port_end)
{
fprintf(stderr, "Invalid port: %s\n", argv[1]);
return -1;
}
argc -= 2; argv += 2;
continue;
}
return -1;
}
if (dbg_active_attach(argc, argv) == start_ok ||
dbg_active_launch(argc, argv) == start_ok)
return gdb_remote(gdb_flags, port);
#else
fprintf(stderr, "GdbProxy mode not supported on this platform\n");
#endif
return -1;
}
|