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