aboutsummaryrefslogtreecommitdiff
blob: cd9ab996eb7cd89ec030a754c44b6a5486d6bf34 (plain)
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
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
/* Multi-threaded debugging support for the thread_db interface,
   used on operating systems such as Solaris and Linux.
   Copyright 1999 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

/* This module implements a thread_stratum target that sits on top of
   a normal process_stratum target (such as procfs or ptrace).  The
   process_stratum target must install this thread_stratum target when
   it detects the presence of the thread_db shared library.

   This module will then use the thread_db API to add thread-awareness
   to the functionality provided by the process_stratum target (or in
   some cases, to add user-level thread awareness on top of the 
   kernel-level thread awareness that is already provided by the 
   process_stratum target).

   Solaris threads (for instance) are a multi-level thread implementation;
   the kernel provides a Light Weight Process (LWP) which the procfs 
   process_stratum module is aware of.  This module must then mediate
   the relationship between kernel LWP threads and user (eg. posix)
   threads.

   Linux threads are likely to be different -- but the thread_db 
   library API should make the difference largely transparent to GDB.

   */

/* The thread_db API provides a number of functions that give the caller
   access to the inner workings of the child process's thread library. 
   We will be using the following (others may be added):

   td_thr_validate		Confirm valid "live" thread
   td_thr_get_info		Get info about a thread
   td_thr_getgregs		Get thread's general registers
   td_thr_getfpregs		Get thread's floating point registers
   td_thr_setgregs		Set thread's general registers
   td_thr_setfpregs		Set thread's floating point registers
   td_ta_map_id2thr		Get thread handle from thread id
   td_ta_map_lwp2thr		Get thread handle from LWP id
   td_ta_thr_iter		Iterate over all threads (with callback)

   In return, the debugger has to provide certain services to the 
   thread_db library.  Some of these aren't actually required to do
   anything in practice.  For instance, the thread_db expects to be
   able to stop the child process and start it again: but in our
   context, the child process will always be stopped already when we
   invoke the thread_db library, so the functions that we provide for
   the library to stop and start the child process are no-ops.

   Here is the list of functions which we export to the thread_db
   library, divided into no-op functions vs. functions that actually
   have to do something:

   No-op functions:

   ps_pstop			Stop the child process
   ps_pcontinue			Continue the child process
   ps_lstop			Stop a specific LWP (kernel thread)
   ps_lcontinue			Continue an LWP
   ps_lgetxregsize		Get size of LWP's xregs (sparc)
   ps_lgetxregs			Get LWP's xregs (sparc)
   ps_lsetxregs			Set LWP's xregs (sparc)

   Functions that have to do useful work:

   ps_pglobal_lookup		Get the address of a global symbol
   ps_pdread			Read memory, data segment
   ps_ptread			Read memory, text segment
   ps_pdwrite			Write memory, data segment
   ps_ptwrite			Write memory, text segment
   ps_lgetregs			Get LWP's general registers
   ps_lgetfpregs		Get LWP's floating point registers
   ps_lsetregs			Set LWP's general registers
   ps_lsetfpregs		Set LWP's floating point registers
   ps_lgetLDT			Get LWP's Local Descriptor Table (x86)
   
   Thus, if we ask the thread_db library to give us the general registers
   for user thread X, thread_db may figure out that user thread X is 
   actually mapped onto kernel thread Y.  Thread_db does not know how
   to obtain the registers for kernel thread Y, but GDB does, so thread_db
   turns the request right back to us via the ps_lgetregs callback.  */

#include "defs.h"
#include "gdbthread.h"
#include "target.h"
#include "inferior.h"
#include "gdbcmd.h"

#include "gdb_wait.h"

#include <time.h>

#if defined(USE_PROC_FS) || defined(HAVE_GREGSET_T)
#include <sys/procfs.h>
#endif

#if defined (HAVE_PROC_SERVICE_H)
#include <proc_service.h>	/* defines incoming API (ps_* callbacks) */
#else
#include "gdb_proc_service.h"
#endif

#if defined HAVE_STDINT_H	/* Pre-5.2 systems don't have this header */
#if defined (HAVE_THREAD_DB_H)
#include <thread_db.h>		/* defines outgoing API (td_thr_* calls) */
#else
#include "gdb_thread_db.h"
#endif

#include <dlfcn.h>		/* dynamic library interface */

/* Prototypes for supply_gregset etc. */
#include "gregset.h"

#ifndef TIDGET
#define TIDGET(PID)		(((PID) & 0x7fffffff) >> 16)
#define PIDGET(PID)		(((PID) & 0xffff))
#define MERGEPID(PID, TID)	(((PID) & 0xffff) | ((TID) << 16))
#endif

/* Macros for superimposing PID and TID into inferior_pid.  */
#define THREAD_FLAG		0x80000000
#define is_thread(ARG)		(((ARG) & THREAD_FLAG) != 0)
#define is_lwp(ARG)		(((ARG) & THREAD_FLAG) == 0)
#define GET_LWP(PID)		TIDGET (PID)
#define GET_THREAD(PID)		TIDGET (PID)
#define BUILD_LWP(TID, PID)	MERGEPID (PID, TID)
#define BUILD_THREAD(TID, PID)	(MERGEPID (PID, TID) | THREAD_FLAG)

/*
 * target_beneath is a pointer to the target_ops underlying this one.
 */

static struct target_ops *target_beneath;


/*
 * target vector defined in this module:
 */

static struct target_ops thread_db_ops;

/*
 * Typedefs required to resolve differences between the thread_db
 * and proc_service API defined on different versions of Solaris:
 */

#if defined(PROC_SERVICE_IS_OLD)
typedef const struct ps_prochandle *gdb_ps_prochandle_t;
typedef char *gdb_ps_read_buf_t;
typedef char *gdb_ps_write_buf_t;
typedef int gdb_ps_size_t;
#else
typedef struct ps_prochandle *gdb_ps_prochandle_t;
typedef void *gdb_ps_read_buf_t;
typedef const void *gdb_ps_write_buf_t;
typedef size_t gdb_ps_size_t;
#endif

/* Unfortunately glibc 2.1.3 was released with a broken prfpregset_t
   type.  We let configure check for this lossage, and make
   appropriate typedefs here.  */

#ifdef PRFPREGSET_T_BROKEN
typedef elf_fpregset_t gdb_prfpregset_t;
#else
typedef prfpregset_t gdb_prfpregset_t;
#endif

/* 
 * proc_service callback functions, called by thread_db.
 */

ps_err_e
ps_pstop (gdb_ps_prochandle_t ph)		/* Process stop */
{
  return PS_OK;
}

ps_err_e
ps_pcontinue (gdb_ps_prochandle_t ph)		/* Process continue */
{
  return PS_OK;
}

ps_err_e
ps_lstop (gdb_ps_prochandle_t ph,		/* LWP stop */
	  lwpid_t lwpid)
{
  return PS_OK;
}

ps_err_e
ps_lcontinue (gdb_ps_prochandle_t ph,		/* LWP continue */
	      lwpid_t lwpid)
{
  return PS_OK;
}

ps_err_e
ps_lgetxregsize (gdb_ps_prochandle_t ph,	/* Get XREG size */
		 lwpid_t lwpid,
		 int *xregsize)
{
  return PS_OK;
}

ps_err_e
ps_lgetxregs (gdb_ps_prochandle_t ph,		/* Get XREGS */
	      lwpid_t lwpid,
	      caddr_t xregset)
{
  return PS_OK;
}

ps_err_e
ps_lsetxregs (gdb_ps_prochandle_t ph,		/* Set XREGS */
	      lwpid_t lwpid,
	      caddr_t xregset)
{
  return PS_OK;
}

void
ps_plog (const char *fmt, ...)
{
  va_list args;

  va_start (args, fmt);
  vfprintf_filtered (gdb_stderr, fmt, args);
}

/* Look up a symbol in GDB's global symbol table.
   Return the symbol's address.
   FIXME: it would be more correct to look up the symbol in the context 
   of the LD_OBJECT_NAME provided.  However we're probably fairly safe 
   as long as there aren't name conflicts with other libraries.  */

