blob: e5d499e4478fc6288acc1f49de465af1592116c6 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
njn717cde52005-05-10 02:47:21 +00003/*--- A header file for various private parts of Valgrind's core. ---*/
rjwalsh7109a8c2004-09-02 00:31:02 +00004/*--- core.h ---*/
sewardjde4a1d02002-03-22 01:27:54 +00005/*--------------------------------------------------------------------*/
6
7/*
njnb9c427c2004-12-01 14:14:42 +00008 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
sewardjde4a1d02002-03-22 01:27:54 +000010
njn53612422005-03-12 16:22:54 +000011 Copyright (C) 2000-2005 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000012 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000013
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
njn25e49d8e72002-09-23 09:36:25 +000029 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000030*/
31
rjwalsh7109a8c2004-09-02 00:31:02 +000032#ifndef __CORE_H
33#define __CORE_H
sewardjde4a1d02002-03-22 01:27:54 +000034
nethercote13343132004-09-02 15:49:09 +000035#include "tool.h" // tool stuff
nethercotebb4222b2004-09-10 17:42:11 +000036#include "core_arch.h" // arch-specific stuff, eg. x86/core_arch.h
nethercote8ff888f2004-11-17 17:11:45 +000037
sewardjb5f6f512005-03-10 23:59:00 +000038#include "core_os.h" // OS-specific stuff, eg. linux/core_os.h
sewardjde4a1d02002-03-22 01:27:54 +000039
njn278b3d62005-05-30 23:20:51 +000040#include <setjmp.h> // for jmp_buf
njnd01fef72005-03-25 23:35:48 +000041
njn278b3d62005-05-30 23:20:51 +000042#include "pub_core_mallocfree.h" // for type 'ArenaId'
43#include "pub_core_scheduler.h" // for types 'ThreadState', 'ThreadArchState'
njn4f6e3702005-05-16 20:50:52 +000044
nethercote7be47252004-09-02 16:02:58 +000045/* ---------------------------------------------------------------------
njn14319cc2005-03-13 06:26:22 +000046 Global macros.
nethercote7be47252004-09-02 16:02:58 +000047 ------------------------------------------------------------------ */
48
sewardjde4a1d02002-03-22 01:27:54 +000049/* Max length of a text fragment used to construct error messages. */
njn47b209a2005-03-25 23:47:16 +000050#define VG_ERRTXT_LEN 4096
sewardjde4a1d02002-03-22 01:27:54 +000051
fitzhardinge98abfc72003-12-16 02:05:15 +000052/* Useful macros */
53/* a - alignment - must be a power of 2 */
tomde2ec262005-03-29 12:16:10 +000054#define ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
fitzhardinge98abfc72003-12-16 02:05:15 +000055#define ROUNDUP(p, a) ROUNDDN((p)+(a)-1, (a))
nethercote73b526f2004-10-31 18:48:21 +000056#define PGROUNDDN(p) ROUNDDN(p, VKI_PAGE_SIZE)
57#define PGROUNDUP(p) ROUNDUP(p, VKI_PAGE_SIZE)
fitzhardinge98abfc72003-12-16 02:05:15 +000058
sewardj51ac0872004-12-21 01:20:49 +000059
nethercote80013e92004-09-05 20:39:51 +000060/* ---------------------------------------------------------------------
61 Environment variables
62 ------------------------------------------------------------------ */
63
64/* The directory we look for all our auxillary files in */
65#define VALGRINDLIB "VALGRINDLIB"
66
67/* Additional command-line arguments; they are overridden by actual
68 command-line option. Each argument is separated by spaces. There
69 is no quoting mechanism.
70 */
71#define VALGRINDOPTS "VALGRIND_OPTS"
72
73/* If this variable is present in the environment, then valgrind will
74 not parse the command line for options at all; all options come
75 from this variable. Arguments are terminated by ^A (\001). There
76 is no quoting mechanism.
77
78 This variable is not expected to be set by anything other than
79 Valgrind itself, as part of its handling of execve with
80 --trace-children=yes. This variable should not be present in the
81 client environment.
82 */
83#define VALGRINDCLO "_VALGRIND_CLO"
84
fitzhardinge98abfc72003-12-16 02:05:15 +000085
thughesad1c9562004-06-26 11:27:52 +000086/* Application-visible file descriptor limits */
87extern Int VG_(fd_soft_limit);
88extern Int VG_(fd_hard_limit);
fitzhardingef0046f22003-12-18 02:39:22 +000089
sewardjde4a1d02002-03-22 01:27:54 +000090/* ---------------------------------------------------------------------
nethercote85cdd342004-08-01 22:36:40 +000091 Profiling stuff
sewardjde4a1d02002-03-22 01:27:54 +000092 ------------------------------------------------------------------ */
93
njn31066fd2005-03-26 00:42:02 +000094extern void VG_(init_profiling) ( void );
95extern void VG_(done_profiling) ( void );
sewardjde4a1d02002-03-22 01:27:54 +000096
njn25e49d8e72002-09-23 09:36:25 +000097#undef VGP_PUSHCC
98#undef VGP_POPCC
njn31066fd2005-03-26 00:42:02 +000099#define VGP_PUSHCC(x) if (VG_(clo_profile)) VG_(pushcc)(x)
100#define VGP_POPCC(x) if (VG_(clo_profile)) VG_(popcc)(x)
sewardjde4a1d02002-03-22 01:27:54 +0000101
sewardj51ac0872004-12-21 01:20:49 +0000102
sewardjde4a1d02002-03-22 01:27:54 +0000103/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000104 Exports of vg_intercept.c
sewardj2e93c502002-04-12 11:12:52 +0000105 ------------------------------------------------------------------ */
106
njna91498f2005-05-11 22:32:39 +0000107/* These are the internal client request codes. The publically-visible
108 request codes are also defined in valgrind.h, and similar headers for
109 some tools. */
sewardj2e93c502002-04-12 11:12:52 +0000110
njna91498f2005-05-11 22:32:39 +0000111/* Get the tool's malloc-wrapping functions */
fitzhardinge98abfc72003-12-16 02:05:15 +0000112#define VG_USERREQ__GET_MALLOCFUNCS 0x3030
sewardjb5f6f512005-03-10 23:59:00 +0000113
fitzhardinge39de4b42003-10-31 07:12:21 +0000114/* Internal equivalent of VALGRIND_PRINTF . */
115#define VG_USERREQ__INTERNAL_PRINTF 0x3103
sewardj45b4b372002-04-16 22:50:32 +0000116
sewardjb5f6f512005-03-10 23:59:00 +0000117/* Denote the finish of __libc_freeres_wrapper().
118 A synonym for exit. */
119#define VG_USERREQ__LIBC_FREERES_DONE 0x3029
sewardj54cacf02002-04-12 23:24:59 +0000120
njn717cde52005-05-10 02:47:21 +0000121/* Intercept prefix stuff. See
122 coregrind/m_replace_malloc/vg_replace_malloc.c for details.
123 Unfortunately the "_vgi_" literal is also hardcoded in that file, so if
124 you change this one you must also change the other one. */
sewardj9ee81f52005-04-02 17:38:59 +0000125#define VG_INTERCEPT_PREFIX "_vgi_"
126#define VG_INTERCEPT_PREFIX_LEN 5
rjwalshe4e779d2004-04-16 23:02:29 +0000127
sewardj9ee81f52005-04-02 17:38:59 +0000128/* Not sure what these are for. Todo: clarify */
129#define VG_WRAPPER_PREFIX "_vgw_"
130#define VG_WRAPPER_PREFIX_LEN 5
131#define VG_WRAPPER(name) _vgw_##name
132#define VG_WRAPPER_ALIAS(name) "_vgw_" #name
rjwalshe4e779d2004-04-16 23:02:29 +0000133
njn4c791212003-05-02 17:53:54 +0000134
sewardj2e93c502002-04-12 11:12:52 +0000135/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000136 Exports of vg_mylibc.c
137 ------------------------------------------------------------------ */
138
njnca0518d2004-11-26 19:34:36 +0000139// Useful for making failing stubs, when certain things haven't yet been
140// implemented.
njn50ae1a72005-04-08 23:28:23 +0000141#define I_die_here \
sewardj0ab10c42005-05-12 08:26:36 +0000142 VG_(assert_fail) (/*isCore*//*BOGUS*/True, \
143 "Unimplemented functionality", \
njn50ae1a72005-04-08 23:28:23 +0000144 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
145 "valgrind", VG_BUGS_TO, "")
njnca0518d2004-11-26 19:34:36 +0000146
njn50ae1a72005-04-08 23:28:23 +0000147#define vg_assert(expr) \
148 ((void) ((expr) ? 0 : \
149 (VG_(assert_fail) (/*isCore*/True, VG_STRINGIFY(expr), \
150 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
151 ""), \
152 0)))
153
154#define vg_assert2(expr, format, args...) \
155 ((void) ((expr) ? 0 : \
156 (VG_(assert_fail) (/*isCore*/True, VG_STRINGIFY(expr), \
157 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
158 format, ##args), \
159 0)))
160
njne427a662002-10-02 11:08:25 +0000161__attribute__ ((__noreturn__))
162extern void VG_(core_panic) ( Char* str );
thughes5876d552004-09-26 18:44:06 +0000163__attribute__ ((__noreturn__))
njndb13c4f2005-06-01 00:00:46 +0000164extern void VG_(core_panic_at) ( Char* str, Addr ip, Addr sp, Addr fp );
sewardjde4a1d02002-03-22 01:27:54 +0000165
njn04e16982005-05-31 00:23:43 +0000166/* Called when some unhandleable client behaviour is detected.
167 Prints a msg and aborts. */
168extern void VG_(unimplemented) ( Char* msg )
169 __attribute__((__noreturn__));
170
171/* Tell the logging mechanism whether we are logging to a file
172 descriptor or a socket descriptor. */
173extern Bool VG_(logging_to_socket);
174
nethercote05675c82004-08-04 10:37:49 +0000175/* Tools use VG_(strdup)() which doesn't expose ArenaId */
njn25e49d8e72002-09-23 09:36:25 +0000176extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
sewardjde4a1d02002-03-22 01:27:54 +0000177
njn25e49d8e72002-09-23 09:36:25 +0000178extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
jsgf855d93d2003-10-13 22:26:55 +0000179extern Int VG_(poll)( struct vki_pollfd *, UInt nfds, Int timeout);
sewardj2e93c502002-04-12 11:12:52 +0000180
fitzhardinge98abfc72003-12-16 02:05:15 +0000181/* system/mman.h */
njn7df470b2005-05-29 18:46:38 +0000182extern void* VG_(mmap) ( void* start, SizeT length, UInt prot, UInt flags,
183 UInt sf_flags, UInt fd, OffT offset );
184extern void* VG_(mmap_native)( void* start, SizeT length, UInt prot, UInt flags,
185 UInt fd, OffT offset );
186extern Int VG_(munmap) ( void* start, SizeT length );
187extern Int VG_(mprotect) ( void *start, SizeT length, UInt prot );
sewardj79048ce2005-02-18 08:28:32 +0000188extern Int VG_(mprotect_native)( void *start, SizeT length, UInt prot );
fitzhardinge98abfc72003-12-16 02:05:15 +0000189
190
jsgf855d93d2003-10-13 22:26:55 +0000191/* Move an fd into the Valgrind-safe range */
192Int VG_(safe_fd)(Int oldfd);
193
sewardj570f8902002-11-03 11:44:36 +0000194extern Int VG_(write_socket)( Int sd, void *msg, Int count );
sewardj73cf3bc2002-11-03 03:20:15 +0000195
196/* --- Connecting over the network --- */
197extern Int VG_(connect_via_socket)( UChar* str );
198
fitzhardinge98abfc72003-12-16 02:05:15 +0000199/* Environment manipulations */
nethercote60a96c52004-08-03 13:08:31 +0000200extern Char **VG_(env_setenv) ( Char ***envp, const Char* varname,
201 const Char *val );
202extern void VG_(env_unsetenv) ( Char **env, const Char *varname );
203extern void VG_(env_remove_valgrind_env_stuff) ( Char** env );
sewardj570f8902002-11-03 11:44:36 +0000204
sewardjb5f6f512005-03-10 23:59:00 +0000205extern void VG_(nanosleep)(struct vki_timespec *);
njn2521d322005-05-08 14:45:13 +0000206
sewardj570f8902002-11-03 11:44:36 +0000207/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000208 Exports of vg_syscall.S
209 ------------------------------------------------------------------ */
210
njnca6fef02004-11-29 16:49:18 +0000211// We use a full prototype rather than "..." here to ensure that all
212// arguments get converted to a UWord appropriately. Not doing so can
213// cause problems when passing 32-bit integers on 64-bit platforms, because
214// the top 32-bits might not be zeroed appropriately, eg. as would happen
215// with the 6th arg on AMD64 which is passed on the stack.
njnf4aeaea2004-11-29 17:33:31 +0000216extern Word VG_(do_syscall) ( UInt, UWord, UWord, UWord, UWord, UWord, UWord );
njnca6fef02004-11-29 16:49:18 +0000217
218// Macros make life easier.
219#define vgPlain_do_syscall0(s) VG_(do_syscall)((s),0,0,0,0,0,0)
220#define vgPlain_do_syscall1(s,a) VG_(do_syscall)((s),(a),0,0,0,0,0)
221#define vgPlain_do_syscall2(s,a,b) VG_(do_syscall)((s),(a),(b),0,0,0,0)
222#define vgPlain_do_syscall3(s,a,b,c) VG_(do_syscall)((s),(a),(b),(c),0,0,0)
223#define vgPlain_do_syscall4(s,a,b,c,d) VG_(do_syscall)((s),(a),(b),(c),(d),0,0)
224#define vgPlain_do_syscall5(s,a,b,c,d,e) VG_(do_syscall)((s),(a),(b),(c),(d),(e),0)
225#define vgPlain_do_syscall6(s,a,b,c,d,e,f) VG_(do_syscall)((s),(a),(b),(c),(d),(e),(f))
226
fitzhardinge4f10ada2004-06-03 10:00:42 +0000227extern void VG_(sigreturn)(void);
sewardjde4a1d02002-03-22 01:27:54 +0000228
229/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000230 Exports of vg_helpers.S
231 ------------------------------------------------------------------ */
232
fitzhardinge92360792003-12-24 10:11:11 +0000233/* Information about trampoline code (for signal return and syscalls) */
234extern const Char VG_(trampoline_code_start);
235extern const Int VG_(trampoline_code_length);
236extern const Int VG_(tramp_sigreturn_offset);
sewardjb5f6f512005-03-10 23:59:00 +0000237extern const Int VG_(tramp_rt_sigreturn_offset);
fitzhardinge92360792003-12-24 10:11:11 +0000238extern const Int VG_(tramp_syscall_offset);
tomee0bcbf2005-05-02 10:28:42 +0000239extern const Int VG_(tramp_gettimeofday_offset);
240extern const Int VG_(tramp_time_offset);
njn3c008782005-05-31 22:15:14 +0000241
nethercotec06e2132004-09-03 13:45:29 +0000242// ---------------------------------------------------------------------
243// Architecture-specific things defined in eg. x86/*.c
244// ---------------------------------------------------------------------
245
sewardj51ac0872004-12-21 01:20:49 +0000246// Returns the architecture and subarchitecture, or indicates
247// that this subarchitecture is unable to run Valgrind
248// Returns False to indicate we cannot proceed further.
sewardj51ac0872004-12-21 01:20:49 +0000249extern Bool VGA_(getArchAndSubArch)( /*OUT*/VexArch*,
250 /*OUT*/VexSubArch* );
njncf45fd42004-11-24 16:30:22 +0000251// Accessors for the ThreadArchState
njn35172bc2005-03-26 00:04:03 +0000252#define INSTR_PTR(regs) ((regs).vex.VGA_INSTR_PTR)
253#define STACK_PTR(regs) ((regs).vex.VGA_STACK_PTR)
254#define FRAME_PTR(regs) ((regs).vex.VGA_FRAME_PTR)
255#define CLREQ_ARGS(regs) ((regs).vex.VGA_CLREQ_ARGS)
njn35172bc2005-03-26 00:04:03 +0000256#define CLREQ_RET(regs) ((regs).vex.VGA_CLREQ_RET)
njn16de5572004-11-27 14:27:21 +0000257// Offsets for the Vex state
njn35172bc2005-03-26 00:04:03 +0000258#define O_STACK_PTR (offsetof(VexGuestArchState, VGA_STACK_PTR))
259#define O_FRAME_PTR (offsetof(VexGuestArchState, VGA_FRAME_PTR))
260#define O_CLREQ_RET (offsetof(VexGuestArchState, VGA_CLREQ_RET))
njncf45fd42004-11-24 16:30:22 +0000261
262
sewardj2a99cf62004-11-24 10:44:19 +0000263// Setting up the initial thread (1) state
264extern void
265 VGA_(init_thread1state) ( Addr client_eip,
266 Addr esp_at_startup,
267 /*MOD*/ ThreadArchState* arch );
sewardjde4a1d02002-03-22 01:27:54 +0000268
sewardjb5f6f512005-03-10 23:59:00 +0000269// OS/Platform-specific thread clear (after thread exit)
sewardj1d887112005-05-30 21:44:08 +0000270extern void VGO_(os_state_clear)(ThreadState *);
sewardjb5f6f512005-03-10 23:59:00 +0000271
272// OS/Platform-specific thread init (at scheduler init time)
sewardj1d887112005-05-30 21:44:08 +0000273extern void VGO_(os_state_init)(ThreadState *);
sewardjb5f6f512005-03-10 23:59:00 +0000274
sewardj1d887112005-05-30 21:44:08 +0000275// Run a thread from beginning to end.
276extern VgSchedReturnCode VGO_(thread_wrapper)(Word /*ThreadId*/ tid);
sewardjb5f6f512005-03-10 23:59:00 +0000277
sewardj1d887112005-05-30 21:44:08 +0000278// Call here to exit the entire Valgrind system.
279extern void VGO_(terminate_NORETURN)(ThreadId tid, VgSchedReturnCode src);
280
281// Allocates a stack for the first thread, then runs it,
282// as if the thread had been set up by clone()
283extern void VGP_(main_thread_wrapper_NORETURN)(ThreadId tid);
sewardjb5f6f512005-03-10 23:59:00 +0000284
285// Return how many bytes of a thread's Valgrind stack are unused
sewardj1d887112005-05-30 21:44:08 +0000286extern SSizeT VGA_(stack_unused)(ThreadId tid);
sewardjb5f6f512005-03-10 23:59:00 +0000287
sewardjb5f6f512005-03-10 23:59:00 +0000288// wait until all other threads are dead
289extern void VGA_(reap_threads)(ThreadId self);
290
291// handle an arch-specific client request
292extern Bool VGA_(client_request)(ThreadId tid, UWord *args);
293
nethercotefedd8102004-09-13 15:19:34 +0000294// Pointercheck
njn04e16982005-05-31 00:23:43 +0000295extern Bool VGA_(setup_pointercheck) ( Addr client_base, Addr client_end );
nethercotefedd8102004-09-13 15:19:34 +0000296
297// For attaching the debugger
sewardj2a99cf62004-11-24 10:44:19 +0000298extern Int VGA_(ptrace_setregs_from_tst) ( Int pid, ThreadArchState* arch );
nethercotefedd8102004-09-13 15:19:34 +0000299
sewardjb5f6f512005-03-10 23:59:00 +0000300// Used by leakcheck
301extern void VGA_(mark_from_registers)(ThreadId tid, void (*marker)(Addr));
302
njn20242342005-05-16 23:31:24 +0000303// Set up the libc freeres wrapper
304extern void VGA_(intercept_libc_freeres_wrapper)(Addr);
305
306// Clean up the client by calling before the final reports
307extern void VGA_(final_tidyup)(ThreadId tid);
308
309// Arch-specific client requests
310extern Bool VGA_(client_requests)(ThreadId tid, UWord *args);
311
nethercote9b3c7652004-10-19 13:18:00 +0000312
sewardjb5f6f512005-03-10 23:59:00 +0000313///* ---------------------------------------------------------------------
314// Thread modelling
315// ------------------------------------------------------------------ */
316//extern void VG_(tm_thread_create) (ThreadId creator, ThreadId tid, Bool detached);
317//extern void VG_(tm_thread_exit) (ThreadId tid);
318//extern Bool VG_(tm_thread_exists) (ThreadId tid);
319//extern void VG_(tm_thread_detach) (ThreadId tid);
320//extern void VG_(tm_thread_join) (ThreadId joiner, ThreadId joinee);
321//extern void VG_(tm_thread_switchto)(ThreadId tid);
322//
323//extern void VG_(tm_mutex_init) (ThreadId tid, Addr mutexp);
324//extern void VG_(tm_mutex_destroy)(ThreadId tid, Addr mutexp);
325//extern void VG_(tm_mutex_trylock)(ThreadId tid, Addr mutexp);
326//extern void VG_(tm_mutex_giveup) (ThreadId tid, Addr mutexp);
327//extern void VG_(tm_mutex_acquire)(ThreadId tid, Addr mutexp);
328//extern void VG_(tm_mutex_tryunlock)(ThreadId tid, Addr mutexp);
329//extern void VG_(tm_mutex_unlock) (ThreadId tid, Addr mutexp);
330//extern Bool VG_(tm_mutex_exists) (Addr mutexp);
331//
332//extern UInt VG_(tm_error_update_extra) (Error *err);
333//extern Bool VG_(tm_error_equal) (VgRes res, Error *e1, Error *e2);
334//extern void VG_(tm_error_print) (Error *err);
335//
336//extern void VG_(tm_init) ();
337//
338//extern void VG_(tm_cond_init) (ThreadId tid, Addr condp);
339//extern void VG_(tm_cond_destroy) (ThreadId tid, Addr condp);
340//extern void VG_(tm_cond_wait) (ThreadId tid, Addr condp, Addr mutexp);
341//extern void VG_(tm_cond_wakeup) (ThreadId tid, Addr condp, Addr mutexp);
342//extern void VG_(tm_cond_signal) (ThreadId tid, Addr condp);
343//
344///* ----- pthreads ----- */
345//extern void VG_(pthread_init) ();
346//extern void VG_(pthread_startfunc_wrapper)(Addr wrapper);
347//
348//struct vg_pthread_newthread_data {
349// void *(*startfunc)(void *arg);
350// void *arg;
351//};
sewardj3b2736a2002-03-24 12:18:35 +0000352
353/* ---------------------------------------------------------------------
354 Finally - autoconf-generated settings
355 ------------------------------------------------------------------ */
356
357#include "config.h"
358
nethercotec06e2132004-09-03 13:45:29 +0000359#endif /* ndef __CORE_H */
360
sewardjde4a1d02002-03-22 01:27:54 +0000361/*--------------------------------------------------------------------*/
nethercote109d0df2004-09-02 08:10:13 +0000362/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +0000363/*--------------------------------------------------------------------*/