2002-07-15 H.J. Lu <hjl@gnu.org>
[deliverable/binutils-gdb.git] / gdb / aix-thread.c
CommitLineData
c11d79f2
KB
1/* Low level interface for debugging AIX 4.3+ pthreads.
2
3 Copyright 1999, 2000, 2002 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23
24/* This module uses the libpthdebug.a library provided by AIX 4.3+ for
25 debugging pthread applications.
26
27 Some name prefix conventions:
28 pthdb_ provided by libpthdebug.a
29 pdc_ callbacks that this module provides to libpthdebug.a
30 pd_ variables or functions interfacing with libpthdebug.a
31
32 libpthdebug peculiarities:
33
34 - pthdb_ptid_pthread() is prototyped in <sys/pthdebug.h>, but it's not
35 documented, and after several calls it stops working and causes other
36 libpthdebug functions to fail.
37
38 - pthdb_tid_pthread() doesn't always work after pthdb_session_update(),
39 but it does work after cycling through all threads using
40 pthdb_pthread().
41
42 */
43
44#include "defs.h"
45#include "gdbthread.h"
46#include "target.h"
47#include "inferior.h"
48#include "regcache.h"
8e2c28d4 49#include "gdbcmd.h"
c11d79f2
KB
50
51#if 0
52#include "coff/internal.h" /* for libcoff.h */
53#include "bfd/libcoff.h" /* for xcoff_data */
54#endif
55
56#include <procinfo.h>
57#include <sys/types.h>
58#include <sys/ptrace.h>
59#include <sys/reg.h>
60#if 0
61#include <pthread.h>
62#endif
63#include <sched.h>
64#include <sys/pthdebug.h>
65
66/* Whether to emit debugging output. */
8e2c28d4 67static int debug_aix_thread;
c11d79f2
KB
68
69/* in AIX 5.1, functions use pthdb_tid_t instead of tid_t */
70#ifndef PTHDB_VERSION_3
71#define pthdb_tid_t tid_t
72#endif
73
74/* Return whether to treat PID as a debuggable thread id. */
75
76#define PD_TID(ptid) (pd_active && ptid_get_tid (ptid) != 0)
77
78/* Build a thread ptid. */
79#define BUILD_THREAD(TID, PID) ptid_build (PID, 0, TID)
80
81/* Build and lwp ptid. */
82#define BUILD_LWP(LWP, PID) MERGEPID (PID, LWP)
83
c11d79f2
KB
84/* pthdb_user_t value that we pass to pthdb functions. 0 causes
85 PTHDB_BAD_USER errors, so use 1. */
86
87#define PD_USER 1
88
89/* Success and failure values returned by pthdb callbacks. */
90
91#define PDC_SUCCESS PTHDB_SUCCESS
92#define PDC_FAILURE PTHDB_CALLBACK
93
c11d79f2
KB
94/* Private data attached to each element in GDB's thread list. */
95
96struct private_thread_info {
97 pthdb_pthread_t pdtid; /* thread's libpthdebug id */
98 pthdb_tid_t tid; /* kernel thread id */
99};
100
101/* Information about a thread of which libpthdebug is aware. */
102
103struct pd_thread {
104 pthdb_pthread_t pdtid;
105 pthread_t pthid;
106 pthdb_tid_t tid;
107};
108
109/* This module's target-specific operations, active while pd_able is true. */
110
111static struct target_ops ops;
112
113/* Copy of the target over which ops is pushed. This is
114 more convenient than a pointer to child_ops or core_ops, because
115 they lack current_target's default callbacks. */
116
117static struct target_ops base_ops;
118
119/* Address of the function that libpthread will call when libpthdebug is
120 ready to be initialized. */
121
122static CORE_ADDR pd_brk_addr;
123
124/* Whether the current application is debuggable by pthdb. */
125
126static int pd_able = 0;
127
128/* Whether a threaded application is being debugged. */
129
130static int pd_active = 0;
131
132/* Whether the current architecture is 64-bit. Only valid when pd_able is
133 true. */
134
135static int arch64;
136
137/* Saved pointer to previous owner of target_new_objfile_hook. */
138
139static void (*target_new_objfile_chain)(struct objfile *);
140
141/* Forward declarations for pthdb callbacks. */
142
143static int pdc_symbol_addrs (pthdb_user_t, pthdb_symbol_t *, int);
144static int pdc_read_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
145static int pdc_write_data (pthdb_user_t, void *, pthdb_addr_t, size_t);
146static int pdc_read_regs (pthdb_user_t user, pthdb_tid_t tid,
147 unsigned long long flags, pthdb_context_t *context);
148static int pdc_write_regs (pthdb_user_t user, pthdb_tid_t tid,
149 unsigned long long flags, pthdb_context_t *context);
150static int pdc_alloc (pthdb_user_t, size_t, void **);
151static int pdc_realloc (pthdb_user_t, void *, size_t, void **);
152static int pdc_dealloc (pthdb_user_t, void *);
153
154/* pthdb callbacks. */
155
156static pthdb_callbacks_t pd_callbacks = {
157 pdc_symbol_addrs,
158 pdc_read_data,
159 pdc_write_data,
160 pdc_read_regs,
161 pdc_write_regs,
162 pdc_alloc,
163 pdc_realloc,
164 pdc_dealloc,
165 NULL
166};
167
168/* Current pthdb session. */
169
170static pthdb_session_t pd_session;
171
c11d79f2
KB
172/* Return a printable representation of pthdebug function return STATUS. */
173
174static char *
175pd_status2str (int status)
176{
177 switch (status)
178 {
179 case PTHDB_SUCCESS: return "SUCCESS";
180 case PTHDB_NOSYS: return "NOSYS";
181 case PTHDB_NOTSUP: return "NOTSUP";
182 case PTHDB_BAD_VERSION: return "BAD_VERSION";
183 case PTHDB_BAD_USER: return "BAD_USER";
184 case PTHDB_BAD_SESSION: return "BAD_SESSION";
185 case PTHDB_BAD_MODE: return "BAD_MODE";
186 case PTHDB_BAD_FLAGS: return "BAD_FLAGS";
187 case PTHDB_BAD_CALLBACK: return "BAD_CALLBACK";
188 case PTHDB_BAD_POINTER: return "BAD_POINTER";
189 case PTHDB_BAD_CMD: return "BAD_CMD";
190 case PTHDB_BAD_PTHREAD: return "BAD_PTHREAD";
191 case PTHDB_BAD_ATTR: return "BAD_ATTR";
192 case PTHDB_BAD_MUTEX: return "BAD_MUTEX";
193 case PTHDB_BAD_MUTEXATTR: return "BAD_MUTEXATTR";
194 case PTHDB_BAD_COND: return "BAD_COND";
195 case PTHDB_BAD_CONDATTR: return "BAD_CONDATTR";
196 case PTHDB_BAD_RWLOCK: return "BAD_RWLOCK";
197 case PTHDB_BAD_RWLOCKATTR: return "BAD_RWLOCKATTR";
198 case PTHDB_BAD_KEY: return "BAD_KEY";
199 case PTHDB_BAD_PTID: return "BAD_PTID";
200 case PTHDB_BAD_TID: return "BAD_TID";
201 case PTHDB_CALLBACK: return "CALLBACK";
202 case PTHDB_CONTEXT: return "CONTEXT";
203 case PTHDB_HELD: return "HELD";
204 case PTHDB_NOT_HELD: return "NOT_HELD";
205 case PTHDB_MEMORY: return "MEMORY";
206 case PTHDB_NOT_PTHREADED: return "NOT_PTHREADED";
207 case PTHDB_SYMBOL: return "SYMBOL";
208 case PTHDB_NOT_AVAIL: return "NOT_AVAIL";
209 case PTHDB_INTERNAL: return "INTERNAL";
210 default: return "UNKNOWN";
211 }
212}
213
214/* A call to ptrace(REQ, ID, ...) just returned RET. Check for exceptional
215 conditions and either return nonlocally or else return 1 for success and 0
216 for failure. */
217
218static int
219ptrace_check (int req, int id, int ret)
220{
221 if (ret == 0 && !errno)
222 return 1;
223
224 /* According to ptrace(2), ptrace may fail with EPERM if "the Identifier
225 parameter corresponds to a kernel thread which is stopped in kernel mode
226 and whose computational state cannot be read or written." This happens
227 quite often with register reads. */
228
229 switch (req)
230 {
231 case PTT_READ_GPRS:
232 case PTT_READ_FPRS:
233 case PTT_READ_SPRS:
234 if (ret == -1 && errno == EPERM)
42cc437f
KB
235 {
236 if (debug_aix_thread)
237 fprintf_unfiltered (gdb_stdlog, "ptrace (%d, %d) = %d (errno = %d)",
238 req, id, ret, errno);
239 return ret == -1 ? 0 : 1;
240 }
c11d79f2
KB
241 break;
242 }
243 error ("aix-thread: ptrace (%d, %d) returned %d (errno = %d %s)",
244 req, id, ret, errno, strerror (errno));
42cc437f 245 return 0; /* not reached. */
c11d79f2
KB
246}
247
248/* Call ptracex(REQ, ID, ADDR, DATA, BUF). Return success. */
249
250static int
251ptrace64aix (int req, int id, long long addr, int data, int *buf)
252{
253 errno = 0;
254 return ptrace_check (req, id, ptracex (req, id, addr, data, buf));
255}
256
257/* Call ptrace(REQ, ID, ADDR, DATA, BUF). Return success. */
258
259static int
260ptrace32 (int req, int id, int *addr, int data, int *buf)
261{
262 errno = 0;
263 return ptrace_check (req, id, ptrace (req, id, (int *)addr, data, buf));
264}
265
266/* If *PIDP is a composite process/thread id, convert it to a process id. */
267
268static void
269pid_to_prc (ptid_t *ptidp)
270{
271 ptid_t ptid;
272
273 ptid = *ptidp;
274 if (PD_TID (ptid))
275 *ptidp = pid_to_ptid (PIDGET (ptid));
276}
277
278/* pthdb callback: for <i> from 0 to COUNT, set SYMBOLS[<i>].addr to the
279 address of SYMBOLS[<i>].name. */
280
281static int
282pdc_symbol_addrs (pthdb_user_t user, pthdb_symbol_t *symbols, int count)
283{
284 struct minimal_symbol *ms;
285 int i;
286 char *name;
287
8e2c28d4
KB
288 if (debug_aix_thread)
289 fprintf_unfiltered (gdb_stdlog,
290 "pdc_symbol_addrs (user = %ld, symbols = 0x%lx, count = %d)",
291 user, (long) symbols, count);
c11d79f2
KB
292
293 for (i = 0; i < count; i++)
294 {
295 name = symbols[i].name;
8e2c28d4
KB
296 if (debug_aix_thread)
297 fprintf_unfiltered (gdb_stdlog, " symbols[%d].name = \"%s\"", i, name);
c11d79f2
KB
298
299 if (!*name)
300 symbols[i].addr = 0;
301 else
302 {
303 if (!(ms = lookup_minimal_symbol (name, NULL, NULL)))
304 {
8e2c28d4
KB
305 if (debug_aix_thread)
306 fprintf_unfiltered (gdb_stdlog, " returning PDC_FAILURE");
c11d79f2
KB
307 return PDC_FAILURE;
308 }
309 symbols[i].addr = SYMBOL_VALUE_ADDRESS (ms);
310 }
8e2c28d4
KB
311 if (debug_aix_thread)
312 fprintf_unfiltered (gdb_stdlog, " symbols[%d].addr = 0x%llx",
313 i, symbols[i].addr);
c11d79f2 314 }
8e2c28d4
KB
315 if (debug_aix_thread)
316 fprintf_unfiltered (gdb_stdlog, " returning PDC_SUCCESS");
c11d79f2
KB
317 return PDC_SUCCESS;
318}
319
320/* Read registers call back function should be able to read the context */
321/* information of a debuggee kernel thread from an active process or from */
322/* a core file. The information should be formatted in context64 form for */
323/* both 32-bit and 64-bit process. If successful return 0, else non-zero */
324/* is returned. */
325static int
326pdc_read_regs (pthdb_user_t user,
327 pthdb_tid_t tid,
328 unsigned long long flags,
329 pthdb_context_t *context)
330{
331 /* this function doesn't appear to be used, so we could probably just */
332 /* return 0 here. HOWEVER, if it is not defined, the OS will complain */
333 /* and several thread debug functions will fail. In case this is needed, */
334 /* I have implemented what I think it should do, however this code is */
335 /* untested. */
336 uint64_t gprs64[32];
337 uint32_t gprs32[32];
338 double fprs[32];
339 struct ptxsprs sprs64;
340 struct ptsprs sprs32;
341
8e2c28d4
KB
342 if (debug_aix_thread)
343 fprintf_unfiltered (gdb_stdlog, "pdc_read_regs tid=%d flags=%llx\n",
344 (int)tid, flags);
c11d79f2
KB
345
346 /* General-purpose registers. */
347 if (flags & PTHDB_FLAG_GPRS)
348 {
349 if (arch64)
350 {
351 if (!ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL))
352 memset (gprs64, 0, sizeof (gprs64));
353 memcpy (context->gpr, gprs64, sizeof(gprs64));
354 }
355 else
356 {
357 if (!ptrace32 (PTT_READ_GPRS, tid, gprs32, 0, NULL))
358 memset (gprs32, 0, sizeof (gprs32));
359 memcpy (context->gpr, gprs32, sizeof(gprs32));
360 }
361 }
362
363 /* Floating-point registers. */
364 if (flags & PTHDB_FLAG_FPRS)
365 {
366 if (!ptrace32 (PTT_READ_FPRS, tid, (int *) fprs, 0, NULL))
367 memset (fprs, 0, sizeof (fprs));
368 memcpy (context->fpr, fprs, sizeof(fprs));
369 }
370
371 /* Special-purpose registers. */
372 if (flags & PTHDB_FLAG_SPRS)
373 {
374 if (arch64)
375 {
376 if (!ptrace64aix (PTT_READ_SPRS, tid, (unsigned long) &sprs64, 0, NULL))
377 memset (&sprs64, 0, sizeof (sprs64));
378 memcpy (&context->msr, &sprs64, sizeof(sprs64));
379 }
380 else
381 {
382 if (!ptrace32 (PTT_READ_SPRS, tid, (int *) &sprs32, 0, NULL))
383 memset (&sprs32, 0, sizeof (sprs32));
384 memcpy (&context->msr, &sprs32, sizeof(sprs32));
385 }
386 }
387 return 0;
388}
389
390/* Write register function should be able to write requested context */
391/* information to specified debuggee's kernel thread id. If successful */
392/* return 0, else non-zero is returned. */
393static int
394pdc_write_regs (pthdb_user_t user,
395 pthdb_tid_t tid,
396 unsigned long long flags,
397 pthdb_context_t *context)
398{
399 /* this function doesn't appear to be used, so we could probably just */
400 /* return 0 here. HOWEVER, if it is not defined, the OS will complain */
401 /* and several thread debug functions will fail. In case this is needed, */
402 /* I have implemented what I think it should do, however this code is */
403 /* untested. */
404
8e2c28d4
KB
405 if (debug_aix_thread)
406 fprintf_unfiltered (gdb_stdlog, "pdc_write_regs tid=%d flags=%llx\n",
407 (int)tid, flags);
c11d79f2
KB
408
409 /* General-purpose registers. */
410 if (flags & PTHDB_FLAG_GPRS)
411 {
412 if (arch64)
413 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long)context->gpr, 0, NULL);
414 else
415 ptrace32 (PTT_WRITE_GPRS, tid, (int *)context->gpr, 0, NULL);
416 }
417
418 /* Floating-point registers. */
419 if (flags & PTHDB_FLAG_FPRS)
420 {
421 ptrace32 (PTT_WRITE_FPRS, tid, (int *)context->fpr, 0, NULL);
422 }
423
424 /* Special-purpose registers. */
425 if (flags & PTHDB_FLAG_SPRS)
426 {
427 if (arch64)
428 {
429 ptrace64aix (PTT_WRITE_SPRS, tid, (unsigned long) &context->msr, 0, NULL);
430 }
431 else
432 {
433 ptrace32 (PTT_WRITE_SPRS, tid, (int *)&context->msr, 0, NULL);
434 }
435 }
436 return 0;
437}
438
439/* pthdb callback: read LEN bytes from process ADDR into BUF. */
440
441static int
442pdc_read_data (pthdb_user_t user, void *buf, pthdb_addr_t addr, size_t len)
443{
444 int status, ret;
445
8e2c28d4
KB
446 if (debug_aix_thread)
447 fprintf_unfiltered (gdb_stdlog,
448 "pdc_read_data (user = %ld, buf = 0x%lx, addr = 0x%llx, len = %ld)",
449 user, (long) buf, addr, len);
c11d79f2
KB
450
451 status = target_read_memory (addr, buf, len);
452 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
453
8e2c28d4
KB
454 if (debug_aix_thread)
455 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s", status,
456 pd_status2str (ret));
c11d79f2
KB
457 return ret;
458}
459
460/* pthdb callback: write LEN bytes from BUF to process ADDR. */
461
462static int
463pdc_write_data (pthdb_user_t user, void *buf, pthdb_addr_t addr, size_t len)
464{
465 int status, ret;
466
8e2c28d4
KB
467 if (debug_aix_thread)
468 fprintf_unfiltered (gdb_stdlog,
469 "pdc_write_data (user = %ld, buf = 0x%lx, addr = 0x%llx, len = %ld)",
470 user, (long) buf, addr, len);
c11d79f2
KB
471
472 status = target_write_memory (addr, buf, len);
473 ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE;
474
8e2c28d4
KB
475 if (debug_aix_thread)
476 fprintf_unfiltered (gdb_stdlog, " status=%d, returning %s", status,
477 pd_status2str (ret));
c11d79f2
KB
478 return ret;
479}
480
481/* pthdb callback: allocate a LEN-byte buffer and store a pointer to it in
482 BUFP. */
483
484static int
485pdc_alloc (pthdb_user_t user, size_t len, void **bufp)
486{
8e2c28d4
KB
487 if (debug_aix_thread)
488 fprintf_unfiltered (gdb_stdlog,
489 "pdc_alloc (user = %ld, len = %ld, bufp = 0x%lx)",
490 user, len, (long) bufp);
c11d79f2 491 *bufp = xmalloc (len);
8e2c28d4
KB
492 if (debug_aix_thread)
493 fprintf_unfiltered (gdb_stdlog, " malloc returned 0x%lx", (long) *bufp);
c11d79f2
KB
494 /* Note: xmalloc() can't return 0; therefore PDC_FAILURE will never be
495 returned. */
496 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
497}
498
499/* pthdb callback: reallocate BUF, which was allocated by the alloc or realloc
500 callback, so that it contains LEN bytes, and store a pointer to the result
501 in BUFP. */
502
503static int
504pdc_realloc (pthdb_user_t user, void *buf, size_t len, void **bufp)
505{
8e2c28d4
KB
506 if (debug_aix_thread)
507 fprintf_unfiltered (gdb_stdlog,
508 "pdc_realloc (user = %ld, buf = 0x%lx, len = %ld, bufp = 0x%lx)",
509 user, (long) buf, len, (long) bufp);
c11d79f2 510 *bufp = realloc (buf, len);
8e2c28d4
KB
511 if (debug_aix_thread)
512 fprintf_unfiltered (gdb_stdlog, " realloc returned 0x%lx", (long) *bufp);
c11d79f2
KB
513 return *bufp ? PDC_SUCCESS : PDC_FAILURE;
514}
515
516/* pthdb callback: free BUF, which was allocated by the alloc or realloc
517 callback. */
518
519static int
520pdc_dealloc (pthdb_user_t user, void *buf)
521{
8e2c28d4
KB
522 if (debug_aix_thread)
523 fprintf_unfiltered (gdb_stdlog, "pdc_free (user = %ld, buf = 0x%lx)", user,
524 (long) buf);
c11d79f2
KB
525 xfree (buf);
526 return PDC_SUCCESS;
527}
528
529/* Return a printable representation of pthread STATE. */
530
531static char *
532state2str (pthdb_state_t state)
533{
534 switch (state)
535 {
536 case PST_IDLE: return "idle"; /* being created */
537 case PST_RUN: return "running"; /* running */
538 case PST_SLEEP: return "sleeping"; /* awaiting an event */
539 case PST_READY: return "ready"; /* runnable */
540 case PST_TERM: return "finished"; /* awaiting a join/detach */
541 default: return "unknown";
542 }
543}
544
545/* qsort() comparison function for sorting pd_thread structs by pthid. */
546
547static int
548pcmp (const void *p1v, const void *p2v)
549{
550 struct pd_thread *p1 = (struct pd_thread *) p1v;
551 struct pd_thread *p2 = (struct pd_thread *) p2v;
552 return p1->pthid < p2->pthid ? -1 : p1->pthid > p2->pthid;
553}
554
555/* iterate_over_threads() callback for counting GDB threads. */
556
557static int
558giter_count (struct thread_info *thread, void *countp)
559{
560 (*(int *) countp)++;
561 return 0;
562}
563
564/* iterate_over_threads() callback for accumulating GDB thread pids. */
565
566static int
567giter_accum (struct thread_info *thread, void *bufp)
568{
569 **(struct thread_info ***) bufp = thread;
570 (*(struct thread_info ***) bufp)++;
571 return 0;
572}
573
574/* ptid comparison function */
575static int
576ptid_cmp (ptid_t ptid1, ptid_t ptid2)
577{
578 int pid1, pid2;
579
580 if (ptid_get_pid (ptid1) < ptid_get_pid (ptid2))
581 return -1;
582 else if (ptid_get_pid (ptid1) > ptid_get_pid (ptid2))
583 return 1;
584 else if (ptid_get_tid (ptid1) < ptid_get_tid (ptid2))
585 return -1;
586 else if (ptid_get_tid (ptid1) > ptid_get_tid (ptid2))
587 return 1;
588 else if (ptid_get_lwp (ptid1) < ptid_get_lwp (ptid2))
589 return -1;
590 else if (ptid_get_lwp (ptid1) > ptid_get_lwp (ptid2))
591 return 1;
592 else
593 return 0;
594}
595
596/* qsort() comparison function for sorting thread_info structs by pid. */
597
598static int
599gcmp (const void *t1v, const void *t2v)
600{
601 struct thread_info *t1 = *(struct thread_info **) t1v;
602 struct thread_info *t2 = *(struct thread_info **) t2v;
603 return ptid_cmp (t1->ptid, t2->ptid);
604}
605
606/* Synchronize GDB's thread list with libpthdebug's.
607
608 There are some benefits of doing this every time the inferior stops:
609
610 - allows users to run thread-specific commands without needing to run
611 "info threads" first
612
613 - helps pthdb_tid_pthread() work properly (see "libpthdebug
614 peculiarities" at the top of this module)
615
616 - simplifies the demands placed on libpthdebug, which seems to have
617 difficulty with certain call patterns */
618
619static void
620sync_threadlists (void)
621{
622 int cmd, status, infpid;
623 int pcount, psize, pi, gcount, gi;
624 struct pd_thread *pbuf;
625 struct thread_info **gbuf, **g, *thread;
626 pthdb_pthread_t pdtid;
627 pthread_t pthid;
628 pthdb_tid_t tid;
c11d79f2
KB
629
630 /* Accumulate an array of libpthdebug threads sorted by pthread id. */
631
632 pcount = 0;
633 psize = 1;
634 pbuf = (struct pd_thread *) xmalloc (psize * sizeof *pbuf);
635
636 for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT)
637 {
638 status = pthdb_pthread (pd_session, &pdtid, cmd);
639 if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD)
640 break;
641
642 status = pthdb_pthread_ptid (pd_session, pdtid, &pthid);
643 if (status != PTHDB_SUCCESS || pthid == PTHDB_INVALID_PTID)
644 continue;
645
646 if (pcount == psize)
647 {
648 psize *= 2;
649 pbuf = (struct pd_thread *) xrealloc (pbuf, psize * sizeof *pbuf);
650 }
651 pbuf[pcount].pdtid = pdtid;
652 pbuf[pcount].pthid = pthid;
653 pcount++;
654 }
655
656 for (pi = 0; pi < pcount; pi++)
657 {
658 status = pthdb_pthread_tid (pd_session, pbuf[pi].pdtid, &tid);
659 if (status != PTHDB_SUCCESS)
660 tid = PTHDB_INVALID_TID;
661 pbuf[pi].tid = tid;
662 }
663
664 qsort (pbuf, pcount, sizeof *pbuf, pcmp);
665
666 /* Accumulate an array of GDB threads sorted by pid. */
667
668 gcount = 0;
669 iterate_over_threads (giter_count, &gcount);
670 g = gbuf = (struct thread_info **) xmalloc (gcount * sizeof *gbuf);
671 iterate_over_threads (giter_accum, &g);
672 qsort (gbuf, gcount, sizeof *gbuf, gcmp);
673
674 /* Apply differences between the two arrays to GDB's thread list. */
675
676 infpid = PIDGET (inferior_ptid);
677 for (pi = gi = 0; pi < pcount || gi < gcount;)
678 {
c11d79f2 679 if (pi == pcount)
c11d79f2 680 {
42cc437f 681 delete_thread (gbuf[gi]->ptid);
c11d79f2
KB
682 gi++;
683 }
42cc437f 684 else if (gi == gcount)
c11d79f2 685 {
42cc437f 686 thread = add_thread (BUILD_THREAD (pbuf[pi].pthid, infpid));
c11d79f2 687 thread->private = xmalloc (sizeof (struct private_thread_info));
42cc437f
KB
688 thread->private->pdtid = pbuf[pi].pdtid;
689 thread->private->tid = pbuf[pi].tid;
c11d79f2
KB
690 pi++;
691 }
42cc437f
KB
692 else
693 {
694 ptid_t pptid, gptid;
695 int cmp_result;
696
697 pptid = BUILD_THREAD (pbuf[pi].pthid, infpid);
698 gptid = gbuf[gi]->ptid;
699 pdtid = pbuf[pi].pdtid;
700 tid = pbuf[pi].tid;
c11d79f2 701
42cc437f
KB
702 cmp_result = ptid_cmp (pptid, gptid);
703
704 if (cmp_result == 0)
705 {
706 gbuf[gi]->private->pdtid = pdtid;
707 gbuf[gi]->private->tid = tid;
708 pi++;
709 gi++;
710 }
711 else if (cmp_result > 0)
712 {
713 delete_thread (gptid);
714 gi++;
715 }
716 else
717 {
718 thread = add_thread (pptid);
719 thread->private = xmalloc (sizeof (struct private_thread_info));
720 thread->private->pdtid = pdtid;
721 thread->private->tid = tid;
722 pi++;
723 }
724 }
c11d79f2
KB
725 }
726
727 xfree (pbuf);
728 xfree (gbuf);
729}
730
731/* iterate_over_threads() callback for locating a thread whose kernel thread
732 just received a trap signal. */
733
734static int
735iter_trap (struct thread_info *thread, void *unused)
736{
737 struct thrdsinfo64 thrinf;
738 pthdb_tid_t tid;
739
740 /* getthrds(3) isn't prototyped in any AIX 4.3.3 #include file. */
741 extern int getthrds (pid_t, struct thrdsinfo64 *, int, pthdb_tid_t *, int);
742
743 tid = thread->private->tid;
744 if (tid == PTHDB_INVALID_TID)
745 return 0;
746
747 if (getthrds (PIDGET (inferior_ptid), &thrinf, sizeof (thrinf), &tid, 1) != 1)
748 return 0;
749
750 return thrinf.ti_cursig == SIGTRAP;
751}
752
753/* Synchronize libpthdebug's state with the inferior and with GDB, generate a
754 composite process/thread <pid> for the current thread, set inferior_ptid to
755 <pid> if SET_INFPID, and return <pid>. */
756
757static ptid_t
758pd_update (int set_infpid)
759{
760 int status;
761 ptid_t ptid;
762 struct thread_info *thread;
763
764 if (!pd_active)
765 return inferior_ptid;
766
767 status = pthdb_session_update (pd_session);
768 if (status != PTHDB_SUCCESS)
769 return inferior_ptid;
770
771 sync_threadlists ();
772
773 /* Define "current thread" as one that just received a trap signal. */
774
775 thread = iterate_over_threads (iter_trap, NULL);
776 if (!thread)
777 ptid = inferior_ptid;
778 else
779 {
780 ptid = thread->ptid;
781 if (set_infpid)
782 inferior_ptid = ptid;
783 }
784 return ptid;
785}
786
787/* Try to start debugging threads in the current process. If successful and
788 SET_INFPID, set inferior_ptid to reflect the current thread. */
789
790static ptid_t
791pd_activate (int set_infpid)
792{
793 int status;
794
795 status = pthdb_session_init (PD_USER, arch64 ? PEM_64BIT : PEM_32BIT,
796 PTHDB_FLAG_REGS, &pd_callbacks, &pd_session);
797 if (status != PTHDB_SUCCESS)
798 {
799 return inferior_ptid;
800 }
801 pd_active = 1;
802 return pd_update (set_infpid);
803}
804
805/* Undo the effects of pd_activate(). */
806
807static void
808pd_deactivate (void)
809{
810 if (!pd_active)
811 return;
812 pthdb_session_destroy (pd_session);
813
814 pid_to_prc (&inferior_ptid);
815 pd_active = 0;
816}
817
818/* An object file has just been loaded. Check whether the current application
819 is pthreaded, and if so, prepare for thread debugging. */
820
821static void
822pd_enable (void)
823{
824 int status;
825 char *stub_name;
826 struct minimal_symbol *ms;
827
828 /* Don't initialize twice. */
829 if (pd_able)
830 return;
831
832 /* Check application word size. */
833 arch64 = REGISTER_RAW_SIZE (0) == 8;
834
835 /* Check whether the application is pthreaded. */
836 stub_name = NULL;
837 status = pthdb_session_pthreaded (PD_USER, PTHDB_FLAG_REGS, &pd_callbacks,
838 &stub_name);
839 if ((status != PTHDB_SUCCESS && status != PTHDB_NOT_PTHREADED) || !stub_name)
840 return;
841
842 /* Set a breakpoint on the returned stub function. */
843 if (!(ms = lookup_minimal_symbol (stub_name, NULL, NULL)))
844 return;
845 pd_brk_addr = SYMBOL_VALUE_ADDRESS (ms);
846 if (!create_thread_event_breakpoint (pd_brk_addr))
847 return;
848
849 /* Prepare for thread debugging. */
850 base_ops = current_target;
851 push_target (&ops);
852 pd_able = 1;
853
854 /* If we're debugging a core file or an attached inferior, the pthread
855 library may already have been initialized, so try to activate thread
856 debugging. */
857 pd_activate (1);
858}
859
860/* Undo the effects of pd_enable(). */
861
862static void
863pd_disable (void)
864{
865 if (!pd_able)
866 return;
867 if (pd_active)
868 pd_deactivate ();
869 pd_able = 0;
870 unpush_target (&ops);
871}
872
873/* target_new_objfile_hook callback.
874
875 If OBJFILE is non-null, check whether a threaded application is being
876 debugged, and if so, prepare for thread debugging.
877
878 If OBJFILE is null, stop debugging threads. */
879
880static void
881new_objfile (struct objfile *objfile)
882{
883 if (objfile)
884 pd_enable ();
885 else
886 pd_disable ();
887
888 if (target_new_objfile_chain)
889 target_new_objfile_chain (objfile);
890}
891
892/* Attach to process specified by ARGS. */
893
894static void
895ops_attach (char *args, int from_tty)
896{
897 base_ops.to_attach (args, from_tty);
898 pd_activate (1);
899}
900
901/* Detach from the process attached to by ops_attach(). */
902
903static void
904ops_detach (char *args, int from_tty)
905{
906 pd_deactivate ();
907 base_ops.to_detach (args, from_tty);
908}
909
910/* Tell the inferior process to continue running thread PID if != -1
911 and all threads otherwise. */
912
913static void
914ops_resume (ptid_t ptid, int step, enum target_signal sig)
915{
916 struct thread_info *thread;
917 pthdb_tid_t tid[2];
918
919 if (!PD_TID (ptid))
14fa3751
KB
920 {
921 struct cleanup *cleanup = save_inferior_ptid ();
922 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
923 base_ops.to_resume (ptid, step, sig);
924 do_cleanups (cleanup);
925 }
c11d79f2
KB
926 else
927 {
928 thread = find_thread_pid (ptid);
929 if (!thread)
930 error ("aix-thread resume: unknown pthread %ld", TIDGET (ptid));
931
932 tid[0] = thread->private->tid;
933 if (tid[0] == PTHDB_INVALID_TID)
934 error ("aix-thread resume: no tid for pthread %ld", TIDGET (ptid));
935 tid[1] = 0;
936
937 if (arch64)
938 ptrace64aix (PTT_CONTINUE, tid[0], 1, target_signal_to_host (sig), (int *)tid);
939 else
940 ptrace32 (PTT_CONTINUE, tid[0], (int *) 1,
941 target_signal_to_host (sig), (int *)tid);
942 }
943}
944
945/* Wait for thread/process ID if != -1 or for any thread otherwise. If an
946 error occurs, return -1, else return the pid of the stopped thread. */
947
948static ptid_t
949ops_wait (ptid_t ptid, struct target_waitstatus *status)
950{
14fa3751
KB
951 struct cleanup *cleanup = save_inferior_ptid ();
952
c11d79f2 953 pid_to_prc (&ptid);
14fa3751
KB
954
955 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
956 ptid = base_ops.to_wait (ptid, status);
957 do_cleanups (cleanup);
958
c11d79f2
KB
959 if (PIDGET (ptid) == -1)
960 return pid_to_ptid (-1);
961
962 /* Check whether libpthdebug might be ready to be initialized. */
963 if (!pd_active && status->kind == TARGET_WAITKIND_STOPPED &&
964 status->value.sig == TARGET_SIGNAL_TRAP &&
965 read_pc_pid (ptid) - DECR_PC_AFTER_BREAK == pd_brk_addr)
966 return pd_activate (0);
967
968 return pd_update (0);
969}
970
971/* Record that the 64-bit general-purpose registers contain VALS. */
972
973static void
974supply_gprs64 (uint64_t *vals)
975{
976 int regno;
977
978 for (regno = 0; regno < 32; regno++)
979 supply_register (regno, (char *) (vals + regno));
980}
981
982/* Record that 32-bit register REGNO contains VAL. */
983
984static void
985supply_reg32 (int regno, uint32_t val)
986{
987 supply_register (regno, (char *) &val);
988}
989
990/* Record that the floating-point registers contain VALS. */
991
992static void
993supply_fprs (double *vals)
994{
995 int regno;
996
997 for (regno = 0; regno < 32; regno++)
998 supply_register (regno + FP0_REGNUM, (char *) (vals + regno));
999}
1000
1001/* Record that the special registers contain the specified 64-bit and 32-bit
1002 values. */
1003
1004static void
1005supply_sprs64 (uint64_t iar, uint64_t msr, uint32_t cr,
1006 uint64_t lr, uint64_t ctr, uint32_t xer)
1007{
1008 int regno = FIRST_UISA_SP_REGNUM;
1009 supply_register (regno, (char *) &iar);
1010 supply_register (regno + 1, (char *) &msr);
1011 supply_register (regno + 2, (char *) &cr);
1012 supply_register (regno + 3, (char *) &lr);
1013 supply_register (regno + 4, (char *) &ctr);
1014 supply_register (regno + 5, (char *) &xer);
1015}
1016
1017/* Record that the special registers contain the specified 32-bit values. */
1018
1019static void
1020supply_sprs32 (uint32_t iar, uint32_t msr, uint32_t cr,
1021 uint32_t lr, uint32_t ctr, uint32_t xer)
1022{
1023 int regno = FIRST_UISA_SP_REGNUM;
1024 supply_register (regno, (char *) &iar);
1025 supply_register (regno + 1, (char *) &msr);
1026 supply_register (regno + 2, (char *) &cr);
1027 supply_register (regno + 3, (char *) &lr);
1028 supply_register (regno + 4, (char *) &ctr);
1029 supply_register (regno + 5, (char *) &xer);
1030}
1031
1032/* Fetch all registers from pthread PDTID, which doesn't have a kernel
1033 thread.
1034
1035 There's no way to query a single register from a non-kernel pthread,
1036 so there's no need for a single-register version of this function. */
1037
1038static void
1039fetch_regs_lib (pthdb_pthread_t pdtid)
1040{
1041 int status, i;
1042 pthdb_context_t ctx;
1043
8e2c28d4
KB
1044 if (debug_aix_thread)
1045 fprintf_unfiltered (gdb_stdlog, "fetch_regs_lib %lx\n", (long)pdtid);
c11d79f2
KB
1046 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1047 if (status != PTHDB_SUCCESS)
14fa3751
KB
1048 error ("aix-thread: fetch_registers: pthdb_pthread_context returned %s",
1049 pd_status2str (status));
c11d79f2
KB
1050
1051 /* General-purpose registers. */
1052
1053 if (arch64)
1054 supply_gprs64 (ctx.gpr);
1055 else
1056 for (i = 0; i < 32; i++)
1057 supply_reg32 (i, ctx.gpr[i]);
1058
1059 /* Floating-point registers. */
1060
1061 supply_fprs (ctx.fpr);
1062
1063 /* Special registers. */
1064
1065 if (arch64)
1066 supply_sprs64 (ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr, ctx.xer);
1067 else
1068 supply_sprs32 (ctx.iar, ctx.msr, ctx.cr, ctx.lr, ctx.ctr, ctx.xer);
1069}
1070
1071/* Fetch register REGNO if != -1 or all registers otherwise from kernel thread
1072 TID.
1073
1074 AIX provides a way to query all of a kernel thread's GPRs, FPRs, or SPRs,
1075 but there's no way to query individual registers within those groups.
1076 Therefore, if REGNO != -1, this function fetches an entire group.
1077
1078 Unfortunately, kernel thread register queries often fail with EPERM,
1079 indicating that the thread is in kernel space. This breaks backtraces of
1080 threads other than the current one. To make that breakage obvious without
1081 throwing an error to top level (which is bad e.g. during "info threads"
1082 output), zero registers that can't be retrieved. */
1083
1084static void
1085fetch_regs_kern (int regno, pthdb_tid_t tid)
1086{
1087 uint64_t gprs64[32];
1088 uint32_t gprs32[32];
1089 double fprs[32];
1090 struct ptxsprs sprs64;
1091 struct ptsprs sprs32;
1092 int i;
1093
8e2c28d4
KB
1094 if (debug_aix_thread)
1095 fprintf_unfiltered (gdb_stdlog,
1096 "fetch_regs_kern tid=%lx regno=%d arch64=%d\n",
1097 (long)tid, regno, arch64);
c11d79f2
KB
1098
1099 /* General-purpose registers. */
1100 if (regno == -1 || regno < FP0_REGNUM)
1101 {
1102 if (arch64)
1103 {
1104 if (!ptrace64aix (PTT_READ_GPRS, tid, (unsigned long) gprs64, 0, NULL))
1105 memset (gprs64, 0, sizeof (gprs64));
1106 supply_gprs64 (gprs64);
1107 }
1108 else
1109 {
1110 if (!ptrace32 (PTT_READ_GPRS, tid, gprs32, 0, NULL))
1111 memset (gprs32, 0, sizeof (gprs32));
1112 for (i = 0; i < 32; i++)
1113 supply_reg32 (i, gprs32[i]);
1114 }
1115 }
1116
1117 /* Floating-point registers. */
1118
1119 if (regno == -1 || (regno >= FP0_REGNUM && regno <= FPLAST_REGNUM))
1120 {
1121 if (!ptrace32 (PTT_READ_FPRS, tid, (int *) fprs, 0, NULL))
1122 memset (fprs, 0, sizeof (fprs));
1123 supply_fprs (fprs);
1124 }
1125
1126 /* Special-purpose registers. */
1127
1128 if (regno == -1 || (regno > FPLAST_REGNUM && regno <= LAST_UISA_SP_REGNUM))
1129 {
1130 if (arch64)
1131 {
1132 if (!ptrace64aix (PTT_READ_SPRS, tid, (unsigned long) &sprs64, 0, NULL))
1133 memset (&sprs64, 0, sizeof (sprs64));
1134 supply_sprs64 (sprs64.pt_iar, sprs64.pt_msr, sprs64.pt_cr,
1135 sprs64.pt_lr, sprs64.pt_ctr, sprs64.pt_xer);
1136 }
1137 else
1138 {
1139 if (!ptrace32 (PTT_READ_SPRS, tid, (int *) &sprs32, 0, NULL))
1140 memset (&sprs32, 0, sizeof (sprs32));
1141 supply_sprs32 (sprs32.pt_iar, sprs32.pt_msr, sprs32.pt_cr,
1142 sprs32.pt_lr, sprs32.pt_ctr, sprs32.pt_xer);
1143
1144 if (REGISTER_RAW_SIZE (LAST_UISA_SP_REGNUM))
1145 supply_register (LAST_UISA_SP_REGNUM, (char *) &sprs32.pt_mq);
1146 }
1147 }
1148}
1149
1150/* Fetch register REGNO if != -1 or all registers otherwise in the
1151 thread/process specified by inferior_ptid. */
1152
1153static void
1154ops_fetch_registers (int regno)
1155{
1156 struct thread_info *thread;
1157 pthdb_tid_t tid;
1158
1159 if (!PD_TID (inferior_ptid))
1160 base_ops.to_fetch_registers (regno);
1161 else
1162 {
1163 thread = find_thread_pid (inferior_ptid);
1164 tid = thread->private->tid;
1165
1166 if (tid == PTHDB_INVALID_TID)
1167 fetch_regs_lib (thread->private->pdtid);
1168 else
1169 fetch_regs_kern (regno, tid);
1170 }
1171}
1172
1173/* Store the special registers into the specified 64-bit and 32-bit
1174 locations. */
1175
1176static void
1177fill_sprs64 (uint64_t *iar, uint64_t *msr, uint32_t *cr,
1178 uint64_t *lr, uint64_t *ctr, uint32_t *xer)
1179{
1180 int regno = FIRST_UISA_SP_REGNUM;
1181 *iar = read_register (regno);
1182 *msr = read_register (regno + 1);
1183 *cr = read_register (regno + 2);
1184 *lr = read_register (regno + 3);
1185 *ctr = read_register (regno + 4);
1186 *xer = read_register (regno + 5);
1187}
1188
1189/* Store all registers into pthread PDTID, which doesn't have a kernel
1190 thread.
1191
1192 It's possible to store a single register into a non-kernel pthread, but I
1193 doubt it's worth the effort. */
1194
1195static void
1196store_regs_lib (pthdb_pthread_t pdtid)
1197{
1198 int status, i;
1199 pthdb_context_t ctx;
1200
8e2c28d4
KB
1201 if (debug_aix_thread)
1202 fprintf_unfiltered (gdb_stdlog, "store_regs_lib %lx\n", (long)pdtid);
c11d79f2
KB
1203
1204 /* Retrieve the thread's current context for its non-register values. */
1205 status = pthdb_pthread_context (pd_session, pdtid, &ctx);
1206 if (status != PTHDB_SUCCESS)
14fa3751
KB
1207 error ("aix-thread: store_registers: pthdb_pthread_context returned %s",
1208 pd_status2str (status));
c11d79f2
KB
1209
1210 /* General-purpose registers. */
1211
1212 for (i = 0; i < 32; i++)
1213 ctx.gpr[i] = read_register (i);
1214
1215 /* Floating-point registers. */
1216
1217 for (i = 0; i < 32; i++)
1218 ctx.fpr[i] = *(double *) &registers[REGISTER_BYTE (FP0_REGNUM + i)];
1219
1220 /* Special registers. */
1221
1222 fill_sprs64 (&ctx.iar, &ctx.msr, &ctx.cr, &ctx.lr, &ctx.ctr, &ctx.xer);
1223
1224 status = pthdb_pthread_setcontext (pd_session, pdtid, &ctx);
1225 if (status != PTHDB_SUCCESS)
14fa3751
KB
1226 error ("aix-thread: store_registers: pthdb_pthread_setcontext returned %s",
1227 pd_status2str (status));
c11d79f2
KB
1228}
1229
1230/* Store register REGNO if != -1 or all registers otherwise into kernel
1231 thread TID.
1232
1233 AIX provides a way to set all of a kernel thread's GPRs, FPRs, or SPRs, but
1234 there's no way to set individual registers within those groups. Therefore,
1235 if REGNO != -1, this function stores an entire group. */
1236
1237static void
1238store_regs_kern (int regno, pthdb_tid_t tid)
1239{
1240 struct ptxsprs sprs64;
1241 struct ptsprs sprs32;
1242 char *regp;
1243
8e2c28d4
KB
1244 if (debug_aix_thread)
1245 fprintf_unfiltered (gdb_stdlog, "store_regs_kern tid=%lx regno=%d\n",
1246 (long)tid, regno);
c11d79f2
KB
1247
1248 /* General-purpose registers. */
1249 if (regno == -1 || regno < FP0_REGNUM)
1250 {
1251 regp = &registers[REGISTER_BYTE (0)];
1252 if (arch64)
1253 ptrace64aix (PTT_WRITE_GPRS, tid, (unsigned long) regp, 0, NULL);
1254 else
1255 ptrace32 (PTT_WRITE_GPRS, tid, (int *) regp, 0, NULL);
1256 }
1257
1258 /* Floating-point registers. */
1259
1260 if (regno == -1 || (regno >= FP0_REGNUM && regno <= FPLAST_REGNUM))
1261 {
1262 regp = &registers[REGISTER_BYTE (FP0_REGNUM)];
1263 ptrace32 (PTT_WRITE_FPRS, tid, (int *) regp, 0, NULL);
1264 }
1265
1266 /* Special-purpose registers. */
1267
1268 if (regno == -1 || (regno > FPLAST_REGNUM && regno <= LAST_UISA_SP_REGNUM))
1269 {
1270 if (arch64)
1271 {
1272 ptrace64aix (PTT_READ_SPRS, tid, (unsigned long) &sprs64, 0, NULL);
1273 fill_sprs64 (&sprs64.pt_iar, &sprs64.pt_msr, &sprs64.pt_cr,
1274 &sprs64.pt_lr, &sprs64.pt_ctr, &sprs64.pt_xer);
1275 ptrace64aix (PTT_WRITE_SPRS, tid, (unsigned long) &sprs64, 0, NULL);
1276 }
1277 else
1278 {
1279 ptrace32 (PTT_READ_SPRS, tid, (int *) &sprs32, 0, NULL);
1280
1281 regno = FIRST_UISA_SP_REGNUM;
1282 sprs32.pt_iar = read_register (regno);
1283 sprs32.pt_msr = read_register (regno + 1);
1284 sprs32.pt_cr = read_register (regno + 2);
1285 sprs32.pt_lr = read_register (regno + 3);
1286 sprs32.pt_ctr = read_register (regno + 4);
1287 sprs32.pt_xer = read_register (regno + 5);
1288
1289 if (REGISTER_RAW_SIZE (LAST_UISA_SP_REGNUM))
1290 sprs32.pt_mq = read_register (LAST_UISA_SP_REGNUM);
1291
1292 ptrace32 (PTT_WRITE_SPRS, tid, (int *) &sprs32, 0, NULL);
1293 }
1294 }
1295}
1296
1297/* Store gdb's current view of the register set into the thread/process
1298 specified by inferior_ptid. */
1299
1300static void
1301ops_store_registers (int regno)
1302{
1303 struct thread_info *thread;
1304 pthdb_tid_t tid;
1305
1306 if (!PD_TID (inferior_ptid))
1307 base_ops.to_store_registers (regno);
1308 else
1309 {
1310 thread = find_thread_pid (inferior_ptid);
1311 tid = thread->private->tid;
1312
1313 if (tid == PTHDB_INVALID_TID)
1314 store_regs_lib (thread->private->pdtid);
1315 else
1316 store_regs_kern (regno, tid);
1317 }
1318}
1319
1320/* Prepare to modify the registers array. */
1321
1322static void
1323ops_prepare_to_store (void)
1324{
1325 if (!PD_TID (inferior_ptid))
1326 base_ops.to_prepare_to_store ();
1327 else
1328 read_register_bytes (0, NULL, REGISTER_BYTES);
1329}
1330
1331/* Transfer LEN bytes of memory from GDB address MYADDR to target address
1332 MEMADDR if WRITE and vice versa otherwise. */
1333
1334static int
1335ops_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
1336 struct mem_attrib *attrib,
1337 struct target_ops *target)
1338{
1339 int n;
14fa3751
KB
1340 struct cleanup *cleanup = save_inferior_ptid ();
1341
1342 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1343 n = base_ops.to_xfer_memory (memaddr, myaddr, len, write, attrib, &base_ops);
1344 do_cleanups (cleanup);
c11d79f2 1345
c11d79f2
KB
1346 return n;
1347}
1348
1349/* Kill and forget about the inferior process. */
1350
1351static void
1352ops_kill (void)
1353{
14fa3751
KB
1354 struct cleanup *cleanup = save_inferior_ptid ();
1355
1356 inferior_ptid = pid_to_ptid (PIDGET (inferior_ptid));
1357 base_ops.to_kill ();
1358 do_cleanups (cleanup);
c11d79f2
KB
1359}
1360
1361/* Clean up after the inferior exits. */
1362
1363static void
1364ops_mourn_inferior (void)
1365{
1366 pd_deactivate ();
1367 base_ops.to_mourn_inferior ();
1368}
1369
1370/* Return whether thread PID is still valid. */
1371
1372static int
1373ops_thread_alive (ptid_t ptid)
1374{
1375 if (!PD_TID (ptid))
1376 return base_ops.to_thread_alive (ptid);
1377
1378 /* We update the thread list every time the child stops, so all valid
1379 threads should be in the thread list. */
1380 return in_thread_list (ptid);
1381}
1382
1383/* Return a printable representation of composite PID for use in "info
1384 threads" output. */
1385
1386static char *
1387ops_pid_to_str (ptid_t ptid)
1388{
1389 static char *ret = NULL;
1390
1391 if (!PD_TID (ptid))
1392 return base_ops.to_pid_to_str (ptid);
1393
1394 /* Free previous return value; a new one will be allocated by
1395 xasprintf(). */
1396 xfree (ret);
1397
1398 xasprintf (&ret, "Thread %ld", ptid_get_tid (ptid));
1399 return ret;
1400}
1401
1402/* Return a printable representation of extra information about THREAD, for
1403 use in "info threads" output. */
1404
1405static char *
1406ops_extra_thread_info (struct thread_info *thread)
1407{
1408 struct ui_file *buf;
1409 int status;
1410 pthdb_pthread_t pdtid;
1411 pthdb_tid_t tid;
1412 pthdb_state_t state;
1413 pthdb_suspendstate_t suspendstate;
1414 pthdb_detachstate_t detachstate;
1415 int cancelpend;
1416 long length;
1417 static char *ret = NULL;
1418
1419 if (!PD_TID (thread->ptid))
1420 return NULL;
1421
1422 buf = mem_fileopen ();
1423
1424 pdtid = thread->private->pdtid;
1425 tid = thread->private->tid;
1426
1427 if (tid != PTHDB_INVALID_TID)
1428 fprintf_unfiltered (buf, "tid %d", tid);
1429
1430 status = pthdb_pthread_state (pd_session, pdtid, &state);
1431 if (status != PTHDB_SUCCESS)
1432 state = PST_NOTSUP;
1433 fprintf_unfiltered (buf, ", %s", state2str (state));
1434
1435 status = pthdb_pthread_suspendstate (pd_session, pdtid, &suspendstate);
1436 if (status == PTHDB_SUCCESS && suspendstate == PSS_SUSPENDED)
1437 fprintf_unfiltered (buf, ", suspended");
1438
1439 status = pthdb_pthread_detachstate (pd_session, pdtid, &detachstate);
1440 if (status == PTHDB_SUCCESS && detachstate == PDS_DETACHED)
1441 fprintf_unfiltered (buf, ", detached");
1442
1443 pthdb_pthread_cancelpend (pd_session, pdtid, &cancelpend);
1444 if (status == PTHDB_SUCCESS && cancelpend)
1445 fprintf_unfiltered (buf, ", cancel pending");
1446
1447 ui_file_write (buf, "", 1);
1448
1449 xfree (ret); /* Free old buffer. */
1450
1451 ret = ui_file_xstrdup (buf, &length);
1452 ui_file_delete (buf);
1453
1454 return ret;
1455}
1456
1457/* Initialize target ops. */
1458
1459static void
1460init_ops (void)
1461{
1462 ops.to_shortname = "aix-threads";
1463 ops.to_longname = "AIX pthread support";
1464 ops.to_doc = "AIX pthread support";
1465
1466 ops.to_attach = ops_attach;
1467 ops.to_detach = ops_detach;
1468 ops.to_resume = ops_resume;
1469 ops.to_wait = ops_wait;
1470 ops.to_fetch_registers = ops_fetch_registers;
1471 ops.to_store_registers = ops_store_registers;
1472 ops.to_prepare_to_store = ops_prepare_to_store;
1473 ops.to_xfer_memory = ops_xfer_memory;
1474 /* No need for ops.to_create_inferior, because we activate thread debugging
1475 when the inferior reaches pd_brk_addr. */
1476 ops.to_kill = ops_kill;
1477 ops.to_mourn_inferior = ops_mourn_inferior;
1478 ops.to_thread_alive = ops_thread_alive;
1479 ops.to_pid_to_str = ops_pid_to_str;
1480 ops.to_extra_thread_info = ops_extra_thread_info;
1481 ops.to_stratum = thread_stratum;
1482 ops.to_magic = OPS_MAGIC;
1483}
1484
1485/* Module startup initialization function, automagically called by
1486 init.c. */
1487
1488void
1489_initialize_aix_thread (void)
1490{
1491 init_ops ();
1492 add_target (&ops);
1493
1494 /* Notice when object files get loaded and unloaded. */
1495 target_new_objfile_chain = target_new_objfile_hook;
1496 target_new_objfile_hook = new_objfile;
8e2c28d4
KB
1497
1498 add_show_from_set (add_set_cmd ("aix-thread", no_class, var_zinteger,
1499 (char *) &debug_aix_thread,
1500 "Set debugging of AIX thread module.\n"
1501 "Enables printf debugging output.\n",
1502 &setdebuglist),
1503 &showdebuglist);
c11d79f2 1504}
This page took 0.139824 seconds and 4 git commands to generate.