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