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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [uw-thread.c] - Blame information for rev 1775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 104 markom
/* Low level interface for debugging UnixWare user-mode threads for
2
   GDB, the GNU debugger.
3
 
4
   Copyright 1999, 2000 Free Software Foundation, Inc.
5
   Written by Nick Duffek <nsd@cygnus.com>.
6
 
7
   This file is part of GDB.
8
 
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 2 of the License, or
12
   (at your option) any later version.
13
 
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with this program; if not, write to the Free Software
21
   Foundation, Inc., 59 Temple Place - Suite 330,
22
   Boston, MA 02111-1307, USA.  */
23
 
24
 
25
/* Like many systems, UnixWare implements two classes of threads:
26
   kernel-mode threads, which are scheduled by the kernel; and
27
   user-mode threads, which are scheduled by a library.  UnixWare
28
   calls these two classes lightweight processes (LWPs) and threads,
29
   respectively.
30
 
31
   This module deals with user-mode threads.  It calls procfs_ops
32
   functions to deal with LWPs and processes and core_ops functions to
33
   deal with core files.
34
 
35
   As of this writing, the user-mode thread debugging interface is not
36
   documented beyond the comments in <thread.h>.  The following
37
   description has been gleaned from experience and from information
38
   provided by SCO.
39
 
40
   libthread.so, against which all UnixWare user-mode thread programs
41
   link, provides a global thread_debug structure named _thr_debug.
42
   It has three fields:
43
 
44
     (1) thr_map is a pointer to a pointer to an element of a
45
         thread_map ring.  A thread_map contains a single thread's id
46
         number, state, LWP pointer, recent register state, and other
47
         useful information.
48
 
49
     (2) thr_brk is a pointer to a stub function that libthread.so
50
         calls when it changes a thread's state, e.g. by creating it,
51
         switching it to an LWP, or causing it to exit.
52
 
53
     (3) thr_debug_on controls whether libthread.so calls thr_brk().
54
 
55
   Debuggers are able to track thread activity by setting a private
56
   breakpoint on thr_brk() and setting thr_debug_on to 1.
57
 
58
   thr_brk() receives two arguments:
59
 
60
     (1) a pointer to a thread_map describing the thread being
61
         changed; and
62
 
63
     (2) an enum thread_change specifying one of the following
64
         changes:
65
 
66
         invalid                 unknown
67
         thread_create           thread has just been created
68
         thread_exit             thread has just exited
69
         switch_begin            thread will be switched to an LWP
70
         switch_complete         thread has been switched to an LWP
71
         cancel_complete         thread wasn't switched to an LWP
72
         thread_suspend          thread has been thr_suspend()ed
73
         thread_suspend_pending  thread will be thr_suspend()ed
74
         thread_continue         thread has been thr_continue()d
75
 
76
   The thread_map argument to thr_brk() is NULL under the following
77
   circumstances:
78
 
79
     - The main thread is being acted upon.  The main thread always
80
       has id 1, so its thread_map is easy to find by scanning through
81
       _thr_debug.thr_map.
82
 
83
     - A "switch_complete" change is occurring, which means that the
84
       thread specified in the most recent "switch_begin" change has
85
       moved to an LWP.
86
 
87
     - A "cancel_complete" change is occurring, which means that the
88
       thread specified in the most recent "switch_begin" change has
89
       not moved to an LWP after all.
90
 
91
     - A spurious "switch_begin" change is occurring after a
92
       "thread_exit" change.
93
 
94
   Between switch_begin and switch_complete or cancel_complete, the
95
   affected thread's LWP pointer is not reliable.  It is possible that
96
   other parts of the thread's thread_map are also unreliable during
97
   that time. */
98
 
99
 
100
#include "defs.h"
101
#include "gdbthread.h"
102
#include "target.h"
103
#include "inferior.h"
104
#include <fcntl.h>
105
 
106
/* <thread.h> includes <sys/priocntl.h>, which requires boolean_t from
107
   <sys/types.h>, which doesn't typedef boolean_t with gcc. */
108
 
109
#define boolean_t int
110
#include <thread.h>
111
#undef boolean_t
112
 
113
#include <synch.h>              /* for UnixWare 2.x */
114
 
115
 
116
/* Whether to emit debugging output. */
117
 
118
#define DEBUG 0
119
 
120
/* Default debugging output file, overridden by envvar UWTHR_DEBUG. */
121
 
122
#define DEBUG_FILE "/dev/tty"
123
 
124
/* #if DEBUG, write string S to the debugging output channel. */
125
 
