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