OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [gdb/] [thread.c] - Blame information for rev 842

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/* Multi-process/thread control for GDB, the GNU debugger.
2
 
3
   Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4
   2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010
5
   Free Software Foundation, Inc.
6
 
7
   Contributed by Lynx Real-Time Systems, Inc.  Los Gatos, CA.
8
 
9
   This file is part of GDB.
10
 
11
   This program is free software; you can redistribute it and/or modify
12
   it under the terms of the GNU General Public License as published by
13
   the Free Software Foundation; either version 3 of the License, or
14
   (at your option) any later version.
15
 
16
   This program is distributed in the hope that it will be useful,
17
   but WITHOUT ANY WARRANTY; without even the implied warranty of
18
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
   GNU General Public License for more details.
20
 
21
   You should have received a copy of the GNU General Public License
22
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
 
24
#include "defs.h"
25
#include "symtab.h"
26
#include "frame.h"
27
#include "inferior.h"
28
#include "environ.h"
29
#include "value.h"
30
#include "target.h"
31
#include "gdbthread.h"
32
#include "exceptions.h"
33
#include "command.h"
34
#include "gdbcmd.h"
35
#include "regcache.h"
36
#include "gdb.h"
37
#include "gdb_string.h"
38
 
39
#include <ctype.h>
40
#include <sys/types.h>
41
#include <signal.h>
42
#include "ui-out.h"
43
#include "observer.h"
44
#include "annotate.h"
45
#include "cli/cli-decode.h"
46
 
47
/* Definition of struct thread_info exported to gdbthread.h */
48
 
49
/* Prototypes for exported functions. */
50
 
51
void _initialize_thread (void);
52
 
53
/* Prototypes for local functions. */
54
 
55
static struct thread_info *thread_list = NULL;
56
static int highest_thread_num;
57
 
58
static void thread_command (char *tidstr, int from_tty);
59
static void thread_apply_all_command (char *, int);
60
static int thread_alive (struct thread_info *);
61
static void info_threads_command (char *, int);
62
static void thread_apply_command (char *, int);
63
static void restore_current_thread (ptid_t);
64
static void prune_threads (void);
65
 
66
/* Frontend view of the thread state.  Possible extensions: stepping,
67
   finishing, until(ling),...  */
68
enum thread_state
69
{
70
  THREAD_STOPPED,
71
  THREAD_RUNNING,
72
  THREAD_EXITED,
73
};
74
 
75
struct thread_info*
76
inferior_thread (void)
77
{
78
  struct thread_info *tp = find_thread_ptid (inferior_ptid);
79
  gdb_assert (tp);
80
  return tp;
81
}
82
 
83
void
84
delete_step_resume_breakpoint (struct thread_info *tp)
85
{
86
  if (tp && tp->step_resume_breakpoint)
87
    {
88
      delete_breakpoint (tp->step_resume_breakpoint);
89
      tp->step_resume_breakpoint = NULL;
90
    }
91
}
92
 
93
static void
94
clear_thread_inferior_resources (struct thread_info *tp)
95
{
96
  /* NOTE: this will take care of any left-over step_resume breakpoints,
97
     but not any user-specified thread-specific breakpoints.  We can not
98
     delete the breakpoint straight-off, because the inferior might not
99
     be stopped at the moment.  */
100
  if (tp->step_resume_breakpoint)
101
    {
102
      tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
103
      tp->step_resume_breakpoint = NULL;
104
    }
105
 
106
  bpstat_clear (&tp->stop_bpstat);
107
 
108
  discard_all_intermediate_continuations_thread (tp);
109
  discard_all_continuations_thread (tp);
110
}
111
 
112
static void
113
free_thread (struct thread_info *tp)
114
{
115
  clear_thread_inferior_resources (tp);
116
 
117
  if (tp->private)
118
    {
119
      if (tp->private_dtor)
120
        tp->private_dtor (tp->private);
121
      else
122
        xfree (tp->private);
123
    }
124
 
125
  xfree (tp);
126
}
127
 
128
void
129
init_thread_list (void)
130
{
131
  struct thread_info *tp, *tpnext;
132
 
133
  highest_thread_num = 0;
134
 
135
  if (!thread_list)
136
    return;
137
 
138
  for (tp = thread_list; tp; tp = tpnext)
139
    {
140
      tpnext = tp->next;
141
      free_thread (tp);
142
    }
143
 
144
  thread_list = NULL;
145
}
146
 
147
/* Allocate a new thread with target id PTID and add it to the thread
148
   list.  */
149
 
150
static struct thread_info *
151
new_thread (ptid_t ptid)
152
{
153
  struct thread_info *tp;
154
 
155
  tp = xcalloc (1, sizeof (*tp));
156
 
157
  tp->ptid = ptid;
158
  tp->num = ++highest_thread_num;
159
  tp->next = thread_list;
160
  thread_list = tp;
161
 
162
  /* Nothing to follow yet.  */
163
  tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
164
  tp->state_ = THREAD_STOPPED;
165
 
166
  return tp;
167
}
168
 
