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