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