Automatic date update in version.in
[binutils-gdb.git] / gdb / aix-thread.c
blob0fe87b2b0809d509f15a4ba638eb90495d1b0e3e
1 /* Low level interface for debugging AIX 4.3+ pthreads.
3 Copyright (C) 1999-2024 Free Software Foundation, Inc.
4 Written by Nick Duffek <nsd@redhat.com>.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This module uses the libpthdebug.a library provided by AIX 4.3+ for
23 debugging pthread applications.
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
30 libpthdebug peculiarities:
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.
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().
42 #include "gdbthread.h"
43 #include "target.h"
44 #include "inferior.h"
45 #include "regcache.h"
46 #include "gdbcmd.h"
47 #include "ppc-tdep.h"
48 #include "observable.h"
49 #include "objfiles.h"
51 #include <procinfo.h>
52 #include <sys/types.h>
53 #include <sys/ptrace.h>
54 #include <sys/reg.h>
55 #include <sched.h>
56 #include <sys/pthdebug.h>
58 #if !HAVE_DECL_GETTHRDS
59 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
60 #endif
62 /* Whether to emit debugging output. */
63 static bool debug_aix_thread;
65 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
66 #ifndef PTHDB_VERSION_3
67 #define pthdb_tid_t tid_t
68 #endif
70 /* Success and failure values returned by pthdb callbacks. */
72 #define PDC_SUCCESS PTHDB_SUCCESS
73 #define PDC_FAILURE PTHDB_CALLBACK
75 /* Private data attached to each element in GDB's thread list. */
77 struct aix_thread_info : public private_thread_info
79 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
80 pthdb_tid_t tid; /* kernel thread id */
83 /* Return the aix_thread_info attached to THREAD. */
85 static aix_thread_info *
86 get_aix_thread_info (thread_info *thread)
88 return gdb::checked_static_cast<aix_thread_info *> (thread->priv.get ());
91 /* Information about a thread of which libpthdebug is aware. */
93 struct pd_thread {
94 pthdb_pthread_t pdtid;
95 pthread_t pthid;
96 pthdb_tid_t tid;
99 /* This module's target-specific operations, active while pd_able is true. */
101 static const target_info aix_thread_target_info = {
102 "aix-threads",
103 N_("AIX pthread support"),
104 N_("AIX pthread support")
107 class aix_thread_target final : public target_ops
109 public:
110 const target_info &info () const override
111 { return aix_thread_target_info; }
113 strata stratum () const override { return thread_stratum; }
115 void detach (inferior *, int) override;
116 void resume (ptid_t, int, enum gdb_signal) override;
117 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
119 void fetch_registers (struct regcache *, int) override;
120 void store_registers (struct regcache *, int) override;
122 enum target_xfer_status xfer_partial (enum target_object object,
123 const char *annex,
124 gdb_byte *readbuf,
125 const gdb_byte *writebuf,
126 ULONGEST offset, ULONGEST len,
127 ULONGEST *xfered_len) override;
129 void mourn_inferior () override;
131 bool thread_alive (ptid_t ptid) override;
133 std::string pid_to_str (ptid_t) override;
135 const char *extra_thread_info (struct thread_info *) override;
137 ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
139 void update_thread_list () override;
142 static aix_thread_target aix_thread_ops;
144 /* Forward declarations for pthdb callbacks. */
146 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
147 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
148 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
149 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
150 unsigned long long flags,
151 pthdb_context_t *context);
152 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
153 unsigned long long flags,
154 pthdb_context_t *context);
155 static int pdc_alloc (pthdb_user_t, size_t, void **);
156 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
157 static int pdc_dealloc (pthdb_user_t, void *);
159 /* pthdb callbacks. */
161 static pthdb_callbacks_t pd_callbacks = {
162 pdc_symbol_addrs,
163 pdc_read_data,
164 pdc_write_data,
165 pdc_read_regs,
166 pdc_write_regs,
167 pdc_alloc,
168 pdc_realloc,
169 pdc_dealloc,
170 NULL
173 /* Aix variable structure. */
174 struct aix_thread_variables
176 /* Whether the current application is debuggable by pthdb. */
177 int pd_able;
179 /* Whether a threaded application is being debugged. */
180 int pd_active;
182 /* Current pthdb session. */
183 pthdb_session_t pd_session;
185 /* Address of the function that libpthread will call when libpthdebug
186 is ready to be initialized. */
187 CORE_ADDR pd_brk_addr;
189 /* Whether the current architecture is 64-bit.
190 Only valid when pd_able is true. */
191 int arch64;
194 /* Key to our per-inferior data. */
195 static const registry<inferior>::key<aix_thread_variables>
196 aix_thread_variables_handle;
198 /* Function to Get aix_thread_variables data. */
199 static struct aix_thread_variables*
200 get_aix_thread_variables_data (struct inferior *inf)
202 if (inf == NULL)
203 return NULL;
205 struct aix_thread_variables* data;
207 data = aix_thread_variables_handle.get (inf);
208 if (data == NULL)
209 data = aix_thread_variables_handle.emplace (inf);
211 return data;
214 /* Helper to get data for ptid in a function. */
216 static struct aix_thread_variables*
217 get_thread_data_helper_for_ptid (ptid_t ptid)
219 inferior *inf = find_inferior_ptid (current_inferior ()->process_target (),
220 ptid);
221 return get_aix_thread_variables_data (inf);
224 /* Helper to get data for pid in a function. */
226 static struct aix_thread_variables*
227 get_thread_data_helper_for_pid (pid_t pid)
229 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
230 pid);
231 return get_aix_thread_variables_data (inf);
234 /* Return a printable representation of pthdebug function return
235 STATUS. */
237 static const char *
238 pd_status2str (int status)
240 switch (status)
242 case PTHDB_SUCCESS: return "SUCCESS";
243 case PTHDB_NOSYS: return "NOSYS";
244 case PTHDB_NOTSUP: return "NOTSUP";
245 case PTHDB_BAD_VERSION: return "BAD_VERSION";
246 case PTHDB_BAD_USER: return "BAD_USER";
247 case PTHDB_BAD_SESSION: return "BAD_SESSION";
248 case PTHDB_BAD_MODE: return "BAD_MODE";
249 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
250 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
251 case PTHDB_BAD_POINTER: return "BAD_POINTER";
252 case PTHDB_BAD_CMD: return "BAD_CMD";
253 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
254 case PTHDB_BAD_ATTR: return "BAD_ATTR";
255 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
256 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
257 case PTHDB_BAD_COND: return "BAD_COND";
258 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
259 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
260 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
261 case PTHDB_BAD_KEY: return "BAD_KEY";
262 case PTHDB_BAD_PTID: return "BAD_PTID";
263 case PTHDB_BAD_TID: return "BAD_TID";
264 case PTHDB_CALLBACK: return "CALLBACK";
265 case PTHDB_CONTEXT: return "CONTEXT";
266 case PTHDB_HELD: return "HELD";
267 case PTHDB_NOT_HELD: return "NOT_HELD";
268 case PTHDB_MEMORY: return "MEMORY";
269 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
270 case PTHDB_SYMBOL: return "SYMBOL";
271 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
272 case PTHDB_INTERNAL: return "INTERNAL";
273 default: return "UNKNOWN";
277 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
278 exceptional conditions and either return nonlocally or else return
279 1 for success and 0 for failure. */
281 static int
282 ptrace_check (int req, int id, int ret)
284 if (ret == 0 && !errno)
285 return 1;
287 /* According to ptrace(2), ptrace may fail with EPERM if "the
288 Identifier parameter corresponds to a kernel thread which is
289 stopped in kernel mode and whose computational state cannot be
290 read or written." This happens quite often with register reads. */
292 switch (req)
294 case PTT_READ_GPRS:
295 case PTT_READ_FPRS:
296 case PTT_READ_SPRS:
297 if (ret == -1 && errno == EPERM)
299 if (debug_aix_thread)
300 gdb_printf (gdb_stdlog,
301 "ptrace (%d, %d) = %d (errno = %d)\n",
302 req, id, ret, errno);
303 return ret == -1 ? 0 : 1;
305 break;
306 case PTT_READ_VEC:
307 case PTT_READ_VSX:
308 if (debug_aix_thread)
309 gdb_printf (gdb_stdlog,
310 "ptrace (%d, %d) = %d (errno = %d)\n",
311 req, id, ret, errno);
312 if (ret == -1)
313 return -1;
314 break;
316 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
317 req, id, ret, errno, safe_strerror (errno));
318 return 0; /* Not reached. */
321 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
322 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
323 Return success. */
325 #ifdef HAVE_PTRACE64
326 # define ptracex(request, pid, addr, data, buf) \
327 ptrace64 (request, pid, addr, data, buf)
328 #endif
330 static int
331 ptrace64aix (int req, int id, long long addr, int data, int *buf)
333 errno = 0;
334 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
337 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
338 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
339 Return success. */
341 #ifdef HAVE_PTRACE64
342 # define ptrace(request, pid, addr, data, buf) \
343 ptrace64 (request, pid, addr, data, buf)
344 # define addr_ptr long long
345 #else
346 # define addr_ptr int *
347 #endif
349 static int
350 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
352 errno = 0;
353 return ptrace_check (req, id,
354 ptrace (req, id, addr, data, buf));
357 /* If *PIDP is a composite process/thread id, convert it to a
358 process id. */
360 static void
361 pid_to_prc (ptid_t *ptidp)
363 ptid_t ptid;
365 ptid = *ptidp;
366 if (ptid.tid () != 0)
367 *ptidp = ptid_t (ptid.pid ());
370 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
371 the address of SYMBOLS[<i>].name. */
373 static int
374 pdc_symbol_addrs (pthdb_user_t user_current_pid, pthdb_symbol_t *symbols, int count)
376 struct bound_minimal_symbol ms;
377 int i;
378 char *name;
380 if (debug_aix_thread)
381 gdb_printf (gdb_stdlog,
382 "pdc_symbol_addrs (user_current_pid = %ld, symbols = 0x%lx, count = %d)\n",
383 user_current_pid, (long) symbols, count);
385 for (i = 0; i < count; i++)
387 name = symbols[i].name;
388 if (debug_aix_thread)
389 gdb_printf (gdb_stdlog,
390 " symbols[%d].name = \"%s\"\n", i, name);
392 if (!*name)
393 symbols[i].addr = 0;
394 else
396 ms = lookup_minimal_symbol (name, NULL, NULL);
397 if (ms.minsym == NULL)
399 if (debug_aix_thread)
400 gdb_printf (gdb_stdlog, " returning PDC_FAILURE\n");
401 return PDC_FAILURE;
403 symbols[i].addr = ms.value_address ();
405 if (debug_aix_thread)
406 gdb_printf (gdb_stdlog, " symbols[%d].addr = %s\n",
407 i, hex_string (symbols[i].addr));
409 if (debug_aix_thread)
410 gdb_printf (gdb_stdlog, " returning PDC_SUCCESS\n");
411 return PDC_SUCCESS;
414 /* Read registers call back function should be able to read the
415 context information of a debuggee kernel thread from an active
416 process or from a core file. The information should be formatted
417 in context64 form for both 32-bit and 64-bit process.
418 If successful return 0, else non-zero is returned. */
420 static int
421 pdc_read_regs (pthdb_user_t user_current_pid,
422 pthdb_tid_t tid,
423 unsigned long long flags,
424 pthdb_context_t *context)
426 /* This function doesn't appear to be used, so we could probably
427 just return 0 here. HOWEVER, if it is not defined, the OS will
428 complain and several thread debug functions will fail. In case
429 this is needed, I have implemented what I think it should do,
430 however this code is untested. */
432 uint64_t gprs64[ppc_num_gprs];
433 uint32_t gprs32[ppc_num_gprs];
434 double fprs[ppc_num_fprs];
435 struct ptxsprs sprs64;
436 struct ptsprs sprs32;
437 struct aix_thread_variables *data;
439 data = get_thread_data_helper_for_pid (user_current_pid);
441 if (debug_aix_thread)
442 gdb_printf (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
443 (int) tid, hex_string (flags));
445 /* General-purpose registers. */
446 if (flags & PTHDB_FLAG_GPRS)
448 if (data->arch64)
450 if (!ptrace64aix (PTT_READ_GPRS, tid,
451 (unsigned long) gprs64, 0, NULL))
452 memset (gprs64, 0, sizeof (gprs64));
453 memcpy (context->gpr, gprs64, sizeof(gprs64));
455 else
457 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
458 memset (gprs32, 0, sizeof (gprs32));
459 memcpy (context->gpr, gprs32, sizeof(gprs32));
463 /* Floating-point registers. */
464 if (flags & PTHDB_FLAG_FPRS)
466 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
467 memset (fprs, 0, sizeof (fprs));
468 memcpy (context->fpr, fprs, sizeof(fprs));
471 /* Special-purpose registers. */
472 if (flags & PTHDB_FLAG_SPRS)
474 if (data->arch64)
476 if (!ptrace64aix (PTT_READ_SPRS, tid,
477 (unsigned long) &sprs64, 0, NULL))
478 memset (&sprs64, 0, sizeof (sprs64));
479 memcpy (&context->msr, &sprs64, sizeof(sprs64));
481 else
483 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
484 memset (&sprs32, 0, sizeof (sprs32));
485 memcpy (&context->msr, &sprs32, sizeof(sprs32));
489 /* vector registers. */
490 __vmx_context_t vmx;
491 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
493 if (data->arch64)
495 if (!ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
496 memset (&vmx, 0, sizeof (vmx));
497 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
499 else
501 if (!ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
502 memset (&vmx, 0, sizeof (vmx));
503 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
507 /* vsx registers. */
508 __vsx_context_t vsx;
509 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
511 if (data->arch64)
513 if (!ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
514 memset (&vsx, 0, sizeof (vsx));
515 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
517 else
519 if (!ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
520 memset (&vsx, 0, sizeof (vsx));
521 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
524 return 0;
527 /* Write register function should be able to write requested context
528 information to specified debuggee's kernel thread id.
529 If successful return 0, else non-zero is returned. */
531 static int
532 pdc_write_regs (pthdb_user_t user_current_pid,
533 pthdb_tid_t tid,
534 unsigned long long flags,
535 pthdb_context_t *context)
537 /* This function doesn't appear to be used, so we could probably
538 just return 0 here. HOWEVER, if it is not defined, the OS will
539 complain and several thread debug functions will fail. In case
540 this is needed, I have implemented what I think it should do,
541 however this code is untested. */
543 struct aix_thread_variables *data;
545 data = get_thread_data_helper_for_pid (user_current_pid);
547 if (debug_aix_thread)
548 gdb_printf (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
549 (int) tid, hex_string (flags));
551 /* General-purpose registers. */
552 if (flags & PTHDB_FLAG_GPRS)
554 if (data->arch64)
555 ptrace64aix (PTT_WRITE_GPRS, tid,
556 (unsigned long) context->gpr, 0, NULL);
557 else
558 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
561 /* Floating-point registers. */
562 if (flags & PTHDB_FLAG_FPRS)
564 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
567 /* Special-purpose registers. */
568 if (flags & PTHDB_FLAG_SPRS)
570 if (data->arch64)
572 ptrace64aix (PTT_WRITE_SPRS, tid,
573 (unsigned long) &context->msr, 0, NULL);
575 else
577 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
581 /* vector registers. */
582 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
584 if (data->arch64)
585 ptrace64aix (PTT_WRITE_VEC, tid, (unsigned long) &context->vmx, 0, 0);
586 else
587 ptrace32 (PTT_WRITE_VEC, tid, (uintptr_t) &context->vmx, 0, 0);
590 /* vsx registers. */
591 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
593 if (data->arch64)
594 ptrace64aix (PTT_WRITE_VSX, tid, (unsigned long) &context->vsx, 0, 0);
595 else
596 ptrace32 (PTT_WRITE_VSX, tid, (uintptr_t) &context->vsx, 0, 0);
598 return 0;
601 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
603 static int
604 pdc_read_data (pthdb_user_t user_current_pid, void *buf,
605 pthdb_addr_t addr, size_t len)
607 int status, ret;
608 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
609 user_current_pid);
611 if (debug_aix_thread)
612 gdb_printf (gdb_stdlog,
613 "pdc_read_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
614 user_current_pid, (long) buf, hex_string (addr), len);
616 /* This is needed to eliminate the dependency of current thread
617 which is null so that thread reads the correct target memory. */
619 scoped_restore_current_inferior_for_memory save_inferior (inf);
620 status = target_read_memory (addr, (gdb_byte *) buf, len);
622 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
624 if (debug_aix_thread)
625 gdb_printf (gdb_stdlog, " status=%d, returning %s\n",
626 status, pd_status2str (ret));
627 return ret;
630 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
632 static int
633 pdc_write_data (pthdb_user_t user_current_pid, void *buf,
634 pthdb_addr_t addr, size_t len)
636 int status, ret;
637 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
638 user_current_pid);
640 if (debug_aix_thread)
641 gdb_printf (gdb_stdlog,
642 "pdc_write_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
643 user_current_pid, (long) buf, hex_string (addr), len);
646 scoped_restore_current_inferior_for_memory save_inferior (inf);
647 status = target_write_memory (addr, (gdb_byte *) buf, len);
650 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
652 if (debug_aix_thread)
653 gdb_printf (gdb_stdlog, " status=%d, returning %s\n", status,
654 pd_status2str (ret));
655 return ret;
658 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
659 in BUFP. */
661 static int
662 pdc_alloc (pthdb_user_t user_current_pid, size_t len, void **bufp)
664 if (debug_aix_thread)
665 gdb_printf (gdb_stdlog,
666 "pdc_alloc (user_current_pid = %ld, len = %ld, bufp = 0x%lx)\n",
667 user_current_pid, len, (long) bufp);
668 *bufp = xmalloc (len);
669 if (debug_aix_thread)
670 gdb_printf (gdb_stdlog,
671 " malloc returned 0x%lx\n", (long) *bufp);
673 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
674 be returned. */
676 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
679 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
680 realloc callback, so that it contains LEN bytes, and store a
681 pointer to the result in BUFP. */
683 static int
684 pdc_realloc (pthdb_user_t user_current_pid, void *buf, size_t len, void **bufp)
686 if (debug_aix_thread)
687 gdb_printf (gdb_stdlog,
688 "pdc_realloc (user_current_pid = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
689 user_current_pid, (long) buf, len, (long) bufp);
690 *bufp = xrealloc (buf, len);
691 if (debug_aix_thread)
692 gdb_printf (gdb_stdlog,
693 " realloc returned 0x%lx\n", (long) *bufp);
694 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
697 /* pthdb callback: free BUF, which was allocated by the alloc or
698 realloc callback. */
700 static int
701 pdc_dealloc (pthdb_user_t user_current_pid, void *buf)
703 if (debug_aix_thread)
704 gdb_printf (gdb_stdlog,
705 "pdc_free (user_current_pid = %ld, buf = 0x%lx)\n", user_current_pid,
706 (long) buf);
707 xfree (buf);
708 return PDC_SUCCESS;
711 /* Return a printable representation of pthread STATE. */
713 static char *
714 state2str (pthdb_state_t state)
716 switch (state)
718 case PST_IDLE:
719 /* i18n: Like "Thread-Id %d, [state] idle" */
720 return _("idle"); /* being created */
721 case PST_RUN:
722 /* i18n: Like "Thread-Id %d, [state] running" */
723 return _("running"); /* running */
724 case PST_SLEEP:
725 /* i18n: Like "Thread-Id %d, [state] sleeping" */
726 return _("sleeping"); /* awaiting an event */
727 case PST_READY:
728 /* i18n: Like "Thread-Id %d, [state] ready" */
729 return _("ready"); /* runnable */
730 case PST_TERM:
731 /* i18n: Like "Thread-Id %d, [state] finished" */
732 return _("finished"); /* awaiting a join/detach */
733 default:
734 /* i18n: Like "Thread-Id %d, [state] unknown" */
735 return _("unknown");
739 /* qsort() comparison function for sorting pd_thread structs by pthid. */
741 static int
742 pcmp (const void *p1v, const void *p2v)
744 struct pd_thread *p1 = (struct pd_thread *) p1v;
745 struct pd_thread *p2 = (struct pd_thread *) p2v;
746 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
749 /* ptid comparison function */
751 static int
752 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
754 if (ptid1.pid () < ptid2.pid ())
755 return -1;
756 else if (ptid1.pid () > ptid2.pid ())
757 return 1;
758 else if (ptid1.tid () < ptid2.tid ())
759 return -1;
760 else if (ptid1.tid () > ptid2.tid ())
761 return 1;
762 else if (ptid1.lwp () < ptid2.lwp ())
763 return -1;
764 else if (ptid1.lwp () > ptid2.lwp ())
765 return 1;
766 else
767 return 0;
770 /* qsort() comparison function for sorting thread_info structs by pid. */
772 static int
773 gcmp (const void *t1v, const void *t2v)
775 struct thread_info *t1 = *(struct thread_info **) t1v;
776 struct thread_info *t2 = *(struct thread_info **) t2v;
777 return ptid_cmp (t1->ptid, t2->ptid);
780 /* Search through the list of all kernel threads for the thread
781 that has stopped on a SIGTRAP signal, and return its TID.
782 Return 0 if none found. */
784 static pthdb_tid_t
785 get_signaled_thread (int pid)
787 struct thrdsinfo64 thrinf;
788 tid_t ktid = 0;
790 while (1)
792 if (getthrds (pid, &thrinf,
793 sizeof (thrinf), &ktid, 1) != 1)
794 break;
796 /* We also need to keep in mind Trap and interrupt or any
797 signal that needs to be handled in pd_update (). */
799 if (thrinf.ti_cursig)
800 return thrinf.ti_tid;
803 /* Didn't find any thread stopped on a SIGTRAP signal. */
804 return 0;
807 /* Synchronize GDB's thread list with libpthdebug's.
809 There are some benefits of doing this every time the inferior stops:
811 - allows users to run thread-specific commands without needing to
812 run "info threads" first
814 - helps pthdb_tid_pthread() work properly (see "libpthdebug
815 peculiarities" at the top of this module)
817 - simplifies the demands placed on libpthdebug, which seems to
818 have difficulty with certain call patterns */
820 static void
821 sync_threadlists (pid_t pid)
823 int cmd, status;
824 int pcount, psize, pi, gcount, gi;
825 struct pd_thread *pbuf;
826 struct thread_info **gbuf, **g, *thread;
827 pthdb_pthread_t pdtid;
828 pthread_t pthid;
829 pthdb_tid_t tid;
830 process_stratum_target *proc_target = current_inferior ()->process_target ();
831 struct aix_thread_variables *data;
832 data = get_thread_data_helper_for_pid (pid);
834 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
836 pcount = 0;
837 psize = 1;
838 pbuf = XNEWVEC (struct pd_thread, psize);
840 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
842 status = pthdb_pthread (data->pd_session, &pdtid, cmd);
843 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
844 break;
846 status = pthdb_pthread_ptid (data->pd_session, pdtid, &pthid);
847 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
848 continue;
850 if (pcount == psize)
852 psize *= 2;
853 pbuf = (struct pd_thread *) xrealloc (pbuf,
854 psize * sizeof *pbuf);
856 pbuf[pcount].pdtid = pdtid;
857 pbuf[pcount].pthid = pthid;
858 pcount++;
861 for (pi = 0; pi < pcount; pi++)
863 status = pthdb_pthread_tid (data->pd_session, pbuf[pi].pdtid, &tid);
864 if (status != PTHDB_SUCCESS)
865 tid = PTHDB_INVALID_TID;
866 pbuf[pi].tid = tid;
869 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
871 /* Accumulate an array of GDB threads sorted by pid. */
873 /* gcount is GDB thread count and pcount is pthreadlib thread count. */
875 gcount = 0;
876 for (thread_info *tp : all_threads (proc_target, ptid_t (pid)))
877 gcount++;
878 g = gbuf = XNEWVEC (struct thread_info *, gcount);
879 for (thread_info *tp : all_threads (proc_target, ptid_t (pid)))
880 *g++ = tp;
881 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
883 /* Apply differences between the two arrays to GDB's thread list. */
885 for (pi = gi = 0; pi < pcount || gi < gcount;)
887 if (pi == pcount)
889 delete_thread (gbuf[gi]);
890 gi++;
892 else if (gi == gcount)
894 aix_thread_info *priv = new aix_thread_info;
895 priv->pdtid = pbuf[pi].pdtid;
896 priv->tid = pbuf[pi].tid;
898 thread = add_thread_with_info (proc_target,
899 ptid_t (pid, 0, pbuf[pi].pthid),
900 private_thread_info_up (priv));
902 pi++;
904 else
906 ptid_t pptid, gptid;
907 int cmp_result;
909 pptid = ptid_t (pid, 0, pbuf[pi].pthid);
910 gptid = gbuf[gi]->ptid;
911 pdtid = pbuf[pi].pdtid;
912 tid = pbuf[pi].tid;
914 cmp_result = ptid_cmp (pptid, gptid);
916 if (cmp_result == 0)
918 aix_thread_info *priv = get_aix_thread_info (gbuf[gi]);
920 priv->pdtid = pdtid;
921 priv->tid = tid;
922 pi++;
923 gi++;
925 else if (cmp_result > 0)
927 /* This is to make the main process thread now look
928 like a thread. */
930 if (gptid.is_pid ())
932 thread_info *tp = proc_target->find_thread (gptid);
933 thread_change_ptid (proc_target, gptid, pptid);
934 aix_thread_info *priv = new aix_thread_info;
935 priv->pdtid = pbuf[pi].pdtid;
936 priv->tid = pbuf[pi].tid;
937 tp->priv.reset (priv);
938 gi++;
939 pi++;
941 else
943 delete_thread (gbuf[gi]);
944 gi++;
947 else
949 thread = add_thread (proc_target, pptid);
951 aix_thread_info *priv = new aix_thread_info;
952 thread->priv.reset (priv);
953 priv->pdtid = pdtid;
954 priv->tid = tid;
955 pi++;
960 xfree (pbuf);
961 xfree (gbuf);
964 /* Iterate_over_threads() callback for locating a thread, using
965 the TID of its associated kernel thread. */
967 static int
968 iter_tid (struct thread_info *thread, void *tidp)
970 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
971 aix_thread_info *priv = get_aix_thread_info (thread);
973 return priv->tid == tid;
976 /* Synchronize libpthdebug's state with the inferior and with GDB,
977 generate a composite process/thread <pid> for the current thread,
978 Return the ptid of the event thread if one can be found, else
979 return a pid-only ptid with PID. */
981 static ptid_t
982 pd_update (pid_t pid)
984 int status;
985 ptid_t ptid;
986 pthdb_tid_t tid;
987 struct thread_info *thread = NULL;
988 struct aix_thread_variables *data;
990 data = get_thread_data_helper_for_pid (pid);
992 if (!data->pd_active)
993 return ptid_t (pid);
995 status = pthdb_session_update (data->pd_session);
996 if (status != PTHDB_SUCCESS)
997 return ptid_t (pid);
999 sync_threadlists (pid);
1001 /* Define "current thread" as one that just received a trap signal. */
1003 tid = get_signaled_thread (pid);
1004 if (tid != 0)
1005 thread = iterate_over_threads (iter_tid, &tid);
1006 if (!thread)
1007 ptid = ptid_t (pid);
1008 else
1009 ptid = thread->ptid;
1011 return ptid;
1014 /* Try to start debugging threads in the current process.
1015 If successful and there exists and we can find an event thread, return a ptid
1016 for that thread. Otherwise, return a ptid-only ptid using PID. */
1018 static void
1019 pd_activate (pid_t pid)
1021 int status;
1022 struct aix_thread_variables *data;
1023 data = get_thread_data_helper_for_pid (pid);
1025 status = pthdb_session_init (pid, data->arch64 ? PEM_64BIT : PEM_32BIT,
1026 PTHDB_FLAG_REGS, &pd_callbacks,
1027 &data->pd_session);
1028 if (status == PTHDB_SUCCESS)
1029 data->pd_active = 1;
1032 /* AIX implementation of update_thread_list. */
1034 void
1035 aix_thread_target::update_thread_list ()
1037 for (inferior *inf : all_inferiors ())
1039 if (inf->pid == 0)
1040 continue;
1042 pd_update (inf->pid);
1047 /* An object file has just been loaded. Check whether the current
1048 application is pthreaded, and if so, prepare for thread debugging. */
1050 static void
1051 pd_enable (inferior *inf)
1053 int status;
1054 char *stub_name;
1055 struct bound_minimal_symbol ms;
1056 struct aix_thread_variables *data;
1058 if (inf == NULL)
1059 return;
1061 data = get_aix_thread_variables_data (inf);
1063 /* Don't initialize twice. */
1064 if (data->pd_able)
1065 return;
1067 /* Check application word size. */
1068 data->arch64 = register_size (current_inferior ()->arch (), 0) == 8;
1070 /* Check whether the application is pthreaded. */
1071 stub_name = NULL;
1072 status = pthdb_session_pthreaded (inf->pid, PTHDB_FLAG_REGS,
1073 &pd_callbacks, &stub_name);
1074 if ((status != PTHDB_SUCCESS
1075 && status != PTHDB_NOT_PTHREADED) || !stub_name)
1076 return;
1078 /* Set a breakpoint on the returned stub function. */
1079 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
1080 if (ms.minsym == NULL)
1081 return;
1082 data->pd_brk_addr = ms.value_address ();
1083 if (!create_thread_event_breakpoint (current_inferior ()->arch (),
1084 data->pd_brk_addr))
1085 return;
1087 /* Prepare for thread debugging. */
1088 current_inferior ()->push_target (&aix_thread_ops);
1089 data->pd_able = 1;
1091 /* If we're debugging a core file or an attached inferior, the
1092 pthread library may already have been initialized, so try to
1093 activate thread debugging. */
1094 pd_activate (inf->pid);
1097 /* Undo the effects of pd_enable(). */
1099 static void
1100 pd_disable (inferior *inf)
1102 struct aix_thread_variables *data;
1103 data = get_aix_thread_variables_data (inf);
1105 if (!data->pd_able)
1106 return;
1107 if (!data->pd_active)
1108 return;
1109 pthdb_session_destroy (data->pd_session);
1111 pid_to_prc (&inferior_ptid);
1112 data->pd_active = 0;
1113 data->pd_able = 0;
1114 current_inferior ()->unpush_target (&aix_thread_ops);
1117 /* new_objfile observer callback.
1119 Check whether a threaded application is being debugged, and if so, prepare
1120 for thread debugging. */
1122 static void
1123 new_objfile (struct objfile *objfile)
1125 pd_enable (current_inferior ());
1128 /* Attach to process specified by ARGS. */
1130 static void
1131 aix_thread_inferior_created (inferior *inf)
1133 pd_enable (inf);
1136 /* Detach from the process attached to by aix_thread_attach(). */
1138 void
1139 aix_thread_target::detach (inferior *inf, int from_tty)
1141 target_ops *beneath = this->beneath ();
1143 pd_disable (inf);
1144 beneath->detach (inf, from_tty);
1147 /* Tell the inferior process to continue running thread PID if != -1
1148 and all threads otherwise. */
1150 void
1151 aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1153 struct thread_info *thread;
1154 pthdb_tid_t tid[2];
1155 struct aix_thread_variables *data;
1157 data = get_thread_data_helper_for_ptid (ptid);
1159 if (ptid.tid () == 0)
1161 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1163 inferior_ptid = ptid_t (inferior_ptid.pid ());
1164 beneath ()->resume (ptid, step, sig);
1166 else
1168 thread = current_inferior ()->find_thread (ptid);
1169 if (!thread)
1170 error (_("aix-thread resume: unknown pthread %ld"),
1171 ptid.lwp ());
1173 aix_thread_info *priv = get_aix_thread_info (thread);
1175 tid[0] = priv->tid;
1176 if (tid[0] == PTHDB_INVALID_TID)
1177 error (_("aix-thread resume: no tid for pthread %ld"),
1178 ptid.lwp ());
1179 tid[1] = 0;
1181 if (data->arch64)
1182 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1183 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1184 else
1185 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1186 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1190 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1191 If an error occurs, return -1, else return the pid of the stopped
1192 thread. */
1194 ptid_t
1195 aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1196 target_wait_flags options)
1198 struct aix_thread_variables *data;
1200 pid_to_prc (&ptid);
1202 ptid = beneath ()->wait (ptid, status, options);
1205 if (ptid.pid () == -1)
1206 return ptid_t (-1);
1208 /* The target beneath does not deal with threads, so it should only return
1209 pid-only ptids. */
1210 gdb_assert (ptid.is_pid ());
1212 data = get_thread_data_helper_for_ptid (ptid);
1214 /* Check whether libpthdebug might be ready to be initialized. */
1215 if (!data->pd_active && status->kind () == TARGET_WAITKIND_STOPPED
1216 && status->sig () == GDB_SIGNAL_TRAP)
1218 process_stratum_target *proc_target
1219 = current_inferior ()->process_target ();
1220 struct regcache *regcache = get_thread_regcache (proc_target, ptid);
1221 struct gdbarch *gdbarch = regcache->arch ();
1223 if (regcache_read_pc (regcache)
1224 - gdbarch_decr_pc_after_break (gdbarch) == data->pd_brk_addr)
1225 pd_activate (ptid.pid ());
1228 return pd_update (ptid.pid ());
1231 /* Supply AIX altivec registers, both 64 and 32 bit. */
1233 static void
1234 supply_altivec_regs (struct regcache *regcache, __vmx_context_t vmx)
1236 ppc_gdbarch_tdep *tdep
1237 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1238 int regno;
1239 for (regno = 0; regno < ppc_num_vrs; regno++)
1240 regcache->raw_supply (tdep->ppc_vr0_regnum + regno,
1241 &(vmx.__vr[regno]));
1242 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1243 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1246 /* Supply AIX VSX registers, both 64 and 32 bit. */
1248 static void
1249 supply_vsx_regs (struct regcache *regcache, __vsx_context_t vsx)
1251 ppc_gdbarch_tdep *tdep
1252 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1253 int regno;
1255 for (regno = 0; regno < ppc_num_vshrs; regno++)
1256 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + regno,
1257 &(vsx.__vsr_dw1[regno]));
1260 /* Record that the 64-bit general-purpose registers contain VALS. */
1262 static void
1263 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1265 ppc_gdbarch_tdep *tdep
1266 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1267 int regno;
1269 for (regno = 0; regno < ppc_num_gprs; regno++)
1270 regcache->raw_supply (tdep->ppc_gp0_regnum + regno,
1271 (char *) (vals + regno));
1274 /* Record that 32-bit register REGNO contains VAL. */
1276 static void
1277 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1279 regcache->raw_supply (regno, (char *) &val);
1282 /* Record that the floating-point registers contain VALS. */
1284 static void
1285 supply_fprs (struct regcache *regcache, double *vals)
1287 struct gdbarch *gdbarch = regcache->arch ();
1288 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1289 int regno;
1291 /* This function should never be called on architectures without
1292 floating-point registers. */
1293 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1295 for (regno = tdep->ppc_fp0_regnum;
1296 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1297 regno++)
1298 regcache->raw_supply (regno,
1299 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1302 /* Predicate to test whether given register number is a "special" register. */
1303 static int
1304 special_register_p (struct gdbarch *gdbarch, int regno)
1306 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1308 return regno == gdbarch_pc_regnum (gdbarch)
1309 || regno == tdep->ppc_ps_regnum
1310 || regno == tdep->ppc_cr_regnum
1311 || regno == tdep->ppc_lr_regnum
1312 || regno == tdep->ppc_ctr_regnum
1313 || regno == tdep->ppc_xer_regnum
1314 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1315 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1319 /* Record that the special registers contain the specified 64-bit and
1320 32-bit values. */
1322 static void
1323 supply_sprs64 (struct regcache *regcache,
1324 uint64_t iar, uint64_t msr, uint32_t cr,
1325 uint64_t lr, uint64_t ctr, uint32_t xer,
1326 uint32_t fpscr)
1328 struct gdbarch *gdbarch = regcache->arch ();
1329 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1331 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1332 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1333 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1334 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1335 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1336 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1337 if (tdep->ppc_fpscr_regnum >= 0)
1338 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1341 /* Record that the special registers contain the specified 32-bit
1342 values. */
1344 static void
1345 supply_sprs32 (struct regcache *regcache,
1346 uint32_t iar, uint32_t msr, uint32_t cr,
1347 uint32_t lr, uint32_t ctr, uint32_t xer,
1348 uint32_t fpscr)
1350 struct gdbarch *gdbarch = regcache->arch ();
1351 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1353 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1354 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1355 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1356 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1357 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1358 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1359 if (tdep->ppc_fpscr_regnum >= 0)
1360 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1363 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1364 thread.
1366 There's no way to query a single register from a non-kernel
1367 pthread, so there's no need for a single-register version of this
1368 function. */
1370 static void
1371 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1373 struct gdbarch *gdbarch = regcache->arch ();
1374 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1375 int status, i;
1376 pthdb_context_t ctx;
1377 struct aix_thread_variables *data;
1378 data = get_thread_data_helper_for_ptid (inferior_ptid);
1380 if (debug_aix_thread)
1381 gdb_printf (gdb_stdlog,
1382 "fetch_regs_user_thread %lx\n", (long) pdtid);
1383 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1384 if (status != PTHDB_SUCCESS)
1385 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1386 pd_status2str (status));
1388 /* General-purpose registers. */
1390 if (data->arch64)
1391 supply_gprs64 (regcache, ctx.gpr);
1392 else
1393 for (i = 0; i < ppc_num_gprs; i++)
1394 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1396 /* Floating-point registers. */
1398 if (ppc_floating_point_unit_p (gdbarch))
1399 supply_fprs (regcache, ctx.fpr);
1401 /* Special registers. */
1403 if (data->arch64)
1404 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1405 ctx.xer, ctx.fpscr);
1406 else
1407 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1408 ctx.xer, ctx.fpscr);
1410 /* Altivec registers. */
1411 supply_altivec_regs (regcache, ctx.vmx);
1413 /* VSX registers. */
1414 supply_vsx_regs (regcache, ctx.vsx);
1417 /* Fetch register REGNO if != -1 or all registers otherwise from
1418 kernel thread TID.
1420 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1421 SPRs, but there's no way to query individual registers within those
1422 groups. Therefore, if REGNO != -1, this function fetches an entire
1423 group.
1425 Unfortunately, kernel thread register queries often fail with
1426 EPERM, indicating that the thread is in kernel space. This breaks
1427 backtraces of threads other than the current one. To make that
1428 breakage obvious without throwing an error to top level (which is
1429 bad e.g. during "info threads" output), zero registers that can't
1430 be retrieved. */
1432 static void
1433 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1434 pthdb_tid_t tid)
1436 struct gdbarch *gdbarch = regcache->arch ();
1437 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1438 uint64_t gprs64[ppc_num_gprs];
1439 uint32_t gprs32[ppc_num_gprs];
1440 double fprs[ppc_num_fprs];
1441 struct ptxsprs sprs64;
1442 struct ptsprs sprs32;
1443 int i;
1444 struct aix_thread_variables *data;
1446 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1448 if (debug_aix_thread)
1449 gdb_printf (gdb_stdlog,
1450 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1451 (long) tid, regno, data->arch64);
1453 /* General-purpose registers. */
1454 if (regno == -1
1455 || (tdep->ppc_gp0_regnum <= regno
1456 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1458 if (data->arch64)
1460 if (!ptrace64aix (PTT_READ_GPRS, tid,
1461 (unsigned long) gprs64, 0, NULL))
1462 memset (gprs64, 0, sizeof (gprs64));
1463 supply_gprs64 (regcache, gprs64);
1465 else
1467 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1468 memset (gprs32, 0, sizeof (gprs32));
1469 for (i = 0; i < ppc_num_gprs; i++)
1470 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1474 /* vector registers. */
1475 if (tdep->ppc_vr0_regnum != -1)
1477 int ret = 0;
1478 __vmx_context_t vmx;
1479 if (data->arch64)
1480 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1481 else
1482 ret = ptrace32 (PTT_READ_VEC, tid, (uintptr_t) &vmx, 0, 0);
1483 if (ret < 0)
1484 memset(&vmx, 0, sizeof(__vmx_context_t));
1485 for (i = 0; i < ppc_num_vrs; i++)
1486 regcache->raw_supply (tdep->ppc_vr0_regnum + i, &(vmx.__vr[i]));
1487 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1488 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1491 /* vsx registers. */
1492 if (tdep->ppc_vsr0_upper_regnum != -1)
1494 __vsx_context_t vsx;
1495 int ret = 0;
1496 if (data->arch64)
1497 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1498 else
1499 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1500 if (ret < 0)
1501 memset(&vsx, 0, sizeof(__vsx_context_t));
1502 for (i = 0; i < ppc_num_vshrs; i++)
1503 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i, &(vsx.__vsr_dw1[i]));
1506 /* Floating-point registers. */
1508 if (ppc_floating_point_unit_p (gdbarch)
1509 && (regno == -1
1510 || (regno >= tdep->ppc_fp0_regnum
1511 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1513 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1514 memset (fprs, 0, sizeof (fprs));
1515 supply_fprs (regcache, fprs);
1518 /* Special-purpose registers. */
1520 if (regno == -1 || special_register_p (gdbarch, regno))
1522 if (data->arch64)
1524 if (!ptrace64aix (PTT_READ_SPRS, tid,
1525 (unsigned long) &sprs64, 0, NULL))
1526 memset (&sprs64, 0, sizeof (sprs64));
1527 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1528 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1529 sprs64.pt_xer, sprs64.pt_fpscr);
1531 else
1533 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1534 memset (&sprs32, 0, sizeof (sprs32));
1535 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1536 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1537 sprs32.pt_fpscr);
1539 if (tdep->ppc_mq_regnum >= 0)
1540 regcache->raw_supply (tdep->ppc_mq_regnum, (char *) &sprs32.pt_mq);
1545 /* Fetch register REGNO if != -1 or all registers otherwise from the
1546 thread/process connected to REGCACHE. */
1548 void
1549 aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
1551 struct thread_info *thread;
1552 pthdb_tid_t tid;
1554 /* If a new inferior is born, then its pthread debug library is yet to
1555 initialised and hence has no private data. So the below if condition
1556 exists. */
1558 if (regcache->ptid ().tid () == 0)
1559 beneath ()->fetch_registers (regcache, regno);
1560 else
1562 thread = current_inferior ()->find_thread (regcache->ptid ());
1563 aix_thread_info *priv = get_aix_thread_info (thread);
1564 tid = priv->tid;
1566 if (tid == PTHDB_INVALID_TID)
1567 fetch_regs_user_thread (regcache, priv->pdtid);
1568 else
1569 fetch_regs_kernel_thread (regcache, regno, tid);
1573 /* Fill altivec registers. */
1575 static void
1576 fill_altivec (const struct regcache *regcache, __vmx_context_t *vmx)
1578 struct gdbarch *gdbarch = regcache->arch ();
1579 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1580 int regno;
1582 for (regno = 0; regno < ppc_num_vrs; regno++)
1583 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + regno))
1584 regcache->raw_collect (tdep->ppc_vr0_regnum + regno,
1585 &(vmx->__vr[regno]));
1587 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1588 regcache->raw_collect (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
1589 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1590 regcache->raw_collect (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
1593 /* Fill vsx registers. */
1595 static void
1596 fill_vsx (const struct regcache *regcache, __vsx_context_t *vsx)
1598 struct gdbarch *gdbarch = regcache->arch ();
1599 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1600 int regno;
1602 for (regno = 0; regno < ppc_num_vshrs; regno++)
1603 if (REG_VALID == regcache->get_register_status ( tdep->ppc_vsr0_upper_regnum + regno))
1604 regcache->raw_collect (tdep->ppc_vsr0_upper_regnum + regno,
1605 &(vsx->__vsr_dw1[0]) + regno);
1608 /* Store the gp registers into an array of uint32_t or uint64_t. */
1610 static void
1611 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1613 ppc_gdbarch_tdep *tdep
1614 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1615 int regno;
1617 for (regno = 0; regno < ppc_num_gprs; regno++)
1618 if (REG_VALID == regcache->get_register_status
1619 (tdep->ppc_gp0_regnum + regno))
1620 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1623 static void
1624 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1626 ppc_gdbarch_tdep *tdep
1627 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1628 int regno;
1630 for (regno = 0; regno < ppc_num_gprs; regno++)
1631 if (REG_VALID == regcache->get_register_status
1632 (tdep->ppc_gp0_regnum + regno))
1633 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1636 /* Store the floating point registers into a double array. */
1637 static void
1638 fill_fprs (const struct regcache *regcache, double *vals)
1640 struct gdbarch *gdbarch = regcache->arch ();
1641 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1642 int regno;
1644 /* This function should never be called on architectures without
1645 floating-point registers. */
1646 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1648 for (regno = tdep->ppc_fp0_regnum;
1649 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1650 regno++)
1651 if (REG_VALID == regcache->get_register_status (regno))
1652 regcache->raw_collect (regno, vals + regno - tdep->ppc_fp0_regnum);
1655 /* Store the special registers into the specified 64-bit and 32-bit
1656 locations. */
1658 static void
1659 fill_sprs64 (const struct regcache *regcache,
1660 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1661 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1662 uint32_t *fpscr)
1664 struct gdbarch *gdbarch = regcache->arch ();
1665 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1667 /* Verify that the size of the size of the IAR buffer is the
1668 same as the raw size of the PC (in the register cache). If
1669 they're not, then either GDB has been built incorrectly, or
1670 there's some other kind of internal error. To be really safe,
1671 we should check all of the sizes. */
1672 gdb_assert (sizeof (*iar) == register_size
1673 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1675 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1676 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1677 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1678 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1679 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1680 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1681 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1682 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1683 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1684 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1685 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1686 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1687 if (tdep->ppc_fpscr_regnum >= 0
1688 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1689 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1692 static void
1693 fill_sprs32 (const struct regcache *regcache,
1694 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1695 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1696 uint32_t *fpscr)
1698 struct gdbarch *gdbarch = regcache->arch ();
1699 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1701 /* Verify that the size of the size of the IAR buffer is the
1702 same as the raw size of the PC (in the register cache). If
1703 they're not, then either GDB has been built incorrectly, or
1704 there's some other kind of internal error. To be really safe,
1705 we should check all of the sizes. */
1706 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1707 gdbarch_pc_regnum (gdbarch)));
1709 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1710 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1711 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1712 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1713 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1714 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1715 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1716 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1717 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1718 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1719 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1720 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1721 if (tdep->ppc_fpscr_regnum >= 0
1722 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1723 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1726 /* Store all registers into pthread PDTID, which doesn't have a kernel
1727 thread.
1729 It's possible to store a single register into a non-kernel pthread,
1730 but I doubt it's worth the effort. */
1732 static void
1733 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1735 struct gdbarch *gdbarch = regcache->arch ();
1736 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1737 int status, i;
1738 pthdb_context_t ctx;
1739 uint32_t int32;
1740 uint64_t int64;
1741 struct aix_thread_variables *data;
1742 data = get_thread_data_helper_for_ptid (inferior_ptid);
1743 __vmx_context_t vmx;
1744 __vsx_context_t vsx;
1746 if (debug_aix_thread)
1747 gdb_printf (gdb_stdlog,
1748 "store_regs_user_thread %lx\n", (long) pdtid);
1750 /* Retrieve the thread's current context for its non-register
1751 values. */
1752 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1753 if (status != PTHDB_SUCCESS)
1754 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1755 pd_status2str (status));
1757 /* Fill altivec-registers. */
1759 if (__power_vmx())
1761 memset(&vmx, 0, sizeof(__vmx_context_t));
1762 for (i = 0; i < ppc_num_vrs; i++)
1763 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + i))
1765 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1766 &(vmx.__vr[i]));
1767 ctx.vmx.__vr[i] = vmx.__vr[i];
1769 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1770 ctx.vmx.__vrsave = vmx.__vrsave;
1771 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1772 ctx.vmx.__vscr = vmx.__vscr;
1775 /* Fill vsx registers. */
1777 if (__power_vsx())
1779 memset(&vsx, 0, sizeof(__vsx_context_t));
1780 for (i = 0; i < ppc_num_vshrs; i++)
1781 if (REG_VALID == regcache->get_register_status (tdep->ppc_vsr0_regnum + i))
1783 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1784 &(vsx.__vsr_dw1[i]));
1785 ctx.vsx.__vsr_dw1[i] = vsx.__vsr_dw1[i];
1789 /* Collect general-purpose register values from the regcache. */
1791 for (i = 0; i < ppc_num_gprs; i++)
1792 if (REG_VALID == regcache->get_register_status (tdep->ppc_gp0_regnum + i))
1794 if (data->arch64)
1796 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int64);
1797 ctx.gpr[i] = int64;
1799 else
1801 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int32);
1802 ctx.gpr[i] = int32;
1806 /* Collect floating-point register values from the regcache. */
1807 if (ppc_floating_point_unit_p (gdbarch))
1808 fill_fprs (regcache, ctx.fpr);
1810 /* Special registers (always kept in ctx as 64 bits). */
1811 if (data->arch64)
1813 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1814 &ctx.xer, &ctx.fpscr);
1816 else
1818 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1819 Solution: use 32-bit temp variables. */
1820 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1821 tmp_fpscr;
1823 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1824 &tmp_xer, &tmp_fpscr);
1825 if (REG_VALID == regcache->get_register_status
1826 (gdbarch_pc_regnum (gdbarch)))
1827 ctx.iar = tmp_iar;
1828 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1829 ctx.msr = tmp_msr;
1830 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1831 ctx.cr = tmp_cr;
1832 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1833 ctx.lr = tmp_lr;
1834 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1835 ctx.ctr = tmp_ctr;
1836 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1837 ctx.xer = tmp_xer;
1838 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1839 ctx.fpscr = tmp_fpscr;
1842 status = pthdb_pthread_setcontext (data->pd_session, pdtid, &ctx);
1843 if (status != PTHDB_SUCCESS)
1844 error (_("aix-thread: store_registers: "
1845 "pthdb_pthread_setcontext returned %s"),
1846 pd_status2str (status));
1849 /* Store register REGNO if != -1 or all registers otherwise into
1850 kernel thread TID.
1852 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1853 SPRs, but there's no way to set individual registers within those
1854 groups. Therefore, if REGNO != -1, this function stores an entire
1855 group. */
1857 static void
1858 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1859 pthdb_tid_t tid)
1861 struct gdbarch *gdbarch = regcache->arch ();
1862 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1863 uint64_t gprs64[ppc_num_gprs];
1864 uint32_t gprs32[ppc_num_gprs];
1865 double fprs[ppc_num_fprs];
1866 struct ptxsprs sprs64;
1867 struct ptsprs sprs32;
1868 struct aix_thread_variables *data;
1869 int ret = 0;
1871 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1873 if (debug_aix_thread)
1874 gdb_printf (gdb_stdlog,
1875 "store_regs_kernel_thread tid=%lx regno=%d\n",
1876 (long) tid, regno);
1878 /* General-purpose registers. */
1879 if (regno == -1
1880 || (tdep->ppc_gp0_regnum <= regno
1881 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1883 if (data->arch64)
1885 /* Pre-fetch: some regs may not be in the cache. */
1886 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1887 fill_gprs64 (regcache, gprs64);
1888 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1890 else
1892 /* Pre-fetch: some regs may not be in the cache. */
1893 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1894 fill_gprs32 (regcache, gprs32);
1895 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1899 /* Floating-point registers. */
1901 if (ppc_floating_point_unit_p (gdbarch)
1902 && (regno == -1
1903 || (regno >= tdep->ppc_fp0_regnum
1904 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1906 /* Pre-fetch: some regs may not be in the cache. */
1907 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1908 fill_fprs (regcache, fprs);
1909 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1912 /* Special-purpose registers. */
1914 if (regno == -1 || special_register_p (gdbarch, regno))
1916 if (data->arch64)
1918 /* Pre-fetch: some registers won't be in the cache. */
1919 ptrace64aix (PTT_READ_SPRS, tid,
1920 (unsigned long) &sprs64, 0, NULL);
1921 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1922 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1923 &sprs64.pt_xer, &sprs64.pt_fpscr);
1924 ptrace64aix (PTT_WRITE_SPRS, tid,
1925 (unsigned long) &sprs64, 0, NULL);
1927 else
1929 /* The contents of "struct ptspr" were declared as "unsigned
1930 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1931 Use temporaries to work around this problem. Also, add an
1932 assert here to make sure we fail if the system header files
1933 use "unsigned long", and the size of that type is not what
1934 the headers expect. */
1935 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1936 tmp_fpscr;
1938 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1940 /* Pre-fetch: some registers won't be in the cache. */
1941 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1943 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1944 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1946 sprs32.pt_iar = tmp_iar;
1947 sprs32.pt_msr = tmp_msr;
1948 sprs32.pt_cr = tmp_cr;
1949 sprs32.pt_lr = tmp_lr;
1950 sprs32.pt_ctr = tmp_ctr;
1951 sprs32.pt_xer = tmp_xer;
1952 sprs32.pt_fpscr = tmp_fpscr;
1954 if (tdep->ppc_mq_regnum >= 0)
1955 if (REG_VALID == regcache->get_register_status
1956 (tdep->ppc_mq_regnum))
1957 regcache->raw_collect (tdep->ppc_mq_regnum, &sprs32.pt_mq);
1959 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1963 /* Vector registers. */
1964 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1
1965 && (regno == -1 || (regno >= tdep->ppc_vr0_regnum
1966 && regno <= tdep->ppc_vrsave_regnum)))
1968 __vmx_context_t vmx;
1969 if (__power_vmx())
1971 if (data->arch64)
1972 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1973 else
1974 ret = ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1975 if (ret > 0)
1977 fill_altivec(regcache, &vmx);
1978 if (data->arch64)
1979 ret = ptrace64aix (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1980 else
1981 ret = ptrace32 (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1982 if (ret < 0)
1983 perror_with_name (_("Unable to store AltiVec register after read"));
1988 /* VSX registers. */
1989 if (tdep->ppc_vsr0_upper_regnum != -1 && (regno == -1
1990 || (regno >=tdep->ppc_vsr0_upper_regnum
1991 && regno < tdep->ppc_vsr0_upper_regnum + ppc_num_vshrs)))
1993 __vsx_context_t vsx;
1994 if (__power_vsx())
1996 if (data->arch64)
1997 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1998 else
1999 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
2000 if (ret > 0)
2002 fill_vsx (regcache, &vsx);
2003 if (data->arch64)
2004 ret = ptrace64aix (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
2005 else
2006 ret = ptrace32 (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
2007 if (ret < 0)
2008 perror_with_name (_("Unable to store VSX register after read"));
2014 /* Store gdb's current view of the register set into the
2015 thread/process connected to REGCACHE. */
2017 void
2018 aix_thread_target::store_registers (struct regcache *regcache, int regno)
2020 struct thread_info *thread;
2021 pthdb_tid_t tid;
2023 if (regcache->ptid ().tid () == 0)
2024 beneath ()->store_registers (regcache, regno);
2025 else
2027 thread = current_inferior ()->find_thread (regcache->ptid ());
2028 aix_thread_info *priv = get_aix_thread_info (thread);
2029 tid = priv->tid;
2031 if (tid == PTHDB_INVALID_TID)
2032 store_regs_user_thread (regcache, priv->pdtid);
2033 else
2034 store_regs_kernel_thread (regcache, regno, tid);
2038 /* Implement the to_xfer_partial target_ops method. */
2040 enum target_xfer_status
2041 aix_thread_target::xfer_partial (enum target_object object,
2042 const char *annex, gdb_byte *readbuf,
2043 const gdb_byte *writebuf,
2044 ULONGEST offset, ULONGEST len,
2045 ULONGEST *xfered_len)
2047 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
2049 inferior_ptid = ptid_t (inferior_ptid.pid ());
2050 return beneath ()->xfer_partial (object, annex, readbuf,
2051 writebuf, offset, len, xfered_len);
2054 /* Clean up after the inferior exits. */
2056 void
2057 aix_thread_target::mourn_inferior ()
2059 target_ops *beneath = this->beneath ();
2061 pd_disable (current_inferior ());
2062 beneath->mourn_inferior ();
2065 /* Return whether thread PID is still valid. */
2067 bool
2068 aix_thread_target::thread_alive (ptid_t ptid)
2070 if (ptid.tid () == 0)
2071 return beneath ()->thread_alive (ptid);
2073 /* We update the thread list every time the child stops, so all
2074 valid threads should be in the thread list. */
2075 process_stratum_target *proc_target
2076 = current_inferior ()->process_target ();
2077 return in_thread_list (proc_target, ptid);
2080 /* Return a printable representation of composite PID for use in
2081 "info threads" output. */
2083 std::string
2084 aix_thread_target::pid_to_str (ptid_t ptid)
2086 if (ptid.tid () == 0)
2087 return beneath ()->pid_to_str (ptid);
2089 return string_printf (_("Thread %s"), pulongest (ptid.tid ()));
2092 /* Return a printable representation of extra information about
2093 THREAD, for use in "info threads" output. */
2095 const char *
2096 aix_thread_target::extra_thread_info (struct thread_info *thread)
2098 int status;
2099 pthdb_pthread_t pdtid;
2100 pthdb_tid_t tid;
2101 pthdb_state_t state;
2102 pthdb_suspendstate_t suspendstate;
2103 pthdb_detachstate_t detachstate;
2104 int cancelpend;
2105 static char *ret = NULL;
2106 struct aix_thread_variables *data;
2108 data = get_thread_data_helper_for_ptid (thread->ptid);
2110 if (thread->ptid.tid () == 0)
2111 return NULL;
2113 string_file buf;
2114 aix_thread_info *priv = get_aix_thread_info (thread);
2116 pdtid = priv->pdtid;
2117 tid = priv->tid;
2119 if (tid != PTHDB_INVALID_TID)
2120 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
2121 buf.printf (_("tid %d"), (int)tid);
2123 status = pthdb_pthread_state (data->pd_session, pdtid, &state);
2124 if (status != PTHDB_SUCCESS)
2125 state = PST_NOTSUP;
2126 buf.printf (", %s", state2str (state));
2128 status = pthdb_pthread_suspendstate (data->pd_session, pdtid,
2129 &suspendstate);
2130 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
2131 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
2132 buf.printf (_(", suspended"));
2134 status = pthdb_pthread_detachstate (data->pd_session, pdtid,
2135 &detachstate);
2136 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
2137 /* i18n: Like "Thread-Id %d, [state] running, detached" */
2138 buf.printf (_(", detached"));
2140 pthdb_pthread_cancelpend (data->pd_session, pdtid, &cancelpend);
2141 if (status == PTHDB_SUCCESS && cancelpend)
2142 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
2143 buf.printf (_(", cancel pending"));
2145 buf.write ("", 1);
2147 xfree (ret); /* Free old buffer. */
2149 ret = xstrdup (buf.c_str ());
2151 return ret;
2154 ptid_t
2155 aix_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
2157 return ptid_t (inferior_ptid.pid (), 0, thread);
2161 /* Module startup initialization function, automagically called by
2162 init.c. */
2164 void _initialize_aix_thread ();
2165 void
2166 _initialize_aix_thread ()
2168 /* Notice when object files get loaded and unloaded. */
2169 gdb::observers::new_objfile.attach (new_objfile, "aix-thread");
2171 /* Add ourselves to inferior_created event chain.
2172 This is needed to enable the thread target on "attach". */
2173 gdb::observers::inferior_created.attach (aix_thread_inferior_created,
2174 "aix-thread");
2176 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
2177 _("Set debugging of AIX thread module."),
2178 _("Show debugging of AIX thread module."),
2179 _("Enables debugging output (used to debug GDB)."),
2180 NULL, NULL,
2181 /* FIXME: i18n: Debugging of AIX thread
2182 module is \"%d\". */
2183 &setdebuglist, &showdebuglist);