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