blob: 68f16cca7610db31150323afe39e72f42a170752 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00003/*--- A header file for all private parts of Valgrind's core. ---*/
nethercote5a2664c2004-09-02 15:37:39 +00004/*--- Include no other! (more or less...) ---*/
rjwalsh7109a8c2004-09-02 00:31:02 +00005/*--- core.h ---*/
sewardjde4a1d02002-03-22 01:27:54 +00006/*--------------------------------------------------------------------*/
7
8/*
njnc9539842002-10-02 13:26:35 +00009 This file is part of Valgrind, an extensible x86 protected-mode
10 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +000011
nethercotebb1c9912004-01-04 16:43:23 +000012 Copyright (C) 2000-2004 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000013 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000014
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 02111-1307, USA.
29
njn25e49d8e72002-09-23 09:36:25 +000030 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000031*/
32
rjwalsh7109a8c2004-09-02 00:31:02 +000033#ifndef __CORE_H
34#define __CORE_H
sewardjde4a1d02002-03-22 01:27:54 +000035
nethercote7be47252004-09-02 16:02:58 +000036/*
37 Header hierarchy:
38
39 - core C files include core.h
40 - core asm files include core_asm.h
41 - tool C files include tool.h
42 - tool asm files include tool_asm.h
43
44 - The hierarchy of the header files themselves is based around the
45 following rules:
46
47 - core headers include tool headers
48 - generic headers include arch/OS/platform headers
49 - C headers include asm headers
50
51 This gives the following hierarchy (only showing 'arch' headers, not
nethercote80cca432004-09-02 16:25:49 +000052 'os' or 'platform' headers), where arrows indicate inclusion, and
53 $VG_ARCH==x86:
nethercote7be47252004-09-02 16:02:58 +000054
nethercote80cca432004-09-02 16:25:49 +000055
56 (include/x86/tool_arch_asm.h?) <----- coregrind/x86/core_arch_asm.h
57 ^ ^ ^ ^
58 / \ / \
59 / \ / \
60 / \ / \
61 include/tool_asm.h <-\---- coregrind/core_asm.h \
62 ^ \ ^ \
63 \ include/x86/tool_arch.h <--------coregrind/x86/core_arch.h
64 \ ^ \ ^
65 \ / \ /
66 \ / \ /
67 \ / \ /
68 include/tool.h <------------ coregrind/core.h
69
nethercote7be47252004-09-02 16:02:58 +000070
71 Note that core.h contains the *declarations* of arch-specific functions
72 and variables, which can be used by the core_arch.h file of any
73 architecture. (The functions/variables are *defined* within arch/.)
74 However, arch-specific macros and types cannot go into core.h, because
75 there is no separation between declaration and definition for
76 macros/types, so they instead go into $VG_ARCH/core_arch.h.
nethercote80cca432004-09-02 16:25:49 +000077
78 The tool-specific headers are all in include/ so they can be seen by any
79 external tools.
nethercote7be47252004-09-02 16:02:58 +000080*/
81
sewardjde4a1d02002-03-22 01:27:54 +000082
jsgf855d93d2003-10-13 22:26:55 +000083/* For system call numbers __NR_... */
nethercotef94fe2f2004-09-10 14:23:59 +000084#include "vki_unistd.h"
jsgf855d93d2003-10-13 22:26:55 +000085
nethercote13343132004-09-02 15:49:09 +000086#include "core_asm.h" // asm stuff
87#include "tool.h" // tool stuff
nethercotebb4222b2004-09-10 17:42:11 +000088#include "core_arch.h" // arch-specific stuff, eg. x86/core_arch.h
89#include "core_platform.h" // platform-specific stuff,
90 // eg. x86-linux/core_platform.h
sewardjde4a1d02002-03-22 01:27:54 +000091
fitzhardinge39de4b42003-10-31 07:12:21 +000092#include "valgrind.h"
sewardjde4a1d02002-03-22 01:27:54 +000093
fitzhardinge98abfc72003-12-16 02:05:15 +000094#undef SK_
95#define SK_(x) vgSkinInternal_##x
96
nethercote7be47252004-09-02 16:02:58 +000097
98/* ---------------------------------------------------------------------
99 Build options and table sizes. You should be able to change these
100 options or sizes, recompile, and still have a working system.
101 ------------------------------------------------------------------ */
102
sewardjde4a1d02002-03-22 01:27:54 +0000103/* Constants for the slow translation lookup cache. */
104#define VG_TRANSTAB_SLOW_BITS 11
105#define VG_TRANSTAB_SLOW_SIZE (1 << VG_TRANSTAB_SLOW_BITS)
106#define VG_TRANSTAB_SLOW_MASK ((VG_TRANSTAB_SLOW_SIZE) - 1)
107
108/* Size of a buffer used for creating messages. */
109#define M_VG_MSGBUF 10000
110
111/* Size of a smallish table used to read /proc/self/map entries. */
sewardjebc82332002-04-24 14:44:23 +0000112#define M_PROCMAP_BUF 50000
sewardjde4a1d02002-03-22 01:27:54 +0000113
114/* Max length of pathname to a .so/executable file. */
115#define M_VG_LIBNAMESTR 100
116
117/* Max length of a text fragment used to construct error messages. */
nethercote3b38c1d2004-10-18 15:47:18 +0000118#define M_VG_ERRTXT 4096
sewardjde4a1d02002-03-22 01:27:54 +0000119
120/* Max length of the string copied from env var VG_ARGS at startup. */
121#define M_VG_CMDLINE_STRLEN 1000
122
123/* Max number of options for Valgrind which we can handle. */
124#define M_VG_CMDLINE_OPTS 100
125
126/* After this many different unsuppressed errors have been observed,
127 be more conservative about collecting new ones. */
128#define M_VG_COLLECT_ERRORS_SLOWLY_AFTER 50
129
130/* After this many different unsuppressed errors have been observed,
131 stop collecting errors at all, and tell the user their program is
132 evidently a steaming pile of camel dung. */
sewardj1bebcbf2002-04-24 21:24:18 +0000133#define M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN 300
sewardjf2537be2002-04-24 21:03:47 +0000134
135/* After this many total errors have been observed, stop collecting
136 errors at all. Counterpart to M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN. */
sewardj1bebcbf2002-04-24 21:24:18 +0000137#define M_VG_COLLECT_NO_ERRORS_AFTER_FOUND 30000
sewardjde4a1d02002-03-22 01:27:54 +0000138
sewardjde4a1d02002-03-22 01:27:54 +0000139/* The maximum number of calls we're prepared to save in a
140 backtrace. */
141#define VG_DEEPEST_BACKTRACE 50
142
sewardjde4a1d02002-03-22 01:27:54 +0000143/* Number of lists in which we keep track of ExeContexts. Should be
144 prime. */
sewardj59fb25c2003-09-28 16:32:58 +0000145#define VG_N_EC_LISTS 4999 /* a prime number */
sewardjde4a1d02002-03-22 01:27:54 +0000146
sewardj2e93c502002-04-12 11:12:52 +0000147/* Defines the thread-scheduling timeslice, in terms of the number of
148 basic blocks we attempt to run each thread for. Smaller values
149 give finer interleaving but much increased scheduling overheads. */
sewardj4505b9e2002-05-28 11:27:31 +0000150#define VG_SCHEDULING_QUANTUM 50000
sewardj2e93c502002-04-12 11:12:52 +0000151
fitzhardingef0046f22003-12-18 02:39:22 +0000152/* Number of file descriptors that Valgrind tries to reserve for
153 it's own use - two per thread plues a small number of extras. */
154#define VG_N_RESERVED_FDS (VG_N_THREADS*2 + 4)
sewardj2e93c502002-04-12 11:12:52 +0000155
sewardjbf290b92002-05-01 02:28:01 +0000156/* Stack size for a thread. We try and check that they do not go
157 beyond it. */
sewardjf0b06452002-06-04 08:38:04 +0000158#define VG_PTHREAD_STACK_SIZE (1 << 20)
sewardjbf290b92002-05-01 02:28:01 +0000159
sewardj8ad94e12002-05-29 00:10:20 +0000160/* Number of entries in each thread's cleanup stack. */
sewardj61821c02003-05-04 13:02:10 +0000161#define VG_N_CLEANUPSTACK 16
sewardj8ad94e12002-05-29 00:10:20 +0000162
sewardj2cb00342002-06-28 01:46:26 +0000163/* Number of entries in each thread's fork-handler stack. */
sewardj4700f042003-07-26 17:49:58 +0000164#define VG_N_FORKHANDLERSTACK 4
sewardj2cb00342002-06-28 01:46:26 +0000165
njn25e49d8e72002-09-23 09:36:25 +0000166/* Max number of callers for context in a suppression. */
167#define VG_N_SUPP_CALLERS 4
sewardj73cf3bc2002-11-03 03:20:15 +0000168
thughes8abf3922004-10-16 10:59:49 +0000169/* Numer of entries in each thread's signal queue. */
170#define VG_N_SIGNALQUEUE 8
171
fitzhardinge98abfc72003-12-16 02:05:15 +0000172/* Useful macros */
173/* a - alignment - must be a power of 2 */
174#define ROUNDDN(p, a) ((Addr)(p) & ~((a)-1))
175#define ROUNDUP(p, a) ROUNDDN((p)+(a)-1, (a))
176#define PGROUNDDN(p) ROUNDDN(p, VKI_BYTES_PER_PAGE)
177#define PGROUNDUP(p) ROUNDUP(p, VKI_BYTES_PER_PAGE)
178
nethercote80013e92004-09-05 20:39:51 +0000179/* ---------------------------------------------------------------------
180 Environment variables
181 ------------------------------------------------------------------ */
182
183/* The directory we look for all our auxillary files in */
184#define VALGRINDLIB "VALGRINDLIB"
185
186/* Additional command-line arguments; they are overridden by actual
187 command-line option. Each argument is separated by spaces. There
188 is no quoting mechanism.
189 */
190#define VALGRINDOPTS "VALGRIND_OPTS"
191
192/* If this variable is present in the environment, then valgrind will
193 not parse the command line for options at all; all options come
194 from this variable. Arguments are terminated by ^A (\001). There
195 is no quoting mechanism.
196
197 This variable is not expected to be set by anything other than
198 Valgrind itself, as part of its handling of execve with
199 --trace-children=yes. This variable should not be present in the
200 client environment.
201 */
202#define VALGRINDCLO "_VALGRIND_CLO"
203
fitzhardinge98abfc72003-12-16 02:05:15 +0000204
sewardjde4a1d02002-03-22 01:27:54 +0000205/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000206 Command-line-settable options
207 ------------------------------------------------------------------ */
208
sewardj4f094a72002-11-05 23:37:35 +0000209/* Default destination port to be used in logging over a network, if
210 none specified. */
211#define VG_CLO_DEFAULT_LOGPORT 1500
sewardj73cf3bc2002-11-03 03:20:15 +0000212
213/* The max number of suppression files. */
sewardjde4a1d02002-03-22 01:27:54 +0000214#define VG_CLO_MAX_SFILES 10
215
nethercote04d0fbc2004-01-26 16:48:06 +0000216/* Default debugger command. */
217#define VG_CLO_DEFAULT_DBCOMMAND GDB_PATH " -nw %f %p"
218
sewardj4cf05692002-10-27 20:28:29 +0000219/* Describes where logging output is to be sent. */
220typedef
221 enum {
222 VgLogTo_Fd,
223 VgLogTo_File,
224 VgLogTo_Socket
225 } VgLogTo;
226
jsgf855d93d2003-10-13 22:26:55 +0000227/* pid of main process */
228extern Int VG_(main_pid);
229
230/* pgrp of process (global to all threads) */
231extern Int VG_(main_pgrp);
sewardj4cf05692002-10-27 20:28:29 +0000232
thughesad1c9562004-06-26 11:27:52 +0000233/* Application-visible file descriptor limits */
234extern Int VG_(fd_soft_limit);
235extern Int VG_(fd_hard_limit);
fitzhardingef0046f22003-12-18 02:39:22 +0000236
sewardj72f98ff2002-06-13 17:23:38 +0000237/* Should we stop collecting errors if too many appear? default: YES */
sewardj2e432902002-06-13 20:44:00 +0000238extern Bool VG_(clo_error_limit);
nethercote04d0fbc2004-01-26 16:48:06 +0000239/* Enquire about whether to attach to a debugger at errors? default: NO */
240extern Bool VG_(clo_db_attach);
241/* The debugger command? default: whatever gdb ./configure found */
242extern Char* VG_(clo_db_command);
njn43c799e2003-04-08 00:08:52 +0000243/* Enquire about generating a suppression for each error? default: NO */
244extern Bool VG_(clo_gen_suppressions);
sewardjde4a1d02002-03-22 01:27:54 +0000245/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
nethercote27fec902004-06-16 21:26:32 +0000246extern Int VG_(clo_sanity_level);
sewardjde4a1d02002-03-22 01:27:54 +0000247/* Automatically attempt to demangle C++ names? default: YES */
248extern Bool VG_(clo_demangle);
sewardjde4a1d02002-03-22 01:27:54 +0000249/* Simulate child processes? default: NO */
250extern Bool VG_(clo_trace_children);
sewardj4cf05692002-10-27 20:28:29 +0000251
252/* Where logging output is to be sent to.
253
nethercotef8548672004-06-21 12:42:35 +0000254 When log_to == VgLogTo_Fd, clo_log_fd holds the file id, and is
255 taken from the command line. clo_log_name is irrelevant.
sewardj4cf05692002-10-27 20:28:29 +0000256
nethercotef8548672004-06-21 12:42:35 +0000257 When log_to == VgLogTo_File, clo_log_name holds the log-file
258 name, and is taken from the command line. clo_log_fd is then
259 made to hold the relevant file id, by opening clo_log_name
sewardj4cf05692002-10-27 20:28:29 +0000260 (concatenated with the process ID) for writing.
261
nethercotef8548672004-06-21 12:42:35 +0000262 When log_to == VgLogTo_Socket, clo_log_name holds the
sewardj4cf05692002-10-27 20:28:29 +0000263 hostname:portnumber pair, and is taken from the command line.
nethercotef8548672004-06-21 12:42:35 +0000264 clo_log_fd is then made to hold the relevant file handle, by
sewardj4cf05692002-10-27 20:28:29 +0000265 opening a connection to said hostname:portnumber pair.
266
nethercotef8548672004-06-21 12:42:35 +0000267 Global default is to set log_to == VgLogTo_Fd and log_fd == 2
sewardj4cf05692002-10-27 20:28:29 +0000268 (stderr). */
269extern VgLogTo VG_(clo_log_to);
nethercotef8548672004-06-21 12:42:35 +0000270extern Int VG_(clo_log_fd);
271extern Char* VG_(clo_log_name);
sewardjde4a1d02002-03-22 01:27:54 +0000272
thughes6233a382004-08-21 11:10:44 +0000273/* Add timestamps to log messages? default: NO */
274extern Bool VG_(clo_time_stamp);
275
sewardj6024b212003-07-13 10:54:33 +0000276/* The file descriptor to read for input. default: 0 == stdin */
277extern Int VG_(clo_input_fd);
sewardjde4a1d02002-03-22 01:27:54 +0000278/* The number of suppression files specified. */
279extern Int VG_(clo_n_suppressions);
280/* The names of the suppression files. */
281extern Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
282
283/* Single stepping? default: NO */
284extern Bool VG_(clo_single_step);
285/* Code improvement? default: YES */
286extern Bool VG_(clo_optimise);
njn25e49d8e72002-09-23 09:36:25 +0000287/* DEBUG: print generated code? default: 00000 ( == NO ) */
288extern Bool VG_(clo_trace_codegen);
sewardjde4a1d02002-03-22 01:27:54 +0000289/* DEBUG: print system calls? default: NO */
290extern Bool VG_(clo_trace_syscalls);
291/* DEBUG: print signal details? default: NO */
292extern Bool VG_(clo_trace_signals);
293/* DEBUG: print symtab details? default: NO */
294extern Bool VG_(clo_trace_symtab);
sewardj8937c812002-04-12 20:12:20 +0000295/* DEBUG: print thread scheduling events? default: NO */
296extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000297/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
298 (some), 2 (all) */
299extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000300/* Display gory details for the k'th most popular error. default:
301 Infinity. */
302extern Int VG_(clo_dump_error);
303/* Number of parents of a backtrace. Default: 8. */
304extern Int VG_(clo_backtrace_size);
daywalker7e73e5f2003-07-04 16:18:15 +0000305/* Engage miscellaneous weird hacks needed for some progs. */
sewardj8d365b52002-05-12 10:52:16 +0000306extern Char* VG_(clo_weird_hacks);
jsgf855d93d2003-10-13 22:26:55 +0000307/* How often we should poll for signals, assuming we need to poll for
308 signals. */
309extern Int VG_(clo_signal_polltime);
310
311/* Low latency syscalls and signals */
312extern Bool VG_(clo_lowlat_syscalls);
313extern Bool VG_(clo_lowlat_signals);
314
rjwalshf5f536f2003-11-17 17:45:00 +0000315/* Track open file descriptors? */
316extern Bool VG_(clo_track_fds);
317
sewardj858964b2002-10-05 14:15:43 +0000318/* Should we run __libc_freeres at exit? Sometimes causes crashes.
319 Default: YES. Note this is subservient to VG_(needs).libc_freeres;
320 if the latter says False, then the setting of VG_(clo_weird_hacks)
nethercote996901a2004-08-03 13:29:09 +0000321 is ignored. Ie if a tool says no, I don't want this to run, that
sewardj858964b2002-10-05 14:15:43 +0000322 cannot be overridden from the command line. */
323extern Bool VG_(clo_run_libc_freeres);
sewardjb5ff83e2002-12-01 19:40:49 +0000324/* Use the basic-block chaining optimisation? Default: YES */
sewardj22854b92002-11-30 14:00:47 +0000325extern Bool VG_(clo_chain_bb);
fitzhardinge462f4f92003-12-18 02:10:54 +0000326/* Generate branch-prediction hints? */
327extern Bool VG_(clo_branchpred);
nethercote77eba602003-11-13 17:35:04 +0000328/* Continue stack traces below main()? Default: NO */
329extern Bool VG_(clo_show_below_main);
fitzhardinge98abfc72003-12-16 02:05:15 +0000330/* Test each client pointer dereference to check it's within the
331 client address space bounds */
332extern Bool VG_(clo_pointercheck);
sewardjde4a1d02002-03-22 01:27:54 +0000333
rjwalshe4e779d2004-04-16 23:02:29 +0000334/* Set up the libc freeres wrapper */
335extern void VG_(intercept_libc_freeres_wrapper)(Addr);
336
sewardjde4a1d02002-03-22 01:27:54 +0000337/* ---------------------------------------------------------------------
nethercote85cdd342004-08-01 22:36:40 +0000338 Profiling stuff
sewardjde4a1d02002-03-22 01:27:54 +0000339 ------------------------------------------------------------------ */
340
sewardjde4a1d02002-03-22 01:27:54 +0000341extern void VGP_(init_profiling) ( void );
342extern void VGP_(done_profiling) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000343
njn25e49d8e72002-09-23 09:36:25 +0000344#undef VGP_PUSHCC
345#undef VGP_POPCC
346#define VGP_PUSHCC(x) if (VG_(clo_profile)) VGP_(pushcc)(x)
347#define VGP_POPCC(x) if (VG_(clo_profile)) VGP_(popcc)(x)
sewardjde4a1d02002-03-22 01:27:54 +0000348
sewardjde4a1d02002-03-22 01:27:54 +0000349/* ---------------------------------------------------------------------
nethercote996901a2004-08-03 13:29:09 +0000350 Tool-related types
njn810086f2002-11-14 12:42:47 +0000351 ------------------------------------------------------------------ */
nethercote996901a2004-08-03 13:29:09 +0000352/* These structs are not exposed to tools to mitigate possibility of
353 binary-incompatibilities when the core/tool interface changes. Instead,
nethercote46063202004-09-02 08:51:43 +0000354 set functions are provided (see include/tool.h). */
njn810086f2002-11-14 12:42:47 +0000355typedef
356 struct {
357 Char* name;
358 Char* version;
359 Char* description;
360 Char* copyright_author;
361 Char* bug_reports_to;
njn120281f2003-02-03 12:20:07 +0000362 UInt avg_translation_sizeB;
njn810086f2002-11-14 12:42:47 +0000363 }
364 VgDetails;
365
366extern VgDetails VG_(details);
367
368/* If new fields are added to this type, update:
369 * - vg_main.c:initialisation of VG_(needs)
370 * - vg_main.c:sanity_check_needs()
371 *
372 * If the name of this type or any of its fields change, update:
373 * - dependent comments (just search for "VG_(needs)").
374 */
375typedef
376 struct {
377 Bool libc_freeres;
378 Bool core_errors;
njn810086f2002-11-14 12:42:47 +0000379 Bool skin_errors;
380 Bool basic_block_discards;
381 Bool shadow_regs;
382 Bool command_line_options;
383 Bool client_requests;
384 Bool extended_UCode;
385 Bool syscall_wrapper;
njn810086f2002-11-14 12:42:47 +0000386 Bool sanity_checks;
387 Bool data_syms;
fitzhardinge98abfc72003-12-16 02:05:15 +0000388 Bool shadow_memory;
njn810086f2002-11-14 12:42:47 +0000389 }
390 VgNeeds;
391
392extern VgNeeds VG_(needs);
393
fitzhardinge98abfc72003-12-16 02:05:15 +0000394extern void VG_(tool_init_dlsym)(void *dlhandle);
njn810086f2002-11-14 12:42:47 +0000395
fitzhardinge98abfc72003-12-16 02:05:15 +0000396#include "vg_toolint.h"
njn810086f2002-11-14 12:42:47 +0000397
398/* ---------------------------------------------------------------------
399 Exports of vg_needs.c
400 ------------------------------------------------------------------ */
401
402void VG_(sanity_check_needs)(void);
403
404/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000405 Exports of vg_malloc2.c
406 ------------------------------------------------------------------ */
407
408/* Allocation arenas.
njn3e884182003-04-15 13:03:23 +0000409
410 CORE for the core's general use.
nethercote60f5b822004-01-26 17:24:42 +0000411 TOOL for the tool to use (and the only one it uses).
njn3e884182003-04-15 13:03:23 +0000412 SYMTAB for Valgrind's symbol table storage.
413 JITTER for small storage during translation.
nethercote996901a2004-08-03 13:29:09 +0000414 CLIENT for the client's mallocs/frees, if the tool replaces glibc's
415 malloc() et al -- redzone size is chosen by the tool.
njn3e884182003-04-15 13:03:23 +0000416 DEMANGLE for the C++ demangler.
417 EXECTXT for storing ExeContexts.
418 ERRORS for storing CoreErrors.
419 TRANSIENT for very short-term use. It should be empty in between uses.
420
njn25e49d8e72002-09-23 09:36:25 +0000421 When adding a new arena, remember also to add it to ensure_mm_init().
sewardjde4a1d02002-03-22 01:27:54 +0000422*/
423typedef Int ArenaId;
424
njn3e884182003-04-15 13:03:23 +0000425#define VG_N_ARENAS 9
sewardjde4a1d02002-03-22 01:27:54 +0000426
njn3e884182003-04-15 13:03:23 +0000427#define VG_AR_CORE 0
nethercote60f5b822004-01-26 17:24:42 +0000428#define VG_AR_TOOL 1
njn3e884182003-04-15 13:03:23 +0000429#define VG_AR_SYMTAB 2
430#define VG_AR_JITTER 3
431#define VG_AR_CLIENT 4
432#define VG_AR_DEMANGLE 5
433#define VG_AR_EXECTXT 6
434#define VG_AR_ERRORS 7
435#define VG_AR_TRANSIENT 8
sewardjde4a1d02002-03-22 01:27:54 +0000436
nethercote2d5b8162004-08-11 09:40:52 +0000437// This is both the minimum payload size of a malloc'd block, and its
438// minimum alignment. Must be a power of 2 greater than 4, and should be
439// greater than 8.
440#define VG_MIN_MALLOC_SZB 8
441
442// Round-up size for --sloppy-malloc=yes.
443#define VG_SLOPPY_MALLOC_SZB 4
444
njn25e49d8e72002-09-23 09:36:25 +0000445extern void* VG_(arena_malloc) ( ArenaId arena, Int nbytes );
446extern void VG_(arena_free) ( ArenaId arena, void* ptr );
njn3e884182003-04-15 13:03:23 +0000447extern void* VG_(arena_calloc) ( ArenaId arena, Int alignment,
448 Int nmemb, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000449extern void* VG_(arena_realloc) ( ArenaId arena, void* ptr, Int alignment,
450 Int size );
451extern void* VG_(arena_malloc_aligned) ( ArenaId aid, Int req_alignB,
sewardjde4a1d02002-03-22 01:27:54 +0000452 Int req_pszB );
453
njn8a6b6c02003-04-22 22:45:55 +0000454extern Int VG_(arena_payload_szB) ( ArenaId aid, void* payload );
455
nethercote885dd912004-08-03 23:14:00 +0000456extern void VG_(sanity_check_malloc_all) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000457
nethercote3a42fb82004-08-03 18:08:50 +0000458extern void VG_(print_all_arena_stats) ( void );
nethercote885dd912004-08-03 23:14:00 +0000459
sewardjde4a1d02002-03-22 01:27:54 +0000460extern Bool VG_(is_empty_arena) ( ArenaId aid );
461
sewardjde4a1d02002-03-22 01:27:54 +0000462/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000463 Exports of vg_intercept.c
sewardj2e93c502002-04-12 11:12:52 +0000464 ------------------------------------------------------------------ */
465
466/* This doesn't export code or data that valgrind.so needs to link
467 against. However, the scheduler does need to know the following
468 request codes. A few, publically-visible, request codes are also
nethercote996901a2004-08-03 13:29:09 +0000469 defined in valgrind.h, and similar headers for some tools. */
sewardj2e93c502002-04-12 11:12:52 +0000470
njn4c791212003-05-02 17:53:54 +0000471#define VG_USERREQ__MALLOC 0x2001
472#define VG_USERREQ__FREE 0x2002
473
sewardj20917d82002-05-28 01:36:45 +0000474/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn
475 MUST NOT return -- ever. Eventually it will do either __QUIT or
476 __WAIT_JOINER. */
477#define VG_USERREQ__APPLY_IN_NEW_THREAD 0x3001
478
479/* ( no-args ): calling thread disappears from the system forever.
480 Reclaim resources. */
481#define VG_USERREQ__QUIT 0x3002
482
483/* ( void* ): calling thread waits for joiner and returns the void* to
484 it. */
485#define VG_USERREQ__WAIT_JOINER 0x3003
486
487/* ( ThreadId, void** ): wait to join a thread. */
488#define VG_USERREQ__PTHREAD_JOIN 0x3004
489
490/* Set cancellation state and type for this thread. */
491#define VG_USERREQ__SET_CANCELSTATE 0x3005
492#define VG_USERREQ__SET_CANCELTYPE 0x3006
493
494/* ( no-args ): Test if we are at a cancellation point. */
495#define VG_USERREQ__TESTCANCEL 0x3007
496
497/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
498 with this arg to indicate that a cancel is now pending for the
499 specified thread. */
500#define VG_USERREQ__SET_CANCELPEND 0x3008
501
502/* Set/get detach state for this thread. */
503#define VG_USERREQ__SET_OR_GET_DETACH 0x3009
504
thughese321d492004-10-17 15:00:20 +0000505#define VG_USERREQ__PTHREAD_GET_THREADID 0x300A
506#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x300B
507#define VG_USERREQ__PTHREAD_MUTEX_TIMEDLOCK 0x300C
sewardj20917d82002-05-28 01:36:45 +0000508#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK 0x300D
509#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x300E
510#define VG_USERREQ__PTHREAD_COND_WAIT 0x300F
511#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT 0x3010
512#define VG_USERREQ__PTHREAD_COND_SIGNAL 0x3011
513#define VG_USERREQ__PTHREAD_COND_BROADCAST 0x3012
514#define VG_USERREQ__PTHREAD_KEY_CREATE 0x3013
515#define VG_USERREQ__PTHREAD_KEY_DELETE 0x3014
sewardj00a66b12002-10-12 16:42:35 +0000516#define VG_USERREQ__PTHREAD_SETSPECIFIC_PTR 0x3015
517#define VG_USERREQ__PTHREAD_GETSPECIFIC_PTR 0x3016
sewardj20917d82002-05-28 01:36:45 +0000518#define VG_USERREQ__READ_MILLISECOND_TIMER 0x3017
519#define VG_USERREQ__PTHREAD_SIGMASK 0x3018
jsgf855d93d2003-10-13 22:26:55 +0000520#define VG_USERREQ__SIGWAIT 0x3019 /* unused */
sewardj20917d82002-05-28 01:36:45 +0000521#define VG_USERREQ__PTHREAD_KILL 0x301A
522#define VG_USERREQ__PTHREAD_YIELD 0x301B
sewardj00a66b12002-10-12 16:42:35 +0000523#define VG_USERREQ__PTHREAD_KEY_VALIDATE 0x301C
sewardj2e93c502002-04-12 11:12:52 +0000524
sewardj8ad94e12002-05-29 00:10:20 +0000525#define VG_USERREQ__CLEANUP_PUSH 0x3020
526#define VG_USERREQ__CLEANUP_POP 0x3021
sewardj870497a2002-05-29 01:06:47 +0000527#define VG_USERREQ__GET_KEY_D_AND_S 0x3022
sewardj8ad94e12002-05-29 00:10:20 +0000528
sewardjef037c72002-05-30 00:40:03 +0000529#define VG_USERREQ__NUKE_OTHER_THREADS 0x3023
sewardjefbfcdf2002-06-19 17:35:45 +0000530
531/* Ask how many signal handler returns have happened to this
532 thread. */
jsgf855d93d2003-10-13 22:26:55 +0000533#define VG_USERREQ__GET_N_SIGS_RETURNED 0x3024 /* unused */
sewardjef037c72002-05-30 00:40:03 +0000534
sewardj2cb00342002-06-28 01:46:26 +0000535/* Get/set entries for a thread's pthread_atfork stack. */
536#define VG_USERREQ__SET_FHSTACK_USED 0x3025
537#define VG_USERREQ__GET_FHSTACK_USED 0x3026
538#define VG_USERREQ__SET_FHSTACK_ENTRY 0x3027
539#define VG_USERREQ__GET_FHSTACK_ENTRY 0x3028
sewardjefbfcdf2002-06-19 17:35:45 +0000540
nethercotef971ab72004-08-02 16:27:40 +0000541/* Denote the finish of __libc_freeres_wrapper(). */
sewardj1fe7b002002-07-16 01:43:15 +0000542#define VG_USERREQ__LIBC_FREERES_DONE 0x3029
fitzhardinge98abfc72003-12-16 02:05:15 +0000543
544/* Allocate RT signals */
545#define VG_USERREQ__GET_SIGRT_MIN 0x302B
546#define VG_USERREQ__GET_SIGRT_MAX 0x302C
547#define VG_USERREQ__ALLOC_RTSIG 0x302D
548
549/* Hook for replace_malloc.o to get malloc functions */
550#define VG_USERREQ__GET_MALLOCFUNCS 0x3030
551
thughesdaa34562004-06-27 12:48:53 +0000552/* Get stack information for a thread. */
553#define VG_USERREQ__GET_STACK_INFO 0x3033
554
sewardj45b4b372002-04-16 22:50:32 +0000555/* Cosmetic ... */
556#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
sewardj4dced352002-06-04 22:54:20 +0000557/* Log a pthread error from client-space. Cosmetic. */
558#define VG_USERREQ__PTHREAD_ERROR 0x3102
fitzhardinge39de4b42003-10-31 07:12:21 +0000559/* Internal equivalent of VALGRIND_PRINTF . */
560#define VG_USERREQ__INTERNAL_PRINTF 0x3103
561/* Internal equivalent of VALGRIND_PRINTF_BACKTRACE . */
562#define VG_USERREQ__INTERNAL_PRINTF_BACKTRACE 0x3104
sewardj45b4b372002-04-16 22:50:32 +0000563
sewardj54cacf02002-04-12 23:24:59 +0000564/*
nethercote5a2664c2004-09-02 15:37:39 +0000565In core_asm.h:
sewardj54cacf02002-04-12 23:24:59 +0000566#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardj54cacf02002-04-12 23:24:59 +0000567*/
568
rjwalshe4e779d2004-04-16 23:02:29 +0000569#define VG_INTERCEPT_PREFIX "_vgi__"
570#define VG_INTERCEPT_PREFIX_LEN 6
571#define VG_INTERCEPT(name) _vgi__##name
572#define VG_INTERCEPT_ALIAS(name) "_vgi__" #name
573
574#define VG_WRAPPER_PREFIX "_vgw__"
575#define VG_WRAPPER_PREFIX_LEN 6
576#define VG_WRAPPER(name) _vgw__##name
577#define VG_WRAPPER_ALIAS(name) "_vgw__" #name
578
njn4c791212003-05-02 17:53:54 +0000579
fitzhardinge98abfc72003-12-16 02:05:15 +0000580struct vg_mallocfunc_info {
581 /* things vg_replace_malloc.o needs to know about */
582 Addr sk_malloc;
583 Addr sk_calloc;
584 Addr sk_realloc;
585 Addr sk_memalign;
586 Addr sk___builtin_new;
587 Addr sk___builtin_vec_new;
588 Addr sk_free;
589 Addr sk___builtin_delete;
590 Addr sk___builtin_vec_delete;
591
592 Addr arena_payload_szB;
593
594 Bool clo_sloppy_malloc;
595 Bool clo_trace_malloc;
596};
sewardj1fe7b002002-07-16 01:43:15 +0000597
sewardj2e93c502002-04-12 11:12:52 +0000598/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000599 Exports of vg_defaults.c
600 ------------------------------------------------------------------ */
601
602extern Bool VG_(sk_malloc_called_by_scheduler);
603
nethercotef9b59412004-09-10 15:33:32 +0000604/* ---------------------------------------------------------------------
nethercote1f0173b2004-02-28 15:40:36 +0000605 Exports of vg_libpthread.c
606 ------------------------------------------------------------------ */
607
608/* Replacements for pthread types, shared between vg_libpthread.c and
609 vg_scheduler.c. See comment in vg_libpthread.c above the other
610 vg_pthread_*_t types for a description of how these are used. */
611
612struct _vg_pthread_fastlock
613{
614 long int __vg_status; /* "Free" or "taken" or head of waiting list */
615 int __vg_spinlock; /* Used by compare_and_swap emulation. Also,
616 adaptive SMP lock stores spin count here. */
617};
618
619typedef struct
620{
621 int __vg_m_reserved; /* Reserved for future use */
622 int __vg_m_count; /* Depth of recursive locking */
623 /*_pthread_descr*/ void* __vg_m_owner; /* Owner thread (if recursive or errcheck) */
624 int __vg_m_kind; /* Mutex kind: fast, recursive or errcheck */
625 struct _vg_pthread_fastlock __vg_m_lock; /* Underlying fast lock */
626} vg_pthread_mutex_t;
627
628typedef struct
629{
630 struct _vg_pthread_fastlock __vg_c_lock; /* Protect against concurrent access */
nethercotedffad082004-02-28 23:32:11 +0000631 /*_pthread_descr*/ void* __vg_c_waiting; /* Threads waiting on this condition */
632
633 // Nb: the following padding removed because it was missing from an
634 // earlier glibc, so the size test in the CONVERT macro was failing.
635 // --njn
636
nethercote1f0173b2004-02-28 15:40:36 +0000637 // Padding ensures the size is 48 bytes
nethercotedffad082004-02-28 23:32:11 +0000638 /*char __vg_padding[48 - sizeof(struct _vg_pthread_fastlock)
nethercote1f0173b2004-02-28 15:40:36 +0000639 - sizeof(void*) - sizeof(long long)];
nethercotedffad082004-02-28 23:32:11 +0000640 long long __vg_align;*/
nethercote1f0173b2004-02-28 15:40:36 +0000641} vg_pthread_cond_t;
642
643
644/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000645 Exports of vg_scheduler.c
646 ------------------------------------------------------------------ */
647
sewardj2e93c502002-04-12 11:12:52 +0000648typedef
jsgf855d93d2003-10-13 22:26:55 +0000649 enum ThreadStatus {
sewardj2e93c502002-04-12 11:12:52 +0000650 VgTs_Empty, /* this slot is not in use */
651 VgTs_Runnable, /* waiting to be scheduled */
652 VgTs_WaitJoiner, /* waiting for someone to do join on me */
653 VgTs_WaitJoinee, /* waiting for the thread I did join on */
sewardj2e93c502002-04-12 11:12:52 +0000654 VgTs_WaitMX, /* waiting on a mutex */
sewardj3b5d8862002-04-20 13:53:23 +0000655 VgTs_WaitCV, /* waiting on a condition variable */
jsgf855d93d2003-10-13 22:26:55 +0000656 VgTs_WaitSys, /* waiting for a syscall to complete */
657 VgTs_Sleeping, /* sleeping for a while */
sewardj2e93c502002-04-12 11:12:52 +0000658 }
659 ThreadStatus;
sewardj8ad94e12002-05-29 00:10:20 +0000660
thughes11975ff2004-06-12 12:58:22 +0000661typedef
662 enum CleanupType {
663 VgCt_None, /* this cleanup entry is not initialised */
664 VgCt_Function, /* an old-style function pointer cleanup */
665 VgCt_Longjmp /* a new-style longjmp based cleanup */
666 }
667 CleanupType;
668
thughesdaa34562004-06-27 12:48:53 +0000669/* Information on a thread's stack. */
670typedef
671 struct {
672 Addr base;
673 UInt size;
674 UInt guardsize;
675 }
676 StackInfo;
677
sewardj8ad94e12002-05-29 00:10:20 +0000678/* An entry in a threads's cleanup stack. */
679typedef
680 struct {
thughes11975ff2004-06-12 12:58:22 +0000681 CleanupType type;
682 union {
683 struct {
684 void (*fn)(void*);
685 void* arg;
686 } function;
687 struct {
thughesebed9982004-06-12 17:25:25 +0000688 void *ub;
thughes11975ff2004-06-12 12:58:22 +0000689 int ctype;
690 } longjmp;
691 } data;
sewardj8ad94e12002-05-29 00:10:20 +0000692 }
693 CleanupEntry;
sewardj2cb00342002-06-28 01:46:26 +0000694
695/* An entry in a thread's fork-handler stack. */
696typedef
697 struct {
698 void (*prepare)(void);
699 void (*parent)(void);
700 void (*child)(void);
701 }
702 ForkHandlerEntry;
703
jsgf855d93d2003-10-13 22:26:55 +0000704typedef struct ProxyLWP ProxyLWP;
sewardj2cb00342002-06-28 01:46:26 +0000705
njn72718642003-07-24 08:45:32 +0000706typedef
707 struct _ThreadState {
njn25e49d8e72002-09-23 09:36:25 +0000708 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
709 The thread identity is simply the index in vg_threads[].
710 ThreadId == 1 is the root thread and has the special property
711 that we don't try and allocate or deallocate its stack. For
712 convenience of generating error message, we also put the
713 ThreadId in this tid field, but be aware that it should
714 ALWAYS == the index in vg_threads[]. */
715 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000716
njn25e49d8e72002-09-23 09:36:25 +0000717 /* Current scheduling status.
sewardj5f07b662002-04-23 16:52:51 +0000718
njn25e49d8e72002-09-23 09:36:25 +0000719 Complications: whenever this is set to VgTs_WaitMX, you
720 should also set .m_edx to whatever the required return value
721 is for pthread_mutex_lock / pthread_cond_timedwait for when
722 the mutex finally gets unblocked. */
723 ThreadStatus status;
sewardj2e93c502002-04-12 11:12:52 +0000724
njn25e49d8e72002-09-23 09:36:25 +0000725 /* When .status == WaitMX, points to the mutex I am waiting for.
726 When .status == WaitCV, points to the mutex associated with
727 the condition variable indicated by the .associated_cv field.
728 In all other cases, should be NULL. */
nethercote1f0173b2004-02-28 15:40:36 +0000729 vg_pthread_mutex_t* associated_mx;
sewardj3b5d8862002-04-20 13:53:23 +0000730
njn25e49d8e72002-09-23 09:36:25 +0000731 /* When .status == WaitCV, points to the condition variable I am
732 waiting for. In all other cases, should be NULL. */
733 void* /*pthread_cond_t* */ associated_cv;
sewardj2e93c502002-04-12 11:12:52 +0000734
njn25e49d8e72002-09-23 09:36:25 +0000735 /* If VgTs_Sleeping, this is when we should wake up, measured in
njn6c846552003-09-16 07:41:43 +0000736 milliseconds as supplied by VG_(read_millisecond_timer).
sewardj2e93c502002-04-12 11:12:52 +0000737
njn25e49d8e72002-09-23 09:36:25 +0000738 If VgTs_WaitCV, this indicates the time at which
739 pthread_cond_timedwait should wake up. If == 0xFFFFFFFF,
740 this means infinitely far in the future, viz,
741 pthread_cond_wait. */
742 UInt awaken_at;
sewardj20917d82002-05-28 01:36:45 +0000743
njn25e49d8e72002-09-23 09:36:25 +0000744 /* If VgTs_WaitJoiner, return value, as generated by joinees. */
745 void* joinee_retval;
sewardj20917d82002-05-28 01:36:45 +0000746
njn25e49d8e72002-09-23 09:36:25 +0000747 /* If VgTs_WaitJoinee, place to copy the return value to, and
748 the identity of the thread we're waiting for. */
749 void** joiner_thread_return;
750 ThreadId joiner_jee_tid;
sewardj8ad94e12002-05-29 00:10:20 +0000751
jsgf855d93d2003-10-13 22:26:55 +0000752 /* If VgTs_WaitSys, this is the result of the pre-syscall check */
753 void *sys_pre_res;
754
755 /* If VgTs_WaitSys, this is the syscall we're currently running */
756 Int syscallno;
757
thughesbaa46e52004-07-29 17:44:23 +0000758 /* If VgTs_WaitSys, this is the syscall flags */
759 UInt sys_flags;
760
jsgf855d93d2003-10-13 22:26:55 +0000761 /* Details about this thread's proxy LWP */
762 ProxyLWP *proxy;
763
njn25e49d8e72002-09-23 09:36:25 +0000764 /* Whether or not detached. */
765 Bool detached;
sewardj20917d82002-05-28 01:36:45 +0000766
njn25e49d8e72002-09-23 09:36:25 +0000767 /* Cancelability state and type. */
768 Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
769 Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
770
771 /* Pointer to fn to call to do cancellation. Indicates whether
772 or not cancellation is pending. If NULL, not pending. Else
773 should be &thread_exit_wrapper(), indicating that
774 cancallation is pending. */
775 void (*cancel_pend)(void*);
sewardj2e93c502002-04-12 11:12:52 +0000776
njn25e49d8e72002-09-23 09:36:25 +0000777 /* The cleanup stack. */
778 Int custack_used;
779 CleanupEntry custack[VG_N_CLEANUPSTACK];
sewardj5f07b662002-04-23 16:52:51 +0000780
sewardj00a66b12002-10-12 16:42:35 +0000781 /* A pointer to the thread's-specific-data. This is handled almost
782 entirely from vg_libpthread.c. We just provide hooks to get and
783 set this ptr. This is either NULL, indicating the thread has
784 read/written none of its specifics so far, OR points to a
785 void*[VG_N_THREAD_KEYS], allocated and deallocated in
786 vg_libpthread.c. */
787 void** specifics_ptr;
sewardjb48e5002002-05-13 00:16:03 +0000788
njn25e49d8e72002-09-23 09:36:25 +0000789 /* This thread's blocked-signals mask. Semantics is that for a
790 signal to be delivered to this thread, the signal must not be
jsgf855d93d2003-10-13 22:26:55 +0000791 blocked by this signal mask. If more than one thread accepts a
792 signal, then it will be delivered to one at random. If all
793 threads block the signal, it will remain pending until either a
794 thread unblocks it or someone uses sigwaitsig/sigtimedwait.
795
796 sig_mask reflects what the client told us its signal mask should
797 be, but isn't necessarily the current signal mask of the proxy
798 LWP: it may have more signals blocked because of signal
799 handling, or it may be different because of sigsuspend.
800 */
njn25e49d8e72002-09-23 09:36:25 +0000801 vki_ksigset_t sig_mask;
sewardjb48e5002002-05-13 00:16:03 +0000802
fitzhardingef0dd7e12004-01-16 02:17:30 +0000803 /* Effective signal mask. This is the mask which currently
804 applies; it may be different from sig_mask while a signal
jsgf855d93d2003-10-13 22:26:55 +0000805 handler is running.
806 */
807 vki_ksigset_t eff_sig_mask;
sewardj2e93c502002-04-12 11:12:52 +0000808
thughes8abf3922004-10-16 10:59:49 +0000809 /* Signal queue. This is used when the kernel doesn't route
810 signals properly in order to remember the signal information
811 while we are routing the signal. It is a circular queue with
812 insertions performed at the head and removals at the tail.
813 */
814 vki_ksiginfo_t sigqueue[VG_N_SIGNALQUEUE];
815 Int sigqueue_head;
816 Int sigqueue_tail;
817
njn25e49d8e72002-09-23 09:36:25 +0000818 /* Stacks. When a thread slot is freed, we don't deallocate its
819 stack; we just leave it lying around for the next use of the
820 slot. If the next use of the slot requires a larger stack,
821 only then is the old one deallocated and a new one
822 allocated.
sewardj2e93c502002-04-12 11:12:52 +0000823
njn25e49d8e72002-09-23 09:36:25 +0000824 For the main thread (threadid == 0), this mechanism doesn't
825 apply. We don't know the size of the stack since we didn't
826 allocate it, and furthermore we never reallocate it. */
sewardj2e93c502002-04-12 11:12:52 +0000827
njn25e49d8e72002-09-23 09:36:25 +0000828 /* The allocated size of this thread's stack (permanently zero
829 if this is ThreadId == 0, since we didn't allocate its stack) */
830 UInt stack_size;
sewardj1e8cdc92002-04-18 11:37:52 +0000831
njn25e49d8e72002-09-23 09:36:25 +0000832 /* Address of the lowest word in this thread's stack. NULL means
833 not allocated yet.
834 */
835 Addr stack_base;
sewardj2e93c502002-04-12 11:12:52 +0000836
thughesdaa34562004-06-27 12:48:53 +0000837 /* The allocated size of this thread's stack's guard area (permanently
838 zero if this is ThreadId == 0, since we didn't allocate its stack) */
839 UInt stack_guard_size;
840
sewardj92a59562002-09-30 00:53:10 +0000841 /* Address of the highest legitimate word in this stack. This is
842 used for error messages only -- not critical for execution
843 correctness. Is is set for all stacks, specifically including
844 ThreadId == 0 (the main thread). */
njn25e49d8e72002-09-23 09:36:25 +0000845 Addr stack_highest_word;
846
fitzhardinge98c4dc02004-03-16 08:27:29 +0000847 /* Alternate signal stack */
848 vki_kstack_t altstack;
849
nethercotec06e2132004-09-03 13:45:29 +0000850 /* Architecture-specific thread state */
851 arch_thread_t arch;
njn72718642003-07-24 08:45:32 +0000852}
853ThreadState;
sewardj2e93c502002-04-12 11:12:52 +0000854
855
sewardj018f7622002-05-15 21:13:39 +0000856/* The thread table. */
857extern ThreadState VG_(threads)[VG_N_THREADS];
858
859/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000860extern Bool VG_(is_valid_tid) ( ThreadId tid );
861
njn72718642003-07-24 08:45:32 +0000862/* Determine if 'tid' is that of the current running thread (Nb: returns
863 False if no thread is currently running. */
864extern Bool VG_(is_running_thread)(ThreadId tid);
865
jsgf855d93d2003-10-13 22:26:55 +0000866/* Get the ThreadState for a particular thread */
867extern ThreadState *VG_(get_ThreadState)(ThreadId tid);
868
sewardj1e8cdc92002-04-18 11:37:52 +0000869/* Similarly ... */
870extern ThreadId VG_(get_current_tid) ( void );
871
sewardjccef2e62002-05-29 19:26:32 +0000872/* Nuke all threads except tid. */
873extern void VG_(nuke_all_threads_except) ( ThreadId me );
874
jsgf855d93d2003-10-13 22:26:55 +0000875/* Give a hint to the scheduler that it may be a good time to find a
876 new runnable thread. If prefer_sched != VG_INVALID_THREADID, then
877 try to schedule that thread.
878*/
879extern void VG_(need_resched) ( ThreadId prefer_sched );
880
sewardj2e93c502002-04-12 11:12:52 +0000881/* Return codes from the scheduler. */
882typedef
sewardj7e87e382002-05-03 19:09:05 +0000883 enum {
884 VgSrc_Deadlock, /* no runnable threads and no prospect of any
885 even if we wait for a long time */
886 VgSrc_ExitSyscall, /* client called exit(). This is the normal
887 route out. */
jsgf855d93d2003-10-13 22:26:55 +0000888 VgSrc_FatalSig /* Killed by the default action of a fatal
889 signal */
sewardj7e87e382002-05-03 19:09:05 +0000890 }
sewardj2e93c502002-04-12 11:12:52 +0000891 VgSchedReturnCode;
892
sewardj7e87e382002-05-03 19:09:05 +0000893
nethercote238a3c32004-08-09 13:13:31 +0000894// The scheduler. 'fatal_sigNo' is only set if VgSrc_FatalSig is returned.
895extern VgSchedReturnCode VG_(scheduler)
896 ( Int* exit_code, ThreadId* last_run_thread, Int* fatal_sigNo );
sewardj2e93c502002-04-12 11:12:52 +0000897
898extern void VG_(scheduler_init) ( void );
899
sewardj15a43e12002-04-17 19:35:12 +0000900extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000901
nethercote75d26242004-08-01 22:59:18 +0000902// Longjmp back to the scheduler and thus enter the sighandler immediately.
903extern void VG_(resume_scheduler) ( Int sigNo, vki_ksiginfo_t *info );
sewardj2e93c502002-04-12 11:12:52 +0000904
nethercote238a3c32004-08-09 13:13:31 +0000905// Longjmp, ending the scheduler, when a fatal signal occurs in the client.
906extern void VG_(scheduler_handle_fatal_signal)( Int sigNo );
907
sewardj2e93c502002-04-12 11:12:52 +0000908/* The red-zone size which we put at the bottom (highest address) of
909 thread stacks, for paranoia reasons. This can be arbitrary, and
910 doesn't really need to be set at compile time. */
nethercote6e4f9dc2004-07-30 23:36:37 +0000911#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB 16
sewardj2e93c502002-04-12 11:12:52 +0000912
nethercoteaff1c772004-09-13 16:11:09 +0000913// Write a value to a client's thread register, and shadow (if necessary).
914// Note that there are some further similar macros in the arch- and
915// platform-specific parts; these ones are the totally generic ones.
916#define SET_THREAD_REG( zztid, zzval, zzGETREG, zzREG, zzevent, zzargs... ) \
917 do { zzGETREG(VG_(threads)[zztid].arch) = (zzval); \
918 VG_TRACK( zzevent, zztid, zzREG, ##zzargs ); \
sewardj018f7622002-05-15 21:13:39 +0000919 } while (0)
920
njnd3040452003-05-19 15:04:06 +0000921#define SET_CLREQ_RETVAL(zztid, zzval) \
nethercoteaff1c772004-09-13 16:11:09 +0000922 SET_THREAD_REG(zztid, zzval, ARCH_CLREQ_RET, R_CLREQ_RET, \
923 post_reg_write_clientreq_return)
njnd3040452003-05-19 15:04:06 +0000924
925#define SET_CLCALL_RETVAL(zztid, zzval, f) \
nethercoteaff1c772004-09-13 16:11:09 +0000926 SET_THREAD_REG(zztid, zzval, ARCH_CLREQ_RET, R_CLREQ_RET, \
927 post_reg_write_clientcall_return, f)
njnd3040452003-05-19 15:04:06 +0000928
929#define SET_PTHREQ_ESP(zztid, zzval) \
nethercoteaff1c772004-09-13 16:11:09 +0000930 SET_THREAD_REG(zztid, zzval, ARCH_STACK_PTR, R_STACK_PTR, \
931 post_reg_write_pthread_return)
njnd3040452003-05-19 15:04:06 +0000932
933#define SET_PTHREQ_RETVAL(zztid, zzval) \
nethercoteaff1c772004-09-13 16:11:09 +0000934 SET_THREAD_REG(zztid, zzval, ARCH_PTHREQ_RET, R_PTHREQ_RET, \
935 post_reg_write_pthread_return)
sewardj018f7622002-05-15 21:13:39 +0000936
sewardj2e93c502002-04-12 11:12:52 +0000937
938/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000939 Exports of vg_signals.c
940 ------------------------------------------------------------------ */
941
jsgf855d93d2003-10-13 22:26:55 +0000942extern Bool VG_(do_signal_routing); /* whether scheduler LWP has to route signals */
943
944/* RT signal allocation */
945extern Int VG_(sig_rtmin);
946extern Int VG_(sig_rtmax);
947extern Int VG_(sig_alloc_rtsig) ( Int high );
948
sewardjde4a1d02002-03-22 01:27:54 +0000949extern void VG_(sigstartup_actions) ( void );
950
jsgf855d93d2003-10-13 22:26:55 +0000951extern void VG_(deliver_signal) ( ThreadId tid, const vki_ksiginfo_t *, Bool async );
sewardjde4a1d02002-03-22 01:27:54 +0000952extern void VG_(unblock_host_signal) ( Int sigNo );
sewardj018f7622002-05-15 21:13:39 +0000953
jsgf855d93d2003-10-13 22:26:55 +0000954extern Bool VG_(is_sig_ign) ( Int sigNo );
955
956/* Route pending signals from the scheduler LWP to the appropriate
957 thread LWP. */
958extern void VG_(route_signals) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000959
960/* Fake system calls for signal handling. */
sewardj2342c972002-05-22 23:34:20 +0000961extern void VG_(do__NR_sigaltstack) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000962extern void VG_(do__NR_sigaction) ( ThreadId tid );
nethercote85cdd342004-08-01 22:36:40 +0000963extern void VG_(do__NR_sigprocmask) ( ThreadId tid, Int how,
sewardj018f7622002-05-15 21:13:39 +0000964 vki_ksigset_t* set,
965 vki_ksigset_t* oldset );
nethercote85cdd342004-08-01 22:36:40 +0000966extern void VG_(do_pthread_sigmask_SCSS_upd) ( ThreadId tid, Int how,
sewardj018f7622002-05-15 21:13:39 +0000967 vki_ksigset_t* set,
968 vki_ksigset_t* oldset );
sewardjefbfcdf2002-06-19 17:35:45 +0000969
sewardj2e93c502002-04-12 11:12:52 +0000970/* Modify the current thread's state once we have detected it is
971 returning from a signal handler. */
nethercoteaff1c772004-09-13 16:11:09 +0000972extern Bool VG_(signal_returns) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +0000973
sewardj2e93c502002-04-12 11:12:52 +0000974/* Handy utilities to block/restore all host signals. */
975extern void VG_(block_all_host_signals)
976 ( /* OUT */ vki_ksigset_t* saved_mask );
sewardj018f7622002-05-15 21:13:39 +0000977extern void VG_(restore_all_host_signals)
sewardj2e93c502002-04-12 11:12:52 +0000978 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +0000979
jsgf855d93d2003-10-13 22:26:55 +0000980extern void VG_(kill_self)(Int sigNo);
981
fitzhardingef1beb252004-03-16 09:49:08 +0000982/* These function synthesize a fault, as if the running instruction
983 had had a fault. These functions do not return - they longjmp back
984 into the scheduler so the signal can be delivered. */
985extern void VG_(synth_fault) (ThreadId tid);
986extern void VG_(synth_fault_mapping)(ThreadId tid, Addr addr);
987extern void VG_(synth_fault_perms) (ThreadId tid, Addr addr);
988
nethercote759dda32004-08-07 18:16:56 +0000989extern void VG_(get_sigstack_bounds)( Addr* low, Addr* high );
fitzhardingef1beb252004-03-16 09:49:08 +0000990
sewardjde4a1d02002-03-22 01:27:54 +0000991/* ---------------------------------------------------------------------
992 Exports of vg_mylibc.c
993 ------------------------------------------------------------------ */
994
njne427a662002-10-02 11:08:25 +0000995#define vg_assert(expr) \
996 ((void) ((expr) ? 0 : \
997 (VG_(core_assert_fail) (VG__STRING(expr), \
998 __FILE__, __LINE__, \
999 __PRETTY_FUNCTION__), 0)))
1000__attribute__ ((__noreturn__))
daywalker3222e0a2003-09-18 01:39:50 +00001001extern void VG_(core_assert_fail) ( const Char* expr, const Char* file,
1002 Int line, const Char* fn );
njne427a662002-10-02 11:08:25 +00001003__attribute__ ((__noreturn__))
1004extern void VG_(core_panic) ( Char* str );
thughes5876d552004-09-26 18:44:06 +00001005__attribute__ ((__noreturn__))
1006extern void VG_(core_panic_at) ( Char* str, ExeContext *ec );
sewardjde4a1d02002-03-22 01:27:54 +00001007
nethercote05675c82004-08-04 10:37:49 +00001008/* Tools use VG_(strdup)() which doesn't expose ArenaId */
njn25e49d8e72002-09-23 09:36:25 +00001009extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
sewardjde4a1d02002-03-22 01:27:54 +00001010
njn25e49d8e72002-09-23 09:36:25 +00001011extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
jsgf855d93d2003-10-13 22:26:55 +00001012extern Int VG_(poll)( struct vki_pollfd *, UInt nfds, Int timeout);
sewardj2e93c502002-04-12 11:12:52 +00001013
fitzhardinge98abfc72003-12-16 02:05:15 +00001014/* system/mman.h */
nethercoteb4250ae2004-07-10 16:50:09 +00001015extern void* VG_(mmap)( void* start, UInt length, UInt prot, UInt flags,
1016 UInt sf_flags, UInt fd, UInt offset );
fitzhardinge98abfc72003-12-16 02:05:15 +00001017extern Int VG_(munmap)( void* start, Int length );
1018extern Int VG_(mprotect)( void *start, Int length, UInt prot );
1019
1020
jsgf855d93d2003-10-13 22:26:55 +00001021/* Move an fd into the Valgrind-safe range */
1022Int VG_(safe_fd)(Int oldfd);
1023
sewardj570f8902002-11-03 11:44:36 +00001024extern Int VG_(write_socket)( Int sd, void *msg, Int count );
sewardj73cf3bc2002-11-03 03:20:15 +00001025
1026/* --- Connecting over the network --- */
1027extern Int VG_(connect_via_socket)( UChar* str );
1028
fitzhardinge98abfc72003-12-16 02:05:15 +00001029/* Environment manipulations */
nethercote60a96c52004-08-03 13:08:31 +00001030extern Char **VG_(env_setenv) ( Char ***envp, const Char* varname,
1031 const Char *val );
1032extern void VG_(env_unsetenv) ( Char **env, const Char *varname );
1033extern void VG_(env_remove_valgrind_env_stuff) ( Char** env );
sewardj570f8902002-11-03 11:44:36 +00001034
1035/* ---------------------------------------------------------------------
1036 Exports of vg_message.c
1037 ------------------------------------------------------------------ */
1038
1039/* Low-level -- send bytes directly to the message sink. Do not
1040 use. */
1041extern void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes );
1042
nethercoted1da90e2004-08-03 17:26:39 +00001043// Functions for printing from code within Valgrind, but which runs on the
nethercotec91ce8d2004-08-09 11:15:10 +00001044// sim'd CPU. Defined here because needed for vg_libpthread.c,
1045// vg_replace_malloc.c, plus the rest of the core. The weak attribute
1046// ensures the multiple definitions are not a problem. They must be functions
1047// rather than macros so that va_list can be used.
1048
1049__attribute__((weak))
1050int
1051VALGRIND_INTERNAL_PRINTF(char *format, ...)
1052{
1053 unsigned int _qzz_res = 0;
1054 va_list vargs;
1055 va_start(vargs, format);
1056 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__INTERNAL_PRINTF,
1057 (unsigned int)format, (unsigned int)vargs, 0, 0);
1058 va_end(vargs);
1059 return _qzz_res;
1060}
1061
1062__attribute__((weak))
1063int
1064VALGRIND_INTERNAL_PRINTF_BACKTRACE(char *format, ...)
1065{
1066 unsigned int _qzz_res = 0;
1067 va_list vargs;
1068 va_start(vargs, format);
1069 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__INTERNAL_PRINTF_BACKTRACE,
1070 (unsigned int)format, (unsigned int)vargs, 0, 0);
1071 va_end(vargs);
1072 return _qzz_res;
1073}
1074
nethercoted1da90e2004-08-03 17:26:39 +00001075
sewardjde4a1d02002-03-22 01:27:54 +00001076/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001077 Exports of vg_demangle.c
1078 ------------------------------------------------------------------ */
1079
1080extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
1081
sewardjde4a1d02002-03-22 01:27:54 +00001082/* ---------------------------------------------------------------------
1083 Exports of vg_from_ucode.c
1084 ------------------------------------------------------------------ */
1085
sewardj22854b92002-11-30 14:00:47 +00001086extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes, UShort jumps[VG_MAX_JUMPS] );
sewardjde4a1d02002-03-22 01:27:54 +00001087
njn25e49d8e72002-09-23 09:36:25 +00001088extern void VG_(print_ccall_stats) ( void );
1089extern void VG_(print_UInstr_histogram) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001090
sewardj22854b92002-11-30 14:00:47 +00001091extern void VG_(unchain_jumpsite) ( Addr jumpsite );
1092extern Addr VG_(get_jmp_dest) ( Addr jumpsite );
sewardj22854b92002-11-30 14:00:47 +00001093
sewardjde4a1d02002-03-22 01:27:54 +00001094/* ---------------------------------------------------------------------
1095 Exports of vg_to_ucode.c
1096 ------------------------------------------------------------------ */
1097
fitzhardingec2dbbac2004-01-23 23:09:01 +00001098Bool VG_(cpu_has_feature)(UInt feat);
1099
sewardjde4a1d02002-03-22 01:27:54 +00001100extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
sewardjde4a1d02002-03-22 01:27:54 +00001101
1102/* ---------------------------------------------------------------------
1103 Exports of vg_translate.c
1104 ------------------------------------------------------------------ */
1105
njn810086f2002-11-14 12:42:47 +00001106/* Expandable arrays of uinstrs. */
1107struct _UCodeBlock {
sewardj22854b92002-11-30 14:00:47 +00001108 Addr orig_eip;
njn810086f2002-11-14 12:42:47 +00001109 Int used;
1110 Int size;
1111 UInstr* instrs;
1112 Int nextTemp;
1113};
1114
nethercote4d714382004-10-13 09:47:24 +00001115extern Bool VG_(translate) ( ThreadId tid, Addr orig_addr, Bool debugging );
sewardjde4a1d02002-03-22 01:27:54 +00001116
nethercote885dd912004-08-03 23:14:00 +00001117extern void VG_(sanity_check_UInstr) ( UInt n, UInstr* u );
sewardjde4a1d02002-03-22 01:27:54 +00001118
nethercotebee3fd92004-08-02 15:17:43 +00001119extern void VG_(print_reg_alloc_stats) ( void );
sewardjb5ff83e2002-12-01 19:40:49 +00001120
sewardjde4a1d02002-03-22 01:27:54 +00001121/* ---------------------------------------------------------------------
1122 Exports of vg_execontext.c.
1123 ------------------------------------------------------------------ */
1124
1125/* Records the PC and a bit of the call chain. The first 4 %eip
1126 values are used in comparisons do remove duplicate errors, and for
1127 comparing against suppression specifications. The rest are purely
1128 informational (but often important). */
1129
njn25e49d8e72002-09-23 09:36:25 +00001130struct _ExeContext {
1131 struct _ExeContext * next;
1132 /* Variable-length array. The size is VG_(clo_backtrace_size); at
nethercote86c5dcb2004-09-05 21:32:37 +00001133 least 1, at most VG_DEEPEST_BACKTRACE. [0] is the current IP,
njn25e49d8e72002-09-23 09:36:25 +00001134 [1] is its caller, [2] is the caller of [1], etc. */
nethercote86c5dcb2004-09-05 21:32:37 +00001135 Addr ips[0];
njn25e49d8e72002-09-23 09:36:25 +00001136};
sewardjde4a1d02002-03-22 01:27:54 +00001137
1138
sewardjde4a1d02002-03-22 01:27:54 +00001139/* Print stats (informational only). */
nethercote3a42fb82004-08-03 18:08:50 +00001140extern void VG_(print_ExeContext_stats) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001141
njn25e49d8e72002-09-23 09:36:25 +00001142/* Like VG_(get_ExeContext), but with a slightly different type */
nethercote3d4a0b12004-09-06 15:14:46 +00001143extern ExeContext* VG_(get_ExeContext2) ( Addr ip, Addr fp,
1144 Addr fp_min, Addr fp_max );
sewardjde4a1d02002-03-22 01:27:54 +00001145
1146
1147/* ---------------------------------------------------------------------
1148 Exports of vg_errcontext.c.
1149 ------------------------------------------------------------------ */
1150
njn25e49d8e72002-09-23 09:36:25 +00001151extern void VG_(load_suppressions) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001152
njn25e49d8e72002-09-23 09:36:25 +00001153extern void VG_(record_pthread_error) ( ThreadId tid, Char* msg );
sewardjde4a1d02002-03-22 01:27:54 +00001154
njn25e49d8e72002-09-23 09:36:25 +00001155extern void VG_(show_all_errors) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001156
nethercotef2b11482004-08-02 12:36:01 +00001157extern Bool VG_(is_action_requested) ( Char* action, Bool* clo );
njn43c799e2003-04-08 00:08:52 +00001158
nethercotef2b11482004-08-02 12:36:01 +00001159extern UInt VG_(get_n_errs_found) ( void );
njn47363ab2003-04-21 13:24:40 +00001160
sewardjde4a1d02002-03-22 01:27:54 +00001161/* ---------------------------------------------------------------------
1162 Exports of vg_procselfmaps.c
1163 ------------------------------------------------------------------ */
1164
njnfa1016e2003-09-25 17:54:11 +00001165/* Reads /proc/self/maps into a static buffer which can be parsed by
1166 VG_(parse_procselfmaps)(). */
1167extern void VG_(read_procselfmaps) ( void );
njn3e884182003-04-15 13:03:23 +00001168
1169/* Parses /proc/self/maps, calling `record_mapping' for each entry. If
1170 `read_from_file' is True, /proc/self/maps is read directly, otherwise
1171 it's read from the buffer filled by VG_(read_procselfmaps_contents)(). */
sewardjde4a1d02002-03-22 01:27:54 +00001172extern
njnfa1016e2003-09-25 17:54:11 +00001173void VG_(parse_procselfmaps) (
fitzhardinge98abfc72003-12-16 02:05:15 +00001174 void (*record_mapping)( Addr addr, UInt len, Char rr, Char ww, Char xx,
nethercote85cdd342004-08-01 22:36:40 +00001175 UInt dev, UInt ino, ULong foff,
1176 const UChar *filename ) );
sewardjde4a1d02002-03-22 01:27:54 +00001177
1178
1179/* ---------------------------------------------------------------------
1180 Exports of vg_symtab2.c
1181 ------------------------------------------------------------------ */
1182
fitzhardinge98abfc72003-12-16 02:05:15 +00001183typedef struct _Segment Segment;
1184
1185extern Bool VG_(is_object_file) ( const void *hdr );
njnfa1016e2003-09-25 17:54:11 +00001186extern void VG_(mini_stack_dump) ( Addr eips[], UInt n_eips );
fitzhardinge98abfc72003-12-16 02:05:15 +00001187extern SegInfo * VG_(read_seg_symbols) ( Segment *seg );
fitzhardinge98abfc72003-12-16 02:05:15 +00001188extern void VG_(symtab_incref) ( SegInfo * );
1189extern void VG_(symtab_decref) ( SegInfo *, Addr a, UInt len );
sewardjde4a1d02002-03-22 01:27:54 +00001190
njn25e49d8e72002-09-23 09:36:25 +00001191extern Bool VG_(get_fnname_nodemangle)( Addr a, Char* fnname, Int n_fnname );
sewardj25c7c3a2003-07-10 00:17:58 +00001192
fitzhardinge98abfc72003-12-16 02:05:15 +00001193/* Set up some default redirects */
1194extern void VG_(setup_code_redirect_table) ( void );
sewardj25c7c3a2003-07-10 00:17:58 +00001195
fitzhardinge98abfc72003-12-16 02:05:15 +00001196/* Redirection machinery */
nethercote85cdd342004-08-01 22:36:40 +00001197extern Addr VG_(code_redirect) ( Addr orig );
sewardjde4a1d02002-03-22 01:27:54 +00001198
1199/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001200 Exports of vg_main.c
1201 ------------------------------------------------------------------ */
1202
sewardjb91ae7f2003-04-29 23:50:00 +00001203/* Is this a SSE/SSE2-capable CPU? If so, we had better save/restore
1204 the SSE state all over the place. This is set up very early, in
nethercote1d447092004-02-01 17:29:59 +00001205 main(). We have to determine it early since we can't even
sewardjb91ae7f2003-04-29 23:50:00 +00001206 correctly snapshot the startup machine state without it. */
1207extern Bool VG_(have_ssestate);
1208
sewardj73cf3bc2002-11-03 03:20:15 +00001209/* Tell the logging mechanism whether we are logging to a file
1210 descriptor or a socket descriptor. */
1211extern Bool VG_(logging_to_filedes);
1212
njn25e49d8e72002-09-23 09:36:25 +00001213/* Sanity checks which may be done at any time. The scheduler decides when. */
nethercote885dd912004-08-03 23:14:00 +00001214extern void VG_(sanity_check_general) ( Bool force_expensive );
njn25e49d8e72002-09-23 09:36:25 +00001215
fitzhardinge98abfc72003-12-16 02:05:15 +00001216/* Address space */
1217extern Addr VG_(client_base); /* client address space limits */
1218extern Addr VG_(client_end);
1219extern Addr VG_(client_mapbase); /* base of mappings */
1220extern Addr VG_(clstk_base); /* client stack range */
1221extern Addr VG_(clstk_end);
fitzhardinge92360792003-12-24 10:11:11 +00001222extern Addr VG_(client_trampoline_code);
1223
fitzhardinge98abfc72003-12-16 02:05:15 +00001224extern Addr VG_(brk_base); /* start of brk */
1225extern Addr VG_(brk_limit); /* current brk */
nethercote996901a2004-08-03 13:29:09 +00001226extern Addr VG_(shadow_base); /* tool's shadow memory */
fitzhardinge98abfc72003-12-16 02:05:15 +00001227extern Addr VG_(shadow_end);
1228extern Addr VG_(valgrind_base); /* valgrind's address range */
nethercote820bd8c2004-09-07 23:04:49 +00001229extern Addr VG_(valgrind_last); // Nb: last byte, rather than one past the end
fitzhardinge98abfc72003-12-16 02:05:15 +00001230
fitzhardingeb50068f2004-02-24 23:42:55 +00001231extern vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
thughesc37184f2004-09-11 14:16:57 +00001232extern vki_rlimit VG_(client_rlimit_stack); /* client's original rlimit stack */
fitzhardingeb50068f2004-02-24 23:42:55 +00001233
fitzhardingea49f9b52003-12-16 22:26:45 +00001234/* client executable file descriptor */
1235extern Int VG_(clexecfd);
fitzhardinge98abfc72003-12-16 02:05:15 +00001236
nethercotef6a1d502004-08-09 12:21:57 +00001237// Help set up the child used when doing execve() with --trace-children=yes
1238Char* VG_(build_child_VALGRINDCLO) ( Char* exename );
1239Char* VG_(build_child_exename) ( void );
1240
njn9b007f62003-04-07 14:40:25 +00001241/* Determine if %esp adjustment must be noted */
njnf4ce3d32003-02-10 10:17:26 +00001242extern Bool VG_(need_to_handle_esp_assignment) ( void );
1243
sewardjde4a1d02002-03-22 01:27:54 +00001244/* Called when some unhandleable client behaviour is detected.
1245 Prints a msg and aborts. */
njn25e49d8e72002-09-23 09:36:25 +00001246extern void VG_(unimplemented) ( Char* msg )
1247 __attribute__((__noreturn__));
sewardjde4a1d02002-03-22 01:27:54 +00001248
nethercote04d0fbc2004-01-26 16:48:06 +00001249/* Something of a function looking for a home ... start up debugger. */
1250extern void VG_(start_debugger) ( Int tid );
sewardjde4a1d02002-03-22 01:27:54 +00001251
sewardjde4a1d02002-03-22 01:27:54 +00001252/* Counts downwards in vg_run_innerloop. */
1253extern UInt VG_(dispatch_ctr);
1254
sewardjde4a1d02002-03-22 01:27:54 +00001255/* --- Counters, for informational purposes only. --- */
1256
nethercote92e7b7f2004-08-07 17:52:25 +00001257// These counters must be declared here because they're maintained by
1258// vg_dispatch.S.
1259extern UInt VG_(bb_enchain_count); // Counts of chain operations done
1260extern UInt VG_(bb_dechain_count); // Counts of unchain operations done
1261extern UInt VG_(unchained_jumps_done); // Number of unchained jumps performed
sewardj22854b92002-11-30 14:00:47 +00001262
nethercote844e7122004-08-02 15:27:22 +00001263extern void VG_(print_scheduler_stats) ( void );
sewardj2e93c502002-04-12 11:12:52 +00001264
nethercote2e05c332004-09-06 16:43:37 +00001265extern Int VG_(alloc_BaB)( Int ); // Allocate slots in baseBlock
1266extern void VG_(align_BaB)( UInt ); // Align baseBlock offset
1267extern Int VG_(alloc_BaB_1_set)( Addr ); // Allocate & init baseBlock slot
1268
sewardjde4a1d02002-03-22 01:27:54 +00001269/* ---------------------------------------------------------------------
1270 Exports of vg_memory.c
1271 ------------------------------------------------------------------ */
1272
fitzhardinge98abfc72003-12-16 02:05:15 +00001273/* A Segment is mapped piece of client memory. This covers all kinds
1274 of mapped memory (exe, brk, mmap, .so, shm, stack, etc)
1275
1276 We try to encode everything we know about a particular segment here.
1277*/
nethercote85cdd342004-08-01 22:36:40 +00001278#define SF_FIXED (1 << 0) // client asked for MAP_FIXED
1279#define SF_SHARED (1 << 1) // shared
1280#define SF_SHM (1 << 2) // SYSV SHM (also SF_SHARED)
1281#define SF_MMAP (1 << 3) // mmap memory
1282#define SF_FILE (1 << 4) // mapping is backed by a file
1283#define SF_STACK (1 << 5) // is a stack
1284#define SF_GROWDOWN (1 << 6) // segment grows down
1285#define SF_GROWUP (1 << 7) // segment grows up
1286#define SF_EXEC (1 << 8) // segment created by exec
1287#define SF_DYNLIB (1 << 9) // mapped from dynamic library
1288#define SF_NOSYMS (1 << 10) // don't load syms, even if present
1289#define SF_BRK (1 << 11) // brk segment
1290#define SF_CORE (1 << 12) // allocated by core on behalf of the client
1291#define SF_VALGRIND (1 << 13) // a valgrind-internal mapping - not in client
1292#define SF_CODE (1 << 14) // segment contains cached code
fitzhardinge98abfc72003-12-16 02:05:15 +00001293
1294struct _Segment {
1295 UInt prot; /* VKI_PROT_* */
1296 UInt flags; /* SF_* */
1297
1298 Addr addr; /* mapped addr (page aligned) */
1299 UInt len; /* size of mapping (page aligned) */
1300
1301 /* These are valid if (flags & SF_FILE) */
1302 ULong offset; /* file offset */
1303 const Char *filename; /* filename (NULL if unknown) */
1304 UInt dev; /* device */
1305 UInt ino; /* inode */
1306
1307 SegInfo *symtab; /* symbol table */
1308};
1309
1310/* segment mapped from a file descriptor */
1311extern void VG_(map_fd_segment) (Addr addr, UInt len, UInt prot, UInt flags,
1312 Int fd, ULong off, const Char *filename);
1313
1314/* segment mapped from a file */
1315extern void VG_(map_file_segment)(Addr addr, UInt len, UInt prot, UInt flags,
1316 UInt dev, UInt ino, ULong off, const Char *filename);
1317
1318/* simple segment */
1319extern void VG_(map_segment) (Addr addr, UInt len, UInt prot, UInt flags);
1320
1321extern void VG_(unmap_range) (Addr addr, UInt len);
1322extern void VG_(mprotect_range)(Addr addr, UInt len, UInt prot);
1323extern Addr VG_(find_map_space)(Addr base, UInt len, Bool for_client);
1324
1325extern Segment *VG_(find_segment)(Addr a);
fitzhardinged65dcad2004-03-13 02:06:58 +00001326extern Segment *VG_(first_segment)(void);
fitzhardinge98abfc72003-12-16 02:05:15 +00001327extern Segment *VG_(next_segment)(Segment *);
1328
1329extern Bool VG_(seg_contains)(const Segment *s, Addr ptr, UInt size);
1330extern Bool VG_(seg_overlaps)(const Segment *s, Addr ptr, UInt size);
1331
thughes9aaebc32004-07-15 23:13:37 +00001332extern void VG_(pad_address_space)(void);
1333extern void VG_(unpad_address_space)(void);
1334
nethercoteeec46302004-08-23 15:06:23 +00001335extern REGPARM(1)
njnfa1016e2003-09-25 17:54:11 +00001336 void VG_(unknown_esp_update) ( Addr new_ESP );
sewardjde4a1d02002-03-22 01:27:54 +00001337
jsgf855d93d2003-10-13 22:26:55 +00001338/* ---------------------------------------------------------------------
1339 Exports of vg_proxylwp.c
1340 ------------------------------------------------------------------ */
1341
nethercote24e0d442004-10-18 17:36:40 +00001342enum PXState
1343{
1344 PXS_BAD = -1,
1345 PXS_WaitReq, /* waiting for a request */
1346 PXS_RunSyscall, /* running a syscall */
1347 PXS_IntReply, /* request interrupted - need to send reply */
1348 PXS_SysDone, /* small window between syscall
1349 complete and results written out */
1350 PXS_SigACK, /* waiting for a signal ACK */
1351};
1352
1353
jsgf855d93d2003-10-13 22:26:55 +00001354/* Issue a syscall for thread tid */
1355extern Int VG_(sys_issue)(int tid);
1356
1357extern void VG_(proxy_init) ( void );
1358extern void VG_(proxy_create) ( ThreadId tid );
1359extern void VG_(proxy_delete) ( ThreadId tid, Bool force );
1360extern void VG_(proxy_results) ( void );
1361extern void VG_(proxy_sendsig) ( ThreadId tid, Int signo );
1362extern void VG_(proxy_setsigmask)(ThreadId tid);
1363extern void VG_(proxy_sigack) ( ThreadId tid, const vki_ksigset_t *);
1364extern void VG_(proxy_abort_syscall) ( ThreadId tid );
1365extern void VG_(proxy_waitsig) ( void );
fitzhardinge31ba9052004-01-16 02:15:23 +00001366extern void VG_(proxy_wait_sys) (ThreadId tid, Bool restart);
jsgf855d93d2003-10-13 22:26:55 +00001367
nethercote85cdd342004-08-01 22:36:40 +00001368extern void VG_(proxy_shutdown) ( void ); // shut down the syscall workers
1369extern Int VG_(proxy_resfd) ( void ); // FD something can select on to know
1370 // a syscall finished
jsgf855d93d2003-10-13 22:26:55 +00001371
1372/* Sanity-check the whole proxy-LWP machinery */
nethercote885dd912004-08-03 23:14:00 +00001373void VG_(sanity_check_proxy)(void);
jsgf855d93d2003-10-13 22:26:55 +00001374
1375/* Send a signal from a thread's proxy to the thread. This longjmps
1376 back into the proxy's main loop, so it doesn't return. */
1377__attribute__ ((__noreturn__))
1378extern void VG_(proxy_handlesig)( const vki_ksiginfo_t *siginfo,
nethercotee5a3e2b2004-10-13 14:42:57 +00001379 Addr ip, Int sysnum );
jsgf855d93d2003-10-13 22:26:55 +00001380
sewardjde4a1d02002-03-22 01:27:54 +00001381/* ---------------------------------------------------------------------
njn25e49d8e72002-09-23 09:36:25 +00001382 Exports of vg_syscalls.c
sewardjde4a1d02002-03-22 01:27:54 +00001383 ------------------------------------------------------------------ */
1384
fitzhardinge98abfc72003-12-16 02:05:15 +00001385extern Char *VG_(resolve_filename)(Int fd);
njn25e49d8e72002-09-23 09:36:25 +00001386
jsgf855d93d2003-10-13 22:26:55 +00001387extern Bool VG_(pre_syscall) ( ThreadId tid );
fitzhardinge31ba9052004-01-16 02:15:23 +00001388extern void VG_(post_syscall)( ThreadId tid, Bool restart );
sewardjde4a1d02002-03-22 01:27:54 +00001389
1390extern Bool VG_(is_kerror) ( Int res );
1391
jsgf855d93d2003-10-13 22:26:55 +00001392/* Internal atfork handlers */
1393typedef void (*vg_atfork_t)(ThreadId);
1394extern void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, vg_atfork_t child);
sewardjde4a1d02002-03-22 01:27:54 +00001395
rjwalshf5f536f2003-11-17 17:45:00 +00001396/* fd leakage calls. */
1397extern void VG_(init_preopened_fds) ( void );
nethercote3a42fb82004-08-03 18:08:50 +00001398extern void VG_(show_open_fds) ( void );
rjwalshf5f536f2003-11-17 17:45:00 +00001399
sewardjde4a1d02002-03-22 01:27:54 +00001400/* ---------------------------------------------------------------------
1401 Exports of vg_transtab.c
1402 ------------------------------------------------------------------ */
1403
njn25e49d8e72002-09-23 09:36:25 +00001404/* The fast-cache for tt-lookup. */
1405extern Addr VG_(tt_fast)[VG_TT_FAST_SIZE];
1406
nethercote92e7b7f2004-08-07 17:52:25 +00001407extern void VG_(init_tt_tc) ( void );
sewardjc0d8f682002-11-30 00:49:43 +00001408extern void VG_(add_to_trans_tab) ( Addr orig_addr, Int orig_size,
sewardj22854b92002-11-30 14:00:47 +00001409 Addr trans_addr, Int trans_size,
1410 UShort jumps[VG_MAX_JUMPS]);
nethercote92e7b7f2004-08-07 17:52:25 +00001411extern Addr VG_(search_transtab) ( Addr original_addr );
sewardj6c3769f2002-11-29 01:02:45 +00001412
nethercote92e7b7f2004-08-07 17:52:25 +00001413extern void VG_(invalidate_translations) ( Addr start, UInt range,
1414 Bool unchain_blocks );
sewardjde4a1d02002-03-22 01:27:54 +00001415
nethercote92e7b7f2004-08-07 17:52:25 +00001416extern void VG_(sanity_check_tt_tc) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001417
nethercote92e7b7f2004-08-07 17:52:25 +00001418extern void VG_(print_tt_tc_stats) ( void );
1419
1420extern Int VG_(get_bbs_translated) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001421
sewardjde4a1d02002-03-22 01:27:54 +00001422/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001423 Exports of vg_syscall.S
1424 ------------------------------------------------------------------ */
1425
jsgf855d93d2003-10-13 22:26:55 +00001426extern Int VG_(do_syscall) ( UInt, ... );
1427extern Int VG_(clone) ( Int (*fn)(void *), void *stack, Int flags, void *arg,
1428 Int *child_tid, Int *parent_tid);
fitzhardinge4f10ada2004-06-03 10:00:42 +00001429extern void VG_(sigreturn)(void);
sewardjde4a1d02002-03-22 01:27:54 +00001430
1431/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001432 Exports of vg_dispatch.S
1433 ------------------------------------------------------------------ */
1434
sewardj2e93c502002-04-12 11:12:52 +00001435/* Run a thread for a (very short) while, until some event happens
1436 which means we need to defer to the scheduler. */
1437extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001438
sewardj22854b92002-11-30 14:00:47 +00001439/* The patching routing called when a BB wants to chain itself to
1440 another. */
1441extern UInt VG_(patch_me);
sewardjde4a1d02002-03-22 01:27:54 +00001442
1443/* ---------------------------------------------------------------------
1444 Exports of vg_helpers.S
1445 ------------------------------------------------------------------ */
1446
sewardj51096432002-12-14 23:59:09 +00001447extern void VG_(helper_undefined_instruction);
1448
fitzhardinge92360792003-12-24 10:11:11 +00001449/* Information about trampoline code (for signal return and syscalls) */
1450extern const Char VG_(trampoline_code_start);
1451extern const Int VG_(trampoline_code_length);
1452extern const Int VG_(tramp_sigreturn_offset);
1453extern const Int VG_(tramp_syscall_offset);
sewardj20917d82002-05-28 01:36:45 +00001454
njn4f9c9342002-04-29 16:03:24 +00001455/* ---------------------------------------------------------------------
nethercote996901a2004-08-03 13:29:09 +00001456 Things relating to the used tool
njn4f9c9342002-04-29 16:03:24 +00001457 ------------------------------------------------------------------ */
1458
fitzhardinge98abfc72003-12-16 02:05:15 +00001459#define VG_TRACK(fn, args...) \
1460 do { \
1461 if (VG_(defined_##fn)()) \
1462 SK_(fn)(args); \
1463 } while(0)
sewardj18d75132002-05-16 11:06:21 +00001464
fitzhardinge98abfc72003-12-16 02:05:15 +00001465__attribute__ ((noreturn))
1466extern void VG_(missing_tool_func) ( const Char* fn );
sewardj18d75132002-05-16 11:06:21 +00001467
sewardjde4a1d02002-03-22 01:27:54 +00001468/* ---------------------------------------------------------------------
nethercote6a27d832004-09-07 10:17:02 +00001469 The baseBlock -- arch-neutral bits
sewardjde4a1d02002-03-22 01:27:54 +00001470 ------------------------------------------------------------------ */
1471
nethercote2e05c332004-09-06 16:43:37 +00001472#define INVALID_OFFSET (-1)
sewardjde4a1d02002-03-22 01:27:54 +00001473
1474/* An array of words. In generated code, %ebp always points to the
1475 start of this array. Useful stuff, like the simulated CPU state,
1476 and the addresses of helper functions, can then be found by
1477 indexing off %ebp. The following declares variables which, at
1478 startup time, are given values denoting offsets into baseBlock.
1479 These offsets are in *words* from the start of baseBlock. */
1480
sewardjb91ae7f2003-04-29 23:50:00 +00001481#define VG_BASEBLOCK_WORDS 400
sewardjde4a1d02002-03-22 01:27:54 +00001482
1483extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1484
nethercotec06e2132004-09-03 13:45:29 +00001485// ---------------------------------------------------------------------
1486// Architecture-specific things defined in eg. x86/*.c
1487// ---------------------------------------------------------------------
1488
nethercotec009ebe2004-09-13 11:05:11 +00001489// For setting up the baseBlock
nethercote2e05c332004-09-06 16:43:37 +00001490extern void VGA_(init_low_baseBlock) ( Addr client_eip, Addr esp_at_startup );
1491extern void VGA_(init_high_baseBlock) ( Addr client_eip, Addr esp_at_startup );
1492
nethercotec009ebe2004-09-13 11:05:11 +00001493// Register state moving
nethercotec06e2132004-09-03 13:45:29 +00001494extern void VGA_(load_state) ( arch_thread_t*, ThreadId tid );
1495extern void VGA_(save_state) ( arch_thread_t*, ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001496
nethercotec009ebe2004-09-13 11:05:11 +00001497// Thread stuff
nethercote7587d472004-09-11 23:51:50 +00001498extern void VGA_(clear_thread) ( arch_thread_t* );
1499extern void VGA_(init_thread) ( arch_thread_t* );
nethercotef9b59412004-09-10 15:33:32 +00001500extern void VGA_(cleanup_thread) ( arch_thread_t* );
1501extern void VGA_(setup_child) ( arch_thread_t*, arch_thread_t* );
1502
nethercote6b9c8472004-09-13 13:16:40 +00001503extern void VGA_(set_arg_and_bogus_ret) ( ThreadId tid, UWord arg, Addr ret );
1504extern void VGA_(thread_initial_stack) ( ThreadId tid, UWord arg, Addr ret );
1505
nethercotec009ebe2004-09-13 11:05:11 +00001506// Symtab stuff
nethercotecd656042004-09-11 23:48:22 +00001507extern UInt* VGA_(reg_addr_from_BB) ( Int reg );
1508extern UInt* VGA_(reg_addr_from_tst) ( Int reg, arch_thread_t* );
1509
nethercotefedd8102004-09-13 15:19:34 +00001510// Pointercheck
1511extern Bool VGA_(setup_pointercheck) ( void );
1512
1513// For attaching the debugger
1514extern Int VGA_(ptrace_setregs_from_BB) ( Int pid );
1515extern Int VGA_(ptrace_setregs_from_tst) ( Int pid, arch_thread_t* arch );
1516
1517// Making coredumps
1518extern void VGA_(fill_elfregs_from_BB) ( struct user_regs_struct* regs );
1519extern void VGA_(fill_elfregs_from_tst) ( struct user_regs_struct* regs,
1520 const arch_thread_t* arch );
1521extern void VGA_(fill_elffpregs_from_BB) ( elf_fpregset_t* fpu );
1522extern void VGA_(fill_elffpregs_from_tst) ( elf_fpregset_t* fpu,
1523 const arch_thread_t* arch );
1524extern void VGA_(fill_elffpxregs_from_BB) ( elf_fpxregset_t* xfpu );
1525extern void VGA_(fill_elffpxregs_from_tst) ( elf_fpxregset_t* xfpu,
1526 const arch_thread_t* arch );
1527
nethercote6eec4602004-09-13 14:15:36 +00001528// Signal stuff
1529extern void VGA_(push_signal_frame) ( ThreadId tid, Addr esp_top_of_frame,
1530 const vki_ksiginfo_t *siginfo,
1531 void *handler, UInt flags,
1532 const vki_ksigset_t *mask);
1533extern Int VGA_(pop_signal_frame) ( ThreadId tid );
1534
nethercote9b3c7652004-10-19 13:18:00 +00001535// libpthread stuff
1536typedef struct arch_thread_aux arch_thread_aux_t;
1537
1538void VGA_(thread_create) ( arch_thread_aux_t *aux );
1539void VGA_(thread_wrapper)( arch_thread_aux_t *aux );
1540void VGA_(thread_exit) ( void );
1541
1542Bool VGA_(has_tls) ( void );
1543
1544#define MY__STRING(__str) #__str
1545
1546// Assertion to use in code running on the simulated CPU.
1547#define my_assert(expr) \
1548 ((void) ((expr) ? 0 : \
1549 (VG_(user_assert_fail) (MY__STRING(expr), \
1550 __FILE__, __LINE__, \
1551 __PRETTY_FUNCTION__), 0)))
1552
1553extern void VG_(user_assert_fail) ( const Char* expr, const Char* file,
1554 Int line, const Char* fn );
1555
1556
nethercote41c75da2004-10-18 15:34:14 +00001557// ---------------------------------------------------------------------
1558// Platform-specific things defined in eg. x86/*.c
1559// ---------------------------------------------------------------------
nethercote775508a2004-09-07 22:38:23 +00001560
nethercote24e0d442004-10-18 17:36:40 +00001561extern const Addr vga_sys_before, vga_sys_restarted,
1562 vga_sys_after, vga_sys_done;
1563
nethercote6eb106b2004-10-18 17:41:36 +00001564extern void VGA_(restart_syscall)(arch_thread_t* arch);
nethercote24e0d442004-10-18 17:36:40 +00001565
nethercote6eb106b2004-10-18 17:41:36 +00001566extern void VGA_(thread_syscall)(Int syscallno, arch_thread_t* arch,
nethercote24e0d442004-10-18 17:36:40 +00001567 enum PXState* state, enum PXState poststate);
sewardj3b2736a2002-03-24 12:18:35 +00001568
1569/* ---------------------------------------------------------------------
1570 Finally - autoconf-generated settings
1571 ------------------------------------------------------------------ */
1572
1573#include "config.h"
1574
nethercotec06e2132004-09-03 13:45:29 +00001575#endif /* ndef __CORE_H */
1576
sewardjde4a1d02002-03-22 01:27:54 +00001577/*--------------------------------------------------------------------*/
nethercote109d0df2004-09-02 08:10:13 +00001578/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001579/*--------------------------------------------------------------------*/