ps_err_e
ps_pglobal_lookup (gdb_ps_prochandle_t ph,
		   const char *ld_object_name,	/* the library name */
		   const char *ld_symbol_name,	/* the symbol name */
		   paddr_t    *ld_symbol_addr)	/* return the symbol addr */
{
  struct minimal_symbol *ms;

  ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL);

  if (!ms)
    return PS_NOSYM;

  *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms);

  return PS_OK;
}

/* Worker function for all memory reads and writes: */
static ps_err_e rw_common (const struct ps_prochandle *ph, 
			   paddr_t addr,
			   char *buf, 
			   int size, 
			   int write_p);

/* target_xfer_memory direction consts */
enum {PS_READ = 0, PS_WRITE = 1};

ps_err_e
ps_pdread (gdb_ps_prochandle_t ph, 	/* read from data segment */
	   paddr_t             addr,
	   gdb_ps_read_buf_t   buf,
	   gdb_ps_size_t       size)
{
  return rw_common (ph, addr, buf, size, PS_READ);
}

ps_err_e
ps_pdwrite (gdb_ps_prochandle_t ph,	/* write to data segment */
	    paddr_t             addr,
	    gdb_ps_write_buf_t  buf,
	    gdb_ps_size_t       size)
{
  return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
}

ps_err_e
ps_ptread (gdb_ps_prochandle_t ph,	/* read from text segment */
	   paddr_t             addr,
	   gdb_ps_read_buf_t   buf,
	   gdb_ps_size_t       size)
{
  return rw_common (ph, addr, buf, size, PS_READ);
}

ps_err_e
ps_ptwrite (gdb_ps_prochandle_t ph,	/* write to text segment */
	    paddr_t             addr,
	    gdb_ps_write_buf_t  buf,
	    gdb_ps_size_t       size)
{
  return rw_common (ph, addr, (char *) buf, size, PS_WRITE);
}

static struct cleanup *save_inferior_pid    (void);
static void            restore_inferior_pid (void *saved_pid);
static char *thr_err_string   (td_err_e);
static char *thr_state_string (td_thr_state_e);

struct ps_prochandle {
  int pid;
};

struct ps_prochandle main_prochandle;
td_thragent_t *      main_threadagent;

/* 
 * Common proc_service routine for reading and writing memory.  
 */

/* FIXME: once we've munged the inferior_pid, why can't we
   simply call target_read/write_memory and return?  */


static ps_err_e
rw_common (const struct ps_prochandle *ph,
	   paddr_t addr,
	   char   *buf,
	   int     size,
	   int     write_p)
{
  struct cleanup *old_chain = save_inferior_pid ();
  int to_do = size;
  int done  = 0;

  inferior_pid = main_prochandle.pid;

  while (to_do > 0)
    {
      done = current_target.to_xfer_memory (addr, buf, size, write_p, 
					    &current_target);
      if (done <= 0)
	{
	  if (write_p == PS_READ)
	    print_sys_errmsg ("rw_common (): read", errno);
	  else
	    print_sys_errmsg ("rw_common (): write", errno);

	  return PS_ERR;
	}
      to_do -= done;
      buf   += done;
    }
  do_cleanups (old_chain);
  return PS_OK;
}

/* Cleanup functions used by the register callbacks
   (which have to manipulate the global inferior_pid).  */

ps_err_e
ps_lgetregs (gdb_ps_prochandle_t ph,		/* Get LWP general regs */
	     lwpid_t     lwpid,
	     prgregset_t gregset)
{
  struct cleanup *old_chain = save_inferior_pid ();

  inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
  current_target.to_fetch_registers (-1);

  fill_gregset (gregset, -1);
  do_cleanups (old_chain);

  return PS_OK;
}

ps_err_e
ps_lsetregs (gdb_ps_prochandle_t ph,		/* Set LWP general regs */
	     lwpid_t           lwpid,
	     const prgregset_t gregset)
{
  struct cleanup *old_chain = save_inferior_pid ();

  inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
  supply_gregset (gregset);
  current_target.to_store_registers (-1);
  do_cleanups (old_chain);
  return PS_OK;
}

ps_err_e
ps_lgetfpregs (gdb_ps_prochandle_t ph,		/* Get LWP float regs */
	       lwpid_t       lwpid,
	       gdb_prfpregset_t *fpregset)
{
  struct cleanup *old_chain = save_inferior_pid ();

  inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
  current_target.to_fetch_registers (-1);
  fill_fpregset (fpregset, -1);
  do_cleanups (old_chain);
  return PS_OK;
}

ps_err_e
ps_lsetfpregs (gdb_ps_prochandle_t ph,		/* Set LWP float regs */
	       lwpid_t             lwpid,
	       const gdb_prfpregset_t *fpregset)
{
  struct cleanup *old_chain = save_inferior_pid ();

  inferior_pid = BUILD_LWP (lwpid, main_prochandle.pid);
  supply_fpregset (fpregset);
  current_target.to_store_registers (-1);
  do_cleanups (old_chain);
  return PS_OK;
}

/*
 * ps_getpid
 *
 * return the main pid for the child process
 * (special for Linux -- not used on Solaris)
 */

pid_t
ps_getpid (gdb_ps_prochandle_t ph)
{
  return ph->pid;
}

#ifdef TM_I386SOL2_H

/* Reads the local descriptor table of a LWP.  */

ps_err_e
ps_lgetLDT (gdb_ps_prochandle_t ph, lwpid_t lwpid,
	    struct ssd *pldt)
{
  /* NOTE: only used on Solaris, therefore OK to refer to procfs.c */
  extern struct ssd *procfs_find_LDT_entry (int);
  struct ssd *ret;

  ret = procfs_find_LDT_entry (BUILD_LWP (lwpid, 
					  PIDGET (main_prochandle.pid)));
  if (ret)
    {
      memcpy (pldt, ret, sizeof (struct ssd));
      return PS_OK;
    }
  else	/* LDT not found. */
    return PS_ERR;
}
#endif /* TM_I386SOL2_H */

/*
 * Pointers to thread_db functions:
 *
 * These are a dynamic library mechanism.
 * The dlfcn.h interface will be used to initialize these 
 * so that they point to the appropriate functions in the
 * thread_db dynamic library.  This is done dynamically 
 * so that GDB can still run on systems that lack thread_db.  
 */

static td_err_e (*p_td_init)              (void);

static td_err_e (*p_td_ta_new)            (const struct ps_prochandle *ph_p, 
					   td_thragent_t **ta_pp);

static td_err_e (*p_td_ta_delete)         (td_thragent_t *ta_p);

static td_err_e (*p_td_ta_get_nthreads)   (const td_thragent_t *ta_p,
					   int *nthread_p);


static td_err_e (*p_td_ta_thr_iter)       (const td_thragent_t *ta_p,
					   td_thr_iter_f *cb,
					   void *cbdata_p,
					   td_thr_state_e state,
					   int ti_pri, 
					   sigset_t *ti_sigmask_p,
					   unsigned ti_user_flags);

static td_err_e (*p_td_ta_event_addr)     (const td_thragent_t *ta_p,
					   u_long event,
					   td_notify_t *notify_p);

static td_err_e (*p_td_ta_event_getmsg)   (const td_thragent_t *ta_p,
					   td_event_msg_t *msg);

static td_err_e (*p_td_ta_set_event)      (const td_thragent_t *ta_p,
					   td_thr_events_t *events);

static td_err_e (*p_td_thr_validate)      (const td_thrhandle_t *th_p);

static td_err_e (*p_td_thr_event_enable)  (const td_thrhandle_t *th_p,
					   int on_off);

static td_err_e (*p_td_thr_get_info)      (const td_thrhandle_t *th_p,
					   td_thrinfo_t *ti_p);

static td_err_e (*p_td_thr_getgregs)      (const td_thrhandle_t *th_p,
					   prgregset_t regset);

static td_err_e (*p_td_thr_setgregs)      (const td_thrhandle_t *th_p,
					   const prgregset_t regset);

static td_err_e (*p_td_thr_getfpregs)     (const td_thrhandle_t *th_p,
					   gdb_prfpregset_t *fpregset);

static td_err_e (*p_td_thr_setfpregs)     (const td_thrhandle_t *th_p,
					   const gdb_prfpregset_t *fpregset);

static td_err_e (*p_td_ta_map_id2thr)     (const td_thragent_t *ta_p,
					   thread_t tid,
					   td_thrhandle_t *th_p);

