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