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