static td_err_e (*p_td_ta_map_lwp2thr)    (const td_thragent_t *ta_p,
					   lwpid_t lwpid,
					   td_thrhandle_t *th_p);

/*
 * API and target vector initialization function: thread_db_initialize.
 *
 * NOTE: this function is deliberately NOT named with the GDB convention
 * of module initializer function names that begin with "_initialize".
 * This module is NOT intended to be auto-initialized at GDB startup.
 * Rather, it will only be initialized when a multi-threaded child
 * process is detected.
 *
 */

/* 
 * Initializer for thread_db library interface.
 * This function does the dynamic library stuff (dlopen, dlsym), 
 * and then calls the thread_db library's one-time initializer 
 * function (td_init).  If everything succeeds, this function
 * returns true; otherwise it returns false, and this module
 * cannot be used.
 */

static int
init_thread_db_library ()
{
  void *dlhandle;
  td_err_e ret;

  /* Open a handle to the "thread_db" dynamic library.  */
  if ((dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW)) == NULL)
    return 0;			/* fail */

  /* Initialize pointers to the dynamic library functions we will use.
   * Note that we are not calling the functions here -- we are only
   * establishing pointers to them.
   */

  /* td_init: initialize thread_db library. */
  if ((p_td_init = dlsym (dlhandle, "td_init")) == NULL)
    return 0;			/* fail */
  /* td_ta_new: register a target process with thread_db.  */
  if ((p_td_ta_new = dlsym (dlhandle, "td_ta_new")) == NULL)
    return 0;			/* fail */
  /* td_ta_delete: un-register a target process with thread_db.  */
  if ((p_td_ta_delete = dlsym (dlhandle, "td_ta_delete")) == NULL)
    return 0;			/* fail */

  /* td_ta_map_id2thr: get thread handle from thread id.  */
  if ((p_td_ta_map_id2thr = dlsym (dlhandle, "td_ta_map_id2thr")) == NULL)
    return 0;			/* fail */
  /* td_ta_map_lwp2thr: get thread handle from lwp id.  */
  if ((p_td_ta_map_lwp2thr = dlsym (dlhandle, "td_ta_map_lwp2thr")) == NULL)
    return 0;			/* fail */
  /* td_ta_get_nthreads: get number of threads in target process.  */
  if ((p_td_ta_get_nthreads = dlsym (dlhandle, "td_ta_get_nthreads")) == NULL)
    return 0;			/* fail */
  /* td_ta_thr_iter: iterate over all thread handles.  */
  if ((p_td_ta_thr_iter = dlsym (dlhandle, "td_ta_thr_iter")) == NULL)
    return 0;			/* fail */

  /* td_thr_validate: make sure a thread handle is real and alive.  */
  if ((p_td_thr_validate = dlsym (dlhandle, "td_thr_validate")) == NULL)
    return 0;			/* fail */
  /* td_thr_get_info: get a bunch of info about a thread.  */
  if ((p_td_thr_get_info = dlsym (dlhandle, "td_thr_get_info")) == NULL)
    return 0;			/* fail */
  /* td_thr_getgregs: get general registers for thread.  */
  if ((p_td_thr_getgregs = dlsym (dlhandle, "td_thr_getgregs")) == NULL)
    return 0;			/* fail */
  /* td_thr_setgregs: set general registers for thread.  */
  if ((p_td_thr_setgregs = dlsym (dlhandle, "td_thr_setgregs")) == NULL)
    return 0;			/* fail */
  /* td_thr_getfpregs: get floating point registers for thread.  */
  if ((p_td_thr_getfpregs = dlsym (dlhandle, "td_thr_getfpregs")) == NULL)
    return 0;			/* fail */
  /* td_thr_setfpregs: set floating point registers for thread.  */
  if ((p_td_thr_setfpregs = dlsym (dlhandle, "td_thr_setfpregs")) == NULL)
    return 0;			/* fail */
  
  ret = p_td_init ();
  if (ret != TD_OK)
    {
      warning ("init_thread_db: td_init: %s", thr_err_string (ret));
      return 0;
    }

  /* Optional functions:
     We can still debug even if the following functions are not found.  */

  /* td_ta_event_addr: get the breakpoint address for specified event.  */
  p_td_ta_event_addr = dlsym (dlhandle, "td_ta_event_addr");

  /* td_ta_event_getmsg: get the next event message for the process.  */
  p_td_ta_event_getmsg = dlsym (dlhandle, "td_ta_event_getmsg");

  /* td_ta_set_event: request notification of an event.  */
  p_td_ta_set_event = dlsym (dlhandle, "td_ta_set_event");

  /* td_thr_event_enable: enable event reporting in a thread.  */
  p_td_thr_event_enable = dlsym (dlhandle, "td_thr_event_enable");

  return 1;			/* success */
}

/*
 * Local utility functions:
 */


/*

   LOCAL FUNCTION

   save_inferior_pid - Save inferior_pid on the cleanup list
   restore_inferior_pid - Restore inferior_pid from the cleanup list

   SYNOPSIS

   struct cleanup *save_inferior_pid (void);
   void            restore_inferior_pid (void *saved_pid);

   DESCRIPTION

   These two functions act in unison to restore inferior_pid in
   case of an error.

   NOTES

   inferior_pid is a global variable that needs to be changed by many
   of these routines before calling functions in procfs.c.  In order
   to guarantee that inferior_pid gets restored (in case of errors),
   you need to call save_inferior_pid before changing it.  At the end
   of the function, you should invoke do_cleanups to restore it.

 */

static struct cleanup *
save_inferior_pid (void)
{
  int *saved_pid_ptr;
  
  saved_pid_ptr = xmalloc (sizeof (int));
  *saved_pid_ptr = inferior_pid;
  return make_cleanup (restore_inferior_pid, saved_pid_ptr);
}

static void
restore_inferior_pid (void *arg)
{
  int *saved_pid_ptr = arg;
  inferior_pid = *saved_pid_ptr;
  free (arg);
}

/*

   LOCAL FUNCTION

   thr_err_string - Convert a thread_db error code to a string

   SYNOPSIS

   char * thr_err_string (errcode)

   DESCRIPTION

   Return a string description of the thread_db errcode.  If errcode
   is unknown, then return an <unknown> message.

 */

static char *
thr_err_string (errcode)
     td_err_e errcode;
{
  static char buf[50];

  switch (errcode) {
  case TD_OK:		return "generic 'call succeeded'";
  case TD_ERR:		return "generic error";
  case TD_NOTHR:	return "no thread to satisfy query";
  case TD_NOSV:		return "no sync handle to satisfy query";
  case TD_NOLWP:	return "no lwp to satisfy query";
  case TD_BADPH:	return "invalid process handle";
  case TD_BADTH:	return "invalid thread handle";
  case TD_BADSH:	return "invalid synchronization handle";
  case TD_BADTA:	return "invalid thread agent";
  case TD_BADKEY:	return "invalid key";
  case TD_NOMSG:	return "no event message for getmsg";
  case TD_NOFPREGS:	return "FPU register set not available";
  case TD_NOLIBTHREAD:	return "application not linked with libthread";
  case TD_NOEVENT:	return "requested event is not supported";
  case TD_NOCAPAB:	return "capability not available";
  case TD_DBERR:	return "debugger service failed";
  case TD_NOAPLIC:	return "operation not applicable to";
  case TD_NOTSD:	return "no thread-specific data for this thread";
  case TD_MALLOC:	return "malloc failed";
  case TD_PARTIALREG:	return "only part of register set was written/read";
  case TD_NOXREGS:	return "X register set not available for this thread";
  default:
    sprintf (buf, "unknown thread_db error '%d'", errcode);
    return buf;
  }
}

/*

   LOCAL FUNCTION

   thr_state_string - Convert a thread_db state code to a string

   SYNOPSIS

   char *thr_state_string (statecode)

   DESCRIPTION

   Return the thread_db state string associated with statecode.  
   If statecode is unknown, then return an <unknown> message.

 */

static char *
thr_state_string (statecode)
     td_thr_state_e statecode;
{
  static char buf[50];

  switch (statecode) {
  case TD_THR_STOPPED:		return "stopped by debugger";
  case TD_THR_RUN:		return "runnable";
  case TD_THR_ACTIVE:		return "active";
  case TD_THR_ZOMBIE:		return "zombie";
  case TD_THR_SLEEP:		return "sleeping";
  case TD_THR_STOPPED_ASLEEP:	return "stopped by debugger AND blocked";
  default:
    sprintf (buf, "unknown thread_db state %d", statecode);
    return buf;
  }
}

