blob: 58e08f7cea2c62d904fd431ed6341065b4fa9ee4 [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/*--------------------------------------------------------------------*/
njned6b8242005-06-01 00:03:17 +00006
sewardjde4a1d02002-03-22 01:27:54 +00007/*
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
nethercote80013e92004-09-05 20:39:51 +000052/* ---------------------------------------------------------------------
53 Environment variables
54 ------------------------------------------------------------------ */
55
56/* The directory we look for all our auxillary files in */
57#define VALGRINDLIB "VALGRINDLIB"
58
thughesad1c9562004-06-26 11:27:52 +000059/* Application-visible file descriptor limits */
60extern Int VG_(fd_soft_limit);
61extern Int VG_(fd_hard_limit);
fitzhardingef0046f22003-12-18 02:39:22 +000062
sewardjde4a1d02002-03-22 01:27:54 +000063/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +000064 Exports of vg_intercept.c
sewardj2e93c502002-04-12 11:12:52 +000065 ------------------------------------------------------------------ */
66
njna91498f2005-05-11 22:32:39 +000067/* These are the internal client request codes. The publically-visible
68 request codes are also defined in valgrind.h, and similar headers for
69 some tools. */
sewardj2e93c502002-04-12 11:12:52 +000070
njna91498f2005-05-11 22:32:39 +000071/* Get the tool's malloc-wrapping functions */
fitzhardinge98abfc72003-12-16 02:05:15 +000072#define VG_USERREQ__GET_MALLOCFUNCS 0x3030
sewardjb5f6f512005-03-10 23:59:00 +000073
fitzhardinge39de4b42003-10-31 07:12:21 +000074/* Internal equivalent of VALGRIND_PRINTF . */
75#define VG_USERREQ__INTERNAL_PRINTF 0x3103
sewardj45b4b372002-04-16 22:50:32 +000076
sewardjb5f6f512005-03-10 23:59:00 +000077/* Denote the finish of __libc_freeres_wrapper().
78 A synonym for exit. */
79#define VG_USERREQ__LIBC_FREERES_DONE 0x3029
sewardj54cacf02002-04-12 23:24:59 +000080
njn717cde52005-05-10 02:47:21 +000081/* Intercept prefix stuff. See
82 coregrind/m_replace_malloc/vg_replace_malloc.c for details.
83 Unfortunately the "_vgi_" literal is also hardcoded in that file, so if
84 you change this one you must also change the other one. */
sewardj9ee81f52005-04-02 17:38:59 +000085#define VG_INTERCEPT_PREFIX "_vgi_"
86#define VG_INTERCEPT_PREFIX_LEN 5
rjwalshe4e779d2004-04-16 23:02:29 +000087
sewardj9ee81f52005-04-02 17:38:59 +000088/* Not sure what these are for. Todo: clarify */
89#define VG_WRAPPER_PREFIX "_vgw_"
90#define VG_WRAPPER_PREFIX_LEN 5
91#define VG_WRAPPER(name) _vgw_##name
92#define VG_WRAPPER_ALIAS(name) "_vgw_" #name
rjwalshe4e779d2004-04-16 23:02:29 +000093
njn4c791212003-05-02 17:53:54 +000094
sewardj2e93c502002-04-12 11:12:52 +000095/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +000096 Exports of vg_mylibc.c
97 ------------------------------------------------------------------ */
98
njnca0518d2004-11-26 19:34:36 +000099// Useful for making failing stubs, when certain things haven't yet been
100// implemented.
njn50ae1a72005-04-08 23:28:23 +0000101#define I_die_here \
sewardj0ab10c42005-05-12 08:26:36 +0000102 VG_(assert_fail) (/*isCore*//*BOGUS*/True, \
103 "Unimplemented functionality", \
njn50ae1a72005-04-08 23:28:23 +0000104 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
105 "valgrind", VG_BUGS_TO, "")
njnca0518d2004-11-26 19:34:36 +0000106
njn50ae1a72005-04-08 23:28:23 +0000107#define vg_assert(expr) \
108 ((void) ((expr) ? 0 : \
109 (VG_(assert_fail) (/*isCore*/True, VG_STRINGIFY(expr), \
110 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
111 ""), \
112 0)))
113
114#define vg_assert2(expr, format, args...) \
115 ((void) ((expr) ? 0 : \
116 (VG_(assert_fail) (/*isCore*/True, VG_STRINGIFY(expr), \
117 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
118 format, ##args), \
119 0)))
120
njne427a662002-10-02 11:08:25 +0000121__attribute__ ((__noreturn__))
122extern void VG_(core_panic) ( Char* str );
thughes5876d552004-09-26 18:44:06 +0000123__attribute__ ((__noreturn__))
njndb13c4f2005-06-01 00:00:46 +0000124extern void VG_(core_panic_at) ( Char* str, Addr ip, Addr sp, Addr fp );
sewardjde4a1d02002-03-22 01:27:54 +0000125
njn04e16982005-05-31 00:23:43 +0000126/* Called when some unhandleable client behaviour is detected.
127 Prints a msg and aborts. */
128extern void VG_(unimplemented) ( Char* msg )
129 __attribute__((__noreturn__));
130
131/* Tell the logging mechanism whether we are logging to a file
132 descriptor or a socket descriptor. */
133extern Bool VG_(logging_to_socket);
134
nethercote05675c82004-08-04 10:37:49 +0000135/* Tools use VG_(strdup)() which doesn't expose ArenaId */
njn25e49d8e72002-09-23 09:36:25 +0000136extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
sewardjde4a1d02002-03-22 01:27:54 +0000137
njn25e49d8e72002-09-23 09:36:25 +0000138extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
jsgf855d93d2003-10-13 22:26:55 +0000139extern Int VG_(poll)( struct vki_pollfd *, UInt nfds, Int timeout);
sewardj2e93c502002-04-12 11:12:52 +0000140
fitzhardinge98abfc72003-12-16 02:05:15 +0000141/* system/mman.h */
njn7df470b2005-05-29 18:46:38 +0000142extern void* VG_(mmap) ( void* start, SizeT length, UInt prot, UInt flags,
143 UInt sf_flags, UInt fd, OffT offset );
144extern void* VG_(mmap_native)( void* start, SizeT length, UInt prot, UInt flags,
145 UInt fd, OffT offset );
146extern Int VG_(munmap) ( void* start, SizeT length );
147extern Int VG_(mprotect) ( void *start, SizeT length, UInt prot );
sewardj79048ce2005-02-18 08:28:32 +0000148extern Int VG_(mprotect_native)( void *start, SizeT length, UInt prot );
fitzhardinge98abfc72003-12-16 02:05:15 +0000149
150
jsgf855d93d2003-10-13 22:26:55 +0000151/* Move an fd into the Valgrind-safe range */
152Int VG_(safe_fd)(Int oldfd);
153
sewardj570f8902002-11-03 11:44:36 +0000154extern Int VG_(write_socket)( Int sd, void *msg, Int count );
sewardj73cf3bc2002-11-03 03:20:15 +0000155
156/* --- Connecting over the network --- */
157extern Int VG_(connect_via_socket)( UChar* str );
158
fitzhardinge98abfc72003-12-16 02:05:15 +0000159/* Environment manipulations */
nethercote60a96c52004-08-03 13:08:31 +0000160extern Char **VG_(env_setenv) ( Char ***envp, const Char* varname,
161 const Char *val );
162extern void VG_(env_unsetenv) ( Char **env, const Char *varname );
163extern void VG_(env_remove_valgrind_env_stuff) ( Char** env );
sewardj570f8902002-11-03 11:44:36 +0000164
sewardjb5f6f512005-03-10 23:59:00 +0000165extern void VG_(nanosleep)(struct vki_timespec *);
njn2521d322005-05-08 14:45:13 +0000166
sewardj570f8902002-11-03 11:44:36 +0000167/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000168 Exports of vg_syscall.S
169 ------------------------------------------------------------------ */
170
njnca6fef02004-11-29 16:49:18 +0000171// We use a full prototype rather than "..." here to ensure that all
172// arguments get converted to a UWord appropriately. Not doing so can
173// cause problems when passing 32-bit integers on 64-bit platforms, because
174// the top 32-bits might not be zeroed appropriately, eg. as would happen
175// with the 6th arg on AMD64 which is passed on the stack.
njnf4aeaea2004-11-29 17:33:31 +0000176extern Word VG_(do_syscall) ( UInt, UWord, UWord, UWord, UWord, UWord, UWord );
njnca6fef02004-11-29 16:49:18 +0000177
178// Macros make life easier.
179#define vgPlain_do_syscall0(s) VG_(do_syscall)((s),0,0,0,0,0,0)
180#define vgPlain_do_syscall1(s,a) VG_(do_syscall)((s),(a),0,0,0,0,0)
181#define vgPlain_do_syscall2(s,a,b) VG_(do_syscall)((s),(a),(b),0,0,0,0)
182#define vgPlain_do_syscall3(s,a,b,c) VG_(do_syscall)((s),(a),(b),(c),0,0,0)
183#define vgPlain_do_syscall4(s,a,b,c,d) VG_(do_syscall)((s),(a),(b),(c),(d),0,0)
184#define vgPlain_do_syscall5(s,a,b,c,d,e) VG_(do_syscall)((s),(a),(b),(c),(d),(e),0)
185#define vgPlain_do_syscall6(s,a,b,c,d,e,f) VG_(do_syscall)((s),(a),(b),(c),(d),(e),(f))
186
fitzhardinge4f10ada2004-06-03 10:00:42 +0000187extern void VG_(sigreturn)(void);
sewardjde4a1d02002-03-22 01:27:54 +0000188
189/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000190 Exports of vg_helpers.S
191 ------------------------------------------------------------------ */
192
fitzhardinge92360792003-12-24 10:11:11 +0000193/* Information about trampoline code (for signal return and syscalls) */
194extern const Char VG_(trampoline_code_start);
195extern const Int VG_(trampoline_code_length);
196extern const Int VG_(tramp_sigreturn_offset);
sewardjb5f6f512005-03-10 23:59:00 +0000197extern const Int VG_(tramp_rt_sigreturn_offset);
fitzhardinge92360792003-12-24 10:11:11 +0000198extern const Int VG_(tramp_syscall_offset);
tomee0bcbf2005-05-02 10:28:42 +0000199extern const Int VG_(tramp_gettimeofday_offset);
200extern const Int VG_(tramp_time_offset);
njn3c008782005-05-31 22:15:14 +0000201
nethercotec06e2132004-09-03 13:45:29 +0000202// ---------------------------------------------------------------------
203// Architecture-specific things defined in eg. x86/*.c
204// ---------------------------------------------------------------------
205
sewardj51ac0872004-12-21 01:20:49 +0000206// Returns the architecture and subarchitecture, or indicates
207// that this subarchitecture is unable to run Valgrind
208// Returns False to indicate we cannot proceed further.
sewardj51ac0872004-12-21 01:20:49 +0000209extern Bool VGA_(getArchAndSubArch)( /*OUT*/VexArch*,
210 /*OUT*/VexSubArch* );
njncf45fd42004-11-24 16:30:22 +0000211// Accessors for the ThreadArchState
njn35172bc2005-03-26 00:04:03 +0000212#define INSTR_PTR(regs) ((regs).vex.VGA_INSTR_PTR)
213#define STACK_PTR(regs) ((regs).vex.VGA_STACK_PTR)
214#define FRAME_PTR(regs) ((regs).vex.VGA_FRAME_PTR)
215#define CLREQ_ARGS(regs) ((regs).vex.VGA_CLREQ_ARGS)
njn35172bc2005-03-26 00:04:03 +0000216#define CLREQ_RET(regs) ((regs).vex.VGA_CLREQ_RET)
njn16de5572004-11-27 14:27:21 +0000217// Offsets for the Vex state
njn35172bc2005-03-26 00:04:03 +0000218#define O_STACK_PTR (offsetof(VexGuestArchState, VGA_STACK_PTR))
njn35172bc2005-03-26 00:04:03 +0000219#define O_CLREQ_RET (offsetof(VexGuestArchState, VGA_CLREQ_RET))
njncf45fd42004-11-24 16:30:22 +0000220
221
sewardj2a99cf62004-11-24 10:44:19 +0000222// Setting up the initial thread (1) state
223extern void
224 VGA_(init_thread1state) ( Addr client_eip,
225 Addr esp_at_startup,
226 /*MOD*/ ThreadArchState* arch );
sewardjde4a1d02002-03-22 01:27:54 +0000227
sewardjb5f6f512005-03-10 23:59:00 +0000228// OS/Platform-specific thread clear (after thread exit)
sewardj1d887112005-05-30 21:44:08 +0000229extern void VGO_(os_state_clear)(ThreadState *);
sewardjb5f6f512005-03-10 23:59:00 +0000230
231// OS/Platform-specific thread init (at scheduler init time)
sewardj1d887112005-05-30 21:44:08 +0000232extern void VGO_(os_state_init)(ThreadState *);
sewardjb5f6f512005-03-10 23:59:00 +0000233
sewardj1d887112005-05-30 21:44:08 +0000234// Run a thread from beginning to end.
235extern VgSchedReturnCode VGO_(thread_wrapper)(Word /*ThreadId*/ tid);
sewardjb5f6f512005-03-10 23:59:00 +0000236
sewardj1d887112005-05-30 21:44:08 +0000237// Call here to exit the entire Valgrind system.
238extern void VGO_(terminate_NORETURN)(ThreadId tid, VgSchedReturnCode src);
239
240// Allocates a stack for the first thread, then runs it,
241// as if the thread had been set up by clone()
242extern void VGP_(main_thread_wrapper_NORETURN)(ThreadId tid);
sewardjb5f6f512005-03-10 23:59:00 +0000243
244// Return how many bytes of a thread's Valgrind stack are unused
sewardj1d887112005-05-30 21:44:08 +0000245extern SSizeT VGA_(stack_unused)(ThreadId tid);
sewardjb5f6f512005-03-10 23:59:00 +0000246
sewardjb5f6f512005-03-10 23:59:00 +0000247// wait until all other threads are dead
248extern void VGA_(reap_threads)(ThreadId self);
249
250// handle an arch-specific client request
251extern Bool VGA_(client_request)(ThreadId tid, UWord *args);
252
nethercotefedd8102004-09-13 15:19:34 +0000253// For attaching the debugger
sewardj2a99cf62004-11-24 10:44:19 +0000254extern Int VGA_(ptrace_setregs_from_tst) ( Int pid, ThreadArchState* arch );
nethercotefedd8102004-09-13 15:19:34 +0000255
sewardjb5f6f512005-03-10 23:59:00 +0000256// Used by leakcheck
257extern void VGA_(mark_from_registers)(ThreadId tid, void (*marker)(Addr));
258
njn20242342005-05-16 23:31:24 +0000259// Set up the libc freeres wrapper
260extern void VGA_(intercept_libc_freeres_wrapper)(Addr);
261
262// Clean up the client by calling before the final reports
263extern void VGA_(final_tidyup)(ThreadId tid);
264
265// Arch-specific client requests
266extern Bool VGA_(client_requests)(ThreadId tid, UWord *args);
267
nethercote9b3c7652004-10-19 13:18:00 +0000268
sewardjb5f6f512005-03-10 23:59:00 +0000269///* ---------------------------------------------------------------------
270// Thread modelling
271// ------------------------------------------------------------------ */
272//extern void VG_(tm_thread_create) (ThreadId creator, ThreadId tid, Bool detached);
273//extern void VG_(tm_thread_exit) (ThreadId tid);
274//extern Bool VG_(tm_thread_exists) (ThreadId tid);
275//extern void VG_(tm_thread_detach) (ThreadId tid);
276//extern void VG_(tm_thread_join) (ThreadId joiner, ThreadId joinee);
277//extern void VG_(tm_thread_switchto)(ThreadId tid);
278//
279//extern void VG_(tm_mutex_init) (ThreadId tid, Addr mutexp);
280//extern void VG_(tm_mutex_destroy)(ThreadId tid, Addr mutexp);
281//extern void VG_(tm_mutex_trylock)(ThreadId tid, Addr mutexp);
282//extern void VG_(tm_mutex_giveup) (ThreadId tid, Addr mutexp);
283//extern void VG_(tm_mutex_acquire)(ThreadId tid, Addr mutexp);
284//extern void VG_(tm_mutex_tryunlock)(ThreadId tid, Addr mutexp);
285//extern void VG_(tm_mutex_unlock) (ThreadId tid, Addr mutexp);
286//extern Bool VG_(tm_mutex_exists) (Addr mutexp);
287//
288//extern UInt VG_(tm_error_update_extra) (Error *err);
289//extern Bool VG_(tm_error_equal) (VgRes res, Error *e1, Error *e2);
290//extern void VG_(tm_error_print) (Error *err);
291//
292//extern void VG_(tm_init) ();
293//
294//extern void VG_(tm_cond_init) (ThreadId tid, Addr condp);
295//extern void VG_(tm_cond_destroy) (ThreadId tid, Addr condp);
296//extern void VG_(tm_cond_wait) (ThreadId tid, Addr condp, Addr mutexp);
297//extern void VG_(tm_cond_wakeup) (ThreadId tid, Addr condp, Addr mutexp);
298//extern void VG_(tm_cond_signal) (ThreadId tid, Addr condp);
299//
300///* ----- pthreads ----- */
301//extern void VG_(pthread_init) ();
302//extern void VG_(pthread_startfunc_wrapper)(Addr wrapper);
303//
304//struct vg_pthread_newthread_data {
305// void *(*startfunc)(void *arg);
306// void *arg;
307//};
sewardj3b2736a2002-03-24 12:18:35 +0000308
309/* ---------------------------------------------------------------------
310 Finally - autoconf-generated settings
311 ------------------------------------------------------------------ */
312
313#include "config.h"
314
nethercotec06e2132004-09-03 13:45:29 +0000315#endif /* ndef __CORE_H */
316
sewardjde4a1d02002-03-22 01:27:54 +0000317/*--------------------------------------------------------------------*/
nethercote109d0df2004-09-02 08:10:13 +0000318/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +0000319/*--------------------------------------------------------------------*/