169
struct thread_info *
170
add_thread_silent (ptid_t ptid)
171
{
172
  struct thread_info *tp;
173
 
174
  tp = find_thread_ptid (ptid);
175
  if (tp)
176
    /* Found an old thread with the same id.  It has to be dead,
177
       otherwise we wouldn't be adding a new thread with the same id.
178
       The OS is reusing this id --- delete it, and recreate a new
179
       one.  */
180
    {
181
      /* In addition to deleting the thread, if this is the current
182
         thread, then we need to take care that delete_thread doesn't
183
         really delete the thread if it is inferior_ptid.  Create a
184
         new template thread in the list with an invalid ptid, switch
185
         to it, delete the original thread, reset the new thread's
186
         ptid, and switch to it.  */
187
 
188
      if (ptid_equal (inferior_ptid, ptid))
189
        {
190
          tp = new_thread (ptid);
191
 
192
          /* Make switch_to_thread not read from the thread.  */
193
          tp->state_ = THREAD_EXITED;
194
          switch_to_thread (minus_one_ptid);
195
 
196
          /* Now we can delete it.  */
197
          delete_thread (ptid);
198
 
199
          /* Now reset its ptid, and reswitch inferior_ptid to it.  */
200
          tp->ptid = ptid;
201
          tp->state_ = THREAD_STOPPED;
202
          switch_to_thread (ptid);
203
 
204
          observer_notify_new_thread (tp);
205
 
206
          /* All done.  */
207
          return tp;
208
        }
209
      else
210
        /* Just go ahead and delete it.  */
211
        delete_thread (ptid);
212
    }
213
 
214
  tp = new_thread (ptid);
215
  observer_notify_new_thread (tp);
216
 
217
  return tp;
218
}
219
 
220
struct thread_info *
221
add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
222
{
223
  struct thread_info *result = add_thread_silent (ptid);
224
 
225
  result->private = private;
226
 
227
  if (print_thread_events)
228
    printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
229
 
230
  annotate_new_thread ();
231
  return result;
232
}
233
 
234
struct thread_info *
235
add_thread (ptid_t ptid)
236
{
237
  return add_thread_with_info (ptid, NULL);
238
}
239
 
240
/* Delete thread PTID.  If SILENT, don't notify the observer of this
241
   exit.  */
242
static void
243
delete_thread_1 (ptid_t ptid, int silent)
244
{
245
  struct thread_info *tp, *tpprev;
246
 
247
  tpprev = NULL;
248
 
249
  for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
250
    if (ptid_equal (tp->ptid, ptid))
251
      break;
252
 
253
  if (!tp)
254
    return;
255
 
256
  /* If this is the current thread, or there's code out there that
257
     relies on it existing (refcount > 0) we can't delete yet.  Mark
258
     it as exited, and notify it.  */
259
  if (tp->refcount > 0
260
      || ptid_equal (tp->ptid, inferior_ptid))
261
    {
262
      if (tp->state_ != THREAD_EXITED)
263
        {
264
          observer_notify_thread_exit (tp, silent);
265
 
266
          /* Tag it as exited.  */
267
          tp->state_ = THREAD_EXITED;
268
 
269
          /* Clear breakpoints, etc. associated with this thread.  */
270
          clear_thread_inferior_resources (tp);
271
        }
272
 
273
       /* Will be really deleted some other time.  */
274
       return;
275
     }
276
 
277
  if (tpprev)
278
    tpprev->next = tp->next;
279
  else
280
    thread_list = tp->next;
281
 
282
  /* Notify thread exit, but only if we haven't already.  */
283
  if (tp->state_ != THREAD_EXITED)
284
    observer_notify_thread_exit (tp, silent);
285
 
286
  free_thread (tp);
287
}
288
 
289
/* Delete thread PTID and notify of thread exit.  If this is
290
   inferior_ptid, don't actually delete it, but tag it as exited and
291
   do the notification.  If PTID is the user selected thread, clear
292
   it.  */
293
void
294
delete_thread (ptid_t ptid)
295
{
296
  delete_thread_1 (ptid, 0 /* not silent */);
297
}
298
 
299
void
300
delete_thread_silent (ptid_t ptid)
301
{
302
  delete_thread_1 (ptid, 1 /* silent */);
303
}
304
 
305
struct thread_info *
306
find_thread_id (int num)
307
{
308
  struct thread_info *tp;
309
 
310
  for (tp = thread_list; tp; tp = tp->next)
311
    if (tp->num == num)
312
      return tp;
313
 
314
  return NULL;
315
}
316
 
317
/* Find a thread_info by matching PTID.  */
318
struct thread_info *
319
find_thread_ptid (ptid_t ptid)
320
{
321
  struct thread_info *tp;
322
 
323
  for (tp = thread_list; tp; tp = tp->next)
324
    if (ptid_equal (tp->ptid, ptid))
325
      return tp;
326
 
327
  return NULL;
328
}
329
 
330
/*
331
 * Thread iterator function.
332
 *
333
 * Calls a callback function once for each thread, so long as
334
 * the callback function returns false.  If the callback function
335
 * returns true, the iteration will end and the current thread
336
 * will be returned.  This can be useful for implementing a
337
 * search for a thread with arbitrary attributes, or for applying
338
 * some operation to every thread.
339
 *
340
 * FIXME: some of the existing functionality, such as
341
 * "Thread apply all", might be rewritten using this functionality.
342
 */
