blob: 656e81e0ced7d821162c8206a7c445e8dadf21ac [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_... */
84#include "vg_unistd.h"
85
nethercote13343132004-09-02 15:49:09 +000086#include "core_asm.h" // asm stuff
87#include "tool.h" // tool stuff
nethercotec06e2132004-09-03 13:45:29 +000088#include "core_arch.h" // arch-specific stuff; eg. x86/arch.h
sewardjde4a1d02002-03-22 01:27:54 +000089
fitzhardinge39de4b42003-10-31 07:12:21 +000090#include "valgrind.h"
sewardjde4a1d02002-03-22 01:27:54 +000091
fitzhardinge98abfc72003-12-16 02:05:15 +000092#undef SK_
93#define SK_(x) vgSkinInternal_##x
94
nethercote7be47252004-09-02 16:02:58 +000095
96/* ---------------------------------------------------------------------
97 Build options and table sizes. You should be able to change these
98 options or sizes, recompile, and still have a working system.
99 ------------------------------------------------------------------ */
100
sewardjde4a1d02002-03-22 01:27:54 +0000101/* Total number of spill slots available for allocation, if a TempReg
102 doesn't make it into a RealReg. Just bomb the entire system if
103 this value is too small; we don't expect it will ever get
104 particularly high. */
105#define VG_MAX_SPILLSLOTS 24
106
107
108/* Constants for the slow translation lookup cache. */
109#define VG_TRANSTAB_SLOW_BITS 11
110#define VG_TRANSTAB_SLOW_SIZE (1 << VG_TRANSTAB_SLOW_BITS)
111#define VG_TRANSTAB_SLOW_MASK ((VG_TRANSTAB_SLOW_SIZE) - 1)
112
113/* Size of a buffer used for creating messages. */
114#define M_VG_MSGBUF 10000
115
116/* Size of a smallish table used to read /proc/self/map entries. */
sewardjebc82332002-04-24 14:44:23 +0000117#define M_PROCMAP_BUF 50000
sewardjde4a1d02002-03-22 01:27:54 +0000118
119/* Max length of pathname to a .so/executable file. */
120#define M_VG_LIBNAMESTR 100
121
122/* Max length of a text fragment used to construct error messages. */
123#define M_VG_ERRTXT 512
124
125/* Max length of the string copied from env var VG_ARGS at startup. */
126#define M_VG_CMDLINE_STRLEN 1000
127
128/* Max number of options for Valgrind which we can handle. */
129#define M_VG_CMDLINE_OPTS 100
130
131/* After this many different unsuppressed errors have been observed,
132 be more conservative about collecting new ones. */
133#define M_VG_COLLECT_ERRORS_SLOWLY_AFTER 50
134
135/* After this many different unsuppressed errors have been observed,
136 stop collecting errors at all, and tell the user their program is
137 evidently a steaming pile of camel dung. */
sewardj1bebcbf2002-04-24 21:24:18 +0000138#define M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN 300
sewardjf2537be2002-04-24 21:03:47 +0000139
140/* After this many total errors have been observed, stop collecting
141 errors at all. Counterpart to M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN. */
sewardj1bebcbf2002-04-24 21:24:18 +0000142#define M_VG_COLLECT_NO_ERRORS_AFTER_FOUND 30000
sewardjde4a1d02002-03-22 01:27:54 +0000143
sewardjde4a1d02002-03-22 01:27:54 +0000144/* The maximum number of calls we're prepared to save in a
145 backtrace. */
146#define VG_DEEPEST_BACKTRACE 50
147
sewardjde4a1d02002-03-22 01:27:54 +0000148/* Number of lists in which we keep track of ExeContexts. Should be
149 prime. */
sewardj59fb25c2003-09-28 16:32:58 +0000150#define VG_N_EC_LISTS 4999 /* a prime number */
sewardjde4a1d02002-03-22 01:27:54 +0000151
sewardj2e93c502002-04-12 11:12:52 +0000152/* Defines the thread-scheduling timeslice, in terms of the number of
153 basic blocks we attempt to run each thread for. Smaller values
154 give finer interleaving but much increased scheduling overheads. */
sewardj4505b9e2002-05-28 11:27:31 +0000155#define VG_SCHEDULING_QUANTUM 50000
sewardj2e93c502002-04-12 11:12:52 +0000156
fitzhardingef0046f22003-12-18 02:39:22 +0000157/* Number of file descriptors that Valgrind tries to reserve for
158 it's own use - two per thread plues a small number of extras. */
159#define VG_N_RESERVED_FDS (VG_N_THREADS*2 + 4)
sewardj2e93c502002-04-12 11:12:52 +0000160
sewardjbf290b92002-05-01 02:28:01 +0000161/* Stack size for a thread. We try and check that they do not go
162 beyond it. */
sewardjf0b06452002-06-04 08:38:04 +0000163#define VG_PTHREAD_STACK_SIZE (1 << 20)
sewardjbf290b92002-05-01 02:28:01 +0000164
sewardj20917d82002-05-28 01:36:45 +0000165/* Number of entries in the rwlock-remapping table. */
sewardj89745a52002-09-27 01:04:29 +0000166#define VG_N_RWLOCKS 500
sewardj20917d82002-05-28 01:36:45 +0000167
sewardj8ad94e12002-05-29 00:10:20 +0000168/* Number of entries in each thread's cleanup stack. */
sewardj61821c02003-05-04 13:02:10 +0000169#define VG_N_CLEANUPSTACK 16
sewardj8ad94e12002-05-29 00:10:20 +0000170
sewardj2cb00342002-06-28 01:46:26 +0000171/* Number of entries in each thread's fork-handler stack. */
sewardj4700f042003-07-26 17:49:58 +0000172#define VG_N_FORKHANDLERSTACK 4
sewardj2cb00342002-06-28 01:46:26 +0000173
njn25e49d8e72002-09-23 09:36:25 +0000174/* Max number of callers for context in a suppression. */
175#define VG_N_SUPP_CALLERS 4
sewardj73cf3bc2002-11-03 03:20:15 +0000176
fitzhardinge98abfc72003-12-16 02:05:15 +0000177/* Useful macros */
178/* a - alignment - must be a power of 2 */
179#define ROUNDDN(p, a) ((Addr)(p) & ~((a)-1))
180#define ROUNDUP(p, a) ROUNDDN((p)+(a)-1, (a))
181#define PGROUNDDN(p) ROUNDDN(p, VKI_BYTES_PER_PAGE)
182#define PGROUNDUP(p) ROUNDUP(p, VKI_BYTES_PER_PAGE)
183
nethercote80013e92004-09-05 20:39:51 +0000184/* ---------------------------------------------------------------------
185 Environment variables
186 ------------------------------------------------------------------ */
187
188/* The directory we look for all our auxillary files in */
189#define VALGRINDLIB "VALGRINDLIB"
190
191/* Additional command-line arguments; they are overridden by actual
192 command-line option. Each argument is separated by spaces. There
193 is no quoting mechanism.
194 */
195#define VALGRINDOPTS "VALGRIND_OPTS"
196
197/* If this variable is present in the environment, then valgrind will
198 not parse the command line for options at all; all options come
199 from this variable. Arguments are terminated by ^A (\001). There
200 is no quoting mechanism.
201
202 This variable is not expected to be set by anything other than
203 Valgrind itself, as part of its handling of execve with
204 --trace-children=yes. This variable should not be present in the
205 client environment.
206 */
207#define VALGRINDCLO "_VALGRIND_CLO"
208
fitzhardinge98abfc72003-12-16 02:05:15 +0000209
sewardjde4a1d02002-03-22 01:27:54 +0000210/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000211 Command-line-settable options
212 ------------------------------------------------------------------ */
213
sewardj4f094a72002-11-05 23:37:35 +0000214/* Default destination port to be used in logging over a network, if
215 none specified. */
216#define VG_CLO_DEFAULT_LOGPORT 1500
sewardj73cf3bc2002-11-03 03:20:15 +0000217
218/* The max number of suppression files. */
sewardjde4a1d02002-03-22 01:27:54 +0000219#define VG_CLO_MAX_SFILES 10
220
nethercote04d0fbc2004-01-26 16:48:06 +0000221/* Default debugger command. */
222#define VG_CLO_DEFAULT_DBCOMMAND GDB_PATH " -nw %f %p"
223
sewardj4cf05692002-10-27 20:28:29 +0000224/* Describes where logging output is to be sent. */
225typedef
226 enum {
227 VgLogTo_Fd,
228 VgLogTo_File,
229 VgLogTo_Socket
230 } VgLogTo;
231
jsgf855d93d2003-10-13 22:26:55 +0000232/* pid of main process */
233extern Int VG_(main_pid);
234
235/* pgrp of process (global to all threads) */
236extern Int VG_(main_pgrp);
sewardj4cf05692002-10-27 20:28:29 +0000237
thughesad1c9562004-06-26 11:27:52 +0000238/* Application-visible file descriptor limits */
239extern Int VG_(fd_soft_limit);
240extern Int VG_(fd_hard_limit);
fitzhardingef0046f22003-12-18 02:39:22 +0000241
sewardj72f98ff2002-06-13 17:23:38 +0000242/* Should we stop collecting errors if too many appear? default: YES */
sewardj2e432902002-06-13 20:44:00 +0000243extern Bool VG_(clo_error_limit);
nethercote04d0fbc2004-01-26 16:48:06 +0000244/* Enquire about whether to attach to a debugger at errors? default: NO */
245extern Bool VG_(clo_db_attach);
246/* The debugger command? default: whatever gdb ./configure found */
247extern Char* VG_(clo_db_command);
njn43c799e2003-04-08 00:08:52 +0000248/* Enquire about generating a suppression for each error? default: NO */
249extern Bool VG_(clo_gen_suppressions);
sewardjde4a1d02002-03-22 01:27:54 +0000250/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
nethercote27fec902004-06-16 21:26:32 +0000251extern Int VG_(clo_sanity_level);
sewardjde4a1d02002-03-22 01:27:54 +0000252/* Automatically attempt to demangle C++ names? default: YES */
253extern Bool VG_(clo_demangle);
sewardjde4a1d02002-03-22 01:27:54 +0000254/* Simulate child processes? default: NO */
255extern Bool VG_(clo_trace_children);
sewardj4cf05692002-10-27 20:28:29 +0000256
257/* Where logging output is to be sent to.
258
nethercotef8548672004-06-21 12:42:35 +0000259 When log_to == VgLogTo_Fd, clo_log_fd holds the file id, and is
260 taken from the command line. clo_log_name is irrelevant.
sewardj4cf05692002-10-27 20:28:29 +0000261
nethercotef8548672004-06-21 12:42:35 +0000262 When log_to == VgLogTo_File, clo_log_name holds the log-file
263 name, and is taken from the command line. clo_log_fd is then
264 made to hold the relevant file id, by opening clo_log_name
sewardj4cf05692002-10-27 20:28:29 +0000265 (concatenated with the process ID) for writing.
266
nethercotef8548672004-06-21 12:42:35 +0000267 When log_to == VgLogTo_Socket, clo_log_name holds the
sewardj4cf05692002-10-27 20:28:29 +0000268 hostname:portnumber pair, and is taken from the command line.
nethercotef8548672004-06-21 12:42:35 +0000269 clo_log_fd is then made to hold the relevant file handle, by
sewardj4cf05692002-10-27 20:28:29 +0000270 opening a connection to said hostname:portnumber pair.
271
nethercotef8548672004-06-21 12:42:35 +0000272 Global default is to set log_to == VgLogTo_Fd and log_fd == 2
sewardj4cf05692002-10-27 20:28:29 +0000273 (stderr). */
274extern VgLogTo VG_(clo_log_to);
nethercotef8548672004-06-21 12:42:35 +0000275extern Int VG_(clo_log_fd);
276extern Char* VG_(clo_log_name);
sewardjde4a1d02002-03-22 01:27:54 +0000277
thughes6233a382004-08-21 11:10:44 +0000278/* Add timestamps to log messages? default: NO */
279extern Bool VG_(clo_time_stamp);
280
sewardj6024b212003-07-13 10:54:33 +0000281/* The file descriptor to read for input. default: 0 == stdin */
282extern Int VG_(clo_input_fd);
sewardjde4a1d02002-03-22 01:27:54 +0000283/* The number of suppression files specified. */
284extern Int VG_(clo_n_suppressions);
285/* The names of the suppression files. */
286extern Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
287
288/* Single stepping? default: NO */
289extern Bool VG_(clo_single_step);
290/* Code improvement? default: YES */
291extern Bool VG_(clo_optimise);
njn25e49d8e72002-09-23 09:36:25 +0000292/* DEBUG: print generated code? default: 00000 ( == NO ) */
293extern Bool VG_(clo_trace_codegen);
sewardjde4a1d02002-03-22 01:27:54 +0000294/* DEBUG: print system calls? default: NO */
295extern Bool VG_(clo_trace_syscalls);
296/* DEBUG: print signal details? default: NO */
297extern Bool VG_(clo_trace_signals);
298/* DEBUG: print symtab details? default: NO */
299extern Bool VG_(clo_trace_symtab);
sewardj8937c812002-04-12 20:12:20 +0000300/* DEBUG: print thread scheduling events? default: NO */
301extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000302/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
303 (some), 2 (all) */
304extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000305/* Display gory details for the k'th most popular error. default:
306 Infinity. */
307extern Int VG_(clo_dump_error);
308/* Number of parents of a backtrace. Default: 8. */
309extern Int VG_(clo_backtrace_size);
daywalker7e73e5f2003-07-04 16:18:15 +0000310/* Engage miscellaneous weird hacks needed for some progs. */
sewardj8d365b52002-05-12 10:52:16 +0000311extern Char* VG_(clo_weird_hacks);
jsgf855d93d2003-10-13 22:26:55 +0000312/* How often we should poll for signals, assuming we need to poll for
313 signals. */
314extern Int VG_(clo_signal_polltime);
315
316/* Low latency syscalls and signals */
317extern Bool VG_(clo_lowlat_syscalls);
318extern Bool VG_(clo_lowlat_signals);
319
rjwalshf5f536f2003-11-17 17:45:00 +0000320/* Track open file descriptors? */
321extern Bool VG_(clo_track_fds);
322
sewardj858964b2002-10-05 14:15:43 +0000323/* Should we run __libc_freeres at exit? Sometimes causes crashes.
324 Default: YES. Note this is subservient to VG_(needs).libc_freeres;
325 if the latter says False, then the setting of VG_(clo_weird_hacks)
nethercote996901a2004-08-03 13:29:09 +0000326 is ignored. Ie if a tool says no, I don't want this to run, that
sewardj858964b2002-10-05 14:15:43 +0000327 cannot be overridden from the command line. */
328extern Bool VG_(clo_run_libc_freeres);
sewardjb5ff83e2002-12-01 19:40:49 +0000329/* Use the basic-block chaining optimisation? Default: YES */
sewardj22854b92002-11-30 14:00:47 +0000330extern Bool VG_(clo_chain_bb);
fitzhardinge462f4f92003-12-18 02:10:54 +0000331/* Generate branch-prediction hints? */
332extern Bool VG_(clo_branchpred);
nethercote77eba602003-11-13 17:35:04 +0000333/* Continue stack traces below main()? Default: NO */
334extern Bool VG_(clo_show_below_main);
fitzhardinge98abfc72003-12-16 02:05:15 +0000335/* Test each client pointer dereference to check it's within the
336 client address space bounds */
337extern Bool VG_(clo_pointercheck);
sewardjde4a1d02002-03-22 01:27:54 +0000338
rjwalshe4e779d2004-04-16 23:02:29 +0000339/* Set up the libc freeres wrapper */
340extern void VG_(intercept_libc_freeres_wrapper)(Addr);
341
sewardjde4a1d02002-03-22 01:27:54 +0000342/* ---------------------------------------------------------------------
nethercote85cdd342004-08-01 22:36:40 +0000343 Profiling stuff
sewardjde4a1d02002-03-22 01:27:54 +0000344 ------------------------------------------------------------------ */
345
sewardjde4a1d02002-03-22 01:27:54 +0000346extern void VGP_(init_profiling) ( void );
347extern void VGP_(done_profiling) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000348
njn25e49d8e72002-09-23 09:36:25 +0000349#undef VGP_PUSHCC
350#undef VGP_POPCC
351#define VGP_PUSHCC(x) if (VG_(clo_profile)) VGP_(pushcc)(x)
352#define VGP_POPCC(x) if (VG_(clo_profile)) VGP_(popcc)(x)
sewardjde4a1d02002-03-22 01:27:54 +0000353
sewardjde4a1d02002-03-22 01:27:54 +0000354/* ---------------------------------------------------------------------
nethercote996901a2004-08-03 13:29:09 +0000355 Tool-related types
njn810086f2002-11-14 12:42:47 +0000356 ------------------------------------------------------------------ */
nethercote996901a2004-08-03 13:29:09 +0000357/* These structs are not exposed to tools to mitigate possibility of
358 binary-incompatibilities when the core/tool interface changes. Instead,
nethercote46063202004-09-02 08:51:43 +0000359 set functions are provided (see include/tool.h). */
njn810086f2002-11-14 12:42:47 +0000360typedef
361 struct {
362 Char* name;
363 Char* version;
364 Char* description;
365 Char* copyright_author;
366 Char* bug_reports_to;
njn120281f2003-02-03 12:20:07 +0000367 UInt avg_translation_sizeB;
njn810086f2002-11-14 12:42:47 +0000368 }
369 VgDetails;
370
371extern VgDetails VG_(details);
372
373/* If new fields are added to this type, update:
374 * - vg_main.c:initialisation of VG_(needs)
375 * - vg_main.c:sanity_check_needs()
376 *
377 * If the name of this type or any of its fields change, update:
378 * - dependent comments (just search for "VG_(needs)").
379 */
380typedef
381 struct {
382 Bool libc_freeres;
383 Bool core_errors;
njn810086f2002-11-14 12:42:47 +0000384 Bool skin_errors;
385 Bool basic_block_discards;
386 Bool shadow_regs;
387 Bool command_line_options;
388 Bool client_requests;
389 Bool extended_UCode;
390 Bool syscall_wrapper;
njn810086f2002-11-14 12:42:47 +0000391 Bool sanity_checks;
392 Bool data_syms;
fitzhardinge98abfc72003-12-16 02:05:15 +0000393 Bool shadow_memory;
njn810086f2002-11-14 12:42:47 +0000394 }
395 VgNeeds;
396
397extern VgNeeds VG_(needs);
398
fitzhardinge98abfc72003-12-16 02:05:15 +0000399extern void VG_(tool_init_dlsym)(void *dlhandle);
njn810086f2002-11-14 12:42:47 +0000400
fitzhardinge98abfc72003-12-16 02:05:15 +0000401#include "vg_toolint.h"
njn810086f2002-11-14 12:42:47 +0000402
403/* ---------------------------------------------------------------------
404 Exports of vg_needs.c
405 ------------------------------------------------------------------ */
406
407void VG_(sanity_check_needs)(void);
408
409/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000410 Exports of vg_malloc2.c
411 ------------------------------------------------------------------ */
412
413/* Allocation arenas.
njn3e884182003-04-15 13:03:23 +0000414
415 CORE for the core's general use.
nethercote60f5b822004-01-26 17:24:42 +0000416 TOOL for the tool to use (and the only one it uses).
njn3e884182003-04-15 13:03:23 +0000417 SYMTAB for Valgrind's symbol table storage.
418 JITTER for small storage during translation.
nethercote996901a2004-08-03 13:29:09 +0000419 CLIENT for the client's mallocs/frees, if the tool replaces glibc's
420 malloc() et al -- redzone size is chosen by the tool.
njn3e884182003-04-15 13:03:23 +0000421 DEMANGLE for the C++ demangler.
422 EXECTXT for storing ExeContexts.
423 ERRORS for storing CoreErrors.
424 TRANSIENT for very short-term use. It should be empty in between uses.
425
njn25e49d8e72002-09-23 09:36:25 +0000426 When adding a new arena, remember also to add it to ensure_mm_init().
sewardjde4a1d02002-03-22 01:27:54 +0000427*/
428typedef Int ArenaId;
429
njn3e884182003-04-15 13:03:23 +0000430#define VG_N_ARENAS 9
sewardjde4a1d02002-03-22 01:27:54 +0000431
njn3e884182003-04-15 13:03:23 +0000432#define VG_AR_CORE 0
nethercote60f5b822004-01-26 17:24:42 +0000433#define VG_AR_TOOL 1
njn3e884182003-04-15 13:03:23 +0000434#define VG_AR_SYMTAB 2
435#define VG_AR_JITTER 3
436#define VG_AR_CLIENT 4
437#define VG_AR_DEMANGLE 5
438#define VG_AR_EXECTXT 6
439#define VG_AR_ERRORS 7
440#define VG_AR_TRANSIENT 8
sewardjde4a1d02002-03-22 01:27:54 +0000441
nethercote2d5b8162004-08-11 09:40:52 +0000442// This is both the minimum payload size of a malloc'd block, and its
443// minimum alignment. Must be a power of 2 greater than 4, and should be
444// greater than 8.
445#define VG_MIN_MALLOC_SZB 8
446
447// Round-up size for --sloppy-malloc=yes.
448#define VG_SLOPPY_MALLOC_SZB 4
449
njn25e49d8e72002-09-23 09:36:25 +0000450extern void* VG_(arena_malloc) ( ArenaId arena, Int nbytes );
451extern void VG_(arena_free) ( ArenaId arena, void* ptr );
njn3e884182003-04-15 13:03:23 +0000452extern void* VG_(arena_calloc) ( ArenaId arena, Int alignment,
453 Int nmemb, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000454extern void* VG_(arena_realloc) ( ArenaId arena, void* ptr, Int alignment,
455 Int size );
456extern void* VG_(arena_malloc_aligned) ( ArenaId aid, Int req_alignB,
sewardjde4a1d02002-03-22 01:27:54 +0000457 Int req_pszB );
458
njn8a6b6c02003-04-22 22:45:55 +0000459extern Int VG_(arena_payload_szB) ( ArenaId aid, void* payload );
460
nethercote885dd912004-08-03 23:14:00 +0000461extern void VG_(sanity_check_malloc_all) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000462
nethercote3a42fb82004-08-03 18:08:50 +0000463extern void VG_(print_all_arena_stats) ( void );
nethercote885dd912004-08-03 23:14:00 +0000464
sewardjde4a1d02002-03-22 01:27:54 +0000465extern Bool VG_(is_empty_arena) ( ArenaId aid );
466
sewardjde4a1d02002-03-22 01:27:54 +0000467/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000468 Exports of vg_intercept.c
sewardj2e93c502002-04-12 11:12:52 +0000469 ------------------------------------------------------------------ */
470
471/* This doesn't export code or data that valgrind.so needs to link
472 against. However, the scheduler does need to know the following
473 request codes. A few, publically-visible, request codes are also
nethercote996901a2004-08-03 13:29:09 +0000474 defined in valgrind.h, and similar headers for some tools. */
sewardj2e93c502002-04-12 11:12:52 +0000475
njn4c791212003-05-02 17:53:54 +0000476#define VG_USERREQ__MALLOC 0x2001
477#define VG_USERREQ__FREE 0x2002
478
sewardj20917d82002-05-28 01:36:45 +0000479/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn
480 MUST NOT return -- ever. Eventually it will do either __QUIT or
481 __WAIT_JOINER. */
482#define VG_USERREQ__APPLY_IN_NEW_THREAD 0x3001
483
484/* ( no-args ): calling thread disappears from the system forever.
485 Reclaim resources. */
486#define VG_USERREQ__QUIT 0x3002
487
488/* ( void* ): calling thread waits for joiner and returns the void* to
489 it. */
490#define VG_USERREQ__WAIT_JOINER 0x3003
491
492/* ( ThreadId, void** ): wait to join a thread. */
493#define VG_USERREQ__PTHREAD_JOIN 0x3004
494
495/* Set cancellation state and type for this thread. */
496#define VG_USERREQ__SET_CANCELSTATE 0x3005
497#define VG_USERREQ__SET_CANCELTYPE 0x3006
498
499/* ( no-args ): Test if we are at a cancellation point. */
500#define VG_USERREQ__TESTCANCEL 0x3007
501
502/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
503 with this arg to indicate that a cancel is now pending for the
504 specified thread. */
505#define VG_USERREQ__SET_CANCELPEND 0x3008
506
507/* Set/get detach state for this thread. */
508#define VG_USERREQ__SET_OR_GET_DETACH 0x3009
509
510#define VG_USERREQ__PTHREAD_GET_THREADID 0x300B
511#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x300C
512#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK 0x300D
513#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x300E
514#define VG_USERREQ__PTHREAD_COND_WAIT 0x300F
515#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT 0x3010
516#define VG_USERREQ__PTHREAD_COND_SIGNAL 0x3011
517#define VG_USERREQ__PTHREAD_COND_BROADCAST 0x3012
518#define VG_USERREQ__PTHREAD_KEY_CREATE 0x3013
519#define VG_USERREQ__PTHREAD_KEY_DELETE 0x3014
sewardj00a66b12002-10-12 16:42:35 +0000520#define VG_USERREQ__PTHREAD_SETSPECIFIC_PTR 0x3015
521#define VG_USERREQ__PTHREAD_GETSPECIFIC_PTR 0x3016
sewardj20917d82002-05-28 01:36:45 +0000522#define VG_USERREQ__READ_MILLISECOND_TIMER 0x3017
523#define VG_USERREQ__PTHREAD_SIGMASK 0x3018
jsgf855d93d2003-10-13 22:26:55 +0000524#define VG_USERREQ__SIGWAIT 0x3019 /* unused */
sewardj20917d82002-05-28 01:36:45 +0000525#define VG_USERREQ__PTHREAD_KILL 0x301A
526#define VG_USERREQ__PTHREAD_YIELD 0x301B
sewardj00a66b12002-10-12 16:42:35 +0000527#define VG_USERREQ__PTHREAD_KEY_VALIDATE 0x301C
sewardj2e93c502002-04-12 11:12:52 +0000528
sewardj8ad94e12002-05-29 00:10:20 +0000529#define VG_USERREQ__CLEANUP_PUSH 0x3020
530#define VG_USERREQ__CLEANUP_POP 0x3021
sewardj870497a2002-05-29 01:06:47 +0000531#define VG_USERREQ__GET_KEY_D_AND_S 0x3022
sewardj8ad94e12002-05-29 00:10:20 +0000532
sewardjef037c72002-05-30 00:40:03 +0000533#define VG_USERREQ__NUKE_OTHER_THREADS 0x3023
sewardjefbfcdf2002-06-19 17:35:45 +0000534
535/* Ask how many signal handler returns have happened to this
536 thread. */
jsgf855d93d2003-10-13 22:26:55 +0000537#define VG_USERREQ__GET_N_SIGS_RETURNED 0x3024 /* unused */
sewardjef037c72002-05-30 00:40:03 +0000538
sewardj2cb00342002-06-28 01:46:26 +0000539/* Get/set entries for a thread's pthread_atfork stack. */
540#define VG_USERREQ__SET_FHSTACK_USED 0x3025
541#define VG_USERREQ__GET_FHSTACK_USED 0x3026
542#define VG_USERREQ__SET_FHSTACK_ENTRY 0x3027
543#define VG_USERREQ__GET_FHSTACK_ENTRY 0x3028
sewardjefbfcdf2002-06-19 17:35:45 +0000544
nethercotef971ab72004-08-02 16:27:40 +0000545/* Denote the finish of __libc_freeres_wrapper(). */
sewardj1fe7b002002-07-16 01:43:15 +0000546#define VG_USERREQ__LIBC_FREERES_DONE 0x3029
fitzhardinge98abfc72003-12-16 02:05:15 +0000547
548/* Allocate RT signals */
549#define VG_USERREQ__GET_SIGRT_MIN 0x302B
550#define VG_USERREQ__GET_SIGRT_MAX 0x302C
551#define VG_USERREQ__ALLOC_RTSIG 0x302D
552
553/* Hook for replace_malloc.o to get malloc functions */
554#define VG_USERREQ__GET_MALLOCFUNCS 0x3030
555
thughesdaa34562004-06-27 12:48:53 +0000556/* Get stack information for a thread. */
557#define VG_USERREQ__GET_STACK_INFO 0x3033
558
sewardj45b4b372002-04-16 22:50:32 +0000559/* Cosmetic ... */
560#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
sewardj4dced352002-06-04 22:54:20 +0000561/* Log a pthread error from client-space. Cosmetic. */
562#define VG_USERREQ__PTHREAD_ERROR 0x3102
fitzhardinge39de4b42003-10-31 07:12:21 +0000563/* Internal equivalent of VALGRIND_PRINTF . */
564#define VG_USERREQ__INTERNAL_PRINTF 0x3103
565/* Internal equivalent of VALGRIND_PRINTF_BACKTRACE . */
566#define VG_USERREQ__INTERNAL_PRINTF_BACKTRACE 0x3104
sewardj45b4b372002-04-16 22:50:32 +0000567
sewardj54cacf02002-04-12 23:24:59 +0000568/*
nethercote5a2664c2004-09-02 15:37:39 +0000569In core_asm.h:
sewardj54cacf02002-04-12 23:24:59 +0000570#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardj54cacf02002-04-12 23:24:59 +0000571*/
572
rjwalshe4e779d2004-04-16 23:02:29 +0000573#define VG_INTERCEPT_PREFIX "_vgi__"
574#define VG_INTERCEPT_PREFIX_LEN 6
575#define VG_INTERCEPT(name) _vgi__##name
576#define VG_INTERCEPT_ALIAS(name) "_vgi__" #name
577
578#define VG_WRAPPER_PREFIX "_vgw__"
579#define VG_WRAPPER_PREFIX_LEN 6
580#define VG_WRAPPER(name) _vgw__##name
581#define VG_WRAPPER_ALIAS(name) "_vgw__" #name
582
njn4c791212003-05-02 17:53:54 +0000583
fitzhardinge98abfc72003-12-16 02:05:15 +0000584struct vg_mallocfunc_info {
585 /* things vg_replace_malloc.o needs to know about */
586 Addr sk_malloc;
587 Addr sk_calloc;
588 Addr sk_realloc;
589 Addr sk_memalign;
590 Addr sk___builtin_new;
591 Addr sk___builtin_vec_new;
592 Addr sk_free;
593 Addr sk___builtin_delete;
594 Addr sk___builtin_vec_delete;
595
596 Addr arena_payload_szB;
597
598 Bool clo_sloppy_malloc;
599 Bool clo_trace_malloc;
600};
sewardj1fe7b002002-07-16 01:43:15 +0000601
sewardj2e93c502002-04-12 11:12:52 +0000602/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000603 Exports of vg_defaults.c
604 ------------------------------------------------------------------ */
605
606extern Bool VG_(sk_malloc_called_by_scheduler);
607
608
sewardj92a59562002-09-30 00:53:10 +0000609/* Maximum number of LDT entries supported (by the x86). */
610#define VG_M_LDT_ENTRIES 8192
611/* The size of each LDT entry == sizeof(VgLdtEntry) */
612#define VG_LDT_ENTRY_SIZE 8
613
614/* Alloc & copy, and dealloc. */
nethercote85cdd342004-08-01 22:36:40 +0000615extern VgLdtEntry* VG_(allocate_LDT_for_thread) ( VgLdtEntry* parent_ldt );
616extern void VG_(deallocate_LDT_for_thread) ( VgLdtEntry* ldt );
617extern void VG_(clear_TLS_for_thread) ( VgLdtEntry* tls );
sewardj92a59562002-09-30 00:53:10 +0000618
619/* Simulate the modify_ldt syscall. */
620extern Int VG_(sys_modify_ldt) ( ThreadId tid,
621 Int func, void* ptr, UInt bytecount );
622
fitzhardinge47735af2004-01-21 01:27:27 +0000623/* Simulate the {get,set}_thread_area syscalls. */
624extern Int VG_(sys_set_thread_area) ( ThreadId tid,
625 struct vki_modify_ldt_ldt_s* info );
626extern Int VG_(sys_get_thread_area) ( ThreadId tid,
627 struct vki_modify_ldt_ldt_s* info );
628
sewardje1042472002-09-30 12:33:11 +0000629/* Called from generated code. Given a segment selector and a virtual
630 address, return a linear address, and do limit checks too. */
631extern Addr VG_(do_useseg) ( UInt seg_selector, Addr virtual_addr );
632
sewardj92a59562002-09-30 00:53:10 +0000633
634/* ---------------------------------------------------------------------
nethercote1f0173b2004-02-28 15:40:36 +0000635 Exports of vg_libpthread.c
636 ------------------------------------------------------------------ */
637
638/* Replacements for pthread types, shared between vg_libpthread.c and
639 vg_scheduler.c. See comment in vg_libpthread.c above the other
640 vg_pthread_*_t types for a description of how these are used. */
641
642struct _vg_pthread_fastlock
643{
644 long int __vg_status; /* "Free" or "taken" or head of waiting list */
645 int __vg_spinlock; /* Used by compare_and_swap emulation. Also,
646 adaptive SMP lock stores spin count here. */
647};
648
649typedef struct
650{
651 int __vg_m_reserved; /* Reserved for future use */
652 int __vg_m_count; /* Depth of recursive locking */
653 /*_pthread_descr*/ void* __vg_m_owner; /* Owner thread (if recursive or errcheck) */
654 int __vg_m_kind; /* Mutex kind: fast, recursive or errcheck */
655 struct _vg_pthread_fastlock __vg_m_lock; /* Underlying fast lock */
656} vg_pthread_mutex_t;
657
658typedef struct
659{
660 struct _vg_pthread_fastlock __vg_c_lock; /* Protect against concurrent access */
nethercotedffad082004-02-28 23:32:11 +0000661 /*_pthread_descr*/ void* __vg_c_waiting; /* Threads waiting on this condition */
662
663 // Nb: the following padding removed because it was missing from an
664 // earlier glibc, so the size test in the CONVERT macro was failing.
665 // --njn
666
nethercote1f0173b2004-02-28 15:40:36 +0000667 // Padding ensures the size is 48 bytes
nethercotedffad082004-02-28 23:32:11 +0000668 /*char __vg_padding[48 - sizeof(struct _vg_pthread_fastlock)
nethercote1f0173b2004-02-28 15:40:36 +0000669 - sizeof(void*) - sizeof(long long)];
nethercotedffad082004-02-28 23:32:11 +0000670 long long __vg_align;*/
nethercote1f0173b2004-02-28 15:40:36 +0000671} vg_pthread_cond_t;
672
673
674/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000675 Exports of vg_scheduler.c
676 ------------------------------------------------------------------ */
677
sewardj2e93c502002-04-12 11:12:52 +0000678typedef
jsgf855d93d2003-10-13 22:26:55 +0000679 enum ThreadStatus {
sewardj2e93c502002-04-12 11:12:52 +0000680 VgTs_Empty, /* this slot is not in use */
681 VgTs_Runnable, /* waiting to be scheduled */
682 VgTs_WaitJoiner, /* waiting for someone to do join on me */
683 VgTs_WaitJoinee, /* waiting for the thread I did join on */
sewardj2e93c502002-04-12 11:12:52 +0000684 VgTs_WaitMX, /* waiting on a mutex */
sewardj3b5d8862002-04-20 13:53:23 +0000685 VgTs_WaitCV, /* waiting on a condition variable */
jsgf855d93d2003-10-13 22:26:55 +0000686 VgTs_WaitSys, /* waiting for a syscall to complete */
687 VgTs_Sleeping, /* sleeping for a while */
sewardj2e93c502002-04-12 11:12:52 +0000688 }
689 ThreadStatus;
sewardj8ad94e12002-05-29 00:10:20 +0000690
thughes11975ff2004-06-12 12:58:22 +0000691typedef
692 enum CleanupType {
693 VgCt_None, /* this cleanup entry is not initialised */
694 VgCt_Function, /* an old-style function pointer cleanup */
695 VgCt_Longjmp /* a new-style longjmp based cleanup */
696 }
697 CleanupType;
698
thughesdaa34562004-06-27 12:48:53 +0000699/* Information on a thread's stack. */
700typedef
701 struct {
702 Addr base;
703 UInt size;
704 UInt guardsize;
705 }
706 StackInfo;
707
sewardj8ad94e12002-05-29 00:10:20 +0000708/* An entry in a threads's cleanup stack. */
709typedef
710 struct {
thughes11975ff2004-06-12 12:58:22 +0000711 CleanupType type;
712 union {
713 struct {
714 void (*fn)(void*);
715 void* arg;
716 } function;
717 struct {
thughesebed9982004-06-12 17:25:25 +0000718 void *ub;
thughes11975ff2004-06-12 12:58:22 +0000719 int ctype;
720 } longjmp;
721 } data;
sewardj8ad94e12002-05-29 00:10:20 +0000722 }
723 CleanupEntry;
sewardj2cb00342002-06-28 01:46:26 +0000724
725/* An entry in a thread's fork-handler stack. */
726typedef
727 struct {
728 void (*prepare)(void);
729 void (*parent)(void);
730 void (*child)(void);
731 }
732 ForkHandlerEntry;
733
jsgf855d93d2003-10-13 22:26:55 +0000734typedef struct ProxyLWP ProxyLWP;
sewardj2cb00342002-06-28 01:46:26 +0000735
njn72718642003-07-24 08:45:32 +0000736typedef
737 struct _ThreadState {
njn25e49d8e72002-09-23 09:36:25 +0000738 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
739 The thread identity is simply the index in vg_threads[].
740 ThreadId == 1 is the root thread and has the special property
741 that we don't try and allocate or deallocate its stack. For
742 convenience of generating error message, we also put the
743 ThreadId in this tid field, but be aware that it should
744 ALWAYS == the index in vg_threads[]. */
745 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000746
njn25e49d8e72002-09-23 09:36:25 +0000747 /* Current scheduling status.
sewardj5f07b662002-04-23 16:52:51 +0000748
njn25e49d8e72002-09-23 09:36:25 +0000749 Complications: whenever this is set to VgTs_WaitMX, you
750 should also set .m_edx to whatever the required return value
751 is for pthread_mutex_lock / pthread_cond_timedwait for when
752 the mutex finally gets unblocked. */
753 ThreadStatus status;
sewardj2e93c502002-04-12 11:12:52 +0000754
njn25e49d8e72002-09-23 09:36:25 +0000755 /* When .status == WaitMX, points to the mutex I am waiting for.
756 When .status == WaitCV, points to the mutex associated with
757 the condition variable indicated by the .associated_cv field.
758 In all other cases, should be NULL. */
nethercote1f0173b2004-02-28 15:40:36 +0000759 vg_pthread_mutex_t* associated_mx;
sewardj3b5d8862002-04-20 13:53:23 +0000760
njn25e49d8e72002-09-23 09:36:25 +0000761 /* When .status == WaitCV, points to the condition variable I am
762 waiting for. In all other cases, should be NULL. */
763 void* /*pthread_cond_t* */ associated_cv;
sewardj2e93c502002-04-12 11:12:52 +0000764
njn25e49d8e72002-09-23 09:36:25 +0000765 /* If VgTs_Sleeping, this is when we should wake up, measured in
njn6c846552003-09-16 07:41:43 +0000766 milliseconds as supplied by VG_(read_millisecond_timer).
sewardj2e93c502002-04-12 11:12:52 +0000767
njn25e49d8e72002-09-23 09:36:25 +0000768 If VgTs_WaitCV, this indicates the time at which
769 pthread_cond_timedwait should wake up. If == 0xFFFFFFFF,
770 this means infinitely far in the future, viz,
771 pthread_cond_wait. */
772 UInt awaken_at;
sewardj20917d82002-05-28 01:36:45 +0000773
njn25e49d8e72002-09-23 09:36:25 +0000774 /* If VgTs_WaitJoiner, return value, as generated by joinees. */
775 void* joinee_retval;
sewardj20917d82002-05-28 01:36:45 +0000776
njn25e49d8e72002-09-23 09:36:25 +0000777 /* If VgTs_WaitJoinee, place to copy the return value to, and
778 the identity of the thread we're waiting for. */
779 void** joiner_thread_return;
780 ThreadId joiner_jee_tid;
sewardj8ad94e12002-05-29 00:10:20 +0000781
jsgf855d93d2003-10-13 22:26:55 +0000782 /* If VgTs_WaitSys, this is the result of the pre-syscall check */
783 void *sys_pre_res;
784
785 /* If VgTs_WaitSys, this is the syscall we're currently running */
786 Int syscallno;
787
thughesbaa46e52004-07-29 17:44:23 +0000788 /* If VgTs_WaitSys, this is the syscall flags */
789 UInt sys_flags;
790
jsgf855d93d2003-10-13 22:26:55 +0000791 /* Details about this thread's proxy LWP */
792 ProxyLWP *proxy;
793
njn25e49d8e72002-09-23 09:36:25 +0000794 /* Whether or not detached. */
795 Bool detached;
sewardj20917d82002-05-28 01:36:45 +0000796
njn25e49d8e72002-09-23 09:36:25 +0000797 /* Cancelability state and type. */
798 Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
799 Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
800
801 /* Pointer to fn to call to do cancellation. Indicates whether
802 or not cancellation is pending. If NULL, not pending. Else
803 should be &thread_exit_wrapper(), indicating that
804 cancallation is pending. */
805 void (*cancel_pend)(void*);
sewardj2e93c502002-04-12 11:12:52 +0000806
njn25e49d8e72002-09-23 09:36:25 +0000807 /* The cleanup stack. */
808 Int custack_used;
809 CleanupEntry custack[VG_N_CLEANUPSTACK];
sewardj5f07b662002-04-23 16:52:51 +0000810
sewardj00a66b12002-10-12 16:42:35 +0000811 /* A pointer to the thread's-specific-data. This is handled almost
812 entirely from vg_libpthread.c. We just provide hooks to get and
813 set this ptr. This is either NULL, indicating the thread has
814 read/written none of its specifics so far, OR points to a
815 void*[VG_N_THREAD_KEYS], allocated and deallocated in
816 vg_libpthread.c. */
817 void** specifics_ptr;
sewardjb48e5002002-05-13 00:16:03 +0000818
njn25e49d8e72002-09-23 09:36:25 +0000819 /* This thread's blocked-signals mask. Semantics is that for a
820 signal to be delivered to this thread, the signal must not be
jsgf855d93d2003-10-13 22:26:55 +0000821 blocked by this signal mask. If more than one thread accepts a
822 signal, then it will be delivered to one at random. If all
823 threads block the signal, it will remain pending until either a
824 thread unblocks it or someone uses sigwaitsig/sigtimedwait.
825
826 sig_mask reflects what the client told us its signal mask should
827 be, but isn't necessarily the current signal mask of the proxy
828 LWP: it may have more signals blocked because of signal
829 handling, or it may be different because of sigsuspend.
830 */
njn25e49d8e72002-09-23 09:36:25 +0000831 vki_ksigset_t sig_mask;
sewardjb48e5002002-05-13 00:16:03 +0000832
fitzhardingef0dd7e12004-01-16 02:17:30 +0000833 /* Effective signal mask. This is the mask which currently
834 applies; it may be different from sig_mask while a signal
jsgf855d93d2003-10-13 22:26:55 +0000835 handler is running.
836 */
837 vki_ksigset_t eff_sig_mask;
sewardj2e93c502002-04-12 11:12:52 +0000838
njn25e49d8e72002-09-23 09:36:25 +0000839 /* Stacks. When a thread slot is freed, we don't deallocate its
840 stack; we just leave it lying around for the next use of the
841 slot. If the next use of the slot requires a larger stack,
842 only then is the old one deallocated and a new one
843 allocated.
sewardj2e93c502002-04-12 11:12:52 +0000844
njn25e49d8e72002-09-23 09:36:25 +0000845 For the main thread (threadid == 0), this mechanism doesn't
846 apply. We don't know the size of the stack since we didn't
847 allocate it, and furthermore we never reallocate it. */
sewardj2e93c502002-04-12 11:12:52 +0000848
njn25e49d8e72002-09-23 09:36:25 +0000849 /* The allocated size of this thread's stack (permanently zero
850 if this is ThreadId == 0, since we didn't allocate its stack) */
851 UInt stack_size;
sewardj1e8cdc92002-04-18 11:37:52 +0000852
njn25e49d8e72002-09-23 09:36:25 +0000853 /* Address of the lowest word in this thread's stack. NULL means
854 not allocated yet.
855 */
856 Addr stack_base;
sewardj2e93c502002-04-12 11:12:52 +0000857
thughesdaa34562004-06-27 12:48:53 +0000858 /* The allocated size of this thread's stack's guard area (permanently
859 zero if this is ThreadId == 0, since we didn't allocate its stack) */
860 UInt stack_guard_size;
861
sewardj92a59562002-09-30 00:53:10 +0000862 /* Address of the highest legitimate word in this stack. This is
863 used for error messages only -- not critical for execution
864 correctness. Is is set for all stacks, specifically including
865 ThreadId == 0 (the main thread). */
njn25e49d8e72002-09-23 09:36:25 +0000866 Addr stack_highest_word;
867
fitzhardinge98c4dc02004-03-16 08:27:29 +0000868 /* Alternate signal stack */
869 vki_kstack_t altstack;
870
nethercotec06e2132004-09-03 13:45:29 +0000871 /* Architecture-specific thread state */
872 arch_thread_t arch;
njn72718642003-07-24 08:45:32 +0000873}
874ThreadState;
sewardj2e93c502002-04-12 11:12:52 +0000875
876
sewardj018f7622002-05-15 21:13:39 +0000877/* The thread table. */
878extern ThreadState VG_(threads)[VG_N_THREADS];
879
880/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000881extern Bool VG_(is_valid_tid) ( ThreadId tid );
882
njn72718642003-07-24 08:45:32 +0000883/* Determine if 'tid' is that of the current running thread (Nb: returns
884 False if no thread is currently running. */
885extern Bool VG_(is_running_thread)(ThreadId tid);
886
jsgf855d93d2003-10-13 22:26:55 +0000887/* Get the ThreadState for a particular thread */
888extern ThreadState *VG_(get_ThreadState)(ThreadId tid);
889
sewardj1e8cdc92002-04-18 11:37:52 +0000890/* Similarly ... */
891extern ThreadId VG_(get_current_tid) ( void );
892
sewardjccef2e62002-05-29 19:26:32 +0000893/* Nuke all threads except tid. */
894extern void VG_(nuke_all_threads_except) ( ThreadId me );
895
jsgf855d93d2003-10-13 22:26:55 +0000896/* Give a hint to the scheduler that it may be a good time to find a
897 new runnable thread. If prefer_sched != VG_INVALID_THREADID, then
898 try to schedule that thread.
899*/
900extern void VG_(need_resched) ( ThreadId prefer_sched );
901
sewardj2e93c502002-04-12 11:12:52 +0000902/* Return codes from the scheduler. */
903typedef
sewardj7e87e382002-05-03 19:09:05 +0000904 enum {
905 VgSrc_Deadlock, /* no runnable threads and no prospect of any
906 even if we wait for a long time */
907 VgSrc_ExitSyscall, /* client called exit(). This is the normal
908 route out. */
jsgf855d93d2003-10-13 22:26:55 +0000909 VgSrc_FatalSig /* Killed by the default action of a fatal
910 signal */
sewardj7e87e382002-05-03 19:09:05 +0000911 }
sewardj2e93c502002-04-12 11:12:52 +0000912 VgSchedReturnCode;
913
sewardj7e87e382002-05-03 19:09:05 +0000914
nethercote238a3c32004-08-09 13:13:31 +0000915// The scheduler. 'fatal_sigNo' is only set if VgSrc_FatalSig is returned.
916extern VgSchedReturnCode VG_(scheduler)
917 ( Int* exit_code, ThreadId* last_run_thread, Int* fatal_sigNo );
sewardj2e93c502002-04-12 11:12:52 +0000918
919extern void VG_(scheduler_init) ( void );
920
sewardj15a43e12002-04-17 19:35:12 +0000921extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000922
nethercote75d26242004-08-01 22:59:18 +0000923// Longjmp back to the scheduler and thus enter the sighandler immediately.
924extern void VG_(resume_scheduler) ( Int sigNo, vki_ksiginfo_t *info );
sewardj2e93c502002-04-12 11:12:52 +0000925
nethercote238a3c32004-08-09 13:13:31 +0000926// Longjmp, ending the scheduler, when a fatal signal occurs in the client.
927extern void VG_(scheduler_handle_fatal_signal)( Int sigNo );
928
sewardj2e93c502002-04-12 11:12:52 +0000929/* The red-zone size which we put at the bottom (highest address) of
930 thread stacks, for paranoia reasons. This can be arbitrary, and
931 doesn't really need to be set at compile time. */
nethercote6e4f9dc2004-07-30 23:36:37 +0000932#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB 16
sewardj2e93c502002-04-12 11:12:52 +0000933
njnd3040452003-05-19 15:04:06 +0000934/* Write a value to a client's thread register, and shadow (if necessary) */
935#define SET_THREAD_REG( zztid, zzval, zzreg, zzREG, zzevent, zzargs... ) \
nethercotec06e2132004-09-03 13:45:29 +0000936 do { VG_(threads)[zztid].arch.m_##zzreg = (zzval); \
njnd3040452003-05-19 15:04:06 +0000937 VG_TRACK( zzevent, zztid, R_##zzREG, ##zzargs ); \
sewardj018f7622002-05-15 21:13:39 +0000938 } while (0)
939
njnd3040452003-05-19 15:04:06 +0000940#define SET_SYSCALL_RETVAL(zztid, zzval) \
941 SET_THREAD_REG(zztid, zzval, eax, EAX, post_reg_write_syscall_return)
942
njnd3040452003-05-19 15:04:06 +0000943#define SET_SIGNAL_ESP(zztid, zzval) \
944 SET_THREAD_REG(zztid, zzval, esp, ESP, post_reg_write_deliver_signal)
945
946#define SET_CLREQ_RETVAL(zztid, zzval) \
947 SET_THREAD_REG(zztid, zzval, edx, EDX, post_reg_write_clientreq_return)
948
949#define SET_CLCALL_RETVAL(zztid, zzval, f) \
950 SET_THREAD_REG(zztid, zzval, edx, EDX, post_reg_write_clientcall_return, f)
951
952#define SET_PTHREQ_ESP(zztid, zzval) \
953 SET_THREAD_REG(zztid, zzval, esp, ESP, post_reg_write_pthread_return)
954
955#define SET_PTHREQ_RETVAL(zztid, zzval) \
956 SET_THREAD_REG(zztid, zzval, edx, EDX, post_reg_write_pthread_return)
sewardj018f7622002-05-15 21:13:39 +0000957
sewardj2e93c502002-04-12 11:12:52 +0000958
959/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000960 Exports of vg_signals.c
961 ------------------------------------------------------------------ */
962
jsgf855d93d2003-10-13 22:26:55 +0000963extern Bool VG_(do_signal_routing); /* whether scheduler LWP has to route signals */
964
965/* RT signal allocation */
966extern Int VG_(sig_rtmin);
967extern Int VG_(sig_rtmax);
968extern Int VG_(sig_alloc_rtsig) ( Int high );
969
sewardjde4a1d02002-03-22 01:27:54 +0000970extern void VG_(sigstartup_actions) ( void );
971
jsgf855d93d2003-10-13 22:26:55 +0000972extern void VG_(deliver_signal) ( ThreadId tid, const vki_ksiginfo_t *, Bool async );
sewardjde4a1d02002-03-22 01:27:54 +0000973extern void VG_(unblock_host_signal) ( Int sigNo );
sewardj018f7622002-05-15 21:13:39 +0000974
jsgf855d93d2003-10-13 22:26:55 +0000975extern Bool VG_(is_sig_ign) ( Int sigNo );
976
977/* Route pending signals from the scheduler LWP to the appropriate
978 thread LWP. */
979extern void VG_(route_signals) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000980
981/* Fake system calls for signal handling. */
sewardj2342c972002-05-22 23:34:20 +0000982extern void VG_(do__NR_sigaltstack) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000983extern void VG_(do__NR_sigaction) ( ThreadId tid );
nethercote85cdd342004-08-01 22:36:40 +0000984extern void VG_(do__NR_sigprocmask) ( ThreadId tid, Int how,
sewardj018f7622002-05-15 21:13:39 +0000985 vki_ksigset_t* set,
986 vki_ksigset_t* oldset );
nethercote85cdd342004-08-01 22:36:40 +0000987extern void VG_(do_pthread_sigmask_SCSS_upd) ( ThreadId tid, Int how,
sewardj018f7622002-05-15 21:13:39 +0000988 vki_ksigset_t* set,
989 vki_ksigset_t* oldset );
sewardjefbfcdf2002-06-19 17:35:45 +0000990
sewardj2e93c502002-04-12 11:12:52 +0000991/* Modify the current thread's state once we have detected it is
992 returning from a signal handler. */
sewardj77e466c2002-04-14 02:29:29 +0000993extern Bool VG_(signal_returns) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000994
sewardj2e93c502002-04-12 11:12:52 +0000995/* Handy utilities to block/restore all host signals. */
996extern void VG_(block_all_host_signals)
997 ( /* OUT */ vki_ksigset_t* saved_mask );
sewardj018f7622002-05-15 21:13:39 +0000998extern void VG_(restore_all_host_signals)
sewardj2e93c502002-04-12 11:12:52 +0000999 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +00001000
jsgf855d93d2003-10-13 22:26:55 +00001001extern void VG_(kill_self)(Int sigNo);
1002
fitzhardingef1beb252004-03-16 09:49:08 +00001003/* These function synthesize a fault, as if the running instruction
1004 had had a fault. These functions do not return - they longjmp back
1005 into the scheduler so the signal can be delivered. */
1006extern void VG_(synth_fault) (ThreadId tid);
1007extern void VG_(synth_fault_mapping)(ThreadId tid, Addr addr);
1008extern void VG_(synth_fault_perms) (ThreadId tid, Addr addr);
1009
nethercote759dda32004-08-07 18:16:56 +00001010extern void VG_(get_sigstack_bounds)( Addr* low, Addr* high );
fitzhardingef1beb252004-03-16 09:49:08 +00001011
sewardjde4a1d02002-03-22 01:27:54 +00001012/* ---------------------------------------------------------------------
1013 Exports of vg_mylibc.c
1014 ------------------------------------------------------------------ */
1015
njne427a662002-10-02 11:08:25 +00001016#define vg_assert(expr) \
1017 ((void) ((expr) ? 0 : \
1018 (VG_(core_assert_fail) (VG__STRING(expr), \
1019 __FILE__, __LINE__, \
1020 __PRETTY_FUNCTION__), 0)))
1021__attribute__ ((__noreturn__))
daywalker3222e0a2003-09-18 01:39:50 +00001022extern void VG_(core_assert_fail) ( const Char* expr, const Char* file,
1023 Int line, const Char* fn );
njne427a662002-10-02 11:08:25 +00001024__attribute__ ((__noreturn__))
1025extern void VG_(core_panic) ( Char* str );
sewardjde4a1d02002-03-22 01:27:54 +00001026
nethercote05675c82004-08-04 10:37:49 +00001027/* Tools use VG_(strdup)() which doesn't expose ArenaId */
njn25e49d8e72002-09-23 09:36:25 +00001028extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
sewardjde4a1d02002-03-22 01:27:54 +00001029
njn25e49d8e72002-09-23 09:36:25 +00001030extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
jsgf855d93d2003-10-13 22:26:55 +00001031extern Int VG_(poll)( struct vki_pollfd *, UInt nfds, Int timeout);
sewardj2e93c502002-04-12 11:12:52 +00001032
fitzhardinge98abfc72003-12-16 02:05:15 +00001033/* system/mman.h */
nethercoteb4250ae2004-07-10 16:50:09 +00001034extern void* VG_(mmap)( void* start, UInt length, UInt prot, UInt flags,
1035 UInt sf_flags, UInt fd, UInt offset );
fitzhardinge98abfc72003-12-16 02:05:15 +00001036extern Int VG_(munmap)( void* start, Int length );
1037extern Int VG_(mprotect)( void *start, Int length, UInt prot );
1038
1039
jsgf855d93d2003-10-13 22:26:55 +00001040/* Move an fd into the Valgrind-safe range */
1041Int VG_(safe_fd)(Int oldfd);
1042
sewardj570f8902002-11-03 11:44:36 +00001043extern Int VG_(write_socket)( Int sd, void *msg, Int count );
sewardj73cf3bc2002-11-03 03:20:15 +00001044
1045/* --- Connecting over the network --- */
1046extern Int VG_(connect_via_socket)( UChar* str );
1047
fitzhardinge98abfc72003-12-16 02:05:15 +00001048/* Environment manipulations */
nethercote60a96c52004-08-03 13:08:31 +00001049extern Char **VG_(env_setenv) ( Char ***envp, const Char* varname,
1050 const Char *val );
1051extern void VG_(env_unsetenv) ( Char **env, const Char *varname );
1052extern void VG_(env_remove_valgrind_env_stuff) ( Char** env );
sewardj570f8902002-11-03 11:44:36 +00001053
1054/* ---------------------------------------------------------------------
1055 Exports of vg_message.c
1056 ------------------------------------------------------------------ */
1057
1058/* Low-level -- send bytes directly to the message sink. Do not
1059 use. */
1060extern void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes );
1061
nethercoted1da90e2004-08-03 17:26:39 +00001062// Functions for printing from code within Valgrind, but which runs on the
nethercotec91ce8d2004-08-09 11:15:10 +00001063// sim'd CPU. Defined here because needed for vg_libpthread.c,
1064// vg_replace_malloc.c, plus the rest of the core. The weak attribute
1065// ensures the multiple definitions are not a problem. They must be functions
1066// rather than macros so that va_list can be used.
1067
1068__attribute__((weak))
1069int
1070VALGRIND_INTERNAL_PRINTF(char *format, ...)
1071{
1072 unsigned int _qzz_res = 0;
1073 va_list vargs;
1074 va_start(vargs, format);
1075 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__INTERNAL_PRINTF,
1076 (unsigned int)format, (unsigned int)vargs, 0, 0);
1077 va_end(vargs);
1078 return _qzz_res;
1079}
1080
1081__attribute__((weak))
1082int
1083VALGRIND_INTERNAL_PRINTF_BACKTRACE(char *format, ...)
1084{
1085 unsigned int _qzz_res = 0;
1086 va_list vargs;
1087 va_start(vargs, format);
1088 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, VG_USERREQ__INTERNAL_PRINTF_BACKTRACE,
1089 (unsigned int)format, (unsigned int)vargs, 0, 0);
1090 va_end(vargs);
1091 return _qzz_res;
1092}
1093
nethercoted1da90e2004-08-03 17:26:39 +00001094
sewardjde4a1d02002-03-22 01:27:54 +00001095/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001096 Exports of vg_demangle.c
1097 ------------------------------------------------------------------ */
1098
1099extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
1100
sewardjde4a1d02002-03-22 01:27:54 +00001101/* ---------------------------------------------------------------------
1102 Exports of vg_from_ucode.c
1103 ------------------------------------------------------------------ */
1104
sewardj22854b92002-11-30 14:00:47 +00001105extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes, UShort jumps[VG_MAX_JUMPS] );
sewardjde4a1d02002-03-22 01:27:54 +00001106
njn25e49d8e72002-09-23 09:36:25 +00001107extern void VG_(print_ccall_stats) ( void );
1108extern void VG_(print_UInstr_histogram) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001109
sewardj22854b92002-11-30 14:00:47 +00001110extern void VG_(unchain_jumpsite) ( Addr jumpsite );
1111extern Addr VG_(get_jmp_dest) ( Addr jumpsite );
sewardj22854b92002-11-30 14:00:47 +00001112
sewardjde4a1d02002-03-22 01:27:54 +00001113/* ---------------------------------------------------------------------
1114 Exports of vg_to_ucode.c
1115 ------------------------------------------------------------------ */
1116
fitzhardingec2dbbac2004-01-23 23:09:01 +00001117Bool VG_(cpu_has_feature)(UInt feat);
1118
sewardjde4a1d02002-03-22 01:27:54 +00001119extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
sewardjde4a1d02002-03-22 01:27:54 +00001120
1121/* ---------------------------------------------------------------------
1122 Exports of vg_translate.c
1123 ------------------------------------------------------------------ */
1124
njn810086f2002-11-14 12:42:47 +00001125/* Expandable arrays of uinstrs. */
1126struct _UCodeBlock {
sewardj22854b92002-11-30 14:00:47 +00001127 Addr orig_eip;
njn810086f2002-11-14 12:42:47 +00001128 Int used;
1129 Int size;
1130 UInstr* instrs;
1131 Int nextTemp;
1132};
1133
nethercote59a122d2004-08-03 17:16:51 +00001134extern void VG_(translate) ( ThreadId tid, Addr orig_addr, Bool debugging );
sewardjde4a1d02002-03-22 01:27:54 +00001135
nethercote885dd912004-08-03 23:14:00 +00001136extern void VG_(sanity_check_UInstr) ( UInt n, UInstr* u );
sewardjde4a1d02002-03-22 01:27:54 +00001137
nethercotebee3fd92004-08-02 15:17:43 +00001138extern void VG_(print_reg_alloc_stats) ( void );
sewardjb5ff83e2002-12-01 19:40:49 +00001139
sewardjde4a1d02002-03-22 01:27:54 +00001140/* ---------------------------------------------------------------------
1141 Exports of vg_execontext.c.
1142 ------------------------------------------------------------------ */
1143
1144/* Records the PC and a bit of the call chain. The first 4 %eip
1145 values are used in comparisons do remove duplicate errors, and for
1146 comparing against suppression specifications. The rest are purely
1147 informational (but often important). */
1148
njn25e49d8e72002-09-23 09:36:25 +00001149struct _ExeContext {
1150 struct _ExeContext * next;
1151 /* Variable-length array. The size is VG_(clo_backtrace_size); at
njn6c846552003-09-16 07:41:43 +00001152 least 1, at most VG_DEEPEST_BACKTRACE. [0] is the current %eip,
njn25e49d8e72002-09-23 09:36:25 +00001153 [1] is its caller, [2] is the caller of [1], etc. */
1154 Addr eips[0];
1155};
sewardjde4a1d02002-03-22 01:27:54 +00001156
1157
sewardjde4a1d02002-03-22 01:27:54 +00001158/* Print stats (informational only). */
nethercote3a42fb82004-08-03 18:08:50 +00001159extern void VG_(print_ExeContext_stats) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001160
njn25e49d8e72002-09-23 09:36:25 +00001161/* Like VG_(get_ExeContext), but with a slightly different type */
1162extern ExeContext* VG_(get_ExeContext2) ( Addr eip, Addr ebp,
1163 Addr ebp_min, Addr ebp_max );
sewardjde4a1d02002-03-22 01:27:54 +00001164
1165
1166/* ---------------------------------------------------------------------
1167 Exports of vg_errcontext.c.
1168 ------------------------------------------------------------------ */
1169
njn25e49d8e72002-09-23 09:36:25 +00001170extern void VG_(load_suppressions) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001171
njn25e49d8e72002-09-23 09:36:25 +00001172extern void VG_(record_pthread_error) ( ThreadId tid, Char* msg );
sewardjde4a1d02002-03-22 01:27:54 +00001173
njn25e49d8e72002-09-23 09:36:25 +00001174extern void VG_(show_all_errors) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001175
nethercotef2b11482004-08-02 12:36:01 +00001176extern Bool VG_(is_action_requested) ( Char* action, Bool* clo );
njn43c799e2003-04-08 00:08:52 +00001177
nethercotef2b11482004-08-02 12:36:01 +00001178extern UInt VG_(get_n_errs_found) ( void );
njn47363ab2003-04-21 13:24:40 +00001179
sewardjde4a1d02002-03-22 01:27:54 +00001180/* ---------------------------------------------------------------------
1181 Exports of vg_procselfmaps.c
1182 ------------------------------------------------------------------ */
1183
njnfa1016e2003-09-25 17:54:11 +00001184/* Reads /proc/self/maps into a static buffer which can be parsed by
1185 VG_(parse_procselfmaps)(). */
1186extern void VG_(read_procselfmaps) ( void );
njn3e884182003-04-15 13:03:23 +00001187
1188/* Parses /proc/self/maps, calling `record_mapping' for each entry. If
1189 `read_from_file' is True, /proc/self/maps is read directly, otherwise
1190 it's read from the buffer filled by VG_(read_procselfmaps_contents)(). */
sewardjde4a1d02002-03-22 01:27:54 +00001191extern
njnfa1016e2003-09-25 17:54:11 +00001192void VG_(parse_procselfmaps) (
fitzhardinge98abfc72003-12-16 02:05:15 +00001193 void (*record_mapping)( Addr addr, UInt len, Char rr, Char ww, Char xx,
nethercote85cdd342004-08-01 22:36:40 +00001194 UInt dev, UInt ino, ULong foff,
1195 const UChar *filename ) );
sewardjde4a1d02002-03-22 01:27:54 +00001196
1197
1198/* ---------------------------------------------------------------------
1199 Exports of vg_symtab2.c
1200 ------------------------------------------------------------------ */
1201
fitzhardinge98abfc72003-12-16 02:05:15 +00001202typedef struct _Segment Segment;
1203
1204extern Bool VG_(is_object_file) ( const void *hdr );
njnfa1016e2003-09-25 17:54:11 +00001205extern void VG_(mini_stack_dump) ( Addr eips[], UInt n_eips );
fitzhardinge98abfc72003-12-16 02:05:15 +00001206extern SegInfo * VG_(read_seg_symbols) ( Segment *seg );
fitzhardinge98abfc72003-12-16 02:05:15 +00001207extern void VG_(symtab_incref) ( SegInfo * );
1208extern void VG_(symtab_decref) ( SegInfo *, Addr a, UInt len );
sewardjde4a1d02002-03-22 01:27:54 +00001209
njn25e49d8e72002-09-23 09:36:25 +00001210extern Bool VG_(get_fnname_nodemangle)( Addr a, Char* fnname, Int n_fnname );
sewardj25c7c3a2003-07-10 00:17:58 +00001211
fitzhardinge98abfc72003-12-16 02:05:15 +00001212/* Set up some default redirects */
1213extern void VG_(setup_code_redirect_table) ( void );
sewardj25c7c3a2003-07-10 00:17:58 +00001214
fitzhardinge98abfc72003-12-16 02:05:15 +00001215/* Redirection machinery */
nethercote85cdd342004-08-01 22:36:40 +00001216extern Addr VG_(code_redirect) ( Addr orig );
sewardjde4a1d02002-03-22 01:27:54 +00001217
1218/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001219 Exports of vg_main.c
1220 ------------------------------------------------------------------ */
1221
sewardjb91ae7f2003-04-29 23:50:00 +00001222/* Is this a SSE/SSE2-capable CPU? If so, we had better save/restore
1223 the SSE state all over the place. This is set up very early, in
nethercote1d447092004-02-01 17:29:59 +00001224 main(). We have to determine it early since we can't even
sewardjb91ae7f2003-04-29 23:50:00 +00001225 correctly snapshot the startup machine state without it. */
1226extern Bool VG_(have_ssestate);
1227
sewardj73cf3bc2002-11-03 03:20:15 +00001228/* Tell the logging mechanism whether we are logging to a file
1229 descriptor or a socket descriptor. */
1230extern Bool VG_(logging_to_filedes);
1231
njn25e49d8e72002-09-23 09:36:25 +00001232/* Sanity checks which may be done at any time. The scheduler decides when. */
nethercote885dd912004-08-03 23:14:00 +00001233extern void VG_(sanity_check_general) ( Bool force_expensive );
njn25e49d8e72002-09-23 09:36:25 +00001234
fitzhardinge98abfc72003-12-16 02:05:15 +00001235/* Address space */
1236extern Addr VG_(client_base); /* client address space limits */
1237extern Addr VG_(client_end);
1238extern Addr VG_(client_mapbase); /* base of mappings */
1239extern Addr VG_(clstk_base); /* client stack range */
1240extern Addr VG_(clstk_end);
fitzhardinge92360792003-12-24 10:11:11 +00001241extern Addr VG_(client_trampoline_code);
1242
fitzhardinge98abfc72003-12-16 02:05:15 +00001243extern Addr VG_(brk_base); /* start of brk */
1244extern Addr VG_(brk_limit); /* current brk */
nethercote996901a2004-08-03 13:29:09 +00001245extern Addr VG_(shadow_base); /* tool's shadow memory */
fitzhardinge98abfc72003-12-16 02:05:15 +00001246extern Addr VG_(shadow_end);
1247extern Addr VG_(valgrind_base); /* valgrind's address range */
fitzhardinge98abfc72003-12-16 02:05:15 +00001248extern Addr VG_(valgrind_end);
1249
fitzhardingeb50068f2004-02-24 23:42:55 +00001250extern vki_rlimit VG_(client_rlimit_data); /* client's original rlimit data */
1251
fitzhardingea49f9b52003-12-16 22:26:45 +00001252/* client executable file descriptor */
1253extern Int VG_(clexecfd);
fitzhardinge98abfc72003-12-16 02:05:15 +00001254
nethercotef6a1d502004-08-09 12:21:57 +00001255// Help set up the child used when doing execve() with --trace-children=yes
1256Char* VG_(build_child_VALGRINDCLO) ( Char* exename );
1257Char* VG_(build_child_exename) ( void );
1258
njn9b007f62003-04-07 14:40:25 +00001259/* Determine if %esp adjustment must be noted */
njnf4ce3d32003-02-10 10:17:26 +00001260extern Bool VG_(need_to_handle_esp_assignment) ( void );
1261
sewardjde4a1d02002-03-22 01:27:54 +00001262/* Called when some unhandleable client behaviour is detected.
1263 Prints a msg and aborts. */
njn25e49d8e72002-09-23 09:36:25 +00001264extern void VG_(unimplemented) ( Char* msg )
1265 __attribute__((__noreturn__));
sewardjde4a1d02002-03-22 01:27:54 +00001266
nethercote04d0fbc2004-01-26 16:48:06 +00001267/* Something of a function looking for a home ... start up debugger. */
1268extern void VG_(start_debugger) ( Int tid );
sewardjde4a1d02002-03-22 01:27:54 +00001269
sewardjde4a1d02002-03-22 01:27:54 +00001270/* Counts downwards in vg_run_innerloop. */
1271extern UInt VG_(dispatch_ctr);
1272
sewardjde4a1d02002-03-22 01:27:54 +00001273/* --- Counters, for informational purposes only. --- */
1274
nethercote92e7b7f2004-08-07 17:52:25 +00001275// These counters must be declared here because they're maintained by
1276// vg_dispatch.S.
1277extern UInt VG_(bb_enchain_count); // Counts of chain operations done
1278extern UInt VG_(bb_dechain_count); // Counts of unchain operations done
1279extern UInt VG_(unchained_jumps_done); // Number of unchained jumps performed
sewardj22854b92002-11-30 14:00:47 +00001280
nethercote844e7122004-08-02 15:27:22 +00001281extern void VG_(print_scheduler_stats) ( void );
sewardj2e93c502002-04-12 11:12:52 +00001282
sewardjde4a1d02002-03-22 01:27:54 +00001283/* ---------------------------------------------------------------------
1284 Exports of vg_memory.c
1285 ------------------------------------------------------------------ */
1286
fitzhardinge98abfc72003-12-16 02:05:15 +00001287/* A Segment is mapped piece of client memory. This covers all kinds
1288 of mapped memory (exe, brk, mmap, .so, shm, stack, etc)
1289
1290 We try to encode everything we know about a particular segment here.
1291*/
nethercote85cdd342004-08-01 22:36:40 +00001292#define SF_FIXED (1 << 0) // client asked for MAP_FIXED
1293#define SF_SHARED (1 << 1) // shared
1294#define SF_SHM (1 << 2) // SYSV SHM (also SF_SHARED)
1295#define SF_MMAP (1 << 3) // mmap memory
1296#define SF_FILE (1 << 4) // mapping is backed by a file
1297#define SF_STACK (1 << 5) // is a stack
1298#define SF_GROWDOWN (1 << 6) // segment grows down
1299#define SF_GROWUP (1 << 7) // segment grows up
1300#define SF_EXEC (1 << 8) // segment created by exec
1301#define SF_DYNLIB (1 << 9) // mapped from dynamic library
1302#define SF_NOSYMS (1 << 10) // don't load syms, even if present
1303#define SF_BRK (1 << 11) // brk segment
1304#define SF_CORE (1 << 12) // allocated by core on behalf of the client
1305#define SF_VALGRIND (1 << 13) // a valgrind-internal mapping - not in client
1306#define SF_CODE (1 << 14) // segment contains cached code
fitzhardinge98abfc72003-12-16 02:05:15 +00001307
1308struct _Segment {
1309 UInt prot; /* VKI_PROT_* */
1310 UInt flags; /* SF_* */
1311
1312 Addr addr; /* mapped addr (page aligned) */
1313 UInt len; /* size of mapping (page aligned) */
1314
1315 /* These are valid if (flags & SF_FILE) */
1316 ULong offset; /* file offset */
1317 const Char *filename; /* filename (NULL if unknown) */
1318 UInt dev; /* device */
1319 UInt ino; /* inode */
1320
1321 SegInfo *symtab; /* symbol table */
1322};
1323
1324/* segment mapped from a file descriptor */
1325extern void VG_(map_fd_segment) (Addr addr, UInt len, UInt prot, UInt flags,
1326 Int fd, ULong off, const Char *filename);
1327
1328/* segment mapped from a file */
1329extern void VG_(map_file_segment)(Addr addr, UInt len, UInt prot, UInt flags,
1330 UInt dev, UInt ino, ULong off, const Char *filename);
1331
1332/* simple segment */
1333extern void VG_(map_segment) (Addr addr, UInt len, UInt prot, UInt flags);
1334
1335extern void VG_(unmap_range) (Addr addr, UInt len);
1336extern void VG_(mprotect_range)(Addr addr, UInt len, UInt prot);
1337extern Addr VG_(find_map_space)(Addr base, UInt len, Bool for_client);
1338
1339extern Segment *VG_(find_segment)(Addr a);
fitzhardinged65dcad2004-03-13 02:06:58 +00001340extern Segment *VG_(first_segment)(void);
fitzhardinge98abfc72003-12-16 02:05:15 +00001341extern Segment *VG_(next_segment)(Segment *);
1342
1343extern Bool VG_(seg_contains)(const Segment *s, Addr ptr, UInt size);
1344extern Bool VG_(seg_overlaps)(const Segment *s, Addr ptr, UInt size);
1345
thughes9aaebc32004-07-15 23:13:37 +00001346extern void VG_(pad_address_space)(void);
1347extern void VG_(unpad_address_space)(void);
1348
nethercoteeec46302004-08-23 15:06:23 +00001349extern REGPARM(1)
njnfa1016e2003-09-25 17:54:11 +00001350 void VG_(unknown_esp_update) ( Addr new_ESP );
sewardjde4a1d02002-03-22 01:27:54 +00001351
jsgf855d93d2003-10-13 22:26:55 +00001352/* ---------------------------------------------------------------------
1353 Exports of vg_proxylwp.c
1354 ------------------------------------------------------------------ */
1355
1356/* Issue a syscall for thread tid */
1357extern Int VG_(sys_issue)(int tid);
1358
1359extern void VG_(proxy_init) ( void );
1360extern void VG_(proxy_create) ( ThreadId tid );
1361extern void VG_(proxy_delete) ( ThreadId tid, Bool force );
1362extern void VG_(proxy_results) ( void );
1363extern void VG_(proxy_sendsig) ( ThreadId tid, Int signo );
1364extern void VG_(proxy_setsigmask)(ThreadId tid);
1365extern void VG_(proxy_sigack) ( ThreadId tid, const vki_ksigset_t *);
1366extern void VG_(proxy_abort_syscall) ( ThreadId tid );
1367extern void VG_(proxy_waitsig) ( void );
fitzhardinge31ba9052004-01-16 02:15:23 +00001368extern void VG_(proxy_wait_sys) (ThreadId tid, Bool restart);
jsgf855d93d2003-10-13 22:26:55 +00001369
nethercote85cdd342004-08-01 22:36:40 +00001370extern void VG_(proxy_shutdown) ( void ); // shut down the syscall workers
1371extern Int VG_(proxy_resfd) ( void ); // FD something can select on to know
1372 // a syscall finished
jsgf855d93d2003-10-13 22:26:55 +00001373
1374/* Sanity-check the whole proxy-LWP machinery */
nethercote885dd912004-08-03 23:14:00 +00001375void VG_(sanity_check_proxy)(void);
jsgf855d93d2003-10-13 22:26:55 +00001376
1377/* Send a signal from a thread's proxy to the thread. This longjmps
1378 back into the proxy's main loop, so it doesn't return. */
1379__attribute__ ((__noreturn__))
1380extern void VG_(proxy_handlesig)( const vki_ksiginfo_t *siginfo,
1381 const struct vki_sigcontext *sigcontext );
1382
sewardjde4a1d02002-03-22 01:27:54 +00001383/* ---------------------------------------------------------------------
njn25e49d8e72002-09-23 09:36:25 +00001384 Exports of vg_syscalls.c
sewardjde4a1d02002-03-22 01:27:54 +00001385 ------------------------------------------------------------------ */
1386
fitzhardinge98abfc72003-12-16 02:05:15 +00001387extern Char *VG_(resolve_filename)(Int fd);
njn25e49d8e72002-09-23 09:36:25 +00001388
jsgf855d93d2003-10-13 22:26:55 +00001389extern Bool VG_(pre_syscall) ( ThreadId tid );
fitzhardinge31ba9052004-01-16 02:15:23 +00001390extern void VG_(post_syscall)( ThreadId tid, Bool restart );
sewardjde4a1d02002-03-22 01:27:54 +00001391
1392extern Bool VG_(is_kerror) ( Int res );
1393
jsgf855d93d2003-10-13 22:26:55 +00001394/* Internal atfork handlers */
1395typedef void (*vg_atfork_t)(ThreadId);
1396extern void VG_(atfork)(vg_atfork_t pre, vg_atfork_t parent, vg_atfork_t child);
sewardjde4a1d02002-03-22 01:27:54 +00001397
rjwalshf5f536f2003-11-17 17:45:00 +00001398/* fd leakage calls. */
1399extern void VG_(init_preopened_fds) ( void );
nethercote3a42fb82004-08-03 18:08:50 +00001400extern void VG_(show_open_fds) ( void );
rjwalshf5f536f2003-11-17 17:45:00 +00001401
sewardjde4a1d02002-03-22 01:27:54 +00001402/* ---------------------------------------------------------------------
1403 Exports of vg_transtab.c
1404 ------------------------------------------------------------------ */
1405
njn25e49d8e72002-09-23 09:36:25 +00001406/* The fast-cache for tt-lookup. */
1407extern Addr VG_(tt_fast)[VG_TT_FAST_SIZE];
1408
nethercote92e7b7f2004-08-07 17:52:25 +00001409extern void VG_(init_tt_tc) ( void );
sewardjc0d8f682002-11-30 00:49:43 +00001410extern void VG_(add_to_trans_tab) ( Addr orig_addr, Int orig_size,
sewardj22854b92002-11-30 14:00:47 +00001411 Addr trans_addr, Int trans_size,
1412 UShort jumps[VG_MAX_JUMPS]);
nethercote92e7b7f2004-08-07 17:52:25 +00001413extern Addr VG_(search_transtab) ( Addr original_addr );
sewardj6c3769f2002-11-29 01:02:45 +00001414
nethercote92e7b7f2004-08-07 17:52:25 +00001415extern void VG_(invalidate_translations) ( Addr start, UInt range,
1416 Bool unchain_blocks );
sewardjde4a1d02002-03-22 01:27:54 +00001417
nethercote92e7b7f2004-08-07 17:52:25 +00001418extern void VG_(sanity_check_tt_tc) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001419
nethercote92e7b7f2004-08-07 17:52:25 +00001420extern void VG_(print_tt_tc_stats) ( void );
1421
1422extern Int VG_(get_bbs_translated) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001423
sewardjde4a1d02002-03-22 01:27:54 +00001424/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001425 Exports of vg_syscall.S
1426 ------------------------------------------------------------------ */
1427
jsgf855d93d2003-10-13 22:26:55 +00001428extern Int VG_(do_syscall) ( UInt, ... );
1429extern Int VG_(clone) ( Int (*fn)(void *), void *stack, Int flags, void *arg,
1430 Int *child_tid, Int *parent_tid);
fitzhardinge4f10ada2004-06-03 10:00:42 +00001431extern void VG_(sigreturn)(void);
sewardjde4a1d02002-03-22 01:27:54 +00001432
1433/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001434 Exports of vg_dispatch.S
1435 ------------------------------------------------------------------ */
1436
sewardj2e93c502002-04-12 11:12:52 +00001437/* Run a thread for a (very short) while, until some event happens
1438 which means we need to defer to the scheduler. */
1439extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001440
sewardj22854b92002-11-30 14:00:47 +00001441/* The patching routing called when a BB wants to chain itself to
1442 another. */
1443extern UInt VG_(patch_me);
sewardjde4a1d02002-03-22 01:27:54 +00001444
1445/* ---------------------------------------------------------------------
1446 Exports of vg_helpers.S
1447 ------------------------------------------------------------------ */
1448
sewardjde4a1d02002-03-22 01:27:54 +00001449/* Mul, div, etc, -- we don't codegen these directly. */
1450extern void VG_(helper_idiv_64_32);
1451extern void VG_(helper_div_64_32);
1452extern void VG_(helper_idiv_32_16);
1453extern void VG_(helper_div_32_16);
1454extern void VG_(helper_idiv_16_8);
1455extern void VG_(helper_div_16_8);
1456
1457extern void VG_(helper_imul_32_64);
1458extern void VG_(helper_mul_32_64);
1459extern void VG_(helper_imul_16_32);
1460extern void VG_(helper_mul_16_32);
1461extern void VG_(helper_imul_8_16);
1462extern void VG_(helper_mul_8_16);
1463
1464extern void VG_(helper_CLD);
1465extern void VG_(helper_STD);
1466extern void VG_(helper_get_dirflag);
1467
sewardj7d78e782002-06-02 00:04:00 +00001468extern void VG_(helper_CLC);
1469extern void VG_(helper_STC);
nethercote1018bdd2004-02-11 23:33:29 +00001470extern void VG_(helper_CMC);
sewardj7d78e782002-06-02 00:04:00 +00001471
sewardjde4a1d02002-03-22 01:27:54 +00001472extern void VG_(helper_shldl);
1473extern void VG_(helper_shldw);
1474extern void VG_(helper_shrdl);
1475extern void VG_(helper_shrdw);
1476
daywalkerb18d2532003-09-27 20:15:01 +00001477extern void VG_(helper_IN);
1478extern void VG_(helper_OUT);
1479
sewardjde4a1d02002-03-22 01:27:54 +00001480extern void VG_(helper_RDTSC);
1481extern void VG_(helper_CPUID);
1482
nethercote1018bdd2004-02-11 23:33:29 +00001483extern void VG_(helper_bsfw);
1484extern void VG_(helper_bsfl);
1485extern void VG_(helper_bsrw);
1486extern void VG_(helper_bsrl);
sewardjde4a1d02002-03-22 01:27:54 +00001487
1488extern void VG_(helper_fstsw_AX);
1489extern void VG_(helper_SAHF);
njnd6251f12003-06-03 13:38:51 +00001490extern void VG_(helper_LAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001491extern void VG_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001492extern void VG_(helper_DAA);
nethercote1018bdd2004-02-11 23:33:29 +00001493extern void VG_(helper_AAS);
1494extern void VG_(helper_AAA);
1495extern void VG_(helper_AAD);
1496extern void VG_(helper_AAM);
sewardjde4a1d02002-03-22 01:27:54 +00001497
muellerf217c732004-01-02 22:42:29 +00001498extern void VG_(helper_cmpxchg8b);
1499
sewardj51096432002-12-14 23:59:09 +00001500extern void VG_(helper_undefined_instruction);
1501
fitzhardinge92360792003-12-24 10:11:11 +00001502/* Information about trampoline code (for signal return and syscalls) */
1503extern const Char VG_(trampoline_code_start);
1504extern const Int VG_(trampoline_code_length);
1505extern const Int VG_(tramp_sigreturn_offset);
1506extern const Int VG_(tramp_syscall_offset);
sewardj20917d82002-05-28 01:36:45 +00001507
njn4f9c9342002-04-29 16:03:24 +00001508/* ---------------------------------------------------------------------
nethercote996901a2004-08-03 13:29:09 +00001509 Things relating to the used tool
njn4f9c9342002-04-29 16:03:24 +00001510 ------------------------------------------------------------------ */
1511
fitzhardinge98abfc72003-12-16 02:05:15 +00001512#define VG_TRACK(fn, args...) \
1513 do { \
1514 if (VG_(defined_##fn)()) \
1515 SK_(fn)(args); \
1516 } while(0)
sewardj18d75132002-05-16 11:06:21 +00001517
fitzhardinge98abfc72003-12-16 02:05:15 +00001518__attribute__ ((noreturn))
1519extern void VG_(missing_tool_func) ( const Char* fn );
sewardj18d75132002-05-16 11:06:21 +00001520
sewardjde4a1d02002-03-22 01:27:54 +00001521/* ---------------------------------------------------------------------
1522 The state of the simulated CPU.
1523 ------------------------------------------------------------------ */
1524
sewardjde4a1d02002-03-22 01:27:54 +00001525/* ---------------------------------------------------------------------
1526 Offsets into baseBlock for everything which needs to referred to
1527 from generated code. The order of these decls does not imply
1528 what the order of the actual offsets is. The latter is important
1529 and is set up in vg_main.c.
1530 ------------------------------------------------------------------ */
1531
1532/* An array of words. In generated code, %ebp always points to the
1533 start of this array. Useful stuff, like the simulated CPU state,
1534 and the addresses of helper functions, can then be found by
1535 indexing off %ebp. The following declares variables which, at
1536 startup time, are given values denoting offsets into baseBlock.
1537 These offsets are in *words* from the start of baseBlock. */
1538
sewardjb91ae7f2003-04-29 23:50:00 +00001539#define VG_BASEBLOCK_WORDS 400
sewardjde4a1d02002-03-22 01:27:54 +00001540
1541extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1542
sewardjde4a1d02002-03-22 01:27:54 +00001543/* -----------------------------------------------------
1544 Read-write parts of baseBlock.
1545 -------------------------------------------------- */
1546
1547/* State of the simulated CPU. */
1548extern Int VGOFF_(m_eax);
1549extern Int VGOFF_(m_ecx);
1550extern Int VGOFF_(m_edx);
1551extern Int VGOFF_(m_ebx);
1552extern Int VGOFF_(m_esp);
1553extern Int VGOFF_(m_ebp);
1554extern Int VGOFF_(m_esi);
1555extern Int VGOFF_(m_edi);
1556extern Int VGOFF_(m_eflags);
sewardjb91ae7f2003-04-29 23:50:00 +00001557extern Int VGOFF_(m_ssestate);
sewardjde4a1d02002-03-22 01:27:54 +00001558extern Int VGOFF_(m_eip);
1559
sewardjfa492d42002-12-08 18:20:01 +00001560extern Int VGOFF_(m_dflag); /* D flag is handled specially */
1561
sewardj92a59562002-09-30 00:53:10 +00001562extern Int VGOFF_(m_cs);
1563extern Int VGOFF_(m_ss);
1564extern Int VGOFF_(m_ds);
1565extern Int VGOFF_(m_es);
1566extern Int VGOFF_(m_fs);
1567extern Int VGOFF_(m_gs);
1568
sewardjde4a1d02002-03-22 01:27:54 +00001569/* Reg-alloc spill area (VG_MAX_SPILLSLOTS words long). */
1570extern Int VGOFF_(spillslots);
1571
1572/* Records the valid bits for the 8 integer regs & flags reg. */
1573extern Int VGOFF_(sh_eax);
1574extern Int VGOFF_(sh_ecx);
1575extern Int VGOFF_(sh_edx);
1576extern Int VGOFF_(sh_ebx);
1577extern Int VGOFF_(sh_esp);
1578extern Int VGOFF_(sh_ebp);
1579extern Int VGOFF_(sh_esi);
1580extern Int VGOFF_(sh_edi);
1581extern Int VGOFF_(sh_eflags);
1582
sewardjde4a1d02002-03-22 01:27:54 +00001583/* -----------------------------------------------------
1584 Read-only parts of baseBlock.
1585 -------------------------------------------------- */
1586
sewardj92a59562002-09-30 00:53:10 +00001587/* This thread's LDT pointer. */
1588extern Int VGOFF_(ldt);
1589
fitzhardinge47735af2004-01-21 01:27:27 +00001590/* This thread's TLS pointer. */
nethercoted8510ec2004-08-18 23:11:45 +00001591extern Int VGOFF_(tls_ptr);
fitzhardinge47735af2004-01-21 01:27:27 +00001592
nethercote46063202004-09-02 08:51:43 +00001593/* Nb: Most helper offsets are in include/tool.h, for use by tools */
sewardjde4a1d02002-03-22 01:27:54 +00001594
sewardj51096432002-12-14 23:59:09 +00001595extern Int VGOFF_(helper_undefined_instruction);
1596
nethercotec06e2132004-09-03 13:45:29 +00001597// ---------------------------------------------------------------------
1598// Architecture-specific things defined in eg. x86/*.c
1599// ---------------------------------------------------------------------
1600
1601extern void VGA_(load_state) ( arch_thread_t*, ThreadId tid );
1602extern void VGA_(save_state) ( arch_thread_t*, ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001603
sewardj3b2736a2002-03-24 12:18:35 +00001604
1605/* ---------------------------------------------------------------------
1606 Finally - autoconf-generated settings
1607 ------------------------------------------------------------------ */
1608
1609#include "config.h"
1610
nethercotec06e2132004-09-03 13:45:29 +00001611#endif /* ndef __CORE_H */
1612
sewardjde4a1d02002-03-22 01:27:54 +00001613/*--------------------------------------------------------------------*/
nethercote109d0df2004-09-02 08:10:13 +00001614/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001615/*--------------------------------------------------------------------*/