/*
 * Local thread/event list.
 * This data structure will be used to hold a list of threads and 
 * pending/deliverable events.
 */

typedef struct THREADINFO {
  thread_t       tid;		/* thread ID */
  pid_t          lid;		/* process/lwp ID */
  td_thr_state_e state;		/* thread state (a la thread_db) */
  td_thr_type_e  type;		/* thread type (a la thread_db) */
  int            pending;	/* true if holding a pending event */
  int            status;	/* wait status of any interesting event */
} threadinfo;

threadinfo * threadlist;
int threadlist_max = 0;		/* current size of table */
int threadlist_top = 0;		/* number of threads now in table */
#define THREADLIST_ALLOC 100	/* chunk size by which to expand table */

static threadinfo *
insert_thread (tid, lid, state, type)
     int            tid;
     int            lid;
     td_thr_state_e state;
     td_thr_type_e  type;
{
  if (threadlist_top >= threadlist_max)
    {
      threadlist_max += THREADLIST_ALLOC;
      threadlist      = realloc (threadlist, 
				 threadlist_max * sizeof (threadinfo));
      if (threadlist == NULL)
	return NULL;
    }
  threadlist[threadlist_top].tid     = tid;
  threadlist[threadlist_top].lid     = lid;
  threadlist[threadlist_top].state   = state;
  threadlist[threadlist_top].type    = type;
  threadlist[threadlist_top].pending = 0;
  threadlist[threadlist_top].status  = 0;

  return &threadlist[threadlist_top++];
}

static void
empty_threadlist ()
{
  threadlist_top = 0;
}

static threadinfo *
next_pending_event ()
{
  int i;

  for (i = 0; i < threadlist_top; i++)
    if (threadlist[i].pending)
      return &threadlist[i];

  return NULL;
}

static void
threadlist_iter (func, data, state, type)
     int (*func) ();
     void *data;
     td_thr_state_e state;
     td_thr_type_e  type;
{
  int i;

  for (i = 0; i < threadlist_top; i++)
    if ((state == TD_THR_ANY_STATE || state == threadlist[i].state) &&
	(type  == TD_THR_ANY_TYPE  || type  == threadlist[i].type))
      if ((*func) (&threadlist[i], data) != 0)
	break;

  return;
}     

/*
 * Global state
 * 
 * Here we keep state information all collected in one place.
 */

/* This flag is set when we activate, so that we don't do it twice. 
   Defined in linux-thread.c and used for inter-target syncronization.  */
extern int using_thread_db;

/* The process id for which we've stopped.
 * This is only set when we actually stop all threads.
 * Otherwise it's zero.
 */
static int event_pid;

/*
 * The process id for a new thread to which we've just attached.
 * This process needs special handling at resume time.
 */
static int attach_pid;


/*
 * thread_db event handling:
 *
 * The mechanism for event notification via the thread_db API.
 * These events are implemented as breakpoints.  The thread_db
 * library gives us an address where we can set a breakpoint.
 * When the breakpoint is hit, it represents an event of interest
 * such as:
 *   Thread creation
 *   Thread death
 *   Thread reap
 */

/* Location of the thread creation event breakpoint.  The code at this
   location in the child process will be called by the pthread library
   whenever a new thread is created.  By setting a special breakpoint
   at this location, GDB can detect when a new thread is created.  We
   obtain this location via the td_ta_event_addr call.  */

static CORE_ADDR thread_creation_bkpt_address;

/* Location of the thread death event breakpoint.  The code at this
   location in the child process will be called by the pthread library
   whenever a thread is destroyed.  By setting a special breakpoint at
   this location, GDB can detect when a new thread is created.  We
   obtain this location via the td_ta_event_addr call.  */

static CORE_ADDR thread_death_bkpt_address;

/* This function handles the global parts of enabling thread events.
   The thread-specific enabling is handled per-thread elsewhere.  */

static void
enable_thread_event_reporting (ta)
     td_thragent_t *ta;
{
  td_thr_events_t events;
  td_notify_t     notify;
  CORE_ADDR       addr;

  if (p_td_ta_set_event     == NULL ||
      p_td_ta_event_addr    == NULL ||
      p_td_ta_event_getmsg  == NULL ||
      p_td_thr_event_enable == NULL)
    return;	/* can't do thread event reporting without these funcs */

  /* set process wide mask saying which events we are interested in */
  td_event_emptyset (&events);
  td_event_addset (&events, TD_CREATE);
  td_event_addset (&events, TD_DEATH);

  if (p_td_ta_set_event (ta, &events) != TD_OK)
    {
      warning ("unable to set global thread event mask");
      return;
    }

  /* Delete previous thread event breakpoints, if any.  */
  remove_thread_event_breakpoints ();

  /* create breakpoints -- thread creation and death */
  /* thread creation */
  /* get breakpoint location */
  if (p_td_ta_event_addr (ta, TD_CREATE, &notify) != TD_OK)
    {
      warning ("unable to get location for thread creation breakpoint");
      return;
    }

  /* Set up the breakpoint. */
  create_thread_event_breakpoint (notify.u.bptaddr);

  /* Save it's location. */
  thread_creation_bkpt_address = notify.u.bptaddr;

  /* thread death */
  /* get breakpoint location */
  if (p_td_ta_event_addr (ta, TD_DEATH, &notify) != TD_OK)
    {
      warning ("unable to get location for thread death breakpoint");
      return;
    }
  /* Set up the breakpoint. */
  create_thread_event_breakpoint (notify.u.bptaddr);

  /* Save it's location. */
  thread_death_bkpt_address = notify.u.bptaddr;
}

/* This function handles the global parts of disabling thread events.
   The thread-specific enabling is handled per-thread elsewhere.  */

static void
disable_thread_event_reporting (ta)
     td_thragent_t *ta;
{
  td_thr_events_t events;

  /* set process wide mask saying we aren't interested in any events */
  td_event_emptyset (&events);
  p_td_ta_set_event (main_threadagent, &events);

  /* Delete thread event breakpoints, if any.  */
  remove_thread_event_breakpoints ();
  thread_creation_bkpt_address = 0;
  thread_death_bkpt_address = 0;
}

/* check_for_thread_event
   
   if it's a thread event we recognize (currently
   we only recognize creation and destruction
   events), return 1; else return 0.  */


static int
check_for_thread_event (struct target_waitstatus *tws, int event_pid)
{
  /* FIXME: to be more efficient, we should keep a static 
     list of threads, and update it only here (with td_ta_thr_iter). */
}

static void
thread_db_push_target (void)
{
  /* Called ONLY from thread_db_new_objfile after td_ta_new call succeeds. */

  /* Push this target vector */
  push_target (&thread_db_ops);
  /* Find the underlying process-layer target for calling later.  */
  target_beneath = find_target_beneath (&thread_db_ops);
  using_thread_db = 1;
  /* Turn on thread_db event-reporting API.  */
  enable_thread_event_reporting (main_threadagent);
}

static void
thread_db_unpush_target (void)
{
  /* Must be called whenever we remove ourself from the target stack! */

  using_thread_db = 0;
  target_beneath = NULL;

  /* delete local list of threads */
  empty_threadlist ();
  /* Turn off the thread_db API.  */
  p_td_ta_delete (main_threadagent);
  /* Unpush this target vector */
  unpush_target (&thread_db_ops);
  /* Reset linuxthreads module.  */
  linuxthreads_discard_global_state ();
}

/*
 * New objfile hook function:
 * Called for each new objfile (image, shared lib) in the target process.
 *
 * The purpose of this function is to detect that the target process
 * is linked with the (appropriate) thread library.  So every time a
 * new target shared library is detected, we will call td_ta_new.
 * If it succeeds, we know we have a multi-threaded target process
 * that we can debug using the thread_db API.
 */

/* 
 * new_objfile function:
 *
 * connected to target_new_objfile_hook, this function gets called
 * every time a new binary image is loaded.
 *
 * At each call, we attempt to open the thread_db connection to the
 * child process.  If it succeeds, we know we have a libthread process
 * and we can debug it with this target vector.  Therefore we push
 * ourself onto the target stack.
 */