343
 
344
struct thread_info *
345
iterate_over_threads (int (*callback) (struct thread_info *, void *),
346
                      void *data)
347
{
348
  struct thread_info *tp, *next;
349
 
350
  for (tp = thread_list; tp; tp = next)
351
    {
352
      next = tp->next;
353
      if ((*callback) (tp, data))
354
        return tp;
355
    }
356
 
357
  return NULL;
358
}
359
 
360
int
361
thread_count (void)
362
{
363
  int result = 0;
364
  struct thread_info *tp;
365
 
366
  for (tp = thread_list; tp; tp = tp->next)
367
    ++result;
368
 
369
  return result;
370
}
371
 
372
int
373
valid_thread_id (int num)
374
{
375
  struct thread_info *tp;
376
 
377
  for (tp = thread_list; tp; tp = tp->next)
378
    if (tp->num == num)
379
      return 1;
380
 
381
  return 0;
382
}
383
 
384
int
385
pid_to_thread_id (ptid_t ptid)
386
{
387
  struct thread_info *tp;
388
 
389
  for (tp = thread_list; tp; tp = tp->next)
390
    if (ptid_equal (tp->ptid, ptid))
391
      return tp->num;
392
 
393
  return 0;
394
}
395
 
396
ptid_t
397
thread_id_to_pid (int num)
398
{
399
  struct thread_info *thread = find_thread_id (num);
400
  if (thread)
401
    return thread->ptid;
402
  else
403
    return pid_to_ptid (-1);
404
}
405
 
406
int
407
in_thread_list (ptid_t ptid)
408
{
409
  struct thread_info *tp;
410
 
411
  for (tp = thread_list; tp; tp = tp->next)
412
    if (ptid_equal (tp->ptid, ptid))
413
      return 1;
414
 
415
  return 0;                      /* Never heard of 'im */
416
}
417
 
418
/* Finds the first thread of the inferior given by PID.  If PID is -1,
419
   return the first thread in the list.  */
420
 
421
struct thread_info *
422
first_thread_of_process (int pid)
423
{
424
  struct thread_info *tp, *ret = NULL;
425
 
426
  for (tp = thread_list; tp; tp = tp->next)
427
    if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
428
      if (ret == NULL || tp->num < ret->num)
429
        ret = tp;
430
 
431
  return ret;
432
}
433
 
434
struct thread_info *
435
any_thread_of_process (int pid)
436
{
437
  struct thread_info *tp;
438
 
439
  for (tp = thread_list; tp; tp = tp->next)
440
    if (ptid_get_pid (tp->ptid) == pid)
441
      return tp;
442
 
443
  return NULL;
444
}
445
 
446
struct thread_info *
447
any_live_thread_of_process (int pid)
448
{
449
  struct thread_info *tp;
450
  struct thread_info *tp_running = NULL;
451
 
452
  for (tp = thread_list; tp; tp = tp->next)
453
    if (ptid_get_pid (tp->ptid) == pid)
454
      {
455
        if (tp->state_ == THREAD_STOPPED)
456
          return tp;
457
        else if (tp->state_ == THREAD_RUNNING)
458
          tp_running = tp;
459
      }
460
 
461
  return tp_running;
462
}
463
 
464
/* Print a list of thread ids currently known, and the total number of
465
   threads. To be used from within catch_errors. */
466
static int
467
do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
468
{
469
  struct thread_info *tp;
470
  int num = 0;
471
  struct cleanup *cleanup_chain;
472
  int current_thread = -1;
473
 
474
  update_thread_list ();
475
 
476
  cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
477
 
478
  for (tp = thread_list; tp; tp = tp->next)
479
    {
480
      if (tp->state_ == THREAD_EXITED)
481
        continue;
482
 
483
      if (ptid_equal (tp->ptid, inferior_ptid))
484
        current_thread = tp->num;
485
 
486
      num++;
487
      ui_out_field_int (uiout, "thread-id", tp->num);
488
    }
489
 
490
  do_cleanups (cleanup_chain);
491
 
492
  if (current_thread != -1)
493
    ui_out_field_int (uiout, "current-thread-id", current_thread);
494
  ui_out_field_int (uiout, "number-of-threads", num);
495
  return GDB_RC_OK;
496
}
497
 
498
/* Official gdblib interface function to get a list of thread ids and
499
   the total number. */
500
enum gdb_rc
501
gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
502
{
503
  if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
504
                                 error_message, RETURN_MASK_ALL) < 0)
505
    return GDB_RC_FAIL;
506
  return GDB_RC_OK;
507
}
508
 
509
/* Return true if TP is an active thread. */
510
static int
511
thread_alive (struct thread_info *tp)
512
{
513
  if (tp->state_ == THREAD_EXITED)
514
    return 0;
515
  if (!target_thread_alive (tp->ptid))
516
    return 0;
517
  return 1;
518
}
519
 
