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