]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/aix-thread.c
gdb: remove target_gdbarch
[thirdparty/binutils-gdb.git] / gdb / aix-thread.c
1 /* Low level interface for debugging AIX 4.3+ pthreads.
2
3 Copyright (C) 1999-2023 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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
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.
35
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().
39
40 */
41
42 #include "defs.h"
43 #include "gdbthread.h"
44 #include "target.h"
45 #include "inferior.h"
46 #include "regcache.h"
47 #include "gdbcmd.h"
48 #include "ppc-tdep.h"
49 #include "observable.h"
50 #include "objfiles.h"
51
52 #include <procinfo.h>
53 #include <sys/types.h>
54 #include <sys/ptrace.h>
55 #include <sys/reg.h>
56 #include <sched.h>
57 #include <sys/pthdebug.h>
58
59 #if !HAVE_DECL_GETTHRDS
60 extern int getthrds (pid_t, struct thrdsinfo64 *, int, tid_t *, int);
61 #endif
62
63 /* Whether to emit debugging output. */
64 static bool debug_aix_thread;
65
66 /* In AIX 5.1, functions use pthdb_tid_t instead of tid_t. */
67 #ifndef PTHDB_VERSION_3
68 #define pthdb_tid_t tid_t
69 #endif
70
71 /* Success and failure values returned by pthdb callbacks. */
72
73 #define PDC_SUCCESS PTHDB_SUCCESS
74 #define PDC_FAILURE PTHDB_CALLBACK
75
76 /* Private data attached to each element in GDB's thread list. */
77
78 struct aix_thread_info : public private_thread_info
79 {
80 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
81 pthdb_tid_t tid; /* kernel thread id */
82 };
83
84 /* Return the aix_thread_info attached to THREAD. */
85
86 static aix_thread_info *
87 get_aix_thread_info (thread_info *thread)
88 {
89 return gdb::checked_static_cast<aix_thread_info *> (thread->priv.get ());
90 }
91
92 /* Information about a thread of which libpthdebug is aware. */
93
94 struct pd_thread {
95 pthdb_pthread_t pdtid;
96 pthread_t pthid;
97 pthdb_tid_t tid;
98 };
99
100 /* This module's target-specific operations, active while pd_able is true. */
101
102 static const target_info aix_thread_target_info = {
103 "aix-threads",
104 N_("AIX pthread support"),
105 N_("AIX pthread support")
106 };
107
108 class aix_thread_target final : public target_ops
109 {
110 public:
111 const target_info &info () const override
112 { return aix_thread_target_info; }
113
114 strata stratum () const override { return thread_stratum; }
115
116 void detach (inferior *, int) override;
117 void resume (ptid_t, int, enum gdb_signal) override;
118 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
119
120 void fetch_registers (struct regcache *, int) override;
121 void store_registers (struct regcache *, int) override;
122
123 enum target_xfer_status xfer_partial (enum target_object object,
124 const char *annex,
125 gdb_byte *readbuf,
126 const gdb_byte *writebuf,
127 ULONGEST offset, ULONGEST len,
128 ULONGEST *xfered_len) override;
129
130 void mourn_inferior () override;
131
132 bool thread_alive (ptid_t ptid) override;
133
134 std::string pid_to_str (ptid_t) override;
135
136 const char *extra_thread_info (struct thread_info *) override;
137
138 ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
139 };
140
141 static aix_thread_target aix_thread_ops;
142
143 /* Forward declarations for pthdb callbacks. */
144
145 static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
146 static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
147 static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
148 static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
149 unsigned long long flags,
150 pthdb_context_t *context);
151 static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
152 unsigned long long flags,
153 pthdb_context_t *context);
154 static int pdc_alloc (pthdb_user_t, size_t, void **);
155 static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
156 static int pdc_dealloc (pthdb_user_t, void *);
157
158 /* pthdb callbacks. */
159
160 static pthdb_callbacks_t pd_callbacks = {
161 pdc_symbol_addrs,
162 pdc_read_data,
163 pdc_write_data,
164 pdc_read_regs,
165 pdc_write_regs,
166 pdc_alloc,
167 pdc_realloc,
168 pdc_dealloc,
169 NULL
170 };
171
172 /* Aix variable structure. */
173 struct aix_thread_variables
174 {
175 /* Whether the current application is debuggable by pthdb. */
176 int pd_able;
177
178 /* Whether a threaded application is being debugged. */
179 int pd_active;
180
181 /* Current pthdb session. */
182 pthdb_session_t pd_session;
183
184 /* Address of the function that libpthread will call when libpthdebug
185 is ready to be initialized. */
186 CORE_ADDR pd_brk_addr;
187
188 /* Whether the current architecture is 64-bit.
189 Only valid when pd_able is true. */
190 int arch64;
191 };
192
193 /* Key to our per-inferior data. */
194 static const registry<inferior>::key<aix_thread_variables>
195 aix_thread_variables_handle;
196
197 /* Function to Get aix_thread_variables data. */
198 static struct aix_thread_variables*
199 get_aix_thread_variables_data (struct inferior *inf)
200 {
201 if (inf == NULL)
202 return NULL;
203
204 struct aix_thread_variables* data;
205
206 data = aix_thread_variables_handle.get (inf);
207 if (data == NULL)
208 data = aix_thread_variables_handle.emplace (inf);
209
210 return data;
211 }
212
213 /* Helper to get data for ptid in a function. */
214
215 static struct aix_thread_variables*
216 get_thread_data_helper_for_ptid (ptid_t ptid)
217 {
218 inferior *inf = find_inferior_ptid (current_inferior ()->process_target (),
219 ptid);
220 return get_aix_thread_variables_data (inf);
221 }
222
223 /* Helper to get data for pid in a function. */
224
225 static struct aix_thread_variables*
226 get_thread_data_helper_for_pid (pid_t pid)
227 {
228 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
229 pid);
230 return get_aix_thread_variables_data (inf);
231 }
232
233 /* Return a printable representation of pthdebug function return
234 STATUS. */
235
236 static const char *
237 pd_status2str (int status)
238 {
239 switch (status)
240 {
241 case PTHDB_SUCCESS: return "SUCCESS";
242 case PTHDB_NOSYS: return "NOSYS";
243 case PTHDB_NOTSUP: return "NOTSUP";
244 case PTHDB_BAD_VERSION: return "BAD_VERSION";
245 case PTHDB_BAD_USER: return "BAD_USER";
246 case PTHDB_BAD_SESSION: return "BAD_SESSION";
247 case PTHDB_BAD_MODE: return "BAD_MODE";
248 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
249 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
250 case PTHDB_BAD_POINTER: return "BAD_POINTER";
251 case PTHDB_BAD_CMD: return "BAD_CMD";
252 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
253 case PTHDB_BAD_ATTR: return "BAD_ATTR";
254 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
255 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
256 case PTHDB_BAD_COND: return "BAD_COND";
257 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
258 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
259 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
260 case PTHDB_BAD_KEY: return "BAD_KEY";
261 case PTHDB_BAD_PTID: return "BAD_PTID";
262 case PTHDB_BAD_TID: return "BAD_TID";
263 case PTHDB_CALLBACK: return "CALLBACK";
264 case PTHDB_CONTEXT: return "CONTEXT";
265 case PTHDB_HELD: return "HELD";
266 case PTHDB_NOT_HELD: return "NOT_HELD";
267 case PTHDB_MEMORY: return "MEMORY";
268 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
269 case PTHDB_SYMBOL: return "SYMBOL";
270 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
271 case PTHDB_INTERNAL: return "INTERNAL";
272 default: return "UNKNOWN";
273 }
274 }
275
276 /* A call to ptrace(REQ, ID, ...) just returned RET. Check for
277 exceptional conditions and either return nonlocally or else return
278 1 for success and 0 for failure. */
279
280 static int
281 ptrace_check (int req, int id, int ret)
282 {
283 if (ret == 0 && !errno)
284 return 1;
285
286 /* According to ptrace(2), ptrace may fail with EPERM if "the
287 Identifier parameter corresponds to a kernel thread which is
288 stopped in kernel mode and whose computational state cannot be
289 read or written." This happens quite often with register reads. */
290
291 switch (req)
292 {
293 case PTT_READ_GPRS:
294 case PTT_READ_FPRS:
295 case PTT_READ_SPRS:
296 if (ret == -1 && errno == EPERM)
297 {
298 if (debug_aix_thread)
299 gdb_printf (gdb_stdlog,
300 "ptrace (%d, %d) = %d (errno = %d)\n",
301 req, id, ret, errno);
302 return ret == -1 ? 0 : 1;
303 }
304 break;
305 case PTT_READ_VEC:
306 case PTT_READ_VSX:
307 if (debug_aix_thread)
308 gdb_printf (gdb_stdlog,
309 "ptrace (%d, %d) = %d (errno = %d)\n",
310 req, id, ret, errno);
311 if (ret == -1)
312 return -1;
313 break;
314 }
315 error (_("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)"),
316 req, id, ret, errno, safe_strerror (errno));
317 return 0; /* Not reached. */
318 }
319
320 /* Call ptracex (REQ, ID, ADDR, DATA, BUF) or
321 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
322 Return success. */
323
324 #ifdef HAVE_PTRACE64
325 # define ptracex(request, pid, addr, data, buf) \
326 ptrace64 (request, pid, addr, data, buf)
327 #endif
328
329 static int
330 ptrace64aix (int req, int id, long long addr, int data, int *buf)
331 {
332 errno = 0;
333 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
334 }
335
336 /* Call ptrace (REQ, ID, ADDR, DATA, BUF) or
337 ptrace64 (REQ, ID, ADDR, DATA, BUF) if HAVE_PTRACE64.
338 Return success. */
339
340 #ifdef HAVE_PTRACE64
341 # define ptrace(request, pid, addr, data, buf) \
342 ptrace64 (request, pid, addr, data, buf)
343 # define addr_ptr long long
344 #else
345 # define addr_ptr int *
346 #endif
347
348 static int
349 ptrace32 (int req, int id, addr_ptr addr, int data, int *buf)
350 {
351 errno = 0;
352 return ptrace_check (req, id,
353 ptrace (req, id, addr, data, buf));
354 }
355
356 /* If *PIDP is a composite process/thread id, convert it to a
357 process id. */
358
359 static void
360 pid_to_prc (ptid_t *ptidp)
361 {
362 ptid_t ptid;
363
364 ptid = *ptidp;
365 if (ptid.tid () != 0)
366 *ptidp = ptid_t (ptid.pid ());
367 }
368
369 /* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to
370 the address of SYMBOLS[<i>].name. */
371
372 static int
373 pdc_symbol_addrs (pthdb_user_t user_current_pid, pthdb_symbol_t *symbols, int count)
374 {
375 struct bound_minimal_symbol ms;
376 int i;
377 char *name;
378
379 if (debug_aix_thread)
380 gdb_printf (gdb_stdlog,
381 "pdc_symbol_addrs (user_current_pid = %ld, symbols = 0x%lx, count = %d)\n",
382 user_current_pid, (long) symbols, count);
383
384 for (i = 0; i < count; i++)
385 {
386 name = symbols[i].name;
387 if (debug_aix_thread)
388 gdb_printf (gdb_stdlog,
389 " symbols[%d].name = \"%s\"\n", i, name);
390
391 if (!*name)
392 symbols[i].addr = 0;
393 else
394 {
395 ms = lookup_minimal_symbol (name, NULL, NULL);
396 if (ms.minsym == NULL)
397 {
398 if (debug_aix_thread)
399 gdb_printf (gdb_stdlog, " returning PDC_FAILURE\n");
400 return PDC_FAILURE;
401 }
402 symbols[i].addr = ms.value_address ();
403 }
404 if (debug_aix_thread)
405 gdb_printf (gdb_stdlog, " symbols[%d].addr = %s\n",
406 i, hex_string (symbols[i].addr));
407 }
408 if (debug_aix_thread)
409 gdb_printf (gdb_stdlog, " returning PDC_SUCCESS\n");
410 return PDC_SUCCESS;
411 }
412
413 /* Read registers call back function should be able to read the
414 context information of a debuggee kernel thread from an active
415 process or from a core file. The information should be formatted
416 in context64 form for both 32-bit and 64-bit process.
417 If successful return 0, else non-zero is returned. */
418
419 static int
420 pdc_read_regs (pthdb_user_t user_current_pid,
421 pthdb_tid_t tid,
422 unsigned long long flags,
423 pthdb_context_t *context)
424 {
425 /* This function doesn't appear to be used, so we could probably
426 just return 0 here. HOWEVER, if it is not defined, the OS will
427 complain and several thread debug functions will fail. In case
428 this is needed, I have implemented what I think it should do,
429 however this code is untested. */
430
431 uint64_t gprs64[ppc_num_gprs];
432 uint32_t gprs32[ppc_num_gprs];
433 double fprs[ppc_num_fprs];
434 struct ptxsprs sprs64;
435 struct ptsprs sprs32;
436 struct aix_thread_variables *data;
437
438 data = get_thread_data_helper_for_pid (user_current_pid);
439
440 if (debug_aix_thread)
441 gdb_printf (gdb_stdlog, "pdc_read_regs tid=%d flags=%s\n",
442 (int) tid, hex_string (flags));
443
444 /* General-purpose registers. */
445 if (flags & PTHDB_FLAG_GPRS)
446 {
447 if (data->arch64)
448 {
449 if (!ptrace64aix (PTT_READ_GPRS, tid,
450 (unsigned long) gprs64, 0, NULL))
451 memset (gprs64, 0, sizeof (gprs64));
452 memcpy (context->gpr, gprs64, sizeof(gprs64));
453 }
454 else
455 {
456 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
457 memset (gprs32, 0, sizeof (gprs32));
458 memcpy (context->gpr, gprs32, sizeof(gprs32));
459 }
460 }
461
462 /* Floating-point registers. */
463 if (flags & PTHDB_FLAG_FPRS)
464 {
465 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
466 memset (fprs, 0, sizeof (fprs));
467 memcpy (context->fpr, fprs, sizeof(fprs));
468 }
469
470 /* Special-purpose registers. */
471 if (flags & PTHDB_FLAG_SPRS)
472 {
473 if (data->arch64)
474 {
475 if (!ptrace64aix (PTT_READ_SPRS, tid,
476 (unsigned long) &sprs64, 0, NULL))
477 memset (&sprs64, 0, sizeof (sprs64));
478 memcpy (&context->msr, &sprs64, sizeof(sprs64));
479 }
480 else
481 {
482 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
483 memset (&sprs32, 0, sizeof (sprs32));
484 memcpy (&context->msr, &sprs32, sizeof(sprs32));
485 }
486 }
487
488 /* vector registers. */
489 __vmx_context_t vmx;
490 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
491 {
492 if (data->arch64)
493 {
494 if (!ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
495 memset (&vmx, 0, sizeof (vmx));
496 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
497 }
498 else
499 {
500 if (!ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0))
501 memset (&vmx, 0, sizeof (vmx));
502 memcpy (&context->vmx, &vmx, sizeof(__vmx_context_t));
503 }
504 }
505
506 /* vsx registers. */
507 __vsx_context_t vsx;
508 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
509 {
510 if (data->arch64)
511 {
512 if (!ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
513 memset (&vsx, 0, sizeof (vsx));
514 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
515 }
516 else
517 {
518 if (!ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0))
519 memset (&vsx, 0, sizeof (vsx));
520 memcpy (&context->vsx, &vsx, sizeof(__vsx_context_t));
521 }
522 }
523 return 0;
524 }
525
526 /* Write register function should be able to write requested context
527 information to specified debuggee's kernel thread id.
528 If successful return 0, else non-zero is returned. */
529
530 static int
531 pdc_write_regs (pthdb_user_t user_current_pid,
532 pthdb_tid_t tid,
533 unsigned long long flags,
534 pthdb_context_t *context)
535 {
536 /* This function doesn't appear to be used, so we could probably
537 just return 0 here. HOWEVER, if it is not defined, the OS will
538 complain and several thread debug functions will fail. In case
539 this is needed, I have implemented what I think it should do,
540 however this code is untested. */
541
542 struct aix_thread_variables *data;
543
544 data = get_thread_data_helper_for_pid (user_current_pid);
545
546 if (debug_aix_thread)
547 gdb_printf (gdb_stdlog, "pdc_write_regs tid=%d flags=%s\n",
548 (int) tid, hex_string (flags));
549
550 /* General-purpose registers. */
551 if (flags & PTHDB_FLAG_GPRS)
552 {
553 if (data->arch64)
554 ptrace64aix (PTT_WRITE_GPRS, tid,
555 (unsigned long) context->gpr, 0, NULL);
556 else
557 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) context->gpr, 0, NULL);
558 }
559
560 /* Floating-point registers. */
561 if (flags & PTHDB_FLAG_FPRS)
562 {
563 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) context->fpr, 0, NULL);
564 }
565
566 /* Special-purpose registers. */
567 if (flags & PTHDB_FLAG_SPRS)
568 {
569 if (data->arch64)
570 {
571 ptrace64aix (PTT_WRITE_SPRS, tid,
572 (unsigned long) &context->msr, 0, NULL);
573 }
574 else
575 {
576 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &context->msr, 0, NULL);
577 }
578 }
579
580 /* vector registers. */
581 if (__power_vmx() && (flags & PTHDB_FLAG_REGS))
582 {
583 if (data->arch64)
584 ptrace64aix (PTT_WRITE_VEC, tid, (unsigned long) &context->vmx, 0, 0);
585 else
586 ptrace32 (PTT_WRITE_VEC, tid, (uintptr_t) &context->vmx, 0, 0);
587 }
588
589 /* vsx registers. */
590 if (__power_vsx() && (flags & PTHDB_FLAG_REGS))
591 {
592 if (data->arch64)
593 ptrace64aix (PTT_WRITE_VSX, tid, (unsigned long) &context->vsx, 0, 0);
594 else
595 ptrace32 (PTT_WRITE_VSX, tid, (uintptr_t) &context->vsx, 0, 0);
596 }
597 return 0;
598 }
599
600 /* pthdb callback: read LEN bytes from process ADDR into BUF. */
601
602 static int
603 pdc_read_data (pthdb_user_t user_current_pid, void *buf,
604 pthdb_addr_t addr, size_t len)
605 {
606 int status, ret;
607 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
608 user_current_pid);
609
610 if (debug_aix_thread)
611 gdb_printf (gdb_stdlog,
612 "pdc_read_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
613 user_current_pid, (long) buf, hex_string (addr), len);
614
615 /* This is needed to eliminate the dependency of current thread
616 which is null so that thread reads the correct target memory. */
617 {
618 scoped_restore_current_inferior_for_memory save_inferior (inf);
619 status = target_read_memory (addr, (gdb_byte *) buf, len);
620 }
621 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
622
623 if (debug_aix_thread)
624 gdb_printf (gdb_stdlog, " status=%d, returning %s\n",
625 status, pd_status2str (ret));
626 return ret;
627 }
628
629 /* pthdb callback: write LEN bytes from BUF to process ADDR. */
630
631 static int
632 pdc_write_data (pthdb_user_t user_current_pid, void *buf,
633 pthdb_addr_t addr, size_t len)
634 {
635 int status, ret;
636 inferior *inf = find_inferior_pid (current_inferior ()->process_target (),
637 user_current_pid);
638
639 if (debug_aix_thread)
640 gdb_printf (gdb_stdlog,
641 "pdc_write_data (user_current_pid = %ld, buf = 0x%lx, addr = %s, len = %ld)\n",
642 user_current_pid, (long) buf, hex_string (addr), len);
643
644 {
645 scoped_restore_current_inferior_for_memory save_inferior (inf);
646 status = target_write_memory (addr, (gdb_byte *) buf, len);
647 }
648
649 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
650
651 if (debug_aix_thread)
652 gdb_printf (gdb_stdlog, " status=%d, returning %s\n", status,
653 pd_status2str (ret));
654 return ret;
655 }
656
657 /* pthdb callback: allocate a LEN-byte buffer and store a pointer to it
658 in BUFP. */
659
660 static int
661 pdc_alloc (pthdb_user_t user_current_pid, size_t len, void **bufp)
662 {
663 if (debug_aix_thread)
664 gdb_printf (gdb_stdlog,
665 "pdc_alloc (user_current_pid = %ld, len = %ld, bufp = 0x%lx)\n",
666 user_current_pid, len, (long) bufp);
667 *bufp = xmalloc (len);
668 if (debug_aix_thread)
669 gdb_printf (gdb_stdlog,
670 " malloc returned 0x%lx\n", (long) *bufp);
671
672 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never
673 be returned. */
674
675 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
676 }
677
678 /* pthdb callback: reallocate BUF, which was allocated by the alloc or
679 realloc callback, so that it contains LEN bytes, and store a
680 pointer to the result in BUFP. */
681
682 static int
683 pdc_realloc (pthdb_user_t user_current_pid, void *buf, size_t len, void **bufp)
684 {
685 if (debug_aix_thread)
686 gdb_printf (gdb_stdlog,
687 "pdc_realloc (user_current_pid = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)\n",
688 user_current_pid, (long) buf, len, (long) bufp);
689 *bufp = xrealloc (buf, len);
690 if (debug_aix_thread)
691 gdb_printf (gdb_stdlog,
692 " realloc returned 0x%lx\n", (long) *bufp);
693 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
694 }
695
696 /* pthdb callback: free BUF, which was allocated by the alloc or
697 realloc callback. */
698
699 static int
700 pdc_dealloc (pthdb_user_t user_current_pid, void *buf)
701 {
702 if (debug_aix_thread)
703 gdb_printf (gdb_stdlog,
704 "pdc_free (user_current_pid = %ld, buf = 0x%lx)\n", user_current_pid,
705 (long) buf);
706 xfree (buf);
707 return PDC_SUCCESS;
708 }
709
710 /* Return a printable representation of pthread STATE. */
711
712 static char *
713 state2str (pthdb_state_t state)
714 {
715 switch (state)
716 {
717 case PST_IDLE:
718 /* i18n: Like "Thread-Id %d, [state] idle" */
719 return _("idle"); /* being created */
720 case PST_RUN:
721 /* i18n: Like "Thread-Id %d, [state] running" */
722 return _("running"); /* running */
723 case PST_SLEEP:
724 /* i18n: Like "Thread-Id %d, [state] sleeping" */
725 return _("sleeping"); /* awaiting an event */
726 case PST_READY:
727 /* i18n: Like "Thread-Id %d, [state] ready" */
728 return _("ready"); /* runnable */
729 case PST_TERM:
730 /* i18n: Like "Thread-Id %d, [state] finished" */
731 return _("finished"); /* awaiting a join/detach */
732 default:
733 /* i18n: Like "Thread-Id %d, [state] unknown" */
734 return _("unknown");
735 }
736 }
737
738 /* qsort() comparison function for sorting pd_thread structs by pthid. */
739
740 static int
741 pcmp (const void *p1v, const void *p2v)
742 {
743 struct pd_thread *p1 = (struct pd_thread *) p1v;
744 struct pd_thread *p2 = (struct pd_thread *) p2v;
745 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
746 }
747
748 /* ptid comparison function */
749
750 static int
751 ptid_cmp (ptid_t ptid1, ptid_t ptid2)
752 {
753 if (ptid1.pid () < ptid2.pid ())
754 return -1;
755 else if (ptid1.pid () > ptid2.pid ())
756 return 1;
757 else if (ptid1.tid () < ptid2.tid ())
758 return -1;
759 else if (ptid1.tid () > ptid2.tid ())
760 return 1;
761 else if (ptid1.lwp () < ptid2.lwp ())
762 return -1;
763 else if (ptid1.lwp () > ptid2.lwp ())
764 return 1;
765 else
766 return 0;
767 }
768
769 /* qsort() comparison function for sorting thread_info structs by pid. */
770
771 static int
772 gcmp (const void *t1v, const void *t2v)
773 {
774 struct thread_info *t1 = *(struct thread_info **) t1v;
775 struct thread_info *t2 = *(struct thread_info **) t2v;
776 return ptid_cmp (t1->ptid, t2->ptid);
777 }
778
779 /* Search through the list of all kernel threads for the thread
780 that has stopped on a SIGTRAP signal, and return its TID.
781 Return 0 if none found. */
782
783 static pthdb_tid_t
784 get_signaled_thread (int pid)
785 {
786 struct thrdsinfo64 thrinf;
787 tid_t ktid = 0;
788
789 while (1)
790 {
791 if (getthrds (pid, &thrinf,
792 sizeof (thrinf), &ktid, 1) != 1)
793 break;
794
795 /* We also need to keep in mind Trap and interrupt or any
796 signal that needs to be handled in pd_update (). */
797
798 if (thrinf.ti_cursig)
799 return thrinf.ti_tid;
800 }
801
802 /* Didn't find any thread stopped on a SIGTRAP signal. */
803 return 0;
804 }
805
806 /* Synchronize GDB's thread list with libpthdebug's.
807
808 There are some benefits of doing this every time the inferior stops:
809
810 - allows users to run thread-specific commands without needing to
811 run "info threads" first
812
813 - helps pthdb_tid_pthread() work properly (see "libpthdebug
814 peculiarities" at the top of this module)
815
816 - simplifies the demands placed on libpthdebug, which seems to
817 have difficulty with certain call patterns */
818
819 static void
820 sync_threadlists (pid_t pid)
821 {
822 int cmd, status;
823 int pcount, psize, pi, gcount, gi;
824 struct pd_thread *pbuf;
825 struct thread_info **gbuf, **g, *thread;
826 pthdb_pthread_t pdtid;
827 pthread_t pthid;
828 pthdb_tid_t tid;
829 process_stratum_target *proc_target = current_inferior ()->process_target ();
830 thread_info *tp;
831 struct aix_thread_variables *data;
832 data = get_thread_data_helper_for_pid (pid);
833
834 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
835
836 pcount = 0;
837 psize = 1;
838 pbuf = XNEWVEC (struct pd_thread, psize);
839
840 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
841 {
842 status = pthdb_pthread (data->pd_session, &pdtid, cmd);
843 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
844 break;
845
846 status = pthdb_pthread_ptid (data->pd_session, pdtid, &pthid);
847 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
848 continue;
849
850 if (pcount == psize)
851 {
852 psize *= 2;
853 pbuf = (struct pd_thread *) xrealloc (pbuf,
854 psize * sizeof *pbuf);
855 }
856 pbuf[pcount].pdtid = pdtid;
857 pbuf[pcount].pthid = pthid;
858 pcount++;
859 }
860
861 for (pi = 0; pi < pcount; pi++)
862 {
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;
867 }
868
869 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
870
871 /* Accumulate an array of GDB threads sorted by pid. */
872
873 /* gcount is GDB thread count and pcount is pthreadlib thread count. */
874
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);
882
883 /* Apply differences between the two arrays to GDB's thread list. */
884
885 for (pi = gi = 0; pi < pcount || gi < gcount;)
886 {
887 if (pi == pcount)
888 {
889 delete_thread (gbuf[gi]);
890 gi++;
891 }
892 else if (gi == gcount)
893 {
894 aix_thread_info *priv = new aix_thread_info;
895 priv->pdtid = pbuf[pi].pdtid;
896 priv->tid = pbuf[pi].tid;
897
898 thread = add_thread_with_info (proc_target,
899 ptid_t (pid, 0, pbuf[pi].pthid),
900 private_thread_info_up (priv));
901
902 pi++;
903 }
904 else
905 {
906 ptid_t pptid, gptid;
907 int cmp_result;
908
909 pptid = ptid_t (pid, 0, pbuf[pi].pthid);
910 gptid = gbuf[gi]->ptid;
911 pdtid = pbuf[pi].pdtid;
912 tid = pbuf[pi].tid;
913
914 cmp_result = ptid_cmp (pptid, gptid);
915
916 if (cmp_result == 0)
917 {
918 aix_thread_info *priv = get_aix_thread_info (gbuf[gi]);
919
920 priv->pdtid = pdtid;
921 priv->tid = tid;
922 pi++;
923 gi++;
924 }
925 else if (cmp_result > 0)
926 {
927 /* This is to make the main process thread now look
928 like a thread. */
929
930 if (gptid.is_pid ())
931 {
932 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++;
940 }
941 else
942 {
943 delete_thread (gbuf[gi]);
944 gi++;
945 }
946 }
947 else
948 {
949 thread = add_thread (proc_target, pptid);
950
951 aix_thread_info *priv = new aix_thread_info;
952 thread->priv.reset (priv);
953 priv->pdtid = pdtid;
954 priv->tid = tid;
955 pi++;
956 }
957 }
958 }
959
960 xfree (pbuf);
961 xfree (gbuf);
962 }
963
964 /* Iterate_over_threads() callback for locating a thread, using
965 the TID of its associated kernel thread. */
966
967 static int
968 iter_tid (struct thread_info *thread, void *tidp)
969 {
970 const pthdb_tid_t tid = *(pthdb_tid_t *)tidp;
971 aix_thread_info *priv = get_aix_thread_info (thread);
972
973 return priv->tid == tid;
974 }
975
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. */
980
981 static ptid_t
982 pd_update (pid_t pid)
983 {
984 int status;
985 ptid_t ptid;
986 pthdb_tid_t tid;
987 struct thread_info *thread = NULL;
988 struct aix_thread_variables *data;
989
990 data = get_thread_data_helper_for_pid (pid);
991
992 if (!data->pd_active)
993 return ptid_t (pid);
994
995 status = pthdb_session_update (data->pd_session);
996 if (status != PTHDB_SUCCESS)
997 return ptid_t (pid);
998
999 sync_threadlists (pid);
1000
1001 /* Define "current thread" as one that just received a trap signal. */
1002
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;
1010
1011 return ptid;
1012 }
1013
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. */
1017
1018 static ptid_t
1019 pd_activate (pid_t pid)
1020 {
1021 int status;
1022 struct aix_thread_variables *data;
1023 data = get_thread_data_helper_for_pid (pid);
1024
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 {
1030 return ptid_t (pid);
1031 }
1032 data->pd_active = 1;
1033 return pd_update (pid);
1034 }
1035
1036 /* An object file has just been loaded. Check whether the current
1037 application is pthreaded, and if so, prepare for thread debugging. */
1038
1039 static void
1040 pd_enable (inferior *inf)
1041 {
1042 int status;
1043 char *stub_name;
1044 struct bound_minimal_symbol ms;
1045 struct aix_thread_variables *data;
1046
1047 if (inf == NULL)
1048 return;
1049
1050 data = get_aix_thread_variables_data (inf);
1051
1052 /* Don't initialize twice. */
1053 if (data->pd_able)
1054 return;
1055
1056 /* Check application word size. */
1057 data->arch64 = register_size (current_inferior ()->arch (), 0) == 8;
1058
1059 /* Check whether the application is pthreaded. */
1060 stub_name = NULL;
1061 status = pthdb_session_pthreaded (inf->pid, PTHDB_FLAG_REGS,
1062 &pd_callbacks, &stub_name);
1063 if ((status != PTHDB_SUCCESS
1064 && status != PTHDB_NOT_PTHREADED) || !stub_name)
1065 return;
1066
1067 /* Set a breakpoint on the returned stub function. */
1068 ms = lookup_minimal_symbol (stub_name, NULL, NULL);
1069 if (ms.minsym == NULL)
1070 return;
1071 data->pd_brk_addr = ms.value_address ();
1072 if (!create_thread_event_breakpoint (current_inferior ()->arch (),
1073 data->pd_brk_addr))
1074 return;
1075
1076 /* Prepare for thread debugging. */
1077 current_inferior ()->push_target (&aix_thread_ops);
1078 data->pd_able = 1;
1079
1080 /* When attaching / handling fork child, don't try activating
1081 thread debugging until we know about all shared libraries. */
1082 if (inf->in_initial_library_scan)
1083 return;
1084
1085 /* If we're debugging a core file or an attached inferior, the
1086 pthread library may already have been initialized, so try to
1087 activate thread debugging. */
1088 pd_activate (inf->pid);
1089 }
1090
1091 /* Undo the effects of pd_enable(). */
1092
1093 static void
1094 pd_disable (inferior *inf)
1095 {
1096 struct aix_thread_variables *data;
1097 data = get_aix_thread_variables_data (inf);
1098
1099 if (!data->pd_able)
1100 return;
1101 if (!data->pd_active)
1102 return;
1103 pthdb_session_destroy (data->pd_session);
1104
1105 pid_to_prc (&inferior_ptid);
1106 data->pd_active = 0;
1107 data->pd_able = 0;
1108 current_inferior ()->unpush_target (&aix_thread_ops);
1109 }
1110
1111 /* new_objfile observer callback.
1112
1113 Check whether a threaded application is being debugged, and if so, prepare
1114 for thread debugging. */
1115
1116 static void
1117 new_objfile (struct objfile *objfile)
1118 {
1119 pd_enable (current_inferior ());
1120 }
1121
1122 /* Attach to process specified by ARGS. */
1123
1124 static void
1125 aix_thread_inferior_created (inferior *inf)
1126 {
1127 pd_enable (inf);
1128 }
1129
1130 /* Detach from the process attached to by aix_thread_attach(). */
1131
1132 void
1133 aix_thread_target::detach (inferior *inf, int from_tty)
1134 {
1135 target_ops *beneath = this->beneath ();
1136
1137 pd_disable (inf);
1138 beneath->detach (inf, from_tty);
1139 }
1140
1141 /* Tell the inferior process to continue running thread PID if != -1
1142 and all threads otherwise. */
1143
1144 void
1145 aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
1146 {
1147 struct thread_info *thread;
1148 pthdb_tid_t tid[2];
1149 struct aix_thread_variables *data;
1150
1151 data = get_thread_data_helper_for_ptid (ptid);
1152
1153 if (ptid.tid () == 0)
1154 {
1155 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
1156
1157 inferior_ptid = ptid_t (inferior_ptid.pid ());
1158 beneath ()->resume (ptid, step, sig);
1159 }
1160 else
1161 {
1162 thread = current_inferior ()->find_thread (ptid);
1163 if (!thread)
1164 error (_("aix-thread resume: unknown pthread %ld"),
1165 ptid.lwp ());
1166
1167 aix_thread_info *priv = get_aix_thread_info (thread);
1168
1169 tid[0] = priv->tid;
1170 if (tid[0] == PTHDB_INVALID_TID)
1171 error (_("aix-thread resume: no tid for pthread %ld"),
1172 ptid.lwp ());
1173 tid[1] = 0;
1174
1175 if (data->arch64)
1176 ptrace64aix (PTT_CONTINUE, tid[0], (long long) 1,
1177 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1178 else
1179 ptrace32 (PTT_CONTINUE, tid[0], (addr_ptr) 1,
1180 gdb_signal_to_host (sig), (PTRACE_TYPE_ARG5) tid);
1181 }
1182 }
1183
1184 /* Wait for thread/process ID if != -1 or for any thread otherwise.
1185 If an error occurs, return -1, else return the pid of the stopped
1186 thread. */
1187
1188 ptid_t
1189 aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
1190 target_wait_flags options)
1191 {
1192 struct aix_thread_variables *data;
1193 {
1194 pid_to_prc (&ptid);
1195
1196 ptid = beneath ()->wait (ptid, status, options);
1197 }
1198
1199 if (ptid.pid () == -1)
1200 return ptid_t (-1);
1201
1202 /* The target beneath does not deal with threads, so it should only return
1203 pid-only ptids. */
1204 gdb_assert (ptid.is_pid ());
1205
1206 data = get_thread_data_helper_for_ptid (ptid);
1207
1208 /* Check whether libpthdebug might be ready to be initialized. */
1209 if (!data->pd_active && status->kind () == TARGET_WAITKIND_STOPPED
1210 && status->sig () == GDB_SIGNAL_TRAP)
1211 {
1212 process_stratum_target *proc_target
1213 = current_inferior ()->process_target ();
1214 struct regcache *regcache = get_thread_regcache (proc_target, ptid);
1215 struct gdbarch *gdbarch = regcache->arch ();
1216
1217 if (regcache_read_pc (regcache)
1218 - gdbarch_decr_pc_after_break (gdbarch) == data->pd_brk_addr)
1219 return pd_activate (ptid.pid ());
1220 }
1221
1222 return pd_update (ptid.pid ());
1223 }
1224
1225 /* Supply AIX altivec registers, both 64 and 32 bit. */
1226
1227 static void
1228 supply_altivec_regs (struct regcache *regcache, __vmx_context_t vmx)
1229 {
1230 ppc_gdbarch_tdep *tdep
1231 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1232 int regno;
1233 for (regno = 0; regno < ppc_num_vrs; regno++)
1234 regcache->raw_supply (tdep->ppc_vr0_regnum + regno,
1235 &(vmx.__vr[regno]));
1236 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1237 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1238 }
1239
1240 /* Supply AIX VSX registers, both 64 and 32 bit. */
1241
1242 static void
1243 supply_vsx_regs (struct regcache *regcache, __vsx_context_t vsx)
1244 {
1245 ppc_gdbarch_tdep *tdep
1246 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1247 int regno;
1248
1249 for (regno = 0; regno < ppc_num_vshrs; regno++)
1250 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + regno,
1251 &(vsx.__vsr_dw1[regno]));
1252 }
1253
1254 /* Record that the 64-bit general-purpose registers contain VALS. */
1255
1256 static void
1257 supply_gprs64 (struct regcache *regcache, uint64_t *vals)
1258 {
1259 ppc_gdbarch_tdep *tdep
1260 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1261 int regno;
1262
1263 for (regno = 0; regno < ppc_num_gprs; regno++)
1264 regcache->raw_supply (tdep->ppc_gp0_regnum + regno,
1265 (char *) (vals + regno));
1266 }
1267
1268 /* Record that 32-bit register REGNO contains VAL. */
1269
1270 static void
1271 supply_reg32 (struct regcache *regcache, int regno, uint32_t val)
1272 {
1273 regcache->raw_supply (regno, (char *) &val);
1274 }
1275
1276 /* Record that the floating-point registers contain VALS. */
1277
1278 static void
1279 supply_fprs (struct regcache *regcache, double *vals)
1280 {
1281 struct gdbarch *gdbarch = regcache->arch ();
1282 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1283 int regno;
1284
1285 /* This function should never be called on architectures without
1286 floating-point registers. */
1287 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1288
1289 for (regno = tdep->ppc_fp0_regnum;
1290 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1291 regno++)
1292 regcache->raw_supply (regno,
1293 (char *) (vals + regno - tdep->ppc_fp0_regnum));
1294 }
1295
1296 /* Predicate to test whether given register number is a "special" register. */
1297 static int
1298 special_register_p (struct gdbarch *gdbarch, int regno)
1299 {
1300 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1301
1302 return regno == gdbarch_pc_regnum (gdbarch)
1303 || regno == tdep->ppc_ps_regnum
1304 || regno == tdep->ppc_cr_regnum
1305 || regno == tdep->ppc_lr_regnum
1306 || regno == tdep->ppc_ctr_regnum
1307 || regno == tdep->ppc_xer_regnum
1308 || (tdep->ppc_fpscr_regnum >= 0 && regno == tdep->ppc_fpscr_regnum)
1309 || (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum);
1310 }
1311
1312
1313 /* Record that the special registers contain the specified 64-bit and
1314 32-bit values. */
1315
1316 static void
1317 supply_sprs64 (struct regcache *regcache,
1318 uint64_t iar, uint64_t msr, uint32_t cr,
1319 uint64_t lr, uint64_t ctr, uint32_t xer,
1320 uint32_t fpscr)
1321 {
1322 struct gdbarch *gdbarch = regcache->arch ();
1323 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1324
1325 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1326 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1327 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1328 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1329 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1330 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1331 if (tdep->ppc_fpscr_regnum >= 0)
1332 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1333 }
1334
1335 /* Record that the special registers contain the specified 32-bit
1336 values. */
1337
1338 static void
1339 supply_sprs32 (struct regcache *regcache,
1340 uint32_t iar, uint32_t msr, uint32_t cr,
1341 uint32_t lr, uint32_t ctr, uint32_t xer,
1342 uint32_t fpscr)
1343 {
1344 struct gdbarch *gdbarch = regcache->arch ();
1345 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1346
1347 regcache->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &iar);
1348 regcache->raw_supply (tdep->ppc_ps_regnum, (char *) &msr);
1349 regcache->raw_supply (tdep->ppc_cr_regnum, (char *) &cr);
1350 regcache->raw_supply (tdep->ppc_lr_regnum, (char *) &lr);
1351 regcache->raw_supply (tdep->ppc_ctr_regnum, (char *) &ctr);
1352 regcache->raw_supply (tdep->ppc_xer_regnum, (char *) &xer);
1353 if (tdep->ppc_fpscr_regnum >= 0)
1354 regcache->raw_supply (tdep->ppc_fpscr_regnum, (char *) &fpscr);
1355 }
1356
1357 /* Fetch all registers from pthread PDTID, which doesn't have a kernel
1358 thread.
1359
1360 There's no way to query a single register from a non-kernel
1361 pthread, so there's no need for a single-register version of this
1362 function. */
1363
1364 static void
1365 fetch_regs_user_thread (struct regcache *regcache, pthdb_pthread_t pdtid)
1366 {
1367 struct gdbarch *gdbarch = regcache->arch ();
1368 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1369 int status, i;
1370 pthdb_context_t ctx;
1371 struct aix_thread_variables *data;
1372 data = get_thread_data_helper_for_ptid (inferior_ptid);
1373
1374 if (debug_aix_thread)
1375 gdb_printf (gdb_stdlog,
1376 "fetch_regs_user_thread %lx\n", (long) pdtid);
1377 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1378 if (status != PTHDB_SUCCESS)
1379 error (_("aix-thread: fetch_registers: pthdb_pthread_context returned %s"),
1380 pd_status2str (status));
1381
1382 /* General-purpose registers. */
1383
1384 if (data->arch64)
1385 supply_gprs64 (regcache, ctx.gpr);
1386 else
1387 for (i = 0; i < ppc_num_gprs; i++)
1388 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, ctx.gpr[i]);
1389
1390 /* Floating-point registers. */
1391
1392 if (ppc_floating_point_unit_p (gdbarch))
1393 supply_fprs (regcache, ctx.fpr);
1394
1395 /* Special registers. */
1396
1397 if (data->arch64)
1398 supply_sprs64 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1399 ctx.xer, ctx.fpscr);
1400 else
1401 supply_sprs32 (regcache, ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr,
1402 ctx.xer, ctx.fpscr);
1403
1404 /* Altivec registers. */
1405 supply_altivec_regs (regcache, ctx.vmx);
1406
1407 /* VSX registers. */
1408 supply_vsx_regs (regcache, ctx.vsx);
1409 }
1410
1411 /* Fetch register REGNO if != -1 or all registers otherwise from
1412 kernel thread TID.
1413
1414 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or
1415 SPRs, but there's no way to query individual registers within those
1416 groups. Therefore, if REGNO != -1, this function fetches an entire
1417 group.
1418
1419 Unfortunately, kernel thread register queries often fail with
1420 EPERM, indicating that the thread is in kernel space. This breaks
1421 backtraces of threads other than the current one. To make that
1422 breakage obvious without throwing an error to top level (which is
1423 bad e.g. during "info threads" output), zero registers that can't
1424 be retrieved. */
1425
1426 static void
1427 fetch_regs_kernel_thread (struct regcache *regcache, int regno,
1428 pthdb_tid_t tid)
1429 {
1430 struct gdbarch *gdbarch = regcache->arch ();
1431 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1432 uint64_t gprs64[ppc_num_gprs];
1433 uint32_t gprs32[ppc_num_gprs];
1434 double fprs[ppc_num_fprs];
1435 struct ptxsprs sprs64;
1436 struct ptsprs sprs32;
1437 int i;
1438 struct aix_thread_variables *data;
1439
1440 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1441
1442 if (debug_aix_thread)
1443 gdb_printf (gdb_stdlog,
1444 "fetch_regs_kernel_thread tid=%lx regno=%d arch64=%d\n",
1445 (long) tid, regno, data->arch64);
1446
1447 /* General-purpose registers. */
1448 if (regno == -1
1449 || (tdep->ppc_gp0_regnum <= regno
1450 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs))
1451 {
1452 if (data->arch64)
1453 {
1454 if (!ptrace64aix (PTT_READ_GPRS, tid,
1455 (unsigned long) gprs64, 0, NULL))
1456 memset (gprs64, 0, sizeof (gprs64));
1457 supply_gprs64 (regcache, gprs64);
1458 }
1459 else
1460 {
1461 if (!ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL))
1462 memset (gprs32, 0, sizeof (gprs32));
1463 for (i = 0; i < ppc_num_gprs; i++)
1464 supply_reg32 (regcache, tdep->ppc_gp0_regnum + i, gprs32[i]);
1465 }
1466 }
1467
1468 /* vector registers. */
1469 if (tdep->ppc_vr0_regnum != -1)
1470 {
1471 int ret = 0;
1472 __vmx_context_t vmx;
1473 if (data->arch64)
1474 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1475 else
1476 ret = ptrace32 (PTT_READ_VEC, tid, (uintptr_t) &vmx, 0, 0);
1477 if (ret < 0)
1478 memset(&vmx, 0, sizeof(__vmx_context_t));
1479 for (i = 0; i < ppc_num_vrs; i++)
1480 regcache->raw_supply (tdep->ppc_vr0_regnum + i, &(vmx.__vr[i]));
1481 regcache->raw_supply (tdep->ppc_vrsave_regnum, &(vmx.__vrsave));
1482 regcache->raw_supply (tdep->ppc_vrsave_regnum - 1, &(vmx.__vscr));
1483 }
1484
1485 /* vsx registers. */
1486 if (tdep->ppc_vsr0_upper_regnum != -1)
1487 {
1488 __vsx_context_t vsx;
1489 int ret = 0;
1490 if (data->arch64)
1491 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1492 else
1493 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1494 if (ret < 0)
1495 memset(&vsx, 0, sizeof(__vsx_context_t));
1496 for (i = 0; i < ppc_num_vshrs; i++)
1497 regcache->raw_supply (tdep->ppc_vsr0_upper_regnum + i, &(vsx.__vsr_dw1[i]));
1498 }
1499
1500 /* Floating-point registers. */
1501
1502 if (ppc_floating_point_unit_p (gdbarch)
1503 && (regno == -1
1504 || (regno >= tdep->ppc_fp0_regnum
1505 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1506 {
1507 if (!ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL))
1508 memset (fprs, 0, sizeof (fprs));
1509 supply_fprs (regcache, fprs);
1510 }
1511
1512 /* Special-purpose registers. */
1513
1514 if (regno == -1 || special_register_p (gdbarch, regno))
1515 {
1516 if (data->arch64)
1517 {
1518 if (!ptrace64aix (PTT_READ_SPRS, tid,
1519 (unsigned long) &sprs64, 0, NULL))
1520 memset (&sprs64, 0, sizeof (sprs64));
1521 supply_sprs64 (regcache, sprs64.pt_iar, sprs64.pt_msr,
1522 sprs64.pt_cr, sprs64.pt_lr, sprs64.pt_ctr,
1523 sprs64.pt_xer, sprs64.pt_fpscr);
1524 }
1525 else
1526 {
1527 if (!ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL))
1528 memset (&sprs32, 0, sizeof (sprs32));
1529 supply_sprs32 (regcache, sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1530 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer,
1531 sprs32.pt_fpscr);
1532
1533 if (tdep->ppc_mq_regnum >= 0)
1534 regcache->raw_supply (tdep->ppc_mq_regnum, (char *) &sprs32.pt_mq);
1535 }
1536 }
1537 }
1538
1539 /* Fetch register REGNO if != -1 or all registers otherwise from the
1540 thread/process connected to REGCACHE. */
1541
1542 void
1543 aix_thread_target::fetch_registers (struct regcache *regcache, int regno)
1544 {
1545 struct thread_info *thread;
1546 pthdb_tid_t tid;
1547
1548 /* If a new inferior is born, then its pthread debug library is yet to
1549 initialised and hence has no private data. So the below if condition
1550 exists. */
1551
1552 if (regcache->ptid ().tid () == 0)
1553 beneath ()->fetch_registers (regcache, regno);
1554 else
1555 {
1556 thread = current_inferior ()->find_thread (regcache->ptid ());
1557 aix_thread_info *priv = get_aix_thread_info (thread);
1558 tid = priv->tid;
1559
1560 if (tid == PTHDB_INVALID_TID)
1561 fetch_regs_user_thread (regcache, priv->pdtid);
1562 else
1563 fetch_regs_kernel_thread (regcache, regno, tid);
1564 }
1565 }
1566
1567 /* Fill altivec registers. */
1568
1569 static void
1570 fill_altivec (const struct regcache *regcache, __vmx_context_t *vmx)
1571 {
1572 struct gdbarch *gdbarch = regcache->arch ();
1573 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1574 int regno;
1575
1576 for (regno = 0; regno < ppc_num_vrs; regno++)
1577 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + regno))
1578 regcache->raw_collect (tdep->ppc_vr0_regnum + regno,
1579 &(vmx->__vr[regno]));
1580
1581 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1582 regcache->raw_collect (tdep->ppc_vrsave_regnum, &(vmx->__vrsave));
1583 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1584 regcache->raw_collect (tdep->ppc_vrsave_regnum - 1, &(vmx->__vscr));
1585 }
1586
1587 /* Fill vsx registers. */
1588
1589 static void
1590 fill_vsx (const struct regcache *regcache, __vsx_context_t *vsx)
1591 {
1592 struct gdbarch *gdbarch = regcache->arch ();
1593 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1594 int regno;
1595
1596 for (regno = 0; regno < ppc_num_vshrs; regno++)
1597 if (REG_VALID == regcache->get_register_status ( tdep->ppc_vsr0_upper_regnum + regno))
1598 regcache->raw_collect (tdep->ppc_vsr0_upper_regnum + regno,
1599 &(vsx->__vsr_dw1[0]) + regno);
1600 }
1601
1602 /* Store the gp registers into an array of uint32_t or uint64_t. */
1603
1604 static void
1605 fill_gprs64 (const struct regcache *regcache, uint64_t *vals)
1606 {
1607 ppc_gdbarch_tdep *tdep
1608 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1609 int regno;
1610
1611 for (regno = 0; regno < ppc_num_gprs; regno++)
1612 if (REG_VALID == regcache->get_register_status
1613 (tdep->ppc_gp0_regnum + regno))
1614 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1615 }
1616
1617 static void
1618 fill_gprs32 (const struct regcache *regcache, uint32_t *vals)
1619 {
1620 ppc_gdbarch_tdep *tdep
1621 = gdbarch_tdep<ppc_gdbarch_tdep> (regcache->arch ());
1622 int regno;
1623
1624 for (regno = 0; regno < ppc_num_gprs; regno++)
1625 if (REG_VALID == regcache->get_register_status
1626 (tdep->ppc_gp0_regnum + regno))
1627 regcache->raw_collect (tdep->ppc_gp0_regnum + regno, vals + regno);
1628 }
1629
1630 /* Store the floating point registers into a double array. */
1631 static void
1632 fill_fprs (const struct regcache *regcache, double *vals)
1633 {
1634 struct gdbarch *gdbarch = regcache->arch ();
1635 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1636 int regno;
1637
1638 /* This function should never be called on architectures without
1639 floating-point registers. */
1640 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1641
1642 for (regno = tdep->ppc_fp0_regnum;
1643 regno < tdep->ppc_fp0_regnum + ppc_num_fprs;
1644 regno++)
1645 if (REG_VALID == regcache->get_register_status (regno))
1646 regcache->raw_collect (regno, vals + regno - tdep->ppc_fp0_regnum);
1647 }
1648
1649 /* Store the special registers into the specified 64-bit and 32-bit
1650 locations. */
1651
1652 static void
1653 fill_sprs64 (const struct regcache *regcache,
1654 uint64_t *iar, uint64_t *msr, uint32_t *cr,
1655 uint64_t *lr, uint64_t *ctr, uint32_t *xer,
1656 uint32_t *fpscr)
1657 {
1658 struct gdbarch *gdbarch = regcache->arch ();
1659 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1660
1661 /* Verify that the size of the size of the IAR buffer is the
1662 same as the raw size of the PC (in the register cache). If
1663 they're not, then either GDB has been built incorrectly, or
1664 there's some other kind of internal error. To be really safe,
1665 we should check all of the sizes. */
1666 gdb_assert (sizeof (*iar) == register_size
1667 (gdbarch, gdbarch_pc_regnum (gdbarch)));
1668
1669 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1670 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1671 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1672 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1673 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1674 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1675 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1676 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1677 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1678 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1679 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1680 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1681 if (tdep->ppc_fpscr_regnum >= 0
1682 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1683 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1684 }
1685
1686 static void
1687 fill_sprs32 (const struct regcache *regcache,
1688 uint32_t *iar, uint32_t *msr, uint32_t *cr,
1689 uint32_t *lr, uint32_t *ctr, uint32_t *xer,
1690 uint32_t *fpscr)
1691 {
1692 struct gdbarch *gdbarch = regcache->arch ();
1693 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1694
1695 /* Verify that the size of the size of the IAR buffer is the
1696 same as the raw size of the PC (in the register cache). If
1697 they're not, then either GDB has been built incorrectly, or
1698 there's some other kind of internal error. To be really safe,
1699 we should check all of the sizes. */
1700 gdb_assert (sizeof (*iar) == register_size (gdbarch,
1701 gdbarch_pc_regnum (gdbarch)));
1702
1703 if (REG_VALID == regcache->get_register_status (gdbarch_pc_regnum (gdbarch)))
1704 regcache->raw_collect (gdbarch_pc_regnum (gdbarch), iar);
1705 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1706 regcache->raw_collect (tdep->ppc_ps_regnum, msr);
1707 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1708 regcache->raw_collect (tdep->ppc_cr_regnum, cr);
1709 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1710 regcache->raw_collect (tdep->ppc_lr_regnum, lr);
1711 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1712 regcache->raw_collect (tdep->ppc_ctr_regnum, ctr);
1713 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1714 regcache->raw_collect (tdep->ppc_xer_regnum, xer);
1715 if (tdep->ppc_fpscr_regnum >= 0
1716 && REG_VALID == regcache->get_register_status (tdep->ppc_fpscr_regnum))
1717 regcache->raw_collect (tdep->ppc_fpscr_regnum, fpscr);
1718 }
1719
1720 /* Store all registers into pthread PDTID, which doesn't have a kernel
1721 thread.
1722
1723 It's possible to store a single register into a non-kernel pthread,
1724 but I doubt it's worth the effort. */
1725
1726 static void
1727 store_regs_user_thread (const struct regcache *regcache, pthdb_pthread_t pdtid)
1728 {
1729 struct gdbarch *gdbarch = regcache->arch ();
1730 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1731 int status, i;
1732 pthdb_context_t ctx;
1733 uint32_t int32;
1734 uint64_t int64;
1735 struct aix_thread_variables *data;
1736 data = get_thread_data_helper_for_ptid (inferior_ptid);
1737 int ret;
1738 __vmx_context_t vmx;
1739 __vsx_context_t vsx;
1740
1741 if (debug_aix_thread)
1742 gdb_printf (gdb_stdlog,
1743 "store_regs_user_thread %lx\n", (long) pdtid);
1744
1745 /* Retrieve the thread's current context for its non-register
1746 values. */
1747 status = pthdb_pthread_context (data->pd_session, pdtid, &ctx);
1748 if (status != PTHDB_SUCCESS)
1749 error (_("aix-thread: store_registers: pthdb_pthread_context returned %s"),
1750 pd_status2str (status));
1751
1752 /* Fill altivec-registers. */
1753
1754 if (__power_vmx())
1755 {
1756 memset(&vmx, 0, sizeof(__vmx_context_t));
1757 for (i = 0; i < ppc_num_vrs; i++)
1758 if (REG_VALID == regcache->get_register_status (tdep->ppc_vr0_regnum + i))
1759 {
1760 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1761 &(vmx.__vr[i]));
1762 ctx.vmx.__vr[i] = vmx.__vr[i];
1763 }
1764 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum))
1765 ctx.vmx.__vrsave = vmx.__vrsave;
1766 if (REG_VALID == regcache->get_register_status (tdep->ppc_vrsave_regnum - 1))
1767 ctx.vmx.__vscr = vmx.__vscr;
1768 }
1769
1770 /* Fill vsx registers. */
1771
1772 if (__power_vsx())
1773 {
1774 memset(&vsx, 0, sizeof(__vsx_context_t));
1775 for (i = 0; i < ppc_num_vshrs; i++)
1776 if (REG_VALID == regcache->get_register_status (tdep->ppc_vsr0_regnum + i))
1777 {
1778 regcache->raw_collect (tdep->ppc_vr0_regnum + i,
1779 &(vsx.__vsr_dw1[i]));
1780 ctx.vsx.__vsr_dw1[i] = vsx.__vsr_dw1[i];
1781 }
1782 }
1783
1784 /* Collect general-purpose register values from the regcache. */
1785
1786 for (i = 0; i < ppc_num_gprs; i++)
1787 if (REG_VALID == regcache->get_register_status (tdep->ppc_gp0_regnum + i))
1788 {
1789 if (data->arch64)
1790 {
1791 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int64);
1792 ctx.gpr[i] = int64;
1793 }
1794 else
1795 {
1796 regcache->raw_collect (tdep->ppc_gp0_regnum + i, (void *) &int32);
1797 ctx.gpr[i] = int32;
1798 }
1799 }
1800
1801 /* Collect floating-point register values from the regcache. */
1802 if (ppc_floating_point_unit_p (gdbarch))
1803 fill_fprs (regcache, ctx.fpr);
1804
1805 /* Special registers (always kept in ctx as 64 bits). */
1806 if (data->arch64)
1807 {
1808 fill_sprs64 (regcache, &ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr,
1809 &ctx.xer, &ctx.fpscr);
1810 }
1811 else
1812 {
1813 /* Problem: ctx.iar etc. are 64 bits, but raw_registers are 32.
1814 Solution: use 32-bit temp variables. */
1815 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1816 tmp_fpscr;
1817
1818 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr, &tmp_ctr,
1819 &tmp_xer, &tmp_fpscr);
1820 if (REG_VALID == regcache->get_register_status
1821 (gdbarch_pc_regnum (gdbarch)))
1822 ctx.iar = tmp_iar;
1823 if (REG_VALID == regcache->get_register_status (tdep->ppc_ps_regnum))
1824 ctx.msr = tmp_msr;
1825 if (REG_VALID == regcache->get_register_status (tdep->ppc_cr_regnum))
1826 ctx.cr = tmp_cr;
1827 if (REG_VALID == regcache->get_register_status (tdep->ppc_lr_regnum))
1828 ctx.lr = tmp_lr;
1829 if (REG_VALID == regcache->get_register_status (tdep->ppc_ctr_regnum))
1830 ctx.ctr = tmp_ctr;
1831 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1832 ctx.xer = tmp_xer;
1833 if (REG_VALID == regcache->get_register_status (tdep->ppc_xer_regnum))
1834 ctx.fpscr = tmp_fpscr;
1835 }
1836
1837 status = pthdb_pthread_setcontext (data->pd_session, pdtid, &ctx);
1838 if (status != PTHDB_SUCCESS)
1839 error (_("aix-thread: store_registers: "
1840 "pthdb_pthread_setcontext returned %s"),
1841 pd_status2str (status));
1842 }
1843
1844 /* Store register REGNO if != -1 or all registers otherwise into
1845 kernel thread TID.
1846
1847 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or
1848 SPRs, but there's no way to set individual registers within those
1849 groups. Therefore, if REGNO != -1, this function stores an entire
1850 group. */
1851
1852 static void
1853 store_regs_kernel_thread (const struct regcache *regcache, int regno,
1854 pthdb_tid_t tid)
1855 {
1856 struct gdbarch *gdbarch = regcache->arch ();
1857 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1858 uint64_t gprs64[ppc_num_gprs];
1859 uint32_t gprs32[ppc_num_gprs];
1860 double fprs[ppc_num_fprs];
1861 struct ptxsprs sprs64;
1862 struct ptsprs sprs32;
1863 struct aix_thread_variables *data;
1864 int ret = 0;
1865
1866 data = get_thread_data_helper_for_ptid (regcache->ptid ());
1867
1868 if (debug_aix_thread)
1869 gdb_printf (gdb_stdlog,
1870 "store_regs_kernel_thread tid=%lx regno=%d\n",
1871 (long) tid, regno);
1872
1873 /* General-purpose registers. */
1874 if (regno == -1
1875 || (tdep->ppc_gp0_regnum <= regno
1876 && regno < tdep->ppc_gp0_regnum + ppc_num_fprs))
1877 {
1878 if (data->arch64)
1879 {
1880 /* Pre-fetch: some regs may not be in the cache. */
1881 ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1882 fill_gprs64 (regcache, gprs64);
1883 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) gprs64, 0, NULL);
1884 }
1885 else
1886 {
1887 /* Pre-fetch: some regs may not be in the cache. */
1888 ptrace32 (PTT_READ_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1889 fill_gprs32 (regcache, gprs32);
1890 ptrace32 (PTT_WRITE_GPRS, tid, (uintptr_t) gprs32, 0, NULL);
1891 }
1892 }
1893
1894 /* Floating-point registers. */
1895
1896 if (ppc_floating_point_unit_p (gdbarch)
1897 && (regno == -1
1898 || (regno >= tdep->ppc_fp0_regnum
1899 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)))
1900 {
1901 /* Pre-fetch: some regs may not be in the cache. */
1902 ptrace32 (PTT_READ_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1903 fill_fprs (regcache, fprs);
1904 ptrace32 (PTT_WRITE_FPRS, tid, (uintptr_t) fprs, 0, NULL);
1905 }
1906
1907 /* Special-purpose registers. */
1908
1909 if (regno == -1 || special_register_p (gdbarch, regno))
1910 {
1911 if (data->arch64)
1912 {
1913 /* Pre-fetch: some registers won't be in the cache. */
1914 ptrace64aix (PTT_READ_SPRS, tid,
1915 (unsigned long) &sprs64, 0, NULL);
1916 fill_sprs64 (regcache, &sprs64.pt_iar, &sprs64.pt_msr,
1917 &sprs64.pt_cr, &sprs64.pt_lr, &sprs64.pt_ctr,
1918 &sprs64.pt_xer, &sprs64.pt_fpscr);
1919 ptrace64aix (PTT_WRITE_SPRS, tid,
1920 (unsigned long) &sprs64, 0, NULL);
1921 }
1922 else
1923 {
1924 /* The contents of "struct ptspr" were declared as "unsigned
1925 long" up to AIX 5.2, but are "unsigned int" since 5.3.
1926 Use temporaries to work around this problem. Also, add an
1927 assert here to make sure we fail if the system header files
1928 use "unsigned long", and the size of that type is not what
1929 the headers expect. */
1930 uint32_t tmp_iar, tmp_msr, tmp_cr, tmp_lr, tmp_ctr, tmp_xer,
1931 tmp_fpscr;
1932
1933 gdb_assert (sizeof (sprs32.pt_iar) == 4);
1934
1935 /* Pre-fetch: some registers won't be in the cache. */
1936 ptrace32 (PTT_READ_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1937
1938 fill_sprs32 (regcache, &tmp_iar, &tmp_msr, &tmp_cr, &tmp_lr,
1939 &tmp_ctr, &tmp_xer, &tmp_fpscr);
1940
1941 sprs32.pt_iar = tmp_iar;
1942 sprs32.pt_msr = tmp_msr;
1943 sprs32.pt_cr = tmp_cr;
1944 sprs32.pt_lr = tmp_lr;
1945 sprs32.pt_ctr = tmp_ctr;
1946 sprs32.pt_xer = tmp_xer;
1947 sprs32.pt_fpscr = tmp_fpscr;
1948
1949 if (tdep->ppc_mq_regnum >= 0)
1950 if (REG_VALID == regcache->get_register_status
1951 (tdep->ppc_mq_regnum))
1952 regcache->raw_collect (tdep->ppc_mq_regnum, &sprs32.pt_mq);
1953
1954 ptrace32 (PTT_WRITE_SPRS, tid, (uintptr_t) &sprs32, 0, NULL);
1955 }
1956 }
1957
1958 /* Vector registers. */
1959 if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1
1960 && (regno == -1 || (regno >= tdep->ppc_vr0_regnum
1961 && regno <= tdep->ppc_vrsave_regnum)))
1962 {
1963 __vmx_context_t vmx;
1964 if (__power_vmx())
1965 {
1966 if (data->arch64)
1967 ret = ptrace64aix (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1968 else
1969 ret = ptrace32 (PTT_READ_VEC, tid, (long long) &vmx, 0, 0);
1970 if (ret > 0)
1971 {
1972 fill_altivec(regcache, &vmx);
1973 if (data->arch64)
1974 ret = ptrace64aix (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1975 else
1976 ret = ptrace32 (PTT_WRITE_VEC, tid, (long long) &vmx, 0, 0);
1977 if (ret < 0)
1978 perror_with_name (_("Unable to store AltiVec register after read"));
1979 }
1980 }
1981 }
1982
1983 /* VSX registers. */
1984 if (tdep->ppc_vsr0_upper_regnum != -1 && (regno == -1
1985 || (regno >=tdep->ppc_vsr0_upper_regnum
1986 && regno < tdep->ppc_vsr0_upper_regnum + ppc_num_vshrs)))
1987 {
1988 __vsx_context_t vsx;
1989 if (__power_vsx())
1990 {
1991 if (data->arch64)
1992 ret = ptrace64aix (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1993 else
1994 ret = ptrace32 (PTT_READ_VSX, tid, (long long) &vsx, 0, 0);
1995 if (ret > 0)
1996 {
1997 fill_vsx (regcache, &vsx);
1998 if (data->arch64)
1999 ret = ptrace64aix (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
2000 else
2001 ret = ptrace32 (PTT_WRITE_VSX, tid, (long long) &vsx, 0, 0);
2002 if (ret < 0)
2003 perror_with_name (_("Unable to store VSX register after read"));
2004 }
2005 }
2006 }
2007 }
2008
2009 /* Store gdb's current view of the register set into the
2010 thread/process connected to REGCACHE. */
2011
2012 void
2013 aix_thread_target::store_registers (struct regcache *regcache, int regno)
2014 {
2015 struct thread_info *thread;
2016 pthdb_tid_t tid;
2017
2018 if (regcache->ptid ().tid () == 0)
2019 beneath ()->store_registers (regcache, regno);
2020 else
2021 {
2022 thread = current_inferior ()->find_thread (regcache->ptid ());
2023 aix_thread_info *priv = get_aix_thread_info (thread);
2024 tid = priv->tid;
2025
2026 if (tid == PTHDB_INVALID_TID)
2027 store_regs_user_thread (regcache, priv->pdtid);
2028 else
2029 store_regs_kernel_thread (regcache, regno, tid);
2030 }
2031 }
2032
2033 /* Implement the to_xfer_partial target_ops method. */
2034
2035 enum target_xfer_status
2036 aix_thread_target::xfer_partial (enum target_object object,
2037 const char *annex, gdb_byte *readbuf,
2038 const gdb_byte *writebuf,
2039 ULONGEST offset, ULONGEST len,
2040 ULONGEST *xfered_len)
2041 {
2042 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
2043
2044 inferior_ptid = ptid_t (inferior_ptid.pid ());
2045 return beneath ()->xfer_partial (object, annex, readbuf,
2046 writebuf, offset, len, xfered_len);
2047 }
2048
2049 /* Clean up after the inferior exits. */
2050
2051 void
2052 aix_thread_target::mourn_inferior ()
2053 {
2054 target_ops *beneath = this->beneath ();
2055
2056 pd_disable (current_inferior ());
2057 beneath->mourn_inferior ();
2058 }
2059
2060 /* Return whether thread PID is still valid. */
2061
2062 bool
2063 aix_thread_target::thread_alive (ptid_t ptid)
2064 {
2065 if (ptid.tid () == 0)
2066 return beneath ()->thread_alive (ptid);
2067
2068 /* We update the thread list every time the child stops, so all
2069 valid threads should be in the thread list. */
2070 process_stratum_target *proc_target
2071 = current_inferior ()->process_target ();
2072 return in_thread_list (proc_target, ptid);
2073 }
2074
2075 /* Return a printable representation of composite PID for use in
2076 "info threads" output. */
2077
2078 std::string
2079 aix_thread_target::pid_to_str (ptid_t ptid)
2080 {
2081 if (ptid.tid () == 0)
2082 return beneath ()->pid_to_str (ptid);
2083
2084 return string_printf (_("Thread %s"), pulongest (ptid.tid ()));
2085 }
2086
2087 /* Return a printable representation of extra information about
2088 THREAD, for use in "info threads" output. */
2089
2090 const char *
2091 aix_thread_target::extra_thread_info (struct thread_info *thread)
2092 {
2093 int status;
2094 pthdb_pthread_t pdtid;
2095 pthdb_tid_t tid;
2096 pthdb_state_t state;
2097 pthdb_suspendstate_t suspendstate;
2098 pthdb_detachstate_t detachstate;
2099 int cancelpend;
2100 static char *ret = NULL;
2101 struct aix_thread_variables *data;
2102
2103 data = get_thread_data_helper_for_ptid (thread->ptid);
2104
2105 if (thread->ptid.tid () == 0)
2106 return NULL;
2107
2108 string_file buf;
2109 aix_thread_info *priv = get_aix_thread_info (thread);
2110
2111 pdtid = priv->pdtid;
2112 tid = priv->tid;
2113
2114 if (tid != PTHDB_INVALID_TID)
2115 /* i18n: Like "thread-identifier %d, [state] running, suspended" */
2116 buf.printf (_("tid %d"), (int)tid);
2117
2118 status = pthdb_pthread_state (data->pd_session, pdtid, &state);
2119 if (status != PTHDB_SUCCESS)
2120 state = PST_NOTSUP;
2121 buf.printf (", %s", state2str (state));
2122
2123 status = pthdb_pthread_suspendstate (data->pd_session, pdtid,
2124 &suspendstate);
2125 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
2126 /* i18n: Like "Thread-Id %d, [state] running, suspended" */
2127 buf.printf (_(", suspended"));
2128
2129 status = pthdb_pthread_detachstate (data->pd_session, pdtid,
2130 &detachstate);
2131 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
2132 /* i18n: Like "Thread-Id %d, [state] running, detached" */
2133 buf.printf (_(", detached"));
2134
2135 pthdb_pthread_cancelpend (data->pd_session, pdtid, &cancelpend);
2136 if (status == PTHDB_SUCCESS && cancelpend)
2137 /* i18n: Like "Thread-Id %d, [state] running, cancel pending" */
2138 buf.printf (_(", cancel pending"));
2139
2140 buf.write ("", 1);
2141
2142 xfree (ret); /* Free old buffer. */
2143
2144 ret = xstrdup (buf.c_str ());
2145
2146 return ret;
2147 }
2148
2149 ptid_t
2150 aix_thread_target::get_ada_task_ptid (long lwp, ULONGEST thread)
2151 {
2152 return ptid_t (inferior_ptid.pid (), 0, thread);
2153 }
2154
2155
2156 /* Module startup initialization function, automagically called by
2157 init.c. */
2158
2159 void _initialize_aix_thread ();
2160 void
2161 _initialize_aix_thread ()
2162 {
2163 /* Notice when object files get loaded and unloaded. */
2164 gdb::observers::new_objfile.attach (new_objfile, "aix-thread");
2165
2166 /* Add ourselves to inferior_created event chain.
2167 This is needed to enable the thread target on "attach". */
2168 gdb::observers::inferior_created.attach (aix_thread_inferior_created,
2169 "aix-thread");
2170
2171 add_setshow_boolean_cmd ("aix-thread", class_maintenance, &debug_aix_thread,
2172 _("Set debugging of AIX thread module."),
2173 _("Show debugging of AIX thread module."),
2174 _("Enables debugging output (used to debug GDB)."),
2175 NULL, NULL,
2176 /* FIXME: i18n: Debugging of AIX thread
2177 module is \"%d\". */
2178 &setdebuglist, &showdebuglist);
2179 }