520
static void
521
prune_threads (void)
522
{
523
  struct thread_info *tp, *next;
524
 
525
  for (tp = thread_list; tp; tp = next)
526
    {
527
      next = tp->next;
528
      if (!thread_alive (tp))
529
        delete_thread (tp->ptid);
530
    }
531
}
532
 
533
void
534
thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
535
{
536
  struct inferior *inf;
537
  struct thread_info *tp;
538
 
539
  /* It can happen that what we knew as the target inferior id
540
     changes.  E.g, target remote may only discover the remote process
541
     pid after adding the inferior to GDB's list.  */
542
  inf = find_inferior_pid (ptid_get_pid (old_ptid));
543
  inf->pid = ptid_get_pid (new_ptid);
544
 
545
  tp = find_thread_ptid (old_ptid);
546
  tp->ptid = new_ptid;
547
 
548
  observer_notify_thread_ptid_changed (old_ptid, new_ptid);
549
}
550
 
551
void
552
set_running (ptid_t ptid, int running)
553
{
554
  struct thread_info *tp;
555
  int all = ptid_equal (ptid, minus_one_ptid);
556
 
557
  /* We try not to notify the observer if no thread has actually changed
558
     the running state -- merely to reduce the number of messages to
559
     frontend.  Frontend is supposed to handle multiple *running just fine.  */
560
  if (all || ptid_is_pid (ptid))
561
    {
562
      int any_started = 0;
563
      for (tp = thread_list; tp; tp = tp->next)
564
        if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
565
          {
566
            if (tp->state_ == THREAD_EXITED)
567
              continue;
568
            if (running && tp->state_ == THREAD_STOPPED)
569
              any_started = 1;
570
            tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
571
          }
572
      if (any_started)
573
        observer_notify_target_resumed (ptid);
574
    }
575
  else
576
    {
577
      int started = 0;
578
      tp = find_thread_ptid (ptid);
579
      gdb_assert (tp);
580
      gdb_assert (tp->state_ != THREAD_EXITED);
581
      if (running && tp->state_ == THREAD_STOPPED)
582
        started = 1;
583
      tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
584
      if (started)
585
        observer_notify_target_resumed (ptid);
586
    }
587
}
588
 
589
static int
590
is_thread_state (ptid_t ptid, enum thread_state state)
591
{
592
  struct thread_info *tp;
593
 
594
  tp = find_thread_ptid (ptid);
595
  gdb_assert (tp);
596
  return tp->state_ == state;
597
}
598
 
599
int
600
is_stopped (ptid_t ptid)
601
{
602
  return is_thread_state (ptid, THREAD_STOPPED);
603
}
604
 
605
int
606
is_exited (ptid_t ptid)
607
{
608
  return is_thread_state (ptid, THREAD_EXITED);
609
}
610
 
611
int
612
is_running (ptid_t ptid)
613
{
614
  return is_thread_state (ptid, THREAD_RUNNING);
615
}
616
 
617
int
618
any_running (void)
619
{
620
  struct thread_info *tp;
621
 
622
  for (tp = thread_list; tp; tp = tp->next)
623
    if (tp->state_ == THREAD_RUNNING)
624
      return 1;
625
 
626
  return 0;
627
}
628
 
629
int
630
is_executing (ptid_t ptid)
631
{
632
  struct thread_info *tp;
633
 
634
  tp = find_thread_ptid (ptid);
635
  gdb_assert (tp);
636
  return tp->executing_;
637
}
638
 
639
void
640
set_executing (ptid_t ptid, int executing)
641
{
642
  struct thread_info *tp;
643
  int all = ptid_equal (ptid, minus_one_ptid);
644
 
645
  if (all || ptid_is_pid (ptid))
646
    {
647
      for (tp = thread_list; tp; tp = tp->next)
648
        if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
649
          tp->executing_ = executing;
650
    }
651
  else
652
    {
653
      tp = find_thread_ptid (ptid);
654
      gdb_assert (tp);
655
      tp->executing_ = executing;
656
    }
657
}
658
 
659
void
660
set_stop_requested (ptid_t ptid, int stop)
661
{
662
  struct thread_info *tp;
663
  int all = ptid_equal (ptid, minus_one_ptid);
664
 
665
  if (all || ptid_is_pid (ptid))
666
    {
667
      for (tp = thread_list; tp; tp = tp->next)
668
        if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
669
          tp->stop_requested = stop;
670
    }
671
  else
672
    {
673
      tp = find_thread_ptid (ptid);
674
      gdb_assert (tp);
675
      tp->stop_requested = stop;
676
    }
677
 
678
  /* Call the stop requested observer so other components of GDB can
679
     react to this request.  */
680
  if (stop)
681
    observer_notify_thread_stop_requested (ptid);
682
}
683
 
