-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathETISS.cpp
1006 lines (931 loc) · 37.9 KB
/
ETISS.cpp
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
@copyright
<pre>
Copyright 2018 Infineon Technologies AG
This file is part of ETISS tool, see <https://github.com/tum-ei-eda/etiss>.
The initial version of this software has been created with the funding support by the German Federal
Ministry of Education and Research (BMBF) in the project EffektiV under grant 01IS13022.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</pre>
@author Marc Greim <[email protected]>, Chair of Electronic Design Automation, TUM
@date July 29, 2014
@version 0.1
*/
/**
@file ETISS.cpp
@brief Implementation of etiss/ETISS.h except for
etiss::preloadLibraries
@detail
*/
#include "etiss/ETISS.h"
#include "etiss/fault/Stressor.h"
#include <csignal>
#include <cstring>
#include <fstream>
#include <functional>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/algorithm/string.hpp>
#if ETISS_USE_DLSYM
#include <dlfcn.h>
#endif
using namespace etiss;
std::string etiss_defaultjit_;
std::list<std::shared_ptr<etiss::LibraryInterface>> etiss_libraries_;
std::recursive_mutex etiss_libraries_mu_;
boost::program_options::variables_map vm;
std::vector<std::string> pluginOptions = {"plugin.logger.logaddr", "plugin.logger.logmask", "plugin.gdbserver.port"};
std::set<std::string> etiss::listCPUArchs()
{
std::set<std::string> ret;
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
if ((*iter).get() != 0)
{
if (!(*iter)->isEmpty())
{
for (unsigned i = 0; i < (*iter)->countCPUArchs(); i++)
{
std::string jit = (*iter)->nameCPUArch(i);
if (ret.find(jit) != ret.end())
{
etiss::log(etiss::ERROR, "CPUArch provided by multiple libraries: \"" + jit + "\"");
}
else
{
ret.insert(jit);
}
}
}
}
}
return ret;
}
std::set<std::string> etiss::listJITs()
{
std::set<std::string> ret;
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
if ((*iter).get() != 0)
{
if (!(*iter)->isEmpty())
{
for (unsigned i = 0; i < (*iter)->countJITs(); i++)
{
std::string jit = (*iter)->nameJIT(i);
if (ret.find(jit) != ret.end())
{
etiss::log(etiss::ERROR, "JIT provided by multiple libraries: \"" + jit + "\"");
}
else
{
ret.insert(jit);
}
}
}
}
}
return ret;
}
std::set<std::string> etiss::listPlugins()
{
std::set<std::string> ret;
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
if ((*iter).get() != 0)
{
if (!(*iter)->isEmpty())
{
for (unsigned i = 0; i < (*iter)->countPlugins(); i++)
{
std::string jit = (*iter)->namePlugin(i);
if (ret.find(jit) != ret.end())
{
etiss::log(etiss::ERROR, "JIT provided by multiple libraries: \"" + jit + "\"");
}
else
{
ret.insert(jit);
}
}
}
}
}
return ret;
}
std::shared_ptr<JIT> etiss::getJIT(std::string name, std::map<std::string, std::string> options)
{
std::shared_ptr<JIT> jit;
std::string ujit;
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
std::shared_ptr<LibraryInterface> lib = *iter;
if (lib.get() != 0)
{
if (!lib->isEmpty())
{
for (unsigned i = 0; i < lib->countJITs(); i++)
{
if (lib->nameJIT(i) == name)
{
if (jit.get() != 0)
{
etiss::log(etiss::ERROR, "JIT provided by multiple libraries: using \"" + name +
"\" from library \"" + ujit +
"\" [also provided by library \"" + lib->getName() + "\"]");
}
else
{
etiss::JIT *ca = lib->createJIT(i, options);
if (ca == 0)
{
etiss::log(etiss::ERROR,
"Failed to create JIT via library interface \"" + lib->getName() + "\"");
}
else
{
jit = std::shared_ptr<JIT>(ca, [lib](JIT *j) { lib->deleteJIT(j); });
ujit = lib->getName();
}
}
}
}
}
}
}
return jit;
}
std::shared_ptr<CPUArch> etiss::getCPUArch(std::string name, std::map<std::string, std::string> options)
{
std::shared_ptr<CPUArch> arch;
std::string uarch;
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
std::shared_ptr<LibraryInterface> lib = *iter;
if (lib.get() != 0)
{
if (!lib->isEmpty())
{
for (unsigned i = 0; i < lib->countCPUArchs(); i++)
{
if (lib->nameCPUArch(i) == name)
{
if (arch.get() != 0)
{
etiss::log(etiss::ERROR, "CPU Architecture provided by multiple libraries: using \"" +
name + "\" from library \"" + uarch +
"\" [also provided by library \"" + lib->getName() + "\"]");
}
else
{
etiss::CPUArch *ca = lib->createCPUArch(i, options);
if (ca == 0)
{
etiss::log(etiss::ERROR,
"Failed to create CPUArch via library interface \"" + lib->getName() + "\"");
}
else
{
arch = std::shared_ptr<CPUArch>(ca, [lib](CPUArch *a) { lib->deleteCPUArch(a); });
uarch = lib->getName();
}
}
}
}
}
}
}
return arch;
}
std::shared_ptr<Plugin> etiss::getPlugin(std::string name, std::map<std::string, std::string> options)
{
std::shared_ptr<Plugin> ptrPlugin;
std::string strPluginName;
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
std::shared_ptr<LibraryInterface> lib = *iter;
if (lib.get() != 0)
{
if (!lib->isEmpty())
{
for (unsigned i = 0; i < lib->countPlugins(); i++)
{
if (lib->namePlugin(i) == name)
{
if (ptrPlugin.get() != 0)
{
etiss::log(etiss::ERROR, "Plugin provided by multiple libraries: using \"" + name +
"\" from library \"" + strPluginName +
"\" [also provided by library \"" + lib->getName() + "\"]");
}
else
{
etiss::Plugin *ca = lib->createPlugin(i, options);
if (ca == 0)
{
etiss::log(etiss::ERROR,
"Failed to create Plugin via library interface \"" + lib->getName() + "\"");
}
else
{
ptrPlugin = std::shared_ptr<Plugin>(ca, [lib](Plugin *p) { lib->deletePlugin(p); });
strPluginName = lib->getName();
etiss::log(etiss::INFO, "Plugin \"" + name + "\" loaded via library interface \"" +
strPluginName + "\"\n");
}
}
break;
}
}
}
}
}
return ptrPlugin;
}
bool etiss::loadLibrary(std::string path, std::string name)
{
auto lib = etiss::LibraryInterface::openSharedLibrary(path, name);
if (lib.get())
{
addLibrary(lib);
return true;
}
return false;
}
void etiss::addLibrary(std::shared_ptr<etiss::LibraryInterface> libInterface)
{
etiss::LibraryInterface *lif = libInterface.get();
if (lif == 0)
{
return;
}
etiss::forceInitialization();
{
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
etiss_libraries_.push_back(libInterface);
}
// if no default jit is present, try to use one from this library
for (unsigned i = 0; (etiss_defaultjit_.size() <= 0) && (i < libInterface->countJITs()); i++)
{
etiss_defaultjit_ = libInterface->nameJIT(i);
}
}
std::set<std::string> etiss::listLibraries()
{
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
std::set<std::string> ret;
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
ret.insert((*iter)->getName() + "[" + (*iter)->versionInfo() + "]");
}
return ret;
}
std::set<std::string> etiss::listLibraryPrefixes()
{
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
std::set<std::string> ret;
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
ret.insert((*iter)->getName());
}
return ret;
}
std::shared_ptr<etiss::JIT> etiss::getDefaultJIT()
{
return getJIT(etiss_defaultjit_);
}
// transfer controll to etiss console on SIGINT
void (*etiss_prev_SIGINT_handler)(int) = 0;
bool etiss_SIGINT_handler_enabled = false;
void etiss_SIGINT_handler(int sig)
{
if (sig == SIGINT)
{
std::cout << std::endl << "\033[0;31m" << std::endl;
/// TODO etiss::transferCMDControll();
std::cout << std::endl << "\033[0m" << std::endl;
}
else
{ // false handler assignment
signal(sig, SIG_DFL);
raise(sig);
return;
}
}
//__attribute__((destructor))
static void etiss_remove_SIGINT()
{
if (etiss_SIGINT_handler_enabled)
{
etiss_prev_SIGINT_handler = signal(SIGINT, etiss_prev_SIGINT_handler);
etiss_SIGINT_handler_enabled = false;
}
}
/// replaces __attribute__((destructor)) in a portable way
static class helper_class_etiss_2
{
public:
~helper_class_etiss_2() { etiss_remove_SIGINT(); }
} helper_class_etiss_2;
// initialize
CSimpleIniA *po_simpleIni;
void etiss_loadIni(std::string fileName)
{
if (po_simpleIni == NULL)
po_simpleIni = new CSimpleIniA(true, true, true);
SI_Error rc = po_simpleIni->LoadFile(fileName.c_str());
if (rc < 0)
std::cout << "Initializer::loadIni(): Failed to load Ini: " << fileName << std::endl;
else
std::cout << "Initializer::loadIni(): Ini sucessfully loaded " << fileName << std::endl;
}
void etiss::Initializer::loadIni(std::list<std::string> *files)
{
std::cout << " Load ini file." << std::endl;
// create ini parser
if (po_simpleIni == NULL)
po_simpleIni = new CSimpleIniA(true, true, true);
else
std::cout << "Info: simpleIni already exists!" << std::endl;
// load file
for (auto it_files : *files)
{
// the check above is sufficient no need for this / checked for previous cases false and last true gives true not the case based on files structure
etiss_loadIni(it_files);
}
}
void etiss_loadIniConfigs()
{
if (!po_simpleIni) // no .ini files were given.
{
return;
}
std::cout << " Load Configs from .ini files:" << std::endl;
// preload loglevel
if (!etiss::cfg().isSet("etiss.loglevel"))
{
etiss::cfg().set<int>("etiss.loglevel", po_simpleIni->GetLongValue("IntConfigurations", "etiss.loglevel", etiss::WARNING));
}
{
int ll = cfg().get<int>("etiss.loglevel", etiss::WARNING);
if (ll >= 0 && ll <= etiss::VERBOSE)
{ // valid log level
// dnm
// etiss::verbosity() = etiss::VERBOSE;
etiss::verbosity() = (Verbosity)ll;
etiss::log(etiss::VERBOSE, "Log level set to VERBOSE");
}
else
{
etiss::verbosity() = etiss::WARNING;
etiss::log(etiss::ERROR, "Specified log level is not valid. must range between 0 (= "
"silent) and 5 (= verbose)");
}
}
// get all sections
CSimpleIniA::TNamesDepend sections;
po_simpleIni->GetAllSections(sections);
for (auto iter_section : sections)
{
// only load config sections
if (std::string(iter_section.pItem) != "StringConfigurations" &&
std::string(iter_section.pItem) != "BoolConfigurations" &&
std::string(iter_section.pItem) != "IntConfigurations")
{
continue;
}
etiss::log(etiss::INFO, std::string(" [") + iter_section.pItem + ']');
// get all keys in a section
CSimpleIniA::TNamesDepend keys;
po_simpleIni->GetAllKeys(iter_section.pItem, keys);
for (auto iter_key : keys)
{
std::stringstream message;
bool warning = false;
// skip loglevel
if (std::string(iter_key.pItem) == "etiss.loglevel")
continue;
// check if cfg is already set
if (etiss::cfg().isSet(iter_key.pItem))
{
message << " cfg already set on command line. ";
message << " " << iter_key.pItem << "=";
const ::std::type_info& type = vm[std::string(iter_key.pItem)].value().type() ;
if (type == typeid(::std::string))
message << vm[std::string(iter_key.pItem)].as<std::string>() << ",";
else if (type == typeid(int))
message << vm[std::string(iter_key.pItem)].as<int>() << ",";
else if (type == typeid(bool))
message << std::boolalpha << vm[std::string(iter_key.pItem)].as<bool>() << ",";
warning = true;
}
else
{
// write key (=option) to message.
message << " " << iter_key.pItem << "=";
// get all values of a key with multiple values
CSimpleIniA::TNamesDepend values;
po_simpleIni->GetAllValues(iter_section.pItem, iter_key.pItem, values);
for (auto iter_value : values)
{
// Handle configurations
if (std::string(iter_section.pItem) == "StringConfigurations")
{
etiss::cfg().set<std::string>(iter_key.pItem, iter_value.pItem);
}
else if (std::string(iter_section.pItem) == "BoolConfigurations")
{
std::string itemval = iter_value.pItem;
boost::algorithm::to_lower(itemval); // converts itemval to lower case string
bool val;
if ((itemval == "true") | (itemval == "yes") | (itemval == "1") | (itemval == "t")) val = true;
else if ((itemval == "false") | (itemval == "no") | (itemval == "0") | (itemval == "f")) val = false;
else etiss::log(etiss::FATALERROR, std::string("Configuration value ") + iter_key.pItem + " could not be parsed as boolean");
etiss::cfg().set<bool>(iter_key.pItem, val);
}
else if (std::string(iter_section.pItem) == "IntConfigurations") // already load!
{
std::string itemval = iter_value.pItem;
std::size_t sz = 0;
long long val;
try {
val = std::stoll(itemval, &sz, 0);
}
// catch invalid_argument exception.
catch (std::invalid_argument const&){
etiss::log(etiss::FATALERROR, std::string("Configuration value ") + iter_key.pItem + " could not be parsed as integer");
}
etiss::cfg().set<long long>(iter_key.pItem, val);
// we use double, as long could have only 32 Bit (e.g. on Windows)
// and long long is not offered by the ini library
}
else
// we don't add a DoubleConfigurations section, as converting them
// to and from strings could provoke accuracy issues.
// To support double, a Configuration::get<double>() has to be
// added to Misc.cpp
{
message << " Section not found for Value:";
warning = true;
}
// write item (=option value) to message.
message << iter_value.pItem << ",";
}
// check if more than one value is set in the ini file
if (values.size() > 1)
{
warning = true;
message << " Multi values. Take only LAST one!";
}
}
// add message to etiss log.
etiss::log(warning ? etiss::WARNING : etiss::INFO, message.str());
}
}
}
void etiss::Initializer::loadIniPlugins(std::shared_ptr<etiss::CPUCore> cpu)
{
std::map<std::string, std::string> options;
for (auto iter = pluginOptions.begin(); iter != pluginOptions.end(); iter++)
{
if (etiss::cfg().isSet(*iter))
{
options[*iter] = std::string(vm[std::string(*iter)].as<std::string>());
etiss::log(etiss::INFO, *iter + " written from command line\n" + " options[" +
std::string(*iter) +
"] = " + std::string(vm[std::string(*iter)].as<std::string>()) + "\n");
}
}
if (vm.count("pluginToLoad"))
{
const std::vector<std::string> pluginList = vm["pluginToLoad"].as<std::vector<std::string>>();
for (auto pluginName = pluginList.begin(); pluginName != pluginList.end(); pluginName++)
{
std::string::size_type pos = std::string(*pluginName).length();
bool pluginAlreadyPresent = false;
for (auto iter : *cpu->getPlugins())
{
std::string pluginNamecpu = iter->getPluginName();
if (pos != std::string::npos)
{
pluginNamecpu = pluginNamecpu.substr(0, pos);
}
if (pluginNamecpu == *pluginName)
{
pluginAlreadyPresent = true;
break;
}
}
if (pluginAlreadyPresent)
{
etiss::log(etiss::WARNING, " Warning: Plugin already present. Skipping it: " + *pluginName + "\n");
continue;
}
etiss::log(etiss::INFO, " Adding Plugin " + *pluginName + "\n");
cpu->addPlugin(etiss::getPlugin(*pluginName, options));
}
}
if (!po_simpleIni)
{
etiss::log(etiss::WARNING, "Ini file not loaded. Can't load plugins from simpleIni!");
return;
}
// get all sections
CSimpleIniA::TNamesDepend sections;
po_simpleIni->GetAllSections(sections);
for (auto iter_section : sections)
{
// only load Plugin sections
if (std::string(iter_section.pItem).substr(0, 6) != std::string("Plugin"))
continue;
std::string pluginName = std::string(iter_section.pItem).substr(7);
std::string::size_type pos = pluginName.length();
// check if Plugin is already present
bool pluginAlreadyPresent = false;
for (auto iter : *cpu->getPlugins())
{
std::string pluginNamecpu = iter->getPluginName();
if (pos != std::string::npos)
{
pluginNamecpu = pluginNamecpu.substr(0, pos);
}
if (pluginNamecpu == pluginName)
{
pluginAlreadyPresent = true;
break;
}
}
if (pluginAlreadyPresent)
{
etiss::log(etiss::WARNING, " Warning: Plugin already present. Skipping it: " + pluginName + "\n");
continue;
}
etiss::log(etiss::INFO, " Adding Plugin " + pluginName + "\n");
// get all keys in a section = plugin option
CSimpleIniA::TNamesDepend keys;
po_simpleIni->GetAllKeys(iter_section.pItem, keys);
for (auto iter_key : keys)
{
// get all values of a key with multiple values = value of option
CSimpleIniA::TNamesDepend values;
po_simpleIni->GetAllValues(iter_section.pItem, iter_key.pItem, values);
if (!etiss::cfg().isSet(iter_key.pItem))
{
std::stringstream ss;
ss << iter_key.pItem << " not set on the command line. Checking in .ini file.";
etiss::log(etiss::INFO, ss.str());
for (auto iter_value : values)
{
options[iter_key.pItem] = iter_value.pItem;
etiss::log(etiss::INFO,
" options[" + std::string(iter_key.pItem) + "] = " + std::string(iter_value.pItem) + "\n\n");
}
// check if more than one value is set in the ini file
if (values.size() > 1)
etiss::log(etiss::WARNING, "Multiple values for option. Took only last one!");
}
}
cpu->addPlugin(etiss::getPlugin(pluginName, options));
}
}
void etiss::Initializer::loadIniJIT(std::shared_ptr<etiss::CPUCore> cpu)
{
// check if JIT is set
if (!etiss::cfg().isSet("jit.type"))
{
etiss::log(etiss::INFO, "No JIT configured. Will use default JIT. \n");
cpu->set(etiss::getDefaultJIT());
return;
}
if (cpu->getJITName() != "")
{
etiss::log(etiss::WARNING,
"etiss::Initializer::loadIniJIT:" + std::string(" JIT already present. Overwriting it."));
}
etiss::log(etiss::INFO, " Adding JIT \"" + cfg().get<std::string>("jit.type", "") + '\"');
cpu->set(getJIT(cfg().get<std::string>("jit.type", "")));
}
std::pair<std::string, std::string> inifileload(const std::string& s)
{
if (s.find("-i") == 0)
{
std::string inifile;
inifile = s.substr(2);
etiss_loadIni(inifile);
}
return make_pair(std::string(), std::string());
}
void etiss_initialize(const std::vector<std::string>& args, bool forced = false)
{
static std::mutex mu_;
static bool initialized_(false);
{
std::lock_guard<std::mutex> lock(mu_);
if (initialized_)
{
if (!forced)
{
etiss::log(etiss::WARNING, "Multiple calls to etiss::initialize");
}
else
{
return;
}
}
else
{
if (forced)
{
etiss::log(etiss::WARNING, "etiss::initialize has not been called before using ETISS library "
"functions. Please add the line \'etiss::initialize(argc,argv);\' "
"at the beginning of \'int main(int argc, char**argv);\'");
}
}
initialized_ = true;
}
{
namespace po = boost::program_options;
try
{
po::options_description desc("Allowed options");
desc.add_options()
("help", "Produce a help message that lists all supported options.")
("arch.cpu", po::value<std::string>(), "The CPU Architecture to simulate.")
("arch.or1k.ignore_sr_iee", po::value<bool>(), "Ignore exception on OpenRISC.")
("arch.or1k.if_stall_cycles", po::value<int>(), "Add instruction stall cycles on OpenRISC.")
("arch.cpu_cycle_time_ps", po::value<int>(), "Sets CPU cycles time on OpenRISC and ARM.")
("etiss.enable_dmi", po::value<bool>(), "Enables the Direct Memory Interface feature of SystemC to speed up memory accesses. This needs to be disabled for memory tracing.")
("etiss.log_pc", po::value<bool>(), "Enables logging of the program counter.")
("etiss.max_block_size", po::value<int>(), "Sets maximum amount of instructions in a block.")
("etiss.output_path_prefix", po::value<std::string>(), "Path prefix to use when writing output files.")
("etiss.loglevel", po::value<int>(), "Verbosity of logging output.")
("jit.gcc.cleanup", po::value<bool>(), "Cleans up temporary files in GCCJIT. ")
("jit.verify", po::value<bool>(), "Run some basic checks to verify the functionality of the JIT engine.")
("jit.debug", po::value<bool>(), "Causes the JIT Engines to compile in debug mode.")
("jit.type", po::value<std::string>(), "The JIT compiler to use.")
("jit.external_headers", po::value<std::string>(), "List of semicolon-separated paths to headers for the JIT to include.")
("jit.external_libs", po::value<std::string>(), "List of semicolon-separated library names for the JIT to link.")
("jit.external_header_paths", po::value<std::string>(), "List of semicolon-separated headers paths for the JIT.")
("jit.external_lib_paths", po::value<std::string>(), "List of semicolon-separated library paths for the JIT.")
("vp.sw_binary_ram", po::value<std::string>(), "Path to binary file to be loaded into RAM.")
("vp.sw_binary_rom", po::value<std::string>(), "Path to binary file to be loaded into ROM.")
("vp.elf_file", po::value<std::string>(), "Load ELF file.")
("vp.stats_file_path", po::value<std::string>(), "Path where the output json file gets stored after bare processor is run.")
("simple_mem_system.print_dbus_access", po::value<bool>(), "Traces accesses to the data bus.")
("simple_mem_system.print_ibus_access", po::value<bool>(), "Traces accesses to the instruction bus.")
("simple_mem_system.print_dbgbus_access", po::value<bool>(), "Traces accesses to the debug bus.")
("simple_mem_system.print_to_file", po::value<bool>(), "Write all tracing to a file instead of the terminal. The file will be located at etiss.output_path_prefix.")
("plugin.logger.logaddr", po::value<std::string>(), "Provides the compare address that is used to check for memory accesses that are redirected to the logger.")
("plugin.logger.logmask", po::value<std::string>(), "Provides the mask that is used to check for memory accesses that are redirected to the logger.")
("plugin.gdbserver.port", po::value<std::string>(), "Option for gdbserver")
("pluginToLoad,p", po::value<std::vector<std::string>>()->multitoken(), "List of plugins to be loaded.")
;
po::command_line_parser parser{args};
po::command_line_parser iniparser{args};
iniparser.options(desc).allow_unregistered().extra_parser(inifileload).run();
parser.options(desc).allow_unregistered().extra_parser(etiss::Configuration::set_cmd_line_boost);
po::parsed_options parsed_options = parser.run();
po::store(parsed_options, vm);
if (vm.count("help"))
{
std::cout << "\nPlease begin all options with --\n\n";
std::cout << desc << "\n";
etiss::log(etiss::FATALERROR, std::string("Please choose the right configurations from the list and re-run.\n"));
}
auto unregistered = po::collect_unrecognized(parsed_options.options, po::include_positional);
for (auto iter_unreg : unregistered)
{
if (iter_unreg.find("-i") != 0 && iter_unreg.find("-p"))
{
etiss::log(etiss::FATALERROR, std::string("Unrecognised option ") + iter_unreg +
"\n\t Please use --help to list all recognised options. \n");
}
}
for (po::variables_map::iterator i = vm.begin() ; i != vm.end() ; ++ i)
{
const po::variable_value& v = i->second;
if (!v.empty())
{
const ::std::type_info& type = v.value().type();
if (type == typeid(::std::string))
{
const ::std::string& val = v.as<::std::string>() ;
etiss::cfg().set<std::string>(std::string(i->first), val);
}
else if (type == typeid(int))
{
int val = v.as<int>();
etiss::cfg().set<int>(std::string(i->first), val);
}
else if (type == typeid(bool))
{
bool val = v.as<bool>();
etiss::cfg().set<bool>(std::string(i->first), val);
}
}
}
}
catch(std::exception& e)
{
etiss::log(etiss::FATALERROR, std::string(e.what()) +
"\n\t Please use --help to list all recognised options. \n");
}
}
etiss_loadIniConfigs();
// log level
{
int ll = cfg().get<int>("etiss.loglevel", etiss::WARNING);
if (ll >= 0 && ll <= etiss::VERBOSE)
{ // valid log level
// dnm
// etiss::verbosity() = etiss::VERBOSE;
etiss::verbosity() = (Verbosity)ll;
etiss::log(etiss::VERBOSE, "Log level set to VERBOSE");
}
else
{
etiss::verbosity() = etiss::WARNING;
etiss::log(etiss::ERROR, "Specified log level is not valid. must range between 0 (= "
"silent) and 5 (= verbose)");
}
}
etiss::py::init(); // init python
etiss_remove_SIGINT();
preloadLibraries();
// load integrated library
if (cfg().get<bool>("etiss.load_integrated_libraries", true))
{
etiss::addLibrary(LibraryInterface::openIntegratedLibrary());
}
// check if some required files can be found
{
std::string path = etiss::installDir();
std::vector<std::string> requiredFiles;
// required files
requiredFiles.push_back(path + "/include/jit/etiss/jit/CPU.h");
// check
for (auto iter = requiredFiles.begin(); iter != requiredFiles.end(); iter++)
{
std::ifstream f(iter->c_str());
if (!f)
{
etiss::log(etiss::WARNING, std::string("Could not find file: ") + *iter + "\n" +
"\t The installation seems broken");
}
}
}
// load fault files
{
std::string faults = cfg().get<std::string>("faults.xml", "");
if (!faults.empty())
{
std::list<std::string> ffs = etiss::split(faults, ';');
for (auto ff : ffs)
{
etiss::fault::Stressor::loadXML(ff);
}
}
}
}
void etiss::initialize(std::vector<std::string>& args)
{
etiss_initialize(args, false);
}
void etiss::forceInitialization()
{
std::vector<std::string> args{};
etiss_initialize(args, true);
}
//__attribute__((destructor))
static void etiss_shutdown()
{
etiss::shutdown(); // TODO: verify with spec. assuming shared library close
// after __attribute__((destructor)) functions have been
// called
// force close open libraries
//{
// std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
// etiss_libraries_.clear();
//}
}
/// replaces __attribute__((destructor)) in a portable way
static class helper_class_etiss_1
{
public:
~helper_class_etiss_1() { etiss_shutdown(); }
} helper_class_etiss_1;
bool etiss_shutdownOk = false;
void etiss::shutdown()
{
if (etiss_shutdownOk) // only on shutdown
return;
etiss_shutdownOk = true;
etiss::fault::Stressor::clear();
// unload libraries
{
std::list<std::weak_ptr<LibraryInterface>> libraries_weak;
{
std::lock_guard<std::recursive_mutex> lock(etiss_libraries_mu_);
for (auto iter = etiss_libraries_.begin(); iter != etiss_libraries_.end(); iter++)
{
libraries_weak.push_back(std::weak_ptr<LibraryInterface>(*iter));
}
etiss_libraries_.clear();
}
for (auto iter = libraries_weak.begin(); iter != libraries_weak.end(); iter++)
{
std::shared_ptr<LibraryInterface> li = iter->lock();
if (li.get() != 0)
{
std::stringstream ss;
ss << "Failed to unload library \"" << li.get()->getName() << "\": ";
ss << li.use_count() - 1 << " references " << std::endl;
etiss::log(etiss::ERROR, ss.str());
}
}
}
// check for existing cpu core instances
{
std::list<std::string> cores = CPUCore::list();
if (cores.size() > 0)
{
for (auto iter = cores.begin(); iter != cores.end(); iter++)
{
etiss::log(etiss::ERROR, std::string("CPU core has not been deleted before "
"etiss::shutdown() call: ") +
*iter);
}
}
}
etiss::py::shutdown();
}
/**
@brief check if etiss::shutdown() was called before exiting main.
*/
//__attribute__((destructor))
static void etiss_check_shutdown()
{
if (!etiss_shutdownOk)
{
etiss::log(etiss::ERROR, "To prevent segmentation faults it is neccessary to call "
"\"etiss::shutdown();\" at the end of main and free any "
"resource acquired through ETISS.");
}
}
/// replaces __attribute__((destructor)) in a portable way
static class helper_class_etiss_3
{
public:
~helper_class_etiss_3() { etiss_check_shutdown(); }
} helper_class_etiss_3;
etiss::Initializer::~Initializer()
{
if (po_simpleIni)
delete po_simpleIni;
etiss::shutdown();
}
std::string etiss::errorMessage(etiss::int32 code, CPUArch *arch)
{
if (code <= 0)
{ // global code
const char *msg = etiss::RETURNCODE::getErrorMessages()[code];
if (msg == 0)
return std::string();
return std::string(msg);
}
else
{ // cpu arch dependent code
if (arch != 0)
{
std::string ret = arch->getName() + ": ";
/// TODO: cpu arch error message function
return ret;
}