2007-09-04 Michael Snyder <msnyder@access-company.com>
[deliverable/binutils-gdb.git] / gdb / hpux-thread.c
CommitLineData
1df84f13
AC
1/* Low level interface for debugging HPUX/DCE threads for GDB, the GNU
2 debugger.
3
6aba47ca
DJ
4 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2004, 2007
5 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22/* This module implements a sort of half target that sits between the
23 machine-independent parts of GDB and the ptrace interface (infptrace.c) to
24 provide access to the HPUX user-mode thread implementation.
25
26 HPUX threads are true user-mode threads, which are invoked via the cma_*
27 and pthread_* (DCE and Posix respectivly) interfaces. These are mostly
28 implemented in user-space, with all thread context kept in various
29 structures that live in the user's heap. For the most part, the kernel has
30 no knowlege of these threads.
31
c5aa993b 32 */
c906108c
SS
33
34#include "defs.h"
35
36#define _CMA_NOWRAPPERS_
37
38#include <cma_tcb_defs.h>
39#include <cma_deb_core.h>
40#include "gdbthread.h"
41#include "target.h"
42#include "inferior.h"
4e052eda 43#include "regcache.h"
c906108c 44#include <fcntl.h>
3f6306ec 45#include <string.h>
b8a92b82 46#include "gdb_stat.h"
c906108c 47#include "gdbcore.h"
3f6306ec 48#include "hppa-tdep.h"
06d3b283 49#include "observer.h"
c906108c
SS
50
51extern int child_suppress_run;
c906108c 52
a14ed312 53extern void _initialize_hpux_thread (void);
c906108c
SS
54
55struct string_map
c5aa993b
JM
56 {
57 int num;
58 char *str;
59 };
c906108c
SS
60
61static int hpux_thread_active = 0;
62
39f77062 63static ptid_t main_ptid; /* Real process ID */
c906108c
SS
64
65static CORE_ADDR P_cma__g_known_threads;
66static CORE_ADDR P_cma__g_current_thread;
67
39f77062
KB
68static void hpux_thread_resume (ptid_t ptid, int step,
69 enum target_signal signo);
c906108c 70
a14ed312 71static void init_hpux_thread_ops (void);
c906108c
SS
72
73static struct target_ops hpux_thread_ops;
74\f
f7926acf 75static ptid_t find_active_thread (void);
c906108c
SS
76
77static int cached_thread;
c906108c
SS
78static cma__t_int_tcb cached_tcb;
79
f7926acf 80static ptid_t
fba45db2 81find_active_thread (void)
c906108c
SS
82{
83 static cma__t_int_tcb tcb;
84 CORE_ADDR tcb_ptr;
85
c5aa993b
JM
86 read_memory ((CORE_ADDR) P_cma__g_current_thread,
87 (char *) &tcb_ptr,
c906108c
SS
88 sizeof tcb_ptr);
89
c5aa993b 90 read_memory (tcb_ptr, (char *) &tcb, sizeof tcb);
c906108c 91
f7926acf
JB
92 return (ptid_build (PIDGET (main_ptid), 0,
93 cma_thread_get_unique (&tcb.prolog.client_thread)));
c906108c
SS
94}
95
f7926acf 96static cma__t_int_tcb *find_tcb (ptid_t ptid);
c906108c
SS
97
98static cma__t_int_tcb *
f7926acf 99find_tcb (ptid_t ptid)
c906108c
SS
100{
101 cma__t_known_object queue_header;
102 cma__t_queue *queue_ptr;
f7926acf 103 int thread = ptid_get_tid (ptid);
c906108c
SS
104
105 if (thread == cached_thread)
106 return &cached_tcb;
107
c5aa993b
JM
108 read_memory ((CORE_ADDR) P_cma__g_known_threads,
109 (char *) &queue_header,
c906108c
SS
110 sizeof queue_header);
111
112 for (queue_ptr = queue_header.queue.flink;
c5aa993b 113 queue_ptr != (cma__t_queue *) P_cma__g_known_threads;
c906108c
SS
114 queue_ptr = cached_tcb.threads.flink)
115 {
116 cma__t_int_tcb *tcb_ptr;
117
118 tcb_ptr = cma__base (queue_ptr, threads, cma__t_int_tcb);
119
c5aa993b 120 read_memory ((CORE_ADDR) tcb_ptr, (char *) &cached_tcb, sizeof cached_tcb);
c906108c
SS
121
122 if (cached_tcb.header.type == cma__c_obj_tcb)
f7926acf 123 if (cma_thread_get_unique (&cached_tcb.prolog.client_thread) == thread)
c906108c
SS
124 {
125 cached_thread = thread;
126 return &cached_tcb;
127 }
128 }
129
8a3fe4f8 130 error (_("Can't find TCB %d"), thread);
c906108c
SS
131 return NULL;
132}
133\f
134/* Most target vector functions from here on actually just pass through to
135 inftarg.c, as they don't need to do anything specific for threads. */
136
c906108c 137static void
fba45db2 138hpux_thread_open (char *arg, int from_tty)
c906108c 139{
1df84f13 140 deprecated_child_ops.to_open (arg, from_tty);
c906108c
SS
141}
142
143/* Attach to process PID, then initialize for debugging it
144 and wait for the trace-trap that results from attaching. */
145
146static void
fba45db2 147hpux_thread_attach (char *args, int from_tty)
c906108c 148{
1df84f13 149 deprecated_child_ops.to_attach (args, from_tty);
c906108c
SS
150
151 /* XXX - might want to iterate over all the threads and register them. */
152}
153
154/* Take a program previously attached to and detaches it.
155 The program resumes execution and will no longer stop
156 on signals, etc. We'd better not have left any breakpoints
157 in the program or it'll die when it hits one. For this
158 to work, it may be necessary for the process to have been
159 previously attached. It *might* work if the program was
160 started via the normal ptrace (PTRACE_TRACEME). */
161
162static void
fba45db2 163hpux_thread_detach (char *args, int from_tty)
c906108c 164{
1df84f13 165 deprecated_child_ops.to_detach (args, from_tty);
c906108c
SS
166}
167
168/* Resume execution of process PID. If STEP is nozero, then
169 just single step it. If SIGNAL is nonzero, restart it with that
170 signal activated. We may have to convert pid from a thread-id to an LWP id
171 for procfs. */
172
173static void
39f77062 174hpux_thread_resume (ptid_t ptid, int step, enum target_signal signo)
c906108c
SS
175{
176 struct cleanup *old_chain;
177
39f77062 178 old_chain = save_inferior_ptid ();
c906108c 179
39f77062
KB
180 ptid = main_ptid;
181 inferior_ptid = main_ptid;
c906108c
SS
182
183#if 0
184 if (pid != -1)
185 {
186 pid = thread_to_lwp (pid, -2);
187 if (pid == -2) /* Inactive thread */
8a3fe4f8 188 error (_("This version of Solaris can't start inactive threads."));
c906108c
SS
189 }
190#endif
191
1df84f13 192 deprecated_child_ops.to_resume (ptid, step, signo);
c906108c
SS
193
194 cached_thread = 0;
c906108c
SS
195
196 do_cleanups (old_chain);
197}
198
199/* Wait for any threads to stop. We may have to convert PID from a thread id
200 to a LWP id, and vice versa on the way out. */
201
39f77062
KB
202static ptid_t
203hpux_thread_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
c906108c 204{
39f77062 205 ptid_t rtnval;
c906108c
SS
206 struct cleanup *old_chain;
207
39f77062 208 old_chain = save_inferior_ptid ();
c906108c 209
39f77062 210 inferior_ptid = main_ptid;
c906108c 211
39f77062
KB
212 if (!ptid_equal (ptid, minus_one_ptid))
213 ptid = main_ptid;
c906108c 214
1df84f13 215 rtnval = deprecated_child_ops.to_wait (ptid, ourstatus);
c906108c
SS
216
217 rtnval = find_active_thread ();
218
219 do_cleanups (old_chain);
220
221 return rtnval;
222}
223
3f6306ec 224static char regmap[] =
c906108c 225{
c5aa993b
JM
226 -2, -1, -1, 0, 4, 8, 12, 16, 20, 24, /* flags, r1 -> r9 */
227 28, 32, 36, 40, 44, 48, 52, 56, 60, -1, /* r10 -> r19 */
228 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* r20 -> r29 */
c906108c
SS
229
230 /* r30, r31, sar, pcoqh, pcsqh, pcoqt, pcsqt, eiem, iir, isr */
231 -2, -1, -1, -2, -1, -1, -1, -1, -1, -1,
232
233 /* ior, ipsw, goto, sr4, sr0, sr1, sr2, sr3, sr5, sr6 */
234 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
235
236 /* sr7, cr0, cr8, cr9, ccr, cr12, cr13, cr24, cr25, cr26 */
237 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
238
239 -1, -1, -1, -1, /* mpsfu_high, mpsfu_low, mpsfu_ovflo, pad */
c5aa993b
JM
240 144, -1, -1, -1, -1, -1, -1, -1, /* fpsr, fpe1 -> fpe7 */
241 -1, -1, -1, -1, -1, -1, -1, -1, /* fr4 -> fr7 */
242 -1, -1, -1, -1, -1, -1, -1, -1, /* fr8 -> fr11 */
243 136, -1, 128, -1, 120, -1, 112, -1, /* fr12 -> fr15 */
244 104, -1, 96, -1, 88, -1, 80, -1, /* fr16 -> fr19 */
245 72, -1, 64, -1, -1, -1, -1, -1, /* fr20 -> fr23 */
246 -1, -1, -1, -1, -1, -1, -1, -1, /* fr24 -> fr27 */
247 -1, -1, -1, -1, -1, -1, -1, -1, /* fr28 -> fr31 */
c906108c
SS
248};
249
250static void
56be3814 251hpux_thread_fetch_registers (struct regcache *regcache, int regno)
c906108c
SS
252{
253 cma__t_int_tcb tcb, *tcb_ptr;
254 struct cleanup *old_chain;
255 int i;
256 int first_regno, last_regno;
257
f7926acf 258 tcb_ptr = find_tcb (inferior_ptid);
c906108c 259
39f77062 260 old_chain = save_inferior_ptid ();
c906108c 261
39f77062 262 inferior_ptid = main_ptid;
c906108c
SS
263
264 if (tcb_ptr->state == cma__c_state_running)
265 {
56be3814 266 deprecated_child_ops.to_fetch_registers (regcache, regno);
c906108c
SS
267
268 do_cleanups (old_chain);
269
270 return;
271 }
272
273 if (regno == -1)
274 {
275 first_regno = 0;
f57d151a 276 last_regno = gdbarch_num_regs (current_gdbarch) - 1;
c906108c
SS
277 }
278 else
279 {
280 first_regno = regno;
281 last_regno = regno;
282 }
283
284 for (regno = first_regno; regno <= last_regno; regno++)
285 {
286 if (regmap[regno] == -1)
56be3814 287 deprecated_child_ops.to_fetch_registers (regcache, regno);
c906108c
SS
288 else
289 {
123a958e 290 unsigned char buf[MAX_REGISTER_SIZE];
c906108c
SS
291 CORE_ADDR sp;
292
c5aa993b 293 sp = (CORE_ADDR) tcb_ptr->static_ctx.sp - 160;
c906108c 294
3f6306ec 295 if (regno == HPPA_FLAGS_REGNUM)
c906108c 296 /* Flags must be 0 to avoid bogus value for SS_INSYSCALL */
3acba339 297 memset (buf, '\000', register_size (current_gdbarch, regno));
3f6306ec 298 else if (regno == HPPA_SP_REGNUM)
fbd9dcd3 299 store_unsigned_integer (buf, sizeof sp, sp);
3f6306ec 300 else if (regno == HPPA_PCOQ_HEAD_REGNUM)
3acba339 301 read_memory (sp - 20, buf, register_size (current_gdbarch, regno));
c906108c 302 else
3acba339 303 read_memory (sp + regmap[regno], buf, register_size (current_gdbarch, regno));
c906108c 304
56be3814 305 regcache_raw_supply (regcache, regno, buf);
c906108c
SS
306 }
307 }
308
309 do_cleanups (old_chain);
310}
311
312static void
56be3814 313hpux_thread_store_registers (struct regcache *regcache, int regno)
c906108c
SS
314{
315 cma__t_int_tcb tcb, *tcb_ptr;
316 struct cleanup *old_chain;
317 int i;
318 int first_regno, last_regno;
319
f7926acf 320 tcb_ptr = find_tcb (inferior_ptid);
c906108c 321
39f77062 322 old_chain = save_inferior_ptid ();
c906108c 323
39f77062 324 inferior_ptid = main_ptid;
c906108c
SS
325
326 if (tcb_ptr->state == cma__c_state_running)
327 {
56be3814 328 deprecated_child_ops.to_store_registers (regcache, regno);
c906108c
SS
329
330 do_cleanups (old_chain);
331
332 return;
333 }
334
335 if (regno == -1)
336 {
337 first_regno = 0;
f57d151a 338 last_regno = gdbarch_num_regs (current_gdbarch) - 1;
c906108c
SS
339 }
340 else
341 {
342 first_regno = regno;
343 last_regno = regno;
344 }
345
346 for (regno = first_regno; regno <= last_regno; regno++)
347 {
348 if (regmap[regno] == -1)
56be3814 349 deprecated_child_ops.to_store_registers (regcache, regno);
c906108c
SS
350 else
351 {
123a958e 352 unsigned char buf[MAX_REGISTER_SIZE];
c906108c
SS
353 CORE_ADDR sp;
354
c5aa993b 355 sp = (CORE_ADDR) tcb_ptr->static_ctx.sp - 160;
c906108c 356
3f6306ec 357 if (regno == HPPA_FLAGS_REGNUM)
56be3814 358 deprecated_child_ops.to_store_registers (regcache, regno); /* Let lower layer handle this... */
3f6306ec 359 else if (regno == HPPA_SP_REGNUM)
c906108c 360 {
56be3814 361 regcache_raw_collect (regcache, regno, buf);
3f6306ec
DA
362 write_memory ((CORE_ADDR) &tcb_ptr->static_ctx.sp, buf,
363 register_size (current_gdbarch, regno));
364 tcb_ptr->static_ctx.sp
365 = (cma__t_hppa_regs *) ((CORE_ADDR) buf + 160);
366 }
367 else if (regno == HPPA_PCOQ_HEAD_REGNUM)
368 {
56be3814 369 regcache_raw_collect (regcache, regno, buf);
3f6306ec 370 write_memory (sp - 20, buf,
3acba339 371 register_size (current_gdbarch, regno));
c906108c 372 }
c906108c 373 else
3f6306ec 374 {
56be3814 375 regcache_raw_collect (regcache, regno, buf);
3f6306ec
DA
376 write_memory (sp + regmap[regno], buf,
377 register_size (current_gdbarch, regno));
378 }
c906108c
SS
379 }
380 }
381
382 do_cleanups (old_chain);
383}
384
385/* Get ready to modify the registers array. On machines which store
386 individual registers, this doesn't need to do anything. On machines
387 which store all the registers in one fell swoop, this makes sure
388 that registers contains all the registers from the program being
389 debugged. */
390
391static void
316f2060 392hpux_thread_prepare_to_store (struct regcache *regcache)
c906108c 393{
316f2060 394 deprecated_child_ops.to_prepare_to_store (regcache);
c906108c
SS
395}
396
397static int
165a58fe 398hpux_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
971429b4
AC
399 int dowrite, struct mem_attrib *attribs,
400 struct target_ops *target)
c906108c
SS
401{
402 int retval;
403 struct cleanup *old_chain;
404
39f77062 405 old_chain = save_inferior_ptid ();
c906108c 406
39f77062 407 inferior_ptid = main_ptid;
c906108c 408
971429b4 409 retval =
c8e73a31 410 deprecated_child_ops.deprecated_xfer_memory (memaddr, myaddr, len, dowrite, attribs, target);
c906108c
SS
411
412 do_cleanups (old_chain);
413
414 return retval;
415}
416
417/* Print status information about what we're accessing. */
418
419static void
fba45db2 420hpux_thread_files_info (struct target_ops *ignore)
c906108c 421{
1df84f13 422 deprecated_child_ops.to_files_info (ignore);
c906108c
SS
423}
424
425static void
fba45db2 426hpux_thread_kill_inferior (void)
c906108c 427{
1df84f13 428 deprecated_child_ops.to_kill ();
c906108c
SS
429}
430
431static void
39f77062 432hpux_thread_notice_signals (ptid_t ptid)
c906108c 433{
1df84f13 434 deprecated_child_ops.to_notice_signals (ptid);
c906108c
SS
435}
436
437/* Fork an inferior process, and start debugging it with /proc. */
438
439static void
c27cda74
AC
440hpux_thread_create_inferior (char *exec_file, char *allargs, char **env,
441 int from_tty)
c906108c 442{
1df84f13 443 deprecated_child_ops.to_create_inferior (exec_file, allargs, env, from_tty);
c906108c
SS
444
445 if (hpux_thread_active)
446 {
39f77062 447 main_ptid = inferior_ptid;
c906108c
SS
448
449 push_target (&hpux_thread_ops);
450
39f77062 451 inferior_ptid = find_active_thread ();
c906108c 452
39f77062 453 add_thread (inferior_ptid);
c906108c
SS
454 }
455}
456
457/* This routine is called whenever a new symbol table is read in, or when all
458 symbol tables are removed. libthread_db can only be initialized when it
459 finds the right variables in libthread.so. Since it's a shared library,
460 those variables don't show up until the library gets mapped and the symbol
461 table is read in. */
462
06d3b283 463static void
fba45db2 464hpux_thread_new_objfile (struct objfile *objfile)
c906108c
SS
465{
466 struct minimal_symbol *ms;
467
468 if (!objfile)
469 {
470 hpux_thread_active = 0;
06d3b283 471 return;
c906108c
SS
472 }
473
474 ms = lookup_minimal_symbol ("cma__g_known_threads", NULL, objfile);
475
476 if (!ms)
06d3b283 477 return;
c906108c
SS
478
479 P_cma__g_known_threads = SYMBOL_VALUE_ADDRESS (ms);
480
481 ms = lookup_minimal_symbol ("cma__g_current_thread", NULL, objfile);
482
483 if (!ms)
06d3b283 484 return;
c906108c
SS
485
486 P_cma__g_current_thread = SYMBOL_VALUE_ADDRESS (ms);
487
488 hpux_thread_active = 1;
489}
490
491/* Clean up after the inferior dies. */
492
493static void
fba45db2 494hpux_thread_mourn_inferior (void)
c906108c 495{
1df84f13 496 deprecated_child_ops.to_mourn_inferior ();
c906108c
SS
497}
498
499/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
500
501static int
fba45db2 502hpux_thread_can_run (void)
c906108c
SS
503{
504 return child_suppress_run;
505}
506
507static int
39f77062 508hpux_thread_alive (ptid_t ptid)
c906108c
SS
509{
510 return 1;
511}
512
513static void
fba45db2 514hpux_thread_stop (void)
c906108c 515{
1df84f13 516 deprecated_child_ops.to_stop ();
c906108c
SS
517}
518\f
519/* Convert a pid to printable form. */
520
521char *
39f77062 522hpux_pid_to_str (ptid_t ptid)
c906108c
SS
523{
524 static char buf[100];
39f77062 525 int pid = PIDGET (ptid);
c906108c 526
f7926acf 527 sprintf (buf, "Thread %ld", ptid_get_tid (ptid));
c906108c
SS
528
529 return buf;
530}
531\f
532static void
fba45db2 533init_hpux_thread_ops (void)
c906108c
SS
534{
535 hpux_thread_ops.to_shortname = "hpux-threads";
536 hpux_thread_ops.to_longname = "HPUX threads and pthread.";
537 hpux_thread_ops.to_doc = "HPUX threads and pthread support.";
538 hpux_thread_ops.to_open = hpux_thread_open;
539 hpux_thread_ops.to_attach = hpux_thread_attach;
540 hpux_thread_ops.to_detach = hpux_thread_detach;
541 hpux_thread_ops.to_resume = hpux_thread_resume;
542 hpux_thread_ops.to_wait = hpux_thread_wait;
543 hpux_thread_ops.to_fetch_registers = hpux_thread_fetch_registers;
544 hpux_thread_ops.to_store_registers = hpux_thread_store_registers;
545 hpux_thread_ops.to_prepare_to_store = hpux_thread_prepare_to_store;
c8e73a31 546 hpux_thread_ops.deprecated_xfer_memory = hpux_thread_xfer_memory;
c906108c
SS
547 hpux_thread_ops.to_files_info = hpux_thread_files_info;
548 hpux_thread_ops.to_insert_breakpoint = memory_insert_breakpoint;
549 hpux_thread_ops.to_remove_breakpoint = memory_remove_breakpoint;
550 hpux_thread_ops.to_terminal_init = terminal_init_inferior;
551 hpux_thread_ops.to_terminal_inferior = terminal_inferior;
552 hpux_thread_ops.to_terminal_ours_for_output = terminal_ours_for_output;
a790ad35 553 hpux_thread_ops.to_terminal_save_ours = terminal_save_ours;
c906108c
SS
554 hpux_thread_ops.to_terminal_ours = terminal_ours;
555 hpux_thread_ops.to_terminal_info = child_terminal_info;
556 hpux_thread_ops.to_kill = hpux_thread_kill_inferior;
557 hpux_thread_ops.to_create_inferior = hpux_thread_create_inferior;
558 hpux_thread_ops.to_mourn_inferior = hpux_thread_mourn_inferior;
559 hpux_thread_ops.to_can_run = hpux_thread_can_run;
560 hpux_thread_ops.to_notice_signals = hpux_thread_notice_signals;
b83266a0 561 hpux_thread_ops.to_thread_alive = hpux_thread_alive;
c906108c
SS
562 hpux_thread_ops.to_stop = hpux_thread_stop;
563 hpux_thread_ops.to_stratum = process_stratum;
564 hpux_thread_ops.to_has_all_memory = 1;
565 hpux_thread_ops.to_has_memory = 1;
566 hpux_thread_ops.to_has_stack = 1;
567 hpux_thread_ops.to_has_registers = 1;
568 hpux_thread_ops.to_has_execution = 1;
569 hpux_thread_ops.to_magic = OPS_MAGIC;
570}
571
572void
fba45db2 573_initialize_hpux_thread (void)
c906108c
SS
574{
575 init_hpux_thread_ops ();
576 add_target (&hpux_thread_ops);
577
578 child_suppress_run = 1;
d9788fe8 579 /* Hook into new_objfile notification. */
06d3b283 580 observer_attach_new_objfile (hpux_thread_new_objfile);
c906108c 581}
This page took 0.576849 seconds and 4 git commands to generate.