blob: c5e170bd69efadf817eee6488c0a8f542f226685 [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
38// Ugly: this is needed by linux/core_os.h
39typedef struct _ThreadState ThreadState;
40
nethercotebb4222b2004-09-10 17:42:11 +000041#include "core_platform.h" // platform-specific stuff,
42 // eg. x86-linux/core_platform.h
sewardjb5f6f512005-03-10 23:59:00 +000043#include "core_os.h" // OS-specific stuff, eg. linux/core_os.h
sewardjde4a1d02002-03-22 01:27:54 +000044
njn717cde52005-05-10 02:47:21 +000045#include "pub_core_mallocfree.h" // for type 'ArenaId'
njnd01fef72005-03-25 23:35:48 +000046#include "pub_core_stacktrace.h" // for type 'StackTrace'
47
njn4f6e3702005-05-16 20:50:52 +000048#include <setjmp.h> /* for jmp_buf */
49
nethercote7be47252004-09-02 16:02:58 +000050/* ---------------------------------------------------------------------
njn14319cc2005-03-13 06:26:22 +000051 Global macros.
nethercote7be47252004-09-02 16:02:58 +000052 ------------------------------------------------------------------ */
53
sewardjde4a1d02002-03-22 01:27:54 +000054/* Max length of a text fragment used to construct error messages. */
njn47b209a2005-03-25 23:47:16 +000055#define VG_ERRTXT_LEN 4096
sewardjde4a1d02002-03-22 01:27:54 +000056
sewardjde4a1d02002-03-22 01:27:54 +000057/* The maximum number of calls we're prepared to save in a
58 backtrace. */
59#define VG_DEEPEST_BACKTRACE 50
60
fitzhardinge98abfc72003-12-16 02:05:15 +000061/* Useful macros */
62/* a - alignment - must be a power of 2 */
tomde2ec262005-03-29 12:16:10 +000063#define ROUNDDN(p, a) ((Addr)(p) & ~((Addr)(a)-1))
fitzhardinge98abfc72003-12-16 02:05:15 +000064#define ROUNDUP(p, a) ROUNDDN((p)+(a)-1, (a))
nethercote73b526f2004-10-31 18:48:21 +000065#define PGROUNDDN(p) ROUNDDN(p, VKI_PAGE_SIZE)
66#define PGROUNDUP(p) ROUNDUP(p, VKI_PAGE_SIZE)
fitzhardinge98abfc72003-12-16 02:05:15 +000067
sewardj51ac0872004-12-21 01:20:49 +000068
nethercote80013e92004-09-05 20:39:51 +000069/* ---------------------------------------------------------------------
70 Environment variables
71 ------------------------------------------------------------------ */
72
73/* The directory we look for all our auxillary files in */
74#define VALGRINDLIB "VALGRINDLIB"
75
76/* Additional command-line arguments; they are overridden by actual
77 command-line option. Each argument is separated by spaces. There
78 is no quoting mechanism.
79 */
80#define VALGRINDOPTS "VALGRIND_OPTS"
81
82/* If this variable is present in the environment, then valgrind will
83 not parse the command line for options at all; all options come
84 from this variable. Arguments are terminated by ^A (\001). There
85 is no quoting mechanism.
86
87 This variable is not expected to be set by anything other than
88 Valgrind itself, as part of its handling of execve with
89 --trace-children=yes. This variable should not be present in the
90 client environment.
91 */
92#define VALGRINDCLO "_VALGRIND_CLO"
93
fitzhardinge98abfc72003-12-16 02:05:15 +000094
thughesad1c9562004-06-26 11:27:52 +000095/* Application-visible file descriptor limits */
96extern Int VG_(fd_soft_limit);
97extern Int VG_(fd_hard_limit);
fitzhardingef0046f22003-12-18 02:39:22 +000098
sewardjde4a1d02002-03-22 01:27:54 +000099/* ---------------------------------------------------------------------
nethercote85cdd342004-08-01 22:36:40 +0000100 Profiling stuff
sewardjde4a1d02002-03-22 01:27:54 +0000101 ------------------------------------------------------------------ */
102
njn31066fd2005-03-26 00:42:02 +0000103extern void VG_(init_profiling) ( void );
104extern void VG_(done_profiling) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000105
njn25e49d8e72002-09-23 09:36:25 +0000106#undef VGP_PUSHCC
107#undef VGP_POPCC
njn31066fd2005-03-26 00:42:02 +0000108#define VGP_PUSHCC(x) if (VG_(clo_profile)) VG_(pushcc)(x)
109#define VGP_POPCC(x) if (VG_(clo_profile)) VG_(popcc)(x)
sewardjde4a1d02002-03-22 01:27:54 +0000110
sewardj51ac0872004-12-21 01:20:49 +0000111
sewardjde4a1d02002-03-22 01:27:54 +0000112/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000113 Exports of vg_intercept.c
sewardj2e93c502002-04-12 11:12:52 +0000114 ------------------------------------------------------------------ */
115
njna91498f2005-05-11 22:32:39 +0000116/* These are the internal client request codes. The publically-visible
117 request codes are also defined in valgrind.h, and similar headers for
118 some tools. */
sewardj2e93c502002-04-12 11:12:52 +0000119
njna91498f2005-05-11 22:32:39 +0000120/* Get the tool's malloc-wrapping functions */
fitzhardinge98abfc72003-12-16 02:05:15 +0000121#define VG_USERREQ__GET_MALLOCFUNCS 0x3030
sewardjb5f6f512005-03-10 23:59:00 +0000122
fitzhardinge39de4b42003-10-31 07:12:21 +0000123/* Internal equivalent of VALGRIND_PRINTF . */
124#define VG_USERREQ__INTERNAL_PRINTF 0x3103
sewardj45b4b372002-04-16 22:50:32 +0000125
sewardjb5f6f512005-03-10 23:59:00 +0000126/* Denote the finish of __libc_freeres_wrapper().
127 A synonym for exit. */
128#define VG_USERREQ__LIBC_FREERES_DONE 0x3029
sewardj54cacf02002-04-12 23:24:59 +0000129
njn717cde52005-05-10 02:47:21 +0000130/* Intercept prefix stuff. See
131 coregrind/m_replace_malloc/vg_replace_malloc.c for details.
132 Unfortunately the "_vgi_" literal is also hardcoded in that file, so if
133 you change this one you must also change the other one. */
sewardj9ee81f52005-04-02 17:38:59 +0000134#define VG_INTERCEPT_PREFIX "_vgi_"
135#define VG_INTERCEPT_PREFIX_LEN 5
rjwalshe4e779d2004-04-16 23:02:29 +0000136
sewardj9ee81f52005-04-02 17:38:59 +0000137/* Not sure what these are for. Todo: clarify */
138#define VG_WRAPPER_PREFIX "_vgw_"
139#define VG_WRAPPER_PREFIX_LEN 5
140#define VG_WRAPPER(name) _vgw_##name
141#define VG_WRAPPER_ALIAS(name) "_vgw_" #name
rjwalshe4e779d2004-04-16 23:02:29 +0000142
njn4c791212003-05-02 17:53:54 +0000143
sewardj2e93c502002-04-12 11:12:52 +0000144/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000145 Exports of vg_scheduler.c
146 ------------------------------------------------------------------ */
147
sewardjb5f6f512005-03-10 23:59:00 +0000148/*
149 Thread state machine:
150
151 Empty -> Init -> Runnable <=> WaitSys/Yielding
152 ^ |
153 \---- Zombie -----/
154 */
sewardj2e93c502002-04-12 11:12:52 +0000155typedef
jsgf855d93d2003-10-13 22:26:55 +0000156 enum ThreadStatus {
sewardj2e93c502002-04-12 11:12:52 +0000157 VgTs_Empty, /* this slot is not in use */
sewardjb5f6f512005-03-10 23:59:00 +0000158 VgTs_Init, /* just allocated */
159 VgTs_Runnable, /* ready to run */
jsgf855d93d2003-10-13 22:26:55 +0000160 VgTs_WaitSys, /* waiting for a syscall to complete */
sewardjb5f6f512005-03-10 23:59:00 +0000161 VgTs_Yielding, /* temporarily yielding the CPU */
162 VgTs_Zombie, /* transient state just before exiting */
sewardj2e93c502002-04-12 11:12:52 +0000163 }
164 ThreadStatus;
sewardj8ad94e12002-05-29 00:10:20 +0000165
sewardjb5f6f512005-03-10 23:59:00 +0000166/* Return codes from the scheduler. */
thughes11975ff2004-06-12 12:58:22 +0000167typedef
sewardjb5f6f512005-03-10 23:59:00 +0000168 enum {
169 VgSrc_None, /* not exiting yet */
170 VgSrc_ExitSyscall, /* client called exit(). This is the normal
171 route out. */
172 VgSrc_FatalSig /* Killed by the default action of a fatal
173 signal */
thughes11975ff2004-06-12 12:58:22 +0000174 }
sewardjb5f6f512005-03-10 23:59:00 +0000175 VgSchedReturnCode;
thughes11975ff2004-06-12 12:58:22 +0000176
njn77e8ac12005-05-16 21:16:44 +0000177
178#if defined(VGA_x86)
179 typedef VexGuestX86State VexGuestArchState;
tombb422232005-05-16 23:16:25 +0000180#elif defined(VGA_amd64)
njn77e8ac12005-05-16 21:16:44 +0000181 typedef VexGuestAMD64State VexGuestArchState;
182#elif defined(VGA_arm)
183 typedef VexGuestARMState VexGuestArchState;
184#else
185# error Unknown architecture
186#endif
187
188
189typedef
190 struct {
191 /* --- BEGIN vex-mandated guest state --- */
192
193 /* Saved machine context. */
194 VexGuestArchState vex;
195
196 /* Saved shadow context. */
197 VexGuestArchState vex_shadow;
198
199 /* Spill area. */
200 UChar vex_spill[LibVEX_N_SPILL_BYTES];
201
202 /* --- END vex-mandated guest state --- */
203 }
204 ThreadArchState;
205
206
sewardjb5f6f512005-03-10 23:59:00 +0000207struct _ThreadState {
njn25e49d8e72002-09-23 09:36:25 +0000208 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
209 The thread identity is simply the index in vg_threads[].
210 ThreadId == 1 is the root thread and has the special property
211 that we don't try and allocate or deallocate its stack. For
212 convenience of generating error message, we also put the
213 ThreadId in this tid field, but be aware that it should
214 ALWAYS == the index in vg_threads[]. */
215 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000216
sewardjb5f6f512005-03-10 23:59:00 +0000217 /* Current scheduling status. */
njn25e49d8e72002-09-23 09:36:25 +0000218 ThreadStatus status;
sewardj2e93c502002-04-12 11:12:52 +0000219
sewardjb5f6f512005-03-10 23:59:00 +0000220 /* This is set if the thread is in the process of exiting for any
221 reason. The precise details of the exit are in the OS-specific
222 state. */
223 VgSchedReturnCode exitreason;
sewardj3b5d8862002-04-20 13:53:23 +0000224
sewardjb5f6f512005-03-10 23:59:00 +0000225 /* Architecture-specific thread state. */
226 ThreadArchState arch;
sewardjb48e5002002-05-13 00:16:03 +0000227
njn25e49d8e72002-09-23 09:36:25 +0000228 /* This thread's blocked-signals mask. Semantics is that for a
229 signal to be delivered to this thread, the signal must not be
jsgf855d93d2003-10-13 22:26:55 +0000230 blocked by this signal mask. If more than one thread accepts a
231 signal, then it will be delivered to one at random. If all
232 threads block the signal, it will remain pending until either a
sewardjb5f6f512005-03-10 23:59:00 +0000233 thread unblocks it or someone uses sigwaitsig/sigtimedwait. */
nethercote73b526f2004-10-31 18:48:21 +0000234 vki_sigset_t sig_mask;
sewardjb48e5002002-05-13 00:16:03 +0000235
sewardjb5f6f512005-03-10 23:59:00 +0000236 /* tmp_sig_mask is usually the same as sig_mask, and is kept in
237 sync whenever sig_mask is changed. The only time they have
238 different values is during the execution of a sigsuspend, where
239 tmp_sig_mask is the temporary mask which sigsuspend installs.
240 It is only consulted to compute the signal mask applied to a
241 signal handler. */
242 vki_sigset_t tmp_sig_mask;
sewardj2e93c502002-04-12 11:12:52 +0000243
sewardjb5f6f512005-03-10 23:59:00 +0000244 /* A little signal queue for signals we can't get the kernel to
245 queue for us. This is only allocated as needed, since it should
246 be rare. */
247 struct SigQueue *sig_queue;
248
249 /* Syscall the Thread is currently running; -1 if none. Should only
250 be set while Thread is in VgTs_WaitSys. */
251 Int syscallno;
252
253 /* A value the Tool wants to pass from its pre-syscall to its
254 post-syscall function. */
255 void *tool_pre_syscall_value;
thughes8abf3922004-10-16 10:59:49 +0000256
njn50ba34e2005-04-04 02:41:42 +0000257 /* Client stacks. When a thread slot is freed, we don't deallocate its
njn25e49d8e72002-09-23 09:36:25 +0000258 stack; we just leave it lying around for the next use of the
259 slot. If the next use of the slot requires a larger stack,
260 only then is the old one deallocated and a new one
261 allocated.
sewardj2e93c502002-04-12 11:12:52 +0000262
njn25e49d8e72002-09-23 09:36:25 +0000263 For the main thread (threadid == 0), this mechanism doesn't
264 apply. We don't know the size of the stack since we didn't
265 allocate it, and furthermore we never reallocate it. */
sewardj2e93c502002-04-12 11:12:52 +0000266
njn25e49d8e72002-09-23 09:36:25 +0000267 /* The allocated size of this thread's stack (permanently zero
268 if this is ThreadId == 0, since we didn't allocate its stack) */
njn50ba34e2005-04-04 02:41:42 +0000269 SizeT client_stack_szB;
sewardj2e93c502002-04-12 11:12:52 +0000270
sewardj92a59562002-09-30 00:53:10 +0000271 /* Address of the highest legitimate word in this stack. This is
272 used for error messages only -- not critical for execution
273 correctness. Is is set for all stacks, specifically including
274 ThreadId == 0 (the main thread). */
njn50ba34e2005-04-04 02:41:42 +0000275 Addr client_stack_highest_word;
njn25e49d8e72002-09-23 09:36:25 +0000276
fitzhardinge98c4dc02004-03-16 08:27:29 +0000277 /* Alternate signal stack */
nethercote73b526f2004-10-31 18:48:21 +0000278 vki_stack_t altstack;
fitzhardinge98c4dc02004-03-16 08:27:29 +0000279
sewardjb5f6f512005-03-10 23:59:00 +0000280 /* OS-specific thread state */
281 os_thread_t os_state;
sewardj004e8ca2005-02-28 17:27:04 +0000282
283 /* Used in the syscall handlers. Set to True to indicate that the
284 PRE routine for a syscall has set the syscall result already and
285 so the syscall does not need to be handed to the kernel. */
286 Bool syscall_result_set;
sewardjb5f6f512005-03-10 23:59:00 +0000287
288 /* Per-thread jmp_buf to resume scheduler after a signal */
289 Bool sched_jmpbuf_valid;
290 jmp_buf sched_jmpbuf;
nethercote8ff888f2004-11-17 17:11:45 +0000291};
sewardj2e93c502002-04-12 11:12:52 +0000292
sewardj018f7622002-05-15 21:13:39 +0000293/* The thread table. */
294extern ThreadState VG_(threads)[VG_N_THREADS];
295
sewardjb5f6f512005-03-10 23:59:00 +0000296/* Allocate a new ThreadState */
297extern ThreadId VG_(alloc_ThreadState)(void);
298
299/* A thread exits. tid must currently be running. */
300extern void VG_(exit_thread)(ThreadId tid);
301
302/* Kill a thread. This interrupts whatever a thread is doing, and
303 makes it exit ASAP. This does not set the exitreason or
304 exitcode. */
305extern void VG_(kill_thread)(ThreadId tid);
306
sewardj018f7622002-05-15 21:13:39 +0000307/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000308extern Bool VG_(is_valid_tid) ( ThreadId tid );
309
jsgf855d93d2003-10-13 22:26:55 +0000310/* Get the ThreadState for a particular thread */
311extern ThreadState *VG_(get_ThreadState)(ThreadId tid);
312
sewardjb5f6f512005-03-10 23:59:00 +0000313/* Given an LWP id (ie, real kernel thread id), find the corresponding
314 ThreadId */
315extern ThreadId VG_(get_lwp_tid)(Int lwpid);
316
317/* Returns true if a thread is currently running (ie, has the CPU lock) */
318extern Bool VG_(is_running_thread)(ThreadId tid);
319
320/* Returns true if the thread is in the process of exiting */
321extern Bool VG_(is_exiting)(ThreadId tid);
322
323/* Return the number of non-dead Threads */
324extern Int VG_(count_living_threads)(void);
325
sewardjccef2e62002-05-29 19:26:32 +0000326/* Nuke all threads except tid. */
sewardjb5f6f512005-03-10 23:59:00 +0000327extern void VG_(nuke_all_threads_except) ( ThreadId me, VgSchedReturnCode reason );
sewardjccef2e62002-05-29 19:26:32 +0000328
sewardjb5f6f512005-03-10 23:59:00 +0000329/* Make a thread the running thread. The thread must previously been
330 sleeping, and not holding the CPU semaphore. This will set the
331 thread state to VgTs_Runnable, and the thread will attempt to take
332 the CPU semaphore. By the time it returns, tid will be the running
333 thread. */
334extern void VG_(set_running) ( ThreadId tid );
jsgf855d93d2003-10-13 22:26:55 +0000335
sewardjb5f6f512005-03-10 23:59:00 +0000336/* Set a thread into a sleeping state. Before the call, the thread
337 must be runnable, and holding the CPU semaphore. When this call
338 returns, the thread will be set to the specified sleeping state,
339 and will not be holding the CPU semaphore. Note that another
340 thread could be running by the time this call returns, so the
341 caller must be careful not to touch any shared state. It is also
342 the caller's responsibility to actually block until the thread is
343 ready to run again. */
344extern void VG_(set_sleeping) ( ThreadId tid, ThreadStatus state );
sewardj2e93c502002-04-12 11:12:52 +0000345
sewardjb5f6f512005-03-10 23:59:00 +0000346/* Yield the CPU for a while */
347extern void VG_(vg_yield)(void);
sewardj7e87e382002-05-03 19:09:05 +0000348
sewardjb5f6f512005-03-10 23:59:00 +0000349// The scheduler.
350extern VgSchedReturnCode VG_(scheduler) ( ThreadId tid );
351
352// Do everything which needs doing before the process finally ends,
353// like printing reports, etc
354extern void VG_(shutdown_actions)(ThreadId tid);
sewardj2e93c502002-04-12 11:12:52 +0000355
356extern void VG_(scheduler_init) ( void );
357
sewardj15a43e12002-04-17 19:35:12 +0000358extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000359
nethercote75d26242004-08-01 22:59:18 +0000360// Longjmp back to the scheduler and thus enter the sighandler immediately.
sewardjb5f6f512005-03-10 23:59:00 +0000361extern void VG_(resume_scheduler) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000362
sewardjb5f6f512005-03-10 23:59:00 +0000363/* If true, a fault is Valgrind-internal (ie, a bug) */
364extern Bool VG_(my_fault);
nethercote238a3c32004-08-09 13:13:31 +0000365
sewardj2e93c502002-04-12 11:12:52 +0000366/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000367 Exports of vg_signals.c
368 ------------------------------------------------------------------ */
369
sewardjb5f6f512005-03-10 23:59:00 +0000370/* Highest signal the kernel will let us use */
371extern Int VG_(max_signal);
jsgf855d93d2003-10-13 22:26:55 +0000372
sewardjde4a1d02002-03-22 01:27:54 +0000373extern void VG_(sigstartup_actions) ( void );
374
jsgf855d93d2003-10-13 22:26:55 +0000375extern Bool VG_(is_sig_ign) ( Int sigNo );
376
sewardjb5f6f512005-03-10 23:59:00 +0000377/* Poll a thread's set of pending signals, and update the Thread's context to deliver one */
378extern void VG_(poll_signals) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000379
380/* Fake system calls for signal handling. */
njn502badb2005-05-08 02:04:49 +0000381extern Int VG_(do_sys_sigaltstack) ( ThreadId tid, vki_stack_t* ss,
382 vki_stack_t* oss );
383extern Int VG_(do_sys_sigaction) ( Int signo,
384 const struct vki_sigaction *new_act,
385 struct vki_sigaction *old_act );
386extern Int VG_(do_sys_sigprocmask) ( ThreadId tid, Int how,
387 vki_sigset_t* set,
388 vki_sigset_t* oldset );
sewardjefbfcdf2002-06-19 17:35:45 +0000389
njn444eba12005-05-12 03:47:31 +0000390extern void VG_(clear_out_queued_signals)
391 ( ThreadId tid, /* OUT */ vki_sigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +0000392
jsgf855d93d2003-10-13 22:26:55 +0000393extern void VG_(kill_self)(Int sigNo);
394
fitzhardingef1beb252004-03-16 09:49:08 +0000395/* These function synthesize a fault, as if the running instruction
396 had had a fault. These functions do not return - they longjmp back
397 into the scheduler so the signal can be delivered. */
398extern void VG_(synth_fault) (ThreadId tid);
399extern void VG_(synth_fault_mapping)(ThreadId tid, Addr addr);
400extern void VG_(synth_fault_perms) (ThreadId tid, Addr addr);
sewardj5e2f0012004-12-13 14:10:34 +0000401extern void VG_(synth_sigill) (ThreadId tid, Addr addr);
fitzhardingef1beb252004-03-16 09:49:08 +0000402
sewardjb5f6f512005-03-10 23:59:00 +0000403/* Extend the stack to cover addr, if possible */
404extern Bool VG_(extend_stack)(Addr addr, UInt maxsize);
405
406/* Returns True if the signal is OK for the client to use */
407extern Bool VG_(client_signal_OK)(Int sigNo);
408
409/* Forces the client's signal handler to SIG_DFL - generally just
410 before using that signal to kill the process. */
411extern void VG_(set_default_handler)(Int sig);
412
413/* Adjust a client's signal mask to match our internal requirements */
414extern void VG_(sanitize_client_sigmask)(ThreadId tid, vki_sigset_t *mask);
415
416/* Wait until a thread-related predicate is true */
417extern void VG_(wait_for_threadstate)(Bool (*pred)(void *), void *arg);
sewardj51ac0872004-12-21 01:20:49 +0000418
sewardjde4a1d02002-03-22 01:27:54 +0000419/* ---------------------------------------------------------------------
420 Exports of vg_mylibc.c
421 ------------------------------------------------------------------ */
422
njnca0518d2004-11-26 19:34:36 +0000423// Useful for making failing stubs, when certain things haven't yet been
424// implemented.
njn50ae1a72005-04-08 23:28:23 +0000425#define I_die_here \
sewardj0ab10c42005-05-12 08:26:36 +0000426 VG_(assert_fail) (/*isCore*//*BOGUS*/True, \
427 "Unimplemented functionality", \
njn50ae1a72005-04-08 23:28:23 +0000428 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
429 "valgrind", VG_BUGS_TO, "")
njnca0518d2004-11-26 19:34:36 +0000430
njn50ae1a72005-04-08 23:28:23 +0000431#define vg_assert(expr) \
432 ((void) ((expr) ? 0 : \
433 (VG_(assert_fail) (/*isCore*/True, VG_STRINGIFY(expr), \
434 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
435 ""), \
436 0)))
437
438#define vg_assert2(expr, format, args...) \
439 ((void) ((expr) ? 0 : \
440 (VG_(assert_fail) (/*isCore*/True, VG_STRINGIFY(expr), \
441 __FILE__, __LINE__, __PRETTY_FUNCTION__, \
442 format, ##args), \
443 0)))
444
njne427a662002-10-02 11:08:25 +0000445__attribute__ ((__noreturn__))
446extern void VG_(core_panic) ( Char* str );
thughes5876d552004-09-26 18:44:06 +0000447__attribute__ ((__noreturn__))
njnd01fef72005-03-25 23:35:48 +0000448extern void VG_(core_panic_at) ( Char* str, StackTrace ips );
sewardjde4a1d02002-03-22 01:27:54 +0000449
nethercote05675c82004-08-04 10:37:49 +0000450/* Tools use VG_(strdup)() which doesn't expose ArenaId */
njn25e49d8e72002-09-23 09:36:25 +0000451extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
sewardjde4a1d02002-03-22 01:27:54 +0000452
njn25e49d8e72002-09-23 09:36:25 +0000453extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
jsgf855d93d2003-10-13 22:26:55 +0000454extern Int VG_(poll)( struct vki_pollfd *, UInt nfds, Int timeout);
sewardj2e93c502002-04-12 11:12:52 +0000455
fitzhardinge98abfc72003-12-16 02:05:15 +0000456/* system/mman.h */
nethercote8b5f40c2004-11-02 13:29:50 +0000457extern void* VG_(mmap)( void* start, SizeT length, UInt prot, UInt flags,
nethercote5b9fafd2004-11-04 18:39:22 +0000458 UInt sf_flags, UInt fd, OffT offset );
nethercote8b5f40c2004-11-02 13:29:50 +0000459extern Int VG_(munmap)( void* start, SizeT length );
460extern Int VG_(mprotect)( void *start, SizeT length, UInt prot );
sewardj79048ce2005-02-18 08:28:32 +0000461extern Int VG_(mprotect_native)( void *start, SizeT length, UInt prot );
fitzhardinge98abfc72003-12-16 02:05:15 +0000462
463
jsgf855d93d2003-10-13 22:26:55 +0000464/* Move an fd into the Valgrind-safe range */
465Int VG_(safe_fd)(Int oldfd);
466
sewardj570f8902002-11-03 11:44:36 +0000467extern Int VG_(write_socket)( Int sd, void *msg, Int count );
sewardj73cf3bc2002-11-03 03:20:15 +0000468
469/* --- Connecting over the network --- */
470extern Int VG_(connect_via_socket)( UChar* str );
471
fitzhardinge98abfc72003-12-16 02:05:15 +0000472/* Environment manipulations */
nethercote60a96c52004-08-03 13:08:31 +0000473extern Char **VG_(env_setenv) ( Char ***envp, const Char* varname,
474 const Char *val );
475extern void VG_(env_unsetenv) ( Char **env, const Char *varname );
476extern void VG_(env_remove_valgrind_env_stuff) ( Char** env );
sewardj570f8902002-11-03 11:44:36 +0000477
sewardjb5f6f512005-03-10 23:59:00 +0000478extern void VG_(nanosleep)(struct vki_timespec *);
njn2521d322005-05-08 14:45:13 +0000479
sewardj570f8902002-11-03 11:44:36 +0000480/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000481 Exports of vg_symtab2.c
482 ------------------------------------------------------------------ */
483
fitzhardinge98abfc72003-12-16 02:05:15 +0000484typedef struct _Segment Segment;
sewardjb5f6f512005-03-10 23:59:00 +0000485typedef struct _CodeRedirect CodeRedirect;
fitzhardinge98abfc72003-12-16 02:05:15 +0000486
487extern Bool VG_(is_object_file) ( const void *hdr );
fitzhardinge98abfc72003-12-16 02:05:15 +0000488extern SegInfo * VG_(read_seg_symbols) ( Segment *seg );
njn36ef6ba2005-05-14 18:42:26 +0000489extern void VG_(seginfo_incref) ( SegInfo * );
490extern void VG_(seginfo_decref) ( SegInfo *, Addr a );
sewardjde4a1d02002-03-22 01:27:54 +0000491
njn25e49d8e72002-09-23 09:36:25 +0000492extern Bool VG_(get_fnname_nodemangle)( Addr a, Char* fnname, Int n_fnname );
sewardj25c7c3a2003-07-10 00:17:58 +0000493
sewardjb5f6f512005-03-10 23:59:00 +0000494extern Addr VG_(reverse_search_one_symtab) ( const SegInfo* si, const Char* name );
495
sewardj35165532005-04-30 18:47:48 +0000496extern Bool VG_(use_CFI_info) ( /*MOD*/Addr* ipP,
497 /*MOD*/Addr* spP,
498 /*MOD*/Addr* fpP,
499 Addr min_accessible,
500 Addr max_accessible );
501
502
sewardjb5f6f512005-03-10 23:59:00 +0000503/* ---------------------------------------------------------------------
504 Exports of vg_redir.c
505 ------------------------------------------------------------------ */
njn36b66df2005-05-12 05:13:04 +0000506
fitzhardinge98abfc72003-12-16 02:05:15 +0000507/* Redirection machinery */
nethercote85cdd342004-08-01 22:36:40 +0000508extern Addr VG_(code_redirect) ( Addr orig );
sewardjde4a1d02002-03-22 01:27:54 +0000509
njn36b66df2005-05-12 05:13:04 +0000510/* Set up some default redirects */
511extern void VG_(setup_code_redirect_table) ( void );
512
tom748a1312005-04-02 15:53:01 +0000513extern void VG_(add_redirect_sym_to_addr)(const Char *from_lib,
514 const Char *from_sym,
515 Addr to_addr);
516extern void VG_(add_redirect_addr_to_addr)(Addr from_addr, Addr to_addr);
sewardjb5f6f512005-03-10 23:59:00 +0000517extern void VG_(resolve_seg_redirs)(SegInfo *si);
sewardjb5f6f512005-03-10 23:59:00 +0000518
519/* Wrapping machinery */
520enum return_type {
521 RT_RETURN,
522 RT_LONGJMP,
523 RT_EXIT,
524};
525
526typedef struct _FuncWrapper FuncWrapper;
527struct _FuncWrapper {
528 void *(*before)(va_list args);
529 void (*after) (void *nonce, enum return_type, Word retval);
530};
531
532extern void VG_(wrap_function)(Addr eip, const FuncWrapper *wrapper);
533extern const FuncWrapper *VG_(is_wrapped)(Addr eip);
534extern Bool VG_(is_wrapper_return)(Addr eip);
535
536/* Primary interface for adding wrappers for client-side functions. */
537extern CodeRedirect *VG_(add_wrapper)(const Char *from_lib, const Char *from_sym,
538 const FuncWrapper *wrapper);
539
540extern Bool VG_(is_resolved)(const CodeRedirect *redir);
sewardj51ac0872004-12-21 01:20:49 +0000541
sewardjde4a1d02002-03-22 01:27:54 +0000542/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000543 Exports of vg_main.c
544 ------------------------------------------------------------------ */
545
sewardj73cf3bc2002-11-03 03:20:15 +0000546/* Tell the logging mechanism whether we are logging to a file
547 descriptor or a socket descriptor. */
njnbe9b47b2005-05-15 16:22:58 +0000548extern Bool VG_(logging_to_socket);
sewardj73cf3bc2002-11-03 03:20:15 +0000549
njn25e49d8e72002-09-23 09:36:25 +0000550/* Sanity checks which may be done at any time. The scheduler decides when. */
nethercote885dd912004-08-03 23:14:00 +0000551extern void VG_(sanity_check_general) ( Bool force_expensive );
njn25e49d8e72002-09-23 09:36:25 +0000552
fitzhardinge98abfc72003-12-16 02:05:15 +0000553/* Address space */
554extern Addr VG_(client_base); /* client address space limits */
555extern Addr VG_(client_end);
556extern Addr VG_(client_mapbase); /* base of mappings */
557extern Addr VG_(clstk_base); /* client stack range */
558extern Addr VG_(clstk_end);
fitzhardinge92360792003-12-24 10:11:11 +0000559extern Addr VG_(client_trampoline_code);
560
fitzhardinge98abfc72003-12-16 02:05:15 +0000561extern Addr VG_(brk_base); /* start of brk */
562extern Addr VG_(brk_limit); /* current brk */
nethercote996901a2004-08-03 13:29:09 +0000563extern Addr VG_(shadow_base); /* tool's shadow memory */
fitzhardinge98abfc72003-12-16 02:05:15 +0000564extern Addr VG_(shadow_end);
565extern Addr VG_(valgrind_base); /* valgrind's address range */
nethercote820bd8c2004-09-07 23:04:49 +0000566extern Addr VG_(valgrind_last); // Nb: last byte, rather than one past the end
fitzhardinge98abfc72003-12-16 02:05:15 +0000567
nethercote73b526f2004-10-31 18:48:21 +0000568extern struct vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
569extern struct vki_rlimit VG_(client_rlimit_stack); /* client's original rlimit stack */
fitzhardingeb50068f2004-02-24 23:42:55 +0000570
fitzhardingea49f9b52003-12-16 22:26:45 +0000571/* client executable file descriptor */
572extern Int VG_(clexecfd);
fitzhardinge98abfc72003-12-16 02:05:15 +0000573
nethercotef6a1d502004-08-09 12:21:57 +0000574// Help set up the child used when doing execve() with --trace-children=yes
575Char* VG_(build_child_VALGRINDCLO) ( Char* exename );
576Char* VG_(build_child_exename) ( void );
577
sewardjb5f6f512005-03-10 23:59:00 +0000578/* The master thread the one which will be responsible for mopping
579 everything up at exit. Normally it is tid 1, since that's the
580 first thread created, but it may be something else after a
581 fork(). */
582extern ThreadId VG_(master_tid);
583
sewardjde4a1d02002-03-22 01:27:54 +0000584/* Called when some unhandleable client behaviour is detected.
585 Prints a msg and aborts. */
njn25e49d8e72002-09-23 09:36:25 +0000586extern void VG_(unimplemented) ( Char* msg )
587 __attribute__((__noreturn__));
sewardjde4a1d02002-03-22 01:27:54 +0000588
nethercote04d0fbc2004-01-26 16:48:06 +0000589/* Something of a function looking for a home ... start up debugger. */
njnc6168192004-11-29 13:54:10 +0000590extern void VG_(start_debugger) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +0000591
sewardjde4a1d02002-03-22 01:27:54 +0000592/* Counts downwards in vg_run_innerloop. */
593extern UInt VG_(dispatch_ctr);
594
sewardj4ccf7072004-11-28 16:58:05 +0000595/* Stats ... */
nethercote844e7122004-08-02 15:27:22 +0000596extern void VG_(print_scheduler_stats) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000597
njn1f707722005-03-27 03:17:52 +0000598/* 64-bit counter for the number of basic blocks done. */
599extern ULong VG_(bbs_done);
600
nethercote2e05c332004-09-06 16:43:37 +0000601
sewardjde4a1d02002-03-22 01:27:54 +0000602/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000603 Exports of vg_syscall.S
604 ------------------------------------------------------------------ */
605
njnca6fef02004-11-29 16:49:18 +0000606// We use a full prototype rather than "..." here to ensure that all
607// arguments get converted to a UWord appropriately. Not doing so can
608// cause problems when passing 32-bit integers on 64-bit platforms, because
609// the top 32-bits might not be zeroed appropriately, eg. as would happen
610// with the 6th arg on AMD64 which is passed on the stack.
njnf4aeaea2004-11-29 17:33:31 +0000611extern Word VG_(do_syscall) ( UInt, UWord, UWord, UWord, UWord, UWord, UWord );
njnca6fef02004-11-29 16:49:18 +0000612
613// Macros make life easier.
614#define vgPlain_do_syscall0(s) VG_(do_syscall)((s),0,0,0,0,0,0)
615#define vgPlain_do_syscall1(s,a) VG_(do_syscall)((s),(a),0,0,0,0,0)
616#define vgPlain_do_syscall2(s,a,b) VG_(do_syscall)((s),(a),(b),0,0,0,0)
617#define vgPlain_do_syscall3(s,a,b,c) VG_(do_syscall)((s),(a),(b),(c),0,0,0)
618#define vgPlain_do_syscall4(s,a,b,c,d) VG_(do_syscall)((s),(a),(b),(c),(d),0,0)
619#define vgPlain_do_syscall5(s,a,b,c,d,e) VG_(do_syscall)((s),(a),(b),(c),(d),(e),0)
620#define vgPlain_do_syscall6(s,a,b,c,d,e,f) VG_(do_syscall)((s),(a),(b),(c),(d),(e),(f))
621
fitzhardinge4f10ada2004-06-03 10:00:42 +0000622extern void VG_(sigreturn)(void);
sewardjde4a1d02002-03-22 01:27:54 +0000623
624/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000625 Exports of vg_helpers.S
626 ------------------------------------------------------------------ */
627
fitzhardinge92360792003-12-24 10:11:11 +0000628/* Information about trampoline code (for signal return and syscalls) */
629extern const Char VG_(trampoline_code_start);
630extern const Int VG_(trampoline_code_length);
631extern const Int VG_(tramp_sigreturn_offset);
sewardjb5f6f512005-03-10 23:59:00 +0000632extern const Int VG_(tramp_rt_sigreturn_offset);
fitzhardinge92360792003-12-24 10:11:11 +0000633extern const Int VG_(tramp_syscall_offset);
tomee0bcbf2005-05-02 10:28:42 +0000634extern const Int VG_(tramp_gettimeofday_offset);
635extern const Int VG_(tramp_time_offset);
sewardj20917d82002-05-28 01:36:45 +0000636
nethercotec06e2132004-09-03 13:45:29 +0000637// ---------------------------------------------------------------------
638// Architecture-specific things defined in eg. x86/*.c
639// ---------------------------------------------------------------------
640
sewardj51ac0872004-12-21 01:20:49 +0000641// Returns the architecture and subarchitecture, or indicates
642// that this subarchitecture is unable to run Valgrind
643// Returns False to indicate we cannot proceed further.
sewardj51ac0872004-12-21 01:20:49 +0000644extern Bool VGA_(getArchAndSubArch)( /*OUT*/VexArch*,
645 /*OUT*/VexSubArch* );
njncf45fd42004-11-24 16:30:22 +0000646// Accessors for the ThreadArchState
njn35172bc2005-03-26 00:04:03 +0000647#define INSTR_PTR(regs) ((regs).vex.VGA_INSTR_PTR)
648#define STACK_PTR(regs) ((regs).vex.VGA_STACK_PTR)
649#define FRAME_PTR(regs) ((regs).vex.VGA_FRAME_PTR)
650#define CLREQ_ARGS(regs) ((regs).vex.VGA_CLREQ_ARGS)
njn35172bc2005-03-26 00:04:03 +0000651#define CLREQ_RET(regs) ((regs).vex.VGA_CLREQ_RET)
njn16de5572004-11-27 14:27:21 +0000652// Offsets for the Vex state
njn35172bc2005-03-26 00:04:03 +0000653#define O_STACK_PTR (offsetof(VexGuestArchState, VGA_STACK_PTR))
654#define O_FRAME_PTR (offsetof(VexGuestArchState, VGA_FRAME_PTR))
655#define O_CLREQ_RET (offsetof(VexGuestArchState, VGA_CLREQ_RET))
njncf45fd42004-11-24 16:30:22 +0000656
657
sewardj2a99cf62004-11-24 10:44:19 +0000658// Setting up the initial thread (1) state
659extern void
660 VGA_(init_thread1state) ( Addr client_eip,
661 Addr esp_at_startup,
662 /*MOD*/ ThreadArchState* arch );
sewardjde4a1d02002-03-22 01:27:54 +0000663
sewardjb5f6f512005-03-10 23:59:00 +0000664// OS/Platform-specific thread clear (after thread exit)
665extern void VGA_(os_state_clear)(ThreadState *);
666
667// OS/Platform-specific thread init (at scheduler init time)
668extern void VGA_(os_state_init)(ThreadState *);
669
670// Run a thread from beginning to end. Does not return if tid == VG_(master_tid).
sewardj0c1a5962005-03-22 00:19:55 +0000671void VGA_(thread_wrapper)(Word /*ThreadId*/ tid);
sewardjb5f6f512005-03-10 23:59:00 +0000672
673// Like VGA_(thread_wrapper), but it allocates a stack before calling
674// to VGA_(thread_wrapper) on that stack, as if it had been set up by
675// clone()
676void VGA_(main_thread_wrapper)(ThreadId tid) __attribute__ ((__noreturn__));
677
678// Return how many bytes of a thread's Valgrind stack are unused
njn990e90c2005-04-05 02:49:09 +0000679SSizeT VGA_(stack_unused)(ThreadId tid);
sewardjb5f6f512005-03-10 23:59:00 +0000680
sewardjb5f6f512005-03-10 23:59:00 +0000681// wait until all other threads are dead
682extern void VGA_(reap_threads)(ThreadId self);
683
684// handle an arch-specific client request
685extern Bool VGA_(client_request)(ThreadId tid, UWord *args);
686
nethercotefedd8102004-09-13 15:19:34 +0000687// Pointercheck
688extern Bool VGA_(setup_pointercheck) ( void );
689
690// For attaching the debugger
sewardj2a99cf62004-11-24 10:44:19 +0000691extern Int VGA_(ptrace_setregs_from_tst) ( Int pid, ThreadArchState* arch );
nethercotefedd8102004-09-13 15:19:34 +0000692
sewardjb5f6f512005-03-10 23:59:00 +0000693// Used by leakcheck
694extern void VGA_(mark_from_registers)(ThreadId tid, void (*marker)(Addr));
695
njn20242342005-05-16 23:31:24 +0000696// Set up the libc freeres wrapper
697extern void VGA_(intercept_libc_freeres_wrapper)(Addr);
698
699// Clean up the client by calling before the final reports
700extern void VGA_(final_tidyup)(ThreadId tid);
701
702// Arch-specific client requests
703extern Bool VGA_(client_requests)(ThreadId tid, UWord *args);
704
nethercote9b3c7652004-10-19 13:18:00 +0000705
nethercote41c75da2004-10-18 15:34:14 +0000706// ---------------------------------------------------------------------
707// Platform-specific things defined in eg. x86/*.c
708// ---------------------------------------------------------------------
nethercote775508a2004-09-07 22:38:23 +0000709
njn2521d322005-05-08 14:45:13 +0000710// Do any platform specific redirects.
tom748a1312005-04-02 15:53:01 +0000711extern void VGP_(setup_redirects)(void);
sewardjb5f6f512005-03-10 23:59:00 +0000712
713///* ---------------------------------------------------------------------
714// Thread modelling
715// ------------------------------------------------------------------ */
716//extern void VG_(tm_thread_create) (ThreadId creator, ThreadId tid, Bool detached);
717//extern void VG_(tm_thread_exit) (ThreadId tid);
718//extern Bool VG_(tm_thread_exists) (ThreadId tid);
719//extern void VG_(tm_thread_detach) (ThreadId tid);
720//extern void VG_(tm_thread_join) (ThreadId joiner, ThreadId joinee);
721//extern void VG_(tm_thread_switchto)(ThreadId tid);
722//
723//extern void VG_(tm_mutex_init) (ThreadId tid, Addr mutexp);
724//extern void VG_(tm_mutex_destroy)(ThreadId tid, Addr mutexp);
725//extern void VG_(tm_mutex_trylock)(ThreadId tid, Addr mutexp);
726//extern void VG_(tm_mutex_giveup) (ThreadId tid, Addr mutexp);
727//extern void VG_(tm_mutex_acquire)(ThreadId tid, Addr mutexp);
728//extern void VG_(tm_mutex_tryunlock)(ThreadId tid, Addr mutexp);
729//extern void VG_(tm_mutex_unlock) (ThreadId tid, Addr mutexp);
730//extern Bool VG_(tm_mutex_exists) (Addr mutexp);
731//
732//extern UInt VG_(tm_error_update_extra) (Error *err);
733//extern Bool VG_(tm_error_equal) (VgRes res, Error *e1, Error *e2);
734//extern void VG_(tm_error_print) (Error *err);
735//
736//extern void VG_(tm_init) ();
737//
738//extern void VG_(tm_cond_init) (ThreadId tid, Addr condp);
739//extern void VG_(tm_cond_destroy) (ThreadId tid, Addr condp);
740//extern void VG_(tm_cond_wait) (ThreadId tid, Addr condp, Addr mutexp);
741//extern void VG_(tm_cond_wakeup) (ThreadId tid, Addr condp, Addr mutexp);
742//extern void VG_(tm_cond_signal) (ThreadId tid, Addr condp);
743//
744///* ----- pthreads ----- */
745//extern void VG_(pthread_init) ();
746//extern void VG_(pthread_startfunc_wrapper)(Addr wrapper);
747//
748//struct vg_pthread_newthread_data {
749// void *(*startfunc)(void *arg);
750// void *arg;
751//};
sewardj3b2736a2002-03-24 12:18:35 +0000752
753/* ---------------------------------------------------------------------
754 Finally - autoconf-generated settings
755 ------------------------------------------------------------------ */
756
757#include "config.h"
758
nethercotec06e2132004-09-03 13:45:29 +0000759#endif /* ndef __CORE_H */
760
sewardjde4a1d02002-03-22 01:27:54 +0000761/*--------------------------------------------------------------------*/
nethercote109d0df2004-09-02 08:10:13 +0000762/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +0000763/*--------------------------------------------------------------------*/