126
#if !DEBUG
127
# define DBG(fmt_and_args)
128
# define DBG2(fmt_and_args)
129
#else
130
# define DBG(fmt_and_args) dbg fmt_and_args
131
# define DBG2(fmt_and_args)
132
#endif
133
 
134
/* Back end to CALL_BASE() and TRY_BASE(): evaluate CALL, then convert
135
   inferior_pid to a composite thread/process id. */
136
 
137
#define CALL_BASE_1(call)               \
138
do {                                    \
139
  DBG2(("CALL_BASE(" #call ")"));       \
140
  call;                                 \
141
  do_cleanups (infpid_cleanup);         \
142
} while (0)
143
 
144
/* If inferior_pid can be converted to a composite lwp/process id, do so,
145
   evaluate base_ops function CALL, and then convert inferior_pid back to a
146
   composite thread/process id.
147
 
148
   Otherwise, issue an error message and return nonlocally. */
149
 
150
#define CALL_BASE(call)                 \
151
do {                                    \
152
  if (!lwp_infpid ())                   \
153
    error ("uw-thread: no lwp");        \
154
  CALL_BASE_1 (call);                   \
155
} while (0)
156
 
157
/* Like CALL_BASE(), but instead of returning nonlocally on error, set
158
   *CALLED to whether the inferior_pid conversion was successful. */
159
 
160
#define TRY_BASE(call, called)          \
161
do {                                    \
162
  if ((*(called) = lwp_infpid ()))      \
163
    CALL_BASE_1 (call);                 \
164
} while (0)
165
 
166
/* Information passed by thread_iter() to its callback parameter. */
167
 
168
typedef struct {
169
  struct thread_map map;
170
  __lwp_desc_t lwp;
171
  CORE_ADDR mapp;
172
} iter_t;
173
 
174
/* Private thread data for the thread_info struct. */
175
 
176
struct private_thread_info {
177
  int stable;           /* 0 if libthread.so is modifying thread map */
178
  int thrid;            /* thread id assigned by libthread.so */
179
  int lwpid;            /* thread's lwp if .stable, 0 means no lwp */
180
  CORE_ADDR mapp;       /* address of thread's map structure */
181
};
182
 
183
 
184
/* procfs.c's target-specific operations. */
185
extern struct target_ops procfs_ops;
186
 
187
/* Flag to prevent procfs.c from starting inferior processes. */
188
extern int procfs_suppress_run;
189
 
190
/* This module's target-specific operations. */
191
static struct target_ops uw_thread_ops;
192
 
193
/* Copy of the target over which uw_thread_ops is pushed.  This is
194
   more convenient than a pointer to procfs_ops or core_ops, because
195
   they lack current_target's default callbacks. */
196
static struct target_ops base_ops;
197
 
198
/* Saved pointer to previous owner of target_new_objfile_hook. */
199
static void (*target_new_objfile_chain)(struct objfile *);
200
 
201
/* Whether we are debugging a user-space thread program.  This isn't
202
   set until after libthread.so is loaded by the program being
203
   debugged.
204
 
205
   Except for module one-time intialization and where otherwise
206
   documented, no functions in this module get called when
207
   !uw_thread_active. */
208
static int uw_thread_active;
209
 
210
/* For efficiency, cache the addresses of libthread.so's _thr_debug
211
   structure, its thr_brk stub function, and the main thread's map. */
212
static CORE_ADDR thr_debug_addr;
213
static CORE_ADDR thr_brk_addr;
214
static CORE_ADDR thr_map_main;
215
 
216
/* Remember the thread most recently marked as switching.  Necessary because
217
   libthread.so passes null map when calling stub with tc_*_complete. */
218
static struct thread_info *switchto_thread;
219
 
220
/* Cleanup chain for safely restoring inferior_pid after CALL_BASE. */
221
static struct cleanup *infpid_cleanup;
222
 
223
 
224
#if DEBUG
225
/* Helper function for DBG() macro: if printf-style FMT is non-null, format it
226
   with args and display the result on the debugging output channel. */
227
 
228
static void
229
dbg (char *fmt, ...)
230
{
231
  static int fd = -1, len;
232
  va_list args;
233
  char buf[1024];
234
  char *path;
235
 
236
  if (!fmt)
237
    return;
238
 
239
  if (fd < 0)
240
    {
241
      path = getenv ("UWTHR_DEBUG");
242
      if (!path)
243
        path = DEBUG_FILE;
244
      if ((fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0664)) < 0)
245
        error ("can't open %s\n", path);
246
    }
247
 
248
  va_start (args, fmt);
249
  vsprintf (buf, fmt, args);
250
  va_end (args);
251
 
252
  len = strlen (buf);
253
  buf[len] = '\n';
254
  (void)write (fd, buf, len + 1);
255
}
256
 
257
#if 0
258
/* Return a string representing composite PID's components. */
259
 
260
static char *
261
dbgpid (int pid)
262
{
263
  static char *buf, buf1[80], buf2[80];
264
  if (!buf || buf == buf2)
265
    buf = buf1;
266
  else
267
    buf = buf2;
268
 
269
  if (pid <= 0)
270
    sprintf (buf, "%d", pid);
271
  else
272
    sprintf (buf, "%s %d/%d", ISTID (pid) ? "thr" : "lwp",
273
             TIDGET (pid), PIDGET (pid));
274
 
275
  return buf;
276
}
277
 
278
/* Return a string representing thread state CHANGE. */
279
 
280
static char *
281
dbgchange (enum thread_change change)
282
{
283
  switch (change) {
284
  case tc_invalid:                      return "invalid";
285
  case tc_thread_create:                return "thread_create";
286
  case tc_thread_exit:                  return "thread_exit";
287
  case tc_switch_begin:                 return "switch_begin";
288
  case tc_switch_complete:              return "switch_complete";
289
  case tc_cancel_complete:              return "cancel_complete";
290
  case tc_thread_suspend:               return "thread_suspend";
291
  case tc_thread_suspend_pending:       return "thread_suspend_pending";
292
  case tc_thread_continue:              return "thread_continue";
293
  default:                              return "unknown";
294
  }
295
}
296
 
297
/* Return a string representing thread STATE. */
298
 
299
static char *
300
dbgstate (int state)
301
{
302
  switch (state) {
303
  case TS_ONPROC:       return "running";
304
  case TS_SLEEP:        return "sleeping";
305
  case TS_RUNNABLE:     return "runnable";
306
  case TS_ZOMBIE:       return "zombie";
307
  case TS_SUSPENDED:    return "suspended";
308
#ifdef TS_FORK
309
  case TS_FORK:         return "forking";
310
#endif
311
  default:              return "confused";
312
  }
313
}
314
#endif  /* 0 */
315
#endif  /* DEBUG */
316
 
317
 
318
/* Read the contents of _thr_debug into *DEBUGP.  Return success. */
319
 
320
static int
321
read_thr_debug (struct thread_debug *debugp)
322
{
323
  return base_ops.to_xfer_memory (thr_debug_addr, (char *)debugp,
324
                                  sizeof (*debugp), 0, &base_ops);
325
}
326
 
327
/* Read into MAP the contents of the thread map at inferior process address
328
   MAPP.  Return success. */
329
 
330
static int
331
read_map (CORE_ADDR mapp, struct thread_map *map)
332
{
333
  return base_ops.to_xfer_memory ((CORE_ADDR)THR_MAP (mapp), (char *)map,
334
                                  sizeof (*map), 0, &base_ops);
335
}
336
 
337
/* Read into LWP the contents of the lwp decriptor at inferior process address
338
   LWPP.  Return success. */
339
 
340
static int
341
read_lwp (CORE_ADDR lwpp, __lwp_desc_t *lwp)
342
{
343
  return base_ops.to_xfer_memory (lwpp, (char *)lwp,
344
                                  sizeof (*lwp), 0, &base_ops);
345
}
346
 
347
/* Iterate through all user threads, applying FUNC(<map>, <lwp>, DATA) until
348
     (a) FUNC returns nonzero,
349
     (b) FUNC has been applied to all threads, or
350
     (c) an error occurs,
351
   where <map> is the thread's struct thread_map and <lwp> if non-null is the
352
   thread's current __lwp_desc_t.
353
 
354
   If a call to FUNC returns nonzero, return that value; otherwise, return 0. */
355
 
356
static int
357
thread_iter (int (*func)(iter_t *, void *), void *data)
358
{
359
  struct thread_debug debug;
360
  CORE_ADDR first, mapp;
361
  iter_t iter;
362
  int ret;
363
 
364
  if (!read_thr_debug (&debug))
365
    return 0;
366
  if (!base_ops.to_xfer_memory ((CORE_ADDR)debug.thr_map, (char *)&mapp,
367
                                sizeof (mapp), 0, &base_ops))
368
    return 0;
369
  if (!mapp)
370
    return 0;
371
 
372
  for (first = mapp;;)
373
    {
374
      if (!read_map (mapp, &iter.map))
375
        return 0;
376
 
377
      if (iter.map.thr_lwpp)
378
        if (!read_lwp ((CORE_ADDR)iter.map.thr_lwpp, &iter.lwp))
379
          return 0;
380
 
381
      iter.mapp = mapp;
382
      if ((ret = func (&iter, data)))
383
        return ret;
384
 
385
      mapp = (CORE_ADDR)iter.map.thr_next;
386
      if (mapp == first)
387
        return 0;
388
    }
389
}
390
 
391
/* Deactivate user-mode thread support. */
392
 
393
static void
394
deactivate_uw_thread (void)
395
{
396
  remove_thread_event_breakpoints ();
397
  uw_thread_active = 0;
398
  unpush_target (&uw_thread_ops);
399
}
400
 
401
/* Return the composite lwp/process id corresponding to composite
402
   id PID.  If PID is a thread with no lwp, return 0. */
403
 
404
static int
405
thr_to_lwp (int pid)
406
{
407
  struct thread_info *info;
408
  int lid;
409
 
410
  if (!ISTID (pid))
411
    lid = pid;
412
  else if (!(info = find_thread_pid (pid)))
413
    lid = 0;
414
  else if (!info->private->lwpid)
415
    lid = 0;
416
  else
417
    lid = MKLID (pid, info->private->lwpid);
418
 
419
  DBG2(("  thr_to_lwp(%s) = %s", dbgpid (pid), dbgpid (lid)));
420
  return lid;
421
}
422
 
423
/* find_thread_lwp() callback: return whether TP describes a thread
424
   associated with lwp id DATA. */
425
 
426
static int
427
find_thread_lwp_callback (struct thread_info *tp, void *data)
428
{
429
  int lwpid = (int)data;
430
 
431
  if (!ISTID (tp->pid))
432
    return 0;
433
  if (!tp->private->stable)
434
    return 0;
435
  if (lwpid != tp->private->lwpid)
436
    return 0;
437
 
438
  /* match */
439
  return 1;
440
}
441
 
442
/* If a thread is associated with lwp id LWPID, return the corresponding
443
   member of the global thread list; otherwise, return null. */
444
 
445
static struct thread_info *
446
find_thread_lwp (int lwpid)
447
{
448
  return iterate_over_threads (find_thread_lwp_callback, (void *)lwpid);
449
}
450
 
451
/* Return the composite thread/process id corresponding to composite
452
   id PID.  If PID is an lwp with no thread, return PID. */
453
 
454
static int
455
lwp_to_thr (int pid)
456
{
457
  struct thread_info *info;
458
  int tid = pid, lwpid;
459
 
460
  if (ISTID (pid))
461
    goto done;
462
  if (!(lwpid = LIDGET (pid)))
463
    goto done;
464
  if (!(info = find_thread_lwp (lwpid)))
465
    goto done;
466
  tid = MKTID (pid, info->private->thrid);
467
 
468
 done:
469
  DBG2((ISTID (tid) ? NULL : "lwp_to_thr: no thr for %s", dbgpid (pid)));
470
  return tid;
471
}
472
 
473
/* do_cleanups() callback: convert inferior_pid to a composite
474
   thread/process id after having made a procfs call. */
475
 
476
static void
477
thr_infpid (void *unused)
478
{
479
  int pid = lwp_to_thr (inferior_pid);
480
  DBG2((" inferior_pid from procfs: %s => %s",
481
        dbgpid (inferior_pid), dbgpid (pid)));
482
  inferior_pid = pid;
483
}
484
 
485
/* If possible, convert inferior_pid to a composite lwp/process id in
486
   preparation for making a procfs call.  Return success. */
487
 
488
static int
489
lwp_infpid (void)
490
{
491
  int pid = thr_to_lwp (inferior_pid);
492
  DBG2((" inferior_pid to procfs: %s => %s",
493
        dbgpid (inferior_pid), dbgpid (pid)));
494
 
495
  if (!pid)
496
    return 0;
497
 
498
  inferior_pid = pid;
499
  infpid_cleanup = make_cleanup (thr_infpid, NULL);
500
  return 1;
501
}
502
 
503
/* Add to the global thread list a new user-mode thread with system id THRID,
504
   lwp id LWPID, map address MAPP, and composite thread/process PID. */
505
 
506
static void
507
add_thread_uw (int thrid, int lwpid, CORE_ADDR mapp, int pid)
508
{
509
  struct thread_info *newthread;
510
 
511
  if ((newthread = add_thread (pid)) == NULL)
512
    error ("failed to create new thread structure");
513
 
514
  newthread->private = xmalloc (sizeof (struct private_thread_info));
515
  newthread->private->stable = 1;
516
  newthread->private->thrid = thrid;
517
  newthread->private->lwpid = lwpid;
518
  newthread->private->mapp = mapp;
519
 
520
  if (target_has_execution)
521
    printf_unfiltered ("[New %s]\n", target_pid_to_str (pid));
522
}
523
 
524
/* notice_threads() and find_main() callback: if the thread list doesn't
525
   already contain the thread described by ITER, add it if it's the main
526
   thread or if !DATA. */
527
 
528
static int
529
notice_thread (iter_t *iter, void *data)
530
{
531
  int thrid = iter->map.thr_tid;
532
  int lwpid = !iter->map.thr_lwpp ? 0 : iter->lwp.lwp_id;
533
  int pid = MKTID (inferior_pid, thrid);
534
 
535
  if (!find_thread_pid (pid) && (!data || thrid == 1))
536
    add_thread_uw (thrid, lwpid, iter->mapp, pid);
537
 
538
  return 0;
539
}
540
 
541
/* Add to the thread list any threads it doesn't already contain. */
542
 
543
static void
544
notice_threads (void)
545
{
546
  thread_iter (notice_thread, NULL);
547
}
548
 
549
/* Return the address of the main thread's map.  On error, return 0. */
550
 
551
static CORE_ADDR
552
find_main (void)
553
{
554
  if (!thr_map_main)
555
    {
556
      struct thread_info *info;
557
      thread_iter (notice_thread, (void *)1);
558
      if ((info = find_thread_pid (MKTID (inferior_pid, 1))))
559
        thr_map_main = info->private->mapp;
560
    }
561
  return thr_map_main;
562
}
563
 
564
/* Attach to process specified by ARGS, then initialize for debugging it
565
   and wait for the trace-trap that results from attaching.
566
 
567
   This function only gets called with uw_thread_active == 0. */
568
 
569
static void
570
uw_thread_attach (char *args, int from_tty)
571
{
572
  procfs_ops.to_attach (args, from_tty);
573
  if (uw_thread_active)
574
    thr_infpid (NULL);
575
}
576
 
577
/* Detach from the process attached to by uw_thread_attach(). */
578
 
579
static void
580
uw_thread_detach (char *args, int from_tty)
581
{
582
  deactivate_uw_thread ();
583
  base_ops.to_detach (args, from_tty);
584
}
585
 
586
/* Tell the inferior process to continue running thread PID if >= 0
587
   and all threads otherwise. */
588
 
589
static void
590
uw_thread_resume (int pid, int step, enum target_signal signo)
591
{
592
  if (pid > 0 && !(pid = thr_to_lwp (pid)))
593
    pid = -1;
594
 
595
  CALL_BASE (base_ops.to_resume (pid, step, signo));
596
}
597
 
598
/* If the trap we just received from lwp PID was due to a breakpoint
599
   on the libthread.so debugging stub, update this module's state
600
   accordingly. */
601
 
602
static void
603
libthread_stub (int pid)
604
{
605
  CORE_ADDR sp, mapp, mapp_main;
606
  enum thread_change change;
607
  struct thread_map map;
608
  __lwp_desc_t lwp;
609
  int tid = 0, lwpid;
610
  struct thread_info *info;
611
 
612
  /* Check for stub breakpoint. */
613
  if (read_pc_pid (pid) - DECR_PC_AFTER_BREAK != thr_brk_addr)
614
    return;
615
 
616
  /* Retrieve stub args. */
617
  sp = read_register_pid (SP_REGNUM, pid);
618
  if (!base_ops.to_xfer_memory (sp + SP_ARG0, (char *)&mapp,
619
                                sizeof (mapp), 0, &base_ops))
620
    goto err;
621
  if (!base_ops.to_xfer_memory (sp + SP_ARG0 + sizeof (mapp), (char *)&change,
622
                                sizeof (change), 0, &base_ops))
623
    goto err;
624
 
625
  /* create_inferior() may not have finished yet, so notice the main
626
     thread to ensure that it's displayed first by add_thread(). */
627
  mapp_main = find_main ();
628
 
629
  /* Notice thread creation, deletion, or stability change. */
630
  switch (change) {
631
  case tc_switch_begin:
632
    if (!mapp)                          /* usually means main thread */
633
      mapp = mapp_main;
634
    /* fall through */
635
 
636
  case tc_thread_create:
637
  case tc_thread_exit:
638
    if (!mapp)
639
      break;
640
    if (!read_map (mapp, &map))
641
      goto err;
642
    tid = MKTID (pid, map.thr_tid);
643
 
644
    switch (change) {
645
    case tc_thread_create:              /* new thread */
646
      if (!map.thr_lwpp)
647
        lwpid = 0;
648
      else if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
649
        goto err;
650
      else
651
        lwpid = lwp.lwp_id;
652
      add_thread_uw (map.thr_tid, lwpid, mapp, tid);
653
      break;
654
 
655
    case tc_thread_exit:                /* thread has exited */
656
      printf_unfiltered ("[Exited %s]\n", target_pid_to_str (tid));
657
      delete_thread (tid);
658
      if (tid == inferior_pid)
659
        inferior_pid = pid;
660
      break;
661
 
662
    case tc_switch_begin:               /* lwp is switching threads */
663
      if (switchto_thread)
664
        goto err;
665
      if (!(switchto_thread = find_thread_pid (tid)))
666
        goto err;
667
      switchto_thread->private->stable = 0;
668
      break;
669
 
670
    default:
671
      break;
672
    }
673
    break;
674
 
675
  case tc_switch_complete:              /* lwp has switched threads */
676
  case tc_cancel_complete:              /* lwp didn't switch threads */
677
    if (!switchto_thread)
678
      goto err;
679
 
680
    if (change == tc_switch_complete)
681
      {
682
        /* If switchto_thread is the main thread, then (a) the corresponding
683
           tc_switch_begin probably received a null map argument and therefore
684
           (b) it may have been a spurious switch following a tc_thread_exit.
685
 
686
           Therefore, explicitly query the thread's lwp before caching it in
687
           its thread list entry. */
688
 
689
        if (!read_map (switchto_thread->private->mapp, &map))
690
          goto err;
691
        if (map.thr_lwpp)
692
          {
693
            if (!read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
694
              goto err;
695
            if ((info = find_thread_lwp (lwp.lwp_id)))
696
              info->private->lwpid = 0;
697
            switchto_thread->private->lwpid = lwp.lwp_id;
698
          }
699
      }
700
 
701
    switchto_thread->private->stable = 1;
702
    switchto_thread = NULL;
703
    break;
704
 
705
  case tc_invalid:
706
  case tc_thread_suspend:
707
  case tc_thread_suspend_pending:
708
  case tc_thread_continue:
709
  err:
710
    DBG(("unexpected condition in libthread_stub()"));
711
    break;
712
  }
713
 
714
  DBG2(("libthread_stub(%s): %s %s %s", dbgpid (pid), dbgpid (tid),
715
        dbgchange (change), tid ? dbgstate (map.thr_state) : ""));
716
}
717
 
718
/* Wait for thread/lwp/process ID if >= 0 or for any thread otherwise. */
719
 
720
static int
721
uw_thread_wait (int pid, struct target_waitstatus *status)
722
{
723
  if (pid > 0)
724
    pid = thr_to_lwp (pid);
725
  CALL_BASE (pid = base_ops.to_wait (pid > 0 ? pid : -1, status));
726
 
727
  if (status->kind == TARGET_WAITKIND_STOPPED &&
728
      status->value.sig == TARGET_SIGNAL_TRAP)
729
    libthread_stub (pid);
730
 
731
  return lwp_to_thr (pid);
732
}
733
 
734
/* Tell gdb about the registers in the thread/lwp/process specified by
735
   inferior_pid. */
736
 
737
static void
738
uw_thread_fetch_registers (int regno)
739
{
740
  int called;
741
  struct thread_info *info;
742
  struct thread_map map;
743
 
744
  TRY_BASE (base_ops.to_fetch_registers (regno), &called);
745
  if (called)
746
    return;
747
 
748
  if (!(info = find_thread_pid (inferior_pid)))
749
    return;
750
  if (!read_map (info->private->mapp, &map))
751
    return;
752
 
753
  supply_gregset (&map.thr_ucontext.uc_mcontext.gregs);
754
  supply_fpregset (&map.thr_ucontext.uc_mcontext.fpregs);
755
}
756
 
757
/* Store gdb's current view of the register set into the thread/lwp/process
758
   specified by inferior_pid. */
759
 
760
static void
761
uw_thread_store_registers (int regno)
762
{
763
  CALL_BASE (base_ops.to_store_registers (regno));
764
}
765
 
766
/* Prepare to modify the registers array. */
767
 
768
static void
769
uw_thread_prepare_to_store (void)
770
{
771
  CALL_BASE (base_ops.to_prepare_to_store ());
772
}
773
 
774
/* Fork an inferior process and start debugging it.
775
 
776
   This function only gets called with uw_thread_active == 0. */
777
 
778
static void
779
uw_thread_create_inferior (char *exec_file, char *allargs, char **env)
780
{
781
  if (uw_thread_active)
782
    deactivate_uw_thread ();
783
 
784
  procfs_ops.to_create_inferior (exec_file, allargs, env);
785
  if (uw_thread_active)
786
    {
787
      find_main ();
788
      thr_infpid (NULL);
789
    }
790
}
791
 
792
/* Kill and forget about the inferior process. */
793
 
794
static void
795
uw_thread_kill (void)
796
{
797
  base_ops.to_kill ();
798
}
799
 
800
/* Clean up after the inferior exits. */
801
 
802
static void
803
uw_thread_mourn_inferior (void)
804
{
805
  deactivate_uw_thread ();
806
  base_ops.to_mourn_inferior ();
807
}
808
 
809
/* Return whether this module can attach to and run processes.
810
 
811
   This function only gets called with uw_thread_active == 0. */
812
 
813
static int
814
uw_thread_can_run (void)
815
{
816
  return procfs_suppress_run;
817
}
818
 
819
/* Return whether thread PID is still valid. */
820
 
821
static int
822
uw_thread_alive (int pid)
823
{
824
  if (!ISTID (pid))
825
    return base_ops.to_thread_alive (pid);
826
 
827
  /* If it's in the thread list, it's valid, because otherwise
828
     libthread_stub() would have deleted it. */
829
  return in_thread_list (pid);
830
}
831
 
832
/* Add to the thread list any threads and lwps it doesn't already contain. */
833
 
834
static void
835
uw_thread_find_new_threads (void)
836
{
837
  CALL_BASE (if (base_ops.to_find_new_threads)
838
               base_ops.to_find_new_threads ());
839
  notice_threads ();
840
}
841
 
842
/* Return a string for pretty-printing PID in "info threads" output.
843
   This may be called by either procfs.c or by generic gdb. */
844
 
845
static char *
846
uw_thread_pid_to_str (int pid)
847
{
848
#define FMT "Thread %d"
849
  static char buf[sizeof (FMT) + 3 * sizeof (pid)];
850
 
851
  if (!ISTID (pid))
852
    /* core_ops says "process foo", so call procfs_ops explicitly. */
853
    return procfs_ops.to_pid_to_str (pid);
854
 
855
  sprintf (buf, FMT, TIDGET (pid));
856
#undef FMT
857
  return buf;
858
}
859
 
860
/* Return a string displaying INFO state information in "info threads"
861
   output. */
862
 
863
static char *
864
uw_extra_thread_info (struct thread_info *info)
865
{
866
  static char buf[80];
867
  struct thread_map map;
868
  __lwp_desc_t lwp;
869
  int lwpid;
870
  char *name;
871
 
872
  if (!ISTID (info->pid))
873
    return NULL;
874
 
875
  if (!info->private->stable)
876
    return "switching";
877
 
878
  if (!read_map (info->private->mapp, &map))
879
    return NULL;
880
 
881
  if (!map.thr_lwpp || !read_lwp ((CORE_ADDR)map.thr_lwpp, &lwp))
882
    lwpid = 0;
883
  else
884
    lwpid = lwp.lwp_id;
885
 
886
  switch (map.thr_state) {
887
  case TS_ONPROC:       name = "running";       break;
888
  case TS_SLEEP:        name = "sleeping";      break;
889
  case TS_RUNNABLE:     name = "runnable";      break;
890
  case TS_ZOMBIE:       name = "zombie";        break;
891
  case TS_SUSPENDED:    name = "suspended";     break;
892
#ifdef TS_FORK
893
  case TS_FORK:         name = "forking";       break;
894
#endif
895
  default:              name = "confused";      break;
896
  }
897
 
898
  if (!lwpid)
899
    return name;
900
 
901
  sprintf (buf, "%s, LWP %d", name, lwpid);
902
  return buf;
903
}
904
 
905
/* Check whether libthread.so has just been loaded, and if so, try to
906
   initialize user-space thread debugging support.
907
 
908
   libthread.so loading happens while (a) an inferior process is being
909
   started by procfs and (b) a core image is being loaded.
910
 
911
   This function often gets called with uw_thread_active == 0. */
912
 
913
static void
914
libthread_init (void)
915
{
916
  struct minimal_symbol *ms;
917
  struct thread_debug debug;
918
  CORE_ADDR onp;
919
  struct breakpoint *b;
920
  int one = 1;
921
 
922
  /* Don't initialize twice. */
923
  if (uw_thread_active)
924
    return;
925
 
926
  /* Check whether libthread.so has been loaded. */
927
  if (!(ms = lookup_minimal_symbol ("_thr_debug", NULL, NULL)))
928
    return;
929
 
930
  /* Cache _thr_debug's address. */
931
  if (!(thr_debug_addr = SYMBOL_VALUE_ADDRESS (ms)))
932
    return;
933
 
934
  /* Initialize base_ops.to_xfer_memory(). */
935
  base_ops = current_target;
936
 
937
  /* Load _thr_debug's current contents. */
938
  if (!read_thr_debug (&debug))
939
    return;
940
 
941
  /* User code (e.g. my test programs) may dereference _thr_debug,
942
     making it availble to GDB before shared libs are loaded. */
943
  if (!debug.thr_map)
944
    return;
945
 
946
  /* libthread.so has been loaded, and the current_target should now
947
     reflect core_ops or procfs_ops. */
948
  push_target (&uw_thread_ops);         /* must precede notice_threads() */
949
  uw_thread_active = 1;
950
 
951
  if (!target_has_execution)
952
 
953
    /* Locate threads in core file. */
954
    notice_threads ();
955
 
956
  else
957
    {
958
      /* Set a breakpoint on the stub function provided by libthread.so. */
959
      thr_brk_addr = (CORE_ADDR)debug.thr_brk;
960
      if (!(b = create_thread_event_breakpoint (thr_brk_addr)))
961
        goto err;
962
 
963
      /* Activate the stub function. */
964
      onp = (CORE_ADDR)&((struct thread_debug *)thr_debug_addr)->thr_debug_on;
965
      if (!base_ops.to_xfer_memory ((CORE_ADDR)onp, (char *)&one,
966
                                    sizeof (one), 1, &base_ops))
967
        {
968
          delete_breakpoint (b);
969
          goto err;
970
        }
971
 
972
      /* Prepare for finding the main thread, which doesn't yet exist. */
973
      thr_map_main = 0;
974
    }
975
 
976
  return;
977
 
978
 err:
979
  warning ("uw-thread: unable to initialize user-mode thread debugging\n");
980
  deactivate_uw_thread ();
981
}
982
 
983
/* target_new_objfile_hook callback.
984
 
985
   If OBJFILE is non-null, check whether libthread.so was just loaded,
986
   and if so, prepare for user-mode thread debugging.
987
 
988
   If OBJFILE is null, libthread.so has gone away, so stop debugging
989
   user-mode threads.
990
 
991
   This function often gets called with uw_thread_active == 0. */
992
 
993
static void
994
uw_thread_new_objfile (struct objfile *objfile)
995
{
996
  if (objfile)
997
    libthread_init ();
998
 
999
  else if (uw_thread_active)
1000
    deactivate_uw_thread ();
1001
 
1002
  if (target_new_objfile_chain)
1003
    target_new_objfile_chain (objfile);
1004
}
1005
 
1006
/* Initialize uw_thread_ops. */
1007
 
1008
static void
1009
init_uw_thread_ops (void)
1010
{
1011
  uw_thread_ops.to_shortname          = "unixware-threads";
1012
  uw_thread_ops.to_longname           = "UnixWare threads and pthread.";
1013
  uw_thread_ops.to_doc        = "UnixWare threads and pthread support.";
1014
  uw_thread_ops.to_attach             = uw_thread_attach;
1015
  uw_thread_ops.to_detach             = uw_thread_detach;
1016
  uw_thread_ops.to_resume             = uw_thread_resume;
1017
  uw_thread_ops.to_wait               = uw_thread_wait;
1018
  uw_thread_ops.to_fetch_registers    = uw_thread_fetch_registers;
1019
  uw_thread_ops.to_store_registers    = uw_thread_store_registers;
1020
  uw_thread_ops.to_prepare_to_store   = uw_thread_prepare_to_store;
1021
  uw_thread_ops.to_create_inferior    = uw_thread_create_inferior;
1022
  uw_thread_ops.to_kill               = uw_thread_kill;
1023
  uw_thread_ops.to_mourn_inferior     = uw_thread_mourn_inferior;
1024
  uw_thread_ops.to_can_run            = uw_thread_can_run;
1025
  uw_thread_ops.to_thread_alive       = uw_thread_alive;
1026
  uw_thread_ops.to_find_new_threads   = uw_thread_find_new_threads;
1027
  uw_thread_ops.to_pid_to_str         = uw_thread_pid_to_str;
1028
  uw_thread_ops.to_extra_thread_info  = uw_extra_thread_info;
1029
  uw_thread_ops.to_stratum            = thread_stratum;
1030
  uw_thread_ops.to_magic              = OPS_MAGIC;
1031
}
1032
 
1033
/* Module startup initialization function, automagically called by
1034
   init.c. */
1035
 
1036
void
1037
_initialize_uw_thread (void)
1038
{
1039
  init_uw_thread_ops ();
1040
  add_target (&uw_thread_ops);
1041
 
1042
  procfs_suppress_run = 1;
1043
 
1044
  /* Notice when libthread.so gets loaded. */
1045
  target_new_objfile_chain = target_new_objfile_hook;
1046
  target_new_objfile_hook = uw_thread_new_objfile;
1047
}

powered by: WebSVN 2.1.0

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