gdb/testsuite: Handle underlying type in gdb.cp/var-tag.exp.
[deliverable/binutils-gdb.git] / gdb / aix-thread.c
1 /* Low level interface for debugging AIX 4.3+ pthreads.
2
3 Copyright (C) 1999-2014 Free Software Foundation, Inc.
4 Written by Nick Duffek <nsd@redhat.com>.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
23 debugging pthread applications.
24
25 Some name prefix conventions:
26 pthdb_ provided by libpthdebug.a
27 pdc_ callbacks that this module provides to libpthdebug.a
28 pd_ variables or functions interfacing with libpthdebug.a
29
30 libpthdebug peculiarities:
31
32 - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but
33 it's not documented, and after several calls it stops working
34 and causes other libpthdebug functions to fail.
35
36 - pthdb_tid_pthread() doesn't always work after
37 pthdb_session_update(), but it does work after cycling through
38 all threads using pthdb_pthread().
39
40 */
41
42 #include "defs.h"
43 #include "gdb_assert.h"
44 #include "gdbthread.h"
45 #include "target.h"
46 #include "inferior.h"
47 #include "regcache.h"
48 #include "gdbcmd.h"
49 #include "ppc-tdep.h"
50 #include <string.h>
51 #include "observer.h"
52 #include "objfiles.h"
53
54 #include <procinfo.h>
55 #include <sys/types.h>
56 #include <sys/ptrace.h>
57 #include <sys/reg.h>
58 #include <sched.h>
59 #include <sys/pthdebug.h>
60
61 #if !HAVE_DECL_GETTHRDS
62 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
63 #endif
64
65 /* Whether to emit debugging output. */
66 static int debug_aix_thread;
67
68 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
69 #ifndef PTHDB_VERSION_3
70 #define pthdb_tid_t tid_t
71 #endif
72
73 /* Return whether to treat PID as a debuggable thread id. */
74
75 #define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
76
77 /* pthdb_user_t value that we pass to pthdb functions. 0 causes
78 PTHDB_BAD_USER errors, so use 1. */
79
80 #define PD_USER 1
81
82 /* Success and failure values returned by pthdb callbacks. */
83
84 #define PDC_SUCCESS PTHDB_SUCCESS
85 #define PDC_FAILURE PTHDB_CALLBACK
86
87 /* Private data attached to each element in GDB's thread list. */
88
89 struct private_thread_info {
90 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
91 pthdb_tid_t tid; /* kernel thread id */
92 };
93
94 /* Information about a thread of which libpthdebug is aware. */
95
96 struct pd_thread {
97 pthdb_pthread_t pdtid;
98 pthread_t pthid;
99 pthdb_tid_t tid;
100 };
101
102 /* This module's target-specific operations, active while pd_able is true. */
103
104 static struct target_ops aix_thread_ops;
105
106 /* Address of the function that libpthread will call when libpthdebug
107 is ready to be initialized. */
108
109 static CORE_ADDR pd_brk_addr;
110
111 /* Whether the current application is debuggable by pthdb. */
112
113 static int pd_able = 0;
114
115 /* Whether a threaded application is being debugged. */
116
117 static int pd_active = 0;
118
119 /* Whether the current architecture is 64-bit.
120 Only valid when pd_able is true. */
121
122 static int arch64;
123
124 /* Forward declarations for pthdb callbacks. */
125
126 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
127 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
128 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
129 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
130 unsigned long long flags,
131 pthdb_context_t *context);
132 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
133 unsigned long long flags,
134 pthdb_context_t *context);
135 static int pdc_alloc (pthdb_user_t, size_t, void **);
136 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
137 static int pdc_dealloc (pthdb_user_t, void *);
138
139 /* pthdb callbacks. */
140
141 static pthdb_callbacks_t pd_callbacks = {
142 pdc_symbol_addrs,
143 pdc_read_data,
144 pdc_write_data,
145 pdc_read_regs,
146 pdc_write_regs,
147 pdc_alloc,
148 pdc_realloc,
149 pdc_dealloc,
150 NULL
151 };
152
153 /* Current pthdb session. */
154
155 static pthdb_session_t pd_session;
156
157 /* Return a printable representation of pthdebug function return
158 STATUS. */
159
160 static char *
161 pd_status2str (int status)
162 {
163 switch (status)
164 {
165 case PTHDB_SUCCESS: return "SUCCESS";
166 case PTHDB_NOSYS: return "NOSYS";
167 case PTHDB_NOTSUP: return "NOTSUP";
168 case PTHDB_BAD_VERSION: return "BAD_VERSION";
169 case PTHDB_BAD_USER: return "BAD_USER";
170 case PTHDB_BAD_SESSION: return "BAD_SESSION";
171 case PTHDB_BAD_MODE: return "BAD_MODE";
172 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
173 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
174 case PTHDB_BAD_POINTER: return "BAD_POINTER";
175 case PTHDB_BAD_CMD: return "BAD_CMD";
176 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
177 case PTHDB_BAD_ATTR: return "BAD_ATTR";
178 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
179 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
180 case PTHDB_BAD_COND: return "BAD_COND";
181 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
182 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
183 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
184 case PTHDB_BAD_KEY: return "BAD_KEY";
185 case PTHDB_BAD_PTID: return "BAD_PTID";
186 case PTHDB_BAD_TID: return "BAD_TID";
187 case PTHDB_CALLBACK: return "CALLBACK";
188 case PTHDB_CONTEXT: return "CONTEXT";
189 case PTHDB_HELD: return "HELD";
190 case PTHDB_NOT_HELD: return "NOT_HELD";
191 case PTHDB_MEMORY: return "MEMORY";
192 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
193 case PTHDB_SYMBOL: return "SYMBOL";
194 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
195 case PTHDB_INTERNAL: return "INTERNAL";
196 default: return "UNKNOWN";
197 }
198 }
199
200 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
201 exceptional conditions and either return nonlocally or else return
202 1 for success and 0 for failure. */
203
204 static int
205 ptrace_check (int req, int id, int ret)
206 {
207 if (ret == 0 && !errno)
208 return 1;
209
210 /* According to ptrace(2), ptrace may fail with EPERM if "the
211 Identifier parameter corresponds to a kernel thread which is
212 stopped in kernel mode and whose computational state cannot be
213 read or written." This happens quite often with register reads. */
214
215 switch (req)
216 {
217 case PTT_READ_GPRS:
218 case PTT_READ_FPRS:
219 case PTT_READ_SPRS:
220 if (ret == -1 && errno == EPERM)
221 {
222 if (debug_aix_thread)
223 fprintf_unfiltered (gdb_stdlog,
224 "ptrace (%d, %d) = %d (errno = %d)\n",
225 req, id, ret, errno);
226 return ret == -1 ? 0 : 1;
227 }
228 break;
229 }
230 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
231 req, id, ret, errno, safe_strerror (errno));
232 return 0; /* Not reached. */
233 }
234
235 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
236 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
237 Return success. */
238
239 #ifdef HAVE_PTRACE64
240 # define ptracex(request, pid, addr, data, buf) \
241 ptrace64 (request, pid, addr, data, buf)
242 #endif
243
244 static int
245 ptrace64aix (int req, int id, long long addr, int data, int *buf)
246 {
247 errno = 0;
248 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
249 }
250
251 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
252 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
253 Return success. */
254
255 #ifdef HAVE_PTRACE64
256 # define ptrace(request, pid, addr, data, buf) \
257 ptrace64 (request, pid, addr, data, buf)
258 # define addr_ptr long long
259 #else
260 # define addr_ptr int *
261 #endif
262
263 static int
264 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
265 {
266 errno = 0;
267 return ptrace_check (req, id,
268 ptrace (req, id, addr, data, buf));
269 }
270
271 /* If *PIDP is a composite process/thread id, convert it to a
272 process id. */
273
274 static void
275 pid_to_prc (ptid_t *ptidp)
276 {
277 ptid_t ptid;
278
279 ptid = *ptidp;
280 if (PD_TID (ptid))
281 *ptidp = pid_to_ptid (ptid_get_pid (ptid));
282 }
283
284 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
285 the address of SYMBOLS[<i>].name. */
286
287 static int
288 pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
289 {
290 struct bound_minimal_symbol ms;
291 int i;
292 char *name;
293
294 if (debug_aix_thread)
295 fprintf_unfiltered (gdb_stdlog,
296 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
297 user, (long) symbols, count);
298
299 for (i = 0; i < count; i++)
300 {
301 name = symbols[i].name;
302 if (debug_aix_thread)
303 fprintf_unfiltered (gdb_stdlog,
304 " symbols[%d].name = \"%s\"\n", i, name);
305
306 if (!*name)
307 symbols[i].addr = 0;
308 else
309 {
310 ms = lookup_minimal_symbol (name, NULL, NULL);
311 if (ms.minsym == NULL)
312 {
313 if (debug_aix_thread)
314 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE\n");
315 return PDC_FAILURE;
316 }
317 symbols[i].addr = BMSYMBOL_VALUE_ADDRESS (ms);
318 }
319 if (debug_aix_thread)
320 fprintf_unfiltered (gdb_stdlog, " symbols[%d].addr = %s\n",
321 i, hex_string (symbols[i].addr));
322 }
323 if (debug_aix_thread)
324 fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS\n");
325 return PDC_SUCCESS;
326 }
327
328 /* Read registers call back function should be able to read the
329 context information of a debuggee kernel thread from an active
330 process or from a core file. The information should be formatted
331 in context64 form for both 32-bit and 64-bit process.
332 If successful return 0, else non-zero is returned. */
333
334 static int
335 pdc_read_regs (pthdb_user_t user,
336 pthdb_tid_t tid,
337 unsigned long long flags,
338 pthdb_context_t *context)
339 {
340 /* This function doesn't appear to be used, so we could probably
341 just return 0 here. HOWEVER, if it is not defined, the OS will
342 complain and several thread debug functions will fail. In case
343 this is needed, I have implemented what I think it should do,
344 however this code is untested. */
345
346 uint64_t gprs64[ppc_num_gprs];
347 uint32_t gprs32[ppc_num_gprs];
348 double fprs[ppc_num_fprs];
349 struct ptxsprs sprs64;
350 struct ptsprs sprs32;
351
352 if (debug_aix_thread)
353 fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
354 (int) tid, hex_string (flags));
355
356 /* General-purpose registers. */
357 if (flags & PTHDB_FLAG_GPRS)
358 {
359 if (arch64)
360 {
361 if (!ptrace64aix (PTT_READ_GPRS, tid,
362 (unsigned long) gprs64, 0, NULL))
363 memset (gprs64, 0, sizeof (gprs64));
364 memcpy (context->gpr, gprs64, sizeof(gprs64));
365 }
366 else
367 {
368 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
369 memset (gprs32, 0, sizeof (gprs32));
370 memcpy (context->gpr, gprs32, sizeof(gprs32));
371 }
372 }
373
374 /* Floating-point registers. */
375 if (flags & PTHDB_FLAG_FPRS)
376 {
377 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
378 memset (fprs, 0, sizeof (fprs));
379 memcpy (context->fpr, fprs, sizeof(fprs));
380 }
381
382 /* Special-purpose registers. */
383 if (flags & PTHDB_FLAG_SPRS)
384 {
385 if (arch64)
386 {
387 if (!ptrace64aix (PTT_READ_SPRS, tid,
388 (unsigned long) &sprs64, 0, NULL))
389 memset (&sprs64, 0, sizeof (sprs64));
390 memcpy (&context->msr, &sprs64, sizeof(sprs64));
391 }
392 else
393 {
394 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
395 memset (&sprs32, 0, sizeof (sprs32));
396 memcpy (&context->msr, &sprs32, sizeof(sprs32));
397 }
398 }
399 return 0;
400 }
401
402 /* Write register function should be able to write requested context
403 information to specified debuggee's kernel thread id.
404 If successful return 0, else non-zero is returned. */
405
406 static int
407 pdc_write_regs (pthdb_user_t user,
408 pthdb_tid_t tid,
409 unsigned long long flags,
410 pthdb_context_t *context)
411 {
412 /* This function doesn't appear to be used, so we could probably
413 just return 0 here. HOWEVER, if it is not defined, the OS will
414 complain and several thread debug functions will fail. In case
415 this is needed, I have implemented what I think it should do,
416 however this code is untested. */
417
418 if (debug_aix_thread)
419 fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
420 (int) tid, hex_string (flags));
421
422 /* General-purpose registers. */
423 if (flags & PTHDB_FLAG_GPRS)
424 {
425 if (arch64)
426 ptrace64aix (PTT_WRITE_GPRS, tid,
427 (unsigned long) context->gpr, 0, NULL);
428 else
429 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
430 }
431
432 /* Floating-point registers. */
433 if (flags & PTHDB_FLAG_FPRS)
434 {
435 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
436 }
437
438 /* Special-purpose registers. */
439 if (flags & PTHDB_FLAG_SPRS)
440 {
441 if (arch64)
442 {
443 ptrace64aix (PTT_WRITE_SPRS, tid,
444 (unsigned long) &context->msr, 0, NULL);
445 }
446 else
447 {
448 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
449 }
450 }
451 return 0;
452 }
453
454 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
455
456 static int
457 pdc_read_data (pthdb_user_t user, void *buf,
458 pthdb_addr_t addr, size_t len)
459 {
460 int status, ret;
461
462 if (debug_aix_thread)
463 fprintf_unfiltered (gdb_stdlog,
464 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
465 user, (long) buf, hex_string (addr), len);
466
467 status = target_read_memory (addr, buf, len);
468 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
469
470 if (debug_aix_thread)
471 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n",
472 status, pd_status2str (ret));
473 return ret;
474 }
475
476 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
477
478 static int
479 pdc_write_data (pthdb_user_t user, void *buf,
480 pthdb_addr_t addr, size_t len)
481 {
482 int status, ret;
483
484 if (debug_aix_thread)
485 fprintf_unfiltered (gdb_stdlog,
486 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
487 user, (long) buf, hex_string (addr), len);
488
489 status = target_write_memory (addr, buf, len);
490 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
491
492 if (debug_aix_thread)
493 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n", status,
494 pd_status2str (ret));
495 return ret;
496 }
497
498 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
499 in BUFP. */
500
501 static int
502 pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
503 {
504 if (debug_aix_thread)
505 fprintf_unfiltered (gdb_stdlog,
506 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
507 user, len, (long) bufp);
508 *bufp = xmalloc (len);
509 if (debug_aix_thread)
510 fprintf_unfiltered (gdb_stdlog,
511 " malloc returned 0x%lx\n", (long) *bufp);
512
513 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
514 be returned. */
515
516 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
517 }
518
519 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
520 realloc callback, so that it contains LEN bytes, and store a
521 pointer to the result in BUFP. */
522
523 static int
524 pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
525 {
526 if (debug_aix_thread)
527 fprintf_unfiltered (gdb_stdlog,
528 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
529 user, (long) buf, len, (long) bufp);
530 *bufp = xrealloc (buf, len);
531 if (debug_aix_thread)
532 fprintf_unfiltered (gdb_stdlog,
533 " realloc returned 0x%lx\n", (long) *bufp);
534 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
535 }
536
537 /* pthdb callback: free BUF, which was allocated by the alloc or
538 realloc callback. */
539
540 static int
541 pdc_dealloc (pthdb_user_t user, void *buf)
542 {
543 if (debug_aix_thread)
544 fprintf_unfiltered (gdb_stdlog,
545 "pdc_free (user = %ld, buf = 0x%lx)\n", user,
546 (long) buf);
547 xfree (buf);
548 return PDC_SUCCESS;
549 }
550
551 /* Return a printable representation of pthread STATE. */
552
553 static char *
554 state2str (pthdb_state_t state)
555 {
556 switch (state)
557 {
558 case PST_IDLE:
559 /* i18n: Like "Thread-Id %d, [state] idle" */
560 return _("idle"); /* being created */
561 case PST_RUN:
562 /* i18n: Like "Thread-Id %d, [state] running" */
563 return _("running"); /* running */
564 case PST_SLEEP:
565 /* i18n: Like "Thread-Id %d, [state] sleeping" */
566 return _("sleeping"); /* awaiting an event */
567 case PST_READY:
568 /* i18n: Like "Thread-Id %d, [state] ready" */
569 return _("ready"); /* runnable */
570 case PST_TERM:
571 /* i18n: Like "Thread-Id %d, [state] finished" */
572 return _("finished"); /* awaiting a join/detach */
573 default:
574 /* i18n: Like "Thread-Id %d, [state] unknown" */
575 return _("unknown");
576 }
577 }
578
579 /* qsort() comparison function for sorting pd_thread structs by pthid. */
580
581 static int
582 pcmp (const void *p1v, const void *p2v)
583 {
584 struct pd_thread *p1 = (struct pd_thread *) p1v;
585 struct pd_thread *p2 = (struct pd_thread *) p2v;
586 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
587 }
588
589 /* iterate_over_threads() callback for counting GDB threads.
590
591 Do not count the main thread (whose tid is zero). This matches
592 the list of threads provided by the pthreaddebug library, which
593 does not include that main thread either, and thus allows us
594 to compare the two lists. */
595
596 static int
597 giter_count (struct thread_info *thread, void *countp)
598 {
599 if (PD_TID (thread->ptid))
600 (*(int *) countp)++;
601 return 0;
602 }
603
604 /* iterate_over_threads() callback for accumulating GDB thread pids.
605
606 Do not include the main thread (whose tid is zero). This matches
607 the list of threads provided by the pthreaddebug library, which
608 does not include that main thread either, and thus allows us
609 to compare the two lists. */
610
611 static int
612 giter_accum (struct thread_info *thread, void *bufp)
613 {
614 if (PD_TID (thread->ptid))
615 {
616 **(struct thread_info ***) bufp = thread;
617 (*(struct thread_info ***) bufp)++;
618 }
619 return 0;
620 }
621
622 /* ptid comparison function */
623
624 static int
625 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
626 {
627 int pid1, pid2;
628
629 if (ptid_get_pid (ptid1) < ptid_get_pid (ptid2))
630 return -1;
631 else if (ptid_get_pid (ptid1) > ptid_get_pid (ptid2))
632 return 1;
633 else if (ptid_get_tid (ptid1) < ptid_get_tid (ptid2))
634 return -1;
635 else if (ptid_get_tid (ptid1) > ptid_get_tid (ptid2))
636 return 1;
637 else if (ptid_get_lwp (ptid1) < ptid_get_lwp (ptid2))
638 return -1;
639 else if (ptid_get_lwp (ptid1) > ptid_get_lwp (ptid2))
640 return 1;
641 else
642 return 0;
643 }
644
645 /* qsort() comparison function for sorting thread_info structs by pid. */
646
647 static int
648 gcmp (const void *t1v, const void *t2v)
649 {
650 struct thread_info *t1 = *(struct thread_info **) t1v;
651 struct thread_info *t2 = *(struct thread_info **) t2v;
652 return ptid_cmp (t1->ptid, t2->ptid);
653 }
654
655 /* Search through the list of all kernel threads for the thread
656 that has stopped on a SIGTRAP signal, and return its TID.
657 Return 0 if none found. */
658
659 static pthdb_tid_t
660 get_signaled_thread (void)
661 {
662 struct thrdsinfo64 thrinf;
663 tid_t ktid = 0;
664 int result = 0;
665
666 while (1)
667 {
668 if (getthrds (ptid_get_pid (inferior_ptid), &thrinf,
669 sizeof (thrinf), &ktid, 1) != 1)
670 break;
671
672 if (thrinf.ti_cursig == SIGTRAP)
673 return thrinf.ti_tid;
674 }
675
676 /* Didn't find any thread stopped on a SIGTRAP signal. */
677 return 0;
678 }
679
680 /* Synchronize GDB's thread list with libpthdebug's.
681
682 There are some benefits of doing this every time the inferior stops:
683
684 - allows users to run thread-specific commands without needing to
685 run "info threads" first
686
687 - helps pthdb_tid_pthread() work properly (see "libpthdebug
688 peculiarities" at the top of this module)
689
690 - simplifies the demands placed on libpthdebug, which seems to
691 have difficulty with certain call patterns */
692
693 static void
694 sync_threadlists (void)
695 {
696 int cmd, status, infpid;
697 int pcount, psize, pi, gcount, gi;
698 struct pd_thread *pbuf;
699 struct thread_info **gbuf, **g, *thread;
700 pthdb_pthread_t pdtid;
701 pthread_t pthid;
702 pthdb_tid_t tid;
703
704 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
705
706 pcount = 0;
707 psize = 1;
708 pbuf = (struct pd_thread *) xmalloc (psize * sizeof *pbuf);
709
710 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
711 {
712 status = pthdb_pthread (pd_session, &pdtid, cmd);
713 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
714 break;
715
716 status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
717 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
718 continue;
719
720 if (pcount == psize)
721 {
722 psize *= 2;
723 pbuf = (struct pd_thread *) xrealloc (pbuf,
724 psize * sizeof *pbuf);
725 }
726 pbuf[pcount].pdtid = pdtid;
727 pbuf[pcount].pthid = pthid;
728 pcount++;
729 }
730
731 for (pi = 0; pi < pcount; pi++)
732 {
733 status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
734 if (status != PTHDB_SUCCESS)
735 tid = PTHDB_INVALID_TID;
736 pbuf[pi].tid = tid;
737 }
738
739 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
740
741 /* Accumulate an array of GDB threads sorted by pid. */
742
743 gcount = 0;
744 iterate_over_threads (giter_count, &gcount);
745 g = gbuf = (struct thread_info **) xmalloc (gcount * sizeof *gbuf);
746 iterate_over_threads (giter_accum, &g);
747 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
748
749 /* Apply differences between the two arrays to GDB's thread list. */
750
751 infpid = ptid_get_pid (inferior_ptid);
752 for (pi = gi = 0; pi < pcount || gi < gcount;)
753 {
754 if (pi == pcount)
755 {
756 delete_thread (gbuf[gi]->ptid);
757 gi++;
758 }
759 else if (gi == gcount)
760 {
761 thread = add_thread (ptid_build (infpid, 0, pbuf[pi].pthid));
762 thread->private = xmalloc (sizeof (struct private_thread_info));
763 thread->private->pdtid = pbuf[pi].pdtid;
764 thread->private->tid = pbuf[pi].tid;
765 pi++;
766 }
767 else
768 {
769 ptid_t pptid, gptid;
770 int cmp_result;
771
772 pptid = ptid_build (infpid, 0, pbuf[pi].pthid);
773 gptid = gbuf[gi]->ptid;
774 pdtid = pbuf[pi].pdtid;
775 tid = pbuf[pi].tid;
776
777 cmp_result = ptid_cmp (pptid, gptid);
778
779 if (cmp_result == 0)
780 {
781 gbuf[gi]->private->pdtid = pdtid;
782 gbuf[gi]->private->tid = tid;
783 pi++;
784 gi++;
785 }
786 else if (cmp_result > 0)
787 {
788 delete_thread (gptid);
789 gi++;
790 }
791 else
792 {
793 thread = add_thread (pptid);
794 thread->private = xmalloc (sizeof (struct private_thread_info));
795 thread->private->pdtid = pdtid;
796 thread->private->tid = tid;
797 pi++;
798 }
799 }
800 }
801
802 xfree (pbuf);
803 xfree (gbuf);
804 }
805
806 /* Iterate_over_threads() callback for locating a thread, using
807 the TID of its associated kernel thread. */
808
809 static int
810 iter_tid (struct thread_info *thread, void *tidp)
811 {
812 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
813
814 return (thread->private->tid == tid);
815 }
816
817 /* Synchronize libpthdebug's state with the inferior and with GDB,
818 generate a composite process/thread <pid> for the current thread,
819 set inferior_ptid to <pid> if SET_INFPID, and return <pid>. */
820
821 static ptid_t
822 pd_update (int set_infpid)
823 {
824 int status;
825 ptid_t ptid;
826 pthdb_tid_t tid;
827 struct thread_info *thread = NULL;
828
829 if (!pd_active)
830 return inferior_ptid;
831
832 status = pthdb_session_update (pd_session);
833 if (status != PTHDB_SUCCESS)
834 return inferior_ptid;
835
836 sync_threadlists ();
837
838 /* Define "current thread" as one that just received a trap signal. */
839
840 tid = get_signaled_thread ();
841 if (tid != 0)
842 thread = iterate_over_threads (iter_tid, &tid);
843 if (!thread)
844 ptid = inferior_ptid;
845 else
846 {
847 ptid = thread->ptid;
848 if (set_infpid)
849 inferior_ptid = ptid;
850 }
851 return ptid;
852 }
853
854 /* Try to start debugging threads in the current process.
855 If successful and SET_INFPID, set inferior_ptid to reflect the
856 current thread. */
857
858 static ptid_t
859 pd_activate (int set_infpid)
860 {
861 int status;
862
863 status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
864 PTHDB_FLAG_REGS, &pd_callbacks,
865 &pd_session);
866 if (status != PTHDB_SUCCESS)
867 {
868 return inferior_ptid;
869 }
870 pd_active = 1;
871 return pd_update (set_infpid);
872 }
873
874 /* Undo the effects of pd_activate(). */
875
876 static void
877 pd_deactivate (void)
878 {
879 if (!pd_active)
880 return;
881 pthdb_session_destroy (pd_session);
882
883 pid_to_prc (&inferior_ptid);
884 pd_active = 0;
885 }
886
887 /* An object file has just been loaded. Check whether the current
888 application is pthreaded, and if so, prepare for thread debugging. */
889
890 static void
891 pd_enable (void)
892 {
893 int status;
894 char *stub_name;
895 struct bound_minimal_symbol ms;
896
897 /* Don't initialize twice. */
898 if (pd_able)
899 return;
900
901 /* Check application word size. */
902 arch64 = register_size (target_gdbarch (), 0) == 8;
903
904 /* Check whether the application is pthreaded. */
905 stub_name = NULL;
906 status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
907 &pd_callbacks, &stub_name);
908 if ((status != PTHDB_SUCCESS
909 && status != PTHDB_NOT_PTHREADED) || !stub_name)
910 return;
911
912 /* Set a breakpoint on the returned stub function. */
913 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
914 if (ms.minsym == NULL)
915 return;
916 pd_brk_addr = BMSYMBOL_VALUE_ADDRESS (ms);
917 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
918 return;
919
920 /* Prepare for thread debugging. */
921 push_target (&aix_thread_ops);
922 pd_able = 1;
923
924 /* If we're debugging a core file or an attached inferior, the
925 pthread library may already have been initialized, so try to
926 activate thread debugging. */
927 pd_activate (1);
928 }
929
930 /* Undo the effects of pd_enable(). */
931
932 static void
933 pd_disable (void)
934 {
935 if (!pd_able)
936 return;
937 if (pd_active)
938 pd_deactivate ();
939 pd_able = 0;
940 unpush_target (&aix_thread_ops);
941 }
942
943 /* new_objfile observer callback.
944
945 If OBJFILE is non-null, check whether a threaded application is
946 being debugged, and if so, prepare for thread debugging.
947
948 If OBJFILE is null, stop debugging threads. */
949
950 static void
951 new_objfile (struct objfile *objfile)
952 {
953 if (objfile)
954 pd_enable ();
955 else
956 pd_disable ();
957 }
958
959 /* Attach to process specified by ARGS. */
960
961 static void
962 aix_thread_inferior_created (struct target_ops *ops, int from_tty)
963 {
964 pd_enable ();
965 }
966
967 /* Detach from the process attached to by aix_thread_attach(). */
968
969 static void
970 aix_thread_detach (struct target_ops *ops, const char *args, int from_tty)
971 {
972 struct target_ops *beneath = find_target_beneath (ops);
973
974 pd_disable ();
975 beneath->to_detach (beneath, args, from_tty);
976 }
977
978 /* Tell the inferior process to continue running thread PID if != -1
979 and all threads otherwise. */
980
981 static void
982 aix_thread_resume (struct target_ops *ops,
983 ptid_t ptid, int step, enum gdb_signal sig)
984 {
985 struct thread_info *thread;
986 pthdb_tid_t tid[2];
987
988 if (!PD_TID (ptid))
989 {
990 struct cleanup *cleanup = save_inferior_ptid ();
991 struct target_ops *beneath = find_target_beneath (ops);
992
993 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
994 beneath->to_resume (beneath, ptid, step, sig);
995 do_cleanups (cleanup);
996 }
997 else
998 {
999 thread = find_thread_ptid (ptid);
1000 if (!thread)
1001 error (_("aix-thread resume: unknown pthread %ld"),
1002 ptid_get_lwp (ptid));
1003
1004 tid[0] = thread->private->tid;
1005 if (tid[0] == PTHDB_INVALID_TID)
1006 error (_("aix-thread resume: no tid for pthread %ld"),
1007 ptid_get_lwp (ptid));
1008 tid[1] = 0;
1009
1010 if (arch64)
1011 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1012 gdb_signal_to_host (sig), (void *) tid);
1013 else
1014 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1015 gdb_signal_to_host (sig), (void *) tid);
1016 }
1017 }
1018
1019 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1020 If an error occurs, return -1, else return the pid of the stopped
1021 thread. */
1022
1023 static ptid_t
1024 aix_thread_wait (struct target_ops *ops,
1025 ptid_t ptid, struct target_waitstatus *status, int options)
1026 {
1027 struct cleanup *cleanup = save_inferior_ptid ();
1028 struct target_ops *beneath = find_target_beneath (ops);
1029
1030 pid_to_prc (&ptid);
1031
1032 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1033 ptid = beneath->to_wait (beneath, ptid, status, options);
1034 do_cleanups (cleanup);
1035
1036 if (ptid_get_pid (ptid) == -1)
1037 return pid_to_ptid (-1);
1038
1039 /* Check whether libpthdebug might be ready to be initialized. */
1040 if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
1041 && status->value.sig == GDB_SIGNAL_TRAP)
1042 {
1043 struct regcache *regcache = get_thread_regcache (ptid);
1044 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1045
1046 if (regcache_read_pc (regcache)
1047 - target_decr_pc_after_break (gdbarch) == pd_brk_addr)
1048 return pd_activate (0);
1049 }
1050
1051 return pd_update (0);
1052 }
1053
1054 /* Record that the 64-bit general-purpose registers contain VALS. */
1055
1056 static void
1057 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1058 {
1059 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1060 int regno;
1061
1062 for (regno = 0; regno < ppc_num_gprs; regno++)
1063 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + regno,
1064 (char *) (vals + regno));
1065 }
1066
1067 /* Record that 32-bit register REGNO contains VAL. */
1068
1069 static void
1070 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1071 {
1072 regcache_raw_supply (regcache, regno, (char *) &val);
1073 }
1074
1075 /* Record that the floating-point registers contain VALS. */
1076
1077 static void
1078 supply_fprs (struct regcache *regcache, double *vals)
1079 {
1080 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1081 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1082 int regno;
1083
1084 /* This function should never be called on architectures without
1085 floating-point registers. */
1086 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1087
1088 for (regno = tdep->ppc_fp0_regnum;
1089 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1090 regno++)
1091 regcache_raw_supply (regcache, regno,
1092 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1093 }
1094
1095 /* Predicate to test whether given register number is a "special" register. */
1096 static int
1097 special_register_p (struct gdbarch *gdbarch, int regno)
1098 {
1099 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1100
1101 return regno == gdbarch_pc_regnum (gdbarch)
1102 || regno == tdep->ppc_ps_regnum
1103 || regno == tdep->ppc_cr_regnum
1104 || regno == tdep->ppc_lr_regnum
1105 || regno == tdep->ppc_ctr_regnum
1106 || regno == tdep->ppc_xer_regnum
1107 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1108 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1109 }
1110
1111
1112 /* Record that the special registers contain the specified 64-bit and
1113 32-bit values. */
1114
1115 static void
1116 supply_sprs64 (struct regcache *regcache,
1117 uint64_t iar, uint64_t msr, uint32_t cr,
1118 uint64_t lr, uint64_t ctr, uint32_t xer,
1119 uint32_t fpscr)
1120 {
1121 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1122 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1123
1124 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1125 (char *) &iar);
1126 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1127 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1128 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1129 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1130 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1131 if (tdep->ppc_fpscr_regnum >= 0)
1132 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1133 (char *) &fpscr);
1134 }
1135
1136 /* Record that the special registers contain the specified 32-bit
1137 values. */
1138
1139 static void
1140 supply_sprs32 (struct regcache *regcache,
1141 uint32_t iar, uint32_t msr, uint32_t cr,
1142 uint32_t lr, uint32_t ctr, uint32_t xer,
1143 uint32_t fpscr)
1144 {
1145 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1146 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1147
1148 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
1149 (char *) &iar);
1150 regcache_raw_supply (regcache, tdep->ppc_ps_regnum, (char *) &msr);
1151 regcache_raw_supply (regcache, tdep->ppc_cr_regnum, (char *) &cr);
1152 regcache_raw_supply (regcache, tdep->ppc_lr_regnum, (char *) &lr);
1153 regcache_raw_supply (regcache, tdep->ppc_ctr_regnum, (char *) &ctr);
1154 regcache_raw_supply (regcache, tdep->ppc_xer_regnum, (char *) &xer);
1155 if (tdep->ppc_fpscr_regnum >= 0)
1156 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
1157 (char *) &fpscr);
1158 }
1159
1160 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1161 thread.
1162
1163 There's no way to query a single register from a non-kernel
1164 pthread, so there's no need for a single-register version of this
1165 function. */
1166
1167 static void
1168 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1169 {
1170 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1171 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1172 int status, i;
1173 pthdb_context_t ctx;
1174
1175 if (debug_aix_thread)
1176 fprintf_unfiltered (gdb_stdlog,
1177 "fetch_regs_user_thread %lx\n", (long) pdtid);
1178 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1179 if (status != PTHDB_SUCCESS)
1180 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1181 pd_status2str (status));
1182
1183 /* General-purpose registers. */
1184
1185 if (arch64)
1186 supply_gprs64 (regcache, ctx.gpr);
1187 else
1188 for (i = 0; i < ppc_num_gprs; i++)
1189 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1190
1191 /* Floating-point registers. */
1192
1193 if (ppc_floating_point_unit_p (gdbarch))
1194 supply_fprs (regcache, ctx.fpr);
1195
1196 /* Special registers. */
1197
1198 if (arch64)
1199 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1200 ctx.xer, ctx.fpscr);
1201 else
1202 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1203 ctx.xer, ctx.fpscr);
1204 }
1205
1206 /* Fetch register REGNO if != -1 or all registers otherwise from
1207 kernel thread TID.
1208
1209 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1210 SPRs, but there's no way to query individual registers within those
1211 groups. Therefore, if REGNO != -1, this function fetches an entire
1212 group.
1213
1214 Unfortunately, kernel thread register queries often fail with
1215 EPERM, indicating that the thread is in kernel space. This breaks
1216 backtraces of threads other than the current one. To make that
1217 breakage obvious without throwing an error to top level (which is
1218 bad e.g. during "info threads" output), zero registers that can't
1219 be retrieved. */
1220
1221 static void
1222 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1223 pthdb_tid_t tid)
1224 {
1225 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1226 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1227 uint64_t gprs64[ppc_num_gprs];
1228 uint32_t gprs32[ppc_num_gprs];
1229 double fprs[ppc_num_fprs];
1230 struct ptxsprs sprs64;
1231 struct ptsprs sprs32;
1232 int i;
1233
1234 if (debug_aix_thread)
1235 fprintf_unfiltered (gdb_stdlog,
1236 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1237 (long) tid, regno, arch64);
1238
1239 /* General-purpose registers. */
1240 if (regno == -1
1241 || (tdep->ppc_gp0_regnum <= regno
1242 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1243 {
1244 if (arch64)
1245 {
1246 if (!ptrace64aix (PTT_READ_GPRS, tid,
1247 (unsigned long) gprs64, 0, NULL))
1248 memset (gprs64, 0, sizeof (gprs64));
1249 supply_gprs64 (regcache, gprs64);
1250 }
1251 else
1252 {
1253 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1254 memset (gprs32, 0, sizeof (gprs32));
1255 for (i = 0; i < ppc_num_gprs; i++)
1256 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1257 }
1258 }
1259
1260 /* Floating-point registers. */
1261
1262 if (ppc_floating_point_unit_p (gdbarch)
1263 && (regno == -1
1264 || (regno >= tdep->ppc_fp0_regnum
1265 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1266 {
1267 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1268 memset (fprs, 0, sizeof (fprs));
1269 supply_fprs (regcache, fprs);
1270 }
1271
1272 /* Special-purpose registers. */
1273
1274 if (regno == -1 || special_register_p (gdbarch, regno))
1275 {
1276 if (arch64)
1277 {
1278 if (!ptrace64aix (PTT_READ_SPRS, tid,
1279 (unsigned long) &sprs64, 0, NULL))
1280 memset (&sprs64, 0, sizeof (sprs64));
1281 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1282 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1283 sprs64.pt_xer, sprs64.pt_fpscr);
1284 }
1285 else
1286 {
1287 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1288
1289 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1290 memset (&sprs32, 0, sizeof (sprs32));
1291 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1292 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1293 sprs32.pt_fpscr);
1294
1295 if (tdep->ppc_mq_regnum >= 0)
1296 regcache_raw_supply (regcache, tdep->ppc_mq_regnum,
1297 (char *) &sprs32.pt_mq);
1298 }
1299 }
1300 }
1301
1302 /* Fetch register REGNO if != -1 or all registers otherwise in the
1303 thread/process specified by inferior_ptid. */
1304
1305 static void
1306 aix_thread_fetch_registers (struct target_ops *ops,
1307 struct regcache *regcache, int regno)
1308 {
1309 struct thread_info *thread;
1310 pthdb_tid_t tid;
1311 struct target_ops *beneath = find_target_beneath (ops);
1312
1313 if (!PD_TID (inferior_ptid))
1314 beneath->to_fetch_registers (beneath, regcache, regno);
1315 else
1316 {
1317 thread = find_thread_ptid (inferior_ptid);
1318 tid = thread->private->tid;
1319
1320 if (tid == PTHDB_INVALID_TID)
1321 fetch_regs_user_thread (regcache, thread->private->pdtid);
1322 else
1323 fetch_regs_kernel_thread (regcache, regno, tid);
1324 }
1325 }
1326
1327 /* Store the gp registers into an array of uint32_t or uint64_t. */
1328
1329 static void
1330 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1331 {
1332 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1333 int regno;
1334
1335 for (regno = 0; regno < ppc_num_gprs; regno++)
1336 if (REG_VALID == regcache_register_status (regcache,
1337 tdep->ppc_gp0_regnum + regno))
1338 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1339 vals + regno);
1340 }
1341
1342 static void
1343 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1344 {
1345 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1346 int regno;
1347
1348 for (regno = 0; regno < ppc_num_gprs; regno++)
1349 if (REG_VALID == regcache_register_status (regcache,
1350 tdep->ppc_gp0_regnum + regno))
1351 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
1352 vals + regno);
1353 }
1354
1355 /* Store the floating point registers into a double array. */
1356 static void
1357 fill_fprs (const struct regcache *regcache, double *vals)
1358 {
1359 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1360 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1361 int regno;
1362
1363 /* This function should never be called on architectures without
1364 floating-point registers. */
1365 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1366
1367 for (regno = tdep->ppc_fp0_regnum;
1368 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1369 regno++)
1370 if (REG_VALID == regcache_register_status (regcache, regno))
1371 regcache_raw_collect (regcache, regno,
1372 vals + regno - tdep->ppc_fp0_regnum);
1373 }
1374
1375 /* Store the special registers into the specified 64-bit and 32-bit
1376 locations. */
1377
1378 static void
1379 fill_sprs64 (const struct regcache *regcache,
1380 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1381 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1382 uint32_t *fpscr)
1383 {
1384 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1385 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1386
1387 /* Verify that the size of the size of the IAR buffer is the
1388 same as the raw size of the PC (in the register cache). If
1389 they're not, then either GDB has been built incorrectly, or
1390 there's some other kind of internal error. To be really safe,
1391 we should check all of the sizes. */
1392 gdb_assert (sizeof (*iar) == register_size
1393 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1394
1395 if (REG_VALID == regcache_register_status (regcache,
1396 gdbarch_pc_regnum (gdbarch)))
1397 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1398 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1399 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1400 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1401 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1402 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1403 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1404 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1405 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1406 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1407 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1408 if (tdep->ppc_fpscr_regnum >= 0
1409 && REG_VALID == regcache_register_status (regcache,
1410 tdep->ppc_fpscr_regnum))
1411 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1412 }
1413
1414 static void
1415 fill_sprs32 (const struct regcache *regcache,
1416 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1417 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1418 uint32_t *fpscr)
1419 {
1420 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1421 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1422
1423 /* Verify that the size of the size of the IAR buffer is the
1424 same as the raw size of the PC (in the register cache). If
1425 they're not, then either GDB has been built incorrectly, or
1426 there's some other kind of internal error. To be really safe,
1427 we should check all of the sizes. */
1428 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1429 gdbarch_pc_regnum (gdbarch)));
1430
1431 if (REG_VALID == regcache_register_status (regcache,
1432 gdbarch_pc_regnum (gdbarch)))
1433 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
1434 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1435 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
1436 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1437 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
1438 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1439 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
1440 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
1441 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
1442 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
1443 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
1444 if (tdep->ppc_fpscr_regnum >= 0
1445 && REG_VALID == regcache_register_status (regcache, tdep->ppc_fpscr_regnum))
1446 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
1447 }
1448
1449 /* Store all registers into pthread PDTID, which doesn't have a kernel
1450 thread.
1451
1452 It's possible to store a single register into a non-kernel pthread,
1453 but I doubt it's worth the effort. */
1454
1455 static void
1456 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1457 {
1458 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1459 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1460 int status, i;
1461 pthdb_context_t ctx;
1462 uint32_t int32;
1463 uint64_t int64;
1464 double dbl;
1465
1466 if (debug_aix_thread)
1467 fprintf_unfiltered (gdb_stdlog,
1468 "store_regs_user_thread %lx\n", (long) pdtid);
1469
1470 /* Retrieve the thread's current context for its non-register
1471 values. */
1472 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1473 if (status != PTHDB_SUCCESS)
1474 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1475 pd_status2str (status));
1476
1477 /* Collect general-purpose register values from the regcache. */
1478
1479 for (i = 0; i < ppc_num_gprs; i++)
1480 if (REG_VALID == regcache_register_status (regcache,
1481 tdep->ppc_gp0_regnum + i))
1482 {
1483 if (arch64)
1484 {
1485 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1486 (void *) &int64);
1487 ctx.gpr[i] = int64;
1488 }
1489 else
1490 {
1491 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
1492 (void *) &int32);
1493 ctx.gpr[i] = int32;
1494 }
1495 }
1496
1497 /* Collect floating-point register values from the regcache. */
1498 if (ppc_floating_point_unit_p (gdbarch))
1499 fill_fprs (regcache, ctx.fpr);
1500
1501 /* Special registers (always kept in ctx as 64 bits). */
1502 if (arch64)
1503 {
1504 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1505 &ctx.xer, &ctx.fpscr);
1506 }
1507 else
1508 {
1509 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1510 Solution: use 32-bit temp variables. */
1511 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1512 tmp_fpscr;
1513
1514 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1515 &tmp_xer, &tmp_fpscr);
1516 if (REG_VALID == regcache_register_status (regcache,
1517 gdbarch_pc_regnum (gdbarch)))
1518 ctx.iar = tmp_iar;
1519 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
1520 ctx.msr = tmp_msr;
1521 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
1522 ctx.cr = tmp_cr;
1523 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
1524 ctx.lr = tmp_lr;
1525 if (REG_VALID == regcache_register_status (regcache,
1526 tdep->ppc_ctr_regnum))
1527 ctx.ctr = tmp_ctr;
1528 if (REG_VALID == regcache_register_status (regcache,
1529 tdep->ppc_xer_regnum))
1530 ctx.xer = tmp_xer;
1531 if (REG_VALID == regcache_register_status (regcache,
1532 tdep->ppc_xer_regnum))
1533 ctx.fpscr = tmp_fpscr;
1534 }
1535
1536 status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1537 if (status != PTHDB_SUCCESS)
1538 error (_("aix-thread: store_registers: "
1539 "pthdb_pthread_setcontext returned %s"),
1540 pd_status2str (status));
1541 }
1542
1543 /* Store register REGNO if != -1 or all registers otherwise into
1544 kernel thread TID.
1545
1546 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1547 SPRs, but there's no way to set individual registers within those
1548 groups. Therefore, if REGNO != -1, this function stores an entire
1549 group. */
1550
1551 static void
1552 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1553 pthdb_tid_t tid)
1554 {
1555 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1556 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1557 uint64_t gprs64[ppc_num_gprs];
1558 uint32_t gprs32[ppc_num_gprs];
1559 double fprs[ppc_num_fprs];
1560 struct ptxsprs sprs64;
1561 struct ptsprs sprs32;
1562 int i;
1563
1564 if (debug_aix_thread)
1565 fprintf_unfiltered (gdb_stdlog,
1566 "store_regs_kernel_thread tid=%lx regno=%d\n",
1567 (long) tid, regno);
1568
1569 /* General-purpose registers. */
1570 if (regno == -1
1571 || (tdep->ppc_gp0_regnum <= regno
1572 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1573 {
1574 if (arch64)
1575 {
1576 /* Pre-fetch: some regs may not be in the cache. */
1577 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1578 fill_gprs64 (regcache, gprs64);
1579 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1580 }
1581 else
1582 {
1583 /* Pre-fetch: some regs may not be in the cache. */
1584 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1585 fill_gprs32 (regcache, gprs32);
1586 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1587 }
1588 }
1589
1590 /* Floating-point registers. */
1591
1592 if (ppc_floating_point_unit_p (gdbarch)
1593 && (regno == -1
1594 || (regno >= tdep->ppc_fp0_regnum
1595 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1596 {
1597 /* Pre-fetch: some regs may not be in the cache. */
1598 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1599 fill_fprs (regcache, fprs);
1600 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1601 }
1602
1603 /* Special-purpose registers. */
1604
1605 if (regno == -1 || special_register_p (gdbarch, regno))
1606 {
1607 if (arch64)
1608 {
1609 /* Pre-fetch: some registers won't be in the cache. */
1610 ptrace64aix (PTT_READ_SPRS, tid,
1611 (unsigned long) &sprs64, 0, NULL);
1612 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1613 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1614 &sprs64.pt_xer, &sprs64.pt_fpscr);
1615 ptrace64aix (PTT_WRITE_SPRS, tid,
1616 (unsigned long) &sprs64, 0, NULL);
1617 }
1618 else
1619 {
1620 /* The contents of "struct ptspr" were declared as "unsigned
1621 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1622 Use temporaries to work around this problem. Also, add an
1623 assert here to make sure we fail if the system header files
1624 use "unsigned long", and the size of that type is not what
1625 the headers expect. */
1626 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1627 tmp_fpscr;
1628
1629 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1630
1631 /* Pre-fetch: some registers won't be in the cache. */
1632 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1633
1634 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1635 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1636
1637 sprs32.pt_iar = tmp_iar;
1638 sprs32.pt_msr = tmp_msr;
1639 sprs32.pt_cr = tmp_cr;
1640 sprs32.pt_lr = tmp_lr;
1641 sprs32.pt_ctr = tmp_ctr;
1642 sprs32.pt_xer = tmp_xer;
1643 sprs32.pt_fpscr = tmp_fpscr;
1644
1645 if (tdep->ppc_mq_regnum >= 0)
1646 if (REG_VALID == regcache_register_status (regcache,
1647 tdep->ppc_mq_regnum))
1648 regcache_raw_collect (regcache, tdep->ppc_mq_regnum,
1649 &sprs32.pt_mq);
1650
1651 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1652 }
1653 }
1654 }
1655
1656 /* Store gdb's current view of the register set into the
1657 thread/process specified by inferior_ptid. */
1658
1659 static void
1660 aix_thread_store_registers (struct target_ops *ops,
1661 struct regcache *regcache, int regno)
1662 {
1663 struct thread_info *thread;
1664 pthdb_tid_t tid;
1665 struct target_ops *beneath = find_target_beneath (ops);
1666
1667 if (!PD_TID (inferior_ptid))
1668 beneath->to_store_registers (beneath, regcache, regno);
1669 else
1670 {
1671 thread = find_thread_ptid (inferior_ptid);
1672 tid = thread->private->tid;
1673
1674 if (tid == PTHDB_INVALID_TID)
1675 store_regs_user_thread (regcache, thread->private->pdtid);
1676 else
1677 store_regs_kernel_thread (regcache, regno, tid);
1678 }
1679 }
1680
1681 /* Implement the to_xfer_partial target_ops method. */
1682
1683 static enum target_xfer_status
1684 aix_thread_xfer_partial (struct target_ops *ops, enum target_object object,
1685 const char *annex, gdb_byte *readbuf,
1686 const gdb_byte *writebuf,
1687 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1688 {
1689 struct cleanup *old_chain = save_inferior_ptid ();
1690 enum target_xfer_status xfer;
1691 struct target_ops *beneath = find_target_beneath (ops);
1692
1693 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
1694 xfer = beneath->to_xfer_partial (beneath, object, annex, readbuf,
1695 writebuf, offset, len, xfered_len);
1696
1697 do_cleanups (old_chain);
1698 return xfer;
1699 }
1700
1701 /* Clean up after the inferior exits. */
1702
1703 static void
1704 aix_thread_mourn_inferior (struct target_ops *ops)
1705 {
1706 struct target_ops *beneath = find_target_beneath (ops);
1707
1708 pd_deactivate ();
1709 beneath->to_mourn_inferior (beneath);
1710 }
1711
1712 /* Return whether thread PID is still valid. */
1713
1714 static int
1715 aix_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
1716 {
1717 struct target_ops *beneath = find_target_beneath (ops);
1718
1719 if (!PD_TID (ptid))
1720 return beneath->to_thread_alive (beneath, ptid);
1721
1722 /* We update the thread list every time the child stops, so all
1723 valid threads should be in the thread list. */
1724 return in_thread_list (ptid);
1725 }
1726
1727 /* Return a printable representation of composite PID for use in
1728 "info threads" output. */
1729
1730 static char *
1731 aix_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
1732 {
1733 static char *ret = NULL;
1734 struct target_ops *beneath = find_target_beneath (ops);
1735
1736 if (!PD_TID (ptid))
1737 return beneath->to_pid_to_str (beneath, ptid);
1738
1739 /* Free previous return value; a new one will be allocated by
1740 xstrprintf(). */
1741 xfree (ret);
1742
1743 ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
1744 return ret;
1745 }
1746
1747 /* Return a printable representation of extra information about
1748 THREAD, for use in "info threads" output. */
1749
1750 static char *
1751 aix_thread_extra_thread_info (struct target_ops *self,
1752 struct thread_info *thread)
1753 {
1754 struct ui_file *buf;
1755 int status;
1756 pthdb_pthread_t pdtid;
1757 pthdb_tid_t tid;
1758 pthdb_state_t state;
1759 pthdb_suspendstate_t suspendstate;
1760 pthdb_detachstate_t detachstate;
1761 int cancelpend;
1762 static char *ret = NULL;
1763
1764 if (!PD_TID (thread->ptid))
1765 return NULL;
1766
1767 buf = mem_fileopen ();
1768
1769 pdtid = thread->private->pdtid;
1770 tid = thread->private->tid;
1771
1772 if (tid != PTHDB_INVALID_TID)
1773 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
1774 fprintf_unfiltered (buf, _("tid %d"), (int)tid);
1775
1776 status = pthdb_pthread_state (pd_session, pdtid, &state);
1777 if (status != PTHDB_SUCCESS)
1778 state = PST_NOTSUP;
1779 fprintf_unfiltered (buf, ", %s", state2str (state));
1780
1781 status = pthdb_pthread_suspendstate (pd_session, pdtid,
1782 &suspendstate);
1783 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
1784 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
1785 fprintf_unfiltered (buf, _(", suspended"));
1786
1787 status = pthdb_pthread_detachstate (pd_session, pdtid,
1788 &detachstate);
1789 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
1790 /* i18n: Like "Thread-Id %d, [state] running, detached" */
1791 fprintf_unfiltered (buf, _(", detached"));
1792
1793 pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1794 if (status == PTHDB_SUCCESS && cancelpend)
1795 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
1796 fprintf_unfiltered (buf, _(", cancel pending"));
1797
1798 ui_file_write (buf, "", 1);
1799
1800 xfree (ret); /* Free old buffer. */
1801
1802 ret = ui_file_xstrdup (buf, NULL);
1803 ui_file_delete (buf);
1804
1805 return ret;
1806 }
1807
1808 static ptid_t
1809 aix_thread_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
1810 {
1811 return ptid_build (ptid_get_pid (inferior_ptid), 0, thread);
1812 }
1813
1814 /* Initialize target aix_thread_ops. */
1815
1816 static void
1817 init_aix_thread_ops (void)
1818 {
1819 aix_thread_ops.to_shortname = "aix-threads";
1820 aix_thread_ops.to_longname = _("AIX pthread support");
1821 aix_thread_ops.to_doc = _("AIX pthread support");
1822
1823 aix_thread_ops.to_detach = aix_thread_detach;
1824 aix_thread_ops.to_resume = aix_thread_resume;
1825 aix_thread_ops.to_wait = aix_thread_wait;
1826 aix_thread_ops.to_fetch_registers = aix_thread_fetch_registers;
1827 aix_thread_ops.to_store_registers = aix_thread_store_registers;
1828 aix_thread_ops.to_xfer_partial = aix_thread_xfer_partial;
1829 aix_thread_ops.to_mourn_inferior = aix_thread_mourn_inferior;
1830 aix_thread_ops.to_thread_alive = aix_thread_thread_alive;
1831 aix_thread_ops.to_pid_to_str = aix_thread_pid_to_str;
1832 aix_thread_ops.to_extra_thread_info = aix_thread_extra_thread_info;
1833 aix_thread_ops.to_get_ada_task_ptid = aix_thread_get_ada_task_ptid;
1834 aix_thread_ops.to_stratum = thread_stratum;
1835 aix_thread_ops.to_magic = OPS_MAGIC;
1836 }
1837
1838 /* Module startup initialization function, automagically called by
1839 init.c. */
1840
1841 void _initialize_aix_thread (void);
1842
1843 void
1844 _initialize_aix_thread (void)
1845 {
1846 init_aix_thread_ops ();
1847 complete_target_initialization (&aix_thread_ops);
1848
1849 /* Notice when object files get loaded and unloaded. */
1850 observer_attach_new_objfile (new_objfile);
1851
1852 /* Add ourselves to inferior_created event chain.
1853 This is needed to enable the thread target on "attach". */
1854 observer_attach_inferior_created (aix_thread_inferior_created);
1855
1856 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
1857 _("Set debugging of AIX thread module."),
1858 _("Show debugging of AIX thread module."),
1859 _("Enables debugging output (used to debug GDB)."),
1860 NULL, NULL,
1861 /* FIXME: i18n: Debugging of AIX thread
1862 module is \"%d\". */
1863 &setdebuglist, &showdebuglist);
1864 }
This page took 0.123664 seconds and 4 git commands to generate.