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