blob: b637e4110ebf3d9d3b81bb6644c5d50d631f4a7b [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
12 Copyright (C) 2000-2002 Julian Seward
13 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.) */
119#define VG_N_WAITING_FDS 10
120
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. */
132#define VG_N_CLEANUPSTACK 8
133
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
sewardjde4a1d02002-03-22 01:27:54 +0000140
141/* ---------------------------------------------------------------------
142 Basic types
143 ------------------------------------------------------------------ */
144
sewardjde4a1d02002-03-22 01:27:54 +0000145/* Just pray that gcc's constant folding works properly ... */
146#define BITS(bit7,bit6,bit5,bit4,bit3,bit2,bit1,bit0) \
147 ( ((bit7) << 7) | ((bit6) << 6) | ((bit5) << 5) | ((bit4) << 4) \
148 | ((bit3) << 3) | ((bit2) << 2) | ((bit1) << 1) | (bit0))
149
sewardjde4a1d02002-03-22 01:27:54 +0000150/* ---------------------------------------------------------------------
151 Command-line-settable options
152 ------------------------------------------------------------------ */
153
sewardj4f094a72002-11-05 23:37:35 +0000154/* Default destination port to be used in logging over a network, if
155 none specified. */
156#define VG_CLO_DEFAULT_LOGPORT 1500
sewardj73cf3bc2002-11-03 03:20:15 +0000157
158/* The max number of suppression files. */
sewardjde4a1d02002-03-22 01:27:54 +0000159#define VG_CLO_MAX_SFILES 10
160
sewardj4cf05692002-10-27 20:28:29 +0000161/* Describes where logging output is to be sent. */
162typedef
163 enum {
164 VgLogTo_Fd,
165 VgLogTo_File,
166 VgLogTo_Socket
167 } VgLogTo;
168
169
sewardj72f98ff2002-06-13 17:23:38 +0000170/* Should we stop collecting errors if too many appear? default: YES */
sewardj2e432902002-06-13 20:44:00 +0000171extern Bool VG_(clo_error_limit);
sewardjde4a1d02002-03-22 01:27:54 +0000172/* Enquire about whether to attach to GDB at errors? default: NO */
173extern Bool VG_(clo_GDB_attach);
174/* Sanity-check level: 0 = none, 1 (default), > 1 = expensive. */
175extern Int VG_(sanity_level);
sewardjde4a1d02002-03-22 01:27:54 +0000176/* Automatically attempt to demangle C++ names? default: YES */
177extern Bool VG_(clo_demangle);
sewardjde4a1d02002-03-22 01:27:54 +0000178/* Round malloc sizes upwards to integral number of words? default:
179 NO */
180extern Bool VG_(clo_sloppy_malloc);
sewardj246d4662002-06-14 10:17:05 +0000181/* Minimum alignment in functions that don't specify alignment explicitly.
182 default: 0, i.e. use default of the machine (== 4) */
183extern Int VG_(clo_alignment);
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);
225/* DEBUG: print malloc details? default: NO */
226extern Bool VG_(clo_trace_malloc);
sewardj8937c812002-04-12 20:12:20 +0000227/* DEBUG: print thread scheduling events? default: NO */
228extern Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000229/* DEBUG: print pthread (mutex etc) events? default: 0 (none), 1
230 (some), 2 (all) */
231extern Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000232/* Stop after this many basic blocks. default: Infinity. */
233extern ULong VG_(clo_stop_after);
234/* Display gory details for the k'th most popular error. default:
235 Infinity. */
236extern Int VG_(clo_dump_error);
237/* Number of parents of a backtrace. Default: 8. */
238extern Int VG_(clo_backtrace_size);
sewardj3984b852002-05-12 03:00:17 +0000239/* Engage miscellaneous wierd hacks needed for some progs. */
sewardj8d365b52002-05-12 10:52:16 +0000240extern Char* VG_(clo_weird_hacks);
sewardj858964b2002-10-05 14:15:43 +0000241/* Should we run __libc_freeres at exit? Sometimes causes crashes.
242 Default: YES. Note this is subservient to VG_(needs).libc_freeres;
243 if the latter says False, then the setting of VG_(clo_weird_hacks)
244 is ignored. Ie if a skin says no, I don't want this to run, that
245 cannot be overridden from the command line. */
246extern Bool VG_(clo_run_libc_freeres);
sewardjb5ff83e2002-12-01 19:40:49 +0000247/* Use the basic-block chaining optimisation? Default: YES */
sewardj22854b92002-11-30 14:00:47 +0000248extern Bool VG_(clo_chain_bb);
sewardjb5ff83e2002-12-01 19:40:49 +0000249/* Generate code for fast conditional jumps (not using pushf/popf)?
250 Default: YES */
sewardj2370f3b2002-11-30 15:01:01 +0000251extern Bool VG_(clo_fast_jcc);
sewardjde4a1d02002-03-22 01:27:54 +0000252
253
254/* ---------------------------------------------------------------------
255 Debugging and profiling stuff
256 ------------------------------------------------------------------ */
257
njn25e49d8e72002-09-23 09:36:25 +0000258/* Change to 1 to get more accurate but more expensive core profiling. */
259#if 0
260# define VGP_ACCURATE_PROFILING
261#endif
262
sewardjde4a1d02002-03-22 01:27:54 +0000263/* Create a logfile into which messages can be dumped. */
264extern void VG_(startup_logging) ( void );
njn25e49d8e72002-09-23 09:36:25 +0000265extern void VG_(shutdown_logging)( void );
sewardjde4a1d02002-03-22 01:27:54 +0000266
267extern void VGP_(init_profiling) ( void );
268extern void VGP_(done_profiling) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000269
njn25e49d8e72002-09-23 09:36:25 +0000270#undef VGP_PUSHCC
271#undef VGP_POPCC
272#define VGP_PUSHCC(x) if (VG_(clo_profile)) VGP_(pushcc)(x)
273#define VGP_POPCC(x) if (VG_(clo_profile)) VGP_(popcc)(x)
sewardjde4a1d02002-03-22 01:27:54 +0000274
njn25e49d8e72002-09-23 09:36:25 +0000275/* Use this for ones that happen a lot and thus we don't want to put in
276 all the time, eg. for %esp assignment. */
277#ifdef VGP_ACCURATE_PROFILING
278# define VGP_MAYBE_PUSHCC(x) if (VG_(clo_profile)) VGP_(pushcc)(x)
279# define VGP_MAYBE_POPCC(x) if (VG_(clo_profile)) VGP_(popcc)(x)
sewardjde4a1d02002-03-22 01:27:54 +0000280#else
njn25e49d8e72002-09-23 09:36:25 +0000281# define VGP_MAYBE_PUSHCC(x)
282# define VGP_MAYBE_POPCC(x)
283#endif
sewardjde4a1d02002-03-22 01:27:54 +0000284
285
286/* ---------------------------------------------------------------------
njn810086f2002-11-14 12:42:47 +0000287 Skin-related types
288 ------------------------------------------------------------------ */
289/* These structs are not exposed to skins to mitigate possibility of
290 binary-incompatibilities when the core/skin interface changes. Instead,
291 set functions are provided (see include/vg_skin.h). */
292typedef
293 struct {
294 Char* name;
295 Char* version;
296 Char* description;
297 Char* copyright_author;
298 Char* bug_reports_to;
sewardjc0d8f682002-11-30 00:49:43 +0000299 Int avg_translation_sizeB;
njn810086f2002-11-14 12:42:47 +0000300 }
301 VgDetails;
302
303extern VgDetails VG_(details);
304
305/* If new fields are added to this type, update:
306 * - vg_main.c:initialisation of VG_(needs)
307 * - vg_main.c:sanity_check_needs()
308 *
309 * If the name of this type or any of its fields change, update:
310 * - dependent comments (just search for "VG_(needs)").
311 */
312typedef
313 struct {
314 Bool libc_freeres;
315 Bool core_errors;
316
317 Bool skin_errors;
318 Bool basic_block_discards;
319 Bool shadow_regs;
320 Bool command_line_options;
321 Bool client_requests;
322 Bool extended_UCode;
323 Bool syscall_wrapper;
324 UInt sizeof_shadow_block;
325 Bool alternative_free;
326 Bool sanity_checks;
327 Bool data_syms;
328 }
329 VgNeeds;
330
331extern VgNeeds VG_(needs);
332
333/* Events happening in core to track. To be notified, assign a function
334 to the function pointer. To ignore an event, don't do anything
335 (default assignment is to NULL in which case the call is skipped). */
336typedef
337 struct {
338 /* Memory events */
339 void (*new_mem_startup)( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
340 void (*new_mem_heap) ( Addr a, UInt len, Bool is_inited );
341 void (*new_mem_stack) ( Addr a, UInt len );
342 void (*new_mem_stack_aligned) ( Addr a, UInt len );
343 void (*new_mem_stack_signal) ( Addr a, UInt len );
344 void (*new_mem_brk) ( Addr a, UInt len );
345 void (*new_mem_mmap) ( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
346
347 void (*copy_mem_heap) ( Addr from, Addr to, UInt len );
348 void (*copy_mem_remap) ( Addr from, Addr to, UInt len );
349 void (*change_mem_mprotect) ( Addr a, UInt len, Bool rr, Bool ww, Bool xx );
350
351 /* Used on redzones around malloc'd blocks and at end of stack */
352 void (*ban_mem_heap) ( Addr a, UInt len );
353 void (*ban_mem_stack) ( Addr a, UInt len );
354
355 void (*die_mem_heap) ( Addr a, UInt len );
356 void (*die_mem_stack) ( Addr a, UInt len );
357 void (*die_mem_stack_aligned) ( Addr a, UInt len );
358 void (*die_mem_stack_signal) ( Addr a, UInt len );
359 void (*die_mem_brk) ( Addr a, UInt len );
360 void (*die_mem_munmap) ( Addr a, UInt len );
361
362 void (*bad_free) ( ThreadState* tst, Addr a );
363 void (*mismatched_free) ( ThreadState* tst, Addr a );
364
365 void (*pre_mem_read) ( CorePart part, ThreadState* tst,
366 Char* s, Addr a, UInt size );
367 void (*pre_mem_read_asciiz) ( CorePart part, ThreadState* tst,
368 Char* s, Addr a );
369 void (*pre_mem_write) ( CorePart part, ThreadState* tst,
370 Char* s, Addr a, UInt size );
371 /* Not implemented yet -- have to add in lots of places, which is a
372 pain. Won't bother unless/until there's a need. */
373 /* void (*post_mem_read) ( ThreadState* tst, Char* s,
374 Addr a, UInt size ); */
375 void (*post_mem_write) ( Addr a, UInt size );
376
377
378 /* Scheduler events (not exhaustive) */
379 void (*thread_run) ( ThreadId tid );
380
381
382 /* Thread events (not exhaustive) */
383 void (*post_thread_create) ( ThreadId tid, ThreadId child );
384 void (*post_thread_join) ( ThreadId joiner, ThreadId joinee );
385
386
387 /* Mutex events (not exhaustive) */
388 void (*pre_mutex_lock) ( ThreadId tid,
389 void* /*pthread_mutex_t* */ mutex );
390 void (*post_mutex_lock) ( ThreadId tid,
391 void* /*pthread_mutex_t* */ mutex );
392 void (*post_mutex_unlock) ( ThreadId tid,
393 void* /*pthread_mutex_t* */ mutex );
394
395
396 /* Others... condition variable, signal events... */
397 /* ... */
398 }
399 VgTrackEvents;
400
401extern VgTrackEvents VG_(track_events);
402
403
404/* ---------------------------------------------------------------------
405 Exports of vg_needs.c
406 ------------------------------------------------------------------ */
407
408void VG_(sanity_check_needs)(void);
409
410/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000411 Exports of vg_malloc2.c
412 ------------------------------------------------------------------ */
413
414/* Allocation arenas.
njn25e49d8e72002-09-23 09:36:25 +0000415 CORE is for the core's general use.
416 SKIN is for the skin to use (and the only one it uses).
sewardjde4a1d02002-03-22 01:27:54 +0000417 SYMTAB is for Valgrind's symbol table storage.
njn25e49d8e72002-09-23 09:36:25 +0000418 JITTER is for small storage during translation.
sewardjde4a1d02002-03-22 01:27:54 +0000419 CLIENT is for the client's mallocs/frees.
420 DEMANGLE is for the C++ demangler.
421 EXECTXT is for storing ExeContexts.
njn25e49d8e72002-09-23 09:36:25 +0000422 ERRORS is for storing CoreErrors.
sewardjde4a1d02002-03-22 01:27:54 +0000423 TRANSIENT is for very short-term use. It should be empty
424 in between uses.
njn25e49d8e72002-09-23 09:36:25 +0000425 When adding a new arena, remember also to add it to ensure_mm_init().
sewardjde4a1d02002-03-22 01:27:54 +0000426*/
427typedef Int ArenaId;
428
njn25e49d8e72002-09-23 09:36:25 +0000429#define VG_N_ARENAS 9
sewardjde4a1d02002-03-22 01:27:54 +0000430
njn25e49d8e72002-09-23 09:36:25 +0000431#define VG_AR_CORE 0 /* :: ArenaId */
432#define VG_AR_SKIN 1 /* :: ArenaId */
433#define VG_AR_SYMTAB 2 /* :: ArenaId */
434#define VG_AR_JITTER 3 /* :: ArenaId */
435#define VG_AR_CLIENT 4 /* :: ArenaId */
436#define VG_AR_DEMANGLE 5 /* :: ArenaId */
437#define VG_AR_EXECTXT 6 /* :: ArenaId */
438#define VG_AR_ERRORS 7 /* :: ArenaId */
439#define VG_AR_TRANSIENT 8 /* :: ArenaId */
sewardjde4a1d02002-03-22 01:27:54 +0000440
njn25e49d8e72002-09-23 09:36:25 +0000441extern void* VG_(arena_malloc) ( ArenaId arena, Int nbytes );
442extern void VG_(arena_free) ( ArenaId arena, void* ptr );
443extern void* VG_(arena_calloc) ( ArenaId arena, Int nmemb, Int nbytes );
444extern void* VG_(arena_realloc) ( ArenaId arena, void* ptr, Int alignment,
445 Int size );
446extern void* VG_(arena_malloc_aligned) ( ArenaId aid, Int req_alignB,
sewardjde4a1d02002-03-22 01:27:54 +0000447 Int req_pszB );
448
sewardjde4a1d02002-03-22 01:27:54 +0000449extern void VG_(mallocSanityCheckAll) ( void );
450
451extern void VG_(show_all_arena_stats) ( void );
452extern Bool VG_(is_empty_arena) ( ArenaId aid );
453
454
455/* The red-zone size for the client. This can be arbitrary, but
456 unfortunately must be set at compile time. */
457#define VG_AR_CLIENT_REDZONE_SZW 4
458
459#define VG_AR_CLIENT_REDZONE_SZB \
460 (VG_AR_CLIENT_REDZONE_SZW * VKI_BYTES_PER_WORD)
461
462
463/* ---------------------------------------------------------------------
njn25e49d8e72002-09-23 09:36:25 +0000464 Exports of vg_clientfuncs.c
sewardj2e93c502002-04-12 11:12:52 +0000465 ------------------------------------------------------------------ */
466
467/* This doesn't export code or data that valgrind.so needs to link
468 against. However, the scheduler does need to know the following
469 request codes. A few, publically-visible, request codes are also
njn25e49d8e72002-09-23 09:36:25 +0000470 defined in valgrind.h, and similar headers for some skins. */
sewardj2e93c502002-04-12 11:12:52 +0000471
472#define VG_USERREQ__MALLOC 0x2001
473#define VG_USERREQ__BUILTIN_NEW 0x2002
474#define VG_USERREQ__BUILTIN_VEC_NEW 0x2003
475
476#define VG_USERREQ__FREE 0x2004
477#define VG_USERREQ__BUILTIN_DELETE 0x2005
478#define VG_USERREQ__BUILTIN_VEC_DELETE 0x2006
479
480#define VG_USERREQ__CALLOC 0x2007
481#define VG_USERREQ__REALLOC 0x2008
482#define VG_USERREQ__MEMALIGN 0x2009
483
484
sewardj20917d82002-05-28 01:36:45 +0000485/* (Fn, Arg): Create a new thread and run Fn applied to Arg in it. Fn
486 MUST NOT return -- ever. Eventually it will do either __QUIT or
487 __WAIT_JOINER. */
488#define VG_USERREQ__APPLY_IN_NEW_THREAD 0x3001
489
490/* ( no-args ): calling thread disappears from the system forever.
491 Reclaim resources. */
492#define VG_USERREQ__QUIT 0x3002
493
494/* ( void* ): calling thread waits for joiner and returns the void* to
495 it. */
496#define VG_USERREQ__WAIT_JOINER 0x3003
497
498/* ( ThreadId, void** ): wait to join a thread. */
499#define VG_USERREQ__PTHREAD_JOIN 0x3004
500
501/* Set cancellation state and type for this thread. */
502#define VG_USERREQ__SET_CANCELSTATE 0x3005
503#define VG_USERREQ__SET_CANCELTYPE 0x3006
504
505/* ( no-args ): Test if we are at a cancellation point. */
506#define VG_USERREQ__TESTCANCEL 0x3007
507
508/* ( ThreadId, &thread_exit_wrapper is the only allowable arg ): call
509 with this arg to indicate that a cancel is now pending for the
510 specified thread. */
511#define VG_USERREQ__SET_CANCELPEND 0x3008
512
513/* Set/get detach state for this thread. */
514#define VG_USERREQ__SET_OR_GET_DETACH 0x3009
515
516#define VG_USERREQ__PTHREAD_GET_THREADID 0x300B
517#define VG_USERREQ__PTHREAD_MUTEX_LOCK 0x300C
518#define VG_USERREQ__PTHREAD_MUTEX_TRYLOCK 0x300D
519#define VG_USERREQ__PTHREAD_MUTEX_UNLOCK 0x300E
520#define VG_USERREQ__PTHREAD_COND_WAIT 0x300F
521#define VG_USERREQ__PTHREAD_COND_TIMEDWAIT 0x3010
522#define VG_USERREQ__PTHREAD_COND_SIGNAL 0x3011
523#define VG_USERREQ__PTHREAD_COND_BROADCAST 0x3012
524#define VG_USERREQ__PTHREAD_KEY_CREATE 0x3013
525#define VG_USERREQ__PTHREAD_KEY_DELETE 0x3014
sewardj00a66b12002-10-12 16:42:35 +0000526#define VG_USERREQ__PTHREAD_SETSPECIFIC_PTR 0x3015
527#define VG_USERREQ__PTHREAD_GETSPECIFIC_PTR 0x3016
sewardj20917d82002-05-28 01:36:45 +0000528#define VG_USERREQ__READ_MILLISECOND_TIMER 0x3017
529#define VG_USERREQ__PTHREAD_SIGMASK 0x3018
530#define VG_USERREQ__SIGWAIT 0x3019
531#define VG_USERREQ__PTHREAD_KILL 0x301A
532#define VG_USERREQ__PTHREAD_YIELD 0x301B
sewardj00a66b12002-10-12 16:42:35 +0000533#define VG_USERREQ__PTHREAD_KEY_VALIDATE 0x301C
sewardj2e93c502002-04-12 11:12:52 +0000534
sewardj8ad94e12002-05-29 00:10:20 +0000535#define VG_USERREQ__CLEANUP_PUSH 0x3020
536#define VG_USERREQ__CLEANUP_POP 0x3021
sewardj870497a2002-05-29 01:06:47 +0000537#define VG_USERREQ__GET_KEY_D_AND_S 0x3022
sewardj8ad94e12002-05-29 00:10:20 +0000538
sewardjef037c72002-05-30 00:40:03 +0000539#define VG_USERREQ__NUKE_OTHER_THREADS 0x3023
sewardjefbfcdf2002-06-19 17:35:45 +0000540
541/* Ask how many signal handler returns have happened to this
542 thread. */
sewardj9a2224b2002-06-19 10:17:40 +0000543#define VG_USERREQ__GET_N_SIGS_RETURNED 0x3024
sewardjef037c72002-05-30 00:40:03 +0000544
sewardj2cb00342002-06-28 01:46:26 +0000545/* Get/set entries for a thread's pthread_atfork stack. */
546#define VG_USERREQ__SET_FHSTACK_USED 0x3025
547#define VG_USERREQ__GET_FHSTACK_USED 0x3026
548#define VG_USERREQ__SET_FHSTACK_ENTRY 0x3027
549#define VG_USERREQ__GET_FHSTACK_ENTRY 0x3028
sewardjefbfcdf2002-06-19 17:35:45 +0000550
sewardj1fe7b002002-07-16 01:43:15 +0000551/* Denote the finish of VG_(__libc_freeres_wrapper). */
552#define VG_USERREQ__LIBC_FREERES_DONE 0x3029
553
sewardj45b4b372002-04-16 22:50:32 +0000554/* Cosmetic ... */
555#define VG_USERREQ__GET_PTHREAD_TRACE_LEVEL 0x3101
sewardj4dced352002-06-04 22:54:20 +0000556/* Log a pthread error from client-space. Cosmetic. */
557#define VG_USERREQ__PTHREAD_ERROR 0x3102
sewardj69a72a52002-11-03 13:41:41 +0000558/* Write a string to the logging sink. */
559#define VG_USERREQ__LOGMESSAGE 0x3103
560
sewardj45b4b372002-04-16 22:50:32 +0000561
sewardj54cacf02002-04-12 23:24:59 +0000562/*
563In vg_constants.h:
564#define VG_USERREQ__SIGNAL_RETURNS 0x4001
sewardj54cacf02002-04-12 23:24:59 +0000565*/
566
sewardj1fe7b002002-07-16 01:43:15 +0000567/* The scheduler does need to know the address of it so it can be
568 called at program exit. */
569extern void VG_(__libc_freeres_wrapper)( void );
570
sewardj54cacf02002-04-12 23:24:59 +0000571
sewardj2e93c502002-04-12 11:12:52 +0000572/* ---------------------------------------------------------------------
573 Constants pertaining to the simulated CPU state, VG_(baseBlock),
574 which need to go here to avoid ugly circularities.
575 ------------------------------------------------------------------ */
576
577/* How big is the saved FPU state? */
578#define VG_SIZE_OF_FPUSTATE 108
579/* ... and in words ... */
580#define VG_SIZE_OF_FPUSTATE_W ((VG_SIZE_OF_FPUSTATE+3)/4)
581
582
583/* ---------------------------------------------------------------------
sewardj92a59562002-09-30 00:53:10 +0000584 Exports of vg_ldt.c
585 ------------------------------------------------------------------ */
586
587/* This is the hardware-format for a segment descriptor, ie what the
588 x86 actually deals with. It is 8 bytes long. It's ugly. */
589
590typedef struct _LDT_ENTRY {
591 union {
592 struct {
593 UShort LimitLow;
594 UShort BaseLow;
595 unsigned BaseMid : 8;
596 unsigned Type : 5;
597 unsigned Dpl : 2;
598 unsigned Pres : 1;
599 unsigned LimitHi : 4;
600 unsigned Sys : 1;
601 unsigned Reserved_0 : 1;
602 unsigned Default_Big : 1;
603 unsigned Granularity : 1;
604 unsigned BaseHi : 8;
605 } Bits;
606 struct {
607 UInt word1;
608 UInt word2;
609 } Words;
610 }
611 LdtEnt;
612} VgLdtEntry;
613
614/* Maximum number of LDT entries supported (by the x86). */
615#define VG_M_LDT_ENTRIES 8192
616/* The size of each LDT entry == sizeof(VgLdtEntry) */
617#define VG_LDT_ENTRY_SIZE 8
618
619/* Alloc & copy, and dealloc. */
620extern VgLdtEntry*
621 VG_(allocate_LDT_for_thread) ( VgLdtEntry* parent_ldt );
622extern void
623 VG_(deallocate_LDT_for_thread) ( VgLdtEntry* ldt );
624
625/* Simulate the modify_ldt syscall. */
626extern Int VG_(sys_modify_ldt) ( ThreadId tid,
627 Int func, void* ptr, UInt bytecount );
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/* ---------------------------------------------------------------------
sewardj2e93c502002-04-12 11:12:52 +0000635 Exports of vg_scheduler.c
636 ------------------------------------------------------------------ */
637
sewardj2e93c502002-04-12 11:12:52 +0000638typedef
639 enum {
640 VgTs_Empty, /* this slot is not in use */
641 VgTs_Runnable, /* waiting to be scheduled */
642 VgTs_WaitJoiner, /* waiting for someone to do join on me */
643 VgTs_WaitJoinee, /* waiting for the thread I did join on */
644 VgTs_WaitFD, /* waiting for I/O completion on a fd */
645 VgTs_WaitMX, /* waiting on a mutex */
sewardj3b5d8862002-04-20 13:53:23 +0000646 VgTs_WaitCV, /* waiting on a condition variable */
sewardjb48e5002002-05-13 00:16:03 +0000647 VgTs_WaitSIG, /* waiting due to sigwait() */
sewardj2e93c502002-04-12 11:12:52 +0000648 VgTs_Sleeping /* sleeping for a while */
649 }
650 ThreadStatus;
sewardj8ad94e12002-05-29 00:10:20 +0000651
652/* An entry in a threads's cleanup stack. */
653typedef
654 struct {
655 void (*fn)(void*);
656 void* arg;
657 }
658 CleanupEntry;
sewardj2cb00342002-06-28 01:46:26 +0000659
660/* An entry in a thread's fork-handler stack. */
661typedef
662 struct {
663 void (*prepare)(void);
664 void (*parent)(void);
665 void (*child)(void);
666 }
667 ForkHandlerEntry;
668
669
njn25e49d8e72002-09-23 09:36:25 +0000670struct _ThreadState {
671 /* ThreadId == 0 (and hence vg_threads[0]) is NEVER USED.
672 The thread identity is simply the index in vg_threads[].
673 ThreadId == 1 is the root thread and has the special property
674 that we don't try and allocate or deallocate its stack. For
675 convenience of generating error message, we also put the
676 ThreadId in this tid field, but be aware that it should
677 ALWAYS == the index in vg_threads[]. */
678 ThreadId tid;
sewardj2e93c502002-04-12 11:12:52 +0000679
njn25e49d8e72002-09-23 09:36:25 +0000680 /* Current scheduling status.
sewardj5f07b662002-04-23 16:52:51 +0000681
njn25e49d8e72002-09-23 09:36:25 +0000682 Complications: whenever this is set to VgTs_WaitMX, you
683 should also set .m_edx to whatever the required return value
684 is for pthread_mutex_lock / pthread_cond_timedwait for when
685 the mutex finally gets unblocked. */
686 ThreadStatus status;
sewardj2e93c502002-04-12 11:12:52 +0000687
njn25e49d8e72002-09-23 09:36:25 +0000688 /* When .status == WaitMX, points to the mutex I am waiting for.
689 When .status == WaitCV, points to the mutex associated with
690 the condition variable indicated by the .associated_cv field.
691 In all other cases, should be NULL. */
692 void* /*pthread_mutex_t* */ associated_mx;
sewardj3b5d8862002-04-20 13:53:23 +0000693
njn25e49d8e72002-09-23 09:36:25 +0000694 /* When .status == WaitCV, points to the condition variable I am
695 waiting for. In all other cases, should be NULL. */
696 void* /*pthread_cond_t* */ associated_cv;
sewardj2e93c502002-04-12 11:12:52 +0000697
njn25e49d8e72002-09-23 09:36:25 +0000698 /* If VgTs_Sleeping, this is when we should wake up, measured in
699 milliseconds as supplied by VG_(read_millisecond_counter).
sewardj2e93c502002-04-12 11:12:52 +0000700
njn25e49d8e72002-09-23 09:36:25 +0000701 If VgTs_WaitCV, this indicates the time at which
702 pthread_cond_timedwait should wake up. If == 0xFFFFFFFF,
703 this means infinitely far in the future, viz,
704 pthread_cond_wait. */
705 UInt awaken_at;
sewardj20917d82002-05-28 01:36:45 +0000706
njn25e49d8e72002-09-23 09:36:25 +0000707 /* If VgTs_WaitJoiner, return value, as generated by joinees. */
708 void* joinee_retval;
sewardj20917d82002-05-28 01:36:45 +0000709
njn25e49d8e72002-09-23 09:36:25 +0000710 /* If VgTs_WaitJoinee, place to copy the return value to, and
711 the identity of the thread we're waiting for. */
712 void** joiner_thread_return;
713 ThreadId joiner_jee_tid;
sewardj8ad94e12002-05-29 00:10:20 +0000714
njn25e49d8e72002-09-23 09:36:25 +0000715 /* Whether or not detached. */
716 Bool detached;
sewardj20917d82002-05-28 01:36:45 +0000717
njn25e49d8e72002-09-23 09:36:25 +0000718 /* Cancelability state and type. */
719 Bool cancel_st; /* False==PTH_CANCEL_DISABLE; True==.._ENABLE */
720 Bool cancel_ty; /* False==PTH_CANC_ASYNCH; True==..._DEFERRED */
721
722 /* Pointer to fn to call to do cancellation. Indicates whether
723 or not cancellation is pending. If NULL, not pending. Else
724 should be &thread_exit_wrapper(), indicating that
725 cancallation is pending. */
726 void (*cancel_pend)(void*);
sewardj2e93c502002-04-12 11:12:52 +0000727
njn25e49d8e72002-09-23 09:36:25 +0000728 /* The cleanup stack. */
729 Int custack_used;
730 CleanupEntry custack[VG_N_CLEANUPSTACK];
sewardj5f07b662002-04-23 16:52:51 +0000731
sewardj00a66b12002-10-12 16:42:35 +0000732 /* A pointer to the thread's-specific-data. This is handled almost
733 entirely from vg_libpthread.c. We just provide hooks to get and
734 set this ptr. This is either NULL, indicating the thread has
735 read/written none of its specifics so far, OR points to a
736 void*[VG_N_THREAD_KEYS], allocated and deallocated in
737 vg_libpthread.c. */
738 void** specifics_ptr;
sewardjb48e5002002-05-13 00:16:03 +0000739
njn25e49d8e72002-09-23 09:36:25 +0000740 /* This thread's blocked-signals mask. Semantics is that for a
741 signal to be delivered to this thread, the signal must not be
742 blocked by either the process-wide signal mask nor by this
743 one. So, if this thread is prepared to handle any signal that
744 the process as a whole is prepared to handle, this mask should
745 be made empty -- and that it is its default, starting
746 state. */
747 vki_ksigset_t sig_mask;
sewardjb48e5002002-05-13 00:16:03 +0000748
njn25e49d8e72002-09-23 09:36:25 +0000749 /* When not VgTs_WaitSIG, has no meaning. When VgTs_WaitSIG,
750 is the set of signals for which we are sigwait()ing. */
751 vki_ksigset_t sigs_waited_for;
sewardj9a2224b2002-06-19 10:17:40 +0000752
njn25e49d8e72002-09-23 09:36:25 +0000753 /* Counts the number of times a signal handler for this thread
754 has returned. This makes it easy to implement pause(), by
755 polling this value, of course interspersed with nanosleeps,
756 and waiting till it changes. */
757 UInt n_signals_returned;
sewardj2e93c502002-04-12 11:12:52 +0000758
njn25e49d8e72002-09-23 09:36:25 +0000759 /* Stacks. When a thread slot is freed, we don't deallocate its
760 stack; we just leave it lying around for the next use of the
761 slot. If the next use of the slot requires a larger stack,
762 only then is the old one deallocated and a new one
763 allocated.
sewardj2e93c502002-04-12 11:12:52 +0000764
njn25e49d8e72002-09-23 09:36:25 +0000765 For the main thread (threadid == 0), this mechanism doesn't
766 apply. We don't know the size of the stack since we didn't
767 allocate it, and furthermore we never reallocate it. */
sewardj2e93c502002-04-12 11:12:52 +0000768
njn25e49d8e72002-09-23 09:36:25 +0000769 /* The allocated size of this thread's stack (permanently zero
770 if this is ThreadId == 0, since we didn't allocate its stack) */
771 UInt stack_size;
sewardj1e8cdc92002-04-18 11:37:52 +0000772
njn25e49d8e72002-09-23 09:36:25 +0000773 /* Address of the lowest word in this thread's stack. NULL means
774 not allocated yet.
775 */
776 Addr stack_base;
sewardj2e93c502002-04-12 11:12:52 +0000777
sewardj92a59562002-09-30 00:53:10 +0000778 /* Address of the highest legitimate word in this stack. This is
779 used for error messages only -- not critical for execution
780 correctness. Is is set for all stacks, specifically including
781 ThreadId == 0 (the main thread). */
njn25e49d8e72002-09-23 09:36:25 +0000782 Addr stack_highest_word;
783
sewardj92a59562002-09-30 00:53:10 +0000784 /* Pointer to this thread's Local (Segment) Descriptor Table.
785 Starts out as NULL, indicating there is no table, and we hope to
786 keep it that way. If the thread does __NR_modify_ldt to create
787 entries, we allocate a 8192-entry table at that point. This is
788 a straight copy of the Linux kernel's scheme. Don't forget to
789 deallocate this at thread exit. */
790 VgLdtEntry* ldt;
791
792 /* Saved machine context. Note the FPU state, %EIP and segment
793 registers are not shadowed.
794
795 Although the segment registers are 16 bits long, storage
796 management here, in VG_(baseBlock) and in VG_(m_state_static) is
797 simplified if we pretend they are 32 bits. */
798 UInt m_cs;
799 UInt m_ss;
800 UInt m_ds;
801 UInt m_es;
802 UInt m_fs;
803 UInt m_gs;
804
njn25e49d8e72002-09-23 09:36:25 +0000805 UInt m_eax;
806 UInt m_ebx;
807 UInt m_ecx;
808 UInt m_edx;
809 UInt m_esi;
810 UInt m_edi;
811 UInt m_ebp;
812 UInt m_esp;
813 UInt m_eflags;
814 UInt m_eip;
815 UInt m_fpu[VG_SIZE_OF_FPUSTATE_W];
816
817 UInt sh_eax;
818 UInt sh_ebx;
819 UInt sh_ecx;
820 UInt sh_edx;
821 UInt sh_esi;
822 UInt sh_edi;
823 UInt sh_ebp;
824 UInt sh_esp;
825 UInt sh_eflags;
826};
sewardj2e93c502002-04-12 11:12:52 +0000827
828
sewardj018f7622002-05-15 21:13:39 +0000829/* The thread table. */
830extern ThreadState VG_(threads)[VG_N_THREADS];
831
832/* Check that tid is in range and denotes a non-Empty thread. */
sewardjb48e5002002-05-13 00:16:03 +0000833extern Bool VG_(is_valid_tid) ( ThreadId tid );
834
sewardj018f7622002-05-15 21:13:39 +0000835/* Check that tid is in range. */
836extern Bool VG_(is_valid_or_empty_tid) ( ThreadId tid );
837
sewardj2e93c502002-04-12 11:12:52 +0000838/* Copy the specified thread's state into VG_(baseBlock) in
839 preparation for running it. */
840extern void VG_(load_thread_state)( ThreadId );
841
842/* Save the specified thread's state back in VG_(baseBlock), and fill
843 VG_(baseBlock) with junk, for sanity-check reasons. */
844extern void VG_(save_thread_state)( ThreadId );
845
sewardj1e8cdc92002-04-18 11:37:52 +0000846/* And for the currently running one, if valid. */
847extern ThreadState* VG_(get_current_thread_state) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000848
sewardj1e8cdc92002-04-18 11:37:52 +0000849/* Similarly ... */
850extern ThreadId VG_(get_current_tid) ( void );
851
sewardjccef2e62002-05-29 19:26:32 +0000852/* Nuke all threads except tid. */
853extern void VG_(nuke_all_threads_except) ( ThreadId me );
854
sewardj2e93c502002-04-12 11:12:52 +0000855
856/* Return codes from the scheduler. */
857typedef
sewardj7e87e382002-05-03 19:09:05 +0000858 enum {
859 VgSrc_Deadlock, /* no runnable threads and no prospect of any
860 even if we wait for a long time */
861 VgSrc_ExitSyscall, /* client called exit(). This is the normal
862 route out. */
863 VgSrc_BbsDone /* In a debugging run, the specified number of
864 bbs has been completed. */
865 }
sewardj2e93c502002-04-12 11:12:52 +0000866 VgSchedReturnCode;
867
sewardj7e87e382002-05-03 19:09:05 +0000868
sewardj2e93c502002-04-12 11:12:52 +0000869/* The scheduler. */
870extern VgSchedReturnCode VG_(scheduler) ( void );
871
872extern void VG_(scheduler_init) ( void );
873
sewardj15a43e12002-04-17 19:35:12 +0000874extern void VG_(pp_sched_status) ( void );
sewardj2e93c502002-04-12 11:12:52 +0000875
876/* vg_oursignalhandler() might longjmp(). Here's the jmp_buf. */
877extern jmp_buf VG_(scheduler_jmpbuf);
sewardj872051c2002-07-13 12:12:56 +0000878/* This says whether scheduler_jmpbuf is actually valid. Needed so
879 that our signal handler doesn't longjmp when the buffer isn't
880 actually valid. */
881extern Bool VG_(scheduler_jmpbuf_valid);
sewardj2e93c502002-04-12 11:12:52 +0000882/* ... and if so, here's the signal which caused it to do so. */
883extern Int VG_(longjmpd_on_signal);
884
885
sewardja1679dd2002-05-10 22:31:40 +0000886/* Possible places where the main stack might be based. We check that
887 the initial stack, which we can't move, is allocated here.
888 VG_(scheduler_init) checks this. Andrea Archelangi's 2.4 kernels
889 have been rumoured to start stacks at 0x80000000, so that too is
njn25e49d8e72002-09-23 09:36:25 +0000890 considered. It seems systems with longer uptimes tend to to use
891 stacks which start at 0x40000000 sometimes. JRS 2002-Aug-21: I
892 also have reports of stacks starting at 0xE0000000.*/
893
sewardja1679dd2002-05-10 22:31:40 +0000894#define VG_STARTUP_STACK_BASE_1 (Addr)0xC0000000
895#define VG_STARTUP_STACK_BASE_2 (Addr)0x80000000
daywalkera2562202002-07-15 19:39:51 +0000896#define VG_STARTUP_STACK_BASE_3 (Addr)0x40000000
njn25e49d8e72002-09-23 09:36:25 +0000897#define VG_STARTUP_STACK_BASE_4 (Addr)0xE0000000
sewardja1679dd2002-05-10 22:31:40 +0000898#define VG_STARTUP_STACK_SMALLERTHAN 0x100000 /* 1024k */
899
900#define VG_STACK_MATCHES_BASE(zzstack, zzbase) \
901 ( \
902 ((zzstack) & ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN)) \
903 == \
904 ((zzbase) - VG_STARTUP_STACK_SMALLERTHAN) \
905 )
sewardj2e93c502002-04-12 11:12:52 +0000906
907
908/* The red-zone size which we put at the bottom (highest address) of
909 thread stacks, for paranoia reasons. This can be arbitrary, and
910 doesn't really need to be set at compile time. */
911#define VG_AR_CLIENT_STACKBASE_REDZONE_SZW 4
912
913#define VG_AR_CLIENT_STACKBASE_REDZONE_SZB \
914 (VG_AR_CLIENT_STACKBASE_REDZONE_SZW * VKI_BYTES_PER_WORD)
915
njn25e49d8e72002-09-23 09:36:25 +0000916/* Junk to fill up a thread's shadow regs with when shadow regs aren't
917 * being used. */
918#define VG_UNUSED_SHADOW_REG_VALUE 0x27182818
919
920/* What we set a shadow register to when written by SET_EAX and similar
921 * things. */
922extern UInt VG_(written_shadow_reg);
sewardj2e93c502002-04-12 11:12:52 +0000923
sewardj018f7622002-05-15 21:13:39 +0000924/* Write a value to the client's %EDX (request return value register)
925 and set the shadow to indicate it is defined. */
njn25e49d8e72002-09-23 09:36:25 +0000926#define SET_EDX(zztid, zzval) \
927 do { VG_(threads)[zztid].m_edx = (zzval); \
928 VG_(threads)[zztid].sh_edx = VG_(written_shadow_reg); \
sewardj018f7622002-05-15 21:13:39 +0000929 } while (0)
930
njn25e49d8e72002-09-23 09:36:25 +0000931#define SET_EAX(zztid, zzval) \
932 do { VG_(threads)[zztid].m_eax = (zzval); \
933 VG_(threads)[zztid].sh_eax = VG_(written_shadow_reg); \
sewardj018f7622002-05-15 21:13:39 +0000934 } while (0)
935
sewardj2e93c502002-04-12 11:12:52 +0000936
sewardjd8acdf22002-11-13 21:57:52 +0000937/* This is or'd into a pthread mutex's __m_kind field if it is used
938 before Valgrind is up and running (prehistory). This is used so
939 that if some early code (like the dynamic linker) takes a lock
940 before Valgrind starts and then releases it afterwards, we can work
941 out what's happening. */
942#define VG_PTHREAD_PREHISTORY 0x80000000
943
sewardj2e93c502002-04-12 11:12:52 +0000944/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000945 Exports of vg_signals.c
946 ------------------------------------------------------------------ */
947
sewardjde4a1d02002-03-22 01:27:54 +0000948extern void VG_(sigstartup_actions) ( void );
949
sewardjb48e5002002-05-13 00:16:03 +0000950extern Bool VG_(deliver_signals) ( void );
sewardjde4a1d02002-03-22 01:27:54 +0000951extern void VG_(unblock_host_signal) ( Int sigNo );
sewardj018f7622002-05-15 21:13:39 +0000952extern void VG_(handle_SCSS_change) ( Bool force_update );
953
sewardjde4a1d02002-03-22 01:27:54 +0000954
955/* Fake system calls for signal handling. */
sewardj2342c972002-05-22 23:34:20 +0000956extern void VG_(do__NR_sigaltstack) ( ThreadId tid );
sewardj2e93c502002-04-12 11:12:52 +0000957extern void VG_(do__NR_sigaction) ( ThreadId tid );
sewardj018f7622002-05-15 21:13:39 +0000958extern void VG_(do__NR_sigprocmask) ( ThreadId tid,
959 Int how,
960 vki_ksigset_t* set,
961 vki_ksigset_t* oldset );
962extern void VG_(do_pthread_sigmask_SCSS_upd) ( ThreadId tid,
963 Int how,
964 vki_ksigset_t* set,
965 vki_ksigset_t* oldset );
966extern void VG_(send_signal_to_thread) ( ThreadId thread,
967 Int signo );
sewardjde4a1d02002-03-22 01:27:54 +0000968
sewardjefbfcdf2002-06-19 17:35:45 +0000969extern void VG_(do_sigpending) ( ThreadId tid, vki_ksigset_t* set );
970
971
sewardj2e93c502002-04-12 11:12:52 +0000972/* Modify the current thread's state once we have detected it is
973 returning from a signal handler. */
sewardj77e466c2002-04-14 02:29:29 +0000974extern Bool VG_(signal_returns) ( ThreadId );
sewardjde4a1d02002-03-22 01:27:54 +0000975
sewardj2e93c502002-04-12 11:12:52 +0000976/* Handy utilities to block/restore all host signals. */
977extern void VG_(block_all_host_signals)
978 ( /* OUT */ vki_ksigset_t* saved_mask );
sewardj018f7622002-05-15 21:13:39 +0000979extern void VG_(restore_all_host_signals)
sewardj2e93c502002-04-12 11:12:52 +0000980 ( /* IN */ vki_ksigset_t* saved_mask );
sewardjde4a1d02002-03-22 01:27:54 +0000981
982/* ---------------------------------------------------------------------
983 Exports of vg_mylibc.c
984 ------------------------------------------------------------------ */
985
njne427a662002-10-02 11:08:25 +0000986#define vg_assert(expr) \
987 ((void) ((expr) ? 0 : \
988 (VG_(core_assert_fail) (VG__STRING(expr), \
989 __FILE__, __LINE__, \
990 __PRETTY_FUNCTION__), 0)))
991__attribute__ ((__noreturn__))
992extern void VG_(core_assert_fail) ( Char* expr, Char* file,
993 Int line, Char* fn );
994__attribute__ ((__noreturn__))
995extern void VG_(core_panic) ( Char* str );
sewardjde4a1d02002-03-22 01:27:54 +0000996
njn25e49d8e72002-09-23 09:36:25 +0000997/* VG_(brk) not public so skins cannot screw with curr_dataseg_end */
998extern void* VG_(brk) ( void* end_data_segment );
sewardjde4a1d02002-03-22 01:27:54 +0000999
njn25e49d8e72002-09-23 09:36:25 +00001000/* Skins use VG_(strdup)() which doesn't expose ArenaId */
1001extern Char* VG_(arena_strdup) ( ArenaId aid, const Char* s);
sewardjde4a1d02002-03-22 01:27:54 +00001002
njn25e49d8e72002-09-23 09:36:25 +00001003/* Skins shouldn't need these...(?) */
sewardj5f07b662002-04-23 16:52:51 +00001004extern void VG_(start_rdtsc_calibration) ( void );
1005extern void VG_(end_rdtsc_calibration) ( void );
1006extern UInt VG_(read_millisecond_timer) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001007
njn25e49d8e72002-09-23 09:36:25 +00001008extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg );
sewardj2e93c502002-04-12 11:12:52 +00001009extern Int VG_(select)( Int n,
1010 vki_fd_set* readfds,
1011 vki_fd_set* writefds,
1012 vki_fd_set* exceptfds,
1013 struct vki_timeval * timeout );
1014extern Int VG_(nanosleep)( const struct vki_timespec *req,
1015 struct vki_timespec *rem );
1016
sewardj570f8902002-11-03 11:44:36 +00001017extern Int VG_(write_socket)( Int sd, void *msg, Int count );
sewardj73cf3bc2002-11-03 03:20:15 +00001018
1019/* --- Connecting over the network --- */
1020extern Int VG_(connect_via_socket)( UChar* str );
1021
sewardj570f8902002-11-03 11:44:36 +00001022
1023/* ---------------------------------------------------------------------
1024 Exports of vg_message.c
1025 ------------------------------------------------------------------ */
1026
1027/* Low-level -- send bytes directly to the message sink. Do not
1028 use. */
1029extern void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes );
1030
1031
sewardjde4a1d02002-03-22 01:27:54 +00001032/* ---------------------------------------------------------------------
1033 Definitions for the JITter (vg_translate.c, vg_to_ucode.c,
1034 vg_from_ucode.c).
1035 ------------------------------------------------------------------ */
1036
sewardjde4a1d02002-03-22 01:27:54 +00001037#define VG_IS_FLAG_SUBSET(set1,set2) \
1038 (( ((FlagSet)set1) & ((FlagSet)set2) ) == ((FlagSet)set1) )
1039
1040#define VG_UNION_FLAG_SETS(set1,set2) \
1041 ( ((FlagSet)set1) | ((FlagSet)set2) )
1042
sewardjde4a1d02002-03-22 01:27:54 +00001043/* ---------------------------------------------------------------------
1044 Exports of vg_demangle.c
1045 ------------------------------------------------------------------ */
1046
1047extern void VG_(demangle) ( Char* orig, Char* result, Int result_size );
1048
sewardjde4a1d02002-03-22 01:27:54 +00001049/* ---------------------------------------------------------------------
1050 Exports of vg_from_ucode.c
1051 ------------------------------------------------------------------ */
1052
sewardj22854b92002-11-30 14:00:47 +00001053extern UChar* VG_(emit_code) ( UCodeBlock* cb, Int* nbytes, UShort jumps[VG_MAX_JUMPS] );
sewardjde4a1d02002-03-22 01:27:54 +00001054
njn25e49d8e72002-09-23 09:36:25 +00001055extern void VG_(print_ccall_stats) ( void );
1056extern void VG_(print_UInstr_histogram) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001057
sewardj22854b92002-11-30 14:00:47 +00001058extern void VG_(unchain_jumpsite) ( Addr jumpsite );
1059extern Addr VG_(get_jmp_dest) ( Addr jumpsite );
1060extern Bool VG_(is_unchained_jumpsite) ( Addr jumpsite );
1061extern Bool VG_(is_chained_jumpsite) ( Addr jumpsite );
1062
sewardjde4a1d02002-03-22 01:27:54 +00001063/* ---------------------------------------------------------------------
1064 Exports of vg_to_ucode.c
1065 ------------------------------------------------------------------ */
1066
1067extern Int VG_(disBB) ( UCodeBlock* cb, Addr eip0 );
sewardjde4a1d02002-03-22 01:27:54 +00001068
1069/* ---------------------------------------------------------------------
1070 Exports of vg_translate.c
1071 ------------------------------------------------------------------ */
1072
njn810086f2002-11-14 12:42:47 +00001073/* Expandable arrays of uinstrs. */
1074struct _UCodeBlock {
sewardj22854b92002-11-30 14:00:47 +00001075 Addr orig_eip;
njn810086f2002-11-14 12:42:47 +00001076 Int used;
1077 Int size;
1078 UInstr* instrs;
1079 Int nextTemp;
1080};
1081
1082extern UCodeBlock* VG_(alloc_UCodeBlock) ( void );
1083
sewardj1e8cdc92002-04-18 11:37:52 +00001084extern void VG_(translate) ( ThreadState* tst,
1085 Addr orig_addr,
sewardjde4a1d02002-03-22 01:27:54 +00001086 UInt* orig_size,
1087 Addr* trans_addr,
sewardj22854b92002-11-30 14:00:47 +00001088 UInt* trans_size,
1089 UShort jumps[VG_MAX_JUMPS]);
sewardjde4a1d02002-03-22 01:27:54 +00001090
njn25e49d8e72002-09-23 09:36:25 +00001091extern Char* VG_(nameCondcode) ( Condcode cond );
1092extern Bool VG_(saneUInstr) ( Bool beforeRA, Bool beforeLiveness,
1093 UInstr* u );
1094extern void VG_(saneUCodeBlock) ( UCodeBlock* cb );
1095extern Bool VG_(saneUCodeBlockCalls) ( UCodeBlock* cb );
sewardjde4a1d02002-03-22 01:27:54 +00001096
sewardjb5ff83e2002-12-01 19:40:49 +00001097
sewardjde4a1d02002-03-22 01:27:54 +00001098/* ---------------------------------------------------------------------
1099 Exports of vg_execontext.c.
1100 ------------------------------------------------------------------ */
1101
1102/* Records the PC and a bit of the call chain. The first 4 %eip
1103 values are used in comparisons do remove duplicate errors, and for
1104 comparing against suppression specifications. The rest are purely
1105 informational (but often important). */
1106
njn25e49d8e72002-09-23 09:36:25 +00001107struct _ExeContext {
1108 struct _ExeContext * next;
1109 /* Variable-length array. The size is VG_(clo_backtrace_size); at
1110 least 2, at most VG_DEEPEST_BACKTRACE. [0] is the current %eip,
1111 [1] is its caller, [2] is the caller of [1], etc. */
1112 Addr eips[0];
1113};
sewardjde4a1d02002-03-22 01:27:54 +00001114
1115
1116/* Initialise the ExeContext storage mechanism. */
1117extern void VG_(init_ExeContext_storage) ( void );
1118
1119/* Print stats (informational only). */
1120extern void VG_(show_ExeContext_stats) ( void );
1121
njn25e49d8e72002-09-23 09:36:25 +00001122/* Like VG_(get_ExeContext), but with a slightly different type */
1123extern ExeContext* VG_(get_ExeContext2) ( Addr eip, Addr ebp,
1124 Addr ebp_min, Addr ebp_max );
sewardjde4a1d02002-03-22 01:27:54 +00001125
1126
1127/* ---------------------------------------------------------------------
1128 Exports of vg_errcontext.c.
1129 ------------------------------------------------------------------ */
1130
njn25e49d8e72002-09-23 09:36:25 +00001131/* Note: it is imperative this doesn't overlap with (0..) at all, as skins
1132 * effectively extend it by defining their own enums in the (0..) range. */
sewardjde4a1d02002-03-22 01:27:54 +00001133typedef
njn25e49d8e72002-09-23 09:36:25 +00001134 enum {
1135 PThreadSupp = -1, /* Matches PThreadErr */
sewardjde4a1d02002-03-22 01:27:54 +00001136 }
njn25e49d8e72002-09-23 09:36:25 +00001137 CoreSuppKind;
1138
1139/* For each caller specified for a suppression, record the nature of
1140 the caller name. Not of interest to skins. */
1141typedef
1142 enum {
1143 ObjName, /* Name is of an shared object file. */
1144 FunName /* Name is of a function. */
1145 }
1146 SuppLocTy;
1147
njn810086f2002-11-14 12:42:47 +00001148/* Suppressions. Skins can get/set skin-relevant parts with functions
1149 declared in include/vg_skin.h. Extensible via the 'extra' field.
1150 Skins can use a normal enum (with element values in the normal range
1151 (0..)) for `skind'. */
1152struct _Supp {
1153 struct _Supp* next;
1154 /* The number of times this error has been suppressed. */
1155 Int count;
1156 /* The name by which the suppression is referred to. */
1157 Char* sname;
1158 /* First two (name of fn where err occurs, and immediate caller)
1159 * are mandatory; extra two are optional. */
1160 SuppLocTy caller_ty[VG_N_SUPP_CALLERS];
1161 Char* caller [VG_N_SUPP_CALLERS];
1162
1163 /* The skin-specific part */
1164 /* What kind of suppression. Must use the range (0..) */
1165 SuppKind skind;
1166 /* String -- use is optional. NULL by default. */
1167 Char* string;
1168 /* Anything else -- use is optional. NULL by default. */
1169 void* extra;
1170};
njn25e49d8e72002-09-23 09:36:25 +00001171
1172/* Note: it is imperative this doesn't overlap with (0..) at all, as skins
1173 * effectively extend it by defining their own enums in the (0..) range. */
1174typedef
1175 enum {
1176 PThreadErr = -1, /* Pthreading error */
1177 }
1178 CoreErrorKind;
1179
njn810086f2002-11-14 12:42:47 +00001180/* Errors. Extensible (via the 'extra' field). Skins can use a normal
1181 enum (with element values in the normal range (0..)) for `ekind'.
1182 Functions for getting/setting the skin-relevant fields are in
1183 include/vg_skin.h.
1184
1185 When errors are found and recorded with VG_(maybe_record_error)(), all
1186 the skin must do is pass in the four parameters; core will
1187 allocate/initialise the error record.
1188*/
1189struct _Error {
1190 struct _Error* next;
1191 /* NULL if unsuppressed; or ptr to suppression record. */
1192 Supp* supp;
1193 Int count;
1194 ExeContext* where;
1195 ThreadId tid;
1196 /* These record %EIP, %ESP and %EBP at the error point. They
1197 are only used to make GDB-attaching convenient; there is no
1198 other purpose; specifically they are not used to do
1199 comparisons between errors. */
1200 UInt m_eip;
1201 UInt m_esp;
1202 UInt m_ebp;
1203
1204 /* The skin-specific part */
1205 /* Used by ALL. Must be in the range (0..) */
1206 Int ekind;
1207 /* Used frequently */
1208 Addr addr;
1209 /* Used frequently */
1210 Char* string;
1211 /* For any skin-specific extras */
1212 void* extra;
1213};
sewardjde4a1d02002-03-22 01:27:54 +00001214
1215
njn25e49d8e72002-09-23 09:36:25 +00001216extern void VG_(load_suppressions) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001217
njn25e49d8e72002-09-23 09:36:25 +00001218extern void VG_(record_pthread_error) ( ThreadId tid, Char* msg );
sewardjde4a1d02002-03-22 01:27:54 +00001219
njn25e49d8e72002-09-23 09:36:25 +00001220extern void VG_(show_all_errors) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001221
1222/* ---------------------------------------------------------------------
1223 Exports of vg_procselfmaps.c
1224 ------------------------------------------------------------------ */
1225
1226extern
1227void VG_(read_procselfmaps) (
1228 void (*record_mapping)( Addr, UInt, Char, Char, Char, UInt, UChar* )
1229);
1230
1231
1232/* ---------------------------------------------------------------------
1233 Exports of vg_symtab2.c
1234 ------------------------------------------------------------------ */
1235
1236/* We assume the executable is loaded here ... can't really find
njn25e49d8e72002-09-23 09:36:25 +00001237 out. There is a hacky sanity check in VG_(init_memory)()
sewardjde4a1d02002-03-22 01:27:54 +00001238 which should trip up most stupidities.
1239*/
1240#define VG_ASSUMED_EXE_BASE (Addr)0x8048000
1241
njn25e49d8e72002-09-23 09:36:25 +00001242extern void VG_(maybe_read_symbols) ( void );
1243extern void VG_(read_symtab_callback) ( Addr start, UInt size,
1244 Char rr, Char ww, Char xx,
1245 UInt foffset, UChar* filename );
1246extern void VG_(maybe_unload_symbols) ( Addr start, UInt length );
sewardjde4a1d02002-03-22 01:27:54 +00001247
njn25e49d8e72002-09-23 09:36:25 +00001248extern Bool VG_(get_fnname_nodemangle)( Addr a, Char* fnname, Int n_fnname );
1249extern void VG_(mini_stack_dump) ( ExeContext* ec );
sewardjde4a1d02002-03-22 01:27:54 +00001250
1251
1252/* ---------------------------------------------------------------------
1253 Exports of vg_clientmalloc.c
1254 ------------------------------------------------------------------ */
1255
njn810086f2002-11-14 12:42:47 +00001256typedef
1257 enum {
1258 Vg_AllocMalloc = 0,
1259 Vg_AllocNew = 1,
1260 Vg_AllocNewVec = 2
1261 }
1262 VgAllocKind;
1263
1264/* Description of a malloc'd chunk. Functions for extracting skin-relevant
1265 parts are in include/vg_skin.h Size of skin_extra array is given by
1266 VG_(needs).sizeof_shadow_chunk. */
1267struct _ShadowChunk {
1268 struct _ShadowChunk* next;
1269 UInt size : 30; /* size requested */
1270 VgAllocKind allockind : 2; /* which wrapper did the allocation */
1271 Addr data; /* ptr to actual block */
1272 UInt extra[0]; /* extra skin-specific info */
1273};
1274
1275
njn25e49d8e72002-09-23 09:36:25 +00001276extern void VG_(client_malloc_init)();
sewardjde4a1d02002-03-22 01:27:54 +00001277
sewardj2e93c502002-04-12 11:12:52 +00001278/* These are called from the scheduler, when it intercepts a user
1279 request. */
sewardj8c824512002-04-14 04:16:48 +00001280extern void* VG_(client_malloc) ( ThreadState* tst,
1281 UInt size, VgAllocKind kind );
1282extern void* VG_(client_memalign) ( ThreadState* tst,
1283 UInt align, UInt size );
1284extern void VG_(client_free) ( ThreadState* tst,
1285 void* ptrV, VgAllocKind kind );
1286extern void* VG_(client_calloc) ( ThreadState* tst,
1287 UInt nmemb, UInt size1 );
1288extern void* VG_(client_realloc) ( ThreadState* tst,
1289 void* ptrV, UInt size_new );
sewardjde4a1d02002-03-22 01:27:54 +00001290
1291
1292/* ---------------------------------------------------------------------
1293 Exports of vg_main.c
1294 ------------------------------------------------------------------ */
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,
njn25e49d8e72002-09-23 09:36:25 +00001306 and then the inverse copy is done.
1307 */
sewardj92a59562002-09-30 00:53:10 +00001308extern UInt VG_(m_state_static) [6 /* segment regs, Intel order */
1309 + 8 /* int regs, in Intel order */
sewardjde4a1d02002-03-22 01:27:54 +00001310 + 1 /* %eflags */
1311 + 1 /* %eip */
1312 + VG_SIZE_OF_FPUSTATE_W /* FPU state */
1313 ];
1314
1315/* Handy fns for doing the copy back and forth. */
1316extern void VG_(copy_baseBlock_to_m_state_static) ( void );
1317extern void VG_(copy_m_state_static_to_baseBlock) ( void );
1318
sewardjde4a1d02002-03-22 01:27:54 +00001319/* Called when some unhandleable client behaviour is detected.
1320 Prints a msg and aborts. */
njn25e49d8e72002-09-23 09:36:25 +00001321extern void VG_(unimplemented) ( Char* msg )
1322 __attribute__((__noreturn__));
sewardjde4a1d02002-03-22 01:27:54 +00001323
1324/* The stack on which Valgrind runs. We can't use the same stack as the
1325 simulatee -- that's an important design decision. */
1326extern UInt VG_(stack)[10000];
1327
njn25e49d8e72002-09-23 09:36:25 +00001328/* Similarly, we have to ask for signals to be delivered on an alternative
1329 stack, since it is possible, although unlikely, that we'll have to run
1330 client code from inside the Valgrind-installed signal handler. If this
1331 happens it will be done by vg_deliver_signal_immediately(). */
sewardjde4a1d02002-03-22 01:27:54 +00001332extern UInt VG_(sigstack)[10000];
1333
sewardjde4a1d02002-03-22 01:27:54 +00001334/* Holds client's %esp at the point we gained control. From this the
1335 client's argc, argv and envp are deduced. */
1336extern Addr VG_(esp_at_startup);
sewardjde4a1d02002-03-22 01:27:54 +00001337
njn25e49d8e72002-09-23 09:36:25 +00001338/* Remove valgrind.so and skin's .so from a LD_PRELOAD=... string so child
1339 processes don't get traced into. Also mess up $libdir/valgrind so that
1340 our libpthread.so disappears from view. */
sewardj3e1eb1f2002-05-18 13:14:17 +00001341void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str,
1342 Char* ld_library_path_str );
sewardjde4a1d02002-03-22 01:27:54 +00001343
1344/* Something of a function looking for a home ... start up GDB. This
1345 is called from VG_(swizzle_esp_then_start_GDB) and so runs on the
1346 *client's* stack. This is necessary to give GDB the illusion that
1347 the client program really was running on the real cpu. */
1348extern void VG_(start_GDB_whilst_on_client_stack) ( void );
1349
njn41557122002-10-14 09:25:37 +00001350/* VG_(bbs_done) in include/vg_skin.h */
1351
sewardjde4a1d02002-03-22 01:27:54 +00001352/* 64-bit counter for the number of bbs to go before a debug exit. */
1353extern ULong VG_(bbs_to_go);
1354
1355/* Counts downwards in vg_run_innerloop. */
1356extern UInt VG_(dispatch_ctr);
1357
sewardjde4a1d02002-03-22 01:27:54 +00001358/* Is the client running on the simulated CPU or the real one? */
1359extern Bool VG_(running_on_simd_CPU); /* Initially False */
1360
sewardj7e87e382002-05-03 19:09:05 +00001361/* This is the ThreadId of the last thread the scheduler ran. */
1362extern ThreadId VG_(last_run_tid);
1363
njn25e49d8e72002-09-23 09:36:25 +00001364/* This is the argument to __NR_exit() supplied by the first thread to
1365 call that syscall. We eventually pass that to __NR_exit() for
1366 real. */
1367extern UInt VG_(exitcode);
1368
sewardjde4a1d02002-03-22 01:27:54 +00001369
1370/* --- Counters, for informational purposes only. --- */
1371
1372/* Number of lookups which miss the fast tt helper. */
1373extern UInt VG_(tt_fast_misses);
1374
sewardjc0d8f682002-11-30 00:49:43 +00001375/* Counts for TT/TC informational messages. */
sewardjde4a1d02002-03-22 01:27:54 +00001376
sewardjde4a1d02002-03-22 01:27:54 +00001377/* Number and total o/t size of translations overall. */
1378extern UInt VG_(overall_in_count);
1379extern UInt VG_(overall_in_osize);
1380extern UInt VG_(overall_in_tsize);
1381/* Number and total o/t size of discards overall. */
1382extern UInt VG_(overall_out_count);
1383extern UInt VG_(overall_out_osize);
1384extern UInt VG_(overall_out_tsize);
sewardjc0d8f682002-11-30 00:49:43 +00001385/* The number of discards of TT/TC. */
1386extern UInt VG_(number_of_tc_discards);
sewardj22854b92002-11-30 14:00:47 +00001387/* Counts of chain and unchain operations done. */
1388extern UInt VG_(bb_enchain_count);
1389extern UInt VG_(bb_dechain_count);
1390/* Number of unchained jumps performed. */
1391extern UInt VG_(unchained_jumps_done);
1392
sewardjde4a1d02002-03-22 01:27:54 +00001393
1394/* Counts pertaining to the register allocator. */
1395
1396/* total number of uinstrs input to reg-alloc */
1397extern UInt VG_(uinstrs_prealloc);
1398
1399/* total number of uinstrs added due to spill code */
1400extern UInt VG_(uinstrs_spill);
1401
1402/* number of bbs requiring spill code */
1403extern UInt VG_(translations_needing_spill);
1404
1405/* total of register ranks over all translations */
1406extern UInt VG_(total_reg_rank);
1407
sewardjde4a1d02002-03-22 01:27:54 +00001408/* Counts pertaining to internal sanity checking. */
1409extern UInt VG_(sanity_fast_count);
1410extern UInt VG_(sanity_slow_count);
1411
sewardj2e93c502002-04-12 11:12:52 +00001412/* Counts pertaining to the scheduler. */
1413extern UInt VG_(num_scheduling_events_MINOR);
1414extern UInt VG_(num_scheduling_events_MAJOR);
1415
sewardjfa492d42002-12-08 18:20:01 +00001416/* Insert and extract the D flag from eflags */
1417UInt VG_(insertDflag)(UInt eflags, Int d);
1418Int VG_(extractDflag)(UInt eflags);
sewardjde4a1d02002-03-22 01:27:54 +00001419
1420/* ---------------------------------------------------------------------
1421 Exports of vg_memory.c
1422 ------------------------------------------------------------------ */
1423
njn25e49d8e72002-09-23 09:36:25 +00001424extern void VG_(init_memory) ( void );
1425extern void VG_(new_exe_segment) ( Addr a, UInt len );
1426extern void VG_(remove_if_exe_segment) ( Addr a, UInt len );
sewardjde4a1d02002-03-22 01:27:54 +00001427
njn25e49d8e72002-09-23 09:36:25 +00001428/* Called from generated code. */
1429extern void VG_(handle_esp_assignment) ( Addr new_espA );
sewardjde4a1d02002-03-22 01:27:54 +00001430
1431/* Nasty kludgery to deal with applications which switch stacks,
1432 like netscape. */
sewardjde4a1d02002-03-22 01:27:54 +00001433#define VG_PLAUSIBLE_STACK_SIZE 8000000
1434
sewardjde4a1d02002-03-22 01:27:54 +00001435/* ---------------------------------------------------------------------
njn25e49d8e72002-09-23 09:36:25 +00001436 Exports of vg_syscalls.c
sewardjde4a1d02002-03-22 01:27:54 +00001437 ------------------------------------------------------------------ */
1438
njn25e49d8e72002-09-23 09:36:25 +00001439extern void VG_(init_dataseg_end_for_brk) ( void );
1440
sewardj2e93c502002-04-12 11:12:52 +00001441extern void VG_(perform_assumed_nonblocking_syscall) ( ThreadId tid );
sewardjde4a1d02002-03-22 01:27:54 +00001442
njn25e49d8e72002-09-23 09:36:25 +00001443extern void* VG_(pre_known_blocking_syscall) ( ThreadId tid, Int syscallno );
1444extern void VG_(post_known_blocking_syscall)( ThreadId tid, Int syscallno,
1445 void* pre_res, Int res );
sewardjde4a1d02002-03-22 01:27:54 +00001446
1447extern Bool VG_(is_kerror) ( Int res );
1448
njn25e49d8e72002-09-23 09:36:25 +00001449#define KERNEL_DO_SYSCALL(thread_id, result_lvalue) \
1450 VG_(load_thread_state)(thread_id); \
1451 VG_(copy_baseBlock_to_m_state_static)(); \
1452 VG_(do_syscall)(); \
1453 VG_(copy_m_state_static_to_baseBlock)(); \
1454 VG_(save_thread_state)(thread_id); \
1455 VG_(threads)[thread_id].sh_eax = VG_(written_shadow_reg);\
sewardj018f7622002-05-15 21:13:39 +00001456 result_lvalue = VG_(threads)[thread_id].m_eax;
sewardjde4a1d02002-03-22 01:27:54 +00001457
1458
1459/* ---------------------------------------------------------------------
1460 Exports of vg_transtab.c
1461 ------------------------------------------------------------------ */
1462
njn25e49d8e72002-09-23 09:36:25 +00001463/* The fast-cache for tt-lookup. */
1464extern Addr VG_(tt_fast)[VG_TT_FAST_SIZE];
1465
sewardjde4a1d02002-03-22 01:27:54 +00001466extern void VG_(get_tt_tc_used) ( UInt* tt_used, UInt* tc_used );
sewardj6c3769f2002-11-29 01:02:45 +00001467
sewardjc0d8f682002-11-30 00:49:43 +00001468extern void VG_(add_to_trans_tab) ( Addr orig_addr, Int orig_size,
sewardj22854b92002-11-30 14:00:47 +00001469 Addr trans_addr, Int trans_size,
1470 UShort jumps[VG_MAX_JUMPS]);
sewardj6c3769f2002-11-29 01:02:45 +00001471
sewardj18d75132002-05-16 11:06:21 +00001472extern void VG_(invalidate_translations) ( Addr start, UInt range );
sewardjde4a1d02002-03-22 01:27:54 +00001473
sewardj18d75132002-05-16 11:06:21 +00001474extern void VG_(init_tt_tc) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001475
1476extern void VG_(sanity_check_tc_tt) ( void );
1477extern Addr VG_(search_transtab) ( Addr original_addr );
1478
sewardjde4a1d02002-03-22 01:27:54 +00001479
1480
1481/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +00001482 Exports of vg_syscall.S
1483 ------------------------------------------------------------------ */
1484
1485extern void VG_(do_syscall) ( void );
1486
1487
1488/* ---------------------------------------------------------------------
1489 Exports of vg_startup.S
1490 ------------------------------------------------------------------ */
1491
sewardjde4a1d02002-03-22 01:27:54 +00001492extern void VG_(switch_to_real_CPU) ( void );
1493
sewardj35805422002-04-21 13:05:34 +00001494extern void VG_(swizzle_esp_then_start_GDB) ( Addr m_eip_at_error,
1495 Addr m_esp_at_error,
1496 Addr m_ebp_at_error );
sewardjde4a1d02002-03-22 01:27:54 +00001497
1498
1499/* ---------------------------------------------------------------------
1500 Exports of vg_dispatch.S
1501 ------------------------------------------------------------------ */
1502
sewardj2e93c502002-04-12 11:12:52 +00001503/* Run a thread for a (very short) while, until some event happens
1504 which means we need to defer to the scheduler. */
1505extern UInt VG_(run_innerloop) ( void );
sewardjde4a1d02002-03-22 01:27:54 +00001506
sewardj22854b92002-11-30 14:00:47 +00001507/* The patching routing called when a BB wants to chain itself to
1508 another. */
1509extern UInt VG_(patch_me);
sewardjde4a1d02002-03-22 01:27:54 +00001510
1511/* ---------------------------------------------------------------------
1512 Exports of vg_helpers.S
1513 ------------------------------------------------------------------ */
1514
sewardjde4a1d02002-03-22 01:27:54 +00001515/* Mul, div, etc, -- we don't codegen these directly. */
1516extern void VG_(helper_idiv_64_32);
1517extern void VG_(helper_div_64_32);
1518extern void VG_(helper_idiv_32_16);
1519extern void VG_(helper_div_32_16);
1520extern void VG_(helper_idiv_16_8);
1521extern void VG_(helper_div_16_8);
1522
1523extern void VG_(helper_imul_32_64);
1524extern void VG_(helper_mul_32_64);
1525extern void VG_(helper_imul_16_32);
1526extern void VG_(helper_mul_16_32);
1527extern void VG_(helper_imul_8_16);
1528extern void VG_(helper_mul_8_16);
1529
1530extern void VG_(helper_CLD);
1531extern void VG_(helper_STD);
1532extern void VG_(helper_get_dirflag);
1533
sewardj7d78e782002-06-02 00:04:00 +00001534extern void VG_(helper_CLC);
1535extern void VG_(helper_STC);
1536
sewardjde4a1d02002-03-22 01:27:54 +00001537extern void VG_(helper_shldl);
1538extern void VG_(helper_shldw);
1539extern void VG_(helper_shrdl);
1540extern void VG_(helper_shrdw);
1541
1542extern void VG_(helper_RDTSC);
1543extern void VG_(helper_CPUID);
1544
sewardjde4a1d02002-03-22 01:27:54 +00001545extern void VG_(helper_bsf);
1546extern void VG_(helper_bsr);
1547
1548extern void VG_(helper_fstsw_AX);
1549extern void VG_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001550extern void VG_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001551extern void VG_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001552
sewardj20917d82002-05-28 01:36:45 +00001553/* NOT A FUNCTION; this is a bogus RETURN ADDRESS. */
sewardj54cacf02002-04-12 23:24:59 +00001554extern void VG_(signalreturn_bogusRA)( void );
sewardj20917d82002-05-28 01:36:45 +00001555
njn4f9c9342002-04-29 16:03:24 +00001556/* ---------------------------------------------------------------------
njn25e49d8e72002-09-23 09:36:25 +00001557 Things relating to the used skin
njn4f9c9342002-04-29 16:03:24 +00001558 ------------------------------------------------------------------ */
1559
njn25e49d8e72002-09-23 09:36:25 +00001560#define VG_TRACK(fn, args...) \
1561 do { \
1562 if (VG_(track_events).fn) \
1563 VG_(track_events).fn(args); \
1564 } while (0)
sewardj18d75132002-05-16 11:06:21 +00001565
1566
sewardjde4a1d02002-03-22 01:27:54 +00001567/* ---------------------------------------------------------------------
1568 The state of the simulated CPU.
1569 ------------------------------------------------------------------ */
1570
sewardjde4a1d02002-03-22 01:27:54 +00001571/* ---------------------------------------------------------------------
1572 Offsets into baseBlock for everything which needs to referred to
1573 from generated code. The order of these decls does not imply
1574 what the order of the actual offsets is. The latter is important
1575 and is set up in vg_main.c.
1576 ------------------------------------------------------------------ */
1577
1578/* An array of words. In generated code, %ebp always points to the
1579 start of this array. Useful stuff, like the simulated CPU state,
1580 and the addresses of helper functions, can then be found by
1581 indexing off %ebp. The following declares variables which, at
1582 startup time, are given values denoting offsets into baseBlock.
1583 These offsets are in *words* from the start of baseBlock. */
1584
1585#define VG_BASEBLOCK_WORDS 200
1586
1587extern UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
1588
1589
1590/* -----------------------------------------------------
1591 Read-write parts of baseBlock.
1592 -------------------------------------------------- */
1593
1594/* State of the simulated CPU. */
1595extern Int VGOFF_(m_eax);
1596extern Int VGOFF_(m_ecx);
1597extern Int VGOFF_(m_edx);
1598extern Int VGOFF_(m_ebx);
1599extern Int VGOFF_(m_esp);
1600extern Int VGOFF_(m_ebp);
1601extern Int VGOFF_(m_esi);
1602extern Int VGOFF_(m_edi);
1603extern Int VGOFF_(m_eflags);
1604extern Int VGOFF_(m_fpustate);
1605extern Int VGOFF_(m_eip);
1606
sewardjfa492d42002-12-08 18:20:01 +00001607extern Int VGOFF_(m_dflag); /* D flag is handled specially */
1608
sewardj92a59562002-09-30 00:53:10 +00001609extern Int VGOFF_(m_cs);
1610extern Int VGOFF_(m_ss);
1611extern Int VGOFF_(m_ds);
1612extern Int VGOFF_(m_es);
1613extern Int VGOFF_(m_fs);
1614extern Int VGOFF_(m_gs);
1615
sewardjde4a1d02002-03-22 01:27:54 +00001616/* Reg-alloc spill area (VG_MAX_SPILLSLOTS words long). */
1617extern Int VGOFF_(spillslots);
1618
1619/* Records the valid bits for the 8 integer regs & flags reg. */
1620extern Int VGOFF_(sh_eax);
1621extern Int VGOFF_(sh_ecx);
1622extern Int VGOFF_(sh_edx);
1623extern Int VGOFF_(sh_ebx);
1624extern Int VGOFF_(sh_esp);
1625extern Int VGOFF_(sh_ebp);
1626extern Int VGOFF_(sh_esi);
1627extern Int VGOFF_(sh_edi);
1628extern Int VGOFF_(sh_eflags);
1629
sewardjde4a1d02002-03-22 01:27:54 +00001630/* -----------------------------------------------------
1631 Read-only parts of baseBlock.
1632 -------------------------------------------------- */
1633
sewardj92a59562002-09-30 00:53:10 +00001634/* This thread's LDT pointer. */
1635extern Int VGOFF_(ldt);
1636
sewardjde4a1d02002-03-22 01:27:54 +00001637/* Offsets of addresses of helper functions. A "helper" function is
1638 one which is called from generated code. */
1639
1640extern Int VGOFF_(helper_idiv_64_32);
1641extern Int VGOFF_(helper_div_64_32);
1642extern Int VGOFF_(helper_idiv_32_16);
1643extern Int VGOFF_(helper_div_32_16);
1644extern Int VGOFF_(helper_idiv_16_8);
1645extern Int VGOFF_(helper_div_16_8);
1646
1647extern Int VGOFF_(helper_imul_32_64);
1648extern Int VGOFF_(helper_mul_32_64);
1649extern Int VGOFF_(helper_imul_16_32);
1650extern Int VGOFF_(helper_mul_16_32);
1651extern Int VGOFF_(helper_imul_8_16);
1652extern Int VGOFF_(helper_mul_8_16);
1653
1654extern Int VGOFF_(helper_CLD);
1655extern Int VGOFF_(helper_STD);
1656extern Int VGOFF_(helper_get_dirflag);
1657
sewardj7d78e782002-06-02 00:04:00 +00001658extern Int VGOFF_(helper_CLC);
1659extern Int VGOFF_(helper_STC);
1660
sewardjde4a1d02002-03-22 01:27:54 +00001661extern Int VGOFF_(helper_shldl);
1662extern Int VGOFF_(helper_shldw);
1663extern Int VGOFF_(helper_shrdl);
1664extern Int VGOFF_(helper_shrdw);
1665
1666extern Int VGOFF_(helper_RDTSC);
1667extern Int VGOFF_(helper_CPUID);
1668
sewardjde4a1d02002-03-22 01:27:54 +00001669extern Int VGOFF_(helper_bsf);
1670extern Int VGOFF_(helper_bsr);
1671
1672extern Int VGOFF_(helper_fstsw_AX);
1673extern Int VGOFF_(helper_SAHF);
sewardj4d0ab1f2002-03-24 10:00:09 +00001674extern Int VGOFF_(helper_DAS);
sewardjfe8a1662002-03-24 11:54:07 +00001675extern Int VGOFF_(helper_DAA);
sewardjde4a1d02002-03-22 01:27:54 +00001676
sewardjde4a1d02002-03-22 01:27:54 +00001677extern Int VGOFF_(handle_esp_assignment); /* :: Addr -> void */
sewardjde4a1d02002-03-22 01:27:54 +00001678
njn25e49d8e72002-09-23 09:36:25 +00001679/* For storing extension-specific helpers, determined at runtime. The addr
1680 * and offset arrays together form a (addr, offset) map that allows a
1681 * helper's baseBlock offset to be computed from its address. It's done
1682 * like this so CCALL_M_Ns and other helper calls can use the function
1683 * address rather than having to much around with offsets. */
1684extern UInt VG_(n_compact_helpers);
1685extern UInt VG_(n_noncompact_helpers);
1686
1687extern Addr VG_(compact_helper_addrs) [];
1688extern Int VG_(compact_helper_offsets)[];
1689
1690extern Addr VG_(noncompact_helper_addrs) [];
1691extern Int VG_(noncompact_helper_offsets)[];
1692
sewardjf220ccc2002-10-23 21:53:49 +00001693#define VGL_(x) vgIntercept_##x
sewardjde4a1d02002-03-22 01:27:54 +00001694
1695#endif /* ndef __VG_INCLUDE_H */
1696
sewardj3b2736a2002-03-24 12:18:35 +00001697
1698/* ---------------------------------------------------------------------
1699 Finally - autoconf-generated settings
1700 ------------------------------------------------------------------ */
1701
1702#include "config.h"
1703
sewardjde4a1d02002-03-22 01:27:54 +00001704/*--------------------------------------------------------------------*/
1705/*--- end vg_include.h ---*/
1706/*--------------------------------------------------------------------*/