static void (*target_new_objfile_chain)   (struct objfile *objfile);
static int stop_or_attach_thread_callback (const td_thrhandle_t *th, 
					   void *data);
static int wait_thread_callback           (const td_thrhandle_t *th, 
					   void *data);

static void
thread_db_new_objfile (struct objfile *objfile)
{
  td_err_e   ret;
  
  if (using_thread_db)			/* libthread already detected, and */
    goto quit;				/* thread target vector activated. */

  if (objfile == NULL)
    goto quit;	/* un-interesting object file */

  /* Initialize our "main prochandle" with the main inferior pid.  */
  main_prochandle.pid = PIDGET (inferior_pid);

  /* Now attempt to open a thread_db connection to the 
     thread library running in the child process.  */
  ret = p_td_ta_new (&main_prochandle, &main_threadagent);
  switch (ret) {
  default:
    warning ("Unexpected error initializing thread_db: %s", 
	     thr_err_string (ret));
    break;
  case TD_NOLIBTHREAD:	/* expected: no libthread in child process (yet) */
    break;	
  case TD_OK:		/* libthread detected in child: we go live now! */
    thread_db_push_target ();
    event_pid = inferior_pid;	/* for resume */

    /* Now stop everyone else, and attach any new threads you find.  */
    p_td_ta_thr_iter (main_threadagent, 
		      stop_or_attach_thread_callback,
		      (void *) 0,
		      TD_THR_ANY_STATE,
		      TD_THR_LOWEST_PRIORITY,
		      TD_SIGNO_MASK,
		      TD_THR_ANY_USER_FLAGS);

    /* Now go call wait on all the threads you've stopped:
       This allows us to absorb the SIGKILL event, and to make sure
       that the thread knows that it is stopped (Linux peculiarity).  */
    p_td_ta_thr_iter (main_threadagent, 
		      wait_thread_callback,
		      (void *) 0,
		      TD_THR_ANY_STATE,
		      TD_THR_LOWEST_PRIORITY,
		      TD_SIGNO_MASK,
		      TD_THR_ANY_USER_FLAGS);

    break;
  }
quit:
  if (target_new_objfile_chain)
    target_new_objfile_chain (objfile);
}


/* 

   LOCAL FUNCTION

   thread_db_alive     - test thread for "aliveness"

   SYNOPSIS

   static bool thread_db_alive (int pid);

   DESCRIPTION

   returns true if thread still active in inferior.

 */

static int
thread_db_alive (pid)
     int pid;
{
  if (is_thread (pid))		/* user-space (non-kernel) thread */
    {
      td_thrhandle_t th;
      td_err_e ret;

      pid = GET_THREAD (pid);
      if ((ret = p_td_ta_map_id2thr (main_threadagent, pid, &th)) != TD_OK)
	return 0;		/* thread not found */
      if ((ret = p_td_thr_validate (&th)) != TD_OK)
	return 0;		/* thread not valid */
      return 1;			/* known thread: return true */
    }
  else if (target_beneath->to_thread_alive)
    return target_beneath->to_thread_alive (pid);
  else
    return 0;		/* default to "not alive" (shouldn't happen anyway) */
}

/*
 * get_lwp_from_thread_handle
 */

static int	/* lwpid_t or pid_t */
get_lwp_from_thread_handle (th)
     td_thrhandle_t *th;
{
  td_thrinfo_t ti;
  td_err_e     ret;

  if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
    error ("get_lwp_from_thread_handle: thr_get_info failed: %s", 
	   thr_err_string (ret));

  return ti.ti_lid;
}

/*
 * get_lwp_from_thread_id
 */

static int	/* lwpid_t or pid_t */
get_lwp_from_thread_id (tid)
     int tid;	/* thread_t? */
{
  td_thrhandle_t th;
  td_err_e       ret;

  if ((ret = p_td_ta_map_id2thr (main_threadagent, tid, &th)) != TD_OK)
    error ("get_lwp_from_thread_id: map_id2thr failed: %s", 
	   thr_err_string (ret));

  return get_lwp_from_thread_handle (&th);
}

/* 
 * pid_to_str has to handle user-space threads.
 * If not a user-space thread, then pass the request on to the 
 * underlying stratum if it can handle it: else call normal_pid_to_str.
 */

static char *
thread_db_pid_to_str (int pid)
{
  static char buf[100];
  td_thrhandle_t th;
  td_thrinfo_t ti;
  td_err_e ret;

  if (is_thread (pid))
    {
      if ((ret = p_td_ta_map_id2thr (main_threadagent, 
				     GET_THREAD (pid),
				     &th)) != TD_OK)
	error ("thread_db: map_id2thr failed: %s", thr_err_string (ret));

      if ((ret = p_td_thr_get_info (&th, &ti)) != TD_OK)
	error ("thread_db: thr_get_info failed: %s", thr_err_string (ret));

      if (ti.ti_state == TD_THR_ACTIVE &&
	  ti.ti_lid != 0)
	sprintf (buf, "Thread %d (LWP %d)", ti.ti_tid, ti.ti_lid);
      else
	sprintf (buf, "Thread %d (%s)", ti.ti_tid,
		 thr_state_string (ti.ti_state));
    }
  else if (GET_LWP (pid))
    sprintf (buf, "LWP %d", GET_LWP (pid));
  else return normal_pid_to_str (pid);

  return buf;
}

/* 
 * thread_db target vector functions:
 */

static void
thread_db_files_info (struct target_ops *tgt_vector)
{
  /* This function will be unnecessary in real life.  */
  printf_filtered ("thread_db stratum:\n");
  target_beneath->to_files_info (tgt_vector);
}

/* 
 * xfer_memory has to munge the inferior_pid before passing the call
 * down to the target layer.  
 */

static int
thread_db_xfer_memory (memaddr, myaddr, len, dowrite, target)
     CORE_ADDR memaddr;
     char *myaddr;
     int len;
     int dowrite;
     struct target_ops *target;	/* ignored */
{
  struct cleanup *old_chain;
  int ret;

  old_chain = save_inferior_pid ();

  if (is_thread (inferior_pid) ||
      !target_thread_alive (inferior_pid))
    {
      /* FIXME: use the LID/LWP, so that underlying process layer
	 can read memory from specific threads?  */
      inferior_pid = main_prochandle.pid;
    }

  ret = target_beneath->to_xfer_memory (memaddr, myaddr, len,
					   dowrite, target);
  do_cleanups (old_chain);
  return ret;
}

/* 
 * fetch_registers has to determine if inferior_pid is a user-space thread.
 * If so, we use the thread_db API to get the registers.
 * And if not, we call the underlying process stratum.
 */

static void
thread_db_fetch_registers (regno)
     int regno;
{
  td_thrhandle_t thandle;
  gdb_prfpregset_t fpregset;
  prgregset_t gregset;
  thread_t thread;
  td_err_e ret;

  if (!is_thread (inferior_pid))	/* kernel thread */
    {			/* pass the request on to the target underneath.  */
      target_beneath->to_fetch_registers (regno);
      return;
    }

  /* convert inferior_pid into a td_thrhandle_t */

  if ((thread = GET_THREAD (inferior_pid)) == 0)
    error ("fetch_registers:  thread == 0");

  if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
    error ("fetch_registers: td_ta_map_id2thr: %s", thr_err_string (ret));

  /* Get the integer regs: 
     For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, 
     pc and sp are saved (by a thread context switch).  */
  if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK &&
      ret != TD_PARTIALREG)
    error ("fetch_registers: td_thr_getgregs %s", thr_err_string (ret));

  /* And, now the fp regs */
  if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK &&
      ret != TD_NOFPREGS)
    error ("fetch_registers: td_thr_getfpregs %s", thr_err_string (ret));

/* Note that we must call supply_{g fp}regset *after* calling the td routines
   because the td routines call ps_lget* which affect the values stored in the
   registers array.  */

  supply_gregset (gregset);
  supply_fpregset (&fpregset);

}

/* 
 * store_registers has to determine if inferior_pid is a user-space thread.
 * If so, we use the thread_db API to get the registers.
 * And if not, we call the underlying process stratum.
 */