684
void
685
finish_thread_state (ptid_t ptid)
686
{
687
  struct thread_info *tp;
688
  int all;
689
  int any_started = 0;
690
 
691
  all = ptid_equal (ptid, minus_one_ptid);
692
 
693
  if (all || ptid_is_pid (ptid))
694
    {
695
      for (tp = thread_list; tp; tp = tp->next)
696
        {
697
          if (tp->state_ == THREAD_EXITED)
698
            continue;
699
          if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
700
            {
701
              if (tp->executing_ && tp->state_ == THREAD_STOPPED)
702
                any_started = 1;
703
              tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
704
            }
705
        }
706
    }
707
  else
708
    {
709
      tp = find_thread_ptid (ptid);
710
      gdb_assert (tp);
711
      if (tp->state_ != THREAD_EXITED)
712
        {
713
          if (tp->executing_ && tp->state_ == THREAD_STOPPED)
714
            any_started = 1;
715
          tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
716
        }
717
    }
718
 
719
  if (any_started)
720
    observer_notify_target_resumed (ptid);
721
}
722
 
723
void
724
finish_thread_state_cleanup (void *arg)
725
{
726
  ptid_t *ptid_p = arg;
727
 
728
  gdb_assert (arg);
729
 
730
  finish_thread_state (*ptid_p);
731
}
732
 
733
/* Prints the list of threads and their details on UIOUT.
734
   This is a version of 'info_thread_command' suitable for
735
   use from MI.
736
   If REQUESTED_THREAD is not -1, it's the GDB id of the thread
737
   that should be printed.  Otherwise, all threads are
738
   printed.
739
   If PID is not -1, only print threads from the process PID.
740
   Otherwise, threads from all attached PIDs are printed.
741
   If both REQUESTED_THREAD and PID are not -1, then the thread
742
   is printed if it belongs to the specified process.  Otherwise,
743
   an error is raised.  */
