linux_nat_target: More low methods
[deliverable/binutils-gdb.git] / gdb / aix-thread.c
CommitLineData
c11d79f2
KB
1/* Low level interface for debugging AIX 4.3+ pthreads.
2
e2882c85 3 Copyright (C) 1999-2018 Free Software Foundation, Inc.
c11d79f2
KB
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c11d79f2
KB
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c11d79f2
KB
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
0fe7bf7b
MS
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.
c11d79f2 35
0fe7bf7b
MS
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().
c11d79f2
KB
39
40 */
41
42#include "defs.h"
43#include "gdbthread.h"
44#include "target.h"
45#include "inferior.h"
46#include "regcache.h"
8e2c28d4 47#include "gdbcmd.h"
27bae383 48#include "ppc-tdep.h"
76727919 49#include "observable.h"
77e371c0 50#include "objfiles.h"
c11d79f2 51
c11d79f2
KB
52#include <procinfo.h>
53#include <sys/types.h>
54#include <sys/ptrace.h>
55#include <sys/reg.h>
c11d79f2
KB
56#include <sched.h>
57#include <sys/pthdebug.h>
58
d645e32e 59#if !HAVE_DECL_GETTHRDS
eff44fea 60extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
d645e32e
JB
61#endif
62
0fe7bf7b 63/* Whether to emit debugging output. */
8e2c28d4 64static int debug_aix_thread;
c11d79f2 65
0fe7bf7b 66/* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
c11d79f2
KB
67#ifndef PTHDB_VERSION_3
68#define pthdb_tid_t tid_t
69#endif
70
0fe7bf7b 71/* Return whether to treat PID as a debuggable thread id. */
c11d79f2
KB
72
73#define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
74
c11d79f2 75/* pthdb_user_t value that we pass to pthdb functions. 0 causes
0fe7bf7b 76 PTHDB_BAD_USER errors, so use 1. */
c11d79f2
KB
77
78#define PD_USER 1
79
0fe7bf7b 80/* Success and failure values returned by pthdb callbacks. */
c11d79f2
KB
81
82#define PDC_SUCCESS PTHDB_SUCCESS
83#define PDC_FAILURE PTHDB_CALLBACK
84
0fe7bf7b 85/* Private data attached to each element in GDB's thread list. */
c11d79f2 86
7aabaf9d
SM
87struct aix_thread_info : public private_thread_info
88{
0fe7bf7b 89 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
c11d79f2
KB
90 pthdb_tid_t tid; /* kernel thread id */
91};
92
7aabaf9d
SM
93/* Return the aix_thread_info attached to THREAD. */
94
95static aix_thread_info *
96get_aix_thread_info (thread_info *thread)
97{
98 return static_cast<aix_thread_info *> (thread->priv.get ());
99}
100
0fe7bf7b 101/* Information about a thread of which libpthdebug is aware. */
c11d79f2
KB
102
103struct pd_thread {
104 pthdb_pthread_t pdtid;
105 pthread_t pthid;
106 pthdb_tid_t tid;
107};
108
0fe7bf7b 109/* This module's target-specific operations, active while pd_able is true. */
c11d79f2 110
f6ac5f3d
PA
111class aix_thread_target final : public target_ops
112{
113public:
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
57810aa7 140 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
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
149static aix_thread_target aix_thread_ops;
c11d79f2 150
0fe7bf7b
MS
151/* Address of the function that libpthread will call when libpthdebug
152 is ready to be initialized. */
c11d79f2
KB
153
154static CORE_ADDR pd_brk_addr;
155
0fe7bf7b 156/* Whether the current application is debuggable by pthdb. */
c11d79f2
KB
157
158static int pd_able = 0;
159
0fe7bf7b 160/* Whether a threaded application is being debugged. */
c11d79f2
KB
161
162static int pd_active = 0;
163
0fe7bf7b
MS
164/* Whether the current architecture is 64-bit.
165 Only valid when pd_able is true. */
c11d79f2
KB
166
167static int arch64;
168
0fe7bf7b 169/* Forward declarations for pthdb callbacks. */
c11d79f2
KB
170
171static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
172static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
173static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
174static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
0fe7bf7b
MS
175 unsigned long long flags,
176 pthdb_context_t *context);
c11d79f2 177static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
0fe7bf7b
MS
178 unsigned long long flags,
179 pthdb_context_t *context);
c11d79f2
KB
180static int pdc_alloc (pthdb_user_t, size_t, void **);
181static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
182static int pdc_dealloc (pthdb_user_t, void *);
183
0fe7bf7b 184/* pthdb callbacks. */
c11d79f2
KB
185
186static 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
0fe7bf7b 198/* Current pthdb session. */
c11d79f2
KB
199
200static pthdb_session_t pd_session;
201
0fe7bf7b
MS
202/* Return a printable representation of pthdebug function return
203 STATUS. */
c11d79f2 204
0a31ccfb 205static const char *
c11d79f2
KB
206pd_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
0fe7bf7b
MS
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. */
c11d79f2
KB
248
249static int
250ptrace_check (int req, int id, int ret)
251{
252 if (ret == 0 && !errno)
253 return 1;
254
0fe7bf7b
MS
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. */
c11d79f2
KB
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)
42cc437f
KB
266 {
267 if (debug_aix_thread)
0fe7bf7b 268 fprintf_unfiltered (gdb_stdlog,
27bae383 269 "ptrace (%d, %d) = %d (errno = %d)\n",
42cc437f
KB
270 req, id, ret, errno);
271 return ret == -1 ? 0 : 1;
272 }
c11d79f2
KB
273 break;
274 }
edefbb7c 275 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
be006b8b 276 req, id, ret, errno, safe_strerror (errno));
0fe7bf7b 277 return 0; /* Not reached. */
c11d79f2
KB
278}
279
fecf803e
UW
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
c11d79f2
KB
288
289static int
290ptrace64aix (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
fecf803e
UW
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
c11d79f2
KB
307
308static int
fecf803e 309ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
c11d79f2
KB
310{
311 errno = 0;
0fe7bf7b 312 return ptrace_check (req, id,
26f0edc1 313 ptrace (req, id, addr, data, buf));
c11d79f2
KB
314}
315
0fe7bf7b
MS
316/* If *PIDP is a composite process/thread id, convert it to a
317 process id. */
c11d79f2
KB
318
319static void
320pid_to_prc (ptid_t *ptidp)
321{
322 ptid_t ptid;
323
324 ptid = *ptidp;
325 if (PD_TID (ptid))
dfd4cc63 326 *ptidp = pid_to_ptid (ptid_get_pid (ptid));
c11d79f2
KB
327}
328
0fe7bf7b
MS
329/* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
330 the address of SYMBOLS[<i>].name. */
c11d79f2
KB
331
332static int
333pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
334{
3b7344d5 335 struct bound_minimal_symbol ms;
c11d79f2
KB
336 int i;
337 char *name;
338
8e2c28d4
KB
339 if (debug_aix_thread)
340 fprintf_unfiltered (gdb_stdlog,
27bae383 341 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)\n",
8e2c28d4 342 user, (long) symbols, count);
c11d79f2
KB
343
344 for (i = 0; i < count; i++)
345 {
346 name = symbols[i].name;
8e2c28d4 347 if (debug_aix_thread)
0fe7bf7b 348 fprintf_unfiltered (gdb_stdlog,
27bae383 349 " symbols[%d].name = \"%s\"\n", i, name);
c11d79f2
KB
350
351 if (!*name)
352 symbols[i].addr = 0;
353 else
354 {
3b7344d5
TT
355 ms = lookup_minimal_symbol (name, NULL, NULL);
356 if (ms.minsym == NULL)
c11d79f2 357 {
8e2c28d4 358 if (debug_aix_thread)
27bae383 359 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE\n");
c11d79f2
KB
360 return PDC_FAILURE;
361 }
77e371c0 362 symbols[i].addr = BMSYMBOL_VALUE_ADDRESS (ms);
c11d79f2 363 }
8e2c28d4 364 if (debug_aix_thread)
27bae383 365 fprintf_unfiltered (gdb_stdlog, " symbols[%d].addr = %s\n",
bb599908 366 i, hex_string (symbols[i].addr));
c11d79f2 367 }
8e2c28d4 368 if (debug_aix_thread)
27bae383 369 fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS\n");
c11d79f2
KB
370 return PDC_SUCCESS;
371}
372
0fe7bf7b
MS
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
c11d79f2
KB
379static int
380pdc_read_regs (pthdb_user_t user,
381 pthdb_tid_t tid,
382 unsigned long long flags,
383 pthdb_context_t *context)
384{
0fe7bf7b
MS
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
063715bf
JB
391 uint64_t gprs64[ppc_num_gprs];
392 uint32_t gprs32[ppc_num_gprs];
393 double fprs[ppc_num_fprs];
c11d79f2
KB
394 struct ptxsprs sprs64;
395 struct ptsprs sprs32;
396
8e2c28d4 397 if (debug_aix_thread)
27bae383 398 fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
bb599908 399 (int) tid, hex_string (flags));
c11d79f2 400
0fe7bf7b 401 /* General-purpose registers. */
c11d79f2
KB
402 if (flags & PTHDB_FLAG_GPRS)
403 {
404 if (arch64)
405 {
0fe7bf7b
MS
406 if (!ptrace64aix (PTT_READ_GPRS, tid,
407 (unsigned long) gprs64, 0, NULL))
c11d79f2
KB
408 memset (gprs64, 0, sizeof (gprs64));
409 memcpy (context->gpr, gprs64, sizeof(gprs64));
410 }
411 else
412 {
3b631e37 413 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
c11d79f2
KB
414 memset (gprs32, 0, sizeof (gprs32));
415 memcpy (context->gpr, gprs32, sizeof(gprs32));
416 }
417 }
418
0fe7bf7b 419 /* Floating-point registers. */
c11d79f2
KB
420 if (flags & PTHDB_FLAG_FPRS)
421 {
3b631e37 422 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
c11d79f2 423 memset (fprs, 0, sizeof (fprs));
46bba1ef 424 memcpy (context->fpr, fprs, sizeof(fprs));
c11d79f2
KB
425 }
426
0fe7bf7b 427 /* Special-purpose registers. */
c11d79f2
KB
428 if (flags & PTHDB_FLAG_SPRS)
429 {
430 if (arch64)
431 {
0fe7bf7b
MS
432 if (!ptrace64aix (PTT_READ_SPRS, tid,
433 (unsigned long) &sprs64, 0, NULL))
c11d79f2
KB
434 memset (&sprs64, 0, sizeof (sprs64));
435 memcpy (&context->msr, &sprs64, sizeof(sprs64));
436 }
437 else
438 {
3b631e37 439 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
c11d79f2
KB
440 memset (&sprs32, 0, sizeof (sprs32));
441 memcpy (&context->msr, &sprs32, sizeof(sprs32));
442 }
443 }
444 return 0;
445}
446
0fe7bf7b 447/* Write register function should be able to write requested context
0963b4bd 448 information to specified debuggee's kernel thread id.
0fe7bf7b
MS
449 If successful return 0, else non-zero is returned. */
450
c11d79f2
KB
451static int
452pdc_write_regs (pthdb_user_t user,
453 pthdb_tid_t tid,
454 unsigned long long flags,
455 pthdb_context_t *context)
456{
0fe7bf7b
MS
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. */
c11d79f2 462
8e2c28d4 463 if (debug_aix_thread)
27bae383 464 fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
bb599908 465 (int) tid, hex_string (flags));
c11d79f2 466
0fe7bf7b 467 /* General-purpose registers. */
c11d79f2
KB
468 if (flags & PTHDB_FLAG_GPRS)
469 {
470 if (arch64)
0fe7bf7b 471 ptrace64aix (PTT_WRITE_GPRS, tid,
206d3d3c 472 (unsigned long) context->gpr, 0, NULL);
c11d79f2 473 else
3b631e37 474 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
c11d79f2
KB
475 }
476
0fe7bf7b 477 /* Floating-point registers. */
c11d79f2
KB
478 if (flags & PTHDB_FLAG_FPRS)
479 {
3b631e37 480 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
c11d79f2
KB
481 }
482
0fe7bf7b 483 /* Special-purpose registers. */
c11d79f2
KB
484 if (flags & PTHDB_FLAG_SPRS)
485 {
486 if (arch64)
487 {
0fe7bf7b
MS
488 ptrace64aix (PTT_WRITE_SPRS, tid,
489 (unsigned long) &context->msr, 0, NULL);
c11d79f2
KB
490 }
491 else
492 {
3b631e37 493 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
c11d79f2
KB
494 }
495 }
496 return 0;
497}
498
0fe7bf7b 499/* pthdb callback: read LEN bytes from process ADDR into BUF. */
c11d79f2
KB
500
501static int
0fe7bf7b
MS
502pdc_read_data (pthdb_user_t user, void *buf,
503 pthdb_addr_t addr, size_t len)
c11d79f2
KB
504{
505 int status, ret;
506
8e2c28d4
KB
507 if (debug_aix_thread)
508 fprintf_unfiltered (gdb_stdlog,
27bae383 509 "pdc_read_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
bb599908 510 user, (long) buf, hex_string (addr), len);
c11d79f2 511
71829b1a 512 status = target_read_memory (addr, (gdb_byte *) buf, len);
c11d79f2
KB
513 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
514
8e2c28d4 515 if (debug_aix_thread)
27bae383 516 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n",
0fe7bf7b 517 status, pd_status2str (ret));
c11d79f2
KB
518 return ret;
519}
520
0fe7bf7b 521/* pthdb callback: write LEN bytes from BUF to process ADDR. */
c11d79f2
KB
522
523static int
0fe7bf7b
MS
524pdc_write_data (pthdb_user_t user, void *buf,
525 pthdb_addr_t addr, size_t len)
c11d79f2
KB
526{
527 int status, ret;
528
8e2c28d4
KB
529 if (debug_aix_thread)
530 fprintf_unfiltered (gdb_stdlog,
27bae383 531 "pdc_write_data (user = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
bb599908 532 user, (long) buf, hex_string (addr), len);
c11d79f2 533
71829b1a 534 status = target_write_memory (addr, (gdb_byte *) buf, len);
c11d79f2
KB
535 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
536
8e2c28d4 537 if (debug_aix_thread)
27bae383 538 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s\n", status,
8e2c28d4 539 pd_status2str (ret));
c11d79f2
KB
540 return ret;
541}
542
0fe7bf7b
MS
543/* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
544 in BUFP. */
c11d79f2
KB
545
546static int
547pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
548{
8e2c28d4
KB
549 if (debug_aix_thread)
550 fprintf_unfiltered (gdb_stdlog,
27bae383 551 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)\n",
8e2c28d4 552 user, len, (long) bufp);
c11d79f2 553 *bufp = xmalloc (len);
8e2c28d4 554 if (debug_aix_thread)
0fe7bf7b 555 fprintf_unfiltered (gdb_stdlog,
27bae383 556 " malloc returned 0x%lx\n", (long) *bufp);
0fe7bf7b
MS
557
558 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
559 be returned. */
560
c11d79f2
KB
561 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
562}
563
0fe7bf7b
MS
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. */
c11d79f2
KB
567
568static int
569pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
570{
8e2c28d4
KB
571 if (debug_aix_thread)
572 fprintf_unfiltered (gdb_stdlog,
27bae383 573 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
8e2c28d4 574 user, (long) buf, len, (long) bufp);
be006b8b 575 *bufp = xrealloc (buf, len);
8e2c28d4 576 if (debug_aix_thread)
0fe7bf7b 577 fprintf_unfiltered (gdb_stdlog,
27bae383 578 " realloc returned 0x%lx\n", (long) *bufp);
c11d79f2
KB
579 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
580}
581
0fe7bf7b
MS
582/* pthdb callback: free BUF, which was allocated by the alloc or
583 realloc callback. */
c11d79f2
KB
584
585static int
586pdc_dealloc (pthdb_user_t user, void *buf)
587{
8e2c28d4 588 if (debug_aix_thread)
0fe7bf7b 589 fprintf_unfiltered (gdb_stdlog,
27bae383 590 "pdc_free (user = %ld, buf = 0x%lx)\n", user,
8e2c28d4 591 (long) buf);
c11d79f2
KB
592 xfree (buf);
593 return PDC_SUCCESS;
594}
595
0fe7bf7b 596/* Return a printable representation of pthread STATE. */
c11d79f2
KB
597
598static char *
599state2str (pthdb_state_t state)
600{
601 switch (state)
602 {
edefbb7c
AC
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");
c11d79f2
KB
621 }
622}
623
0fe7bf7b 624/* qsort() comparison function for sorting pd_thread structs by pthid. */
c11d79f2
KB
625
626static int
627pcmp (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
77f0be4e
JB
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. */
c11d79f2
KB
640
641static int
642giter_count (struct thread_info *thread, void *countp)
643{
77f0be4e
JB
644 if (PD_TID (thread->ptid))
645 (*(int *) countp)++;
c11d79f2
KB
646 return 0;
647}
648
77f0be4e
JB
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. */
c11d79f2
KB
655
656static int
657giter_accum (struct thread_info *thread, void *bufp)
658{
77f0be4e
JB
659 if (PD_TID (thread->ptid))
660 {
661 **(struct thread_info ***) bufp = thread;
662 (*(struct thread_info ***) bufp)++;
663 }
c11d79f2
KB
664 return 0;
665}
666
667/* ptid comparison function */
0fe7bf7b 668
c11d79f2
KB
669static int
670ptid_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
0fe7bf7b 690/* qsort() comparison function for sorting thread_info structs by pid. */
c11d79f2
KB
691
692static int
693gcmp (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
9ad7bec7
JB
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
704static pthdb_tid_t
705get_signaled_thread (void)
706{
707 struct thrdsinfo64 thrinf;
eff44fea 708 tid_t ktid = 0;
9ad7bec7
JB
709 int result = 0;
710
9ad7bec7
JB
711 while (1)
712 {
dfd4cc63 713 if (getthrds (ptid_get_pid (inferior_ptid), &thrinf,
9ad7bec7
JB
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
c11d79f2
KB
725/* Synchronize GDB's thread list with libpthdebug's.
726
727 There are some benefits of doing this every time the inferior stops:
728
0fe7bf7b
MS
729 - allows users to run thread-specific commands without needing to
730 run "info threads" first
c11d79f2
KB
731
732 - helps pthdb_tid_pthread() work properly (see "libpthdebug
733 peculiarities" at the top of this module)
734
0fe7bf7b
MS
735 - simplifies the demands placed on libpthdebug, which seems to
736 have difficulty with certain call patterns */
c11d79f2
KB
737
738static void
739sync_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;
c11d79f2 748
0fe7bf7b 749 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
c11d79f2
KB
750
751 pcount = 0;
752 psize = 1;
8d749320 753 pbuf = XNEWVEC (struct pd_thread, psize);
c11d79f2
KB
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;
0fe7bf7b
MS
768 pbuf = (struct pd_thread *) xrealloc (pbuf,
769 psize * sizeof *pbuf);
c11d79f2
KB
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
0fe7bf7b 786 /* Accumulate an array of GDB threads sorted by pid. */
c11d79f2
KB
787
788 gcount = 0;
789 iterate_over_threads (giter_count, &gcount);
8d749320 790 g = gbuf = XNEWVEC (struct thread_info *, gcount);
c11d79f2
KB
791 iterate_over_threads (giter_accum, &g);
792 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
793
0fe7bf7b 794 /* Apply differences between the two arrays to GDB's thread list. */
c11d79f2 795
dfd4cc63 796 infpid = ptid_get_pid (inferior_ptid);
c11d79f2
KB
797 for (pi = gi = 0; pi < pcount || gi < gcount;)
798 {
c11d79f2 799 if (pi == pcount)
c11d79f2 800 {
42cc437f 801 delete_thread (gbuf[gi]->ptid);
c11d79f2
KB
802 gi++;
803 }
42cc437f 804 else if (gi == gcount)
c11d79f2 805 {
7aabaf9d
SM
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
c11d79f2
KB
812 pi++;
813 }
42cc437f
KB
814 else
815 {
816 ptid_t pptid, gptid;
817 int cmp_result;
818
dfd4cc63 819 pptid = ptid_build (infpid, 0, pbuf[pi].pthid);
42cc437f
KB
820 gptid = gbuf[gi]->ptid;
821 pdtid = pbuf[pi].pdtid;
822 tid = pbuf[pi].tid;
c11d79f2 823
42cc437f
KB
824 cmp_result = ptid_cmp (pptid, gptid);
825
826 if (cmp_result == 0)
827 {
7aabaf9d
SM
828 aix_thread_info *priv = get_aix_thread_info (gbuf[gi]);
829
830 priv->pdtid = pdtid;
831 priv->tid = tid;
42cc437f
KB
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);
7aabaf9d
SM
843
844 aix_thread_info *priv = new aix_thread_info;
845 thread->priv.reset (priv);
846 priv->pdtid = pdtid;
847 priv->tid = tid;
42cc437f
KB
848 pi++;
849 }
850 }
c11d79f2
KB
851 }
852
853 xfree (pbuf);
854 xfree (gbuf);
855}
856
9ad7bec7
JB
857/* Iterate_over_threads() callback for locating a thread, using
858 the TID of its associated kernel thread. */
c11d79f2
KB
859
860static int
9ad7bec7 861iter_tid (struct thread_info *thread, void *tidp)
c11d79f2 862{
9ad7bec7 863 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
7aabaf9d 864 aix_thread_info *priv = get_aix_thread_info (thread);
c11d79f2 865
7aabaf9d 866 return priv->tid == tid;
c11d79f2
KB
867}
868
0fe7bf7b
MS
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>. */
c11d79f2
KB
872
873static ptid_t
874pd_update (int set_infpid)
875{
876 int status;
877 ptid_t ptid;
9ad7bec7
JB
878 pthdb_tid_t tid;
879 struct thread_info *thread = NULL;
c11d79f2
KB
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
0fe7bf7b 890 /* Define "current thread" as one that just received a trap signal. */
c11d79f2 891
9ad7bec7
JB
892 tid = get_signaled_thread ();
893 if (tid != 0)
894 thread = iterate_over_threads (iter_tid, &tid);
c11d79f2
KB
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
0963b4bd 906/* Try to start debugging threads in the current process.
0fe7bf7b
MS
907 If successful and SET_INFPID, set inferior_ptid to reflect the
908 current thread. */
c11d79f2
KB
909
910static ptid_t
911pd_activate (int set_infpid)
912{
913 int status;
914
915 status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
0fe7bf7b
MS
916 PTHDB_FLAG_REGS, &pd_callbacks,
917 &pd_session);
c11d79f2
KB
918 if (status != PTHDB_SUCCESS)
919 {
920 return inferior_ptid;
921 }
922 pd_active = 1;
923 return pd_update (set_infpid);
924}
925
0fe7bf7b 926/* Undo the effects of pd_activate(). */
c11d79f2
KB
927
928static void
929pd_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
0fe7bf7b
MS
939/* An object file has just been loaded. Check whether the current
940 application is pthreaded, and if so, prepare for thread debugging. */
c11d79f2
KB
941
942static void
943pd_enable (void)
944{
945 int status;
946 char *stub_name;
3b7344d5 947 struct bound_minimal_symbol ms;
c11d79f2 948
0fe7bf7b 949 /* Don't initialize twice. */
c11d79f2
KB
950 if (pd_able)
951 return;
952
0fe7bf7b 953 /* Check application word size. */
f5656ead 954 arch64 = register_size (target_gdbarch (), 0) == 8;
c11d79f2 955
0fe7bf7b 956 /* Check whether the application is pthreaded. */
c11d79f2 957 stub_name = NULL;
0fe7bf7b
MS
958 status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS,
959 &pd_callbacks, &stub_name);
f8bf5763
PM
960 if ((status != PTHDB_SUCCESS
961 && status != PTHDB_NOT_PTHREADED) || !stub_name)
c11d79f2
KB
962 return;
963
0fe7bf7b 964 /* Set a breakpoint on the returned stub function. */
3b7344d5
TT
965 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
966 if (ms.minsym == NULL)
c11d79f2 967 return;
77e371c0 968 pd_brk_addr = BMSYMBOL_VALUE_ADDRESS (ms);
f5656ead 969 if (!create_thread_event_breakpoint (target_gdbarch (), pd_brk_addr))
c11d79f2
KB
970 return;
971
0fe7bf7b 972 /* Prepare for thread debugging. */
206d3d3c 973 push_target (&aix_thread_ops);
c11d79f2
KB
974 pd_able = 1;
975
0fe7bf7b
MS
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. */
c11d79f2
KB
979 pd_activate (1);
980}
981
0fe7bf7b 982/* Undo the effects of pd_enable(). */
c11d79f2
KB
983
984static void
985pd_disable (void)
986{
987 if (!pd_able)
988 return;
989 if (pd_active)
990 pd_deactivate ();
991 pd_able = 0;
206d3d3c 992 unpush_target (&aix_thread_ops);
c11d79f2
KB
993}
994
06d3b283 995/* new_objfile observer callback.
c11d79f2 996
0fe7bf7b
MS
997 If OBJFILE is non-null, check whether a threaded application is
998 being debugged, and if so, prepare for thread debugging.
c11d79f2 999
0fe7bf7b 1000 If OBJFILE is null, stop debugging threads. */
c11d79f2
KB
1001
1002static void
1003new_objfile (struct objfile *objfile)
1004{
1005 if (objfile)
1006 pd_enable ();
1007 else
1008 pd_disable ();
c11d79f2
KB
1009}
1010
0fe7bf7b 1011/* Attach to process specified by ARGS. */
c11d79f2
KB
1012
1013static void
b3ccfe11 1014aix_thread_inferior_created (struct target_ops *ops, int from_tty)
c11d79f2 1015{
b3ccfe11 1016 pd_enable ();
c11d79f2
KB
1017}
1018
206d3d3c 1019/* Detach from the process attached to by aix_thread_attach(). */
c11d79f2 1020
f6ac5f3d
PA
1021void
1022aix_thread_target::detach (inferior *inf, int from_tty)
c11d79f2 1023{
f6ac5f3d 1024 struct target_ops *beneath = find_target_beneath (this);
d30acaa7 1025
6c0c456d 1026 pd_disable ();
f6ac5f3d 1027 beneath->detach (inf, from_tty);
c11d79f2
KB
1028}
1029
1030/* Tell the inferior process to continue running thread PID if != -1
0fe7bf7b 1031 and all threads otherwise. */
c11d79f2 1032
f6ac5f3d
PA
1033void
1034aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
c11d79f2
KB
1035{
1036 struct thread_info *thread;
1037 pthdb_tid_t tid[2];
1038
1039 if (!PD_TID (ptid))
14fa3751 1040 {
2989a365 1041 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
f6ac5f3d 1042 struct target_ops *beneath = find_target_beneath (this);
d30acaa7 1043
dfd4cc63 1044 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
f6ac5f3d 1045 beneath->resume (ptid, step, sig);
14fa3751 1046 }
c11d79f2
KB
1047 else
1048 {
e09875d4 1049 thread = find_thread_ptid (ptid);
c11d79f2 1050 if (!thread)
edefbb7c 1051 error (_("aix-thread resume: unknown pthread %ld"),
dfd4cc63 1052 ptid_get_lwp (ptid));
c11d79f2 1053
7aabaf9d
SM
1054 aix_thread_info *priv = get_aix_thread_info (thread);
1055
1056 tid[0] = priv->tid;
c11d79f2 1057 if (tid[0] == PTHDB_INVALID_TID)
edefbb7c 1058 error (_("aix-thread resume: no tid for pthread %ld"),
dfd4cc63 1059 ptid_get_lwp (ptid));
c11d79f2
KB
1060 tid[1] = 0;
1061
1062 if (arch64)
fecf803e 1063 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
71829b1a 1064 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
c11d79f2 1065 else
fecf803e 1066 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
71829b1a 1067 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
c11d79f2
KB
1068 }
1069}
1070
0fe7bf7b
MS
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. */
c11d79f2 1074
f6ac5f3d
PA
1075ptid_t
1076aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1077 int options)
c11d79f2 1078{
f6ac5f3d 1079 struct target_ops *beneath = find_target_beneath (this);
14fa3751 1080
2989a365
TT
1081 {
1082 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
14fa3751 1083
2989a365
TT
1084 pid_to_prc (&ptid);
1085
1086 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
f6ac5f3d 1087 ptid = beneath->wait (ptid, status, options);
2989a365 1088 }
14fa3751 1089
dfd4cc63 1090 if (ptid_get_pid (ptid) == -1)
c11d79f2
KB
1091 return pid_to_ptid (-1);
1092
0fe7bf7b 1093 /* Check whether libpthdebug might be ready to be initialized. */
515630c5 1094 if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED
a493e3e2 1095 && status->value.sig == GDB_SIGNAL_TRAP)
515630c5
UW
1096 {
1097 struct regcache *regcache = get_thread_regcache (ptid);
ac7936df 1098 struct gdbarch *gdbarch = regcache->arch ();
515630c5
UW
1099
1100 if (regcache_read_pc (regcache)
527a273a 1101 - gdbarch_decr_pc_after_break (gdbarch) == pd_brk_addr)
515630c5
UW
1102 return pd_activate (0);
1103 }
c11d79f2
KB
1104
1105 return pd_update (0);
1106}
1107
0fe7bf7b 1108/* Record that the 64-bit general-purpose registers contain VALS. */
c11d79f2
KB
1109
1110static void
647478e0 1111supply_gprs64 (struct regcache *regcache, uint64_t *vals)
c11d79f2 1112{
ac7936df 1113 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
c11d79f2
KB
1114 int regno;
1115
063715bf 1116 for (regno = 0; regno < ppc_num_gprs; regno++)
647478e0 1117 regcache_raw_supply (regcache, tdep->ppc_gp0_regnum + regno,
23a6d369 1118 (char *) (vals + regno));
c11d79f2
KB
1119}
1120
0fe7bf7b 1121/* Record that 32-bit register REGNO contains VAL. */
c11d79f2
KB
1122
1123static void
647478e0 1124supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
c11d79f2 1125{
647478e0 1126 regcache_raw_supply (regcache, regno, (char *) &val);
c11d79f2
KB
1127}
1128
0fe7bf7b 1129/* Record that the floating-point registers contain VALS. */
c11d79f2
KB
1130
1131static void
647478e0 1132supply_fprs (struct regcache *regcache, double *vals)
c11d79f2 1133{
ac7936df 1134 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1135 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
c11d79f2
KB
1136 int regno;
1137
383f0f5b
JB
1138 /* This function should never be called on architectures without
1139 floating-point registers. */
c7f30c7a 1140 gdb_assert (ppc_floating_point_unit_p (gdbarch));
383f0f5b 1141
786c562f
JB
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));
c11d79f2
KB
1147}
1148
f1a91342
KB
1149/* Predicate to test whether given register number is a "special" register. */
1150static int
9970f04b 1151special_register_p (struct gdbarch *gdbarch, int regno)
f1a91342 1152{
9970f04b 1153 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
f1a91342 1154
9970f04b 1155 return regno == gdbarch_pc_regnum (gdbarch)
f1a91342
KB
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
383f0f5b 1161 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
f1a91342
KB
1162 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1163}
1164
1165
0fe7bf7b
MS
1166/* Record that the special registers contain the specified 64-bit and
1167 32-bit values. */
c11d79f2
KB
1168
1169static void
647478e0
UW
1170supply_sprs64 (struct regcache *regcache,
1171 uint64_t iar, uint64_t msr, uint32_t cr,
0e061eef
KB
1172 uint64_t lr, uint64_t ctr, uint32_t xer,
1173 uint32_t fpscr)
c11d79f2 1174{
ac7936df 1175 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1176 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
f1a91342 1177
c7f30c7a 1178 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
3e8c568d 1179 (char *) &iar);
647478e0
UW
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);
383f0f5b 1185 if (tdep->ppc_fpscr_regnum >= 0)
647478e0 1186 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
23a6d369 1187 (char *) &fpscr);
c11d79f2
KB
1188}
1189
0fe7bf7b
MS
1190/* Record that the special registers contain the specified 32-bit
1191 values. */
c11d79f2
KB
1192
1193static void
647478e0
UW
1194supply_sprs32 (struct regcache *regcache,
1195 uint32_t iar, uint32_t msr, uint32_t cr,
0e061eef
KB
1196 uint32_t lr, uint32_t ctr, uint32_t xer,
1197 uint32_t fpscr)
c11d79f2 1198{
ac7936df 1199 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1200 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
f1a91342 1201
c7f30c7a 1202 regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
3e8c568d 1203 (char *) &iar);
647478e0
UW
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);
383f0f5b 1209 if (tdep->ppc_fpscr_regnum >= 0)
647478e0 1210 regcache_raw_supply (regcache, tdep->ppc_fpscr_regnum,
23a6d369 1211 (char *) &fpscr);
c11d79f2
KB
1212}
1213
1214/* Fetch all registers from pthread PDTID, which doesn't have a kernel
1215 thread.
1216
0fe7bf7b
MS
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. */
c11d79f2
KB
1220
1221static void
647478e0 1222fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
c11d79f2 1223{
ac7936df 1224 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1225 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
c11d79f2
KB
1226 int status, i;
1227 pthdb_context_t ctx;
1228
8e2c28d4 1229 if (debug_aix_thread)
206d3d3c
KB
1230 fprintf_unfiltered (gdb_stdlog,
1231 "fetch_regs_user_thread %lx\n", (long) pdtid);
c11d79f2
KB
1232 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1233 if (status != PTHDB_SUCCESS)
edefbb7c 1234 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
14fa3751 1235 pd_status2str (status));
c11d79f2 1236
0fe7bf7b 1237 /* General-purpose registers. */
c11d79f2
KB
1238
1239 if (arch64)
647478e0 1240 supply_gprs64 (regcache, ctx.gpr);
c11d79f2 1241 else
063715bf 1242 for (i = 0; i < ppc_num_gprs; i++)
647478e0 1243 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
c11d79f2 1244
0fe7bf7b 1245 /* Floating-point registers. */
c11d79f2 1246
c7f30c7a 1247 if (ppc_floating_point_unit_p (gdbarch))
647478e0 1248 supply_fprs (regcache, ctx.fpr);
c11d79f2 1249
0fe7bf7b 1250 /* Special registers. */
c11d79f2
KB
1251
1252 if (arch64)
647478e0
UW
1253 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1254 ctx.xer, ctx.fpscr);
c11d79f2 1255 else
647478e0
UW
1256 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1257 ctx.xer, ctx.fpscr);
c11d79f2
KB
1258}
1259
0fe7bf7b
MS
1260/* Fetch register REGNO if != -1 or all registers otherwise from
1261 kernel thread TID.
c11d79f2 1262
0fe7bf7b
MS
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.
c11d79f2 1267
0fe7bf7b
MS
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. */
c11d79f2
KB
1274
1275static void
647478e0
UW
1276fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1277 pthdb_tid_t tid)
c11d79f2 1278{
ac7936df 1279 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1280 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
063715bf
JB
1281 uint64_t gprs64[ppc_num_gprs];
1282 uint32_t gprs32[ppc_num_gprs];
1283 double fprs[ppc_num_fprs];
c11d79f2
KB
1284 struct ptxsprs sprs64;
1285 struct ptsprs sprs32;
1286 int i;
1287
8e2c28d4
KB
1288 if (debug_aix_thread)
1289 fprintf_unfiltered (gdb_stdlog,
206d3d3c
KB
1290 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1291 (long) tid, regno, arch64);
c11d79f2 1292
0fe7bf7b 1293 /* General-purpose registers. */
daf6dc85
JB
1294 if (regno == -1
1295 || (tdep->ppc_gp0_regnum <= regno
1296 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
c11d79f2
KB
1297 {
1298 if (arch64)
1299 {
0fe7bf7b
MS
1300 if (!ptrace64aix (PTT_READ_GPRS, tid,
1301 (unsigned long) gprs64, 0, NULL))
c11d79f2 1302 memset (gprs64, 0, sizeof (gprs64));
647478e0 1303 supply_gprs64 (regcache, gprs64);
c11d79f2
KB
1304 }
1305 else
1306 {
3b631e37 1307 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
c11d79f2 1308 memset (gprs32, 0, sizeof (gprs32));
063715bf 1309 for (i = 0; i < ppc_num_gprs; i++)
647478e0 1310 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
c11d79f2
KB
1311 }
1312 }
1313
0fe7bf7b 1314 /* Floating-point registers. */
c11d79f2 1315
c7f30c7a 1316 if (ppc_floating_point_unit_p (gdbarch)
383f0f5b
JB
1317 && (regno == -1
1318 || (regno >= tdep->ppc_fp0_regnum
1319 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
c11d79f2 1320 {
3b631e37 1321 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
c11d79f2 1322 memset (fprs, 0, sizeof (fprs));
647478e0 1323 supply_fprs (regcache, fprs);
c11d79f2
KB
1324 }
1325
0fe7bf7b 1326 /* Special-purpose registers. */
c11d79f2 1327
9970f04b 1328 if (regno == -1 || special_register_p (gdbarch, regno))
c11d79f2
KB
1329 {
1330 if (arch64)
1331 {
0fe7bf7b
MS
1332 if (!ptrace64aix (PTT_READ_SPRS, tid,
1333 (unsigned long) &sprs64, 0, NULL))
c11d79f2 1334 memset (&sprs64, 0, sizeof (sprs64));
647478e0
UW
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);
c11d79f2
KB
1338 }
1339 else
1340 {
c7f30c7a 1341 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
f1a91342 1342
3b631e37 1343 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
c11d79f2 1344 memset (&sprs32, 0, sizeof (sprs32));
647478e0 1345 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
0e061eef
KB
1346 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1347 sprs32.pt_fpscr);
c11d79f2 1348
f1a91342 1349 if (tdep->ppc_mq_regnum >= 0)
647478e0 1350 regcache_raw_supply (regcache, tdep->ppc_mq_regnum,
23a6d369 1351 (char *) &sprs32.pt_mq);
c11d79f2
KB
1352 }
1353 }
1354}
1355
edb5fb00
SM
1356/* Fetch register REGNO if != -1 or all registers otherwise from the
1357 thread/process connected to REGCACHE. */
c11d79f2 1358
f6ac5f3d
PA
1359void
1360aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
c11d79f2
KB
1361{
1362 struct thread_info *thread;
1363 pthdb_tid_t tid;
f6ac5f3d 1364 struct target_ops *beneath = find_target_beneath (this);
c11d79f2 1365
edb5fb00 1366 if (!PD_TID (regcache_get_ptid (regcache)))
f6ac5f3d 1367 beneath->fetch_registers (regcache, regno);
c11d79f2
KB
1368 else
1369 {
edb5fb00 1370 thread = find_thread_ptid (regcache_get_ptid (regcache));
7aabaf9d
SM
1371 aix_thread_info *priv = get_aix_thread_info (thread);
1372 tid = priv->tid;
c11d79f2
KB
1373
1374 if (tid == PTHDB_INVALID_TID)
7aabaf9d 1375 fetch_regs_user_thread (regcache, priv->pdtid);
c11d79f2 1376 else
56be3814 1377 fetch_regs_kernel_thread (regcache, regno, tid);
c11d79f2
KB
1378 }
1379}
1380
61c5da0b
KB
1381/* Store the gp registers into an array of uint32_t or uint64_t. */
1382
1383static void
647478e0 1384fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
61c5da0b 1385{
ac7936df 1386 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
61c5da0b
KB
1387 int regno;
1388
daf6dc85 1389 for (regno = 0; regno < ppc_num_gprs; regno++)
672c9795
YQ
1390 if (REG_VALID == regcache_register_status (regcache,
1391 tdep->ppc_gp0_regnum + regno))
647478e0 1392 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
822c9732 1393 vals + regno);
61c5da0b
KB
1394}
1395
1396static void
647478e0 1397fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
61c5da0b 1398{
ac7936df 1399 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
61c5da0b
KB
1400 int regno;
1401
daf6dc85 1402 for (regno = 0; regno < ppc_num_gprs; regno++)
672c9795
YQ
1403 if (REG_VALID == regcache_register_status (regcache,
1404 tdep->ppc_gp0_regnum + regno))
647478e0 1405 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + regno,
822c9732 1406 vals + regno);
61c5da0b
KB
1407}
1408
1409/* Store the floating point registers into a double array. */
1410static void
647478e0 1411fill_fprs (const struct regcache *regcache, double *vals)
61c5da0b 1412{
ac7936df 1413 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1414 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
61c5da0b
KB
1415 int regno;
1416
383f0f5b
JB
1417 /* This function should never be called on architectures without
1418 floating-point registers. */
c7f30c7a 1419 gdb_assert (ppc_floating_point_unit_p (gdbarch));
383f0f5b 1420
366f009f
JB
1421 for (regno = tdep->ppc_fp0_regnum;
1422 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1423 regno++)
672c9795 1424 if (REG_VALID == regcache_register_status (regcache, regno))
e3ebf1bb
JB
1425 regcache_raw_collect (regcache, regno,
1426 vals + regno - tdep->ppc_fp0_regnum);
61c5da0b
KB
1427}
1428
c11d79f2 1429/* Store the special registers into the specified 64-bit and 32-bit
0fe7bf7b 1430 locations. */
c11d79f2
KB
1431
1432static void
647478e0
UW
1433fill_sprs64 (const struct regcache *regcache,
1434 uint64_t *iar, uint64_t *msr, uint32_t *cr,
0e061eef
KB
1435 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1436 uint32_t *fpscr)
c11d79f2 1437{
ac7936df 1438 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1439 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
f1a91342
KB
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. */
3e8c568d 1446 gdb_assert (sizeof (*iar) == register_size
c7f30c7a 1447 (gdbarch, gdbarch_pc_regnum (gdbarch)));
f1a91342 1448
672c9795
YQ
1449 if (REG_VALID == regcache_register_status (regcache,
1450 gdbarch_pc_regnum (gdbarch)))
c7f30c7a 1451 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
672c9795 1452 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
647478e0 1453 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
672c9795 1454 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
647478e0 1455 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
672c9795 1456 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
647478e0 1457 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
672c9795 1458 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
647478e0 1459 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
672c9795 1460 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
647478e0 1461 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
383f0f5b 1462 if (tdep->ppc_fpscr_regnum >= 0
672c9795
YQ
1463 && REG_VALID == regcache_register_status (regcache,
1464 tdep->ppc_fpscr_regnum))
647478e0 1465 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
61c5da0b
KB
1466}
1467
1468static void
647478e0
UW
1469fill_sprs32 (const struct regcache *regcache,
1470 uint32_t *iar, uint32_t *msr, uint32_t *cr,
0d16ee5d
UW
1471 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1472 uint32_t *fpscr)
61c5da0b 1473{
ac7936df 1474 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1475 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
f1a91342
KB
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,
0d16ee5d 1481 we should check all of the sizes. */
c7f30c7a
UW
1482 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1483 gdbarch_pc_regnum (gdbarch)));
f1a91342 1484
672c9795
YQ
1485 if (REG_VALID == regcache_register_status (regcache,
1486 gdbarch_pc_regnum (gdbarch)))
c7f30c7a 1487 regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), iar);
672c9795 1488 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
647478e0 1489 regcache_raw_collect (regcache, tdep->ppc_ps_regnum, msr);
672c9795 1490 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
647478e0 1491 regcache_raw_collect (regcache, tdep->ppc_cr_regnum, cr);
672c9795 1492 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
647478e0 1493 regcache_raw_collect (regcache, tdep->ppc_lr_regnum, lr);
672c9795 1494 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ctr_regnum))
647478e0 1495 regcache_raw_collect (regcache, tdep->ppc_ctr_regnum, ctr);
672c9795 1496 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_xer_regnum))
647478e0 1497 regcache_raw_collect (regcache, tdep->ppc_xer_regnum, xer);
383f0f5b 1498 if (tdep->ppc_fpscr_regnum >= 0
672c9795 1499 && REG_VALID == regcache_register_status (regcache, tdep->ppc_fpscr_regnum))
647478e0 1500 regcache_raw_collect (regcache, tdep->ppc_fpscr_regnum, fpscr);
c11d79f2
KB
1501}
1502
1503/* Store all registers into pthread PDTID, which doesn't have a kernel
1504 thread.
1505
0fe7bf7b
MS
1506 It's possible to store a single register into a non-kernel pthread,
1507 but I doubt it's worth the effort. */
c11d79f2
KB
1508
1509static void
647478e0 1510store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
c11d79f2 1511{
ac7936df 1512 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1513 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
c11d79f2
KB
1514 int status, i;
1515 pthdb_context_t ctx;
61c5da0b
KB
1516 uint32_t int32;
1517 uint64_t int64;
1518 double dbl;
c11d79f2 1519
8e2c28d4 1520 if (debug_aix_thread)
0fe7bf7b 1521 fprintf_unfiltered (gdb_stdlog,
206d3d3c 1522 "store_regs_user_thread %lx\n", (long) pdtid);
c11d79f2 1523
0fe7bf7b
MS
1524 /* Retrieve the thread's current context for its non-register
1525 values. */
c11d79f2
KB
1526 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1527 if (status != PTHDB_SUCCESS)
edefbb7c 1528 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
14fa3751 1529 pd_status2str (status));
c11d79f2 1530
61c5da0b 1531 /* Collect general-purpose register values from the regcache. */
c11d79f2 1532
063715bf 1533 for (i = 0; i < ppc_num_gprs; i++)
672c9795
YQ
1534 if (REG_VALID == regcache_register_status (regcache,
1535 tdep->ppc_gp0_regnum + i))
cbe92db4
KB
1536 {
1537 if (arch64)
1538 {
647478e0 1539 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
822c9732 1540 (void *) &int64);
cbe92db4
KB
1541 ctx.gpr[i] = int64;
1542 }
1543 else
1544 {
647478e0 1545 regcache_raw_collect (regcache, tdep->ppc_gp0_regnum + i,
822c9732 1546 (void *) &int32);
cbe92db4
KB
1547 ctx.gpr[i] = int32;
1548 }
1549 }
c11d79f2 1550
61c5da0b 1551 /* Collect floating-point register values from the regcache. */
c7f30c7a 1552 if (ppc_floating_point_unit_p (gdbarch))
647478e0 1553 fill_fprs (regcache, ctx.fpr);
c11d79f2 1554
61c5da0b
KB
1555 /* Special registers (always kept in ctx as 64 bits). */
1556 if (arch64)
1557 {
647478e0
UW
1558 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1559 &ctx.xer, &ctx.fpscr);
61c5da0b
KB
1560 }
1561 else
1562 {
1563 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
0d16ee5d
UW
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;
61c5da0b 1567
647478e0
UW
1568 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1569 &tmp_xer, &tmp_fpscr);
672c9795
YQ
1570 if (REG_VALID == regcache_register_status (regcache,
1571 gdbarch_pc_regnum (gdbarch)))
cbe92db4 1572 ctx.iar = tmp_iar;
672c9795 1573 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_ps_regnum))
cbe92db4 1574 ctx.msr = tmp_msr;
672c9795 1575 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_cr_regnum))
cbe92db4 1576 ctx.cr = tmp_cr;
672c9795 1577 if (REG_VALID == regcache_register_status (regcache, tdep->ppc_lr_regnum))
cbe92db4 1578 ctx.lr = tmp_lr;
672c9795
YQ
1579 if (REG_VALID == regcache_register_status (regcache,
1580 tdep->ppc_ctr_regnum))
cbe92db4 1581 ctx.ctr = tmp_ctr;
672c9795
YQ
1582 if (REG_VALID == regcache_register_status (regcache,
1583 tdep->ppc_xer_regnum))
cbe92db4 1584 ctx.xer = tmp_xer;
672c9795
YQ
1585 if (REG_VALID == regcache_register_status (regcache,
1586 tdep->ppc_xer_regnum))
0e061eef 1587 ctx.fpscr = tmp_fpscr;
61c5da0b 1588 }
c11d79f2
KB
1589
1590 status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1591 if (status != PTHDB_SUCCESS)
0963b4bd
MS
1592 error (_("aix-thread: store_registers: "
1593 "pthdb_pthread_setcontext returned %s"),
14fa3751 1594 pd_status2str (status));
c11d79f2
KB
1595}
1596
0fe7bf7b
MS
1597/* Store register REGNO if != -1 or all registers otherwise into
1598 kernel thread TID.
c11d79f2 1599
0fe7bf7b
MS
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. */
c11d79f2
KB
1604
1605static void
647478e0
UW
1606store_regs_kernel_thread (const struct regcache *regcache, int regno,
1607 pthdb_tid_t tid)
c11d79f2 1608{
ac7936df 1609 struct gdbarch *gdbarch = regcache->arch ();
c7f30c7a 1610 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
063715bf
JB
1611 uint64_t gprs64[ppc_num_gprs];
1612 uint32_t gprs32[ppc_num_gprs];
1613 double fprs[ppc_num_fprs];
c11d79f2 1614 struct ptxsprs sprs64;
61c5da0b
KB
1615 struct ptsprs sprs32;
1616 int i;
c11d79f2 1617
8e2c28d4 1618 if (debug_aix_thread)
206d3d3c
KB
1619 fprintf_unfiltered (gdb_stdlog,
1620 "store_regs_kernel_thread tid=%lx regno=%d\n",
1621 (long) tid, regno);
c11d79f2 1622
0fe7bf7b 1623 /* General-purpose registers. */
daf6dc85
JB
1624 if (regno == -1
1625 || (tdep->ppc_gp0_regnum <= regno
1626 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
c11d79f2 1627 {
c11d79f2 1628 if (arch64)
61c5da0b 1629 {
cbe92db4
KB
1630 /* Pre-fetch: some regs may not be in the cache. */
1631 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
647478e0 1632 fill_gprs64 (regcache, gprs64);
61c5da0b
KB
1633 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1634 }
c11d79f2 1635 else
61c5da0b 1636 {
cbe92db4 1637 /* Pre-fetch: some regs may not be in the cache. */
3b631e37 1638 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
647478e0 1639 fill_gprs32 (regcache, gprs32);
3b631e37 1640 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
61c5da0b 1641 }
c11d79f2
KB
1642 }
1643
0fe7bf7b 1644 /* Floating-point registers. */
c11d79f2 1645
c7f30c7a 1646 if (ppc_floating_point_unit_p (gdbarch)
383f0f5b
JB
1647 && (regno == -1
1648 || (regno >= tdep->ppc_fp0_regnum
1649 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
c11d79f2 1650 {
cbe92db4 1651 /* Pre-fetch: some regs may not be in the cache. */
3b631e37 1652 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
647478e0 1653 fill_fprs (regcache, fprs);
3b631e37 1654 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
c11d79f2
KB
1655 }
1656
0fe7bf7b 1657 /* Special-purpose registers. */
c11d79f2 1658
9970f04b 1659 if (regno == -1 || special_register_p (gdbarch, regno))
c11d79f2
KB
1660 {
1661 if (arch64)
1662 {
cbe92db4 1663 /* Pre-fetch: some registers won't be in the cache. */
0fe7bf7b
MS
1664 ptrace64aix (PTT_READ_SPRS, tid,
1665 (unsigned long) &sprs64, 0, NULL);
647478e0
UW
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);
0fe7bf7b
MS
1669 ptrace64aix (PTT_WRITE_SPRS, tid,
1670 (unsigned long) &sprs64, 0, NULL);
c11d79f2
KB
1671 }
1672 else
1673 {
0d16ee5d
UW
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
cbe92db4 1685 /* Pre-fetch: some registers won't be in the cache. */
3b631e37 1686 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
c11d79f2 1687
647478e0
UW
1688 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1689 &tmp_ctr, &tmp_xer, &tmp_fpscr);
0d16ee5d
UW
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;
c11d79f2 1698
f1a91342 1699 if (tdep->ppc_mq_regnum >= 0)
672c9795
YQ
1700 if (REG_VALID == regcache_register_status (regcache,
1701 tdep->ppc_mq_regnum))
647478e0 1702 regcache_raw_collect (regcache, tdep->ppc_mq_regnum,
822c9732 1703 &sprs32.pt_mq);
c11d79f2 1704
3b631e37 1705 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
c11d79f2
KB
1706 }
1707 }
1708}
1709
0fe7bf7b 1710/* Store gdb's current view of the register set into the
edb5fb00 1711 thread/process connected to REGCACHE. */
c11d79f2 1712
f6ac5f3d
PA
1713void
1714aix_thread_target::store_registers (struct regcache *regcache, int regno)
c11d79f2
KB
1715{
1716 struct thread_info *thread;
1717 pthdb_tid_t tid;
f6ac5f3d 1718 struct target_ops *beneath = find_target_beneath (this);
c11d79f2 1719
edb5fb00 1720 if (!PD_TID (regcache_get_ptid (regcache)))
f6ac5f3d 1721 beneath->store_registers (regcache, regno);
c11d79f2
KB
1722 else
1723 {
edb5fb00 1724 thread = find_thread_ptid (regcache_get_ptid (regcache));
7aabaf9d
SM
1725 aix_thread_info *priv = get_aix_thread_info (thread);
1726 tid = priv->tid;
c11d79f2
KB
1727
1728 if (tid == PTHDB_INVALID_TID)
7aabaf9d 1729 store_regs_user_thread (regcache, priv->pdtid);
c11d79f2 1730 else
56be3814 1731 store_regs_kernel_thread (regcache, regno, tid);
c11d79f2
KB
1732 }
1733}
1734
edcc890f 1735/* Implement the to_xfer_partial target_ops method. */
037a727e 1736
f6ac5f3d
PA
1737enum target_xfer_status
1738aix_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)
c11d79f2 1743{
2989a365 1744 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
f6ac5f3d 1745 struct target_ops *beneath = find_target_beneath (this);
14fa3751 1746
dfd4cc63 1747 inferior_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
f6ac5f3d
PA
1748 return beneath->xfer_partial (object, annex, readbuf,
1749 writebuf, offset, len, xfered_len);
c11d79f2
KB
1750}
1751
0fe7bf7b 1752/* Clean up after the inferior exits. */
c11d79f2 1753
f6ac5f3d
PA
1754void
1755aix_thread_target::mourn_inferior ()
c11d79f2 1756{
f6ac5f3d 1757 struct target_ops *beneath = find_target_beneath (this);
d30acaa7 1758
c11d79f2 1759 pd_deactivate ();
f6ac5f3d 1760 beneath->mourn_inferior ();
c11d79f2
KB
1761}
1762
0fe7bf7b 1763/* Return whether thread PID is still valid. */
c11d79f2 1764
57810aa7 1765bool
f6ac5f3d 1766aix_thread_target::thread_alive (ptid_t ptid)
c11d79f2 1767{
f6ac5f3d 1768 struct target_ops *beneath = find_target_beneath (this);
d30acaa7 1769
c11d79f2 1770 if (!PD_TID (ptid))
f6ac5f3d 1771 return beneath->thread_alive (ptid);
c11d79f2 1772
0fe7bf7b
MS
1773 /* We update the thread list every time the child stops, so all
1774 valid threads should be in the thread list. */
c11d79f2
KB
1775 return in_thread_list (ptid);
1776}
1777
0fe7bf7b
MS
1778/* Return a printable representation of composite PID for use in
1779 "info threads" output. */
c11d79f2 1780
f6ac5f3d
PA
1781const char *
1782aix_thread_target::pid_to_str (ptid_t ptid)
c11d79f2
KB
1783{
1784 static char *ret = NULL;
f6ac5f3d 1785 struct target_ops *beneath = find_target_beneath (this);
c11d79f2
KB
1786
1787 if (!PD_TID (ptid))
f6ac5f3d 1788 return beneath->pid_to_str (ptid);
c11d79f2
KB
1789
1790 /* Free previous return value; a new one will be allocated by
b435e160 1791 xstrprintf(). */
c11d79f2
KB
1792 xfree (ret);
1793
edefbb7c 1794 ret = xstrprintf (_("Thread %ld"), ptid_get_tid (ptid));
c11d79f2
KB
1795 return ret;
1796}
1797
0fe7bf7b
MS
1798/* Return a printable representation of extra information about
1799 THREAD, for use in "info threads" output. */
c11d79f2 1800
f6ac5f3d
PA
1801const char *
1802aix_thread_target::extra_thread_info (struct thread_info *thread)
c11d79f2 1803{
c11d79f2
KB
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;
c11d79f2
KB
1811 static char *ret = NULL;
1812
1813 if (!PD_TID (thread->ptid))
1814 return NULL;
1815
d7e74731 1816 string_file buf;
7aabaf9d 1817 aix_thread_info *priv = get_aix_thread_info (thread);
c11d79f2 1818
7aabaf9d
SM
1819 pdtid = priv->pdtid;
1820 tid = priv->tid;
c11d79f2
KB
1821
1822 if (tid != PTHDB_INVALID_TID)
edefbb7c 1823 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
d7e74731 1824 buf.printf (_("tid %d"), (int)tid);
c11d79f2
KB
1825
1826 status = pthdb_pthread_state (pd_session, pdtid, &state);
1827 if (status != PTHDB_SUCCESS)
1828 state = PST_NOTSUP;
d7e74731 1829 buf.printf (", %s", state2str (state));
c11d79f2 1830
0fe7bf7b
MS
1831 status = pthdb_pthread_suspendstate (pd_session, pdtid,
1832 &suspendstate);
c11d79f2 1833 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
edefbb7c 1834 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
d7e74731 1835 buf.printf (_(", suspended"));
c11d79f2 1836
0fe7bf7b
MS
1837 status = pthdb_pthread_detachstate (pd_session, pdtid,
1838 &detachstate);
c11d79f2 1839 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
edefbb7c 1840 /* i18n: Like "Thread-Id %d, [state] running, detached" */
d7e74731 1841 buf.printf (_(", detached"));
c11d79f2
KB
1842
1843 pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1844 if (status == PTHDB_SUCCESS && cancelpend)
edefbb7c 1845 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
d7e74731 1846 buf.printf (_(", cancel pending"));
c11d79f2 1847
d7e74731 1848 buf.write ("", 1);
c11d79f2
KB
1849
1850 xfree (ret); /* Free old buffer. */
1851
d7e74731 1852 ret = xstrdup (buf.c_str ());
c11d79f2
KB
1853
1854 return ret;
1855}
1856
f6ac5f3d
PA
1857ptid_t
1858aix_thread_target::get_ada_task_ptid (long lwp, long thread)
c7660128
JB
1859{
1860 return ptid_build (ptid_get_pid (inferior_ptid), 0, thread);
1861}
1862
c11d79f2
KB
1863
1864/* Module startup initialization function, automagically called by
0fe7bf7b 1865 init.c. */
c11d79f2
KB
1866
1867void
1868_initialize_aix_thread (void)
1869{
0fe7bf7b 1870 /* Notice when object files get loaded and unloaded. */
76727919 1871 gdb::observers::new_objfile.attach (new_objfile);
8e2c28d4 1872
b3ccfe11
TT
1873 /* Add ourselves to inferior_created event chain.
1874 This is needed to enable the thread target on "attach". */
76727919 1875 gdb::observers::inferior_created.attach (aix_thread_inferior_created);
b3ccfe11 1876
577b7047 1877 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
0963b4bd
MS
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);
c11d79f2 1885}
This page took 1.054012 seconds and 4 git commands to generate.