static void
thread_db_store_registers (regno)
     int regno;
{
  td_thrhandle_t thandle;
  gdb_prfpregset_t fpregset;
  prgregset_t  gregset;
  thread_t thread;
  td_err_e ret;

  if (!is_thread (inferior_pid))	/* Kernel thread: */
    {		/* pass the request on to the underlying target vector.  */
      target_beneath->to_store_registers (regno);
      return;
    }

  /* convert inferior_pid into a td_thrhandle_t */

  if ((thread = GET_THREAD (inferior_pid)) == 0)
    error ("store_registers: thread == 0");

  if ((ret = p_td_ta_map_id2thr (main_threadagent, thread, &thandle)) != TD_OK)
    error ("store_registers: td_ta_map_id2thr %s", thr_err_string (ret));

  if (regno != -1)
    {				/* Not writing all the regs */
      /* save new register value */
      /* MVS: I don't understand this... */
      char old_value[REGISTER_SIZE];

      memcpy (old_value, &registers[REGISTER_BYTE (regno)], REGISTER_SIZE);

      if ((ret = p_td_thr_getgregs (&thandle, gregset)) != TD_OK)
	error ("store_registers: td_thr_getgregs %s", thr_err_string (ret));
      if ((ret = p_td_thr_getfpregs (&thandle, &fpregset)) != TD_OK)
	error ("store_registers: td_thr_getfpregs %s", thr_err_string (ret));

      /* restore new register value */
      memcpy (&registers[REGISTER_BYTE (regno)], old_value, REGISTER_SIZE);

    }

  fill_gregset  (gregset, regno);
  fill_fpregset (&fpregset, regno);

  if ((ret = p_td_thr_setgregs (&thandle, gregset)) != TD_OK)
    error ("store_registers: td_thr_setgregs %s", thr_err_string (ret));
  if ((ret = p_td_thr_setfpregs (&thandle, &fpregset)) != TD_OK &&
      ret != TD_NOFPREGS)
    error ("store_registers: td_thr_setfpregs %s", thr_err_string (ret));
}

static void
handle_new_thread (tid, lid, verbose)
     int tid;	/* user thread id */
     int lid;	/* kernel thread id */
     int verbose;
{
  int gdb_pid = BUILD_THREAD (tid, main_prochandle.pid);
  int wait_pid, wait_status;

  if (verbose)
    printf_filtered ("[New %s]\n", target_pid_to_str (gdb_pid));
  add_thread (gdb_pid);

  if (lid != main_prochandle.pid)
    {
      attach_thread (lid);
      /* According to the Eric Paire model, we now have to send
	 the restart signal to the new thread -- however, empirically,
	 I do not find that to be necessary.  */
      attach_pid = lid;
    }
}

static void
test_for_new_thread (tid, lid, verbose)
     int tid;
     int lid;
     int verbose;
{
  if (!in_thread_list (BUILD_THREAD (tid, main_prochandle.pid)))
    handle_new_thread (tid, lid, verbose);
}

/* 
 * Callback function that gets called once per USER thread 
 * (i.e., not kernel) thread by td_ta_thr_iter.
 */

static int
find_new_threads_callback (th, ignored)
     const td_thrhandle_t *th;
     void *ignored;
{
  td_thrinfo_t ti;
  td_err_e     ret;

  if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
    {
      warning ("find_new_threads_callback: %s", thr_err_string (ret));
      return -1;		/* bail out, get_info failed. */
    }

  /* FIXME: 
     As things now stand, this should never detect a new thread.
     But if it does, we could be in trouble because we aren't calling
     wait_thread_callback for it.  */
  test_for_new_thread (ti.ti_tid, ti.ti_lid, 0);
  return 0;
}

/* 
 * find_new_threads uses the thread_db iterator function to discover
 * user-space threads.  Then if the underlying process stratum has a
 * find_new_threads method, we call that too.
 */

static void
thread_db_find_new_threads ()
{
  if (inferior_pid == -1)	/* FIXME: still necessary? */
    {
      printf_filtered ("No process.\n");
      return;
    }
  p_td_ta_thr_iter (main_threadagent, 
		    find_new_threads_callback, 
		    (void *) 0, 
		    TD_THR_ANY_STATE, 
		    TD_THR_LOWEST_PRIORITY,
		    TD_SIGNO_MASK, 
		    TD_THR_ANY_USER_FLAGS);
  if (target_beneath->to_find_new_threads)
    target_beneath->to_find_new_threads ();
}

/*
 * Resume all threads, or resume a single thread.
 * If step is true, then single-step the appropriate thread
 * (or single-step inferior_pid, but continue everyone else).
 * If signo is true, then send that signal to at least one thread.
 */

/*
 * This function is called once for each thread before resuming.
 * It sends continue (no step, and no signal) to each thread except
 *   the main thread, and
 *   the event thread (the one that stopped at a breakpoint etc.)
 *
 * The event thread is handled separately so that it can be sent
 * the stepping and signal args with which target_resume was called.
 *
 * The main thread is resumed last, so that the thread_db proc_service
 * callbacks will still work during the iterator function.
 */

static int
resume_thread_callback (th, data)
     const td_thrhandle_t *th;
     void *data;
{
  td_thrinfo_t ti;
  td_err_e     ret;

  if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
    {
      warning ("resume_thread_callback: %s", thr_err_string (ret));
      return -1;		/* bail out, get_info failed. */
    }
  /* FIXME: 
     As things now stand, this should never detect a new thread.
     But if it does, we could be in trouble because we aren't calling
     wait_thread_callback for it.  */
  test_for_new_thread (ti.ti_tid, ti.ti_lid, 1);

  if (ti.ti_lid != main_prochandle.pid &&
      ti.ti_lid != event_pid)
    {
      /* Unconditionally continue the thread with no signal.
	 Only the event thread will get a signal of any kind.  */

      target_beneath->to_resume (ti.ti_lid, 0, 0);
    }
  return 0;
}

static int
new_resume_thread_callback (thread, data)
     threadinfo *thread;
     void *data;
{
  if (thread->lid != event_pid &&
      thread->lid != main_prochandle.pid)
    {
      /* Unconditionally continue the thread with no signal (for now).  */

      target_beneath->to_resume (thread->lid, 0, 0);
    }
  return 0;
}

static int last_resume_pid;
static int last_resume_step;
static int last_resume_signo;

static void
thread_db_resume (pid, step, signo)
     int pid;
     int step;
     enum target_signal signo;
{
  last_resume_pid   = pid;
  last_resume_step  = step;
  last_resume_signo = signo;

  /* resuming a specific pid? */
  if (pid != -1)
    {
      if (is_thread (pid))
	pid = get_lwp_from_thread_id (GET_THREAD (pid));
      else if (GET_LWP (pid))
	pid = GET_LWP (pid);
    }

  /* Apparently the interpretation of 'pid' is dependent on 'step':
     If step is true, then a specific pid means 'step only this pid'.
     But if step is not true, then pid means 'continue ALL pids, but
     give the signal only to this one'.  */
  if (pid != -1 && step)
    {
      /* FIXME: is this gonna work in all circumstances? */
      target_beneath->to_resume (pid, step, signo);
    }
  else
    {
      /* 1) Continue all threads except the event thread and the main thread.
	 2) resume the event thread with step and signo.
	 3) If event thread != main thread, continue the main thread.

	 Note: order of 2 and 3 may need to be reversed.  */

      threadlist_iter (new_resume_thread_callback, 
			(void *) 0, 
			TD_THR_ANY_STATE, 
			TD_THR_ANY_TYPE);
      /* now resume event thread, and if necessary also main thread. */
      if (event_pid)
	{
	  target_beneath->to_resume (event_pid, step, signo);
	}
      if (event_pid != main_prochandle.pid)
	{
	  target_beneath->to_resume (main_prochandle.pid, 0, 0);
	}
    }
}

/* All new threads will be attached.
   All previously known threads will be stopped using kill (SIGKILL).  */

