Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / coregrind / m_gdbserver / target.c
blob0093ce828dad64ae7d1bad60754d04ce314e59a6
1 /* Target operations for the remote server for GDB.
2 Copyright (C) 2002, 2004, 2005, 2011
3 Free Software Foundation, Inc.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
8 It has been modified to integrate it in valgrind
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
25 #include "server.h"
26 #include "target.h"
27 #include "regdef.h"
28 #include "regcache.h"
29 #include "valgrind_low.h"
30 #include "gdb/signals.h"
31 #include "pub_core_aspacemgr.h"
32 #include "pub_core_machine.h"
33 #include "pub_core_threadstate.h"
34 #include "pub_core_transtab.h"
35 #include "pub_core_gdbserver.h"
36 #include "pub_core_debuginfo.h"
39 /* the_low_target defines the architecture specific aspects depending
40 on the cpu */
41 static struct valgrind_target_ops the_low_target;
43 static
44 char *image_ptid(unsigned long ptid)
46 static char result[50]; // large enough
47 VG_(sprintf) (result, "id %lu", ptid);
48 return result;
50 #define get_thread(inf) ((struct thread_info *)(inf))
51 static
52 void remove_thread_if_not_in_vg_threads (struct inferior_list_entry *inf)
54 struct thread_info *thread = get_thread (inf);
55 if (!VG_(lwpid_to_vgtid)(thread_to_gdb_id(thread))) {
56 dlog(1, "removing gdb ptid %s\n",
57 image_ptid(thread_to_gdb_id(thread)));
58 remove_thread (thread);
62 /* synchronize threads known by valgrind and threads known by gdbserver */
63 static
64 void valgrind_update_threads (int pid)
66 ThreadId tid;
67 ThreadState *ts;
68 unsigned long ptid;
69 struct thread_info *ti;
71 /* call remove_thread for all gdb threads not in valgrind threads */
72 for_each_inferior (&all_threads, remove_thread_if_not_in_vg_threads);
74 /* call add_thread for all valgrind threads not known in gdb all_threads */
75 for (tid = 1; tid < VG_N_THREADS; tid++) {
77 #define LOCAL_THREAD_TRACE " ti* %p vgtid %u status %s as gdb ptid %s lwpid %d\n", \
78 ti, tid, VG_(name_of_ThreadStatus) (ts->status), \
79 image_ptid (ptid), ts->os_state.lwpid
81 if (VG_(is_valid_tid) (tid)) {
82 ts = VG_(get_ThreadState) (tid);
83 ptid = ts->os_state.lwpid;
84 ti = gdb_id_to_thread (ptid);
85 if (!ti) {
86 /* we do not report the threads which are not yet fully
87 initialized otherwise this creates duplicated threads
88 in gdb: once with pid xxx lwpid 0, then after that
89 with pid xxx lwpid yyy. */
90 if (ts->status != VgTs_Init) {
91 dlog(1, "adding_thread" LOCAL_THREAD_TRACE);
92 add_thread (ptid, ts, ptid);
94 } else {
95 dlog(2, "(known thread)" LOCAL_THREAD_TRACE);
98 #undef LOCAL_THREAD_TRACE
102 static
103 struct reg* build_shadow_arch (struct reg *reg_defs, int n) {
104 int i, r;
105 static const char *postfix[3] = { "", "s1", "s2" };
106 struct reg *new_regs = malloc(3 * n * sizeof(reg_defs[0]));
107 int reg_set_len = reg_defs[n-1].offset + reg_defs[n-1].size;
109 for (i = 0; i < 3; i++) {
110 for (r = 0; r < n; r++) {
111 char *regname = malloc(strlen(reg_defs[r].name)
112 + strlen (postfix[i]) + 1);
113 strcpy (regname, reg_defs[r].name);
114 strcat (regname, postfix[i]);
115 new_regs[i*n + r].name = regname;
116 new_regs[i*n + r].offset = i*reg_set_len + reg_defs[r].offset;
117 new_regs[i*n + r].size = reg_defs[r].size;
118 dlog(1,
119 "%-10s Nr %d offset(bit) %d offset(byte) %d size(bit) %d\n",
120 new_regs[i*n + r].name, i*n + r, new_regs[i*n + r].offset,
121 (new_regs[i*n + r].offset) / 8, new_regs[i*n + r].size);
125 return new_regs;
129 static CORE_ADDR stopped_data_address = 0;
130 void VG_(set_watchpoint_stop_address) (Addr addr)
132 stopped_data_address = addr;
135 int valgrind_stopped_by_watchpoint (void)
137 return stopped_data_address != 0;
140 CORE_ADDR valgrind_stopped_data_address (void)
142 return stopped_data_address;
145 /* pc at which we last stopped */
146 static CORE_ADDR stop_pc;
148 /* pc at which we resume.
149 If stop_pc != resume_pc, it means
150 gdb/gdbserver has changed the pc so as to have either
151 a "continue by jumping at that address"
152 or a "continue at that address to call some code from gdb".
154 static CORE_ADDR resume_pc;
156 static vki_siginfo_t vki_signal_to_report;
157 static vki_siginfo_t vki_signal_to_deliver;
159 void gdbserver_signal_encountered (const vki_siginfo_t *info)
161 vki_signal_to_report = *info;
162 vki_signal_to_deliver = *info;
165 void gdbserver_pending_signal_to_report (vki_siginfo_t *info)
167 *info = vki_signal_to_report;
170 Bool gdbserver_deliver_signal (vki_siginfo_t *info)
172 if (info->si_signo != vki_signal_to_deliver.si_signo)
173 dlog(1, "GDB changed signal info %d to_report %d to_deliver %d\n",
174 info->si_signo, vki_signal_to_report.si_signo,
175 vki_signal_to_deliver.si_signo);
176 *info = vki_signal_to_deliver;
177 return vki_signal_to_deliver.si_signo != 0;
180 static Bool before_syscall;
181 static Int sysno_to_report = -1;
182 void gdbserver_syscall_encountered (Bool before, Int sysno)
184 before_syscall = before;
185 sysno_to_report = sysno;
188 Int valgrind_stopped_by_syscall (void)
190 return sysno_to_report;
193 Bool valgrind_stopped_before_syscall()
195 vg_assert (sysno_to_report >= 0);
196 return before_syscall;
200 static unsigned char exit_status_to_report;
201 static int exit_code_to_report;
202 void gdbserver_process_exit_encountered (unsigned char status, Int code)
204 vg_assert (status == 'W' || status == 'X');
205 exit_status_to_report = status;
206 exit_code_to_report = code;
209 static
210 const HChar* sym (Addr addr)
212 // Tracing/debugging so cur_ep is reasonable.
213 const DiEpoch cur_ep = VG_(current_DiEpoch)();
215 return VG_(describe_IP) (cur_ep, addr, NULL);
218 ThreadId vgdb_interrupted_tid = 0;
220 /* 0 => not single stepping.
221 1 => single stepping asked by gdb
222 2 => single stepping asked by valgrind (watchpoint) */
223 static int stepping = 0;
225 Addr valgrind_get_ignore_break_once(void)
227 if (valgrind_single_stepping())
228 return resume_pc;
229 else
230 return 0;
233 void valgrind_set_single_stepping(Bool set)
235 if (set)
236 stepping = 2;
237 else
238 stepping = 0;
241 Bool valgrind_single_stepping(void)
243 if (stepping)
244 return True;
245 else
246 return False;
249 int valgrind_thread_alive (unsigned long tid)
251 struct thread_info *ti = gdb_id_to_thread(tid);
252 ThreadState *tst;
254 if (ti != NULL) {
255 tst = (ThreadState *) inferior_target_data (ti);
256 return tst->status != VgTs_Zombie;
258 else {
259 return 0;
263 void valgrind_resume (struct thread_resume *resume_info)
265 dlog(1,
266 "resume_info step %d sig %d stepping %d\n",
267 resume_info->step,
268 resume_info->sig,
269 stepping);
270 if (valgrind_stopped_by_watchpoint()) {
271 dlog(1, "clearing watchpoint stopped_data_address %p\n",
272 C2v(stopped_data_address));
273 VG_(set_watchpoint_stop_address) ((Addr) 0);
275 if (valgrind_stopped_by_syscall () >= 0) {
276 dlog(1, "clearing stopped by syscall %d\n",
277 valgrind_stopped_by_syscall ());
278 gdbserver_syscall_encountered (False, -1);
281 vki_signal_to_deliver.si_signo = resume_info->sig;
282 /* signal was reported to GDB, GDB told us to resume execution.
283 So, reset the signal to report to 0. */
284 VG_(memset) (&vki_signal_to_report, 0, sizeof(vki_signal_to_report));
286 stepping = resume_info->step;
287 resume_pc = (*the_low_target.get_pc) ();
288 if (resume_pc != stop_pc) {
289 dlog(1,
290 "stop_pc %p changed to be resume_pc %s\n",
291 C2v(stop_pc), sym(resume_pc));
293 regcache_invalidate();
296 unsigned char valgrind_wait (char *ourstatus)
298 int pid;
299 unsigned long wptid;
300 ThreadState *tst;
301 enum target_signal sig;
302 int code;
304 pid = VG_(getpid) ();
305 dlog(1, "enter valgrind_wait pid %d\n", pid);
307 regcache_invalidate();
308 valgrind_update_threads(pid);
310 /* First see if we are done with this process. */
311 if (exit_status_to_report != 0) {
312 *ourstatus = exit_status_to_report;
313 exit_status_to_report = 0;
315 if (*ourstatus == 'W') {
316 code = exit_code_to_report;
317 exit_code_to_report = 0;
318 dlog(1, "exit valgrind_wait status W exit code %d\n", code);
319 return code;
322 if (*ourstatus == 'X') {
323 sig = target_signal_from_host(exit_code_to_report);
324 exit_code_to_report = 0;
325 dlog(1, "exit valgrind_wait status X signal %u\n", sig);
326 return sig;
330 /* in valgrind, we consider that a wait always succeeds with STOPPED 'T'
331 and with a signal TRAP (i.e. a breakpoint), unless there is
332 a signal to report. */
333 *ourstatus = 'T';
334 if (vki_signal_to_report.si_signo == 0)
335 sig = TARGET_SIGNAL_TRAP;
336 else
337 sig = target_signal_from_host(vki_signal_to_report.si_signo);
339 if (vgdb_interrupted_tid != 0)
340 tst = VG_(get_ThreadState) (vgdb_interrupted_tid);
341 else
342 tst = VG_(get_ThreadState) (VG_(running_tid));
343 wptid = tst->os_state.lwpid;
344 /* we can only change the current_inferior when the wptid references
345 an existing thread. Otherwise, we are still in the init phase.
346 (hack similar to main thread hack in valgrind_update_threads) */
347 if (tst->os_state.lwpid)
348 current_inferior = gdb_id_to_thread (wptid);
349 stop_pc = (*the_low_target.get_pc) ();
351 dlog(1,
352 "exit valgrind_wait status T ptid %s stop_pc %s signal %u\n",
353 image_ptid (wptid), sym (stop_pc), sig);
354 return sig;
357 /* Fetch one register from valgrind VEX guest state. */
358 static
359 void fetch_register (int regno)
361 int size;
362 ThreadState *tst = (ThreadState *) inferior_target_data (current_inferior);
363 ThreadId tid = tst->tid;
365 if (regno >= the_low_target.num_regs) {
366 dlog(0, "error fetch_register regno %d max %d\n",
367 regno, the_low_target.num_regs);
368 return;
370 size = register_size (regno);
371 if (size > 0) {
372 Bool mod;
373 char buf [size];
374 VG_(memset) (buf, 0, size); // registers not fetched will be seen as 0.
375 (*the_low_target.transfer_register) (tid, regno, buf,
376 valgrind_to_gdbserver, size, &mod);
377 // Note: the *mod received from transfer_register is not interesting.
378 // We are interested to see if the register data in the register cache is modified.
379 supply_register (regno, buf, &mod);
380 if (mod && VG_(debugLog_getLevel)() > 1) {
381 char bufimage [2*size + 1];
382 heximage (bufimage, buf, size);
383 dlog(3, "fetched register %d size %d name %s value %s tid %u status %s\n",
384 regno, size, the_low_target.reg_defs[regno].name, bufimage,
385 tid, VG_(name_of_ThreadStatus) (tst->status));
390 /* Fetch all registers, or just one, from the child process. */
391 static
392 void usr_fetch_inferior_registers (int regno)
394 if (regno == -1 || regno == 0)
395 for (regno = 0; regno < the_low_target.num_regs; regno++)
396 fetch_register (regno);
397 else
398 fetch_register (regno);
401 /* Store our register values back into the inferior.
402 If REGNO is -1, do this for all registers.
403 Otherwise, REGNO specifies which register (so we can save time). */
404 static
405 void usr_store_inferior_registers (int regno)
407 int size;
408 ThreadState *tst = (ThreadState *) inferior_target_data (current_inferior);
409 ThreadId tid = tst->tid;
411 if (regno >= 0) {
413 if (regno >= the_low_target.num_regs) {
414 dlog(0, "error store_register regno %d max %d\n",
415 regno, the_low_target.num_regs);
416 return;
419 size = register_size (regno);
420 if (size > 0) {
421 Bool mod;
422 Addr old_SP, new_SP;
423 char buf[size];
425 if (regno == the_low_target.stack_pointer_regno) {
426 /* When the stack pointer register is changed such that
427 the stack is extended, we better inform the tool of the
428 stack increase. This is needed in particular to avoid
429 spurious Memcheck errors during Inferior calls. So, we
430 save in old_SP the SP before the change. A change of
431 stack pointer is also assumed to have initialised this
432 new stack space. For the typical example of an inferior
433 call, gdb writes arguments on the stack, and then
434 changes the stack pointer. As the stack increase tool
435 function might mark it as undefined, we have to call it
436 at the good moment. */
437 VG_(memset) ((void *) &old_SP, 0, size);
438 (*the_low_target.transfer_register) (tid, regno, (void *) &old_SP,
439 valgrind_to_gdbserver, size, &mod);
442 VG_(memset) (buf, 0, size);
443 collect_register (regno, buf);
444 (*the_low_target.transfer_register) (tid, regno, buf,
445 gdbserver_to_valgrind, size, &mod);
446 if (mod && VG_(debugLog_getLevel)() > 1) {
447 char bufimage [2*size + 1];
448 heximage (bufimage, buf, size);
449 dlog(2,
450 "stored register %d size %d name %s value %s "
451 "tid %u status %s\n",
452 regno, size, the_low_target.reg_defs[regno].name, bufimage,
453 tid, VG_(name_of_ThreadStatus) (tst->status));
455 if (regno == the_low_target.stack_pointer_regno) {
456 VG_(memcpy) (&new_SP, buf, size);
457 if (old_SP > new_SP) {
458 Word delta = (Word)new_SP - (Word)old_SP;
459 dlog(1,
460 " stack increase by stack pointer changed from %p to %p "
461 "delta %ld\n",
462 (void*) old_SP, (void *) new_SP,
463 delta);
464 VG_TRACK( new_mem_stack_w_ECU, new_SP, -delta, 0 );
465 VG_TRACK( new_mem_stack, new_SP, -delta );
466 VG_TRACK( post_mem_write, Vg_CoreClientReq, tid,
467 new_SP, -delta);
472 else {
473 for (regno = 0; regno < the_low_target.num_regs; regno++)
474 usr_store_inferior_registers (regno);
478 void valgrind_fetch_registers (int regno)
480 usr_fetch_inferior_registers (regno);
483 void valgrind_store_registers (int regno)
485 usr_store_inferior_registers (regno);
488 Bool hostvisibility = False;
490 int valgrind_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
492 const void *sourceaddr = C2v (memaddr);
493 dlog(3, "reading memory %p size %d\n", sourceaddr, len);
494 if (VG_(am_is_valid_for_client) ((Addr) sourceaddr,
495 len, VKI_PROT_READ)
496 || (hostvisibility
497 && VG_(am_is_valid_for_valgrind) ((Addr) sourceaddr,
498 len, VKI_PROT_READ))) {
499 VG_(memcpy) (myaddr, sourceaddr, len);
500 return 0;
501 } else {
502 dlog(1, "error reading memory %p size %d\n", sourceaddr, len);
503 return -1;
507 int valgrind_write_memory (CORE_ADDR memaddr,
508 const unsigned char *myaddr, int len)
510 Bool is_valid_client_memory;
511 void *targetaddr = C2v (memaddr);
512 dlog(3, "writing memory %p size %d\n", targetaddr, len);
513 is_valid_client_memory
514 = VG_(am_is_valid_for_client) ((Addr)targetaddr, len, VKI_PROT_WRITE);
515 if (is_valid_client_memory
516 || (hostvisibility
517 && VG_(am_is_valid_for_valgrind) ((Addr) targetaddr,
518 len, VKI_PROT_READ))) {
519 if (len > 0) {
520 VG_(memcpy) (targetaddr, myaddr, len);
521 if (is_valid_client_memory && VG_(tdict).track_post_mem_write) {
522 /* Inform the tool of the post memwrite. Note that we do the
523 minimum necessary to avoid complains from e.g.
524 memcheck. The idea is that the debugger is as least
525 intrusive as possible. So, we do not inform of the pre
526 mem write (and in any case, this would cause problems with
527 memcheck that does not like our CorePart in
528 pre_mem_write. */
529 ThreadState *tst =
530 (ThreadState *) inferior_target_data (current_inferior);
531 ThreadId tid = tst->tid;
532 VG_(tdict).track_post_mem_write( Vg_CoreClientReq, tid,
533 (Addr) targetaddr, len );
536 return 0;
537 } else {
538 dlog(1, "error writing memory %p size %d\n", targetaddr, len);
539 return -1;
543 /* insert or remove a breakpoint */
544 static
545 int valgrind_point (Bool insert, char type, CORE_ADDR addr, int len)
547 PointKind kind;
548 switch (type) {
549 case '0': /* implemented by inserting checks at each instruction in sb */
550 kind = software_breakpoint;
551 break;
552 case '1': /* hw breakpoint, same implementation as sw breakpoint */
553 kind = hardware_breakpoint;
554 break;
555 case '2':
556 kind = write_watchpoint;
557 break;
558 case '3':
559 kind = read_watchpoint;
560 break;
561 case '4':
562 kind = access_watchpoint;
563 break;
564 default:
565 vg_assert (0);
568 /* Attention: gdbserver convention differs: 0 means ok; 1 means not ok */
569 if (VG_(gdbserver_point) (kind, insert, addr, len))
570 return 0;
571 else
572 return 1; /* error or unsupported */
575 const char* valgrind_target_xml (Bool shadow_mode)
577 return (*the_low_target.target_xml) (shadow_mode);
580 int valgrind_insert_watchpoint (char type, CORE_ADDR addr, int len)
582 return valgrind_point (/* insert */ True, type, addr, len);
585 int valgrind_remove_watchpoint (char type, CORE_ADDR addr, int len)
587 return valgrind_point (/* insert*/ False, type, addr, len);
590 /* Returns the (platform specific) offset of lm_modid field in the link map
591 struct.
592 Stores the offset in *result and returns True if offset can be determined.
593 Returns False otherwise. *result is not to be used then. */
594 static Bool getplatformoffset (SizeT *result)
596 static Bool getplatformoffset_called = False;
598 static Bool lm_modid_offset_found = False;
599 static SizeT lm_modid_offset = 1u << 31; // Rubbish initial value.
600 // lm_modid_offset is a magic offset, retrieved using an external program.
602 if (!getplatformoffset_called) {
603 getplatformoffset_called = True;
604 const HChar *platform = VG_PLATFORM;
605 const HChar *cmdformat = "%s/%s-%s -o %s";
606 const HChar *getoff = "getoff";
607 HChar outfile[VG_(mkstemp_fullname_bufsz) (VG_(strlen)(getoff))];
608 Int fd = VG_(mkstemp) (getoff, outfile);
609 if (fd == -1)
610 return False;
611 HChar cmd[ VG_(strlen)(cmdformat)
612 + VG_(strlen)(VG_(libdir)) - 2
613 + VG_(strlen)(getoff) - 2
614 + VG_(strlen)(platform) - 2
615 + VG_(strlen)(outfile) - 2
616 + 1];
617 UInt cmdlen;
618 struct vg_stat stat_buf;
619 Int ret;
621 cmdlen = VG_(snprintf)(cmd, sizeof(cmd),
622 cmdformat,
623 VG_(libdir), getoff, platform, outfile);
624 vg_assert (cmdlen == sizeof(cmd) - 1);
625 ret = VG_(system) (cmd);
626 if (ret != 0 || VG_(debugLog_getLevel)() >= 1)
627 VG_(dmsg) ("command %s exit code %d\n", cmd, ret);
628 ret = VG_(fstat)( fd, &stat_buf );
629 if (ret != 0)
630 VG_(dmsg) ("error VG_(fstat) %d %s\n", fd, outfile);
631 else {
632 HChar *w;
633 HChar *ssaveptr;
634 HChar *os;
635 HChar *str;
636 HChar *endptr;
638 os = malloc (stat_buf.size+1);
639 vg_assert (os);
640 ret = VG_(read)(fd, os, stat_buf.size);
641 vg_assert(ret == stat_buf.size);
642 os[ret] = '\0';
643 str = os;
644 while ((w = VG_(strtok_r)(str, " \n", &ssaveptr)) != NULL) {
645 if (VG_(strcmp) (w, "lm_modid_offset") == 0) {
646 w = VG_(strtok_r)(NULL, " \n", &ssaveptr);
647 lm_modid_offset = (SizeT) VG_(strtoull16) ( w, &endptr );
648 if (endptr == w)
649 VG_(dmsg) ("%s lm_modid_offset unexpected hex value %s\n",
650 cmd, w);
651 else
652 lm_modid_offset_found = True;
653 } else {
654 VG_(dmsg) ("%s produced unexpected %s\n", cmd, w);
656 str = NULL; // ensure next VG_(strtok_r) continues the parsing.
658 VG_(free) (os);
661 VG_(close)(fd);
662 ret = VG_(unlink)( outfile );
663 if (ret != 0)
664 VG_(umsg) ("error: could not unlink %s\n", outfile);
667 *result = lm_modid_offset;
668 return lm_modid_offset_found;
671 Bool valgrind_get_tls_addr (ThreadState *tst,
672 CORE_ADDR offset,
673 CORE_ADDR lm,
674 CORE_ADDR *tls_addr)
676 CORE_ADDR **dtv_loc;
677 CORE_ADDR *dtv;
678 SizeT lm_modid_offset;
679 unsigned long int modid;
681 #define CHECK_DEREF(addr, len, name) \
682 if (!VG_(am_is_valid_for_client) ((Addr)(addr), (len), VKI_PROT_READ)) { \
683 dlog(0, "get_tls_addr: %s at %p len %lu not addressable\n", \
684 name, (void*)(addr), (unsigned long)(len)); \
685 return False; \
688 *tls_addr = 0;
690 if (the_low_target.target_get_dtv == NULL) {
691 dlog(1, "low level dtv support not available\n");
692 return False;
695 if (!getplatformoffset (&lm_modid_offset)) {
696 dlog(0, "link_map modid field offset not available\n");
697 return False;
699 dlog (2, "link_map modid offset %p\n", (void*)lm_modid_offset);
700 vg_assert (lm_modid_offset < 0x10000); // let's say
702 dtv_loc = (*the_low_target.target_get_dtv)(tst);
703 if (dtv_loc == NULL) {
704 dlog(0, "low level dtv support returned NULL\n");
705 return False;
708 CHECK_DEREF(dtv_loc, sizeof(CORE_ADDR), "dtv_loc");
709 dtv = *dtv_loc;
711 // Check we can read at least 2 address at the beginning of dtv.
712 CHECK_DEREF(dtv, 2*sizeof(CORE_ADDR), "dtv 2 first entries");
713 dlog (2, "tid %u dtv %p\n", tst->tid, (void*)dtv);
715 // Check we can read the modid
716 CHECK_DEREF(lm+lm_modid_offset, sizeof(unsigned long int), "link_map modid");
717 modid = *(unsigned long int *)(lm+lm_modid_offset);
718 dlog (2, "tid %u modid %lu\n", tst->tid, modid);
720 // Check we can access the dtv entry for modid
721 CHECK_DEREF(dtv + 2 * modid, sizeof(CORE_ADDR), "dtv[2*modid]");
723 // Compute the base address of the tls block.
724 *tls_addr = *(dtv + 2 * modid);
726 if (*tls_addr & 1) {
727 /* This means that computed address is not valid, most probably
728 because given module uses Static TLS.
729 However, the best we can is to try to compute address using
730 static TLS. This is what libthread_db does.
731 Ref. GLIBC/nptl_db/td_thr_tlsbase.c:td_thr_tlsbase().
734 CORE_ADDR tls_offset_addr;
735 PtrdiffT tls_offset;
737 dlog(2, "tls_addr (%p & 1) => computing tls_addr using static TLS\n",
738 (void*) *tls_addr);
740 /* Assumes that tls_offset is placed right before tls_modid.
741 To check the assumption, start a gdb on none/tests/tls and do:
742 p &((struct link_map*)0x0)->l_tls_modid
743 p &((struct link_map*)0x0)->l_tls_offset
744 Instead of assuming this, we could calculate this similarly to
745 lm_modid_offset, by extending getplatformoffset to support querying
746 more than one offset.
748 tls_offset_addr = lm + lm_modid_offset - sizeof(PtrdiffT);
750 // Check we can read the tls_offset.
751 CHECK_DEREF(tls_offset_addr, sizeof(PtrdiffT), "link_map tls_offset");
752 tls_offset = *(PtrdiffT *)(tls_offset_addr);
753 dlog(2, "tls_offset_addr %p tls_offset %ld\n",
754 (void*)tls_offset_addr, (long)tls_offset);
756 /* Following two values represent platform dependent constants
757 NO_TLS_OFFSET and FORCED_DYNAMIC_TLS_OFFSET, respectively. */
758 if ((tls_offset == -1) || (tls_offset == -2)) {
759 dlog(2, "link_map tls_offset is not valid for static TLS\n");
760 return False;
763 // This calculation is also platform dependent.
764 #if defined(VGA_mips32) || defined(VGA_mips64)
765 *tls_addr = ((CORE_ADDR)dtv_loc + 2 * sizeof(CORE_ADDR) + tls_offset);
766 #elif defined(VGA_ppc64be) || defined(VGA_ppc64le)
767 *tls_addr = ((CORE_ADDR)dtv_loc + sizeof(CORE_ADDR) + tls_offset);
768 #elif defined(VGA_x86) || defined(VGA_amd64) || defined(VGA_s390x)
769 *tls_addr = (CORE_ADDR)dtv_loc - tls_offset - sizeof(CORE_ADDR);
770 #else
771 // ppc32, arm, arm64
772 dlog(0, "target.c is missing platform code for static TLS\n");
773 return False;
774 #endif
777 // Finally, add tls variable offset to tls block base address.
778 *tls_addr += offset;
780 return True;
782 #undef CHECK_DEREF
785 /* returns a pointer to the architecture state corresponding to
786 the provided register set: 0 => normal guest registers,
787 1 => shadow1
788 2 => shadow2
790 VexGuestArchState* get_arch (int set, ThreadState* tst)
792 switch (set) {
793 case 0: return &tst->arch.vex;
794 case 1: return &tst->arch.vex_shadow1;
795 case 2: return &tst->arch.vex_shadow2;
796 default: vg_assert(0);
800 static int non_shadow_num_regs = 0;
801 static struct reg *non_shadow_reg_defs = NULL;
802 void initialize_shadow_low(Bool shadow_mode)
804 if (non_shadow_reg_defs == NULL) {
805 non_shadow_reg_defs = the_low_target.reg_defs;
806 non_shadow_num_regs = the_low_target.num_regs;
809 regcache_invalidate();
810 if (the_low_target.reg_defs != non_shadow_reg_defs) {
811 free (the_low_target.reg_defs);
813 if (shadow_mode) {
814 the_low_target.num_regs = 3 * non_shadow_num_regs;
815 the_low_target.reg_defs = build_shadow_arch (non_shadow_reg_defs, non_shadow_num_regs);
816 } else {
817 the_low_target.num_regs = non_shadow_num_regs;
818 the_low_target.reg_defs = non_shadow_reg_defs;
820 set_register_cache (the_low_target.reg_defs, the_low_target.num_regs);
823 void set_desired_inferior (int use_general)
825 struct thread_info *found;
827 if (use_general == 1) {
828 found = (struct thread_info *) find_inferior_id (&all_threads,
829 general_thread);
830 } else {
831 found = NULL;
833 /* If we are continuing any (all) thread(s), use step_thread
834 to decide which thread to step and/or send the specified
835 signal to. */
836 if ((step_thread != 0 && step_thread != -1)
837 && (cont_thread == 0 || cont_thread == -1))
838 found = (struct thread_info *) find_inferior_id (&all_threads,
839 step_thread);
841 if (found == NULL)
842 found = (struct thread_info *) find_inferior_id (&all_threads,
843 cont_thread);
846 if (found == NULL)
847 current_inferior = (struct thread_info *) all_threads.head;
848 else
849 current_inferior = found;
851 ThreadState *tst = (ThreadState *) inferior_target_data (current_inferior);
852 ThreadId tid = tst->tid;
853 dlog(1, "set_desired_inferior use_general %d found %p tid %u lwpid %d\n",
854 use_general, found, tid, tst->os_state.lwpid);
858 void* VG_(dmemcpy) ( void *d, const void *s, SizeT sz, Bool *mod )
860 if (VG_(memcmp) (d, s, sz)) {
861 *mod = True;
862 return VG_(memcpy) (d, s, sz);
863 } else {
864 *mod = False;
865 return d;
869 void VG_(transfer) (void *valgrind,
870 void *gdbserver,
871 transfer_direction dir,
872 SizeT sz,
873 Bool *mod)
875 if (dir == valgrind_to_gdbserver)
876 VG_(dmemcpy) (gdbserver, valgrind, sz, mod);
877 else if (dir == gdbserver_to_valgrind)
878 VG_(dmemcpy) (valgrind, gdbserver, sz, mod);
879 else
880 vg_assert (0);
883 void valgrind_initialize_target(void)
885 #if defined(VGA_x86)
886 x86_init_architecture(&the_low_target);
887 #elif defined(VGA_amd64)
888 amd64_init_architecture(&the_low_target);
889 #elif defined(VGA_arm)
890 arm_init_architecture(&the_low_target);
891 #elif defined(VGA_arm64)
892 arm64_init_architecture(&the_low_target);
893 #elif defined(VGA_ppc32)
894 ppc32_init_architecture(&the_low_target);
895 #elif defined(VGA_ppc64be) || defined(VGA_ppc64le)
896 ppc64_init_architecture(&the_low_target);
897 #elif defined(VGA_s390x)
898 s390x_init_architecture(&the_low_target);
899 #elif defined(VGA_mips32)
900 mips32_init_architecture(&the_low_target);
901 #elif defined(VGA_mips64)
902 mips64_init_architecture(&the_low_target);
903 #else
904 #error "architecture missing in target.c valgrind_initialize_target"
905 #endif