744
void
745
print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
746
{
747
  struct thread_info *tp;
748
  ptid_t current_ptid;
749
  struct cleanup *old_chain;
750
  char *extra_info;
751
  int current_thread = -1;
752
 
753
  update_thread_list ();
754
  current_ptid = inferior_ptid;
755
 
756
  /* We'll be switching threads temporarily.  */
757
  old_chain = make_cleanup_restore_current_thread ();
758
 
759
  make_cleanup_ui_out_list_begin_end (uiout, "threads");
760
  for (tp = thread_list; tp; tp = tp->next)
761
    {
762
      struct cleanup *chain2;
763
      int core;
764
 
765
      if (requested_thread != -1 && tp->num != requested_thread)
766
        continue;
767
 
768
      if (pid != -1 && PIDGET (tp->ptid) != pid)
769
        {
770
          if (requested_thread != -1)
771
            error (_("Requested thread not found in requested process"));
772
          continue;
773
        }
774
 
775
      if (ptid_equal (tp->ptid, current_ptid))
776
        current_thread = tp->num;
777
 
778
      if (tp->state_ == THREAD_EXITED)
779
        continue;
780
 
781
      chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
782
 
783
      if (ptid_equal (tp->ptid, current_ptid))
784
        ui_out_text (uiout, "* ");
785
      else
786
        ui_out_text (uiout, "  ");
787
 
788
      ui_out_field_int (uiout, "id", tp->num);
789
      ui_out_text (uiout, " ");
790
      ui_out_field_string (uiout, "target-id", target_pid_to_str (tp->ptid));
791
 
792
      extra_info = target_extra_thread_info (tp);
793
      if (extra_info)
794
        {
795
          ui_out_text (uiout, " (");
796
          ui_out_field_string (uiout, "details", extra_info);
797
          ui_out_text (uiout, ")");
798
        }
799
      ui_out_text (uiout, "  ");
800
 
801
      if (tp->state_ == THREAD_RUNNING)
802
        ui_out_text (uiout, "(running)\n");
803
      else
804
        {
805
          /* The switch below puts us at the top of the stack (leaf
806
             frame).  */
807
          switch_to_thread (tp->ptid);
808
          print_stack_frame (get_selected_frame (NULL),
809
                             /* For MI output, print frame level.  */
810
                             ui_out_is_mi_like_p (uiout),
811
                             LOCATION);
812
        }
813
 
814
      if (ui_out_is_mi_like_p (uiout))
815
        {
816
          char *state = "stopped";
817
          if (tp->state_ == THREAD_RUNNING)
818
            state = "running";
819
          ui_out_field_string (uiout, "state", state);
820
        }
821
 
822
      core = target_core_of_thread (tp->ptid);
823
      if (ui_out_is_mi_like_p (uiout) && core != -1)
824
        ui_out_field_int (uiout, "core", core);
825
 
826
      do_cleanups (chain2);
827
    }
828
 
829
  /* Restores the current thread and the frame selected before
830
     the "info threads" command.  */
831
  do_cleanups (old_chain);
832
 
833
  if (pid == -1 && requested_thread == -1)
834
    {
835
      gdb_assert (current_thread != -1
836
                  || !thread_list
837
                  || ptid_equal (inferior_ptid, null_ptid));
838
      if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
839
        ui_out_field_int (uiout, "current-thread-id", current_thread);
840
 
841
      if (current_thread != -1 && is_exited (current_ptid))
842
        ui_out_message (uiout, 0, "\n\
843
The current thread <Thread ID %d> has terminated.  See `help thread'.\n",
844
                        current_thread);
845
      else if (thread_list
846
               && current_thread == -1
847
               && ptid_equal (current_ptid, null_ptid))
848
        ui_out_message (uiout, 0, "\n\
849
No selected thread.  See `help thread'.\n");
850
    }
851
}
852
 
853
 
854
/* Print information about currently known threads
855
 
856
 * Note: this has the drawback that it _really_ switches
857
 *       threads, which frees the frame cache.  A no-side
858
 *       effects info-threads command would be nicer.
859
 */
860
 
861
static void
862
info_threads_command (char *arg, int from_tty)
863
{
864
  print_thread_info (uiout, -1, -1);
865
}
866
 
867
/* Switch from one thread to another. */
868
 
869
void
870
switch_to_thread (ptid_t ptid)
871
{
872
  /* Switch the program space as well, if we can infer it from the now
873
     current thread.  Otherwise, it's up to the caller to select the
874
     space it wants.  */
875
  if (!ptid_equal (ptid, null_ptid))
876
    {
877
      struct inferior *inf;
878
 
879
      inf = find_inferior_pid (ptid_get_pid (ptid));
880
      gdb_assert (inf != NULL);
881
      set_current_program_space (inf->pspace);
882
      set_current_inferior (inf);
883
    }
884
 
885
  if (ptid_equal (ptid, inferior_ptid))
886
    return;
887
 
888
  inferior_ptid = ptid;
889
  reinit_frame_cache ();
890
  registers_changed ();
891
 
892
  /* We don't check for is_stopped, because we're called at times
893
     while in the TARGET_RUNNING state, e.g., while handling an
894
     internal event.  */
895
  if (!ptid_equal (inferior_ptid, null_ptid)
896
      && !is_exited (ptid)
897
      && !is_executing (ptid))
898
    stop_pc = regcache_read_pc (get_thread_regcache (ptid));
899
  else
900
    stop_pc = ~(CORE_ADDR) 0;
901
}
902
 
903
static void
904
restore_current_thread (ptid_t ptid)
905
{
906
  switch_to_thread (ptid);
907
}
908
 
909
static void
910
restore_selected_frame (struct frame_id a_frame_id, int frame_level)
911
{
912
  struct frame_info *frame = NULL;
913
  int count;
914
 
915
  gdb_assert (frame_level >= 0);
916
 
917
  /* Restore by level first, check if the frame id is the same as
918
     expected.  If that fails, try restoring by frame id.  If that
919
     fails, nothing to do, just warn the user.  */
920
 
921
  count = frame_level;
922
  frame = find_relative_frame (get_current_frame (), &count);
923
  if (count == 0
924
      && frame != NULL
925
      /* The frame ids must match - either both valid or both outer_frame_id.
926
         The latter case is not failsafe, but since it's highly unlikely
927
         the search by level finds the wrong frame, it's 99.9(9)% of
928
         the time (for all practical purposes) safe.  */
929
      && frame_id_eq (get_frame_id (frame), a_frame_id))
930
    {
931
      /* Cool, all is fine.  */
932
      select_frame (frame);
933
      return;
934
    }
935
 
936
  frame = frame_find_by_id (a_frame_id);
937
  if (frame != NULL)
938
    {
939
      /* Cool, refound it.  */
940
      select_frame (frame);
941
      return;
942
    }
943
 
944
  /* Nothing else to do, the frame layout really changed.  Select the
945
     innermost stack frame.  */
946
  select_frame (get_current_frame ());
947
 
948
  /* Warn the user.  */
949
  if (frame_level > 0 && !ui_out_is_mi_like_p (uiout))
950
    {
951
      warning (_("\
952
Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
953
               frame_level);
954
      /* For MI, we should probably have a notification about
955
         current frame change.  But this error is not very
956
         likely, so don't bother for now.  */
957
      print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
958
    }
959
}
960
 
961
struct current_thread_cleanup
962
{
963
  ptid_t inferior_ptid;
964
  struct frame_id selected_frame_id;
965
  int selected_frame_level;
966
  int was_stopped;
967
  int inf_id;
968
};
969
 
970
static void
971
do_restore_current_thread_cleanup (void *arg)
972
{
973
  struct thread_info *tp;
974
  struct current_thread_cleanup *old = arg;
975
 
976
  tp = find_thread_ptid (old->inferior_ptid);
977
 
978
  /* If the previously selected thread belonged to a process that has
979
     in the mean time been deleted (due to normal exit, detach, etc.),
980
     then don't revert back to it, but instead simply drop back to no
981
     thread selected.  */
982
  if (tp
983
      && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
984
    restore_current_thread (old->inferior_ptid);
985
  else
986
    {
987
      restore_current_thread (null_ptid);
988
      set_current_inferior (find_inferior_id (old->inf_id));
989
    }
990
 
991
  /* The running state of the originally selected thread may have
992
     changed, so we have to recheck it here.  */
993
  if (!ptid_equal (inferior_ptid, null_ptid)
994
      && old->was_stopped
995
      && is_stopped (inferior_ptid)
996
      && target_has_registers
997
      && target_has_stack
998
      && target_has_memory)
999
    restore_selected_frame (old->selected_frame_id,
1000
                            old->selected_frame_level);
1001
}
1002
 
1003
static void
1004
restore_current_thread_cleanup_dtor (void *arg)
1005
{
1006
  struct current_thread_cleanup *old = arg;
1007
  struct thread_info *tp;
1008
  tp = find_thread_ptid (old->inferior_ptid);
1009
  if (tp)
1010
    tp->refcount--;
1011
  xfree (old);
1012
}
1013
 
1014
struct cleanup *
1015
make_cleanup_restore_current_thread (void)
1016
{
1017
  struct thread_info *tp;
1018
  struct frame_info *frame;
1019
  struct current_thread_cleanup *old;
1020
 
1021
  old = xmalloc (sizeof (struct current_thread_cleanup));
1022
  old->inferior_ptid = inferior_ptid;
1023
  old->inf_id = current_inferior ()->num;
1024
 
1025
  if (!ptid_equal (inferior_ptid, null_ptid))
1026
    {
1027
      old->was_stopped = is_stopped (inferior_ptid);
1028
      if (old->was_stopped
1029
          && target_has_registers
1030
          && target_has_stack
1031
          && target_has_memory)
1032
        frame = get_selected_frame (NULL);
1033
      else
1034
        frame = NULL;
1035
 
1036
      old->selected_frame_id = get_frame_id (frame);
1037
      old->selected_frame_level = frame_relative_level (frame);
1038
 
1039
      tp = find_thread_ptid (inferior_ptid);
1040
      if (tp)
1041
        tp->refcount++;
1042
    }
1043
 
1044
  return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1045
                            restore_current_thread_cleanup_dtor);
1046
}
1047
 
1048
/* Apply a GDB command to a list of threads.  List syntax is a whitespace
1049
   seperated list of numbers, or ranges, or the keyword `all'.  Ranges consist
1050
   of two numbers seperated by a hyphen.  Examples:
1051
 
1052
   thread apply 1 2 7 4 backtrace       Apply backtrace cmd to threads 1,2,7,4
1053
   thread apply 2-7 9 p foo(1)  Apply p foo(1) cmd to threads 2->7 & 9
1054
   thread apply all p x/i $pc   Apply x/i $pc cmd to all threads
1055
 */
1056
 
1057
static void
1058
thread_apply_all_command (char *cmd, int from_tty)
1059
{
1060
  struct thread_info *tp;
1061
  struct cleanup *old_chain;
1062
  char *saved_cmd;
1063
 
1064
  if (cmd == NULL || *cmd == '\000')
1065
    error (_("Please specify a command following the thread ID list"));
1066
 
1067
  update_thread_list ();
1068
 
1069
  old_chain = make_cleanup_restore_current_thread ();
1070
 
1071
  /* Save a copy of the command in case it is clobbered by
1072
     execute_command */
1073
  saved_cmd = xstrdup (cmd);
1074
  make_cleanup (xfree, saved_cmd);
1075
  for (tp = thread_list; tp; tp = tp->next)
1076
    if (thread_alive (tp))
1077
      {
1078
        switch_to_thread (tp->ptid);
1079
 
1080
        printf_filtered (_("\nThread %d (%s):\n"),
1081
                         tp->num, target_pid_to_str (inferior_ptid));
1082
        execute_command (cmd, from_tty);
1083
        strcpy (cmd, saved_cmd);        /* Restore exact command used previously */
1084
      }
1085
 
1086
  do_cleanups (old_chain);
1087
}
1088
 
1089
static void
1090
thread_apply_command (char *tidlist, int from_tty)
1091
{
1092
  char *cmd;
1093
  char *p;
1094
  struct cleanup *old_chain;
1095
  char *saved_cmd;
1096
 
1097
  if (tidlist == NULL || *tidlist == '\000')
1098
    error (_("Please specify a thread ID list"));
1099
 
1100
  for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1101
 
1102
  if (*cmd == '\000')
1103
    error (_("Please specify a command following the thread ID list"));
1104
 
1105
  /* Save a copy of the command in case it is clobbered by
1106
     execute_command */
1107
  saved_cmd = xstrdup (cmd);
1108
  old_chain = make_cleanup (xfree, saved_cmd);
1109
  while (tidlist < cmd)
1110
    {
1111
      struct thread_info *tp;
1112
      int start, end;
1113
 
1114
      start = strtol (tidlist, &p, 10);
1115
      if (p == tidlist)
1116
        error (_("Error parsing %s"), tidlist);
1117
      tidlist = p;
1118
 
1119
      while (*tidlist == ' ' || *tidlist == '\t')
1120
        tidlist++;
1121
 
1122
      if (*tidlist == '-')      /* Got a range of IDs? */
1123
        {
1124
          tidlist++;            /* Skip the - */
1125
          end = strtol (tidlist, &p, 10);
1126
          if (p == tidlist)
1127
            error (_("Error parsing %s"), tidlist);
1128
          tidlist = p;
1129
 
1130
          while (*tidlist == ' ' || *tidlist == '\t')
1131
            tidlist++;
1132
        }
1133
      else
1134
        end = start;
1135
 
1136
      make_cleanup_restore_current_thread ();
1137
 
1138
      for (; start <= end; start++)
1139
        {
1140
          tp = find_thread_id (start);
1141
 
1142
          if (!tp)
1143
            warning (_("Unknown thread %d."), start);
1144
          else if (!thread_alive (tp))
1145
            warning (_("Thread %d has terminated."), start);
1146
          else
1147
            {
1148
              switch_to_thread (tp->ptid);
1149
 
1150
              printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1151
                               target_pid_to_str (inferior_ptid));
1152
              execute_command (cmd, from_tty);
1153
 
1154
              /* Restore exact command used previously.  */
1155
              strcpy (cmd, saved_cmd);
1156
            }
1157
        }
1158
    }
1159
 
1160
  do_cleanups (old_chain);
1161
}
1162
 
1163
/* Switch to the specified thread.  Will dispatch off to thread_apply_command
1164
   if prefix of arg is `apply'.  */
1165
 
1166
static void
1167
thread_command (char *tidstr, int from_tty)
1168
{
1169
  if (!tidstr)
1170
    {
1171
      if (ptid_equal (inferior_ptid, null_ptid))
1172
        error (_("No thread selected"));
1173
 
1174
      if (target_has_stack)
1175
        {
1176
          if (is_exited (inferior_ptid))
1177
            printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1178
                             pid_to_thread_id (inferior_ptid),
1179
                             target_pid_to_str (inferior_ptid));
1180
          else
1181
            printf_filtered (_("[Current thread is %d (%s)]\n"),
1182
                             pid_to_thread_id (inferior_ptid),
1183
                             target_pid_to_str (inferior_ptid));
1184
        }
1185
      else
1186
        error (_("No stack."));
1187
      return;
1188
    }
1189
 
1190
  gdb_thread_select (uiout, tidstr, NULL);
1191
}
1192
 