static int
stop_or_attach_thread_callback (const td_thrhandle_t *th, void *data)
{
  td_thrinfo_t ti;
  td_err_e     ret;
  int          gdb_pid;
  int on_off = 1;

  if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
    {
      warning ("stop_or_attach_thread_callback: %s", thr_err_string (ret));
      return -1;		/* bail out, get_info failed. */
    }

  /* First add it to our internal list.  
     We build this list anew at every wait event.  */
  insert_thread (ti.ti_tid, ti.ti_lid, ti.ti_state, ti.ti_type);
  /* Now: if we've already seen it, stop it, else add it and attach it.  */
  gdb_pid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
  if (!in_thread_list (gdb_pid))	/* new thread */
    {
      handle_new_thread (ti.ti_tid, ti.ti_lid, 1);
      /* Enable thread events */
      if (p_td_thr_event_enable)
	if ((ret = p_td_thr_event_enable (th, on_off)) != TD_OK)
	  warning ("stop_or_attach_thread: %s", thr_err_string (ret));
    }
  else if (ti.ti_lid != event_pid &&
	   ti.ti_lid != main_prochandle.pid)
    {
      ret = (td_err_e) kill (ti.ti_lid, SIGSTOP);
    }

  return 0;
}
     
/*
 * Wait for signal N from pid PID.
 * If wait returns any other signals, put them back before returning.
 */

static void
wait_for_stop (pid)
     int pid;
{
  int i;
  int retpid;
  int status;

  /* Array of wait/signal status */
  /* FIXME: wrong data structure, we need a queue.
     Realtime signals may be delivered more than once.  
     And at that, we really can't handle them (see below).  */
#if defined (NSIG)
  static int   wstatus [NSIG];
#elif defined (_NSIG)
  static int   wstatus [_NSIG];
#else
#error No definition for number of signals!
#endif

  /* clear wait/status list */
  memset (&wstatus, 0, sizeof (wstatus));

  /* Now look for SIGSTOP event on all threads except event thread.  */
  do {
    errno = 0;
    if (pid == main_prochandle.pid)
      retpid = waitpid (pid, &status, 0);
    else
      retpid = waitpid (pid, &status, __WCLONE);

    if (retpid > 0)
      if (WSTOPSIG (status) == SIGSTOP)
	{
	  /* Got the SIGSTOP event we're looking for.
	     Throw it away, and throw any other events back!  */
	  for (i = 0; i < sizeof(wstatus) / sizeof (wstatus[0]); i++)
	    if (wstatus[i])
	      if (i != SIGSTOP)
		{
		  kill (retpid, i);
		}
	  break;	/* all done */
	}
      else
	{
	  int signo;
	  /* Oops, got an event other than SIGSTOP.
	     Save it, and throw it back after we find the SIGSTOP event.  */
	  
	  /* FIXME (how?)  This method is going to fail for realtime
	     signals, which cannot be put back simply by using kill.  */

	  if (WIFEXITED (status))
	    error ("Ack!  Thread Exited event.  What do I do now???");
	  else if (WIFSTOPPED (status))
	    signo = WSTOPSIG (status);
	  else
	    signo = WTERMSIG (status);
	  
	  /* If a thread other than the event thread has hit a GDB
	     breakpoint (as opposed to some random trap signal), then
	     just arrange for it to hit it again later.  Back up the
	     PC if necessary.  Don't forward the SIGTRAP signal to
	     the thread.  We will handle the current event, eventually
	     we will resume all the threads, and this one will get
	     it's breakpoint trap again.

	     If we do not do this, then we run the risk that the user
	     will delete or disable the breakpoint, but the thread will
	     have already tripped on it.  */

	  if (retpid != event_pid &&
	      signo == SIGTRAP &&
	      breakpoint_inserted_here_p (read_pc_pid (retpid) - 
					  DECR_PC_AFTER_BREAK))
	    {
	      /* Set the pc to before the trap and DO NOT re-send the signal */
	      if (DECR_PC_AFTER_BREAK)
		write_pc_pid (read_pc_pid (retpid) - DECR_PC_AFTER_BREAK,
			      retpid);
	    }

	  /* Since SIGINT gets forwarded to the entire process group
	     (in the case where ^C is typed at the tty / console), 
	     just ignore all SIGINTs from other than the event thread.  */
	  else if (retpid != event_pid && signo == SIGINT)
	    { /* do nothing.  Signal will disappear into oblivion!  */
	      ;
	    }

	  else /* This is some random signal other than a breakpoint. */
	    {
	      wstatus [signo] = 1;
	    }
	  child_resume (retpid, 0, TARGET_SIGNAL_0);
	  continue;
	}

  } while (errno == 0 || errno == EINTR);
}

/*
 * wait_thread_callback
 *
 * Calls waitpid for each thread, repeatedly if necessary, until
 * SIGSTOP is returned.  Afterward, if any other signals were returned
 * by waitpid, return them to the thread's pending queue by calling kill.
 */

static int
wait_thread_callback (const td_thrhandle_t *th, void *data)
{
  td_thrinfo_t ti;
  td_err_e     ret;

  if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
    {
      warning ("wait_thread_callback: %s", thr_err_string (ret));
      return -1;		/* bail out, get_info failed. */
    }

  /* This callback to act on all threads except the event thread: */
  if (ti.ti_lid == event_pid || 	/* no need to wait (no sigstop) */
      ti.ti_lid == main_prochandle.pid)	/* no need to wait (already waited) */
    return 0;	/* don't wait on the event thread.  */

  wait_for_stop (ti.ti_lid);
  return 0;	/* finished: next thread. */
}

static int
new_wait_thread_callback (thread, data)
     threadinfo *thread;
     void *data;
{
  /* don't wait on the event thread -- it's already stopped and waited.  
     Ditto the main thread.  */
  if (thread->lid != event_pid &&
      thread->lid != main_prochandle.pid)
    {
      wait_for_stop (thread->lid);
    }
  return 0;
}

/* 
 * Wait for any thread to stop, by calling the underlying wait method.
 * The PID returned by the underlying target may be a kernel thread,
 * in which case we will want to convert it to the corresponding
 * user-space thread.  
 */

static int
thread_db_wait (int pid, struct target_waitstatus *ourstatus)
{
  td_thrhandle_t thandle;
  td_thrinfo_t ti;
  td_err_e ret;
  lwpid_t lwp;
  int retpid;
  int status;
  int save_errno;

  /* OK, we're about to wait for an event from the running inferior.
     Make sure we're ignoring the right signals.  */

  check_all_signal_numbers ();	/* see if magic signals changed. */

  event_pid = 0;
  attach_pid = 0;

  /* FIXME: should I do the wait right here inline?  */
#if 0
  if (pid == -1)
    lwp = -1;
  else
    lwp = get_lwp_from_thread_id (GET_THREAD (pid));
#endif


  save_errno = linux_child_wait (-1, &retpid, &status);
  store_waitstatus (ourstatus, status);

  /* Thread ID is irrelevant if the target process exited.
     FIXME: do I have any killing to do?
     Can I get this event mistakenly from a thread?  */
  if (ourstatus->kind == TARGET_WAITKIND_EXITED)
    return retpid;

  /* OK, we got an event of interest.
     Go stop all threads and look for new ones.
     FIXME: maybe don't do this for the restart signal?  Optimization...  */
  event_pid = retpid;

  /* If the last call to resume was for a specific thread, then we don't
     need to stop everyone else: they should already be stopped.  */
  if (last_resume_step == 0 || last_resume_pid == -1)
    {
      /* Main thread must be stopped before calling the iterator.  */
      if (retpid != main_prochandle.pid)
	{
	  kill (main_prochandle.pid, SIGSTOP);
	  wait_for_stop (main_prochandle.pid);
	}

      empty_threadlist ();
      /* Now stop everyone else, and attach any new threads you find.  */
      p_td_ta_thr_iter (main_threadagent, 
			stop_or_attach_thread_callback,
			(void *) 0,
			TD_THR_ANY_STATE,
			TD_THR_LOWEST_PRIORITY,
			TD_SIGNO_MASK,
			TD_THR_ANY_USER_FLAGS);

      /* Now go call wait on all the threads we've stopped:
	 This allows us to absorb the SIGKILL event, and to make sure
	 that the thread knows that it is stopped (Linux peculiarity).  */

      threadlist_iter (new_wait_thread_callback, 
		       (void *) 0,
		       TD_THR_ANY_STATE, 
		       TD_THR_ANY_TYPE);
    }

  /* Convert the kernel thread id to the corresponding thread id.  */

  /* If the process layer does not furnish an lwp,
     then perhaps the returned pid IS the lwp... */
  if ((lwp = GET_LWP (retpid)) == 0)
    lwp = retpid;

  if ((ret = p_td_ta_map_lwp2thr (main_threadagent, lwp, &thandle)) != TD_OK)
    return retpid;	/* LWP is not mapped onto a user-space thread. */

  if ((ret = p_td_thr_validate (&thandle)) != TD_OK)
    return retpid;	/* LWP is not mapped onto a valid thread. */

  if ((ret = p_td_thr_get_info (&thandle, &ti)) != TD_OK)
    {
      warning ("thread_db: thr_get_info failed ('%s')", thr_err_string (ret));
      return retpid;
    }

  retpid = BUILD_THREAD (ti.ti_tid, main_prochandle.pid);
  /* If this is a new user thread, notify GDB about it.  */
  if (!in_thread_list (retpid))
    {
      printf_filtered ("[New %s]\n", target_pid_to_str (retpid));
      add_thread (retpid);
    }

#if 0
  /* Now detect if this is a thread creation/deletion event: */
  check_for_thread_event (ourstatus, retpid);
#endif
  return retpid;
}

