Repository navigation
Expand file tree
/
Copy pathvr_clreq.c
More file actions
555 lines (493 loc) · 16.7 KB
/
Copy pathvr_clreq.c
File metadata and controls
555 lines (493 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*--------------------------------------------------------------------*/
/*--- Verrou: a FPU instrumentation tool. ---*/
/*--- This file contains code related to client requests handling. ---*/
/*--- vr_clreq.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Verrou, a FPU instrumentation tool.
Copyright (C) 2014-2021 EDF
F. Févotte <francois.fevotte@edf.fr>
B. Lathuilière <bruno.lathuiliere@edf.fr>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "vr_main.h"
#include "vr_clreq.h"
#include "pub_tool_transtab.h" // VG_(discard_translations_safely)
// * Start-stop instrumentation
//#include "../coregrind/pub_core_threadstate.h"
extern Bool VG_(is_valid_tid) ( ThreadId tid );
//redefinition as workaround to avoid check of coregrind/pub_core_ include
//#ifdef PROFILING_EXACT
#include "interflop_backends/interflop_verrou/interflop_verrou.h"
//#endif
#include "interflop_backends/backend_checkdenorm/interflop_checkdenorm.h"
void vr_set_instrument_state (const HChar* reason, Vr_Instr state, Bool isHard) {
if(isHard){
if (vr.instrument_hard == state) {
if(vr.verbose){
VG_(message)(Vg_DebugMsg,"%s: instrumentation already %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
return;
}
vr.instrument_hard = state;
VG_(discard_translations_safely)( (Addr)0x1000, ~(SizeT)0xfff, "verrou");
vr_clean_cache();
if(vr.instrument_soft_used){
if(vr.instrument_hard && vr.instrument_soft){
vr.instrument_soft_used=False;
}
}
if(vr.verbose){
VG_(message)(Vg_DebugMsg, "%s: instrumentation switched %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
}else{//soft
if(vr.instrument_soft_used==False && vr.instrument_hard==VR_INSTR_ON ){
VG_(discard_translations_safely)( (Addr)0x1000, ~(SizeT)0xfff, "verrou");
}
vr.instrument_soft_used= True;
if (vr.instrument_soft == state) {
if(vr.verbose){
VG_(message)(Vg_DebugMsg,"%s: instrumentation (soft) already %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
return;
}
vr.instrument_soft = state;
vr_clean_cache();
if(vr.verbose){
VG_(message)(Vg_DebugMsg, "%s: instrumentation (soft) switched %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
}
}
void vr_set_rounding_mode(const char* modeStr){
enum vr_RoundingMode mode= verrou_rounding_mode_enum(modeStr);
if(mode==VR_ENUM_SIZE){
VG_(umsg)("Invalid mode %s\n", modeStr);
}else{
if(mode!=vr.roundingMode){
vr.roundingMode=mode;
verrou_set_rounding_mode(mode);
VG_(discard_translations_safely)( (Addr)0x1000, ~(SizeT)0xfff, "verrou");
if(vr.verbose){
VG_(umsg)("New rounding mode: %s", modeStr);
}
}else{
if(vr.verbose){
VG_(umsg)("Keep rounding mode: %s", modeStr);
}
}
}
}
// * Enter/leave deterministic section
static void vr_deterministic_section_name (unsigned int level,
HChar * name,
unsigned int len)
{
Addr ips[8];
const HChar* fnname;
const HChar* filename;
UInt linenum;
Addr addr;
DiEpoch de = VG_(current_DiEpoch)();
Int n_ips=VG_(get_StackTrace)(VG_(get_running_tid)(),
ips, 8,
NULL, NULL,
0);
if(level< n_ips){
addr = ips[level];
VG_(get_fnname)(de, addr, &fnname);
VG_(get_filename_linenum)(de,
addr,
&filename,
NULL,
&linenum);
VG_(snprintf)(name, len,
"%s (%s:%u)", fnname, filename, linenum);
}else{
VG_(tool_panic)("invalid level for vr_deterministic_section_name");
}
}
static ULong vr_deterministic_section_hash (HChar const*const name)
{
ULong hash = VG_(getpid)();
int i = 0;
while (name[i] != 0) {
hash += i * name[i];
++i;
}
return hash;
}
static void vr_start_deterministic_section (unsigned int level) {
HChar name[256];
ULong hash;
vr_deterministic_section_name (level, name, 256);
hash = vr_deterministic_section_hash (name);
verrou_seed_save_state ();
verrou_set_seed (hash);
#ifdef USE_VERROU_QUADMATH
mcaquad_set_seed (hash);
#endif
VG_(message)(Vg_DebugMsg, "Entering deterministic section %llu: %s\n",
hash, name);
}
static void vr_stop_deterministic_section (unsigned int level) {
HChar name[256];
vr_deterministic_section_name (level, name, 256);
VG_(message)(Vg_DebugMsg, "Leaving deterministic section: %s\n",
name);
verrou_seed_restore_state ();
#ifdef USE_VERROU_QUADMATH
mcaquad_set_random_seed ();
#endif
}
#ifdef PROFILING_EXACT
static void vr_print_profiling_exact(void){
unsigned int num;
unsigned int numExact;
verrou_get_profiling_exact(&num,&numExact);
VG_(umsg)("%s #total: %u #exact: %u\n","Profiling exact", num, numExact);
}
#endif
void vr_print_denorm_counter(void){
if(vr.backend==vr_checkdenorm){
checkdenorm_print_counter(&VG_(umsg));
}else{
if(vr.verbose){
VG_(umsg)("PRINT_COUNT_DENORM client request needs --count-denorm=yes\n");
}
}
}
void vr_reset_denorm_counter(void){
if(vr.backend==vr_checkdenorm){
checkdenorm_reset_counter();
}else{
if(vr.verbose){
VG_(umsg)("RESET_COUNT_DENORM client request needs --count-denorm=yes\n");
}
}
}
// * Handle client requests
// ** GDB monitor commands
static void printIpDescGdb(UInt n, DiEpoch ep, Addr ip, void* uu_opaque) {
InlIPCursor *iipc = VG_(new_IIPC)(ep, ip);
do {
const HChar *buf = VG_(describe_IP)(ep, ip, iipc);
VG_(gdb_printf)( "\t%s\n", buf);
} while (VG_(next_IIPC)(iipc));
VG_(delete_IIPC)(iipc);
}
static void vr_handle_monitor_instrumentation_print (void) {
VG_(gdb_printf)("instrumentation status:\n");
VG_(gdb_printf)("\tsoft: %s\n", (vr.instrument_soft==VR_INSTR_ON) ? "ON" : "OFF");
VG_(gdb_printf)("\thard: %s\n", (vr.instrument_hard==VR_INSTR_ON) ? "ON" : "OFF");
}
static Bool vr_handle_monitor_verbose (HChar ** ssaveptr) {
HChar * arg = VG_(strtok_r)(0, " ", ssaveptr);
if (!arg) { /* no argument */
return False;
}
switch (VG_(keyword_id) ("on off", arg, kwd_report_duplicated_matches)) {
case -2: /* multiple matches */
return True;
case -1: /* not found */
return False;
case 0:/* on */
vr.verbose=True;
return True;
case 1: /* off */
vr.verbose=False;
return True;
}
return False;
}
static Bool vr_handle_monitor_instrumentation (HChar ** ssaveptr) {
HChar * arg = VG_(strtok_r)(0, " ", ssaveptr);
if (!arg) { /* no argument */
vr_handle_monitor_instrumentation_print();
return True;
}
Vr_Instr instrStatus= VR_INSTR;
switch (VG_(keyword_id) ("on start off stop", arg, kwd_report_duplicated_matches)) {
case -2: /* multiple matches */
return True;
case -1: /* not found */
return False;
case 0:/* on */
case 1:
instrStatus=VR_INSTR_ON;
break;
case 2: /* off */
case 3:
instrStatus=VR_INSTR_OFF;
break;
}
if(instrStatus!= VR_INSTR){
HChar * subarg = VG_(strtok_r)(0, " ", ssaveptr);
Bool isHard=True;
if(arg!=NULL){
switch (VG_(keyword_id) ("hard soft", subarg, kwd_report_duplicated_matches)) {
case -2: /* multiple matches */
return True;
case -1: /* not found */
return False;
case 0:
isHard=True;
break;
case 1:
isHard=False;
break;
}
}
vr_set_instrument_state("Monitor", instrStatus, isHard);
vr_handle_monitor_instrumentation_print();
return True;
}
return False;
}
static Bool vr_handle_monitor_status (HChar ** ssaveptr) {
HChar* arg = VG_(strtok_r) (0, " ", ssaveptr);
if (arg && (VG_(strcmp)(arg, "back") == 0)) {
unsigned int tid;
for(tid = 1; tid < VG_N_THREADS; tid++){
if( ! VG_(is_valid_tid) (tid )){
continue;
}
VG_(gdb_printf)("Thread tid: %u\n",tid);
static const UInt max_ips=40;
Addr ips[max_ips];
UInt n_ips
= VG_(get_StackTrace)(tid, ips, max_ips,
NULL/*array to dump SP values in*/,
NULL/*array to dump FP values in*/,
0/*first_ip_delta*/);
VG_(apply_StackTrace)( printIpDescGdb, NULL, VG_(current_DiEpoch)(), ips, n_ips );
VG_(gdb_printf)("\n");
}
return True;
}
if (arg && (VG_(strcmp)(arg, "instrumentation") == 0)) {
vr_handle_monitor_instrumentation_print ();
return True;
}
VG_(gdb_printf)("status invalid key %s\n", arg);
return False;
}
static Bool vr_handle_monitor_rounding (HChar ** ssaveptr) {
HChar* arg = VG_(strtok_r) (0, " ", ssaveptr);
vr_set_rounding_mode((char const*const)arg);
return True;
}
static Bool vr_handle_monitor_help (void) {
VG_(gdb_printf)("\n");
VG_(gdb_printf)("verrou monitor commands:\n");
VG_(gdb_printf)(" help : print this help\n");
VG_(gdb_printf)(" display_counter : print instruction counters\n");
VG_(gdb_printf)(" reset_counter : reset instruction counters\n");
VG_(gdb_printf)(" rounding ROUNDING_MODE : switch to ROUNDING_MODE\n");
VG_(gdb_printf)(" instrumentation [on|start|off|stop] [hard|soft] : set instrumentation state\n");
VG_(gdb_printf)(" status back : print backtrace foreach tread\n");
VG_(gdb_printf)(" status instrumentation : print instrumentation status\n");
VG_(gdb_printf)(" verbose on|off : set verbose status\n");
VG_(gdb_printf)("\n");
return True;
}
static Bool vr_handle_monitor_command (HChar * req) {
HChar * wcmd;
HChar s[VG_(strlen(req)) + 1];
HChar *ssaveptr;
VG_(strcpy)(s, req);
wcmd = VG_(strtok_r)(s, " ", &ssaveptr);
switch (VG_(keyword_id) ("help instrumentation display_counter reset_counter status rounding verbose",
wcmd, kwd_report_duplicated_matches)) {
case -2: /* multiple matches */
return True;
case -1: /* not found */
return False;
case 0: /* help */
return vr_handle_monitor_help ();
case 1: /* instrumentation */
return vr_handle_monitor_instrumentation (&ssaveptr);
case 2: /* display_counter */
vr_ppOpCount();
return True;
case 3: /* reset_counter */
vr_resetCount();
return True;
case 4: /* status */
return vr_handle_monitor_status (&ssaveptr);
case 5:
return vr_handle_monitor_rounding (&ssaveptr);
case 6:
return vr_handle_monitor_verbose (&ssaveptr);
}
return False;
}
static Bool vr_isInstrumentedClr(Vr_Prec prec){
Bool res=(vr.instrument_hard && vr.instrument_soft) && vr.instr_prec[prec];
if((vr.genBackTraceBool || vr.useBackTraceBool) && res){
vr_backtrace_dyn_BB();
}
return res && vr.instrument_soft_back;
}
// ** Client requests entry point
Bool vr_handle_client_request (ThreadId tid, UWord *args, UWord *ret) {
if (!VG_IS_TOOL_USERREQ('V','R', args[0])
&& VG_USERREQ__GDB_MONITOR_COMMAND != args[0])
return False;
switch (args[0]) {
case VR_USERREQ__START_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", True, True);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__STOP_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", False, True);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__START_SOFT_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", True, False);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__STOP_SOFT_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", False, False);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__START_DETERMINISTIC:
vr_start_deterministic_section (args[1]);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__STOP_DETERMINISTIC:
vr_stop_deterministic_section (args[1]);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__DISPLAY_COUNTERS:
vr_ppOpCount();
*ret = 0; /* meaningless */
break;
case VR_USERREQ__DUMP_COVER:
*ret=vr_dumpCov();
break;
case VR_USERREQ__PRINT_PROFILING_EXACT:
#ifdef PROFILING_EXACT
vr_print_profiling_exact();
verrou_init_profiling_exact();
#else
VG_(umsg)("Warning : verrou need to be compiled with --enable-verrou-profiling-exact to use the client request VERROU_PRINT_PROFILING_EXACT;\n");
#endif
*ret=0;
break;
case VR_USERREQ__COUNT_FP_INSTRUMENTED:
*ret=(UWord)vr_count_fp_instrumented();
break;
case VR_USERREQ__COUNT_FP_NOT_INSTRUMENTED:
*ret=(UWord)vr_count_fp_not_instrumented();
break;
case VG_USERREQ__GDB_MONITOR_COMMAND:
if (vr_handle_monitor_command((HChar*)args[1])) {
*ret = 1;
return True;
} else {
*ret = 0;
return False;
}
break;
case VR_USERREQ__SET_SEED:
vr.firstSeed=args[1];
VG_(umsg)("New seed : %llu\n", vr.firstSeed);
verrou_set_seed (vr.firstSeed);
vr_clean_cache_seed();
#ifdef USE_VERROU_QUADMATH
mcaquad_set_seed(vr.firstSeed);
#endif
*ret = 0; /* meaningless */
break;
case VR_USERREQ__PRANDOM_UPDATE:
verrou_updatep_prandom();
if(vr.verbose){
if( (vr.roundingMode==VR_PRANDOM)||(vr.roundingMode==VR_PRANDOM_DET) ||(vr.roundingMode==VR_PRANDOM_COMDET)){
VG_(umsg)("\tPRANDOM_UPDATE: pvalue=%f\n", verrou_prandom_pvalue());
}
}
break;
case VR_USERREQ__PRANDOM_UPDATE_VALUE:
{
double value=VG_(strtod)((char*)args[1],NULL);
verrou_updatep_prandom_double(value);
}
if(vr.verbose){
if( (vr.roundingMode==VR_PRANDOM)||(vr.roundingMode==VR_PRANDOM_DET) ||(vr.roundingMode==VR_PRANDOM_COMDET)){
VG_(umsg)("\tPRANDOM_UPDATE_VALUE: pvalue=%f\n", verrou_prandom_pvalue());
}
}
break;
case VR_USERREQ__GET_ROUNDING:
*ret=(UWord)vr.roundingMode;
break;
case VR_USERREQ__GET_LIBM_ROUNDING:
*ret=(UWord)vr.roundingMode;
break;
case VR_USERREQ__GET_LIBM_ROUNDING_NO_INST:
*ret=(UWord)vr.roundingModeNoInst;
break;
case VR_USERREQ__NAN_DETECTED:
vr_handle_NaN();
break;
case VR_USERREQ__INF_DETECTED:
vr_handle_Inf();
break;
case VR_USERREQ__IS_INSTRUMENTED_FLOAT:
*ret=(UWord)(vr_isInstrumentedClr(VR_PREC_FLT));
break;
case VR_USERREQ__IS_INSTRUMENTED_DOUBLE:
*ret=(UWord)(vr_isInstrumentedClr(VR_PREC_DBL));
break;
case VR_USERREQ__IS_INSTRUMENTED_LDOUBLE:
*ret=(UWord)(vr_isInstrumentedClr(VR_PREC_LDBL));
break;
case VR_USERREQ__COUNT_OP:
*ret=(UWord)( (Bool)(vr.count) );
break;
case VR_USERREQ__GENERATE_EXCLUDE_SOURCE:
vr_generate_exclude_source((const char*)args[1], *(int*)args[2], (const char*)args[3], (const char*)args[4] );
break;
case VR_USERREQ__IS_INSTRUMENTED_EXCLUDE_SOURCE:
*ret=(UWord)( (Bool)(vr_clrIsInstrumented((const char*)args[1], *(int*)args[2], (const char*)args[3],(const char*)args[4] )));
break;
case VR_USERREQ__REGISTER_CACHE:
vr_register_cache((unsigned int*) args[1], (unsigned int)args[2]);
break;
case VR_USERREQ__GET_SEED:
*ret = verrou_get_seed();
break;
case VR_USERREQ__REGISTER_CACHE_SEED:
vr_register_cache_seed((unsigned int*) args[1]);
break;
case VR_USERREQ__FLOAT_CONV:
*ret=(UWord)( vr.float_conv );
break;
case VR_USERREQ__PRINT_COUNT_DENORM:
vr_print_denorm_counter();
break;
case VR_USERREQ__RESET_COUNT_DENORM:
vr_reset_denorm_counter();
break;
case VR_USERREQ__SET_ROUNDING_MODE:
vr_set_rounding_mode((char const*const)args[1]);
break;
}
return True;
}