1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
/* Copyright (C) 2008, 2009 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "command.h"
#include "gdbcmd.h"
#include "target.h"
#include "observer.h"
#include <sys/procfs.h>
#include "gregset.h"
#include "regcache.h"
#include "inferior.h"
#include "gdbthread.h"
#include <pthread_debug.h>
/* Print debugging traces if set to non-zero. */
static int debug_dec_thread = 0;
/* Non-zero if the dec-thread layer is active. */
static int dec_thread_active = 0;
/* The pthread_debug context. */
pthreadDebugContext_t debug_context;
/* The dec-thread target_ops structure. */
static struct target_ops dec_thread_ops;
/* Print a debug trace if DEBUG_DEC_THREAD is set (its value is adjusted
by the user using "set debug dec-thread ..."). */
static void
debug (char *format, ...)
{
if (debug_dec_thread)
{
va_list args;
va_start (args, format);
printf_unfiltered ("DEC Threads: ");
vprintf_unfiltered (format, args);
printf_unfiltered ("\n");
va_end (args);
}
}
/* pthread debug callbacks. */
static int
suspend_clbk (void *caller_context)
{
return ESUCCESS;
}
static int
resume_clbk (void *caller_context)
{
return ESUCCESS;
}
static int
hold_clbk (void *caller_context, pthreadDebugKId_t kernel_tid)
{
return ESUCCESS;
}
static int
unhold_clbk (void *caller_context, pthreadDebugKId_t kernel_tid)
{
return ESUCCESS;
}
static int
read_clbk (void *caller_context, void *address, void *buffer,
unsigned long size)
{
int status = target_read_memory ((CORE_ADDR) address, buffer, size);
if (status != 0)
return EINVAL;
return ESUCCESS;
}
static int
write_clbk (void *caller_context, void *address, void *buffer,
unsigned long size)
{
int status = target_write_memory ((CORE_ADDR) address, buffer, size);
if (status != 0)
return EINVAL;
return ESUCCESS;
}
/* Get integer regs */
static int
get_reg_clbk(void *caller_context, pthreadDebugGetRegRtn_t regs,
pthreadDebugKId_t kernel_tid)
{
debug ("get_reg_clbk");
/* Not sure that we actually need to do anything in this callback. */
return ESUCCESS;
}
/* Set integer regs */
static int
set_reg_clbk(void *caller_context, const pthreadDebugRegs_t *regs,
pthreadDebugKId_t kernel_tid)
{
debug ("set_reg_clbk");
/* Not sure that we actually need to do anything in this callback. */
return ESUCCESS;
}
static int
output_clbk (void *caller_context, char *line)
{
printf_filtered ("%s\n", line);
return ESUCCESS;
}
static int
error_clbk (void *caller_context, char *line)
{
fprintf_filtered (gdb_stderr, "%s\n", line);
return ESUCCESS;
}
/* Get floating-point regs. */
static int
get_fpreg_clbk (void *caller_context, pthreadDebugFregs_p fregs,
pthreadDebugKId_t kernel_tid)
{
debug ("get_fpreg_clbk");
/* Not sure that we actually need to do anything in this callback. */
return ESUCCESS;
}
/* Set floating-point regs. */
static int
set_fpreg_clbk (void *caller_context, const pthreadDebugFregs_t *fregs,
pthreadDebugKId_t kernel_tid)
{
debug ("set_fpreg_clbk");
/* Not sure that we actually need to do anything in this callback. */
return ESUCCESS;
}
static void *
malloc_clbk (void *caller_context, size_t size)
{
return xmalloc (size);
}
static void
free_clbk (void *caller_context, void *address)
{
xfree (address);
}
static int
kthdinfo_clbk (pthreadDebugClient_t caller_context,
pthreadDebugKId_t kernel_tid,
pthreadDebugKThreadInfo_p thread_info)
{
return ENOTSUP;
}
static int
speckthd_clbk (pthreadDebugClient_t caller_context,
pthreadDebugSpecialType_t type,
pthreadDebugKId_t *kernel_tid)
{
return ENOTSUP;
}
static pthreadDebugCallbacks_t debug_callbacks =
{
PTHREAD_DEBUG_VERSION,
(pthreadDebugGetMemRtn_t) read_clbk,
(pthreadDebugSetMemRtn_t) write_clbk,
suspend_clbk,
resume_clbk,
kthdinfo_clbk,
hold_clbk,
unhold_clbk,
(pthreadDebugGetFregRtn_t) get_fpreg_clbk,
(pthreadDebugSetFregRtn_t) set_fpreg_clbk,
(pthreadDebugGetRegRtn_t) get_reg_clbk,
(pthreadDebugSetRegRtn_t) set_reg_clbk,
(pthreadDebugOutputRtn_t) output_clbk,
(pthreadDebugOutputRtn_t) error_clbk,
malloc_clbk,
free_clbk,
speckthd_clbk
};
/* Activate thread support if appropriate. Do nothing if thread
support is already active. */
static void
enable_dec_thread (void)
{
struct minimal_symbol *msym;
void* caller_context;
int status;
/* If already active, nothing more to do. */
if (dec_thread_active)
return;
msym = lookup_minimal_symbol ("__pthread_dbg_symtable", NULL, NULL);
if (msym == NULL)
{
debug ("enable_dec_thread: No __pthread_dbg_symtable");
return;
}
status = pthreadDebugContextInit (&caller_context, &debug_callbacks,
(void *) SYMBOL_VALUE_ADDRESS (msym),
&debug_context);
if (status != ESUCCESS)
{
debug ("enable_dec_thread: pthreadDebugContextInit -> %d",
status);
return;
}
push_target (&dec_thread_ops);
dec_thread_active = 1;
debug ("enable_dec_thread: Thread support enabled.");
}
/* Deactivate thread support. Do nothing is thread support is
already inactive. */
static void
disable_dec_thread (void)
{
if (!dec_thread_active)
return;
pthreadDebugContextDestroy (debug_context);
unpush_target (&dec_thread_ops);
dec_thread_active = 0;
}
/* A structure that contains a thread ID and is associated
pthreadDebugThreadInfo_t data. */
struct dec_thread_info
{
pthreadDebugId_t thread;
pthreadDebugThreadInfo_t info;
};
typedef struct dec_thread_info dec_thread_info_s;
/* The list of user threads. */
DEF_VEC_O (dec_thread_info_s);
VEC(dec_thread_info_s) *dec_thread_list;
/* Release the memory used by the given VECP thread list pointer.
Then set *VECP to NULL. */
static void
free_dec_thread_info_vec (VEC(dec_thread_info_s) **vecp)
{
int i;
struct dec_thread_info *item;
VEC(dec_thread_info_s) *vec = *vecp;
for (i = 0; VEC_iterate (dec_thread_info_s, vec, i, item); i++)
xfree (item);
VEC_free (dec_thread_info_s, vec);
*vecp = NULL;
}
/* Return a thread's ptid given its associated INFO. */
static ptid_t
ptid_build_from_info (struct dec_thread_info info)
{
int pid = ptid_get_pid (inferior_ptid);
return ptid_build (pid, 0, (long) info.thread);
}
/* Recompute the list of user threads and store the result in
DEC_THREAD_LIST. */
static void
update_dec_thread_list (void)
{
pthreadDebugId_t thread;
pthreadDebugThreadInfo_t info;
int res;
free_dec_thread_info_vec (&dec_thread_list);
res = pthreadDebugThdSeqInit (debug_context, &thread);
while (res == ESUCCESS)
{
res = pthreadDebugThdGetInfo (debug_context, thread, &info);
if (res != ESUCCESS)
warning (_("unable to get thread info, ignoring thread %ld"),
thread);
else if (info.kind == PTHREAD_DEBUG_THD_KIND_INITIAL
|| info.kind == PTHREAD_DEBUG_THD_KIND_NORMAL)
{
struct dec_thread_info *item =
xmalloc (sizeof (struct dec_thread_info));
item->thread = thread;
item->info = info;
VEC_safe_push (dec_thread_info_s, dec_thread_list, item);
}
res = pthreadDebugThdSeqNext (debug_context, &thread);
}
pthreadDebugThdSeqDestroy (debug_context);
}
/* A callback to count the number of threads known to GDB. */
static int
dec_thread_count_gdb_threads (struct thread_info *ignored, void *context)
{
int *count = (int *) context;
*count++;
return 0;
}
/* A callback that saves the given thread INFO at the end of an
array. The end of the array is given in the CONTEXT and is
incremented once the info has been added. */
static int
dec_thread_add_gdb_thread (struct thread_info *info, void *context)
{
struct thread_info ***listp = (struct thread_info ***) context;
**listp = info;
*listp++;
return 0;
}
/* Resynchronize the list of threads known by GDB with the actual
list of threads reported by libpthread_debug. */
static void
resync_thread_list (void)
{
int i;
struct dec_thread_info *info;
int num_gdb_threads = 0;
struct thread_info **gdb_thread_list;
struct thread_info **next_thread_info;
update_dec_thread_list ();
/* Add new threads. */
for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info);
i++)
{
ptid_t ptid = ptid_build_from_info (*info);
if (!in_thread_list (ptid))
add_thread (ptid);
}
/* Remove threads that no longer exist. To help with the search,
we build an array of GDB threads, and then iterate over this
array. */
iterate_over_threads (dec_thread_count_gdb_threads,
(void *) &num_gdb_threads);
gdb_thread_list = alloca (num_gdb_threads * sizeof (struct thread_info *));
next_thread_info = gdb_thread_list;
iterate_over_threads (dec_thread_add_gdb_thread, (void *) &next_thread_info);
for (i = 0; i < num_gdb_threads; i++)
{
int j;
for (j = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, j, info);
j++)
if (ptid_equal (gdb_thread_list[i]->ptid,
ptid_build_from_info (*info)))
break;
delete_thread (gdb_thread_list[i]->ptid);
}
}
/* The "to_detach" method of the dec_thread_ops. */
static void
dec_thread_detach (struct target_ops *ops, char *args, int from_tty)
{
struct target_ops *beneath = find_target_beneath (ops);
debug ("dec_thread_detach");
disable_dec_thread ();
beneath->to_detach (beneath, args, from_tty);
}
/* Return the ptid of the thread that is currently active. */
static ptid_t
get_active_ptid (void)
{
int i;
struct dec_thread_info *info;
for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info);
i++)
if (info->info.state == PTHREAD_DEBUG_STATE_RUNNING)
return ptid_build_from_info (*info);
/* No active thread found. This can happen when the program
has just exited. */
return null_ptid;
}
/* The "to_wait" method of the dec_thread_ops. */
static ptid_t
dec_thread_wait (struct target_ops *ops,
ptid_t ptid, struct target_waitstatus *status, int options)
{
ptid_t active_ptid;
struct target_ops *beneath = find_target_beneath (ops);
debug ("dec_thread_wait");
ptid = beneath->to_wait (beneath, ptid, status, options);
/* The ptid returned by the target beneath us is the ptid of the process.
We need to find which thread is currently active and return its ptid. */
resync_thread_list ();
active_ptid = get_active_ptid ();
if (ptid_equal (active_ptid, null_ptid))
return ptid;
return active_ptid;
}
/* Fetch the general purpose and floating point registers for the given
thread TID, and store the result in GREGSET and FPREGSET. Return
zero if successful. */
static int
dec_thread_get_regsets (pthreadDebugId_t tid, gdb_gregset_t *gregset,
gdb_fpregset_t *fpregset)
{
int res;
pthreadDebugRegs_t regs;
pthreadDebugFregs_t fregs;
res = pthreadDebugThdGetReg (debug_context, tid, ®s);
if (res != ESUCCESS)
{
debug ("dec_thread_get_regsets: pthreadDebugThdGetReg -> %d", res);
return -1;
}
memcpy (gregset->regs, ®s, sizeof (regs));
res = pthreadDebugThdGetFreg (debug_context, tid, &fregs);
if (res != ESUCCESS)
{
debug ("dec_thread_get_regsets: pthreadDebugThdGetFreg -> %d", res);
return -1;
}
memcpy (fpregset->regs, &fregs, sizeof (fregs));
return 0;
}
/* The "to_fetch_registers" method of the dec_thread_ops.
Because the dec-thread debug API doesn't allow us to fetch
only one register, we simply ignore regno and fetch+supply all
registers. */
static void
dec_thread_fetch_registers (struct target_ops *ops,
struct regcache *regcache, int regno)
{
pthreadDebugId_t tid = ptid_get_tid (inferior_ptid);
gregset_t gregset;
fpregset_t fpregset;
int res;
debug ("dec_thread_fetch_registers (tid=%ld, regno=%d)", tid, regno);
if (tid == 0 || ptid_equal (inferior_ptid, get_active_ptid ()))
{
struct target_ops *beneath = find_target_beneath (ops);
beneath->to_fetch_registers (beneath, regcache, regno);
return;
}
res = dec_thread_get_regsets (tid, &gregset, &fpregset);
if (res != 0)
return;
supply_gregset (regcache, &gregset);
supply_fpregset (regcache, &fpregset);
}
/* Store the registers given in GREGSET and FPREGSET into the associated
general purpose and floating point registers of thread TID. Return
zero if successful. */
static int
dec_thread_set_regsets (pthreadDebugId_t tid, gdb_gregset_t gregset,
gdb_fpregset_t fpregset)
{
int res;
pthreadDebugRegs_t regs;
pthreadDebugFregs_t fregs;
memcpy (®s, gregset.regs, sizeof (regs));
res = pthreadDebugThdSetReg (debug_context, tid, ®s);
if (res != ESUCCESS)
{
debug ("dec_thread_set_regsets: pthreadDebugThdSetReg -> %d", res);
return -1;
}
memcpy (&fregs, fpregset.regs, sizeof (fregs));
res = pthreadDebugThdSetFreg (debug_context, tid, &fregs);
if (res != ESUCCESS)
{
debug ("dec_thread_set_regsets: pthreadDebugThdSetFreg -> %d", res);
return -1;
}
return 0;
}
/* The "to_store_registers" method of the dec_thread_ops.
Because the dec-thread debug API doesn't allow us to store
just one register, we store all the registers. */
static void
dec_thread_store_registers (struct target_ops *ops,
struct regcache *regcache, int regno)
{
pthreadDebugId_t tid = ptid_get_tid (inferior_ptid);
gregset_t gregset;
fpregset_t fpregset;
int res;
debug ("dec_thread_store_registers (tid=%ld, regno=%d)", tid, regno);
if (tid == 0 || ptid_equal (inferior_ptid, get_active_ptid ()))
{
struct target_ops *beneath = find_target_beneath (ops);
beneath->to_store_registers (beneath, regcache, regno);
return;
}
/* FIXME: brobecker/2008-05-28: I wonder if we could simply check
in which register set the register is and then only store the
registers for that register set, instead of storing both register
sets. */
fill_gregset (regcache, &gregset, -1);
fill_fpregset (regcache, &fpregset, -1);
res = dec_thread_set_regsets (tid, gregset, fpregset);
if (res != 0)
warning (_("failed to store registers."));
}
/* The "to_mourn_inferior" method of the dec_thread_ops. */
static void
dec_thread_mourn_inferior (struct target_ops *ops)
{
struct target_ops *beneath = find_target_beneath (ops);
debug ("dec_thread_mourn_inferior");
disable_dec_thread ();
beneath->to_mourn_inferior (beneath);
}
/* The "to_thread_alive" method of the dec_thread_ops. */
static int
dec_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
{
debug ("dec_thread_thread_alive (tid=%ld)", ptid_get_tid (ptid));
/* The thread list maintained by GDB is up to date, since we update
it everytime we stop. So check this list. */
return in_thread_list (ptid);
}
/* The "to_pid_to_str" method of the dec_thread_ops. */
static char *
dec_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
{
static char *ret = NULL;
if (ptid_get_tid (ptid) == 0)
{
struct target_ops *beneath = find_target_beneath (ops);
return beneath->to_pid_to_str (beneath, ptid);
}
/* Free previous return value; a new one will be allocated by
xstrprintf(). */
xfree (ret);
ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
return ret;
}
/* A "new-objfile" observer. Used to activate/deactivate dec-thread
support. */
static void
dec_thread_new_objfile_observer (struct objfile *objfile)
{
if (objfile != NULL)
enable_dec_thread ();
else
disable_dec_thread ();
}
/* The "to_get_ada_task_ptid" method of the dec_thread_ops. */
static ptid_t
dec_thread_get_ada_task_ptid (long lwp, long thread)
{
int i;
struct dec_thread_info *info;
debug ("dec_thread_get_ada_task_ptid (lwp=0x%lx, thread=0x%lx)",
lwp, thread);
for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info);
i++)
if (info->info.teb == (pthread_t) thread)
return ptid_build_from_info (*info);
warning (_("Could not find thread id from THREAD = 0x%lx\n"), thread);
return inferior_ptid;
}
static void
init_dec_thread_ops (void)
{
dec_thread_ops.to_shortname = "dec-threads";
dec_thread_ops.to_longname = _("DEC threads support");
dec_thread_ops.to_doc = _("DEC threads support");
dec_thread_ops.to_detach = dec_thread_detach;
dec_thread_ops.to_wait = dec_thread_wait;
dec_thread_ops.to_fetch_registers = dec_thread_fetch_registers;
dec_thread_ops.to_store_registers = dec_thread_store_registers;
dec_thread_ops.to_mourn_inferior = dec_thread_mourn_inferior;
dec_thread_ops.to_thread_alive = dec_thread_thread_alive;
dec_thread_ops.to_pid_to_str = dec_thread_pid_to_str;
dec_thread_ops.to_stratum = thread_stratum;
dec_thread_ops.to_get_ada_task_ptid = dec_thread_get_ada_task_ptid;
dec_thread_ops.to_magic = OPS_MAGIC;
}
void
_initialize_dec_thread (void)
{
init_dec_thread_ops ();
add_target (&dec_thread_ops);
observer_attach_new_objfile (dec_thread_new_objfile_observer);
add_setshow_boolean_cmd ("dec-thread", class_maintenance, &debug_dec_thread,
_("Set debugging of DEC threads module."),
_("Show debugging of DEC threads module."),
_("Enables debugging output (used to debug GDB)."),
NULL, NULL,
&setdebuglist, &showdebuglist);
}
|