blob: ca5a1d9c5a7a662e907e86df2e009fbc37cd7f46 [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. ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004/*--- Include no other! ---*/
5/*--- vg_include.h ---*/
6/*--------------------------------------------------------------------*/
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
njn0e1b5142003-04-15 14:58:06 +000012 Copyright (C) 2000-2003 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
33#ifndef __VG_INCLUDE_H
34#define __VG_INCLUDE_H
35
sewardjde4a1d02002-03-22 01:27:54 +000036/* ---------------------------------------------------------------------
sewardj2d94c112002-06-03 01:25:54 +000037 Where to send bug reports to.
38 ------------------------------------------------------------------ */
39
40#define VG_EMAIL_ADDR "jseward@acm.org"
41
42
43/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +000044 Build options and table sizes. You should be able to change these
45 options or sizes, recompile, and still have a working system.
46 ------------------------------------------------------------------ */
47
48#include "vg_constants.h"
49
sewardj4cf05692002-10-27 20:28:29 +000050/* All stuff visible to core and skins goes in vg_skin.h. Things
51 * visible to core but not visible to any skins should go in this
52 * file, vg_include.h. */
njn25e49d8e72002-09-23 09:36:25 +000053#include "vg_skin.h"
sewardjde4a1d02002-03-22 01:27:54 +000054
55/* Total number of spill slots available for allocation, if a TempReg
56 doesn't make it into a RealReg. Just bomb the entire system if
57 this value is too small; we don't expect it will ever get
58 particularly high. */
59#define VG_MAX_SPILLSLOTS 24
60
61
62/* Constants for the slow translation lookup cache. */
63#define VG_TRANSTAB_SLOW_BITS 11
64#define VG_TRANSTAB_SLOW_SIZE (1 << VG_TRANSTAB_SLOW_BITS)
65#define VG_TRANSTAB_SLOW_MASK ((VG_TRANSTAB_SLOW_SIZE) - 1)
66
67/* Size of a buffer used for creating messages. */
68#define M_VG_MSGBUF 10000
69
70/* Size of a smallish table used to read /proc/self/map entries. */
sewardjebc82332002-04-24 14:44:23 +000071#define M_PROCMAP_BUF 50000
sewardjde4a1d02002-03-22 01:27:54 +000072
73/* Max length of pathname to a .so/executable file. */
74#define M_VG_LIBNAMESTR 100
75
76/* Max length of a text fragment used to construct error messages. */
77#define M_VG_ERRTXT 512
78
79/* Max length of the string copied from env var VG_ARGS at startup. */
80#define M_VG_CMDLINE_STRLEN 1000
81
82/* Max number of options for Valgrind which we can handle. */
83#define M_VG_CMDLINE_OPTS 100
84
85/* After this many different unsuppressed errors have been observed,
86 be more conservative about collecting new ones. */
87#define M_VG_COLLECT_ERRORS_SLOWLY_AFTER 50
88
89/* After this many different unsuppressed errors have been observed,
90 stop collecting errors at all, and tell the user their program is
91 evidently a steaming pile of camel dung. */
sewardj1bebcbf2002-04-24 21:24:18 +000092#define M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN 300
sewardjf2537be2002-04-24 21:03:47 +000093
94/* After this many total errors have been observed, stop collecting
95 errors at all. Counterpart to M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN. */
sewardj1bebcbf2002-04-24 21:24:18 +000096#define M_VG_COLLECT_NO_ERRORS_AFTER_FOUND 30000
sewardjde4a1d02002-03-22 01:27:54 +000097
sewardjde4a1d02002-03-22 01:27:54 +000098/* The maximum number of calls we're prepared to save in a
99 backtrace. */
100#define VG_DEEPEST_BACKTRACE 50
101
102/* Number of lists in which we keep track of malloc'd but not free'd
103 blocks. Should be prime. */
104#define VG_N_MALLOCLISTS 997
105
106/* Number of lists in which we keep track of ExeContexts. Should be
107 prime. */
108#define VG_N_EC_LISTS /*997*/ 4999
109
sewardj2e93c502002-04-12 11:12:52 +0000110/* Defines the thread-scheduling timeslice, in terms of the number of
111 basic blocks we attempt to run each thread for. Smaller values
112 give finer interleaving but much increased scheduling overheads. */
sewardj4505b9e2002-05-28 11:27:31 +0000113#define VG_SCHEDULING_QUANTUM 50000
sewardj2e93c502002-04-12 11:12:52 +0000114
sewardj2e93c502002-04-12 11:12:52 +0000115/* Number of file descriptors that can simultaneously be waited on for
116 I/O to complete. Perhaps this should be the same as VG_N_THREADS
117 (surely a thread can't wait on more than one fd at once?. Who
118 knows.) */
njn9a07c222003-06-29 10:18:48 +0000119#define VG_N_WAITING_FDS 20
sewardj2e93c502002-04-12 11:12:52 +0000120
sewardjbf290b92002-05-01 02:28:01 +0000121/* Stack size for a thread. We try and check that they do not go
122 beyond it. */
sewardjf0b06452002-06-04 08:38:04 +0000123#define VG_PTHREAD_STACK_SIZE (1 << 20)
sewardjbf290b92002-05-01 02:28:01 +0000124
sewardj20917d82002-05-28 01:36:45 +0000125/* Number of entries in the semaphore-remapping table. */
126#define VG_N_SEMAPHORES 50
127
128/* Number of entries in the rwlock-remapping table. */
sewardj89745a52002-09-27 01:04:29 +0000129#define VG_N_RWLOCKS 500
sewardj20917d82002-05-28 01:36:45 +0000130
sewardj8ad94e12002-05-29 00:10:20 +0000131/* Number of entries in each thread's cleanup stack. */
sewardj61821c02003-05-04 13:02:10 +0000132#define VG_N_CLEANUPSTACK 16
sewardj8ad94e12002-05-29 00:10:20 +0000133
sewardj2cb00342002-06-28 01:46:26 +0000134/* Number of entries in each thread's fork-handler stack. */
135#define VG_N_FORKHANDLERSTACK 2
136
njn25e49d8e72002-09-23 09:36:25 +0000137/* Max number of callers for context in a suppression. */
138#define VG_N_SUPP_CALLERS 4
sewardj73cf3bc2002-11-03 03:20:15 +0000139
njn6eba4ef2003-05-01 08:06:41 +0000140/* Valgrind's stack sizes, in words */
141#define VG_STACK_SIZE_W 10000
142#define VG_SIGSTACK_SIZE_W 10000
njn12a57142003-04-30 20:49:10 +0000143
sewardjde4a1d02002-03-22 01:27:54 +0000144
145/* ---------------------------------------------------------------------
146 Basic types
147 ------------------------------------------------------------------ */
148
sewardjde4a1d02002-03-22 01:27:54 +0000149/* Just pray that gcc's constant folding works properly ... */
150#define BITS(bit7,bit6,bit5,bit4,bit3,bit2,bit1,bit0) \
151 ( ((bit7) << 7) | ((bit6) << 6) | ((bit5) << 5) | ((bit4) << 4) \
152 | ((bit3) << 3) | ((bit2) << 2) | ((bit1) << 1) | (bit0))
153
sewardjde4a1d02002-03-22 01:27:54 +0000154/* ---------------------------------------------------------------------
155 Command-line-settable options
156 ------------------------------------------------------------------ */
157
sewardj4f094a72002-11-05 23:37:35 +0000158/* Default destination port to be used in logging over a network, if
159 none specified. */
160#define VG_CLO_DEFAULT_LOGPORT 1500
sewardj73cf3bc2002-11-03 03:20:15 +0000161
162/* The max number of suppression files. */
sewardjde4a1d02002-03-22 01:27:54 +0000163#define VG_CLO_MAX_SFILES 10
164
sewardj4cf05692002-10-27 20:28:29 +0000165/* Describes where logging output is to be sent. */
166typedef
167 enum {
168 VgLogTo_Fd,
169 VgLogTo_File,
170 VgLogTo_Socket
171 } VgLogTo;
172
173
sewardj72f98ff2002-06-13 17:23:38 +0000174/* Should we stop collecting errors if too many appear? default: YES */
sewardj2e432902002-06-13 20:44:00 +0000175extern Bool VG_(clo_error_limit);
sewardjde4a1d02002-03-22 01:27:54 +0000176/* Enquire about whether to attach to GDB at errors? default: NO */
177extern Bool VG_(clo_GDB_attach);
njn43c799e2003-04-08 00:08:52 +0000178/* Enquire about generating a suppression for each error? default: NO */
179extern Bool VG_(clo_gen_suppressions);
sewardjde4a1d02002-03-22 01:27:54 +0000180/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
181extern Int VG_(sanity_level);
sewardjde4a1d02002-03-22 01:27:54 +0000182/* Automatically attempt to demangle C++ names? default: YES */
183extern Bool VG_(clo_demangle);
sewardjde4a1d02002-03-22 01:27:54 +0000184/* Simulate child processes? default: NO */
185extern Bool VG_(clo_trace_children);
sewardj4cf05692002-10-27 20:28:29 +0000186
187/* Where logging output is to be sent to.
188
189 When log_to == VgLogTo_Fd, clo_logfile_fd holds the file id, and is
190 taken from the command line. clo_logfile_name is irrelevant.
191
192 When log_to == VgLogTo_File, clo_logfile_name holds the logfile
193 name, and is taken from the command line. clo_logfile_fd is then
194 made to hold the relevant file id, by opening clo_logfile_name
195 (concatenated with the process ID) for writing.
196
197 When log_to == VgLogTo_Socket, clo_logfile_name holds the
198 hostname:portnumber pair, and is taken from the command line.
199 clo_logfile_fd is then made to hold the relevant file handle, by
200 opening a connection to said hostname:portnumber pair.
201
202 Global default is to set log_to == VgLogTo_Fd and logfile_fd == 2
203 (stderr). */
204extern VgLogTo VG_(clo_log_to);
205extern Int VG_(clo_logfile_fd);
206extern Char* VG_(clo_logfile_name);
sewardjde4a1d02002-03-22 01:27:54 +0000207
208/* The number of suppression files specified. */
209extern Int VG_(clo_n_suppressions);
210/* The names of the suppression files. */
211extern Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
212
213/* Single stepping? default: NO */
214extern Bool VG_(clo_single_step);
215/* Code improvement? default: YES */
216extern Bool VG_(clo_optimise);
njn25e49d8e72002-09-23 09:36:25 +0000217/* DEBUG: print generated code? default: 00000 ( == NO ) */
218extern Bool VG_(clo_trace_codegen);
sewardjde4a1d02002-03-22 01:27:54 +0000219/* DEBUG: print system calls? default: NO */
220extern Bool VG_(clo_trace_syscalls);
221/* DEBUG: print signal details? default: NO */
222extern Bool VG_(clo_trace_signals);
223/* DEBUG: print symtab details? default: NO */
224extern Bool VG_(clo_trace_symtab);
sewardj8937c812002-04-12 20:12:20 +0000225/* DEBUG: print thread scheduling events? default: NO */
226extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000227/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
228 (some), 2 (all) */
229extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000230/* Stop after this many basic blocks. default: Infinity. */
231extern ULong VG_(clo_stop_after);
232/* Display gory details for the k'th most popular error. default:
233 Infinity. */
234extern Int VG_(clo_dump_error);
235/* Number of parents of a backtrace. Default: 8. */
236extern Int VG_(clo_backtrace_size);
daywalker7e73e5f2003-07-04 16:18:15 +0000237/* Engage miscellaneous weird hacks needed for some progs. */
sewardj8d365b52002-05-12 10:52:16 +0000238extern Char* VG_(clo_weird_hacks);
sewardj858964b2002-10-05 14:15:43 +0000239/* Should we run __libc_freeres at exit? Sometimes causes crashes.
240 Default: YES. Note this is subservient to VG_(needs).libc_freeres;
241 if the latter says False, then the setting of VG_(clo_weird_hacks)
242 is ignored. Ie if a skin says no, I don't want this to run, that
243 cannot be overridden from the command line. */
244extern Bool VG_(clo_run_libc_freeres);
sewardjb5ff83e2002-12-01 19:40:49 +0000245/* Use the basic-block chaining optimisation? Default: YES */
sewardj22854b92002-11-30 14:00:47 +0000246extern Bool VG_(clo_chain_bb);
sewardjde4a1d02002-03-22 01:27:54 +0000247
248
249/* ---------------------------------------------------------------------
250 Debugging and profiling stuff
251 ------------------------------------------------------------------ */
252
sewardjde4a1d02002-03-22 01:27:54 +0000253/* Create a logfile into which messages can be dumped. */
254extern void VG_(startup_logging) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000255extern void VG_(shutdown_logging)( void );
sewardjde4a1d02002-03-22 01:27:54 +0000256
257extern void VGP_(init_profiling) ( void );
258extern void VGP_(done_profiling) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000259
njn25e49d8e72002-09-23 09:36:25 +0000260#undef VGP_PUSHCC
261#undef VGP_POPCC
262#define VGP_PUSHCC(x) if (VG_(clo_profile)) VGP_(pushcc)(x)
263#define VGP_POPCC(x) if (VG_(clo_profile)) VGP_(popcc)(x)
sewardjde4a1d02002-03-22 01:27:54 +0000264
sewardjde4a1d02002-03-22 01:27:54 +0000265/* ---------------------------------------------------------------------
njn810086f2002-11-14 12:42:47 +0000266 Skin-related types
267 ------------------------------------------------------------------ */
268/* These structs are not exposed to skins to mitigate possibility of
269 binary-incompatibilities when the core/skin interface changes. Instead,
270 set functions are provided (see include/vg_skin.h). */
271typedef
272 struct {
273 Char* name;
274 Char* version;
275 Char* description;
276 Char* copyright_author;
277 Char* bug_reports_to;
njn120281f2003-02-03 12:20:07 +0000278 UInt avg_translation_sizeB;
njn810086f2002-11-14 12:42:47 +0000279 }
280 VgDetails;
281
282extern VgDetails VG_(details);
283
284/* If new fields are added to this type, update:
285 * - vg_main.c:initialisation of VG_(needs)
286 * - vg_main.c:sanity_check_needs()
287 *
288 * If the name of this type or any of its fields change, update:
289 * - dependent comments (just search for "VG_(needs)").
290 */
291typedef
292 struct {
293 Bool libc_freeres;
294 Bool core_errors;
295
296 Bool skin_errors;
297 Bool basic_block_discards;
298 Bool shadow_regs;
299 Bool command_line_options;
300 Bool client_requests;
301 Bool extended_UCode;
302 Bool syscall_wrapper;
njn810086f2002-11-14 12:42:47 +0000303 Bool sanity_checks;
304 Bool data_syms;
305 }
306 VgNeeds;
307
308extern VgNeeds VG_(needs);
309
310/* Events happening in core to track. To be notified, assign a function
311 to the function pointer. To ignore an event, don't do anything
312 (default assignment is to NULL in which case the call is skipped). */
313typedef
314 struct {
315 /* Memory events */
316 void (*new_mem_startup)( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
njn810086f2002-11-14 12:42:47 +0000317 void (*new_mem_stack_signal) ( Addr a, UInt len );
318 void (*new_mem_brk) ( Addr a, UInt len );
319 void (*new_mem_mmap) ( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
320
njn3e884182003-04-15 13:03:23 +0000321 void (*copy_mem_remap) ( Addr from, Addr to, UInt len );
322 void (*change_mem_mprotect) ( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
323 void (*die_mem_stack_signal) ( Addr a, UInt len );
324 void (*die_mem_brk) ( Addr a, UInt len );
325 void (*die_mem_munmap) ( Addr a, UInt len );
326
njn9b007f62003-04-07 14:40:25 +0000327 void (*new_mem_stack_4) ( Addr new_ESP );
328 void (*new_mem_stack_8) ( Addr new_ESP );
329 void (*new_mem_stack_12) ( Addr new_ESP );
330 void (*new_mem_stack_16) ( Addr new_ESP );
331 void (*new_mem_stack_32) ( Addr new_ESP );
332 void (*new_mem_stack) ( Addr a, UInt len );
333
njn9b007f62003-04-07 14:40:25 +0000334 void (*die_mem_stack_4) ( Addr die_ESP );
335 void (*die_mem_stack_8) ( Addr die_ESP );
336 void (*die_mem_stack_12) ( Addr die_ESP );
337 void (*die_mem_stack_16) ( Addr die_ESP );
338 void (*die_mem_stack_32) ( Addr die_ESP );
339 void (*die_mem_stack) ( Addr a, UInt len );
340
njn3e884182003-04-15 13:03:23 +0000341 void (*ban_mem_stack) ( Addr a, UInt len );
njn810086f2002-11-14 12:42:47 +0000342
343 void (*pre_mem_read) ( CorePart part, ThreadState* tst,
344 Char* s, Addr a, UInt size );
345 void (*pre_mem_read_asciiz) ( CorePart part, ThreadState* tst,
346 Char* s, Addr a );
347 void (*pre_mem_write) ( CorePart part, ThreadState* tst,
348 Char* s, Addr a, UInt size );
349 /* Not implemented yet -- have to add in lots of places, which is a
350 pain. Won't bother unless/until there's a need. */
351 /* void (*post_mem_read) ( ThreadState* tst, Char* s,
352 Addr a, UInt size ); */
353 void (*post_mem_write) ( Addr a, UInt size );
354
355
njnd3040452003-05-19 15:04:06 +0000356 /* Register events */
357 void (*post_regs_write_init) ( void );
358 void (*post_reg_write_syscall_return) ( ThreadId tid, UInt reg );
359 void (*post_reg_write_deliver_signal) ( ThreadId tid, UInt reg );
360 void (*post_reg_write_pthread_return) ( ThreadId tid, UInt reg );
361 void (*post_reg_write_clientreq_return) ( ThreadId tid, UInt reg );
362 void (*post_reg_write_clientcall_return) ( ThreadId tid, UInt reg,
363 Addr f );
364
365
njn810086f2002-11-14 12:42:47 +0000366 /* Scheduler events (not exhaustive) */
367 void (*thread_run) ( ThreadId tid );
368
369
370 /* Thread events (not exhaustive) */
371 void (*post_thread_create) ( ThreadId tid, ThreadId child );
372 void (*post_thread_join) ( ThreadId joiner, ThreadId joinee );
373
374
375 /* Mutex events (not exhaustive) */
376 void (*pre_mutex_lock) ( ThreadId tid,
377 void* /*pthread_mutex_t* */ mutex );
378 void (*post_mutex_lock) ( ThreadId tid,
379 void* /*pthread_mutex_t* */ mutex );
380 void (*post_mutex_unlock) ( ThreadId tid,
381 void* /*pthread_mutex_t* */ mutex );
382
njnfdc28af2003-02-24 10:36:48 +0000383 /* Signal events (not exhaustive) */
njn3e884182003-04-15 13:03:23 +0000384 void (* pre_deliver_signal) ( ThreadId tid, Int sigNo, Bool alt_stack );
njnfdc28af2003-02-24 10:36:48 +0000385 void (*post_deliver_signal) ( ThreadId tid, Int sigNo );
386
njn810086f2002-11-14 12:42:47 +0000387
njnfdc28af2003-02-24 10:36:48 +0000388 /* Others... condition variable... */
njn810086f2002-11-14 12:42:47 +0000389 /* ... */
390 }
391 VgTrackEvents;
392
393extern VgTrackEvents VG_(track_events);
394
395
396/* ---------------------------------------------------------------------
397 Exports of vg_needs.c
398 ------------------------------------------------------------------ */
399
400void VG_(sanity_check_needs)(void);
401
402/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000403 Exports of vg_malloc2.c
404 ------------------------------------------------------------------ */
405
406/* Allocation arenas.
njn3e884182003-04-15 13:03:23 +0000407
408 CORE for the core's general use.
409 SKIN for the skin to use (and the only one it uses).
410 SYMTAB for Valgrind's symbol table storage.
411 JITTER for small storage during translation.
412 CLIENT for the client's mallocs/frees, if the skin replaces glibc's
413 malloc() et al -- redzone size is chosen by the skin.
414 DEMANGLE for the C++ demangler.
415 EXECTXT for storing ExeContexts.
416 ERRORS for storing CoreErrors.
417 TRANSIENT for very short-term use. It should be empty in between uses.
418
njn25e49d8e72002-09-23 09:36:25 +0000419 When adding a new arena, remember also to add it to ensure_mm_init().
sewardjde4a1d02002-03-22 01:27:54 +0000420*/
421typedef Int ArenaId;
422
njn3e884182003-04-15 13:03:23 +0000423#define VG_N_ARENAS 9
sewardjde4a1d02002-03-22 01:27:54 +0000424
njn3e884182003-04-15 13:03:23 +0000425#define VG_AR_CORE 0
426#define VG_AR_SKIN 1
427#define VG_AR_SYMTAB 2
428#define VG_AR_JITTER 3
429#define VG_AR_CLIENT 4
430#define VG_AR_DEMANGLE 5
431#define VG_AR_EXECTXT 6
432#define VG_AR_ERRORS 7
433#define VG_AR_TRANSIENT 8
sewardjde4a1d02002-03-22 01:27:54 +0000434
njn25e49d8e72002-09-23 09:36:25 +0000435extern void* VG_(arena_malloc) ( ArenaId arena, Int nbytes );
436extern void VG_(arena_free) ( ArenaId arena, void* ptr );
njn3e884182003-04-15 13:03:23 +0000437extern void* VG_(arena_calloc) ( ArenaId arena, Int alignment,
438 Int nmemb, Int nbytes );
njn25e49d8e72002-09-23 09:36:25 +0000439extern void* VG_(arena_realloc) ( ArenaId arena, void* ptr, Int alignment,
440 Int size );
441extern void* VG_(arena_malloc_aligned) ( ArenaId aid, Int req_alignB,
sewardjde4a1d02002-03-22 01:27:54 +0000442 Int req_pszB );
443
njn8a6b6c02003-04-22 22:45:55 +0000444extern Int VG_(arena_payload_szB) ( ArenaId aid, void* payload );
445
sewardjde4a1d02002-03-22 01:27:54 +0000446extern void VG_(mallocSanityCheckAll) ( void );
447
448extern void VG_(show_all_arena_stats) ( void );
449extern Bool VG_(is_empty_arena) ( ArenaId aid );
450
451
sewardjde4a1d02002-03-22 01:27:54 +0000452/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000453 Exports of vg_intercept.c
sewardj2e93c502002-04-12 11:12:52 +0000454 ------------------------------------------------------------------ */
455
456/* This doesn't export code or data that valgrind.so needs to link
457 against. However, the scheduler does need to know the following
458 request codes. A few, publically-visible, request codes are also
njn25e49d8e72002-09-23 09:36:25 +0000459 defined in valgrind.h, and similar headers for some skins. */
sewardj2e93c502002-04-12 11:12:52 +0000460
njn4c791212003-05-02 17:53:54 +0000461#define VG_USERREQ__MALLOC 0x2001
462#define VG_USERREQ__FREE 0x2002
463
464/*
465In vg_skin.h, so skins can use it.
466Call an arbitrary function with ThreadState as the first arg.
467#define VG_USERREQ__CLIENT_tstCALL0 0x2101
468#define VG_USERREQ__CLIENT_tstCALL1 0x2102
469#define VG_USERREQ__CLIENT_tstCALL2 0x2103
470#define VG_USERREQ__CLIENT_tstCALL3 0x2104
471*/
sewardj2e93c502002-04-12 11:12:52 +0000472
sewardj20917d82002-05-28 01:36:45 +0000473/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn
474 MUST NOT return -- ever. Eventually it will do either __QUIT or
475 __WAIT_JOINER. */
476#define VG_USERREQ__APPLY_IN_NEW_THREAD 0x3001
477
478/* ( no-args ): calling thread disappears from the system forever.
479 Reclaim resources. */
480#define VG_USERREQ__QUIT 0x3002
481
482/* ( void* ): calling thread waits for joiner and returns the void* to
483 it. */
484#define VG_USERREQ__WAIT_JOINER 0x3003
485
486/* ( ThreadId, void** ): wait to join a thread. */
487#define VG_USERREQ__PTHREAD_JOIN 0x3004
488
489/* Set cancellation state and type for this thread. */
490#define VG_USERREQ__SET_CANCELSTATE 0x3005
491#define VG_USERREQ__SET_CANCELTYPE 0x3006
492
493/* ( no-args ): Test if we are at a cancellation point. */
494#define VG_USERREQ__TESTCANCEL 0x3007
495
496/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
497 with this arg to indicate that a cancel is now pending for the
498 specified thread. */
499#define VG_USERREQ__SET_CANCELPEND 0x3008
500
501/* Set/get detach state for this thread. */
502#define VG_USERREQ__SET_OR_GET_DETACH 0x3009
503
504#define VG_USERREQ__PTHREAD_GET_THREADID 0x300B
505#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x300C
506#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK 0x300D
507#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x300E
508#define VG_USERREQ__PTHREAD_COND_WAIT 0x300F
509#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT 0x3010
510#define VG_USERREQ__PTHREAD_COND_SIGNAL 0x3011
511#define VG_USERREQ__PTHREAD_COND_BROADCAST 0x3012
512#define VG_USERREQ__PTHREAD_KEY_CREATE 0x3013
513#define VG_USERREQ__PTHREAD_KEY_DELETE 0x3014
sewardj00a66b12002-10-12 16:42:35 +0000514#define VG_USERREQ__PTHREAD_SETSPECIFIC_PTR 0x3015
515#define VG_USERREQ__PTHREAD_GETSPECIFIC_PTR 0x3016
sewardj20917d82002-05-28 01:36:45 +0000516#define VG_USERREQ__READ_MILLISECOND_TIMER 0x3017
517#define VG_USERREQ__PTHREAD_SIGMASK 0x3018
518#define VG_USERREQ__SIGWAIT 0x3019
519#define VG_USERREQ__PTHREAD_KILL 0x301A
520#define VG_USERREQ__PTHREAD_YIELD 0x301B
sewardj00a66b12002-10-12 16:42:35 +0000521#define VG_USERREQ__PTHREAD_KEY_VALIDATE 0x301C
sewardj2e93c502002-04-12 11:12:52 +0000522
sewardj8ad94e12002-05-29 00:10:20 +0000523#define VG_USERREQ__CLEANUP_PUSH 0x3020
524#define VG_USERREQ__CLEANUP_POP 0x3021
sewardj870497a2002-05-29 01:06:47 +0000525#define VG_USERREQ__GET_KEY_D_AND_S 0x3022
sewardj8ad94e12002-05-29 00:10:20 +0000526
sewardjef037c72002-05-30 00:40:03 +0000527#define VG_USERREQ__NUKE_OTHER_THREADS 0x3023
sewardjefbfcdf2002-06-19 17:35:45 +0000528
529/* Ask how many signal handler returns have happened to this
530 thread. */
sewardj9a2224b2002-06-19 10:17:40 +0000531#define VG_USERREQ__GET_N_SIGS_RETURNED 0x3024
sewardjef037c72002-05-30 00:40:03 +0000532
sewardj2cb00342002-06-28 01:46:26 +0000533/* Get/set entries for a thread's pthread_atfork stack. */
534#define VG_USERREQ__SET_FHSTACK_USED 0x3025
535#define VG_USERREQ__GET_FHSTACK_USED 0x3026
536#define VG_USERREQ__SET_FHSTACK_ENTRY 0x3027
537#define VG_USERREQ__GET_FHSTACK_ENTRY 0x3028
sewardjefbfcdf2002-06-19 17:35:45 +0000538
sewardj1fe7b002002-07-16 01:43:15 +0000539/* Denote the finish of VG_(__libc_freeres_wrapper). */
540#define VG_USERREQ__LIBC_FREERES_DONE 0x3029
541
sewardj45b4b372002-04-16 22:50:32 +0000542/* Cosmetic ... */
543#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
sewardj4dced352002-06-04 22:54:20 +0000544/* Log a pthread error from client-space. Cosmetic. */
545#define VG_USERREQ__PTHREAD_ERROR 0x3102
sewardj45b4b372002-04-16 22:50:32 +0000546
sewardj54cacf02002-04-12 23:24:59 +0000547/*
548In vg_constants.h:
549#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardj54cacf02002-04-12 23:24:59 +0000550*/
551
njn4c791212003-05-02 17:53:54 +0000552
sewardj1fe7b002002-07-16 01:43:15 +0000553/* The scheduler does need to know the address of it so it can be
554 called at program exit. */
555extern void VG_(__libc_freeres_wrapper)( void );
556
sewardj11f0bb42003-04-26 20:11:15 +0000557extern void VGR_(wait_for_fd_to_be_readable_or_erring) ( int fd );
558extern void VGR_(wait_for_fd_to_be_writable_or_erring) ( int fd );
559
560extern int VGR_(recv)(int s, void *buf, unsigned int/*size_t*/ len,
561 int flags);
562
563extern int VGR_(accept)(int s, /*struct sockaddr*/ void *addr,
564 /*socklen_t*/ void *addrlen);
565
566
567extern int VGR_(select) ( int n,
568 /*fd_set*/ void *rfds,
569 /*fd_set*/ void *wfds,
570 /*fd_set*/ void *xfds,
571 /*struct timeval*/ void *timeout );
572
573extern int VGR_(msgsnd)(int msgid,
574 const void *msgp,
575 /*size_t*/ unsigned int msgsz,
576 int msgflg);
577
578extern int VGR_(msgrcv)( int msqid,
579 void* msgp,
580 /*size_t*/ unsigned int msgsz,
581 long msgtyp,
582 int msgflg );
583
584extern int VGR_(readv)(int fd,
585 const /*struct iovec*/ void *iovV,
586 int count);
587
588extern int VGR_(writev)(int fd,
589 const /*struct iovec*/ void *iovV,
590 int count);
sewardj54cacf02002-04-12 23:24:59 +0000591
sewardj2e93c502002-04-12 11:12:52 +0000592/* ---------------------------------------------------------------------
593 Constants pertaining to the simulated CPU state, VG_(baseBlock),
594 which need to go here to avoid ugly circularities.
595 ------------------------------------------------------------------ */
596
sewardjb91ae7f2003-04-29 23:50:00 +0000597/* How big is the saved SSE/SSE2 state? Note that this subsumes the
598 FPU state. On machines without SSE, we just save/restore the FPU
599 state into the first part of this area. */
600/* A general comment about SSE save/restore: It appears that the 7th
601 word (which is the MXCSR) has to be &ed with 0x0000FFBF in order
602 that restoring from it later does not cause a GP fault (which is
603 delivered as a segfault). I guess this will have to be done
604 any time we do fxsave :-( 7th word means word offset 6 or byte
605 offset 24 from the start address of the save area.
606 */
607#define VG_SIZE_OF_SSESTATE 512
sewardj2e93c502002-04-12 11:12:52 +0000608/* ... and in words ... */
sewardjb91ae7f2003-04-29 23:50:00 +0000609#define VG_SIZE_OF_SSESTATE_W ((VG_SIZE_OF_SSESTATE+3)/4)
sewardj2e93c502002-04-12 11:12:52 +0000610
611
612/* ---------------------------------------------------------------------
njn3e884182003-04-15 13:03:23 +0000613 Exports of vg_defaults.c
614 ------------------------------------------------------------------ */
615
616extern Bool VG_(sk_malloc_called_by_scheduler);
617
618
619/* ---------------------------------------------------------------------
sewardj92a59562002-09-30 00:53:10 +0000620 Exports of vg_ldt.c
621 ------------------------------------------------------------------ */
622
623/* This is the hardware-format for a segment descriptor, ie what the
624 x86 actually deals with. It is 8 bytes long. It's ugly. */
625
626typedef struct _LDT_ENTRY {
627 union {
628 struct {
629 UShort LimitLow;
630 UShort BaseLow;
631 unsigned BaseMid : 8;
632 unsigned Type : 5;
633 unsigned Dpl : 2;
634 unsigned Pres : 1;
635 unsigned LimitHi : 4;
636 unsigned Sys : 1;
637 unsigned Reserved_0 : 1;
638 unsigned Default_Big : 1;
639 unsigned Granularity : 1;
640 unsigned BaseHi : 8;
641 } Bits;
642 struct {
643 UInt word1;
644 UInt word2;
645 } Words;
646 }
647 LdtEnt;
648} VgLdtEntry;
649
650/* Maximum number of LDT entries supported (by the x86). */
651#define VG_M_LDT_ENTRIES 8192
652/* The size of each LDT entry == sizeof(VgLdtEntry) */
653#define VG_LDT_ENTRY_SIZE 8
654
655/* Alloc & copy, and dealloc. */
656extern VgLdtEntry*
657 VG_(allocate_LDT_for_thread) ( VgLdtEntry* parent_ldt );
658extern void
659 VG_(deallocate_LDT_for_thread) ( VgLdtEntry* ldt );
660
661/* Simulate the modify_ldt syscall. */
662extern Int VG_(sys_modify_ldt) ( ThreadId tid,
663 Int func, void* ptr, UInt bytecount );
664
sewardje1042472002-09-30 12:33:11 +0000665/* Called from generated code. Given a segment selector and a virtual
666 address, return a linear address, and do limit checks too. */
667extern Addr VG_(do_useseg) ( UInt seg_selector, Addr virtual_addr );
668
sewardj92a59562002-09-30 00:53:10 +0000669
670/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000671 Exports of vg_scheduler.c
672 ------------------------------------------------------------------ */
673
sewardj2e93c502002-04-12 11:12:52 +0000674typedef
675 enum {
676 VgTs_Empty, /* this slot is not in use */
677 VgTs_Runnable, /* waiting to be scheduled */
678 VgTs_WaitJoiner, /* waiting for someone to do join on me */
679 VgTs_WaitJoinee, /* waiting for the thread I did join on */
680 VgTs_WaitFD, /* waiting for I/O completion on a fd */
681 VgTs_WaitMX, /* waiting on a mutex */
sewardj3b5d8862002-04-20 13:53:23 +0000682 VgTs_WaitCV, /* waiting on a condition variable */
sewardjb48e5002002-05-13 00:16:03 +0000683 VgTs_WaitSIG, /* waiting due to sigwait() */
sewardj2e93c502002-04-12 11:12:52 +0000684 VgTs_Sleeping /* sleeping for a while */
685 }
686 ThreadStatus;
sewardj8ad94e12002-05-29 00:10:20 +0000687
688/* An entry in a threads's cleanup stack. */
689typedef
690 struct {
691 void (*fn)(void*);
692 void* arg;
693 }
694 CleanupEntry;
sewardj2cb00342002-06-28 01:46:26 +0000695
696/* An entry in a thread's fork-handler stack. */
697typedef
698 struct {
699 void (*prepare)(void);
700 void (*parent)(void);
701 void (*child)(void);
702 }
703 ForkHandlerEntry;
704
705
njn25e49d8e72002-09-23 09:36:25 +0000706struct _ThreadState {
707 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
708 The thread identity is simply the index in vg_threads[].
709 ThreadId == 1 is the root thread and has the special property
710 that we don't try and allocate or deallocate its stack. For
711 convenience of generating error message, we also put the
712 ThreadId in this tid field, but be aware that it should
713 ALWAYS == the index in vg_threads[]. */
714 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000715
njn25e49d8e72002-09-23 09:36:25 +0000716 /* Current scheduling status.
sewardj5f07b662002-04-23 16:52:51 +0000717
njn25e49d8e72002-09-23 09:36:25 +0000718 Complications: whenever this is set to VgTs_WaitMX, you
719 should also set .m_edx to whatever the required return value
720 is for pthread_mutex_lock / pthread_cond_timedwait for when
721 the mutex finally gets unblocked. */
722 ThreadStatus status;
sewardj2e93c502002-04-12 11:12:52 +0000723
njn25e49d8e72002-09-23 09:36:25 +0000724 /* When .status == WaitMX, points to the mutex I am waiting for.
725 When .status == WaitCV, points to the mutex associated with
726 the condition variable indicated by the .associated_cv field.
727 In all other cases, should be NULL. */
728 void* /*pthread_mutex_t* */ associated_mx;
sewardj3b5d8862002-04-20 13:53:23 +0000729
njn25e49d8e72002-09-23 09:36:25 +0000730 /* When .status == WaitCV, points to the condition variable I am
731 waiting for. In all other cases, should be NULL. */
732 void* /*pthread_cond_t* */ associated_cv;
sewardj2e93c502002-04-12 11:12:52 +0000733
njn25e49d8e72002-09-23 09:36:25 +0000734 /* If VgTs_Sleeping, this is when we should wake up, measured in
735 milliseconds as supplied by VG_(read_millisecond_counter).
sewardj2e93c502002-04-12 11:12:52 +0000736
njn25e49d8e72002-09-23 09:36:25 +0000737 If VgTs_WaitCV, this indicates the time at which
738 pthread_cond_timedwait should wake up. If == 0xFFFFFFFF,
739 this means infinitely far in the future, viz,
740 pthread_cond_wait. */
741 UInt awaken_at;
sewardj20917d82002-05-28 01:36:45 +0000742
njn25e49d8e72002-09-23 09:36:25 +0000743 /* If VgTs_WaitJoiner, return value, as generated by joinees. */
744 void* joinee_retval;
sewardj20917d82002-05-28 01:36:45 +0000745
njn25e49d8e72002-09-23 09:36:25 +0000746 /* If VgTs_WaitJoinee, place to copy the return value to, and
747 the identity of the thread we're waiting for. */
748 void** joiner_thread_return;
749 ThreadId joiner_jee_tid;
sewardj8ad94e12002-05-29 00:10:20 +0000750
njn25e49d8e72002-09-23 09:36:25 +0000751 /* Whether or not detached. */
752 Bool detached;
sewardj20917d82002-05-28 01:36:45 +0000753
njn25e49d8e72002-09-23 09:36:25 +0000754 /* Cancelability state and type. */
755 Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
756 Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
757
758 /* Pointer to fn to call to do cancellation. Indicates whether
759 or not cancellation is pending. If NULL, not pending. Else
760 should be &thread_exit_wrapper(), indicating that
761 cancallation is pending. */
762 void (*cancel_pend)(void*);
sewardj2e93c502002-04-12 11:12:52 +0000763
njn25e49d8e72002-09-23 09:36:25 +0000764 /* The cleanup stack. */
765 Int custack_used;
766 CleanupEntry custack[VG_N_CLEANUPSTACK];
sewardj5f07b662002-04-23 16:52:51 +0000767
sewardj00a66b12002-10-12 16:42:35 +0000768 /* A pointer to the thread's-specific-data. This is handled almost
769 entirely from vg_libpthread.c. We just provide hooks to get and
770 set this ptr. This is either NULL, indicating the thread has
771 read/written none of its specifics so far, OR points to a
772 void*[VG_N_THREAD_KEYS], allocated and deallocated in
773 vg_libpthread.c. */
774 void** specifics_ptr;
sewardjb48e5002002-05-13 00:16:03 +0000775
njn25e49d8e72002-09-23 09:36:25 +0000776 /* This thread's blocked-signals mask. Semantics is that for a
777 signal to be delivered to this thread, the signal must not be
778 blocked by either the process-wide signal mask nor by this
779 one. So, if this thread is prepared to handle any signal that
780 the process as a whole is prepared to handle, this mask should
781 be made empty -- and that it is its default, starting
782 state. */
783 vki_ksigset_t sig_mask;
sewardjb48e5002002-05-13 00:16:03 +0000784
njn25e49d8e72002-09-23 09:36:25 +0000785 /* When not VgTs_WaitSIG, has no meaning. When VgTs_WaitSIG,
786 is the set of signals for which we are sigwait()ing. */
787 vki_ksigset_t sigs_waited_for;
sewardj9a2224b2002-06-19 10:17:40 +0000788
njn25e49d8e72002-09-23 09:36:25 +0000789 /* Counts the number of times a signal handler for this thread
790 has returned. This makes it easy to implement pause(), by
791 polling this value, of course interspersed with nanosleeps,
792 and waiting till it changes. */
793 UInt n_signals_returned;
sewardj2e93c502002-04-12 11:12:52 +0000794
njn25e49d8e72002-09-23 09:36:25 +0000795 /* Stacks. When a thread slot is freed, we don't deallocate its
796 stack; we just leave it lying around for the next use of the
797 slot. If the next use of the slot requires a larger stack,
798 only then is the old one deallocated and a new one
799 allocated.
sewardj2e93c502002-04-12 11:12:52 +0000800
njn25e49d8e72002-09-23 09:36:25 +0000801 For the main thread (threadid == 0), this mechanism doesn't
802 apply. We don't know the size of the stack since we didn't
803 allocate it, and furthermore we never reallocate it. */
sewardj2e93c502002-04-12 11:12:52 +0000804
njn25e49d8e72002-09-23 09:36:25 +0000805 /* The allocated size of this thread's stack (permanently zero
806 if this is ThreadId == 0, since we didn't allocate its stack) */
807 UInt stack_size;
sewardj1e8cdc92002-04-18 11:37:52 +0000808
njn25e49d8e72002-09-23 09:36:25 +0000809 /* Address of the lowest word in this thread's stack. NULL means
810 not allocated yet.
811 */
812 Addr stack_base;
sewardj2e93c502002-04-12 11:12:52 +0000813
sewardj92a59562002-09-30 00:53:10 +0000814 /* Address of the highest legitimate word in this stack. This is
815 used for error messages only -- not critical for execution
816 correctness. Is is set for all stacks, specifically including
817 ThreadId == 0 (the main thread). */
njn25e49d8e72002-09-23 09:36:25 +0000818 Addr stack_highest_word;
819
sewardj92a59562002-09-30 00:53:10 +0000820 /* Pointer to this thread's Local (Segment) Descriptor Table.
821 Starts out as NULL, indicating there is no table, and we hope to
822 keep it that way. If the thread does __NR_modify_ldt to create
823 entries, we allocate a 8192-entry table at that point. This is
824 a straight copy of the Linux kernel's scheme. Don't forget to
825 deallocate this at thread exit. */
826 VgLdtEntry* ldt;
827
828 /* Saved machine context. Note the FPU state, %EIP and segment
829 registers are not shadowed.
830
831 Although the segment registers are 16 bits long, storage
832 management here, in VG_(baseBlock) and in VG_(m_state_static) is
833 simplified if we pretend they are 32 bits. */
834 UInt m_cs;
835 UInt m_ss;
836 UInt m_ds;
837 UInt m_es;
838 UInt m_fs;
839 UInt m_gs;
840
njn25e49d8e72002-09-23 09:36:25 +0000841 UInt m_eax;
842 UInt m_ebx;
843 UInt m_ecx;
844 UInt m_edx;
845 UInt m_esi;
846 UInt m_edi;
847 UInt m_ebp;
848 UInt m_esp;
849 UInt m_eflags;
850 UInt m_eip;
sewardjb91ae7f2003-04-29 23:50:00 +0000851
852 /* The SSE/FPU state. This array does not (necessarily) have the
853 required 16-byte alignment required to get stuff in/out by
854 fxsave/fxrestore. So we have to do it "by hand".
855 */
856 UInt m_sse[VG_SIZE_OF_SSESTATE_W];
njn25e49d8e72002-09-23 09:36:25 +0000857
858 UInt sh_eax;
859 UInt sh_ebx;
860 UInt sh_ecx;
861 UInt sh_edx;
862 UInt sh_esi;
863 UInt sh_edi;
864 UInt sh_ebp;
865 UInt sh_esp;
866 UInt sh_eflags;
867};
sewardj2e93c502002-04-12 11:12:52 +0000868
869
sewardj018f7622002-05-15 21:13:39 +0000870/* The thread table. */
871extern ThreadState VG_(threads)[VG_N_THREADS];
872
873/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000874extern Bool VG_(is_valid_tid) ( ThreadId tid );
875
sewardj018f7622002-05-15 21:13:39 +0000876/* Check that tid is in range. */
877extern Bool VG_(is_valid_or_empty_tid) ( ThreadId tid );
878
sewardj2e93c502002-04-12 11:12:52 +0000879/* Copy the specified thread's state into VG_(baseBlock) in
880 preparation for running it. */
881extern void VG_(load_thread_state)( ThreadId );
882
883/* Save the specified thread's state back in VG_(baseBlock), and fill
884 VG_(baseBlock) with junk, for sanity-check reasons. */
885extern void VG_(save_thread_state)( ThreadId );
886
sewardj1e8cdc92002-04-18 11:37:52 +0000887/* And for the currently running one, if valid. */
888extern ThreadState* VG_(get_current_thread_state) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000889
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
sewardj2e93c502002-04-12 11:12:52 +0000896
897/* Return codes from the scheduler. */
898typedef
sewardj7e87e382002-05-03 19:09:05 +0000899 enum {
900 VgSrc_Deadlock, /* no runnable threads and no prospect of any
901 even if we wait for a long time */
902 VgSrc_ExitSyscall, /* client called exit(). This is the normal
903 route out. */
904 VgSrc_BbsDone /* In a debugging run, the specified number of
905 bbs has been completed. */
906 }
sewardj2e93c502002-04-12 11:12:52 +0000907 VgSchedReturnCode;
908
sewardj7e87e382002-05-03 19:09:05 +0000909
sewardj2e93c502002-04-12 11:12:52 +0000910/* The scheduler. */
911extern VgSchedReturnCode VG_(scheduler) ( void );
912
913extern void VG_(scheduler_init) ( void );
914
sewardj15a43e12002-04-17 19:35:12 +0000915extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000916
917/* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */
918extern jmp_buf VG_(scheduler_jmpbuf);
sewardj872051c2002-07-13 12:12:56 +0000919/* This says whether scheduler_jmpbuf is actually valid. Needed so
920 that our signal handler doesn't longjmp when the buffer isn't
921 actually valid. */
922extern Bool VG_(scheduler_jmpbuf_valid);
sewardj2e93c502002-04-12 11:12:52 +0000923/* ... and if so, here's the signal which caused it to do so. */
924extern Int VG_(longjmpd_on_signal);
925
926
sewardj2e93c502002-04-12 11:12:52 +0000927/* The red-zone size which we put at the bottom (highest address) of
928 thread stacks, for paranoia reasons. This can be arbitrary, and
929 doesn't really need to be set at compile time. */
930#define VG_AR_CLIENT_STACKBASE_REDZONE_SZW 4
931
932#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB \
933 (VG_AR_CLIENT_STACKBASE_REDZONE_SZW * VKI_BYTES_PER_WORD)
934
njn25e49d8e72002-09-23 09:36:25 +0000935/* Junk to fill up a thread's shadow regs with when shadow regs aren't
njnd3040452003-05-19 15:04:06 +0000936 being used. */
njn25e49d8e72002-09-23 09:36:25 +0000937#define VG_UNUSED_SHADOW_REG_VALUE 0x27182818
njnd3040452003-05-19 15:04:06 +0000938/* For sanity checking: if this ends up in a thread's shadow regs when
939 shadow regs aren't being used, something went wrong. */
940#define VG_USED_SHADOW_REG_VALUE 0x31415927
njn25e49d8e72002-09-23 09:36:25 +0000941
njnd3040452003-05-19 15:04:06 +0000942/* Write a value to a client's thread register, and shadow (if necessary) */
943#define SET_THREAD_REG( zztid, zzval, zzreg, zzREG, zzevent, zzargs... ) \
944 do { VG_(threads)[zztid].m_##zzreg = (zzval); \
945 VG_TRACK( zzevent, zztid, R_##zzREG, ##zzargs ); \
sewardj018f7622002-05-15 21:13:39 +0000946 } while (0)
947
njnd3040452003-05-19 15:04:06 +0000948#define SET_SYSCALL_RETVAL(zztid, zzval) \
949 SET_THREAD_REG(zztid, zzval, eax, EAX, post_reg_write_syscall_return)
950
951#define SET_SIGNAL_EDX(zztid, zzval) \
952 SET_THREAD_REG(zztid, zzval, edx, EDX, post_reg_write_deliver_signal)
953
954#define SET_SIGNAL_ESP(zztid, zzval) \
955 SET_THREAD_REG(zztid, zzval, esp, ESP, post_reg_write_deliver_signal)
956
957#define SET_CLREQ_RETVAL(zztid, zzval) \
958 SET_THREAD_REG(zztid, zzval, edx, EDX, post_reg_write_clientreq_return)
959
960#define SET_CLCALL_RETVAL(zztid, zzval, f) \
961 SET_THREAD_REG(zztid, zzval, edx, EDX, post_reg_write_clientcall_return, f)
962
963#define SET_PTHREQ_ESP(zztid, zzval) \
964 SET_THREAD_REG(zztid, zzval, esp, ESP, post_reg_write_pthread_return)
965
966#define SET_PTHREQ_RETVAL(zztid, zzval) \
967 SET_THREAD_REG(zztid, zzval, edx, EDX, post_reg_write_pthread_return)
sewardj018f7622002-05-15 21:13:39 +0000968
sewardj2e93c502002-04-12 11:12:52 +0000969
sewardjd8acdf22002-11-13 21:57:52 +0000970/* This is or'd into a pthread mutex's __m_kind field if it is used
971 before Valgrind is up and running (prehistory). This is used so
972 that if some early code (like the dynamic linker) takes a lock
973 before Valgrind starts and then releases it afterwards, we can work
974 out what's happening. */
975#define VG_PTHREAD_PREHISTORY 0x80000000
976
sewardj2e93c502002-04-12 11:12:52 +0000977/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000978 Exports of vg_signals.c
979 ------------------------------------------------------------------ */
980
sewardjde4a1d02002-03-22 01:27:54 +0000981extern void VG_(sigstartup_actions) ( void );
sewardj839299f2003-06-14 11:57:59 +0000982extern void VG_(sigshutdown_actions) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000983
sewardjb48e5002002-05-13 00:16:03 +0000984extern Bool VG_(deliver_signals) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000985extern void VG_(unblock_host_signal) ( Int sigNo );
sewardj018f7622002-05-15 21:13:39 +0000986extern void VG_(handle_SCSS_change) ( Bool force_update );
987
sewardjde4a1d02002-03-22 01:27:54 +0000988
989/* Fake system calls for signal handling. */
sewardj2342c972002-05-22 23:34:20 +0000990extern void VG_(do__NR_sigaltstack) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000991extern void VG_(do__NR_sigaction) ( ThreadId tid );
sewardj018f7622002-05-15 21:13:39 +0000992extern void VG_(do__NR_sigprocmask) ( ThreadId tid,
993 Int how,
994 vki_ksigset_t* set,
995 vki_ksigset_t* oldset );
996extern void VG_(do_pthread_sigmask_SCSS_upd) ( ThreadId tid,
997 Int how,
998 vki_ksigset_t* set,
999 vki_ksigset_t* oldset );
1000extern void VG_(send_signal_to_thread) ( ThreadId thread,
1001 Int signo );
sewardjde4a1d02002-03-22 01:27:54 +00001002
sewardjefbfcdf2002-06-19 17:35:45 +00001003extern void VG_(do_sigpending) ( ThreadId tid, vki_ksigset_t* set );
1004
1005
sewardj2e93c502002-04-12 11:12:52 +00001006/* Modify the current thread's state once we have detected it is
1007 returning from a signal handler. */
sewardj77e466c2002-04-14 02:29:29 +00001008extern Bool VG_(signal_returns) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +00001009
sewardj2e93c502002-04-12 11:12:52 +00001010/* Handy utilities to block/restore all host signals. */
1011extern void VG_(block_all_host_signals)
1012 ( /* OUT */ vki_ksigset_t* saved_mask );
sewardj018f7622002-05-15 21:13:39 +00001013extern void VG_(restore_all_host_signals)
sewardj2e93c502002-04-12 11:12:52 +00001014 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +00001015
1016/* ---------------------------------------------------------------------
1017 Exports of vg_mylibc.c
1018 ------------------------------------------------------------------ */
1019
njne427a662002-10-02 11:08:25 +00001020#define vg_assert(expr) \
1021 ((void) ((expr) ? 0 : \
1022 (VG_(core_assert_fail) (VG__STRING(expr), \
1023 __FILE__, __LINE__, \
1024 __PRETTY_FUNCTION__), 0)))
1025__attribute__ ((__noreturn__))
1026extern void VG_(core_assert_fail) ( Char* expr, Char* file,
1027 Int line, Char* fn );
1028__attribute__ ((__noreturn__))
1029extern void VG_(core_panic) ( Char* str );
sewardjde4a1d02002-03-22 01:27:54 +00001030
njn25e49d8e72002-09-23 09:36:25 +00001031/* VG_(brk) not public so skins cannot screw with curr_dataseg_end */
1032extern void* VG_(brk) ( void* end_data_segment );
sewardjde4a1d02002-03-22 01:27:54 +00001033
njn25e49d8e72002-09-23 09:36:25 +00001034/* Skins use VG_(strdup)() which doesn't expose ArenaId */
1035extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
sewardjde4a1d02002-03-22 01:27:54 +00001036
njn25e49d8e72002-09-23 09:36:25 +00001037/* Skins shouldn't need these...(?) */
sewardj5f07b662002-04-23 16:52:51 +00001038extern void VG_(start_rdtsc_calibration) ( void );
1039extern void VG_(end_rdtsc_calibration) ( void );
1040extern UInt VG_(read_millisecond_timer) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001041
njn25e49d8e72002-09-23 09:36:25 +00001042extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
sewardj2e93c502002-04-12 11:12:52 +00001043extern Int VG_(select)( Int n,
1044 vki_fd_set* readfds,
1045 vki_fd_set* writefds,
1046 vki_fd_set* exceptfds,
1047 struct vki_timeval * timeout );
1048extern Int VG_(nanosleep)( const struct vki_timespec *req,
1049 struct vki_timespec *rem );
1050
sewardj570f8902002-11-03 11:44:36 +00001051extern Int VG_(write_socket)( Int sd, void *msg, Int count );
sewardj73cf3bc2002-11-03 03:20:15 +00001052
1053/* --- Connecting over the network --- */
1054extern Int VG_(connect_via_socket)( UChar* str );
1055
sewardj570f8902002-11-03 11:44:36 +00001056
1057/* ---------------------------------------------------------------------
1058 Exports of vg_message.c
1059 ------------------------------------------------------------------ */
1060
1061/* Low-level -- send bytes directly to the message sink. Do not
1062 use. */
1063extern void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes );
1064
1065
sewardjde4a1d02002-03-22 01:27:54 +00001066/* ---------------------------------------------------------------------
1067 Definitions for the JITter (vg_translate.c, vg_to_ucode.c,
1068 vg_from_ucode.c).
1069 ------------------------------------------------------------------ */
1070
sewardjde4a1d02002-03-22 01:27:54 +00001071#define VG_IS_FLAG_SUBSET(set1,set2) \
1072 (( ((FlagSet)set1) & ((FlagSet)set2) ) == ((FlagSet)set1) )
1073
1074#define VG_UNION_FLAG_SETS(set1,set2) \
1075 ( ((FlagSet)set1) | ((FlagSet)set2) )
1076
sewardjde4a1d02002-03-22 01:27:54 +00001077/* ---------------------------------------------------------------------
1078 Exports of vg_demangle.c
1079 ------------------------------------------------------------------ */
1080
1081extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
1082
sewardjde4a1d02002-03-22 01:27:54 +00001083/* ---------------------------------------------------------------------
1084 Exports of vg_from_ucode.c
1085 ------------------------------------------------------------------ */
1086
sewardj22854b92002-11-30 14:00:47 +00001087extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes, UShort jumps[VG_MAX_JUMPS] );
sewardjde4a1d02002-03-22 01:27:54 +00001088
njn25e49d8e72002-09-23 09:36:25 +00001089extern void VG_(print_ccall_stats) ( void );
1090extern void VG_(print_UInstr_histogram) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001091
sewardj22854b92002-11-30 14:00:47 +00001092extern void VG_(unchain_jumpsite) ( Addr jumpsite );
1093extern Addr VG_(get_jmp_dest) ( Addr jumpsite );
1094extern Bool VG_(is_unchained_jumpsite) ( Addr jumpsite );
1095extern Bool VG_(is_chained_jumpsite) ( Addr jumpsite );
1096
sewardjde4a1d02002-03-22 01:27:54 +00001097/* ---------------------------------------------------------------------
1098 Exports of vg_to_ucode.c
1099 ------------------------------------------------------------------ */
1100
1101extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
sewardjde4a1d02002-03-22 01:27:54 +00001102
1103/* ---------------------------------------------------------------------
1104 Exports of vg_translate.c
1105 ------------------------------------------------------------------ */
1106
njn810086f2002-11-14 12:42:47 +00001107/* Expandable arrays of uinstrs. */
1108struct _UCodeBlock {
sewardj22854b92002-11-30 14:00:47 +00001109 Addr orig_eip;
njn810086f2002-11-14 12:42:47 +00001110 Int used;
1111 Int size;
1112 UInstr* instrs;
1113 Int nextTemp;
1114};
1115
1116extern UCodeBlock* VG_(alloc_UCodeBlock) ( void );
1117
sewardj1e8cdc92002-04-18 11:37:52 +00001118extern void VG_(translate) ( ThreadState* tst,
1119 Addr orig_addr,
sewardjde4a1d02002-03-22 01:27:54 +00001120 UInt* orig_size,
1121 Addr* trans_addr,
sewardj22854b92002-11-30 14:00:47 +00001122 UInt* trans_size,
1123 UShort jumps[VG_MAX_JUMPS]);
sewardjde4a1d02002-03-22 01:27:54 +00001124
njn25e49d8e72002-09-23 09:36:25 +00001125extern Bool VG_(saneUInstr) ( Bool beforeRA, Bool beforeLiveness,
1126 UInstr* u );
1127extern void VG_(saneUCodeBlock) ( UCodeBlock* cb );
1128extern Bool VG_(saneUCodeBlockCalls) ( UCodeBlock* cb );
sewardjde4a1d02002-03-22 01:27:54 +00001129
sewardjb5ff83e2002-12-01 19:40:49 +00001130
sewardjde4a1d02002-03-22 01:27:54 +00001131/* ---------------------------------------------------------------------
1132 Exports of vg_execontext.c.
1133 ------------------------------------------------------------------ */
1134
1135/* Records the PC and a bit of the call chain. The first 4 %eip
1136 values are used in comparisons do remove duplicate errors, and for
1137 comparing against suppression specifications. The rest are purely
1138 informational (but often important). */
1139
njn25e49d8e72002-09-23 09:36:25 +00001140struct _ExeContext {
1141 struct _ExeContext * next;
1142 /* Variable-length array. The size is VG_(clo_backtrace_size); at
1143 least 2, at most VG_DEEPEST_BACKTRACE. [0] is the current %eip,
1144 [1] is its caller, [2] is the caller of [1], etc. */
1145 Addr eips[0];
1146};
sewardjde4a1d02002-03-22 01:27:54 +00001147
1148
1149/* Initialise the ExeContext storage mechanism. */
1150extern void VG_(init_ExeContext_storage) ( void );
1151
1152/* Print stats (informational only). */
1153extern void VG_(show_ExeContext_stats) ( void );
1154
njn25e49d8e72002-09-23 09:36:25 +00001155/* Like VG_(get_ExeContext), but with a slightly different type */
1156extern ExeContext* VG_(get_ExeContext2) ( Addr eip, Addr ebp,
1157 Addr ebp_min, Addr ebp_max );
sewardjde4a1d02002-03-22 01:27:54 +00001158
1159
1160/* ---------------------------------------------------------------------
1161 Exports of vg_errcontext.c.
1162 ------------------------------------------------------------------ */
1163
njn25e49d8e72002-09-23 09:36:25 +00001164/* Note: it is imperative this doesn't overlap with (0..) at all, as skins
1165 * effectively extend it by defining their own enums in the (0..) range. */
sewardjde4a1d02002-03-22 01:27:54 +00001166typedef
njn25e49d8e72002-09-23 09:36:25 +00001167 enum {
1168 PThreadSupp = -1, /* Matches PThreadErr */
sewardjde4a1d02002-03-22 01:27:54 +00001169 }
njn25e49d8e72002-09-23 09:36:25 +00001170 CoreSuppKind;
1171
1172/* For each caller specified for a suppression, record the nature of
1173 the caller name. Not of interest to skins. */
1174typedef
1175 enum {
1176 ObjName, /* Name is of an shared object file. */
1177 FunName /* Name is of a function. */
1178 }
1179 SuppLocTy;
1180
njn810086f2002-11-14 12:42:47 +00001181/* Suppressions. Skins can get/set skin-relevant parts with functions
1182 declared in include/vg_skin.h. Extensible via the 'extra' field.
1183 Skins can use a normal enum (with element values in the normal range
1184 (0..)) for `skind'. */
1185struct _Supp {
1186 struct _Supp* next;
1187 /* The number of times this error has been suppressed. */
1188 Int count;
1189 /* The name by which the suppression is referred to. */
1190 Char* sname;
1191 /* First two (name of fn where err occurs, and immediate caller)
1192 * are mandatory; extra two are optional. */
1193 SuppLocTy caller_ty[VG_N_SUPP_CALLERS];
1194 Char* caller [VG_N_SUPP_CALLERS];
1195
1196 /* The skin-specific part */
1197 /* What kind of suppression. Must use the range (0..) */
1198 SuppKind skind;
1199 /* String -- use is optional. NULL by default. */
1200 Char* string;
1201 /* Anything else -- use is optional. NULL by default. */
1202 void* extra;
1203};
njn25e49d8e72002-09-23 09:36:25 +00001204
1205/* Note: it is imperative this doesn't overlap with (0..) at all, as skins
1206 * effectively extend it by defining their own enums in the (0..) range. */
1207typedef
1208 enum {
1209 PThreadErr = -1, /* Pthreading error */
1210 }
1211 CoreErrorKind;
1212
njn810086f2002-11-14 12:42:47 +00001213/* Errors. Extensible (via the 'extra' field). Skins can use a normal
1214 enum (with element values in the normal range (0..)) for `ekind'.
1215 Functions for getting/setting the skin-relevant fields are in
1216 include/vg_skin.h.
1217
1218 When errors are found and recorded with VG_(maybe_record_error)(), all
1219 the skin must do is pass in the four parameters; core will
1220 allocate/initialise the error record.
1221*/
1222struct _Error {
1223 struct _Error* next;
1224 /* NULL if unsuppressed; or ptr to suppression record. */
1225 Supp* supp;
1226 Int count;
njn810086f2002-11-14 12:42:47 +00001227 ThreadId tid;
njn810086f2002-11-14 12:42:47 +00001228
1229 /* The skin-specific part */
njnae17bec2003-01-28 19:59:38 +00001230 /* Initialised by core */
1231 ExeContext* where;
njn810086f2002-11-14 12:42:47 +00001232 /* Used by ALL. Must be in the range (0..) */
1233 Int ekind;
1234 /* Used frequently */
1235 Addr addr;
1236 /* Used frequently */
1237 Char* string;
1238 /* For any skin-specific extras */
1239 void* extra;
1240};
sewardjde4a1d02002-03-22 01:27:54 +00001241
1242
njn25e49d8e72002-09-23 09:36:25 +00001243extern void VG_(load_suppressions) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001244
njn25e49d8e72002-09-23 09:36:25 +00001245extern void VG_(record_pthread_error) ( ThreadId tid, Char* msg );
sewardjde4a1d02002-03-22 01:27:54 +00001246
njn25e49d8e72002-09-23 09:36:25 +00001247extern void VG_(show_all_errors) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001248
njn43c799e2003-04-08 00:08:52 +00001249extern Bool VG_(is_action_requested) ( Char* action, Bool* clo );
1250
1251extern void VG_(gen_suppression) ( Error* err );
sewardj99aac972002-12-26 01:53:45 +00001252
njn47363ab2003-04-21 13:24:40 +00001253extern UInt VG_(n_errs_found);
1254
sewardjde4a1d02002-03-22 01:27:54 +00001255/* ---------------------------------------------------------------------
1256 Exports of vg_procselfmaps.c
1257 ------------------------------------------------------------------ */
1258
njn3e884182003-04-15 13:03:23 +00001259/* Reads /proc/self/maps into a static buffer. */
1260void VG_(read_procselfmaps_contents) ( void );
1261
1262/* Parses /proc/self/maps, calling `record_mapping' for each entry. If
1263 `read_from_file' is True, /proc/self/maps is read directly, otherwise
1264 it's read from the buffer filled by VG_(read_procselfmaps_contents)(). */
sewardjde4a1d02002-03-22 01:27:54 +00001265extern
1266void VG_(read_procselfmaps) (
njn3e884182003-04-15 13:03:23 +00001267 void (*record_mapping)( Addr, UInt, Char, Char, Char, UInt, UChar* ),
1268 Bool read_from_file
sewardjde4a1d02002-03-22 01:27:54 +00001269);
1270
1271
1272/* ---------------------------------------------------------------------
1273 Exports of vg_symtab2.c
1274 ------------------------------------------------------------------ */
1275
njn25e49d8e72002-09-23 09:36:25 +00001276extern void VG_(maybe_read_symbols) ( void );
1277extern void VG_(read_symtab_callback) ( Addr start, UInt size,
1278 Char rr, Char ww, Char xx,
1279 UInt foffset, UChar* filename );
1280extern void VG_(maybe_unload_symbols) ( Addr start, UInt length );
sewardjde4a1d02002-03-22 01:27:54 +00001281
njn25e49d8e72002-09-23 09:36:25 +00001282extern Bool VG_(get_fnname_nodemangle)( Addr a, Char* fnname, Int n_fnname );
1283extern void VG_(mini_stack_dump) ( ExeContext* ec );
sewardjde4a1d02002-03-22 01:27:54 +00001284
1285
1286/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001287 Exports of vg_main.c
1288 ------------------------------------------------------------------ */
1289
sewardjb91ae7f2003-04-29 23:50:00 +00001290/* Is this a SSE/SSE2-capable CPU? If so, we had better save/restore
1291 the SSE state all over the place. This is set up very early, in
1292 vg_startup.S. We have to determine it early since we can't even
1293 correctly snapshot the startup machine state without it. */
1294extern Bool VG_(have_ssestate);
1295
sewardj73cf3bc2002-11-03 03:20:15 +00001296/* Tell the logging mechanism whether we are logging to a file
1297 descriptor or a socket descriptor. */
1298extern Bool VG_(logging_to_filedes);
1299
njn25e49d8e72002-09-23 09:36:25 +00001300/* Sanity checks which may be done at any time. The scheduler decides when. */
1301extern void VG_(do_sanity_checks) ( Bool force_expensive );
1302
sewardjde4a1d02002-03-22 01:27:54 +00001303/* A structure used as an intermediary when passing the simulated
1304 CPU's state to some assembly fragments, particularly system calls.
1305 Stuff is copied from baseBlock to here, the assembly magic runs,
sewardjb91ae7f2003-04-29 23:50:00 +00001306 and then the inverse copy is done. Alignment: the SSE state must
1307 be 16-byte aligned. We ask for the whole struct to be 16-byte
1308 aligned, and the SSE state starts at the 6+8+1+1th == 16th word,
1309 so it too must be 16-byte aligned. Consequence: change this struct
1310 only _very carefully_ ! See also above comment re masking MXCSR.
1311*/
1312__attribute__ ((aligned (16)))
sewardj92a59562002-09-30 00:53:10 +00001313extern UInt VG_(m_state_static) [6 /* segment regs, Intel order */
1314 + 8 /* int regs, in Intel order */
sewardjde4a1d02002-03-22 01:27:54 +00001315 + 1 /* %eflags */
1316 + 1 /* %eip */
sewardjb91ae7f2003-04-29 23:50:00 +00001317 + VG_SIZE_OF_SSESTATE_W /* SSE state */
sewardjde4a1d02002-03-22 01:27:54 +00001318 ];
1319
1320/* Handy fns for doing the copy back and forth. */
1321extern void VG_(copy_baseBlock_to_m_state_static) ( void );
1322extern void VG_(copy_m_state_static_to_baseBlock) ( void );
1323
njn9b007f62003-04-07 14:40:25 +00001324/* Determine if %esp adjustment must be noted */
njnf4ce3d32003-02-10 10:17:26 +00001325extern Bool VG_(need_to_handle_esp_assignment) ( void );
1326
sewardjde4a1d02002-03-22 01:27:54 +00001327/* Called when some unhandleable client behaviour is detected.
1328 Prints a msg and aborts. */
njn25e49d8e72002-09-23 09:36:25 +00001329extern void VG_(unimplemented) ( Char* msg )
1330 __attribute__((__noreturn__));
sewardjde4a1d02002-03-22 01:27:54 +00001331
1332/* The stack on which Valgrind runs. We can't use the same stack as the
1333 simulatee -- that's an important design decision. */
njn6eba4ef2003-05-01 08:06:41 +00001334extern UInt VG_(stack)[VG_STACK_SIZE_W];
sewardjde4a1d02002-03-22 01:27:54 +00001335
njn25e49d8e72002-09-23 09:36:25 +00001336/* Similarly, we have to ask for signals to be delivered on an alternative
1337 stack, since it is possible, although unlikely, that we'll have to run
1338 client code from inside the Valgrind-installed signal handler. If this
1339 happens it will be done by vg_deliver_signal_immediately(). */
njn6eba4ef2003-05-01 08:06:41 +00001340extern UInt VG_(sigstack)[VG_SIGSTACK_SIZE_W];
sewardjde4a1d02002-03-22 01:27:54 +00001341
sewardjde4a1d02002-03-22 01:27:54 +00001342/* Holds client's %esp at the point we gained control. From this the
1343 client's argc, argv and envp are deduced. */
1344extern Addr VG_(esp_at_startup);
sewardjde4a1d02002-03-22 01:27:54 +00001345
sewardjd5815ec2003-04-06 12:23:27 +00001346/* Indicates presence, and holds address of client's sysinfo page, a
1347 feature of some modern kernels used to provide vsyscalls, etc. */
1348extern Bool VG_(sysinfo_page_exists);
1349extern Addr VG_(sysinfo_page_addr);
1350
njn25e49d8e72002-09-23 09:36:25 +00001351/* Remove valgrind.so and skin's .so from a LD_PRELOAD=... string so child
1352 processes don't get traced into. Also mess up $libdir/valgrind so that
1353 our libpthread.so disappears from view. */
sewardj3e1eb1f2002-05-18 13:14:17 +00001354void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str,
1355 Char* ld_library_path_str );
sewardjde4a1d02002-03-22 01:27:54 +00001356
1357/* Something of a function looking for a home ... start up GDB. This
1358 is called from VG_(swizzle_esp_then_start_GDB) and so runs on the
1359 *client's* stack. This is necessary to give GDB the illusion that
1360 the client program really was running on the real cpu. */
1361extern void VG_(start_GDB_whilst_on_client_stack) ( void );
1362
njn41557122002-10-14 09:25:37 +00001363/* VG_(bbs_done) in include/vg_skin.h */
1364
sewardjde4a1d02002-03-22 01:27:54 +00001365/* 64-bit counter for the number of bbs to go before a debug exit. */
1366extern ULong VG_(bbs_to_go);
1367
1368/* Counts downwards in vg_run_innerloop. */
1369extern UInt VG_(dispatch_ctr);
1370
sewardjde4a1d02002-03-22 01:27:54 +00001371/* Is the client running on the simulated CPU or the real one? */
1372extern Bool VG_(running_on_simd_CPU); /* Initially False */
1373
sewardj7e87e382002-05-03 19:09:05 +00001374/* This is the ThreadId of the last thread the scheduler ran. */
1375extern ThreadId VG_(last_run_tid);
1376
njn25e49d8e72002-09-23 09:36:25 +00001377/* This is the argument to __NR_exit() supplied by the first thread to
1378 call that syscall. We eventually pass that to __NR_exit() for
1379 real. */
njn633de322003-05-12 20:40:13 +00001380extern Int VG_(exitcode);
njn25e49d8e72002-09-23 09:36:25 +00001381
sewardjde4a1d02002-03-22 01:27:54 +00001382
1383/* --- Counters, for informational purposes only. --- */
1384
1385/* Number of lookups which miss the fast tt helper. */
1386extern UInt VG_(tt_fast_misses);
1387
sewardjc0d8f682002-11-30 00:49:43 +00001388/* Counts for TT/TC informational messages. */
sewardjde4a1d02002-03-22 01:27:54 +00001389
sewardjde4a1d02002-03-22 01:27:54 +00001390/* Number and total o/t size of translations overall. */
1391extern UInt VG_(overall_in_count);
1392extern UInt VG_(overall_in_osize);
1393extern UInt VG_(overall_in_tsize);
1394/* Number and total o/t size of discards overall. */
1395extern UInt VG_(overall_out_count);
1396extern UInt VG_(overall_out_osize);
1397extern UInt VG_(overall_out_tsize);
sewardjc0d8f682002-11-30 00:49:43 +00001398/* The number of discards of TT/TC. */
1399extern UInt VG_(number_of_tc_discards);
sewardj22854b92002-11-30 14:00:47 +00001400/* Counts of chain and unchain operations done. */
1401extern UInt VG_(bb_enchain_count);
1402extern UInt VG_(bb_dechain_count);
1403/* Number of unchained jumps performed. */
1404extern UInt VG_(unchained_jumps_done);
1405
sewardjde4a1d02002-03-22 01:27:54 +00001406
1407/* Counts pertaining to the register allocator. */
1408
1409/* total number of uinstrs input to reg-alloc */
1410extern UInt VG_(uinstrs_prealloc);
1411
1412/* total number of uinstrs added due to spill code */
1413extern UInt VG_(uinstrs_spill);
1414
1415/* number of bbs requiring spill code */
1416extern UInt VG_(translations_needing_spill);
1417
1418/* total of register ranks over all translations */
1419extern UInt VG_(total_reg_rank);
1420
sewardjde4a1d02002-03-22 01:27:54 +00001421/* Counts pertaining to internal sanity checking. */
1422extern UInt VG_(sanity_fast_count);
1423extern UInt VG_(sanity_slow_count);
1424
sewardj2e93c502002-04-12 11:12:52 +00001425/* Counts pertaining to the scheduler. */
1426extern UInt VG_(num_scheduling_events_MINOR);
1427extern UInt VG_(num_scheduling_events_MAJOR);
1428
sewardjfa492d42002-12-08 18:20:01 +00001429/* Insert and extract the D flag from eflags */
1430UInt VG_(insertDflag)(UInt eflags, Int d);
1431Int VG_(extractDflag)(UInt eflags);
sewardjde4a1d02002-03-22 01:27:54 +00001432
sewardj49e630d2003-04-23 21:18:52 +00001433/* start address and size of the initial stack */
1434extern Addr VG_(foundstack_start);
1435extern UInt VG_(foundstack_size);
1436
1437
sewardjde4a1d02002-03-22 01:27:54 +00001438/* ---------------------------------------------------------------------
1439 Exports of vg_memory.c
1440 ------------------------------------------------------------------ */
1441
njn25e49d8e72002-09-23 09:36:25 +00001442extern void VG_(init_memory) ( void );
1443extern void VG_(new_exe_segment) ( Addr a, UInt len );
1444extern void VG_(remove_if_exe_segment) ( Addr a, UInt len );
sewardjde4a1d02002-03-22 01:27:54 +00001445
njn9b007f62003-04-07 14:40:25 +00001446extern __attribute__((regparm(1)))
1447 void VG_(unknown_esp_update) ( Addr new_ESP );
sewardjde4a1d02002-03-22 01:27:54 +00001448
sewardjde4a1d02002-03-22 01:27:54 +00001449/* ---------------------------------------------------------------------
njn25e49d8e72002-09-23 09:36:25 +00001450 Exports of vg_syscalls.c
sewardjde4a1d02002-03-22 01:27:54 +00001451 ------------------------------------------------------------------ */
1452
njn25e49d8e72002-09-23 09:36:25 +00001453extern void VG_(init_dataseg_end_for_brk) ( void );
1454
sewardj2e93c502002-04-12 11:12:52 +00001455extern void VG_(perform_assumed_nonblocking_syscall) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001456
njn25e49d8e72002-09-23 09:36:25 +00001457extern void* VG_(pre_known_blocking_syscall) ( ThreadId tid, Int syscallno );
1458extern void VG_(post_known_blocking_syscall)( ThreadId tid, Int syscallno,
1459 void* pre_res, Int res );
sewardjde4a1d02002-03-22 01:27:54 +00001460
1461extern Bool VG_(is_kerror) ( Int res );
1462
njnd3040452003-05-19 15:04:06 +00001463#define KERNEL_DO_SYSCALL(thread_id, result_lvalue) \
1464 VG_(load_thread_state)(thread_id); \
1465 VG_(copy_baseBlock_to_m_state_static)(); \
1466 VG_(do_syscall)(); \
1467 VG_(copy_m_state_static_to_baseBlock)(); \
1468 VG_(save_thread_state)(thread_id); \
1469 result_lvalue = VG_(threads)[thread_id].m_eax; \
1470 VG_TRACK( post_reg_write_syscall_return, thread_id, R_EAX );
sewardjde4a1d02002-03-22 01:27:54 +00001471
1472
1473/* ---------------------------------------------------------------------
1474 Exports of vg_transtab.c
1475 ------------------------------------------------------------------ */
1476
njn25e49d8e72002-09-23 09:36:25 +00001477/* The fast-cache for tt-lookup. */
1478extern Addr VG_(tt_fast)[VG_TT_FAST_SIZE];
1479
sewardjde4a1d02002-03-22 01:27:54 +00001480extern void VG_(get_tt_tc_used) ( UInt* tt_used, UInt* tc_used );
sewardj6c3769f2002-11-29 01:02:45 +00001481
sewardjc0d8f682002-11-30 00:49:43 +00001482extern void VG_(add_to_trans_tab) ( Addr orig_addr, Int orig_size,
sewardj22854b92002-11-30 14:00:47 +00001483 Addr trans_addr, Int trans_size,
1484 UShort jumps[VG_MAX_JUMPS]);
sewardj6c3769f2002-11-29 01:02:45 +00001485
sewardj97ad5522003-05-04 12:32:56 +00001486extern void VG_(invalidate_translations) ( Addr start, UInt range, Bool unchain_blocks );
sewardjde4a1d02002-03-22 01:27:54 +00001487
sewardj18d75132002-05-16 11:06:21 +00001488extern void VG_(init_tt_tc) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001489
1490extern void VG_(sanity_check_tc_tt) ( void );
1491extern Addr VG_(search_transtab) ( Addr original_addr );
1492
sewardjde4a1d02002-03-22 01:27:54 +00001493
1494
1495/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001496 Exports of vg_syscall.S
1497 ------------------------------------------------------------------ */
1498
1499extern void VG_(do_syscall) ( void );
1500
1501
1502/* ---------------------------------------------------------------------
1503 Exports of vg_startup.S
1504 ------------------------------------------------------------------ */
1505
sewardjde4a1d02002-03-22 01:27:54 +00001506extern void VG_(switch_to_real_CPU) ( void );
1507
sewardj35805422002-04-21 13:05:34 +00001508extern void VG_(swizzle_esp_then_start_GDB) ( Addr m_eip_at_error,
1509 Addr m_esp_at_error,
1510 Addr m_ebp_at_error );
sewardjde4a1d02002-03-22 01:27:54 +00001511
1512
1513/* ---------------------------------------------------------------------
1514 Exports of vg_dispatch.S
1515 ------------------------------------------------------------------ */
1516
sewardj2e93c502002-04-12 11:12:52 +00001517/* Run a thread for a (very short) while, until some event happens
1518 which means we need to defer to the scheduler. */
1519extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001520
sewardj22854b92002-11-30 14:00:47 +00001521/* The patching routing called when a BB wants to chain itself to
1522 another. */
1523extern UInt VG_(patch_me);
sewardjde4a1d02002-03-22 01:27:54 +00001524
1525/* ---------------------------------------------------------------------
1526 Exports of vg_helpers.S
1527 ------------------------------------------------------------------ */
1528
sewardjde4a1d02002-03-22 01:27:54 +00001529/* Mul, div, etc, -- we don't codegen these directly. */
1530extern void VG_(helper_idiv_64_32);
1531extern void VG_(helper_div_64_32);
1532extern void VG_(helper_idiv_32_16);
1533extern void VG_(helper_div_32_16);
1534extern void VG_(helper_idiv_16_8);
1535extern void VG_(helper_div_16_8);
1536
1537extern void VG_(helper_imul_32_64);
1538extern void VG_(helper_mul_32_64);
1539extern void VG_(helper_imul_16_32);
1540extern void VG_(helper_mul_16_32);
1541extern void VG_(helper_imul_8_16);
1542extern void VG_(helper_mul_8_16);
1543
1544extern void VG_(helper_CLD);
1545extern void VG_(helper_STD);
1546extern void VG_(helper_get_dirflag);
1547
sewardj7d78e782002-06-02 00:04:00 +00001548extern void VG_(helper_CLC);
1549extern void VG_(helper_STC);
1550
sewardjde4a1d02002-03-22 01:27:54 +00001551extern void VG_(helper_shldl);
1552extern void VG_(helper_shldw);
1553extern void VG_(helper_shrdl);
1554extern void VG_(helper_shrdw);
1555
1556extern void VG_(helper_RDTSC);
1557extern void VG_(helper_CPUID);
1558
sewardjde4a1d02002-03-22 01:27:54 +00001559extern void VG_(helper_bsf);
1560extern void VG_(helper_bsr);
1561
1562extern void VG_(helper_fstsw_AX);
1563extern void VG_(helper_SAHF);
njnd6251f12003-06-03 13:38:51 +00001564extern void VG_(helper_LAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001565extern void VG_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001566extern void VG_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001567
sewardj51096432002-12-14 23:59:09 +00001568extern void VG_(helper_undefined_instruction);
1569
sewardj20917d82002-05-28 01:36:45 +00001570/* NOT A FUNCTION; this is a bogus RETURN ADDRESS. */
sewardj54cacf02002-04-12 23:24:59 +00001571extern void VG_(signalreturn_bogusRA)( void );
sewardj20917d82002-05-28 01:36:45 +00001572
njn4f9c9342002-04-29 16:03:24 +00001573/* ---------------------------------------------------------------------
njn25e49d8e72002-09-23 09:36:25 +00001574 Things relating to the used skin
njn4f9c9342002-04-29 16:03:24 +00001575 ------------------------------------------------------------------ */
1576
njn25e49d8e72002-09-23 09:36:25 +00001577#define VG_TRACK(fn, args...) \
1578 do { \
1579 if (VG_(track_events).fn) \
1580 VG_(track_events).fn(args); \
1581 } while (0)
sewardj18d75132002-05-16 11:06:21 +00001582
1583
sewardjde4a1d02002-03-22 01:27:54 +00001584/* ---------------------------------------------------------------------
1585 The state of the simulated CPU.
1586 ------------------------------------------------------------------ */
1587
sewardjde4a1d02002-03-22 01:27:54 +00001588/* ---------------------------------------------------------------------
1589 Offsets into baseBlock for everything which needs to referred to
1590 from generated code. The order of these decls does not imply
1591 what the order of the actual offsets is. The latter is important
1592 and is set up in vg_main.c.
1593 ------------------------------------------------------------------ */
1594
1595/* An array of words. In generated code, %ebp always points to the
1596 start of this array. Useful stuff, like the simulated CPU state,
1597 and the addresses of helper functions, can then be found by
1598 indexing off %ebp. The following declares variables which, at
1599 startup time, are given values denoting offsets into baseBlock.
1600 These offsets are in *words* from the start of baseBlock. */
1601
sewardjb91ae7f2003-04-29 23:50:00 +00001602#define VG_BASEBLOCK_WORDS 400
sewardjde4a1d02002-03-22 01:27:54 +00001603
1604extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1605
1606
1607/* -----------------------------------------------------
1608 Read-write parts of baseBlock.
1609 -------------------------------------------------- */
1610
1611/* State of the simulated CPU. */
1612extern Int VGOFF_(m_eax);
1613extern Int VGOFF_(m_ecx);
1614extern Int VGOFF_(m_edx);
1615extern Int VGOFF_(m_ebx);
1616extern Int VGOFF_(m_esp);
1617extern Int VGOFF_(m_ebp);
1618extern Int VGOFF_(m_esi);
1619extern Int VGOFF_(m_edi);
1620extern Int VGOFF_(m_eflags);
sewardjb91ae7f2003-04-29 23:50:00 +00001621extern Int VGOFF_(m_ssestate);
sewardjde4a1d02002-03-22 01:27:54 +00001622extern Int VGOFF_(m_eip);
1623
sewardjfa492d42002-12-08 18:20:01 +00001624extern Int VGOFF_(m_dflag); /* D flag is handled specially */
1625
sewardj92a59562002-09-30 00:53:10 +00001626extern Int VGOFF_(m_cs);
1627extern Int VGOFF_(m_ss);
1628extern Int VGOFF_(m_ds);
1629extern Int VGOFF_(m_es);
1630extern Int VGOFF_(m_fs);
1631extern Int VGOFF_(m_gs);
1632
sewardjde4a1d02002-03-22 01:27:54 +00001633/* Reg-alloc spill area (VG_MAX_SPILLSLOTS words long). */
1634extern Int VGOFF_(spillslots);
1635
1636/* Records the valid bits for the 8 integer regs & flags reg. */
1637extern Int VGOFF_(sh_eax);
1638extern Int VGOFF_(sh_ecx);
1639extern Int VGOFF_(sh_edx);
1640extern Int VGOFF_(sh_ebx);
1641extern Int VGOFF_(sh_esp);
1642extern Int VGOFF_(sh_ebp);
1643extern Int VGOFF_(sh_esi);
1644extern Int VGOFF_(sh_edi);
1645extern Int VGOFF_(sh_eflags);
1646
sewardjde4a1d02002-03-22 01:27:54 +00001647/* -----------------------------------------------------
1648 Read-only parts of baseBlock.
1649 -------------------------------------------------- */
1650
sewardj92a59562002-09-30 00:53:10 +00001651/* This thread's LDT pointer. */
1652extern Int VGOFF_(ldt);
1653
njn211b6ad2003-02-03 12:33:31 +00001654/* Nb: Most helper offsets are in include/vg_skin.h, for use by skins */
sewardjde4a1d02002-03-22 01:27:54 +00001655
sewardj51096432002-12-14 23:59:09 +00001656extern Int VGOFF_(helper_undefined_instruction);
1657
njn25e49d8e72002-09-23 09:36:25 +00001658/* For storing extension-specific helpers, determined at runtime. The addr
1659 * and offset arrays together form a (addr, offset) map that allows a
1660 * helper's baseBlock offset to be computed from its address. It's done
1661 * like this so CCALL_M_Ns and other helper calls can use the function
1662 * address rather than having to much around with offsets. */
1663extern UInt VG_(n_compact_helpers);
1664extern UInt VG_(n_noncompact_helpers);
1665
1666extern Addr VG_(compact_helper_addrs) [];
1667extern Int VG_(compact_helper_offsets)[];
1668
1669extern Addr VG_(noncompact_helper_addrs) [];
1670extern Int VG_(noncompact_helper_offsets)[];
1671
sewardjde4a1d02002-03-22 01:27:54 +00001672#endif /* ndef __VG_INCLUDE_H */
1673
sewardj3b2736a2002-03-24 12:18:35 +00001674
1675/* ---------------------------------------------------------------------
1676 Finally - autoconf-generated settings
1677 ------------------------------------------------------------------ */
1678
1679#include "config.h"
1680
sewardjde4a1d02002-03-22 01:27:54 +00001681/*--------------------------------------------------------------------*/
1682/*--- end vg_include.h ---*/
1683/*--------------------------------------------------------------------*/