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