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