aix-thread.c 53 KB

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