/* 
 * kill has to call the underlying kill.
 * FIXME: I'm not sure if it's necessary to check inferior_pid any more,
 * but we might need to fix inferior_pid up if it's a user thread.
 */

static int
kill_thread_callback (th, data)
     td_thrhandle_t *th;
     void *data;
{
  td_thrinfo_t ti;
  td_err_e     ret;

  /* Fixme: 
     For Linux, threads may need to be waited.  */
  if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
    {
      warning ("kill_thread_callback: %s", thr_err_string (ret));
      return -1;		/* bail out, get_info failed. */
    }

  if (ti.ti_lid != main_prochandle.pid)
    {
      kill (ti.ti_lid, SIGKILL);
    }
  return 0;
}


static void thread_db_kill (void)
{
  int rpid;
  int status;

  /* Fixme: 
     For Linux, threads may need to be waited.  */
  if (inferior_pid != 0)
    {
      /* Go kill the children first.  Save the main thread for last. */
      p_td_ta_thr_iter (main_threadagent, 
			kill_thread_callback, 
			(void *) 0, 
			TD_THR_ANY_STATE,
			TD_THR_LOWEST_PRIORITY,
			TD_SIGNO_MASK,
			TD_THR_ANY_USER_FLAGS);

      /* Turn off thread_db event-reporting API *before* killing the
	 main thread, since this operation requires child memory access.
	 Can't move this into thread_db_unpush target because then 
	 detach would not work.  */
      disable_thread_event_reporting (main_threadagent);

      inferior_pid = main_prochandle.pid;

      /* 
       * Since both procfs_kill and ptrace_kill call target_mourn, 
       * it should be sufficient for me to call one of them.
       * That will result in my mourn being called, which will both
       * unpush me and call the underlying mourn.
       */
      target_beneath->to_kill ();
    }

  /* Wait for all threads. */
  /* FIXME: need a universal wait_for_signal func? */
  do
    {
      rpid = waitpid (-1, &status, __WCLONE | WNOHANG);
    }
  while (rpid > 0 || errno == EINTR);

  do
    {
      rpid = waitpid (-1, &status, WNOHANG);
    }
  while (rpid > 0 || errno == EINTR);
}

/* 
 * Mourn has to remove us from the target stack, 
 * and then call the underlying mourn.
 */

static void thread_db_mourn_inferior (void)
{
  thread_db_unpush_target ();
  target_mourn_inferior ();	/* call the underlying mourn */
}

/* 
 * Detach has to remove us from the target stack, 
 * and then call the underlying detach.
 *
 * But first, it has to detach all the cloned threads!
 */

static int
detach_thread_callback (th, data)
     td_thrhandle_t *th;
     void *data;
{
  /* Called once per thread.  */
  td_thrinfo_t ti;
  td_err_e     ret;

  if ((ret = p_td_thr_get_info (th, &ti)) != TD_OK)
    {
      warning ("detach_thread_callback: %s", thr_err_string (ret));
      return -1;                /* bail out, get_info failed. */
    }

  if (!in_thread_list (BUILD_THREAD (ti.ti_tid, main_prochandle.pid)))
    return 0;	/* apparently we don't know this one.  */

  /* Save main thread for last, or the iterator will fail! */
  if (ti.ti_lid != main_prochandle.pid)
    {
      struct cleanup *old_chain;
      int off = 0;

      /* Time to detach this thread. 
	 First disable thread_db event reporting for the thread.  */
      if (p_td_thr_event_enable &&
	  (ret = p_td_thr_event_enable (th, off)) != TD_OK)
	{
	  warning ("detach_thread_callback: %s\n", thr_err_string (ret));
	  return 0;
	}

      /* Now cancel any pending SIGTRAPS.  FIXME!  */

      /* Call underlying detach method.  FIXME just detach it.  */
      old_chain = save_inferior_pid ();
      inferior_pid = ti.ti_lid;
      detach (TARGET_SIGNAL_0);
      do_cleanups (old_chain);
    }
  return 0;
}

static void
thread_db_detach (char *args, int from_tty)
{
  td_err_e ret;

  if ((ret = p_td_ta_thr_iter (main_threadagent, 
			       detach_thread_callback, 
			       (void *) 0, 
			       TD_THR_ANY_STATE,
			       TD_THR_LOWEST_PRIORITY,
			       TD_SIGNO_MASK,
			       TD_THR_ANY_USER_FLAGS))
      != TD_OK)
    warning ("detach (thr_iter): %s", thr_err_string (ret));

  /* Turn off thread_db event-reporting API 
     (before detaching the main thread) */
  disable_thread_event_reporting (main_threadagent);

  thread_db_unpush_target ();

  /* above call nullifies target_beneath, so don't use that! */
  inferior_pid = PIDGET (inferior_pid);
  target_detach (args, from_tty);
}


/* 
 * We never want to actually create the inferior!
 *
 * If this is ever called, it means we were on the target stack
 * when the user said "run".  But we don't want to be on the new
 * inferior's target stack until the thread_db / libthread
 * connection is ready to be made.
 *
 * So, what shall we do?
 * Unpush ourselves from the stack, and then invoke
 * find_default_create_inferior, which will invoke the
 * appropriate process_stratum target to do the create.
 */

static void
thread_db_create_inferior (exec_file, allargs, env)
     char *exec_file;
     char *allargs;
     char **env;
{
  thread_db_unpush_target ();
  find_default_create_inferior (exec_file, allargs, env);
}

/* 
 * Thread_db target vector initializer.
 */

void
init_thread_db_ops ()
{
  thread_db_ops.to_shortname        = "multi-thread";
  thread_db_ops.to_longname         = "multi-threaded child process.";
  thread_db_ops.to_doc              = "Threads and pthreads support.";
  thread_db_ops.to_files_info       = thread_db_files_info;
  thread_db_ops.to_create_inferior  = thread_db_create_inferior;
  thread_db_ops.to_detach           = thread_db_detach;
  thread_db_ops.to_wait             = thread_db_wait;
  thread_db_ops.to_resume           = thread_db_resume;
  thread_db_ops.to_mourn_inferior   = thread_db_mourn_inferior;
  thread_db_ops.to_kill             = thread_db_kill;
  thread_db_ops.to_xfer_memory      = thread_db_xfer_memory;
  thread_db_ops.to_fetch_registers  = thread_db_fetch_registers;
  thread_db_ops.to_store_registers  = thread_db_store_registers;
  thread_db_ops.to_thread_alive     = thread_db_alive;
  thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
  thread_db_ops.to_pid_to_str       = thread_db_pid_to_str;
  thread_db_ops.to_stratum          = thread_stratum;
  thread_db_ops.to_has_thread_control = tc_schedlock;
  thread_db_ops.to_magic            = OPS_MAGIC;
}
#endif	/* HAVE_STDINT_H */

/*
 * Module constructor / initializer function.
 * If connection to thread_db dynamic library is successful, 
 * then initialize this module's target vectors and the 
 * new_objfile hook.
 */


void
_initialize_thread_db ()
{
#ifdef HAVE_STDINT_H	/* stub out entire module, leave initializer empty */
  if (init_thread_db_library ())
    {
      init_thread_db_ops ();
      add_target (&thread_db_ops);
      /*
       * Hook up to the new_objfile event.
       * If someone is already there, arrange for him to be called
       * after we are.
       */
      target_new_objfile_chain = target_new_objfile_hook;
      target_new_objfile_hook  = thread_db_new_objfile;
    }
#endif	/* HAVE_STDINT_H */
}