Commit | Line | Data |
---|---|---|
8fc2b417 SG |
1 | /* Low level interface for debugging Solaris threads for GDB, the GNU debugger. |
2 | Copyright 1996 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
19 | ||
a50cedad SG |
20 | /* This module implements a sort of half target that sits between the |
21 | machine-independent parts of GDB and the /proc interface (procfs.c) to | |
22 | provide access to the Solaris user-mode thread implementation. | |
23 | ||
24 | Solaris threads are true user-mode threads, which are invoked via the thr_* | |
25 | and pthread_* (native and Posix respectivly) interfaces. These are mostly | |
26 | implemented in user-space, with all thread context kept in various | |
27 | structures that live in the user's heap. These should not be confused with | |
28 | lightweight processes (LWPs), which are implemented by the kernel, and | |
29 | scheduled without explicit intervention by the process. | |
30 | ||
31 | Just to confuse things a little, Solaris threads (both native and Posix) are | |
32 | actually implemented using LWPs. In general, there are going to be more | |
33 | threads than LWPs. There is no fixed correspondence between a thread and an | |
34 | LWP. When a thread wants to run, it gets scheduled onto the first available | |
35 | LWP and can therefore migrate from one LWP to another as time goes on. A | |
36 | sleeping thread may not be associated with an LWP at all! | |
37 | ||
38 | To make it possible to mess with threads, Sun provides a library called | |
39 | libthread_db.so.1 (not to be confused with libthread_db.so.0, which doesn't | |
40 | have a published interface). This interface has an upper part, which it | |
41 | provides, and a lower part which I provide. The upper part consists of the | |
42 | td_* routines, which allow me to find all the threads, query their state, | |
43 | etc... The lower part consists of all of the ps_*, which are used by the | |
44 | td_* routines to read/write memory, manipulate LWPs, lookup symbols, etc... | |
45 | The ps_* routines actually do most of their work by calling functions in | |
46 | procfs.c. */ | |
47 | ||
8fc2b417 SG |
48 | #include "defs.h" |
49 | ||
934741a1 SG |
50 | /* Undefine gregset_t and fpregset_t to avoid conflict with defs in xm file. */ |
51 | ||
8fc2b417 SG |
52 | #ifdef gregset_t |
53 | #undef gregset_t | |
54 | #endif | |
55 | ||
56 | #ifdef fpregset_t | |
57 | #undef fpregset_t | |
58 | #endif | |
59 | ||
934741a1 | 60 | #include <thread.h> |
8fc2b417 SG |
61 | #include <proc_service.h> |
62 | #include <thread_db.h> | |
fdfa3315 | 63 | #include "gdbthread.h" |
8fc2b417 SG |
64 | #include "target.h" |
65 | #include "inferior.h" | |
66 | #include <fcntl.h> | |
67 | #include <unistd.h> | |
68 | #include <sys/stat.h> | |
4915acad | 69 | #include <dlfcn.h> |
0f5b751a | 70 | #include "gdbcmd.h" |
8fc2b417 | 71 | |
8fc2b417 SG |
72 | extern struct target_ops sol_thread_ops; /* Forward declaration */ |
73 | ||
74 | extern int procfs_suppress_run; | |
934741a1 | 75 | extern struct target_ops procfs_ops; /* target vector for procfs.c */ |
3780c337 | 76 | extern char *procfs_pid_to_str PARAMS ((int pid)); |
934741a1 | 77 | |
a50cedad SG |
78 | /* Note that these prototypes differ slightly from those used in procfs.c |
79 | for of two reasons. One, we can't use gregset_t, as that's got a whole | |
80 | different meaning under Solaris (also, see above). Two, we can't use the | |
81 | pointer form here as these are actually arrays of ints (for Sparc's at | |
82 | least), and are automatically coerced into pointers to ints when used as | |
83 | parameters. That makes it impossible to avoid a compiler warning when | |
84 | passing pr{g fp}regset_t's from a parameter to an argument of one of | |
85 | these functions. */ | |
86 | ||
934741a1 SG |
87 | extern void supply_gregset PARAMS ((const prgregset_t)); |
88 | extern void fill_gregset PARAMS ((prgregset_t, int)); | |
89 | extern void supply_fpregset PARAMS ((const prfpregset_t)); | |
90 | extern void fill_fpregset PARAMS ((prfpregset_t, int)); | |
8fc2b417 | 91 | |
a50cedad SG |
92 | /* This struct is defined by us, but mainly used for the proc_service interface. |
93 | We don't have much use for it, except as a handy place to get a real pid | |
94 | for memory accesses. */ | |
95 | ||
8fc2b417 SG |
96 | struct ps_prochandle |
97 | { | |
98 | pid_t pid; | |
99 | }; | |
100 | ||
934741a1 SG |
101 | struct string_map |
102 | { | |
103 | int num; | |
104 | char *str; | |
105 | }; | |
106 | ||
8fc2b417 SG |
107 | static struct ps_prochandle main_ph; |
108 | static td_thragent_t *main_ta; | |
8fc2b417 SG |
109 | static int sol_thread_active = 0; |
110 | ||
934741a1 SG |
111 | static struct cleanup * save_inferior_pid PARAMS ((void)); |
112 | static void restore_inferior_pid PARAMS ((int pid)); | |
113 | static char *td_err_string PARAMS ((td_err_e errcode)); | |
114 | static char *td_state_string PARAMS ((td_thr_state_e statecode)); | |
3780c337 | 115 | static int thread_to_lwp PARAMS ((int thread_id, int default_lwp)); |
934741a1 SG |
116 | static void sol_thread_resume PARAMS ((int pid, int step, |
117 | enum target_signal signo)); | |
118 | static int lwp_to_thread PARAMS ((int lwp)); | |
3780c337 | 119 | static int sol_thread_alive PARAMS ((int pid)); |
934741a1 SG |
120 | |
121 | #define THREAD_FLAG 0x80000000 | |
122 | #define is_thread(ARG) (((ARG) & THREAD_FLAG) != 0) | |
123 | #define is_lwp(ARG) (((ARG) & THREAD_FLAG) == 0) | |
124 | #define GET_LWP(LWP_ID) (TIDGET(LWP_ID)) | |
125 | #define GET_THREAD(THREAD_ID) (((THREAD_ID) >> 16) & 0x7fff) | |
126 | #define BUILD_LWP(LWP_ID, PID) ((LWP_ID) << 16 | (PID)) | |
127 | #define BUILD_THREAD(THREAD_ID, PID) (THREAD_FLAG | BUILD_LWP (THREAD_ID, PID)) | |
4915acad SG |
128 | |
129 | /* Pointers to routines from lithread_db resolved by dlopen() */ | |
130 | ||
131 | static void | |
132 | (*p_td_log) (const int on_off); | |
133 | static td_err_e | |
134 | (*p_td_ta_new) (const struct ps_prochandle *ph_p, td_thragent_t **ta_pp); | |
135 | static td_err_e | |
136 | (*p_td_ta_delete) (td_thragent_t *ta_p); | |
137 | static td_err_e | |
138 | (*p_td_init) (void); | |
139 | static td_err_e | |
140 | (*p_td_ta_get_ph) (const td_thragent_t *ta_p, struct ps_prochandle **ph_pp); | |
141 | static td_err_e | |
142 | (*p_td_ta_get_nthreads) (const td_thragent_t *ta_p, int *nthread_p); | |
143 | static td_err_e | |
144 | (*p_td_ta_tsd_iter) (const td_thragent_t *ta_p, td_key_iter_f *cb, void *cbdata_p); | |
145 | static td_err_e | |
146 | (*p_td_ta_thr_iter) (const td_thragent_t *ta_p, td_thr_iter_f *cb, void *cbdata_p, td_thr_state_e state, | |
147 | int ti_pri, sigset_t *ti_sigmask_p, unsigned ti_user_flags); | |
148 | static td_err_e | |
149 | (*p_td_thr_validate) (const td_thrhandle_t *th_p); | |
150 | static td_err_e | |
151 | (*p_td_thr_tsd) (const td_thrhandle_t *th_p, const thread_key_t key, void **data_pp); | |
152 | static td_err_e | |
153 | (*p_td_thr_get_info) (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p); | |
154 | static td_err_e | |
155 | (*p_td_thr_getfpregs) (const td_thrhandle_t *th_p, prfpregset_t *fpregset); | |
156 | static td_err_e | |
157 | (*p_td_thr_getxregsize) (const td_thrhandle_t *th_p, int *xregsize); | |
158 | static td_err_e | |
159 | (*p_td_thr_getxregs) (const td_thrhandle_t *th_p, const caddr_t xregset); | |
160 | static td_err_e | |
161 | (*p_td_thr_sigsetmask) (const td_thrhandle_t *th_p, const sigset_t ti_sigmask); | |
162 | static td_err_e | |
163 | (*p_td_thr_setprio) (const td_thrhandle_t *th_p, const int ti_pri); | |
164 | static td_err_e | |
165 | (*p_td_thr_setsigpending) (const td_thrhandle_t *th_p, const uchar_t ti_pending_flag, const sigset_t ti_pending); | |
166 | static td_err_e | |
167 | (*p_td_thr_setfpregs) (const td_thrhandle_t *th_p, const prfpregset_t *fpregset); | |
168 | static td_err_e | |
169 | (*p_td_thr_setxregs) (const td_thrhandle_t *th_p, const caddr_t xregset); | |
170 | static td_err_e | |
171 | (*p_td_ta_map_id2thr) (const td_thragent_t *ta_p, thread_t tid, td_thrhandle_t *th_p); | |
172 | static td_err_e | |
173 | (*p_td_ta_map_lwp2thr) (const td_thragent_t *ta_p, lwpid_t lwpid, td_thrhandle_t *th_p); | |
174 | static td_err_e | |
175 | (*p_td_thr_getgregs) (const td_thrhandle_t *th_p, prgregset_t regset); | |
176 | static td_err_e | |
177 | (*p_td_thr_setgregs) (const td_thrhandle_t *th_p, const prgregset_t regset); | |
8fc2b417 | 178 | \f |
934741a1 SG |
179 | /* |
180 | ||
181 | LOCAL FUNCTION | |
182 | ||
183 | td_err_string - Convert a thread_db error code to a string | |
184 | ||
185 | SYNOPSIS | |
186 | ||
187 | char * td_err_string (errcode) | |
188 | ||
189 | DESCRIPTION | |
190 | ||
191 | Return the thread_db error string associated with errcode. If errcode | |
192 | is unknown, then return a message. | |
193 | ||
194 | */ | |
195 | ||
196 | static char * | |
197 | td_err_string (errcode) | |
198 | td_err_e errcode; | |
199 | { | |
200 | static struct string_map | |
201 | td_err_table[] = { | |
202 | {TD_OK, "generic \"call succeeded\""}, | |
203 | {TD_ERR, "generic error."}, | |
204 | {TD_NOTHR, "no thread can be found to satisfy query"}, | |
205 | {TD_NOSV, "no synch. variable can be found to satisfy query"}, | |
206 | {TD_NOLWP, "no lwp can be found to satisfy query"}, | |
207 | {TD_BADPH, "invalid process handle"}, | |
208 | {TD_BADTH, "invalid thread handle"}, | |
209 | {TD_BADSH, "invalid synchronization handle"}, | |
210 | {TD_BADTA, "invalid thread agent"}, | |
211 | {TD_BADKEY, "invalid key"}, | |
212 | {TD_NOMSG, "td_thr_event_getmsg() called when there was no message"}, | |
213 | {TD_NOFPREGS, "FPU register set not available for given thread"}, | |
214 | {TD_NOLIBTHREAD, "application not linked with libthread"}, | |
215 | {TD_NOEVENT, "requested event is not supported"}, | |
216 | {TD_NOCAPAB, "capability not available"}, | |
217 | {TD_DBERR, "Debugger service failed"}, | |
218 | {TD_NOAPLIC, "Operation not applicable to"}, | |
219 | {TD_NOTSD, "No thread specific data for this thread"}, | |
220 | {TD_MALLOC, "Malloc failed"}, | |
221 | {TD_PARTIALREG, "Only part of register set was writen/read"}, | |
222 | {TD_NOXREGS, "X register set not available for given thread"} | |
223 | }; | |
224 | const int td_err_size = sizeof td_err_table / sizeof (struct string_map); | |
225 | int i; | |
226 | static char buf[50]; | |
227 | ||
228 | for (i = 0; i < td_err_size; i++) | |
229 | if (td_err_table[i].num == errcode) | |
230 | return td_err_table[i].str; | |
231 | ||
232 | sprintf (buf, "Unknown thread_db error code: %d", errcode); | |
8fc2b417 | 233 | |
934741a1 SG |
234 | return buf; |
235 | } | |
236 | \f | |
237 | /* | |
8fc2b417 | 238 | |
934741a1 | 239 | LOCAL FUNCTION |
8fc2b417 | 240 | |
934741a1 SG |
241 | td_state_string - Convert a thread_db state code to a string |
242 | ||
243 | SYNOPSIS | |
244 | ||
245 | char * td_state_string (statecode) | |
246 | ||
247 | DESCRIPTION | |
248 | ||
249 | Return the thread_db state string associated with statecode. If | |
250 | statecode is unknown, then return a message. | |
251 | ||
252 | */ | |
253 | ||
254 | static char * | |
255 | td_state_string (statecode) | |
256 | td_thr_state_e statecode; | |
257 | { | |
258 | static struct string_map | |
259 | td_thr_state_table[] = { | |
260 | {TD_THR_ANY_STATE, "any state"}, | |
261 | {TD_THR_UNKNOWN, "unknown"}, | |
262 | {TD_THR_STOPPED, "stopped"}, | |
263 | {TD_THR_RUN, "run"}, | |
264 | {TD_THR_ACTIVE, "active"}, | |
265 | {TD_THR_ZOMBIE, "zombie"}, | |
266 | {TD_THR_SLEEP, "sleep"}, | |
267 | {TD_THR_STOPPED_ASLEEP, "stopped asleep"} | |
268 | }; | |
269 | const int td_thr_state_table_size = sizeof td_thr_state_table / sizeof (struct string_map); | |
270 | int i; | |
271 | static char buf[50]; | |
272 | ||
273 | for (i = 0; i < td_thr_state_table_size; i++) | |
274 | if (td_thr_state_table[i].num == statecode) | |
275 | return td_thr_state_table[i].str; | |
276 | ||
277 | sprintf (buf, "Unknown thread_db state code: %d", statecode); | |
278 | ||
279 | return buf; | |
280 | } | |
281 | \f | |
282 | /* | |
283 | ||
284 | LOCAL FUNCTION | |
285 | ||
286 | thread_to_lwp - Convert a Posix or Solaris thread id to a LWP id. | |
287 | ||
288 | SYNOPSIS | |
289 | ||
290 | int thread_to_lwp (thread_id, default_lwp) | |
291 | ||
292 | DESCRIPTION | |
293 | ||
294 | This function converts a Posix or Solaris thread id to a lightweight | |
295 | process id. If thread_id is non-existent, that's an error. If it's | |
296 | an inactive thread, then we return default_lwp. | |
297 | ||
298 | NOTES | |
299 | ||
300 | This function probably shouldn't call error()... | |
301 | ||
302 | */ | |
8fc2b417 SG |
303 | |
304 | static int | |
305 | thread_to_lwp (thread_id, default_lwp) | |
306 | int thread_id; | |
307 | int default_lwp; | |
308 | { | |
309 | td_thrinfo_t ti; | |
310 | td_thrhandle_t th; | |
311 | td_err_e val; | |
8fc2b417 | 312 | |
934741a1 | 313 | if (is_lwp (thread_id)) |
8fc2b417 SG |
314 | return thread_id; /* It's already an LWP id */ |
315 | ||
316 | /* It's a thread. Convert to lwp */ | |
317 | ||
3780c337 MS |
318 | val = p_td_ta_map_id2thr (main_ta, GET_THREAD (thread_id), &th); |
319 | if (val == TD_NOTHR) | |
320 | return -1; /* thread must have terminated */ | |
321 | else if (val != TD_OK) | |
934741a1 | 322 | error ("thread_to_lwp: td_ta_map_id2thr %s", td_err_string (val)); |
8fc2b417 | 323 | |
4915acad | 324 | val = p_td_thr_get_info (&th, &ti); |
3780c337 MS |
325 | if (val == TD_NOTHR) |
326 | return -1; /* thread must have terminated */ | |
327 | else if (val != TD_OK) | |
934741a1 | 328 | error ("thread_to_lwp: td_thr_get_info: %s", td_err_string (val)); |
8fc2b417 SG |
329 | |
330 | if (ti.ti_state != TD_THR_ACTIVE) | |
331 | { | |
332 | if (default_lwp != -1) | |
333 | return default_lwp; | |
934741a1 SG |
334 | error ("thread_to_lwp: thread state not active: %s", |
335 | td_state_string (ti.ti_state)); | |
8fc2b417 | 336 | } |
8fc2b417 | 337 | |
3780c337 | 338 | return BUILD_LWP (ti.ti_lid, PIDGET (thread_id)); |
8fc2b417 | 339 | } |
a50cedad SG |
340 | \f |
341 | /* | |
342 | ||
343 | LOCAL FUNCTION | |
344 | ||
345 | lwp_to_thread - Convert a LWP id to a Posix or Solaris thread id. | |
346 | ||
347 | SYNOPSIS | |
348 | ||
349 | int lwp_to_thread (lwp_id) | |
8fc2b417 | 350 | |
a50cedad SG |
351 | DESCRIPTION |
352 | ||
353 | This function converts a lightweight process id to a Posix or Solaris | |
354 | thread id. If thread_id is non-existent, that's an error. | |
355 | ||
356 | NOTES | |
357 | ||
358 | This function probably shouldn't call error()... | |
359 | ||
360 | */ | |
8fc2b417 | 361 | |
8fc2b417 SG |
362 | static int |
363 | lwp_to_thread (lwp) | |
364 | int lwp; | |
365 | { | |
366 | td_thrinfo_t ti; | |
367 | td_thrhandle_t th; | |
368 | td_err_e val; | |
8fc2b417 | 369 | |
934741a1 | 370 | if (is_thread (lwp)) |
8fc2b417 SG |
371 | return lwp; /* It's already a thread id */ |
372 | ||
373 | /* It's an lwp. Convert it to a thread id. */ | |
374 | ||
3780c337 MS |
375 | if (!sol_thread_alive (lwp)) |
376 | return -1; /* defunct lwp */ | |
8fc2b417 | 377 | |
3780c337 MS |
378 | val = p_td_ta_map_lwp2thr (main_ta, GET_LWP (lwp), &th); |
379 | if (val == TD_NOTHR) | |
380 | return -1; /* thread must have terminated */ | |
381 | else if (val != TD_OK) | |
934741a1 | 382 | error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val)); |
8fc2b417 | 383 | |
3780c337 MS |
384 | val = p_td_thr_validate (&th); |
385 | if (val == TD_NOTHR) | |
386 | return lwp; /* libthread doesn't know about it, just return lwp */ | |
387 | else if (val != TD_OK) | |
388 | error ("lwp_to_thread: td_thr_validate: %s.", td_err_string (val)); | |
8fc2b417 | 389 | |
3780c337 MS |
390 | val = p_td_thr_get_info (&th, &ti); |
391 | if (val == TD_NOTHR) | |
392 | return -1; /* thread must have terminated */ | |
393 | else if (val != TD_OK) | |
934741a1 | 394 | error ("lwp_to_thread: td_thr_get_info: %s.", td_err_string (val)); |
8fc2b417 | 395 | |
3780c337 | 396 | return BUILD_THREAD (ti.ti_tid, PIDGET (lwp)); |
8fc2b417 | 397 | } |
934741a1 | 398 | \f |
a50cedad SG |
399 | /* |
400 | ||
401 | LOCAL FUNCTION | |
402 | ||
403 | save_inferior_pid - Save inferior_pid on the cleanup list | |
404 | restore_inferior_pid - Restore inferior_pid from the cleanup list | |
405 | ||
406 | SYNOPSIS | |
407 | ||
408 | struct cleanup *save_inferior_pid () | |
409 | void restore_inferior_pid (int pid) | |
410 | ||
411 | DESCRIPTION | |
412 | ||
413 | These two functions act in unison to restore inferior_pid in | |
414 | case of an error. | |
415 | ||
416 | NOTES | |
417 | ||
418 | inferior_pid is a global variable that needs to be changed by many of | |
419 | these routines before calling functions in procfs.c. In order to | |
420 | guarantee that inferior_pid gets restored (in case of errors), you | |
421 | need to call save_inferior_pid before changing it. At the end of the | |
422 | function, you should invoke do_cleanups to restore it. | |
423 | ||
424 | */ | |
425 | ||
426 | ||
934741a1 SG |
427 | static struct cleanup * |
428 | save_inferior_pid () | |
429 | { | |
430 | return make_cleanup (restore_inferior_pid, inferior_pid); | |
431 | } | |
8fc2b417 | 432 | |
934741a1 SG |
433 | static void |
434 | restore_inferior_pid (pid) | |
435 | int pid; | |
436 | { | |
437 | inferior_pid = pid; | |
438 | } | |
439 | \f | |
a50cedad SG |
440 | |
441 | /* Most target vector functions from here on actually just pass through to | |
442 | procfs.c, as they don't need to do anything specific for threads. */ | |
443 | ||
444 | ||
8fc2b417 SG |
445 | /* ARGSUSED */ |
446 | static void | |
447 | sol_thread_open (arg, from_tty) | |
448 | char *arg; | |
449 | int from_tty; | |
450 | { | |
451 | procfs_ops.to_open (arg, from_tty); | |
452 | } | |
453 | ||
454 | /* Attach to process PID, then initialize for debugging it | |
455 | and wait for the trace-trap that results from attaching. */ | |
456 | ||
457 | static void | |
458 | sol_thread_attach (args, from_tty) | |
459 | char *args; | |
460 | int from_tty; | |
461 | { | |
462 | procfs_ops.to_attach (args, from_tty); | |
3780c337 MS |
463 | /* Must get symbols from solibs before libthread_db can run! */ |
464 | SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0); | |
465 | if (sol_thread_active) | |
466 | { | |
467 | printf_filtered ("sol-thread active.\n"); | |
468 | main_ph.pid = inferior_pid; /* Save for xfer_memory */ | |
469 | push_target (&sol_thread_ops); | |
470 | inferior_pid = lwp_to_thread (inferior_pid); | |
471 | if (inferior_pid == -1) | |
472 | inferior_pid = main_ph.pid; | |
473 | else | |
474 | add_thread (inferior_pid); | |
475 | } | |
8fc2b417 SG |
476 | /* XXX - might want to iterate over all the threads and register them. */ |
477 | } | |
478 | ||
479 | /* Take a program previously attached to and detaches it. | |
480 | The program resumes execution and will no longer stop | |
481 | on signals, etc. We'd better not have left any breakpoints | |
482 | in the program or it'll die when it hits one. For this | |
483 | to work, it may be necessary for the process to have been | |
484 | previously attached. It *might* work if the program was | |
485 | started via the normal ptrace (PTRACE_TRACEME). */ | |
486 | ||
487 | static void | |
488 | sol_thread_detach (args, from_tty) | |
489 | char *args; | |
490 | int from_tty; | |
491 | { | |
3780c337 | 492 | unpush_target (&sol_thread_ops); |
8fc2b417 SG |
493 | procfs_ops.to_detach (args, from_tty); |
494 | } | |
495 | ||
496 | /* Resume execution of process PID. If STEP is nozero, then | |
497 | just single step it. If SIGNAL is nonzero, restart it with that | |
a50cedad SG |
498 | signal activated. We may have to convert pid from a thread-id to an LWP id |
499 | for procfs. */ | |
8fc2b417 SG |
500 | |
501 | static void | |
502 | sol_thread_resume (pid, step, signo) | |
503 | int pid; | |
504 | int step; | |
505 | enum target_signal signo; | |
506 | { | |
934741a1 | 507 | struct cleanup *old_chain; |
8fc2b417 | 508 | |
934741a1 | 509 | old_chain = save_inferior_pid (); |
8fc2b417 SG |
510 | |
511 | inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid); | |
3780c337 MS |
512 | if (inferior_pid == -1) |
513 | inferior_pid = procfs_first_available (); | |
8fc2b417 SG |
514 | |
515 | if (pid != -1) | |
a50cedad | 516 | { |
3780c337 MS |
517 | int save_pid = pid; |
518 | ||
a50cedad SG |
519 | pid = thread_to_lwp (pid, -2); |
520 | if (pid == -2) /* Inactive thread */ | |
521 | error ("This version of Solaris can't start inactive threads."); | |
3780c337 MS |
522 | if (info_verbose && pid == -1) |
523 | warning ("Specified thread %d seems to have terminated", | |
524 | GET_THREAD (save_pid)); | |
a50cedad | 525 | } |
8fc2b417 SG |
526 | |
527 | procfs_ops.to_resume (pid, step, signo); | |
528 | ||
934741a1 | 529 | do_cleanups (old_chain); |
8fc2b417 SG |
530 | } |
531 | ||
a50cedad SG |
532 | /* Wait for any threads to stop. We may have to convert PID from a thread id |
533 | to a LWP id, and vice versa on the way out. */ | |
8fc2b417 SG |
534 | |
535 | static int | |
536 | sol_thread_wait (pid, ourstatus) | |
537 | int pid; | |
538 | struct target_waitstatus *ourstatus; | |
539 | { | |
8fc2b417 SG |
540 | int rtnval; |
541 | int save_pid; | |
934741a1 | 542 | struct cleanup *old_chain; |
8fc2b417 | 543 | |
8fc2b417 | 544 | save_pid = inferior_pid; |
934741a1 | 545 | old_chain = save_inferior_pid (); |
8fc2b417 SG |
546 | |
547 | inferior_pid = thread_to_lwp (inferior_pid, main_ph.pid); | |
3780c337 MS |
548 | if (inferior_pid == -1) |
549 | inferior_pid = procfs_first_available (); | |
8fc2b417 SG |
550 | |
551 | if (pid != -1) | |
3780c337 MS |
552 | { |
553 | int save_pid = pid; | |
554 | ||
555 | pid = thread_to_lwp (pid, -2); | |
556 | if (pid == -2) /* Inactive thread */ | |
557 | error ("This version of Solaris can't start inactive threads."); | |
558 | if (info_verbose && pid == -1) | |
559 | warning ("Specified thread %d seems to have terminated", | |
560 | GET_THREAD (save_pid)); | |
561 | } | |
8fc2b417 SG |
562 | |
563 | rtnval = procfs_ops.to_wait (pid, ourstatus); | |
564 | ||
3780c337 | 565 | if (ourstatus->kind != TARGET_WAITKIND_EXITED) |
8fc2b417 | 566 | { |
3780c337 MS |
567 | /* Map the LWP of interest back to the appropriate thread ID */ |
568 | rtnval = lwp_to_thread (rtnval); | |
569 | if (rtnval == -1) | |
570 | rtnval = save_pid; | |
571 | ||
572 | /* See if we have a new thread */ | |
573 | if (is_thread (rtnval) | |
574 | && rtnval != save_pid | |
575 | && !in_thread_list (rtnval)) | |
576 | { | |
577 | printf_filtered ("[New %s]\n", target_pid_to_str (rtnval)); | |
578 | add_thread (rtnval); | |
579 | } | |
8fc2b417 SG |
580 | } |
581 | ||
8fc2b417 SG |
582 | /* During process initialization, we may get here without the thread package |
583 | being initialized, since that can only happen after we've found the shared | |
584 | libs. */ | |
585 | ||
934741a1 SG |
586 | do_cleanups (old_chain); |
587 | ||
8fc2b417 SG |
588 | return rtnval; |
589 | } | |
590 | ||
591 | static void | |
592 | sol_thread_fetch_registers (regno) | |
593 | int regno; | |
594 | { | |
595 | thread_t thread; | |
596 | td_thrhandle_t thandle; | |
597 | td_err_e val; | |
9b33e492 | 598 | prgregset_t gregset; |
8fc2b417 | 599 | prfpregset_t fpregset; |
934741a1 | 600 | #if 0 |
8fc2b417 SG |
601 | int xregsize; |
602 | caddr_t xregset; | |
934741a1 | 603 | #endif |
8fc2b417 | 604 | |
0f5b751a MS |
605 | if (!is_thread (inferior_pid)) |
606 | { /* LWP: pass the request on to procfs.c */ | |
607 | procfs_ops.to_fetch_registers (regno); | |
608 | return; | |
609 | } | |
610 | ||
611 | /* Solaris thread: convert inferior_pid into a td_thrhandle_t */ | |
8fc2b417 | 612 | |
934741a1 | 613 | thread = GET_THREAD (inferior_pid); |
8fc2b417 SG |
614 | |
615 | if (thread == 0) | |
616 | error ("sol_thread_fetch_registers: thread == 0"); | |
617 | ||
4915acad | 618 | val = p_td_ta_map_id2thr (main_ta, thread, &thandle); |
8fc2b417 | 619 | if (val != TD_OK) |
934741a1 SG |
620 | error ("sol_thread_fetch_registers: td_ta_map_id2thr: %s", |
621 | td_err_string (val)); | |
8fc2b417 SG |
622 | |
623 | /* Get the integer regs */ | |
624 | ||
4915acad | 625 | val = p_td_thr_getgregs (&thandle, gregset); |
9b33e492 SG |
626 | if (val != TD_OK |
627 | && val != TD_PARTIALREG) | |
934741a1 SG |
628 | error ("sol_thread_fetch_registers: td_thr_getgregs %s", |
629 | td_err_string (val)); | |
8fc2b417 | 630 | |
9b33e492 SG |
631 | /* For the sparc, TD_PARTIALREG means that only i0->i7, l0->l7, pc and sp |
632 | are saved (by a thread context switch). */ | |
633 | ||
8fc2b417 SG |
634 | /* And, now the fp regs */ |
635 | ||
4915acad | 636 | val = p_td_thr_getfpregs (&thandle, &fpregset); |
9b33e492 SG |
637 | if (val != TD_OK |
638 | && val != TD_NOFPREGS) | |
934741a1 SG |
639 | error ("sol_thread_fetch_registers: td_thr_getfpregs %s", |
640 | td_err_string (val)); | |
8fc2b417 | 641 | |
9b33e492 SG |
642 | /* Note that we must call supply_{g fp}regset *after* calling the td routines |
643 | because the td routines call ps_lget* which affect the values stored in the | |
644 | registers array. */ | |
645 | ||
646 | supply_gregset (gregset); | |
647 | supply_fpregset (fpregset); | |
648 | ||
8fc2b417 SG |
649 | #if 0 |
650 | /* thread_db doesn't seem to handle this right */ | |
651 | val = td_thr_getxregsize (&thandle, &xregsize); | |
652 | if (val != TD_OK && val != TD_NOXREGS) | |
934741a1 SG |
653 | error ("sol_thread_fetch_registers: td_thr_getxregsize %s", |
654 | td_err_string (val)); | |
8fc2b417 SG |
655 | |
656 | if (val == TD_OK) | |
657 | { | |
658 | xregset = alloca (xregsize); | |
659 | val = td_thr_getxregs (&thandle, xregset); | |
660 | if (val != TD_OK) | |
934741a1 SG |
661 | error ("sol_thread_fetch_registers: td_thr_getxregs %s", |
662 | td_err_string (val)); | |
8fc2b417 SG |
663 | } |
664 | #endif | |
665 | } | |
666 | ||
667 | static void | |
668 | sol_thread_store_registers (regno) | |
669 | int regno; | |
670 | { | |
671 | thread_t thread; | |
672 | td_thrhandle_t thandle; | |
673 | td_err_e val; | |
674 | prgregset_t regset; | |
675 | prfpregset_t fpregset; | |
934741a1 | 676 | #if 0 |
8fc2b417 SG |
677 | int xregsize; |
678 | caddr_t xregset; | |
934741a1 | 679 | #endif |
8fc2b417 | 680 | |
0f5b751a MS |
681 | if (!is_thread (inferior_pid)) |
682 | { /* LWP: pass the request on to procfs.c */ | |
683 | procfs_ops.to_store_registers (regno); | |
684 | return; | |
685 | } | |
686 | ||
687 | /* Solaris thread: convert inferior_pid into a td_thrhandle_t */ | |
8fc2b417 | 688 | |
934741a1 | 689 | thread = GET_THREAD (inferior_pid); |
8fc2b417 | 690 | |
4915acad | 691 | val = p_td_ta_map_id2thr (main_ta, thread, &thandle); |
8fc2b417 | 692 | if (val != TD_OK) |
934741a1 SG |
693 | error ("sol_thread_store_registers: td_ta_map_id2thr %s", |
694 | td_err_string (val)); | |
8fc2b417 SG |
695 | |
696 | if (regno != -1) | |
697 | { /* Not writing all the regs */ | |
4915acad | 698 | val = p_td_thr_getgregs (&thandle, regset); |
8fc2b417 | 699 | if (val != TD_OK) |
934741a1 SG |
700 | error ("sol_thread_store_registers: td_thr_getgregs %s", |
701 | td_err_string (val)); | |
4915acad | 702 | val = p_td_thr_getfpregs (&thandle, &fpregset); |
8fc2b417 | 703 | if (val != TD_OK) |
934741a1 SG |
704 | error ("sol_thread_store_registers: td_thr_getfpregs %s", |
705 | td_err_string (val)); | |
8fc2b417 SG |
706 | |
707 | #if 0 | |
708 | /* thread_db doesn't seem to handle this right */ | |
709 | val = td_thr_getxregsize (&thandle, &xregsize); | |
710 | if (val != TD_OK && val != TD_NOXREGS) | |
934741a1 SG |
711 | error ("sol_thread_store_registers: td_thr_getxregsize %s", |
712 | td_err_string (val)); | |
8fc2b417 SG |
713 | |
714 | if (val == TD_OK) | |
715 | { | |
716 | xregset = alloca (xregsize); | |
717 | val = td_thr_getxregs (&thandle, xregset); | |
718 | if (val != TD_OK) | |
934741a1 SG |
719 | error ("sol_thread_store_registers: td_thr_getxregs %s", |
720 | td_err_string (val)); | |
8fc2b417 SG |
721 | } |
722 | #endif | |
723 | } | |
724 | ||
725 | fill_gregset (regset, regno); | |
934741a1 | 726 | fill_fpregset (fpregset, regno); |
8fc2b417 | 727 | |
4915acad | 728 | val = p_td_thr_setgregs (&thandle, regset); |
8fc2b417 | 729 | if (val != TD_OK) |
934741a1 SG |
730 | error ("sol_thread_store_registers: td_thr_setgregs %s", |
731 | td_err_string (val)); | |
4915acad | 732 | val = p_td_thr_setfpregs (&thandle, &fpregset); |
8fc2b417 | 733 | if (val != TD_OK) |
934741a1 SG |
734 | error ("sol_thread_store_registers: td_thr_setfpregs %s", |
735 | td_err_string (val)); | |
8fc2b417 SG |
736 | |
737 | #if 0 | |
738 | /* thread_db doesn't seem to handle this right */ | |
739 | val = td_thr_getxregsize (&thandle, &xregsize); | |
740 | if (val != TD_OK && val != TD_NOXREGS) | |
934741a1 SG |
741 | error ("sol_thread_store_registers: td_thr_getxregsize %s", |
742 | td_err_string (val)); | |
8fc2b417 SG |
743 | |
744 | /* Should probably do something about writing the xregs here, but what are | |
745 | they? */ | |
746 | #endif | |
747 | } | |
748 | ||
749 | /* Get ready to modify the registers array. On machines which store | |
750 | individual registers, this doesn't need to do anything. On machines | |
751 | which store all the registers in one fell swoop, this makes sure | |
752 | that registers contains all the registers from the program being | |
753 | debugged. */ | |
754 | ||
755 | static void | |
756 | sol_thread_prepare_to_store () | |
757 | { | |
758 | procfs_ops.to_prepare_to_store (); | |
759 | } | |
760 | ||
761 | static int | |
762 | sol_thread_xfer_memory (memaddr, myaddr, len, dowrite, target) | |
763 | CORE_ADDR memaddr; | |
764 | char *myaddr; | |
765 | int len; | |
766 | int dowrite; | |
767 | struct target_ops *target; /* ignored */ | |
768 | { | |
769 | int retval; | |
934741a1 | 770 | struct cleanup *old_chain; |
8fc2b417 | 771 | |
934741a1 | 772 | old_chain = save_inferior_pid (); |
8fc2b417 | 773 | |
0f5b751a MS |
774 | if (is_thread (inferior_pid) || /* A thread */ |
775 | !target_thread_alive (inferior_pid)) /* An lwp, but not alive */ | |
776 | inferior_pid = procfs_first_available (); /* Find any live lwp. */ | |
777 | /* Note: don't need to call switch_to_thread; we're just reading memory. */ | |
8fc2b417 SG |
778 | |
779 | retval = procfs_ops.to_xfer_memory (memaddr, myaddr, len, dowrite, target); | |
780 | ||
934741a1 | 781 | do_cleanups (old_chain); |
8fc2b417 SG |
782 | |
783 | return retval; | |
784 | } | |
785 | ||
786 | /* Print status information about what we're accessing. */ | |
787 | ||
788 | static void | |
789 | sol_thread_files_info (ignore) | |
790 | struct target_ops *ignore; | |
791 | { | |
792 | procfs_ops.to_files_info (ignore); | |
793 | } | |
794 | ||
795 | static void | |
796 | sol_thread_kill_inferior () | |
797 | { | |
798 | procfs_ops.to_kill (); | |
799 | } | |
800 | ||
801 | static void | |
802 | sol_thread_notice_signals (pid) | |
803 | int pid; | |
804 | { | |
805 | procfs_ops.to_notice_signals (pid); | |
806 | } | |
807 | ||
8fc2b417 SG |
808 | /* Fork an inferior process, and start debugging it with /proc. */ |
809 | ||
810 | static void | |
811 | sol_thread_create_inferior (exec_file, allargs, env) | |
812 | char *exec_file; | |
813 | char *allargs; | |
814 | char **env; | |
815 | { | |
816 | procfs_ops.to_create_inferior (exec_file, allargs, env); | |
817 | ||
3780c337 | 818 | if (sol_thread_active && inferior_pid != 0) |
8fc2b417 | 819 | { |
8fc2b417 SG |
820 | main_ph.pid = inferior_pid; /* Save for xfer_memory */ |
821 | ||
822 | push_target (&sol_thread_ops); | |
823 | ||
824 | inferior_pid = lwp_to_thread (inferior_pid); | |
3780c337 MS |
825 | if (inferior_pid == -1) |
826 | inferior_pid = main_ph.pid; | |
8fc2b417 SG |
827 | |
828 | add_thread (inferior_pid); | |
829 | } | |
830 | } | |
831 | ||
832 | /* This routine is called whenever a new symbol table is read in, or when all | |
a50cedad SG |
833 | symbol tables are removed. libthread_db can only be initialized when it |
834 | finds the right variables in libthread.so. Since it's a shared library, | |
835 | those variables don't show up until the library gets mapped and the symbol | |
836 | table is read in. */ | |
8fc2b417 SG |
837 | |
838 | void | |
839 | sol_thread_new_objfile (objfile) | |
840 | struct objfile *objfile; | |
841 | { | |
842 | td_err_e val; | |
843 | ||
844 | if (!objfile) | |
845 | { | |
846 | sol_thread_active = 0; | |
847 | ||
848 | return; | |
849 | } | |
850 | ||
3780c337 MS |
851 | /* don't do anything if init failed to resolve the libthread_db library */ |
852 | if (!procfs_suppress_run) | |
853 | return; | |
854 | ||
8fc2b417 SG |
855 | /* Now, initialize the thread debugging library. This needs to be done after |
856 | the shared libraries are located because it needs information from the | |
857 | user's thread library. */ | |
858 | ||
4915acad | 859 | val = p_td_init (); |
8fc2b417 | 860 | if (val != TD_OK) |
934741a1 | 861 | error ("target_new_objfile: td_init: %s", td_err_string (val)); |
8fc2b417 | 862 | |
4915acad | 863 | val = p_td_ta_new (&main_ph, &main_ta); |
8fc2b417 SG |
864 | if (val == TD_NOLIBTHREAD) |
865 | return; | |
866 | else if (val != TD_OK) | |
934741a1 | 867 | error ("target_new_objfile: td_ta_new: %s", td_err_string (val)); |
8fc2b417 SG |
868 | |
869 | sol_thread_active = 1; | |
870 | } | |
871 | ||
872 | /* Clean up after the inferior dies. */ | |
873 | ||
874 | static void | |
875 | sol_thread_mourn_inferior () | |
876 | { | |
3780c337 | 877 | unpush_target (&sol_thread_ops); |
8fc2b417 SG |
878 | procfs_ops.to_mourn_inferior (); |
879 | } | |
880 | ||
881 | /* Mark our target-struct as eligible for stray "run" and "attach" commands. */ | |
a50cedad | 882 | |
8fc2b417 SG |
883 | static int |
884 | sol_thread_can_run () | |
885 | { | |
886 | return procfs_suppress_run; | |
887 | } | |
888 | ||
3780c337 MS |
889 | /* |
890 | ||
891 | LOCAL FUNCTION | |
892 | ||
893 | sol_thread_alive - test thread for "aliveness" | |
894 | ||
895 | SYNOPSIS | |
896 | ||
897 | static bool sol_thread_alive (int pid); | |
898 | ||
899 | DESCRIPTION | |
900 | ||
901 | returns true if thread still active in inferior. | |
902 | ||
903 | */ | |
904 | ||
a50cedad | 905 | static int |
8fc2b417 SG |
906 | sol_thread_alive (pid) |
907 | int pid; | |
908 | { | |
3780c337 MS |
909 | if (is_thread (pid)) /* non-kernel thread */ |
910 | { | |
911 | td_err_e val; | |
912 | td_thrhandle_t th; | |
913 | ||
914 | pid = GET_THREAD (pid); | |
915 | if ((val = p_td_ta_map_id2thr (main_ta, pid, &th)) != TD_OK) | |
916 | return 0; /* thread not found */ | |
917 | if ((val = p_td_thr_validate (&th)) != TD_OK) | |
918 | return 0; /* thread not valid */ | |
919 | return 1; /* known thread: return true */ | |
920 | } | |
921 | else /* kernel thread (LWP): let procfs test it */ | |
922 | return procfs_ops.to_thread_alive (pid); | |
8fc2b417 SG |
923 | } |
924 | ||
a50cedad | 925 | static void |
8fc2b417 SG |
926 | sol_thread_stop () |
927 | { | |
928 | procfs_ops.to_stop (); | |
929 | } | |
a50cedad SG |
930 | \f |
931 | /* These routines implement the lower half of the thread_db interface. Ie: the | |
932 | ps_* routines. */ | |
8fc2b417 | 933 | |
a50cedad SG |
934 | /* The next four routines are called by thread_db to tell us to stop and stop |
935 | a particular process or lwp. Since GDB ensures that these are all stopped | |
936 | by the time we call anything in thread_db, these routines need to do | |
937 | nothing. */ | |
8fc2b417 | 938 | |
8fc2b417 SG |
939 | ps_err_e |
940 | ps_pstop (const struct ps_prochandle *ph) | |
941 | { | |
8fc2b417 SG |
942 | return PS_OK; |
943 | } | |
944 | ||
945 | ps_err_e | |
946 | ps_pcontinue (const struct ps_prochandle *ph) | |
947 | { | |
8fc2b417 SG |
948 | return PS_OK; |
949 | } | |
950 | ||
951 | ps_err_e | |
952 | ps_lstop (const struct ps_prochandle *ph, lwpid_t lwpid) | |
953 | { | |
8fc2b417 SG |
954 | return PS_OK; |
955 | } | |
956 | ||
957 | ps_err_e | |
958 | ps_lcontinue (const struct ps_prochandle *ph, lwpid_t lwpid) | |
959 | { | |
8fc2b417 SG |
960 | return PS_OK; |
961 | } | |
962 | ||
963 | ps_err_e | |
964 | ps_pglobal_lookup (const struct ps_prochandle *ph, const char *ld_object_name, | |
965 | const char *ld_symbol_name, paddr_t *ld_symbol_addr) | |
966 | { | |
967 | struct minimal_symbol *ms; | |
968 | ||
969 | ms = lookup_minimal_symbol (ld_symbol_name, NULL, NULL); | |
970 | ||
971 | if (!ms) | |
972 | return PS_NOSYM; | |
973 | ||
974 | *ld_symbol_addr = SYMBOL_VALUE_ADDRESS (ms); | |
975 | ||
976 | return PS_OK; | |
977 | } | |
978 | ||
a50cedad SG |
979 | /* Common routine for reading and writing memory. */ |
980 | ||
8fc2b417 SG |
981 | static ps_err_e |
982 | rw_common (int dowrite, const struct ps_prochandle *ph, paddr_t addr, | |
983 | char *buf, int size) | |
984 | { | |
934741a1 | 985 | struct cleanup *old_chain; |
8fc2b417 | 986 | |
934741a1 | 987 | old_chain = save_inferior_pid (); |
8fc2b417 | 988 | |
0f5b751a MS |
989 | if (is_thread (inferior_pid) || /* A thread */ |
990 | !target_thread_alive (inferior_pid)) /* An lwp, but not alive */ | |
991 | inferior_pid = procfs_first_available (); /* Find any live lwp. */ | |
992 | /* Note: don't need to call switch_to_thread; we're just reading memory. */ | |
8fc2b417 SG |
993 | |
994 | while (size > 0) | |
995 | { | |
996 | int cc; | |
997 | ||
998 | cc = procfs_ops.to_xfer_memory (addr, buf, size, dowrite, &procfs_ops); | |
999 | ||
1000 | if (cc < 0) | |
1001 | { | |
1002 | if (dowrite == 0) | |
3780c337 | 1003 | print_sys_errmsg ("rw_common (): read", errno); |
8fc2b417 | 1004 | else |
3780c337 | 1005 | print_sys_errmsg ("rw_common (): write", errno); |
8fc2b417 | 1006 | |
934741a1 | 1007 | do_cleanups (old_chain); |
8fc2b417 SG |
1008 | |
1009 | return PS_ERR; | |
1010 | } | |
1011 | size -= cc; | |
1012 | buf += cc; | |
1013 | } | |
1014 | ||
934741a1 | 1015 | do_cleanups (old_chain); |
8fc2b417 SG |
1016 | |
1017 | return PS_OK; | |
1018 | } | |
1019 | ||
1020 | ps_err_e | |
1021 | ps_pdread (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size) | |
1022 | { | |
1023 | return rw_common (0, ph, addr, buf, size); | |
1024 | } | |
1025 | ||
1026 | ps_err_e | |
1027 | ps_pdwrite (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size) | |
1028 | { | |
1029 | return rw_common (1, ph, addr, buf, size); | |
1030 | } | |
1031 | ||
1032 | ps_err_e | |
1033 | ps_ptread (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size) | |
1034 | { | |
1035 | return rw_common (0, ph, addr, buf, size); | |
1036 | } | |
1037 | ||
1038 | ps_err_e | |
1039 | ps_ptwrite (const struct ps_prochandle *ph, paddr_t addr, char *buf, int size) | |
1040 | { | |
1041 | return rw_common (1, ph, addr, buf, size); | |
1042 | } | |
1043 | ||
a50cedad SG |
1044 | /* Get integer regs */ |
1045 | ||
8fc2b417 SG |
1046 | ps_err_e |
1047 | ps_lgetregs (const struct ps_prochandle *ph, lwpid_t lwpid, | |
1048 | prgregset_t gregset) | |
1049 | { | |
934741a1 | 1050 | struct cleanup *old_chain; |
8fc2b417 | 1051 | |
934741a1 | 1052 | old_chain = save_inferior_pid (); |
8fc2b417 | 1053 | |
934741a1 | 1054 | inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid)); |
8fc2b417 SG |
1055 | |
1056 | procfs_ops.to_fetch_registers (-1); | |
1057 | fill_gregset (gregset, -1); | |
1058 | ||
934741a1 | 1059 | do_cleanups (old_chain); |
8fc2b417 SG |
1060 | |
1061 | return PS_OK; | |
1062 | } | |
1063 | ||
a50cedad SG |
1064 | /* Set integer regs */ |
1065 | ||
8fc2b417 SG |
1066 | ps_err_e |
1067 | ps_lsetregs (const struct ps_prochandle *ph, lwpid_t lwpid, | |
1068 | const prgregset_t gregset) | |
1069 | { | |
934741a1 | 1070 | struct cleanup *old_chain; |
8fc2b417 | 1071 | |
934741a1 | 1072 | old_chain = save_inferior_pid (); |
8fc2b417 | 1073 | |
934741a1 | 1074 | inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid)); |
8fc2b417 SG |
1075 | |
1076 | supply_gregset (gregset); | |
1077 | procfs_ops.to_store_registers (-1); | |
1078 | ||
934741a1 | 1079 | do_cleanups (old_chain); |
8fc2b417 SG |
1080 | |
1081 | return PS_OK; | |
1082 | } | |
1083 | ||
1084 | void | |
1085 | ps_plog (const char *fmt, ...) | |
1086 | { | |
1087 | va_list args; | |
1088 | ||
1089 | va_start (args, fmt); | |
1090 | ||
1091 | vfprintf_filtered (gdb_stderr, fmt, args); | |
1092 | } | |
1093 | ||
a50cedad SG |
1094 | /* Get size of extra register set. Currently a noop. */ |
1095 | ||
8fc2b417 SG |
1096 | ps_err_e |
1097 | ps_lgetxregsize (const struct ps_prochandle *ph, lwpid_t lwpid, int *xregsize) | |
1098 | { | |
934741a1 | 1099 | #if 0 |
8fc2b417 SG |
1100 | int lwp_fd; |
1101 | int regsize; | |
1102 | ps_err_e val; | |
1103 | ||
8fc2b417 SG |
1104 | val = get_lwp_fd (ph, lwpid, &lwp_fd); |
1105 | if (val != PS_OK) | |
1106 | return val; | |
1107 | ||
1108 | if (ioctl (lwp_fd, PIOCGXREGSIZE, ®size)) | |
1109 | { | |
1110 | if (errno == EINVAL) | |
1111 | return PS_NOFREGS; /* XXX Wrong code, but this is the closest | |
1112 | thing in proc_service.h */ | |
1113 | ||
1114 | print_sys_errmsg ("ps_lgetxregsize (): PIOCGXREGSIZE", errno); | |
1115 | return PS_ERR; | |
1116 | } | |
1117 | #endif | |
1118 | ||
1119 | return PS_OK; | |
1120 | } | |
1121 | ||
a50cedad SG |
1122 | /* Get extra register set. Currently a noop. */ |
1123 | ||
8fc2b417 SG |
1124 | ps_err_e |
1125 | ps_lgetxregs (const struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset) | |
1126 | { | |
934741a1 | 1127 | #if 0 |
8fc2b417 SG |
1128 | int lwp_fd; |
1129 | ps_err_e val; | |
1130 | ||
8fc2b417 SG |
1131 | val = get_lwp_fd (ph, lwpid, &lwp_fd); |
1132 | if (val != PS_OK) | |
1133 | return val; | |
1134 | ||
1135 | if (ioctl (lwp_fd, PIOCGXREG, xregset)) | |
1136 | { | |
1137 | print_sys_errmsg ("ps_lgetxregs (): PIOCGXREG", errno); | |
1138 | return PS_ERR; | |
1139 | } | |
1140 | #endif | |
1141 | ||
1142 | return PS_OK; | |
1143 | } | |
1144 | ||
a50cedad SG |
1145 | /* Set extra register set. Currently a noop. */ |
1146 | ||
8fc2b417 SG |
1147 | ps_err_e |
1148 | ps_lsetxregs (const struct ps_prochandle *ph, lwpid_t lwpid, caddr_t xregset) | |
1149 | { | |
934741a1 | 1150 | #if 0 |
8fc2b417 SG |
1151 | int lwp_fd; |
1152 | ps_err_e val; | |
1153 | ||
8fc2b417 SG |
1154 | val = get_lwp_fd (ph, lwpid, &lwp_fd); |
1155 | if (val != PS_OK) | |
1156 | return val; | |
1157 | ||
1158 | if (ioctl (lwp_fd, PIOCSXREG, xregset)) | |
1159 | { | |
1160 | print_sys_errmsg ("ps_lsetxregs (): PIOCSXREG", errno); | |
1161 | return PS_ERR; | |
1162 | } | |
1163 | #endif | |
1164 | ||
1165 | return PS_OK; | |
1166 | } | |
1167 | ||
a50cedad SG |
1168 | /* Get floating-point regs. */ |
1169 | ||
8fc2b417 SG |
1170 | ps_err_e |
1171 | ps_lgetfpregs (const struct ps_prochandle *ph, lwpid_t lwpid, | |
1172 | prfpregset_t *fpregset) | |
1173 | { | |
934741a1 | 1174 | struct cleanup *old_chain; |
8fc2b417 | 1175 | |
934741a1 | 1176 | old_chain = save_inferior_pid (); |
8fc2b417 | 1177 | |
934741a1 | 1178 | inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid)); |
8fc2b417 SG |
1179 | |
1180 | procfs_ops.to_fetch_registers (-1); | |
934741a1 | 1181 | fill_fpregset (*fpregset, -1); |
8fc2b417 | 1182 | |
934741a1 | 1183 | do_cleanups (old_chain); |
8fc2b417 SG |
1184 | |
1185 | return PS_OK; | |
1186 | } | |
1187 | ||
a50cedad SG |
1188 | /* Set floating-point regs. */ |
1189 | ||
8fc2b417 SG |
1190 | ps_err_e |
1191 | ps_lsetfpregs (const struct ps_prochandle *ph, lwpid_t lwpid, | |
1192 | const prfpregset_t *fpregset) | |
1193 | { | |
934741a1 | 1194 | struct cleanup *old_chain; |
8fc2b417 | 1195 | |
934741a1 | 1196 | old_chain = save_inferior_pid (); |
8fc2b417 | 1197 | |
934741a1 | 1198 | inferior_pid = BUILD_LWP (lwpid, PIDGET (inferior_pid)); |
8fc2b417 | 1199 | |
934741a1 | 1200 | supply_fpregset (*fpregset); |
8fc2b417 SG |
1201 | procfs_ops.to_store_registers (-1); |
1202 | ||
934741a1 | 1203 | do_cleanups (old_chain); |
8fc2b417 SG |
1204 | |
1205 | return PS_OK; | |
1206 | } | |
1207 | \f | |
a50cedad SG |
1208 | /* Convert a pid to printable form. */ |
1209 | ||
8fc2b417 SG |
1210 | char * |
1211 | solaris_pid_to_str (pid) | |
1212 | int pid; | |
1213 | { | |
1214 | static char buf[100]; | |
1215 | ||
3780c337 MS |
1216 | /* in case init failed to resolve the libthread_db library */ |
1217 | if (!procfs_suppress_run) | |
1218 | return procfs_pid_to_str (pid); | |
1219 | ||
934741a1 | 1220 | if (is_thread (pid)) |
8fc2b417 SG |
1221 | { |
1222 | int lwp; | |
1223 | ||
1224 | lwp = thread_to_lwp (pid, -2); | |
1225 | ||
3780c337 MS |
1226 | if (lwp == -1) |
1227 | sprintf (buf, "Thread %d (defunct)", GET_THREAD (pid)); | |
1228 | else if (lwp != -2) | |
934741a1 | 1229 | sprintf (buf, "Thread %d (LWP %d)", GET_THREAD (pid), GET_LWP (lwp)); |
8fc2b417 | 1230 | else |
934741a1 | 1231 | sprintf (buf, "Thread %d ", GET_THREAD (pid)); |
8fc2b417 | 1232 | } |
3780c337 | 1233 | else if (GET_LWP (pid) != 0) |
934741a1 | 1234 | sprintf (buf, "LWP %d ", GET_LWP (pid)); |
3780c337 MS |
1235 | else |
1236 | sprintf (buf, "process %d ", PIDGET (pid)); | |
8fc2b417 SG |
1237 | |
1238 | return buf; | |
1239 | } | |
1240 | \f | |
0f5b751a MS |
1241 | |
1242 | #ifdef MAINTENANCE_CMDS | |
1243 | /* Worker bee for info sol-thread command. This is a callback function that | |
1244 | gets called once for each Solaris thread (ie. not kernel thread) in the | |
1245 | inferior. Print anything interesting that we can think of. */ | |
1246 | ||
1247 | static int | |
1248 | info_cb (th, s) | |
1249 | const td_thrhandle_t *th; | |
1250 | void *s; | |
1251 | { | |
1252 | td_err_e ret; | |
1253 | td_thrinfo_t ti; | |
1254 | struct minimal_symbol *msym; | |
1255 | ||
1256 | if ((ret = p_td_thr_get_info (th, &ti)) == TD_OK) | |
1257 | { | |
1258 | printf_filtered ("%s thread #%d, lwp %d, ", | |
1259 | ti.ti_type == TD_THR_SYSTEM ? "system" : "user ", | |
1260 | ti.ti_tid, ti.ti_lid); | |
1261 | switch (ti.ti_state) { | |
1262 | default: | |
1263 | case TD_THR_UNKNOWN: printf_filtered ("<unknown state>"); break; | |
1264 | case TD_THR_STOPPED: printf_filtered ("(stopped)"); break; | |
1265 | case TD_THR_RUN: printf_filtered ("(run) "); break; | |
1266 | case TD_THR_ACTIVE: printf_filtered ("(active) "); break; | |
1267 | case TD_THR_ZOMBIE: printf_filtered ("(zombie) "); break; | |
1268 | case TD_THR_SLEEP: printf_filtered ("(asleep) "); break; | |
1269 | case TD_THR_STOPPED_ASLEEP: | |
1270 | printf_filtered ("(stopped asleep)"); break; | |
1271 | } | |
1272 | /* Print thr_create start function: */ | |
1273 | if (ti.ti_startfunc != 0) | |
1274 | if (msym = lookup_minimal_symbol_by_pc (ti.ti_startfunc)) | |
1275 | printf_filtered (" startfunc: %s\n", SYMBOL_NAME (msym)); | |
1276 | else | |
1277 | printf_filtered (" startfunc: 0x%08x\n", ti.ti_startfunc); | |
1278 | ||
1279 | /* If thread is asleep, print function that went to sleep: */ | |
1280 | if (ti.ti_state == TD_THR_SLEEP) | |
1281 | if (msym = lookup_minimal_symbol_by_pc (ti.ti_pc)) | |
1282 | printf_filtered (" - Sleep func: %s\n", SYMBOL_NAME (msym)); | |
1283 | else | |
1284 | printf_filtered (" - Sleep func: 0x%08x\n", ti.ti_startfunc); | |
1285 | ||
1286 | /* Wrap up line, if necessary */ | |
1287 | if (ti.ti_state != TD_THR_SLEEP && ti.ti_startfunc == 0) | |
1288 | printf_filtered ("\n"); /* don't you hate counting newlines? */ | |
1289 | } | |
1290 | else | |
1291 | warning ("info sol-thread: failed to get info for thread."); | |
1292 | ||
1293 | return 0; | |
1294 | } | |
1295 | ||
1296 | /* List some state about each Solaris user thread in the inferior. */ | |
1297 | ||
1298 | static void | |
1299 | info_solthreads (args, from_tty) | |
1300 | char *args; | |
1301 | int from_tty; | |
1302 | { | |
1303 | p_td_ta_thr_iter (main_ta, info_cb, args, | |
1304 | TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, | |
1305 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); | |
1306 | } | |
1307 | #endif /* MAINTENANCE_CMDS */ | |
1308 | ||
8fc2b417 SG |
1309 | struct target_ops sol_thread_ops = { |
1310 | "solaris-threads", /* to_shortname */ | |
1311 | "Solaris threads and pthread.", /* to_longname */ | |
1312 | "Solaris threads and pthread support.", /* to_doc */ | |
1313 | sol_thread_open, /* to_open */ | |
1314 | 0, /* to_close */ | |
4915acad | 1315 | sol_thread_attach, /* to_attach */ |
8fc2b417 | 1316 | sol_thread_detach, /* to_detach */ |
4915acad SG |
1317 | sol_thread_resume, /* to_resume */ |
1318 | sol_thread_wait, /* to_wait */ | |
8fc2b417 SG |
1319 | sol_thread_fetch_registers, /* to_fetch_registers */ |
1320 | sol_thread_store_registers, /* to_store_registers */ | |
1321 | sol_thread_prepare_to_store, /* to_prepare_to_store */ | |
4915acad SG |
1322 | sol_thread_xfer_memory, /* to_xfer_memory */ |
1323 | sol_thread_files_info, /* to_files_info */ | |
8fc2b417 SG |
1324 | memory_insert_breakpoint, /* to_insert_breakpoint */ |
1325 | memory_remove_breakpoint, /* to_remove_breakpoint */ | |
1326 | terminal_init_inferior, /* to_terminal_init */ | |
1327 | terminal_inferior, /* to_terminal_inferior */ | |
1328 | terminal_ours_for_output, /* to_terminal_ours_for_output */ | |
1329 | terminal_ours, /* to_terminal_ours */ | |
1330 | child_terminal_info, /* to_terminal_info */ | |
1331 | sol_thread_kill_inferior, /* to_kill */ | |
1332 | 0, /* to_load */ | |
1333 | 0, /* to_lookup_symbol */ | |
1334 | sol_thread_create_inferior, /* to_create_inferior */ | |
1335 | sol_thread_mourn_inferior, /* to_mourn_inferior */ | |
1336 | sol_thread_can_run, /* to_can_run */ | |
1337 | sol_thread_notice_signals, /* to_notice_signals */ | |
1338 | sol_thread_alive, /* to_thread_alive */ | |
1339 | sol_thread_stop, /* to_stop */ | |
1340 | process_stratum, /* to_stratum */ | |
1341 | 0, /* to_next */ | |
1342 | 1, /* to_has_all_memory */ | |
1343 | 1, /* to_has_memory */ | |
1344 | 1, /* to_has_stack */ | |
1345 | 1, /* to_has_registers */ | |
1346 | 1, /* to_has_execution */ | |
1347 | 0, /* sections */ | |
1348 | 0, /* sections_end */ | |
1349 | OPS_MAGIC /* to_magic */ | |
1350 | }; | |
1351 | ||
1352 | void | |
1353 | _initialize_sol_thread () | |
1354 | { | |
4915acad SG |
1355 | void *dlhandle; |
1356 | ||
1357 | dlhandle = dlopen ("libthread_db.so.1", RTLD_NOW); | |
1358 | if (!dlhandle) | |
1359 | goto die; | |
1360 | ||
1361 | #define resolve(X) \ | |
1362 | if (!(p_##X = dlsym (dlhandle, #X))) \ | |
1363 | goto die; | |
1364 | ||
1365 | resolve (td_log); | |
1366 | resolve (td_ta_new); | |
1367 | resolve (td_ta_delete); | |
1368 | resolve (td_init); | |
1369 | resolve (td_ta_get_ph); | |
1370 | resolve (td_ta_get_nthreads); | |
1371 | resolve (td_ta_tsd_iter); | |
1372 | resolve (td_ta_thr_iter); | |
1373 | resolve (td_thr_validate); | |
1374 | resolve (td_thr_tsd); | |
1375 | resolve (td_thr_get_info); | |
1376 | resolve (td_thr_getfpregs); | |
1377 | resolve (td_thr_getxregsize); | |
1378 | resolve (td_thr_getxregs); | |
1379 | resolve (td_thr_sigsetmask); | |
1380 | resolve (td_thr_setprio); | |
1381 | resolve (td_thr_setsigpending); | |
1382 | resolve (td_thr_setfpregs); | |
1383 | resolve (td_thr_setxregs); | |
1384 | resolve (td_ta_map_id2thr); | |
1385 | resolve (td_ta_map_lwp2thr); | |
1386 | resolve (td_thr_getgregs); | |
1387 | resolve (td_thr_setgregs); | |
1388 | ||
8fc2b417 SG |
1389 | add_target (&sol_thread_ops); |
1390 | ||
1391 | procfs_suppress_run = 1; | |
4915acad | 1392 | |
0f5b751a MS |
1393 | #ifdef MAINTENANCE_CMDS |
1394 | add_cmd ("sol-threads", class_maintenance, info_solthreads, | |
1395 | "Show info on Solaris user threads.\n", &maintenanceinfolist); | |
1396 | #endif /* MAINTENANCE_CMDS */ | |
1397 | ||
4915acad SG |
1398 | return; |
1399 | ||
1400 | die: | |
1401 | ||
1402 | fprintf_unfiltered (gdb_stderr, "[GDB will not be able to debug user-mode threads: %s]\n", dlerror ()); | |
1403 | ||
1404 | if (dlhandle) | |
1405 | dlclose (dlhandle); | |
1406 | ||
1407 | return; | |
8fc2b417 | 1408 | } |