Convert struct target_ops to C++
[deliverable/binutils-gdb.git] / gdb / bsd-uthread.c
1 /* BSD user-level threads support.
2
3 Copyright (C) 2005-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdbcore.h"
22 #include "gdbthread.h"
23 #include "inferior.h"
24 #include "objfiles.h"
25 #include "observable.h"
26 #include "regcache.h"
27 #include "solib.h"
28 #include "solist.h"
29 #include "symfile.h"
30 #include "target.h"
31
32 #include "gdb_obstack.h"
33
34 #include "bsd-uthread.h"
35
36 struct bsd_uthread_target final : public target_ops
37 {
38 bsd_uthread_target ()
39 { to_stratum = thread_stratum; }
40
41 const char *shortname () override
42 { return "bsd-uthreads"; }
43
44 const char *longname () override
45 { return _("BSD user-level threads"); }
46
47 const char *doc () override
48 { return _("BSD user-level threads"); }
49
50 void close () override;
51
52 void mourn_inferior () override;
53
54 void fetch_registers (struct regcache *, int) override;
55 void store_registers (struct regcache *, int) override;
56
57 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
58 void resume (ptid_t, int, enum gdb_signal) override;
59
60 int thread_alive (ptid_t ptid) override;
61
62 void update_thread_list () override;
63
64 const char *extra_thread_info (struct thread_info *) override;
65
66 const char *pid_to_str (ptid_t) override;
67 };
68
69 static bsd_uthread_target bsd_uthread_ops;
70 \f
71
72 /* Architecture-specific operations. */
73
74 /* Per-architecture data key. */
75 static struct gdbarch_data *bsd_uthread_data;
76
77 struct bsd_uthread_ops
78 {
79 /* Supply registers for an inactive thread to a register cache. */
80 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
81
82 /* Collect registers for an inactive thread from a register cache. */
83 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
84 };
85
86 static void *
87 bsd_uthread_init (struct obstack *obstack)
88 {
89 struct bsd_uthread_ops *ops;
90
91 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
92 return ops;
93 }
94
95 /* Set the function that supplies registers from an inactive thread
96 for architecture GDBARCH to SUPPLY_UTHREAD. */
97
98 void
99 bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
100 void (*supply_uthread) (struct regcache *,
101 int, CORE_ADDR))
102 {
103 struct bsd_uthread_ops *ops
104 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
105
106 ops->supply_uthread = supply_uthread;
107 }
108
109 /* Set the function that collects registers for an inactive thread for
110 architecture GDBARCH to SUPPLY_UTHREAD. */
111
112 void
113 bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
114 void (*collect_uthread) (const struct regcache *,
115 int, CORE_ADDR))
116 {
117 struct bsd_uthread_ops *ops
118 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
119
120 ops->collect_uthread = collect_uthread;
121 }
122
123 /* Magic number to help recognize a valid thread structure. */
124 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
125
126 /* Check whether the thread structure at ADDR is valid. */
127
128 static void
129 bsd_uthread_check_magic (CORE_ADDR addr)
130 {
131 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
132 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
133
134 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
135 error (_("Bad magic"));
136 }
137
138 /* Thread states. */
139 #define BSD_UTHREAD_PS_RUNNING 0
140 #define BSD_UTHREAD_PS_DEAD 18
141
142 /* Address of the pointer to the thread structure for the running
143 thread. */
144 static CORE_ADDR bsd_uthread_thread_run_addr;
145
146 /* Address of the list of all threads. */
147 static CORE_ADDR bsd_uthread_thread_list_addr;
148
149 /* Offsets of various "interesting" bits in the thread structure. */
150 static int bsd_uthread_thread_state_offset = -1;
151 static int bsd_uthread_thread_next_offset = -1;
152 static int bsd_uthread_thread_ctx_offset;
153
154 /* Name of shared threads library. */
155 static const char *bsd_uthread_solib_name;
156
157 /* Non-zero if the thread startum implemented by this module is active. */
158 static int bsd_uthread_active;
159
160 static CORE_ADDR
161 bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
162 {
163 struct bound_minimal_symbol sym;
164
165 sym = lookup_minimal_symbol (name, NULL, objfile);
166 if (sym.minsym)
167 return BMSYMBOL_VALUE_ADDRESS (sym);
168
169 return 0;
170 }
171
172 static int
173 bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
174 {
175 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
176 CORE_ADDR addr;
177
178 addr = bsd_uthread_lookup_address (name, objfile);
179 if (addr == 0)
180 return 0;
181
182 return read_memory_unsigned_integer (addr, 4, byte_order);
183 }
184
185 static CORE_ADDR
186 bsd_uthread_read_memory_address (CORE_ADDR addr)
187 {
188 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
189 return read_memory_typed_address (addr, ptr_type);
190 }
191
192 /* If OBJFILE contains the symbols corresponding to one of the
193 supported user-level threads libraries, activate the thread stratum
194 implemented by this module. */
195
196 static int
197 bsd_uthread_activate (struct objfile *objfile)
198 {
199 struct gdbarch *gdbarch = target_gdbarch ();
200 struct bsd_uthread_ops *ops
201 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
202
203 /* Skip if the thread stratum has already been activated. */
204 if (bsd_uthread_active)
205 return 0;
206
207 /* There's no point in enabling this module if no
208 architecture-specific operations are provided. */
209 if (!ops->supply_uthread)
210 return 0;
211
212 bsd_uthread_thread_run_addr =
213 bsd_uthread_lookup_address ("_thread_run", objfile);
214 if (bsd_uthread_thread_run_addr == 0)
215 return 0;
216
217 bsd_uthread_thread_list_addr =
218 bsd_uthread_lookup_address ("_thread_list", objfile);
219 if (bsd_uthread_thread_list_addr == 0)
220 return 0;
221
222 bsd_uthread_thread_state_offset =
223 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
224 if (bsd_uthread_thread_state_offset == 0)
225 return 0;
226
227 bsd_uthread_thread_next_offset =
228 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
229 if (bsd_uthread_thread_next_offset == 0)
230 return 0;
231
232 bsd_uthread_thread_ctx_offset =
233 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
234
235 push_target (&bsd_uthread_ops);
236 bsd_uthread_active = 1;
237 return 1;
238 }
239
240 /* Cleanup due to deactivation. */
241
242 void
243 bsd_uthread_target::close ()
244 {
245 bsd_uthread_active = 0;
246 bsd_uthread_thread_run_addr = 0;
247 bsd_uthread_thread_list_addr = 0;
248 bsd_uthread_thread_state_offset = 0;
249 bsd_uthread_thread_next_offset = 0;
250 bsd_uthread_thread_ctx_offset = 0;
251 bsd_uthread_solib_name = NULL;
252 }
253
254 /* Deactivate the thread stratum implemented by this module. */
255
256 static void
257 bsd_uthread_deactivate (void)
258 {
259 /* Skip if the thread stratum has already been deactivated. */
260 if (!bsd_uthread_active)
261 return;
262
263 unpush_target (&bsd_uthread_ops);
264 }
265
266 static void
267 bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
268 {
269 bsd_uthread_activate (NULL);
270 }
271
272 /* Likely candidates for the threads library. */
273 static const char *bsd_uthread_solib_names[] =
274 {
275 "/usr/lib/libc_r.so", /* FreeBSD */
276 "/usr/lib/libpthread.so", /* OpenBSD */
277 NULL
278 };
279
280 static void
281 bsd_uthread_solib_loaded (struct so_list *so)
282 {
283 const char **names = bsd_uthread_solib_names;
284
285 for (names = bsd_uthread_solib_names; *names; names++)
286 {
287 if (startswith (so->so_original_name, *names))
288 {
289 solib_read_symbols (so, 0);
290
291 if (bsd_uthread_activate (so->objfile))
292 {
293 bsd_uthread_solib_name = so->so_original_name;
294 return;
295 }
296 }
297 }
298 }
299
300 static void
301 bsd_uthread_solib_unloaded (struct so_list *so)
302 {
303 if (!bsd_uthread_solib_name)
304 return;
305
306 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
307 bsd_uthread_deactivate ();
308 }
309
310 void
311 bsd_uthread_target::mourn_inferior ()
312 {
313 struct target_ops *beneath = find_target_beneath (this);
314 beneath->mourn_inferior ();
315 bsd_uthread_deactivate ();
316 }
317
318 void
319 bsd_uthread_target::fetch_registers (struct regcache *regcache, int regnum)
320 {
321 struct gdbarch *gdbarch = regcache->arch ();
322 struct bsd_uthread_ops *uthread_ops
323 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
324 ptid_t ptid = regcache_get_ptid (regcache);
325 CORE_ADDR addr = ptid_get_tid (ptid);
326 struct target_ops *beneath = find_target_beneath (this);
327 CORE_ADDR active_addr;
328 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
329
330 /* We are doing operations (e.g. reading memory) that rely on
331 inferior_ptid. */
332 inferior_ptid = ptid;
333
334 /* Always fetch the appropriate registers from the layer beneath. */
335 beneath->fetch_registers (regcache, regnum);
336
337 /* FIXME: That might have gotten us more than we asked for. Make
338 sure we overwrite all relevant registers with values from the
339 thread structure. This can go once we fix the underlying target. */
340 regnum = -1;
341
342 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
343 if (addr != 0 && addr != active_addr)
344 {
345 bsd_uthread_check_magic (addr);
346 uthread_ops->supply_uthread (regcache, regnum,
347 addr + bsd_uthread_thread_ctx_offset);
348 }
349 }
350
351 void
352 bsd_uthread_target::store_registers (struct regcache *regcache, int regnum)
353 {
354 struct gdbarch *gdbarch = regcache->arch ();
355 struct bsd_uthread_ops *uthread_ops
356 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
357 struct target_ops *beneath = find_target_beneath (this);
358 ptid_t ptid = regcache_get_ptid (regcache);
359 CORE_ADDR addr = ptid_get_tid (ptid);
360 CORE_ADDR active_addr;
361 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
362
363 /* We are doing operations (e.g. reading memory) that rely on
364 inferior_ptid. */
365 inferior_ptid = ptid;
366
367 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
368 if (addr != 0 && addr != active_addr)
369 {
370 bsd_uthread_check_magic (addr);
371 uthread_ops->collect_uthread (regcache, regnum,
372 addr + bsd_uthread_thread_ctx_offset);
373 }
374 else
375 {
376 /* Updating the thread that is currently running; pass the
377 request to the layer beneath. */
378 beneath->store_registers (regcache, regnum);
379 }
380 }
381
382 ptid_t
383 bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
384 int options)
385 {
386 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
387 CORE_ADDR addr;
388 struct target_ops *beneath = find_target_beneath (this);
389
390 /* Pass the request to the layer beneath. */
391 ptid = beneath->wait (ptid, status, options);
392
393 /* If the process is no longer alive, there's no point in figuring
394 out the thread ID. It will fail anyway. */
395 if (status->kind == TARGET_WAITKIND_SIGNALLED
396 || status->kind == TARGET_WAITKIND_EXITED)
397 return ptid;
398
399 /* Fetch the corresponding thread ID, and augment the returned
400 process ID with it. */
401 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
402 if (addr != 0)
403 {
404 gdb_byte buf[4];
405
406 /* FIXME: For executables linked statically with the threads
407 library, we end up here before the program has actually been
408 executed. In that case ADDR will be garbage since it has
409 been read from the wrong virtual memory image. */
410 if (target_read_memory (addr, buf, 4) == 0)
411 {
412 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
413 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
414 ptid = ptid_build (ptid_get_pid (ptid), 0, addr);
415 }
416 }
417
418 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
419 ptid with tid set, then ptid is still the initial thread of
420 the process. Notify GDB core about it. */
421 if (ptid_get_tid (inferior_ptid) == 0
422 && ptid_get_tid (ptid) != 0 && !in_thread_list (ptid))
423 thread_change_ptid (inferior_ptid, ptid);
424
425 /* Don't let the core see a ptid without a corresponding thread. */
426 if (!in_thread_list (ptid) || is_exited (ptid))
427 add_thread (ptid);
428
429 return ptid;
430 }
431
432 void
433 bsd_uthread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
434 {
435 /* Pass the request to the layer beneath. */
436 struct target_ops *beneath = find_target_beneath (this);
437 beneath->resume (ptid, step, sig);
438 }
439
440 int
441 bsd_uthread_target::thread_alive (ptid_t ptid)
442 {
443 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
444 struct target_ops *beneath = find_target_beneath (this);
445 CORE_ADDR addr = ptid_get_tid (ptid);
446
447 if (addr != 0)
448 {
449 int offset = bsd_uthread_thread_state_offset;
450 ULONGEST state;
451
452 bsd_uthread_check_magic (addr);
453
454 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
455 if (state == BSD_UTHREAD_PS_DEAD)
456 return 0;
457 }
458
459 return beneath->thread_alive (ptid);
460 }
461
462 void
463 bsd_uthread_target::update_thread_list ()
464 {
465 pid_t pid = ptid_get_pid (inferior_ptid);
466 int offset = bsd_uthread_thread_next_offset;
467 CORE_ADDR addr;
468
469 prune_threads ();
470
471 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
472 while (addr != 0)
473 {
474 ptid_t ptid = ptid_build (pid, 0, addr);
475
476 if (!in_thread_list (ptid) || is_exited (ptid))
477 {
478 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
479 is still the initial thread of the process. Notify GDB
480 core about it. */
481 if (ptid_get_tid (inferior_ptid) == 0)
482 thread_change_ptid (inferior_ptid, ptid);
483 else
484 add_thread (ptid);
485 }
486
487 addr = bsd_uthread_read_memory_address (addr + offset);
488 }
489 }
490
491 /* Possible states a thread can be in. */
492 static const char *bsd_uthread_state[] =
493 {
494 "RUNNING",
495 "SIGTHREAD",
496 "MUTEX_WAIT",
497 "COND_WAIT",
498 "FDLR_WAIT",
499 "FDLW_WAIT",
500 "FDR_WAIT",
501 "FDW_WAIT",
502 "FILE_WAIT",
503 "POLL_WAIT",
504 "SELECT_WAIT",
505 "SLEEP_WAIT",
506 "WAIT_WAIT",
507 "SIGSUSPEND",
508 "SIGWAIT",
509 "SPINBLOCK",
510 "JOIN",
511 "SUSPENDED",
512 "DEAD",
513 "DEADLOCK"
514 };
515
516 /* Return a string describing th state of the thread specified by
517 INFO. */
518
519 const char *
520 bsd_uthread_target::extra_thread_info (thread_info *info)
521 {
522 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
523 CORE_ADDR addr = ptid_get_tid (info->ptid);
524
525 if (addr != 0)
526 {
527 int offset = bsd_uthread_thread_state_offset;
528 ULONGEST state;
529
530 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
531 if (state < ARRAY_SIZE (bsd_uthread_state))
532 return bsd_uthread_state[state];
533 }
534
535 return NULL;
536 }
537
538 const char *
539 bsd_uthread_target::pid_to_str (ptid_t ptid)
540 {
541 if (ptid_get_tid (ptid) != 0)
542 {
543 static char buf[64];
544
545 xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx",
546 ptid_get_pid (ptid), ptid_get_tid (ptid));
547 return buf;
548 }
549
550 return normal_pid_to_str (ptid);
551 }
552
553 void
554 _initialize_bsd_uthread (void)
555 {
556 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
557
558 gdb::observers::inferior_created.attach (bsd_uthread_inferior_created);
559 gdb::observers::solib_loaded.attach (bsd_uthread_solib_loaded);
560 gdb::observers::solib_unloaded.attach (bsd_uthread_solib_unloaded);
561 }
This page took 0.08482 seconds and 5 git commands to generate.