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