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