1193
/* Print notices when new threads are attached and detached.  */
1194
int print_thread_events = 1;
1195
static void
1196
show_print_thread_events (struct ui_file *file, int from_tty,
1197
                          struct cmd_list_element *c, const char *value)
1198
{
1199
  fprintf_filtered (file, _("\
1200
Printing of thread events is %s.\n"),
1201
                    value);
1202
}
1203
 
1204
static int
1205
do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1206
{
1207
  int num;
1208
  struct thread_info *tp;
1209
 
1210
  num = value_as_long (parse_and_eval (tidstr));
1211
 
1212
  tp = find_thread_id (num);
1213
 
1214
  if (!tp)
1215
    error (_("Thread ID %d not known."), num);
1216
 
1217
  if (!thread_alive (tp))
1218
    error (_("Thread ID %d has terminated."), num);
1219
 
1220
  switch_to_thread (tp->ptid);
1221
 
1222
  annotate_thread_changed ();
1223
 
1224
  ui_out_text (uiout, "[Switching to thread ");
1225
  ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1226
  ui_out_text (uiout, " (");
1227
  ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1228
  ui_out_text (uiout, ")]");
1229
 
1230
  /* Note that we can't reach this with an exited thread, due to the
1231
     thread_alive check above.  */
1232
  if (tp->state_ == THREAD_RUNNING)
1233
    ui_out_text (uiout, "(running)\n");
1234
  else
1235
    print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1236
 
1237
  /* Since the current thread may have changed, see if there is any
1238
     exited thread we can now delete.  */
1239
  prune_threads ();
1240
 
1241
  return GDB_RC_OK;
1242
}
1243
 
1244
enum gdb_rc
1245
gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1246
{
1247
  if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1248
                                 error_message, RETURN_MASK_ALL) < 0)
