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