OSDN Git Service

Fix problem in which singlestep operations were apparently ignored.
[pf3gnuchains/pf3gnuchains3x.git] / rda / unix / lwp-pool.c
1 /* lwp-pool.c --- implementation of a stoppable, waitable LWP pool.
2
3    Copyright 2004 Red Hat, Inc.
4
5    This file is part of RDA, the Red Hat Debug Agent (and library).
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.
21    
22    Alternative licenses for RDA may be arranged by contacting Red Hat,
23    Inc.  */
24
25 #include "config.h"
26
27 #define _GNU_SOURCE /* for strerror */
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36
37 #include "lwp-pool.h"
38 #include "lwp-ctrl.h"
39
40 #include "diagnostics.h"
41
42 int debug_lwp_pool = 0;
43
44 \f
45 /* THE LIFETIME OF A TRACED LWP
46
47    POSIX uses these terms in talking about signals:
48
49    - To "generate" a signal is to call kill or raise, divide by zero,
50      etc.
51
52    - To "deliver" a signal is to do whatever that signal's designated
53      action is: ignore it, enter a signal handler, terminate the
54      process, or stop the process.
55
56    - To "accept" a signal is to have 'sigwait' or a similar function
57      select and return the signal.
58
59    - A signal is "pending" between the time it is generated and the
60      time it is delivered.
61
62    So, here is the life cycle of a traced LWP:
63
64    - It is created by fork or vfork and does a PTRACE_TRACEME.  The
65      PTRACE_TRACEME makes it a traced, running LWP.  When a traced LWP
66      does an exec, it gets a SIGTRAP before executing the first
67      instruction in the new process image, so the LWP will then stop.
68
69      Or, we attach to it with a PTRACE_ATTACH.  This sends a SIGSTOP
70      to the LWP, so it will stop.
71
72    - While a traced LWP is stopped, we can read and write its
73      registers and memory.  We can also send it signals; they become
74      pending on the LWP, and will be reported by waitpid.
75
76    - A stopped LWP can be set running again in one of two ways:
77
78      + by doing a PTRACE_CONT, PTRACE_SINGLESTEP, or PTRACE_SYSCALL; or
79
80      + by sending it a SIGCONT.
81
82      The ptrace requests all let you specify a signal to be delivered
83      to the process.  This is the only way signals (other than
84      SIGKILL) ever get actually delivered: every other signal just
85      gets reported to the debugger via waitpid when delivery is
86      attempted.
87
88      Sending a SIGCONT clears any pending SIGSTOPs; PTRACE_CONT and
89      PTRACE_SINGLESTEP don't have that side effect.
90
91      (Sending an LWP a SIGKILL via the 'kill' or 'tkill' system calls
92      acts like sending it a SIGKILL followed by a SIGCONT.)
93
94    - A running LWP may exit or be terminated by a signal at any time,
95      so accessing its memory or registers or sending it a signal is
96      always a race.
97
98    - waitpid will eventually return a status S for a continued LWP:
99
100      + If WIFEXITED (S) or WIFSIGNALED (S), the LWP no longer exists.
101      
102      + IF WIFSTOPPED (S), the LWP is stopped again, because some
103        signal WSTOPSIG (S) was about to be delivered to it.  Here we
104        go back to the second step.
105
106        Note that the signal WSTOPSIG (S) has not yet been delivered to
107        the process, and is no longer pending on the process.  Only
108        signals passed to the ptrace requests get delivered.  In
109        effect, the debugger gets to intercept signals before they are
110        delivered, and decide whether to pass them through or not.
111        (The exception is SIGKILL: that always produces a WIFSIGNALED
112        wait status, and terminates the process.)
113
114    So, to put all that together:
115
116    - A traced LWP goes back and forth from running to stopped, until
117      eventually it goes from running to exited or killed.
118
119    - Running->stopped transitions are always attempted signal
120      deliveries, yielding WIFSTOPPED wait statuses.
121
122    - Stopping->running transitions are generally due to ptrace
123      requests by the debugger.  (The debugger could use kill to send
124      SIGCONT, but that's messy.)
125
126    - Running->exited transitions are due to, duh, the LWP exiting.
127
128    - Running->killed transitions are due to a signal being delivered
129      to the LWP that is neither ignored nor caught.
130
131
132    Under NPTL, this life cycle is a bit different: LWPs simply exit,
133    without creating a zombie; they produce no wait status.  The NPTL
134    libthread_db generates a TD_DEATH event for them, but at the kernel
135    level the only indication that they're gone is that the tkill
136    system call fails with ESRCH ("No such process").
137
138    Under LinuxThreads, LWPs remain zombie processes until they're
139    waited for.  Attempts to send them signals while zombies have no
140    effect, but return no error.
141
142
143    STOPPING A PROCESS
144
145    The major challenge here is implementing the lwp_pool_stop_all
146    function.  The only way to stop a running LWP is to send it a
147    SIGSTOP, and then wait for a status acknowledging the stop.  But as
148    explained above, a running LWP could stop at any time of its own
149    accord, so sending it a SIGSTOP is always a race.  By the time you
150    call waitpid, you don't know whether you'll get a status for the
151    SIGSTOP you just sent, or for something else: some other signal, an
152    exit, or a termination by signal.
153
154    If the LWP turns out to have exited or died, then that's pretty
155    easy to handle.  Your attempt to send a SIGSTOP will get an error,
156    and then you'll get a wait status for the termination.  A
157    termination status is always the last status you'll get from wait
158    for that LWP, so there'll be no further record of your SIGSTOP.
159
160    If the LWP was about to have some other signal delivered to it,
161    then the next wait will return a WIFSTOPPED status for that signal;
162    we'll have to continue the LWP and wait again until we get the
163    status for our SIGSTOP.  The kernel forgets about any signals the
164    LWP has received once it has reported them to us, so it's up to us
165    to keep track of them and report them via lwp_pool_waitpid.  */
166
167
168 \f
169 /* The LWP structure.  */
170
171 /* The states an LWP we're managing might be in.
172
173    For the purposes of these states, we classify wait statuses as
174    follows:
175
176    - An "interesting" wait status is one that isn't a result of us
177      attaching to the LWP or sending it a SIGSTOP for
178      lwp_pool_stop_all.  It indicates something that happened to the
179      LWP other than as a result of this code's fiddling with it.  We
180      report all interesting wait statuses via lwp_pool_waitpid.
181
182    - A "boring" wait status is one that results from our attaching to
183      it or sending it a SIGSTOP for lwp_pool_stop_all.  We do not
184      report these via lwp_pool_stop_all.
185
186    Most of these states are combinations of various semi-independent
187    factors, which we'll name and define here:
188
189    - RUNNING / STOPPED / DEAD: These are the kernel states of the LWP:
190      it's either running freely and could stop at any moment, is
191      stopped but can be continued, or has died.
192
193    - INTERESTING: this LWP has stopped or died with a wait status that
194      has not yet been reported via lwp_pool_waitpid.  It is on the
195      interesting LWP queue.
196
197      This never applies to RUNNING LWPs: we never continue an
198      INTERESTING LWP until we've reported its status.
199
200      It always applies to DEAD LWPs.
201
202    - STOP PENDING: we've sent this LWP a SIGSTOP, or attached to it,
203      but we haven't yet received the boring WIFSTOPPED SIGSTOP status.
204
205      This never applies to DEAD LWPs; the wait status that announces a
206      LWP's death is always the last for that LWP.
207
208    We could certainly represent these with independent bits or
209    bitfields, but not all combinations are possible.  So instead, we
210    assign each possible combination a distinct enum value, to make it
211    easier to enumerate all the valid possibilities and be sure we've
212    handled them.  */
213
214 enum lwp_state {
215
216   /* An uninitialized LWP entry.  Only the lookup function itself,
217      hash_find, creates entries in this state, and any function
218      that calls that should put the entry in a meaningful state before
219      returning.  */
220   lwp_state_uninitialized,
221
222   /* RUNNING.  This LWP is running --- last we knew.  It may have
223      exited or been terminated by a signal, or it may have had a
224      signal about to be delivered to it.  We won't know until we wait
225      for it.  */
226   lwp_state_running,
227
228   /* STOPPED.  This LWP has stopped, and has no interesting status to
229      report.  */
230   lwp_state_stopped,
231
232   /* STOPPED, INTERESTING.  This LWP has stopped with an interesting
233      wait status, which we haven't yet reported to the user.  It is on
234      the interesting LWP queue.  */
235   lwp_state_stopped_interesting,
236
237   /* DEAD, INTERESTING.  This LWP exited, or was killed by a signal.
238      This LWP is on the interesting LWP queue.  Once we've reported it
239      to the user, we'll delete it altogether.  */
240   lwp_state_dead_interesting,
241
242   /* RUNNING, STOP PENDING.  This LWP was running, and will eventually
243      stop with a boring WIFSTOPPED SIGSTOP status, but may report an
244      interesting status first.
245
246      It's always safe to wait for an LWP in this state, so we do that
247      as soon as possible; there shouldn't be any LWPs in this state
248      between calls to public lwp_pool functions.  This is an
249      internal-use state.  */
250   lwp_state_running_stop_pending,
251
252   /* STOPPED, STOP PENDING.  This LWP is stopped, and has no
253      interesting status to report, but still has a boring status on
254      the way.  After we report the status for a STOPPED, STOP PENDING,
255      and INTERESTING LWP, this is the state it enters.
256
257      See the note below on why this state is not avoidable.  */
258   lwp_state_stopped_stop_pending,
259
260   /* STOPPED, STOP PENDING, and INTERESTING.  This LWP has stopped with
261      an interesting wait status.  We're also expecting a boring wait
262      status from it.  */
263   lwp_state_stopped_stop_pending_interesting,
264
265 };
266
267
268 /* Why we need lwp_state_stopped_stop_pending:
269
270    I originally thought we could avoid having this state at all by
271    simply always continuing STOPPED, STOP PENDING, INTERESTING LWPs
272    in lwp_pool_waitpid as soon as we reported their wait status, and
273    then waiting for them immediately, making them either STOPPED and
274    un-INTERESTING, or STOPPED, STOP PENDING, and INTERESTING again.
275
276    But the user has the right to call lwp_pool_continue_lwp on any LWP
277    they've just gotten a wait status for --- and this simplification
278    interferes with that.  First, note that you mustn't call
279    continue_lwp on an interesting LWP: you might get yet another
280    interesting wait status, and we don't want to queue up multiple
281    interesting wait statuses per LWP --- the job is complex enough
282    already.  Then, note that the proposed simplification means that
283    lwp_pool_waitpid could return a status for some LWP, and have that
284    LWP still be interesting.  If that happens, then you've got an LWP
285    the user has the right to continue, but that can't actually be
286    continued.
287
288    I first tried to deal with this by having lwp_pool_continue_lwp
289    simply do nothing if the user continues an interesting LWP.  After
290    all, it's already in the interesting queue, so lwp_pool_waitpid
291    will report it, and the user will be none the wiser.  But that's
292    wrong: the user can specify a signal to deliver when they continue
293    the LWP, and the only way signals are ever delivered to traced LWPs
294    is via ptrace continue and single-step requests.  You can't use
295    kill: that *generates* a signal, it doesn't *deliver* it.  You'd
296    just get the signal back again via waitpid.  So if we don't
297    actually continue the LWP with the user's signal, we've lost our
298    only chance to deliver it.
299
300    Clear as mud, no doubt.  I did my best.  */
301
302
303 struct lwp
304 {
305   /* This lwp's PID.  */
306   pid_t pid;
307
308   /* The state this LWP is in.  */
309   enum lwp_state state;
310
311   /* If STATE is one of the lwp_state_*_interesting states, then this
312      LWP is on the interesting LWP queue, headed by interesting_queue.
313
314      If STATE is lwp_state_running_stop_pending, then this LWP is on
315      the stopping LWP queue, stopping_queue.  (Note that
316      stopping_queue is local to lwp_pool_stop_all; no LWP should be in
317      that state by the time that function returns.  */
318   struct lwp *prev, *next;
319
320   /* If STATE is one of the lwp_state_*_interesting states, then
321      STATUS is the interesting wait status.  */
322   int status;
323
324   /* Indicates the stepping status.  We must be prepared to step the
325      given lwp upon continue since it's possible to get thread notification
326      signals prior to a step actually occuring.  Receipt of a SIGTRAP is
327      sufficient to clear this flag.  */
328   int do_step;
329 };
330  
331   
332 \f
333 /* The LWP hash table.  */
334
335 /* A hash table of all the live LWP's we know about.
336    hash_population is the number of occupied entries in the table.
337
338    hash_size is the total length of the table; it is always a power of
339    two.  We resize the table to ensure that it is between 12.5% and
340    50% occupied.  (Since the table's size is a power of two, resizing
341    the table will always halve or double the populated ratio.  So
342    there should be comfortably more than a factor of two between the
343    maximum and minimum populations, for hysteresis.)
344
345    The first slot we try is hash[PID % hash_size].  After C
346    collisions, we try hash[(PID + C * STRIDE) % hash_size], where
347    STRIDE is hash_size / 4 + 1.  The kernel assigns pids sequentially,
348    so a STRIDE of 1, as many hash tables use, would make further
349    collisions very likely.  But since hash_size is always a power of
350    two, and hash_size / 4 + 1 is always odd, they are always
351    relatively prime, so stepping by that many elements each time will
352    eventually visit every table element.  A constant odd stride would
353    be fine, but it's nice to have it scale with the overall population
354    of the table.
355
356    The table is an array of pointers to lwp's, rather than a direct
357    array of lwp structures, so that pointers to lwp's don't become
358    invalid when we rehash or delete entries.  */
359 static size_t hash_size, hash_population;
360 static struct lwp **hash;
361
362 /* The minimum size for the hash table.  Small for testing.  */
363 enum { minimum_hash_size = 8 };
364
365
366 /* Return the hash slot for pid PID.  */
367 static int
368 hash_slot (pid_t pid, size_t size)
369 {
370   return pid & (size - 1);
371 }
372
373
374 /* If there was a collision in SLOT, return the next slot.  */
375 static int
376 hash_next_slot (int slot, size_t size)
377 {
378   int stride = size / 4 + 1;
379
380   return (slot + stride) & (size - 1);
381 }
382
383
384 /* Return the earliest empty hash slot for PID.  */
385 static int
386 hash_empty_slot (pid_t pid)
387 {
388   int slot = hash_slot (pid, hash_size);
389
390   /* Since hash_next_slot will eventually visit every slot, and we
391      know the table isn't full, this loop will terminate.  */
392   while (hash[slot])
393     slot = hash_next_slot (slot, hash_size);
394
395   return slot;
396 }
397
398
399 /* Return a new, empty hash table containing ELEMENTS elements.  This has
400    no effect on the LWP pool's global variables.  */
401 static struct lwp **
402 make_hash_table (size_t elements)
403 {
404   struct lwp **hash;
405   size_t size = elements * sizeof (*hash);
406
407   hash = malloc (size);
408   memset (hash, 0, size);
409
410   return hash;
411 }
412
413
414 /* Resize hash as needed to ensure that the table's population is
415    between 12.5% and 50% of its size.  */
416 static void
417 resize_hash (void)
418 {
419   struct lwp **new_hash;
420   size_t new_hash_size;
421   int new_hash_population; /* just for sanity checking */
422   int i;
423
424   /* Pick a new size.  */
425   new_hash_size = hash_size;
426   while (new_hash_size < hash_population * 2)
427     new_hash_size *= 2;
428   while (new_hash_size > minimum_hash_size
429          && new_hash_size > hash_population * 8)
430     new_hash_size /= 2;
431
432   /* We may have re-chosen the minimum table size.  */
433   if (new_hash_size == hash_size)
434     return;
435
436   new_hash = make_hash_table (new_hash_size);
437   new_hash_population = 0;
438
439   /* Re-insert all the old lwp's in the new table.  */
440   for (i = 0; i < hash_size; i++)
441     if (hash[i])
442       {
443         struct lwp *l = hash[i];
444         int new_slot = hash_slot (l->pid, new_hash_size);
445
446         while (new_hash[new_slot])
447           new_slot = hash_next_slot (new_slot, new_hash_size);
448
449         new_hash[new_slot] = l;
450         new_hash_population++;
451       }
452
453   if (new_hash_population != hash_population)
454     fprintf (stderr, "ERROR: rehashing changed population from %d to %d\n",
455              hash_population, new_hash_population);
456
457   /* Free the old table, and drop in the new one.  */
458   free (hash);
459   hash = new_hash;
460   hash_size = new_hash_size;
461 }
462
463
464 /* Find an existing hash table entry for LWP.  If there is none,
465    create one in state lwp_state_uninitialized.  */
466 static struct lwp *
467 hash_find (pid_t lwp)
468 {
469   int slot;
470   struct lwp *l;
471
472   /* Do we need to initialize the hash table?  */
473   if (! hash)
474     {
475       hash_size = minimum_hash_size;
476       hash = make_hash_table (hash_size);
477       hash_population = 0;
478     }
479
480   for (slot = hash_slot (lwp, hash_size);
481        hash[slot];
482        slot = hash_next_slot (slot, hash_size))
483     if (hash[slot]->pid == lwp)
484       return hash[slot];
485
486   /* There is no entry for this lwp.  Create one.  */
487   l = malloc (sizeof (*l));
488   l->pid = lwp;
489   l->state = lwp_state_uninitialized;
490   l->next = l->prev = NULL;
491   l->status = 42;
492
493   hash[slot] = l;
494   hash_population++;
495
496   /* Do we need to resize?  */
497   if (hash_size < hash_population * 2)
498     resize_hash ();
499
500   return l;
501 }
502
503
504 /* Remove the LWP L from the pool.  This does not free L itself.  */
505 static void
506 hash_delete (struct lwp *l)
507 {
508   int slot;
509
510   for (slot = hash_slot (l->pid, hash_size);
511        hash[slot];
512        slot = hash_next_slot (slot, hash_size))
513     if (hash[slot]->pid == l->pid)
514       break;
515
516   /* We shouldn't ever be asked to delete a 'struct lwp' that isn't in
517      the table.  */
518   assert (hash[slot]);
519
520   /* There should be only one 'struct lwp' with a given PID.  */
521   assert (hash[slot] == l);
522
523   /* Deleting from this kind of hash table is interesting, because of
524      the way we handle collisions.
525
526      For the sake of discussion, pretend that STRIDE is 1 (the
527      reasoning is basically the same either way, but this has less
528      hair).
529
530      When we search for an LWP that hashes to slot S, because there
531      may be collisions, the set of slots we'll actually search is the
532      contiguous run of non-empty table entries that starts at S,
533      heading towards higher indices (and possibly wrapping around at
534      the end of the table).  When we find an empty table entry, we
535      give up the search.
536
537      When we delete an LWP, if we simply set its slot to zero, that
538      could cause us to cut off later searches too early.  For example,
539      if three LWP's all hash to slot S, and have been placed in slots
540      S, S+1, and S+2, and we set slot S+1 to zero, then a search for
541      the LWP at S+2 will start at S, and then stop at S+1 without ever
542      seeing the right entry at S+2.
543
544      Some implementations place a special "deleted" marker in the slot
545      to let searches continue.  But then it's hard to ensure that the
546      table doesn't get choked with deleted markers; and should deleted
547      markers count towards the population for resizing purposes?  It's
548      a mess.
549
550      So after clearing a slot, we walk the remainder of the contiguous
551      run of entries and re-hash them all.  If the hash function is
552      doing a good job distributing entries across the table,
553      contiguous runs should be short.  And it had better be good,
554      because this is potentially quadratic.
555
556      Of course, if we're going to resize the table, that removes all
557      deleted elements, so we needn't bother with any of this.  */
558
559   hash[slot] = NULL;
560   hash_population--;
561
562   if (hash_size > minimum_hash_size
563       && hash_size > hash_population * 8)
564     resize_hash ();
565   else
566     for (slot = hash_next_slot (slot, hash_size);
567          hash[slot];
568          slot = hash_next_slot (slot, hash_size))
569       {
570         struct lwp *refugee = hash[slot];
571
572         hash[slot] = NULL;
573         hash[hash_empty_slot (refugee->pid)] = refugee;
574       }
575 }
576
577
578 \f
579 /* Queue functions.  */ 
580
581 /* Insert L at the end of the queue headed by QUEUE.  */ 
582 static void
583 queue_enqueue (struct lwp *queue, struct lwp *l)
584 {
585   assert (! l->next && ! l->prev);
586
587   l->next = queue;
588   l->prev = queue->prev;
589   l->prev->next = l;
590   l->next->prev = l;
591 }
592
593
594 /* If L is part of some queue, remove it.  */
595 static void
596 queue_delete (struct lwp *l)
597 {
598   assert (l->next && l->prev);
599
600   l->next->prev = l->prev;
601   l->prev->next = l->next;
602   l->next = l->prev = NULL;
603 }
604
605
606 /* Return non-zero if there is anything in QUEUE, zero otherwise.  */
607 static int
608 queue_non_empty (struct lwp *queue)
609 {
610   return queue->next != queue;
611 }
612
613
614 /* Return the first LWP from QUEUE, but don't remove it.  If QUEUE is
615    empty, return NULL.  */
616 static struct lwp *
617 queue_first (struct lwp *queue)
618 {
619   struct lwp *l = queue->next;
620
621   if (l != queue)
622     return l;
623   else
624     return NULL;
625 }
626
627
628 \f
629 /* Hashing LWP's, but with error checking and cleanup.  */
630
631
632 /* Add an entry for LWP to the pool and return it.  There should be no
633    existing entry for LWP; if there is, clean it up.  The returned
634    LWP's state is always lwp_state_uninitialized; the caller must
635    initialize the LWP before returning.  */
636 static struct lwp *
637 hash_find_new (pid_t lwp)
638 {
639   struct lwp *l = hash_find (lwp);
640
641   if (l->state != lwp_state_uninitialized)
642     {
643       fprintf (stderr, "ERROR: new LWP %d already in table\n", (int) lwp);
644
645       /* Remove ourselves from any queue we might be in.  */
646       if (l->next)
647         queue_delete (l);
648     }
649
650   l->state = lwp_state_uninitialized;
651
652   return l;
653 }
654
655
656 /* Find an entry for an existing LWP, and return it.  If we have no
657    existing entry for LWP, print an error message, but return the new,
658    uninitialized entry anyway.  */
659 static struct lwp *
660 hash_find_known (pid_t lwp)
661 {
662   struct lwp *l = hash_find (lwp);
663
664   if (l->state == lwp_state_uninitialized)
665     fprintf (stderr, "ERROR: unexpected lwp: %d\n", (int) lwp);
666
667   return l;
668 }
669
670
671 \f
672 /* Waiting.  */
673
674
675 /* The head of the queue of LWP's with interesting wait statuses.
676    Only the prev and next members are meaningful.
677
678    Every LWP in one of the lwp_state_*_interesting states should be on
679    this queue.  If an LWP's state is lwp_state_dead_interesting, the
680    LWP is not in the hash table any more.  */
681 static struct lwp interesting_queue
682 = { -1, 0, &interesting_queue, &interesting_queue, 42 };
683
684
685 static const char *
686 wait_status_str (int status)
687 {
688   static char buf[100];
689
690   if (WIFSTOPPED (status))
691     sprintf (buf, "WIFSTOPPED (s) && WSTOPSIG (s) == %d (%s)",
692              WSTOPSIG (status), strsignal (WSTOPSIG (status)));
693   else if (WIFEXITED (status))
694     sprintf (buf, "WIFEXITED (s) && WEXITSTATUS (s) == %d",
695              WEXITSTATUS (status));
696   else if (WIFSIGNALED (status))
697     sprintf (buf, "WIFSIGNALED (s) && WTERMSIG (s) == %d (%s)%s",
698              WTERMSIG (status),
699              strsignal (WTERMSIG (status)),
700              WCOREDUMP (status) ? " && WCOREDUMP(s)" : "");
701   else
702     sprintf (buf, "%d (unrecognized status)", status);
703
704   return buf;
705 }
706
707
708 static const char *
709 wait_flags_str (int flags)
710 {
711   static const struct {
712     int flag;
713     const char *name;
714   } flag_table[] = {
715     { WNOHANG, "WNOHANG" },
716     { WUNTRACED, "WUNTRACED" },
717 #ifdef __WCLONE
718     { __WCLONE, "__WCLONE" },
719 #endif
720 #ifdef __WALL
721     { __WALL, "__WALL" },
722 #endif
723 #ifdef __WNOTHREAD
724     { __WNOTHREAD, "__WNOTHREAD" },
725 #endif
726     { 0, 0 }
727   };
728   static char buf[100];
729   int i;
730
731   buf[0] = '\0';
732   for (i = 0; flag_table[i].flag; i++)
733     if (flags & flag_table[i].flag)
734       {
735         strcat (buf, flag_table[i].name);
736         flags &= ~flag_table[i].flag;
737         if (flags)
738           strcat (buf, " | ");
739       }
740
741   if (flags)
742     sprintf (buf + strlen (buf), "0x%x", (unsigned) flags);
743
744   if (buf[0] == '\0')
745     return "0";
746   else
747     return buf;
748 }
749
750
751 static const char *
752 lwp_state_str (enum lwp_state state)
753 {
754   switch (state)
755     {
756     case lwp_state_uninitialized:
757       return "uninitialized";
758     case lwp_state_running:
759       return "running";
760     case lwp_state_stopped:
761       return "stopped";
762     case lwp_state_stopped_interesting:
763       return "stopped_interesting";
764     case lwp_state_dead_interesting:
765       return "dead_interesting";
766     case lwp_state_running_stop_pending:
767       return "running_stop_pending";
768     case lwp_state_stopped_stop_pending:
769       return "stopped_stop_pending";
770     case lwp_state_stopped_stop_pending_interesting:
771       return "stopped_stop_pending_interesting";
772     default:
773       {
774         static char buf[100];
775         sprintf (buf, "%d (unrecognized lwp_state)", state);
776         return buf;
777       }
778     }
779 }
780
781
782 static void
783 debug_report_state_change (struct gdbserv *serv,
784                            pid_t lwp,
785                            enum lwp_state old,
786                            enum lwp_state new)
787 {
788   if (debug_lwp_pool && old != new)
789     {
790       fprintf (stderr,
791                "%32s -- %5d -> %s",
792                lwp_state_str (old), (int) lwp, lwp_state_str (new));
793       if (new == lwp_state_stopped)
794         fprintf (stderr, "    (at %#lx)", debug_get_pc (serv, lwp));
795       fprintf (stderr, "\n");
796     }
797 }
798
799
800 /* Wait for a status from the LWP L (or any LWP, if L is NULL),
801    passing FLAGS to waitpid, and record the resulting wait status in
802    the LWP pool appropriately.
803
804    If no wait status was available (if FLAGS & WNOHANG), return zero.
805    If we successfully processed some wait status, return 1.  If an
806    error occurs, set errno and return -1.
807
808    If waitpid returns an error, print a message to stderr.  */
809 static int
810 wait_and_handle (struct gdbserv *serv, struct lwp *l, int flags)
811 {
812   int status;
813   pid_t new_pid; 
814   enum lwp_state old_state;
815   
816   /* We can only wait for LWP's that are running.  */
817   if (l)
818     assert (l->state == lwp_state_running
819             || l->state == lwp_state_running_stop_pending);
820
821   /* This should be the only call to waitpid in this module, to ensure
822      that we always keep each LWP's state up to date.  In fact, it
823      should be the only call to waitpid used by any module using the
824      LWP pool code at all.  */
825   new_pid = waitpid (l ? l->pid : -1, &status, flags);
826
827   if (debug_lwp_pool)
828     {
829       fprintf (stderr,
830                "lwp_pool: wait_and_handle: waitpid (%d, %s, %s) == %d\n",
831                l ? l->pid : -1,
832                (new_pid <= 0 ? "(unset)" : wait_status_str (status)),
833                wait_flags_str (flags),
834                new_pid);
835     }
836
837   if (new_pid == -1)
838     {
839       /* If we call fprintf, that'll wipe out the value of errno.  */
840       int saved_errno = errno;
841
842       fprintf (stderr, "ERROR: waitpid (%d) failed: %s\n",
843                l ? (int) l->pid : -1,
844                strerror (saved_errno));
845
846       errno = saved_errno;
847       return -1;
848     }
849
850   if (new_pid == 0)
851     /* No status, so no LWP has changed state.  */
852     return 0;
853
854   if (l)
855     {
856       if (l->pid != new_pid)
857         {
858           fprintf (stderr, "ERROR: waited for %d, but got %d\n",
859                    l->pid, new_pid);
860           l = hash_find_known (new_pid);
861         }
862     }
863   else
864     l = hash_find_known (new_pid);
865
866   old_state = l->state;
867   
868   l->status = status;
869
870   if (WIFEXITED (status) || WIFSIGNALED (status))
871     {
872       /* Remove dead LWP's from the hash table, and put them in the
873          interesting queue.  */
874       hash_delete (l);
875       l->state = lwp_state_dead_interesting;
876       if (l->next)
877         queue_delete (l);
878       queue_enqueue (&interesting_queue, l);
879     }
880   else
881     {
882       int stopsig;
883
884       assert (WIFSTOPPED (status));
885       
886       stopsig = WSTOPSIG (status);
887
888       if (stopsig == SIGTRAP)
889         {
890           /* No longer stepping once a SIGTRAP is received.  */
891           l->do_step = 0;
892         }
893
894       switch (l->state)
895         {
896         case lwp_state_uninitialized:
897           /* Might as well clean it up.  */
898         case lwp_state_running:
899           /* It stopped, but not because of anything we did, so it's
900              interesting even if it was a SIGSTOP.  */
901           l->state = lwp_state_stopped_interesting;
902           queue_enqueue (&interesting_queue, l);
903           break;
904
905         case lwp_state_running_stop_pending:
906
907           /* If we were in stopping_queue, we're stopped now.  */
908           if (l->next)
909             queue_delete (l);
910
911           /* We are expecting a boring SIGSTOP.  Is this it?  */
912           if (stopsig == SIGSTOP)
913             l->state = lwp_state_stopped;
914           else
915             {
916               /* Report this status, but remember that we're still
917                  expecting the boring SIGSTOP.  */
918               l->state = lwp_state_stopped_stop_pending_interesting;
919               queue_enqueue (&interesting_queue, l);
920             }
921           break;
922
923         default:
924           /* The assert at top should prevent any other states from
925              showing up here.  */
926           fprintf (stderr, "ERROR: called waitpid on LWP %d in bad state %s\n",
927                    (int) l->pid, lwp_state_str (l->state));
928           abort ();
929           break;
930         }
931     }
932
933   debug_report_state_change (serv, l->pid, old_state, l->state);
934
935   return 1;
936 }
937
938
939 /* Wait for a pending stop on the running LWP L.  Return non-zero if L
940    ends up in an interesting state, or zero if L ends up in
941    lwp_state_stopped.
942
943    Whenever we have an LWP with no interesting status, but with a stop
944    pending, we can always wait on it:
945
946    - Since SIGCONT can't be blocked, caught, or ignored, the wait will
947      always return immediately.  The process won't run amok.
948
949    - Since the LWP is uninteresting to begin with, we'll end up with
950      at most one interesting wait status to report; no need to queue
951      up multiple statuses per LWP (which we'd rather not implement if
952      we can avoid it).
953
954    So, this function takes an LWP in lwp_state_running_stop_pending,
955    and puts that LWP in either lwp_state_stopped (no stop pending) or
956    some INTERESTING state.  It's really just wait_and_handle, with
957    some error checking wrapped around it.  */
958 static int
959 check_stop_pending (struct gdbserv *serv, struct lwp *l)
960 {
961   assert (l->state == lwp_state_running_stop_pending);
962
963   wait_and_handle (serv, l, __WALL);
964
965   switch (l->state)
966     {
967     case lwp_state_stopped:
968       return 0;
969
970     case lwp_state_stopped_stop_pending_interesting:
971     case lwp_state_dead_interesting:
972       return 1;
973
974     case lwp_state_stopped_interesting:
975       /* This state shouldn't happen: since there was a pending stop,
976          a single waitpid on that LWP should have either gotten the
977          SIGSTOP, yielding 'lwp_state_stopped', or something interesting,
978          yielding 'lwp_state_stopped_stop_pending_interesting'.  */
979     default:
980       fprintf (stderr,
981                "ERROR: checking lwp %d for pending stop yielded "
982                "bad state %s\n",
983                (int) l->pid, lwp_state_str (l->state));
984       hash_delete (l);
985       if (l->next)
986         queue_delete (l);
987       free (l);
988       return 0;
989     }
990 }
991
992
993 pid_t
994 lwp_pool_waitpid (struct gdbserv *serv, pid_t pid, int *stat_loc, int options)
995 {
996   struct lwp *l;
997   enum lwp_state old_state;
998   
999   if (debug_lwp_pool)
1000     fprintf (stderr, "lwp_pool_waitpid (%d, stat_loc, %s)\n",
1001              (int) pid, wait_flags_str (options));
1002
1003   /* Check that we're not being passed arguments that would be
1004      meaningful for the real waitpid, but that we can't handle.  */
1005   assert (pid == -1 || pid > 0);
1006   assert (! (options & ~WNOHANG));
1007
1008   /* Do the wait, and choose an LWP to report on.  */
1009   if (pid == -1)
1010     {
1011       /* Handle wait statuses of any sort until something appears on
1012          the interesting queue.  */
1013       while (! queue_non_empty (&interesting_queue))
1014         {
1015           int result = wait_and_handle (serv, NULL, options | __WALL);
1016
1017           if (result <= 0)
1018             return result;
1019         }
1020
1021       l = queue_first (&interesting_queue);
1022     }
1023   else
1024     {
1025       /* Waiting for a status from a specific pid PID.  */
1026       l = hash_find_known (pid);
1027
1028       /* We should only wait for known, running LWP's.  */
1029       assert (l->state == lwp_state_running
1030               || l->state == lwp_state_running_stop_pending);
1031
1032       /* Wait until this pid is no longer running.  */
1033       while (l->state == lwp_state_running
1034              || l->state == lwp_state_running_stop_pending)
1035         {
1036           int result = wait_and_handle (serv, l, options | __WALL);
1037
1038           if (result <= 0)
1039             return result;
1040         }
1041     }
1042
1043   /* Gather info from L early, in case we free it.  */
1044   pid = l->pid;
1045   old_state = l->state;
1046   if (stat_loc)
1047     *stat_loc = l->status;
1048
1049   /* The INTERESTING states specifically mean that the LWP has a
1050      status which should be reported to the user, but that hasn't been
1051      yet.  Now we're about to report that status, so we need to mark
1052      interesting LWP's as uninteresting.  */
1053   switch (l->state)
1054     {
1055     case lwp_state_uninitialized:
1056     case lwp_state_running:
1057     case lwp_state_stopped:
1058     case lwp_state_stopped_stop_pending:
1059     case lwp_state_running_stop_pending:
1060       /* These are uninteresting states.  The waiting code above
1061          should never have chosen an LWP in one of these states.  */
1062       fprintf (stderr,
1063                "ERROR: %s: selected uninteresting LWP %d state %s\n",
1064                __func__, l->pid, lwp_state_str (l->state));
1065       abort ();
1066       break;
1067
1068     case lwp_state_stopped_interesting:
1069       /* Now that we've reported this wait status to the user, the LWP
1070          is not interesting any more.  */
1071       l->state = lwp_state_stopped;
1072       queue_delete (l);
1073       debug_report_state_change (serv, l->pid, old_state, l->state);
1074       break;
1075
1076     case lwp_state_dead_interesting:
1077       /* Once we've reported this status, we have washed our hands of
1078          this LWP entirely.  */
1079       queue_delete (l);
1080       free (l);
1081       if (debug_lwp_pool)
1082         fprintf (stderr, 
1083                  "lwp_pool: %s: LWP %d state dead_interesting -> freed\n",
1084                  __func__, pid);
1085       break;
1086
1087     case lwp_state_stopped_stop_pending_interesting:
1088       /* We're about to report this LWP's status, making it
1089          uninteresting, but it's still got a stop pending.  */
1090       queue_delete (l);
1091       l->state = lwp_state_stopped_stop_pending;
1092       debug_report_state_change (serv, l->pid, old_state, l->state);
1093       break;
1094
1095     default:
1096       fprintf (stderr, "ERROR: lwp %d in bad state: %s\n",
1097                (int) l->pid, lwp_state_str (l->state));
1098       abort ();
1099       break;
1100     }
1101
1102   return pid;
1103 }
1104
1105
1106 \f
1107 /* Stopping and continuing.  */
1108
1109
1110 void
1111 lwp_pool_stop_all (struct gdbserv *serv)
1112 {
1113   int i;
1114
1115   if (debug_lwp_pool)
1116     fprintf (stderr, "lwp_pool_stop_all ()\n");
1117
1118   /* The head of the queue of running LWP's that we are stopping.
1119      Only the prev and next members are meaningful.  */
1120   struct lwp stopping_queue;
1121
1122   stopping_queue.next = stopping_queue.prev = &stopping_queue;
1123
1124   /* First, put every LWP that's not already STOPPED or DEAD in a STOP
1125      PENDING state, and put them all on stopping_queue.  */ 
1126   for (i = 0; i < hash_size; i++)
1127     {
1128       struct lwp *l = hash[i];
1129
1130       if (l)
1131         {
1132           enum lwp_state old_state = l->state;
1133
1134           switch (l->state)
1135             {
1136               /* There should never be 'uninitialized' entries left in
1137                  the table.  Whoever created them ought to have put them
1138                  in some meaningful state before returning.  */
1139             case lwp_state_uninitialized:
1140               assert (l->state != lwp_state_uninitialized);
1141               break;
1142
1143             case lwp_state_running:
1144               /* A 'no such process' error here indicates an NPTL thread
1145                  that has exited.  */
1146               kill_lwp (l->pid, SIGSTOP);
1147               l->state = lwp_state_running_stop_pending;
1148               queue_enqueue (&stopping_queue, l);
1149               break;
1150
1151             case lwp_state_stopped:
1152             case lwp_state_stopped_stop_pending:
1153             case lwp_state_stopped_interesting:
1154             case lwp_state_dead_interesting:
1155             case lwp_state_stopped_stop_pending_interesting:
1156               /* Nothing needs to be done here.  */
1157               break;
1158
1159             case lwp_state_running_stop_pending:
1160               /* LWPs should never be in this state between calls to
1161                  public lwp_pool functions.  */
1162               assert (l->state != lwp_state_running_stop_pending);
1163               break;
1164
1165             default:
1166               fprintf (stderr, "ERROR: lwp %d in bad state: %s\n",
1167                        (int) l->pid, lwp_state_str (l->state));
1168               abort ();
1169               break;
1170             }
1171
1172           debug_report_state_change (serv, l->pid, old_state, l->state);
1173         }
1174     }
1175
1176   /* Gather wait results until the stopping queue is empty.  */
1177   while (queue_non_empty (&stopping_queue))
1178     if (wait_and_handle (serv, NULL, __WALL) < 0)
1179       {
1180         fprintf (stderr, "ERROR: lwp_pool_stop_all wait failed: %s",
1181                  strerror (errno));
1182         return;
1183       }
1184
1185   /* Now all LWPs should be stopped or dead.  But let's check.  */
1186   for (i = 0; i < hash_size; i++)
1187     {
1188       struct lwp *l = hash[i];
1189       if (l)
1190         switch (l->state)
1191           {
1192           case lwp_state_uninitialized:
1193             assert (l->state != lwp_state_uninitialized);
1194             break;
1195
1196           case lwp_state_running:
1197           case lwp_state_running_stop_pending:
1198             fprintf (stderr,
1199                      "ERROR: lwp_pool_stop_all failed: LWP %d still running\n",
1200                      (int) l->pid);
1201             break;
1202
1203           case lwp_state_stopped:
1204           case lwp_state_stopped_stop_pending:
1205           case lwp_state_stopped_interesting:
1206           case lwp_state_dead_interesting:
1207           case lwp_state_stopped_stop_pending_interesting:
1208             /* That's all as it should be.  */
1209             break;
1210
1211           default:
1212             fprintf (stderr, "ERROR: lwp %d in bad state: %s\n",
1213                      (int) l->pid, lwp_state_str (l->state));
1214             abort ();
1215             break;
1216           }
1217     }
1218 }
1219
1220 int
1221 continue_or_step_lwp (struct gdbserv *serv, struct lwp *l, int sig)
1222 {
1223   int status;
1224   if (l->do_step)
1225     status = singlestep_lwp (serv, l->pid, sig);
1226   else
1227     status = continue_lwp (l->pid, sig);
1228
1229   return status;
1230 }
1231
1232
1233 void
1234 lwp_pool_continue_all (struct gdbserv *serv)
1235 {
1236   int i;
1237
1238   if (debug_lwp_pool)
1239     fprintf (stderr, "lwp_pool_continue_all ()\n");
1240
1241   /* This loop makes every LWP either INTERESTING, or RUNNING.  */
1242   for (i = 0; i < hash_size; i++)
1243     {
1244       struct lwp *l = hash[i];
1245
1246       if (l)
1247         {
1248           enum lwp_state old_state = l->state;
1249
1250           switch (l->state)
1251             {
1252               /* There should never be 'uninitialized' entries left in
1253                  the table.  Whoever created them ought to have put them
1254                  in some meaningful state before returning.  */
1255             case lwp_state_uninitialized:
1256               assert (l->state != lwp_state_uninitialized);
1257               break;
1258
1259             case lwp_state_running:
1260               /* It's already running, so nothing needs to be done.  */
1261               break;
1262
1263             case lwp_state_stopped:
1264               if (continue_or_step_lwp (serv, l, 0) == 0)
1265                 l->state = lwp_state_running;
1266               break;
1267
1268             case lwp_state_stopped_interesting:
1269             case lwp_state_dead_interesting:
1270             case lwp_state_stopped_stop_pending_interesting:
1271               /* We still have an unreported wait status here, so leave it
1272                  alone; we'll report it.  */
1273               break;
1274
1275             case lwp_state_running_stop_pending:
1276               /* There shouldn't be any LWPs in this state at this
1277                  point.  We should be calling check_stop_pending or
1278                  wait_and_handle as soon as we create them.  */
1279               assert (l->state != lwp_state_running_stop_pending);
1280               break;
1281
1282             case lwp_state_stopped_stop_pending:
1283               /* Continue it, and then wait for the pending stop.
1284                  Since SIGSTOP cannot be blocked, caught, or ignored,
1285                  the wait will always return immediately; the LWP
1286                  won't run amok.  */
1287               if (continue_lwp (l->pid, 0) == 0)
1288                 {
1289                   l->state = lwp_state_running_stop_pending;
1290                   if (check_stop_pending (serv, l) == 0)
1291                     {
1292                       if (continue_or_step_lwp (serv, l, 0) == 0)
1293                         l->state = lwp_state_running;
1294                     }
1295                 }
1296               break;
1297
1298             default:
1299               fprintf (stderr, "ERROR: lwp %d in bad state: %s\n", 
1300                        (int) l->pid, lwp_state_str (l->state));
1301               abort ();
1302               break;
1303             }
1304
1305           debug_report_state_change (serv, l->pid, old_state, l->state);
1306         }
1307     }
1308 }
1309
1310
1311 int
1312 lwp_pool_continue_lwp (struct gdbserv *serv, pid_t pid, int signal)
1313 {
1314   struct lwp *l = hash_find_known (pid);
1315   enum lwp_state old_state = l->state;
1316   int result = 0;
1317
1318   if (debug_lwp_pool)
1319     fprintf (stderr, "lwp_pool_continue_lwp (%d, %d)\n",
1320              (int) pid, signal);
1321
1322   switch (l->state)
1323     {
1324     case lwp_state_uninitialized:
1325       assert (l->state != lwp_state_uninitialized);
1326       break;
1327
1328       /* We should only be continuing LWPs that have reported a
1329          WIFSTOPPED status via lwp_pool_waitpid and have not been
1330          continued or singlestepped since.  */
1331     case lwp_state_running:
1332     case lwp_state_stopped_interesting:
1333     case lwp_state_dead_interesting:
1334     case lwp_state_running_stop_pending:
1335     case lwp_state_stopped_stop_pending_interesting:
1336       fprintf (stderr, "ERROR: continuing LWP %d in unwaited state: %s\n",
1337                (int) l->pid, lwp_state_str (l->state));
1338       break;
1339
1340     case lwp_state_stopped:
1341       result = continue_or_step_lwp (serv, l, signal);
1342       if (result == 0)
1343         l->state = lwp_state_running;
1344       break;
1345
1346     case lwp_state_stopped_stop_pending:
1347       /* Continue it, delivering the given signal, and then wait for
1348          the pending stop.  Since SIGSTOP cannot be blocked, caught,
1349          or ignored, the wait will always return immediately; the LWP
1350          won't run amok.
1351
1352          We must deliver the signal with the first continue_lwp call;
1353          if check_stop_pending says the LWP has a new interesting
1354          status, then we'll never reach the second continue_lwp, and
1355          we'll lose our chance to deliver the signal.  */
1356       if (continue_lwp (l->pid, signal) == 0)
1357         {
1358           l->state = lwp_state_running_stop_pending;
1359           if (check_stop_pending (serv, l) == 0)
1360             {
1361               if (continue_or_step_lwp (serv, l, 0) == 0)
1362                 l->state = lwp_state_running;
1363             }
1364         }
1365       break;
1366
1367     default:
1368       fprintf (stderr, "ERROR: lwp %d in bad state: %s\n", 
1369                (int) l->pid, lwp_state_str (l->state));
1370       abort ();
1371       break;
1372     }
1373
1374   debug_report_state_change (serv, l->pid, old_state, l->state);
1375
1376   return result;
1377 }
1378
1379
1380 int
1381 lwp_pool_singlestep_lwp (struct gdbserv *serv, pid_t lwp, int signal)
1382 {
1383   struct lwp *l = hash_find_known (lwp);
1384   enum lwp_state old_state = l->state;
1385   int result = 0;
1386
1387   if (debug_lwp_pool)
1388     fprintf (stderr, "lwp_pool_singlestep_lwp (%p, %d, %d)\n",
1389              serv, (int) lwp, signal);
1390
1391   switch (l->state)
1392     {
1393     case lwp_state_uninitialized:
1394       assert (l->state != lwp_state_uninitialized);
1395       break;
1396
1397       /* We should only be stepping LWPs that have reported a
1398          WIFSTOPPED status via lwp_pool_waitpid and have not been
1399          continued or singlestepped since.  */
1400     case lwp_state_running:
1401     case lwp_state_stopped_interesting:
1402     case lwp_state_dead_interesting:
1403     case lwp_state_running_stop_pending:
1404     case lwp_state_stopped_stop_pending_interesting:
1405       fprintf (stderr, "ERROR: stepping LWP %d in unwaited state: %s\n",
1406                (int) l->pid, lwp_state_str (l->state));
1407       break;
1408
1409     case lwp_state_stopped:
1410       result = singlestep_lwp (serv, l->pid, signal);
1411       if (result == 0)
1412         {
1413           l->state = lwp_state_running;
1414           l->do_step = 1;
1415         }
1416       break;
1417
1418     case lwp_state_stopped_stop_pending:
1419       /* Continue it, delivering the given signal, and then wait for
1420          the pending stop.  Since SIGSTOP cannot be blocked, caught,
1421          or ignored, the wait will always return immediately; the LWP
1422          won't run amok.
1423
1424          We must deliver the signal with the continue_lwp call; if
1425          check_stop_pending says the LWP has a new interesting status,
1426          then we'll never reach the singlestep_lwp, and we'll lose our
1427          chance to deliver the signal at all.  */
1428       if (continue_lwp (l->pid, signal) == 0)
1429         {
1430           l->state = lwp_state_running_stop_pending;
1431           if (check_stop_pending (serv, l) == 0)
1432             {
1433               if (singlestep_lwp (serv, l->pid, 0) == 0)
1434                 {
1435                   l->state = lwp_state_running;
1436                   l->do_step = 1;
1437                 }
1438             }
1439         }
1440       break;
1441
1442     default:
1443       fprintf (stderr, "ERROR: lwp %d in bad state: %s\n", 
1444                (int) l->pid, lwp_state_str (l->state));
1445       abort ();
1446       break;
1447     }
1448
1449   debug_report_state_change (serv, l->pid, old_state, l->state);
1450
1451   return result;
1452 }
1453
1454
1455 \f
1456 /* Adding new LWP's to the pool.  */
1457
1458 void
1459 lwp_pool_new_stopped (pid_t pid)
1460 {
1461   struct lwp *l = hash_find_new (pid);
1462
1463   if (debug_lwp_pool)
1464     fprintf (stderr, "lwp_pool_new_stopped (%d)\n", (int) pid);
1465
1466   l->state = lwp_state_stopped;
1467
1468   if (debug_lwp_pool)
1469     fprintf (stderr, "lwp_pool: %s: new LWP %d state %s\n",
1470              __func__, l->pid, lwp_state_str (l->state));
1471 }
1472
1473
1474 int
1475 lwp_pool_attach (struct gdbserv *serv, pid_t pid)
1476 {
1477   /* Are we already managing this LWP?  */
1478   struct lwp *l = hash_find (pid);
1479
1480   if (debug_lwp_pool)
1481     fprintf (stderr, "lwp_pool_attach (%d)\n", (int) pid);
1482
1483   if (l->state == lwp_state_uninitialized)
1484     {
1485       /* No, we really need to attach to it.  */
1486       int status = attach_lwp (pid);
1487
1488       if (status)
1489         {
1490           /* Forget about the lwp.  */
1491           hash_delete (l);
1492           free (l);
1493           return status;
1494         }
1495
1496       /* Since we attached to it, we'll get a SIGSTOP for this
1497          eventually.  Wait for it now, to put it in either
1498          lwp_state_stopped, or in some interesting state.  */
1499       l->state = lwp_state_running_stop_pending;
1500
1501       if (debug_lwp_pool)
1502         fprintf (stderr, "lwp_pool: %s: new LWP %d state %s\n",
1503                  __func__, l->pid, lwp_state_str (l->state));
1504
1505       check_stop_pending (serv, l);
1506
1507       return 1;
1508     }
1509      
1510   return 0;
1511 }