1249
    return GDB_RC_FAIL;
1250
  return GDB_RC_OK;
1251
}
1252
 
1253
void
1254
update_thread_list (void)
1255
{
1256
  prune_threads ();
1257
  target_find_new_threads ();
1258
}
1259
 
1260
/* Commands with a prefix of `thread'.  */
1261
struct cmd_list_element *thread_cmd_list = NULL;
1262
 
1263
void
1264
_initialize_thread (void)
1265
{
1266
  static struct cmd_list_element *thread_apply_list = NULL;
1267
 
1268
  add_info ("threads", info_threads_command,
1269
            _("IDs of currently known threads."));
1270
 
1271
  add_prefix_cmd ("thread", class_run, thread_command, _("\
1272
Use this command to switch between threads.\n\
1273
The new thread ID must be currently known."),
1274
                  &thread_cmd_list, "thread ", 1, &cmdlist);
1275
 
1276
  add_prefix_cmd ("apply", class_run, thread_apply_command,
1277
                  _("Apply a command to a list of threads."),
1278
                  &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1279
 
1280
  add_cmd ("all", class_run, thread_apply_all_command,
1281
           _("Apply a command to all threads."), &thread_apply_list);
1282
 
1283
  if (!xdb_commands)
1284
    add_com_alias ("t", "thread", class_run, 1);
1285
 
1286
  add_setshow_boolean_cmd ("thread-events", no_class,
1287
         &print_thread_events, _("\
1288
Set printing of thread events (such as thread start and exit)."), _("\
1289
Show printing of thread events (such as thread start and exit)."), NULL,
1290
         NULL,
1291
         show_print_thread_events,
1292
         &setprintlist, &showprintlist);
1293
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.