forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_rps.cc
2016 lines (1883 loc) · 70.9 KB
/
main_rps.cc
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
/****************************************************************
* file main_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the main function and related, program option parsing,
* code.
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright 2019 - 2022 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* 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, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "refpersys.hh"
extern "C" const char rps_main_gitid[];
const char rps_main_gitid[]= RPS_GITID;
extern "C" const char rps_main_date[];
const char rps_main_date[]= __DATE__;
extern "C" pid_t rps_gui_pid;
/// actually, in function main we have something like asm volatile ("rps_end_of_main: nop");
extern "C" void rps_end_of_main(void);
extern "C" void rps_edit_run_cplusplus_code (Rps_CallFrame*callerframe);
extern "C" void rps_small_quick_tests_after_load (void);
extern "C" std::vector<Rps_Plugin> rps_plugins_vector;
std::vector<Rps_Plugin> rps_plugins_vector;
extern "C" std::string rps_cpluspluseditor_str;
std::string rps_cpluspluseditor_str;
extern "C" std::string rps_cplusplusflags_str;
std::string rps_cplusplusflags_str;
extern "C" std::string rps_dumpdir_str;
std::string rps_dumpdir_str;
extern "C" std::vector<std::string> rps_command_vec;
std::vector<std::string> rps_command_vec;
static void rps_kill_wait_gui_process(void);
error_t rps_parse1opt (int key, char *arg, struct argp_state *state);
struct argp_option rps_progoptions[] =
{
/* ======= the load directory ======= */
{/*name:*/ "load", ///
/*key:*/ RPSPROGOPT_LOADDIR, ///
/*arg:*/ "LOADDIR", ///
/*flags:*/ 0, ///
/*doc:*/ "loads persistent state from LOADDIR, defaults to the source directory", ///
/*group:*/0 ///
},
/* ======= the RefPerSys home directory ======= */
{/*name:*/ "refpersys-home", ///
/*key:*/ RPSPROGOPT_HOMEDIR, ///
/*arg:*/ "HOMEDIR", ///
/*flags:*/ 0, ///
/*doc:*/ "set the RefPerSys homedir, default to $REFPERSYS_HOME or $HOME", ///
/*group:*/0 ///
},
/* ======= debug flags ======= */
{/*name:*/ "debug", ///
/*key:*/ RPSPROGOPT_DEBUG, ///
/*arg:*/ "DEBUGFLAGS", ///
/*flags:*/ 0, ///
/*doc:*/ "To set RefPerSys comma separated debug flags, pass --debug=help to get their list.\n"
" Also from $REFPERSYS_DEBUG environment variable, if provided", ///
/*group:*/0 ///
},
/* ======= debug after load flags ======= */
{/*name:*/ "debug-after-load", ///
/*key:*/ RPSPROGOPT_DEBUG_AFTER_LOAD, ///
/*arg:*/ "DEBUGFLAGS", ///
/*flags:*/ 0, ///
/*doc:*/ "To set RefPerSys comma separated debug flags after the sucessful load.", ///
/*group:*/0 ///
},
/* ======= debug file path ======= */
{/*name:*/ "debug-path", ///
/*key:*/ RPSPROGOPT_DEBUG_PATH, ///
/*arg:*/ "DEBUGFILEPATH", ///
/*flags:*/ 0, ///
/*doc:*/ "Output debug messages into given DEBUGFILEPATH instead of stderr.", ///
/*group:*/0 ///
},
/* ======= dump into given directory ======= */
{/*name:*/ "dump", ///
/*key:*/ RPSPROGOPT_DUMP, ///
/*arg:*/ "DUMPDIR", ///
/*flags:*/ 0, ///
/*doc:*/ "Dump the persistent state to given DUMPDIR directory.", ///
/*group:*/0 ///
},
/* ======= random oids ======= */
{/*name:*/ "random-oid", ///
/*key:*/ RPSPROGOPT_RANDOMOID, ///
/*arg:*/ "NBOIDS", ///
/*flags:*/ 0, ///
/*doc:*/ "Print NBOIDS random object identifiers",
/*group:*/0 ///
},
/* ======= type information ======= */
{/*name:*/ "type-info", ///
/*key:*/ RPSPROGOPT_TYPEINFO, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Show type information (and test tagged integers)", //
/*group:*/0 ///
},
/* ======= syslog-ing ======= */
{/*name:*/ "syslog", ///
/*key:*/ RPSPROGOPT_SYSLOG, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "use system log with syslog(3)", //
/*group:*/0 ///
},
/* ======= without terminal ======= */
{/*name:*/ "no-terminal", ///
/*key:*/ RPSPROGOPT_NO_TERMINAL, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Forcibly disable terminal ANSI escape codes, even if stdout is a tty.", //
/*group:*/0 ///
},
/* ======= without ASLR ======= */
{/*name:*/ "no-aslr", ///
/*key:*/ RPSPROGOPT_NO_ASLR, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Forcibly disable Adress Space Layout Randomization.", //
/*group:*/0 ///
},
/* ======= without quick tests ======= */
{/*name:*/ "no-quick-tests", ///
/*key:*/ RPSPROGOPT_NO_QUICK_TESTS, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Disable quick tests after load by rps_small_quick_tests_after_load.", //
/*group:*/0 ///
},
/* ======= batch ======= */
{/*name:*/ "batch", ///
/*key:*/ RPSPROGOPT_BATCH, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Run in batch mode, that is without any user interface (either graphical or command-line REPL).", //
/*group:*/0 ///
},
/* ======= version info ======= */
{/*name:*/ "version", ///
/*key:*/ RPSPROGOPT_VERSION, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Show version information, then exit.", //
/*group:*/0 ///
},
/* ======= interface thru some FIFO, relevant for JSONRPC ======= */
{/*name:*/ "interface-fifo", ///
/*key:*/ RPSPROGOPT_INTERFACEFIFO, ///
/*arg:*/ "FIFO", ///
/*flags:*/ 0, ///
/*doc:*/ "use a pair of fifo(7) named FIFO.cmd (written) "
"and FIFO.out (read) for communication"
, //
/*group:*/0 ///
},
/* ======= run a shell command with system(3) after load ======= */
{/*name:*/ "run-after-load", ///
/*key:*/ RPSPROGOPT_RUN_AFTER_LOAD, ///
/*arg:*/ "SHELL_COMMAND", ///
/*flags:*/ 0, ///
/*doc:*/ "Run using system(3) the given shell SHELL_COMMAND after load and plugins;\n"
" The environment variable REFPERSYS_PID has been set", //
/*group:*/0 ///
},
/* ======= run a REPL command after load ======= */
{/*name:*/ "command", ///
/*key:*/ RPSPROGOPT_COMMAND, ///
/*arg:*/ "REPL_COMMAND", ///
/*flags:*/ 0, ///
/*doc:*/ "Run the given REPL_COMMAND;\n"
"Try the help command for details.", //
/*group:*/0 ///
},
/* ======= edit the C++ code of a temporary plugin after load ======= */
{/*name:*/ "cplusplus-editor-after-load", ///
/*key:*/ RPSPROGOPT_CPLUSPLUSEDITOR_AFTER_LOAD, ///
/*arg:*/ "EDITOR", ///
/*flags:*/ 0, ///
/*doc:*/ "prefill some C++ temporary file for plugin code,\n"
" edit it with given EDITOR, then compile it"
" and run its " RPS_PLUGIN_INIT_NAME "(const Rps_Plugin*) function.\n"
" (if none is given, use $EDITOR from environment)\n"
, //
/*group:*/0 ///
},
/* ======= extra compilation flags for C++ code above after load ======= */
{/*name:*/ "cplusplus-flags-after-load", ///
/*key:*/ RPSPROGOPT_CPLUSPLUSFLAGS_AFTER_LOAD, ///
/*arg:*/ "FLAGS", ///
/*flags:*/ 0, ///
/*doc:*/ "set to FLAGS the extra compilation flags for the C++ code of the temporary plugin.", //
/*group:*/0 ///
},
/* ======= dlopen a given plugin file after load ======= */
{/*name:*/ "plugin-after-load", ///
/*key:*/ RPSPROGOPT_PLUGIN_AFTER_LOAD, ///
/*arg:*/ "PLUGIN", ///
/*flags:*/ 0, ///
/*doc:*/ "dlopen(3) after load the given PLUGIN "
"(some *.so ELF shared object)"
" and run its " RPS_PLUGIN_INIT_NAME "(const Rps_Plugin*) function", //
/*group:*/0 ///
},
/* ======= command textual read eval print loop lexer testing ======= */
{/*name:*/ "test-repl-lexer", ///
/*key:*/ RPSPROGOPT_TEST_REPL_LEXER, ///
/*arg:*/ nullptr, ///
/*flags:*/ 0, ///
/*doc:*/ "Test the read-eval-print-loop lexer.\n"
" (this option might become obsolete)", //
/*group:*/0 ///
},
/* ======= number of jobs or threads ======= */
{/*name:*/ "jobs", ///
/*key:*/ RPSPROGOPT_JOBS, ///
/*arg:*/ "NBJOBS", ///
/*flags:*/ 0, ///
/*doc:*/ "Run <NBJOBS> threads - default is 3, minimum 2, maximum 20", //
/*group:*/0 ///
},
/* ======= terminating empty option ======= */
{/*name:*/(const char*)0, ///
/*key:*/0, ///
/*arg:*/(const char*)0, ///
/*flags:*/0, ///
/*doc:*/(const char*)0, ///
/*group:*/0 ///
}
};
struct backtrace_state* rps_backtrace_common_state;
const char* rps_progname;
char* rps_run_command_after_load = nullptr;
char* rps_debugflags_after_load = nullptr;
std::vector<std::function<void(Rps_CallFrame*)>> rps_do_after_load_vect;
void* rps_proghdl = nullptr;
bool rps_batch = false;
bool rps_disable_aslr = false;
bool rps_without_terminal_escape = false;
bool rps_without_quick_tests = false;
bool rps_test_repl_lexer = false;
bool rps_syslog_enabled = false;
bool rps_stdout_istty = false;
bool rps_stderr_istty = false;
unsigned rps_debug_flags;
FILE* rps_debug_file;
static char rps_debug_path[128];
static struct rps_fifo_fdpair_st rps_fifo_pair;
pid_t rps_gui_pid;
thread_local Rps_Random Rps_Random::_rand_thr_;
typedef std::function<void(void)> rps_todo_func_t;
static std::vector<rps_todo_func_t> rps_main_todo_vect;
static std::string rps_my_load_dir;
// we may have a pair of FIFO to communicate with some external
// process (for graphical user interface), perhaps mini-edit-fltk on
// https://github.com/bstarynk/misc-basile/ ... The FIFO prefix is
// $FIFOPREFIX. The messages from the GUI user interface to RefPerSys
// are on $FIFOPREFIX.out; the messages from RefPerSys to that GUI
// user interface are on $FIFOPREFIX.cmd
static std::string rps_fifo_prefix;
std::string
rps_get_fifo_prefix(void)
{
return rps_fifo_prefix;
} // end rps_get_fifo_prefix
struct rps_fifo_fdpair_st
rps_get_gui_fifo_fds(void)
{
if (rps_gui_pid)
return rps_fifo_pair;
else return {-1, -1};
} // end rps_get_gui_fifo_fd
pid_t
rps_get_gui_pid(void)
{
return rps_gui_pid;
} // end rps_get_gui_pid
#warning missing code to deal with rps_fifo_prefix and --interface-fifo=<FIFO> program option
unsigned rps_call_frame_depth(const Rps_CallFrame*callframe)
{
if (callframe==nullptr) return 0;
else
return callframe->call_frame_depth();
} // end rps_call_frame_depth
static void rps_parse_program_arguments(int &argc, char**argv);
static char rps_bufpath_homedir[384];
static pthread_t rps_main_thread_handle;
bool rps_is_main_thread(void)
{
return pthread_self() == rps_main_thread_handle;
} // end rps_is_main_thread
const char*
rps_homedir(void)
{
static std::mutex homedirmtx;
std::lock_guard<std::mutex> gu(homedirmtx);
if (RPS_UNLIKELY(rps_bufpath_homedir[0] == (char)0))
{
const char*rpshome = getenv("REFPERSYS_HOME");
const char*home = getenv("HOME");
const char*path = rpshome?rpshome:home;
if (!path)
RPS_FATAL("no RefPerSys home ($REFPERSYS_HOME or $HOME)");
char* rp = realpath(path, nullptr);
if (!rp)
RPS_FATAL("realpath failed on RefPerSys home %s - %m",
path);
if (strlen(rp) >= sizeof(rps_bufpath_homedir) -1)
RPS_FATAL("too long realpath %s on RefPerSys home %s", rp, path);
strncpy(rps_bufpath_homedir, rp, sizeof(rps_bufpath_homedir) -1);
}
return rps_bufpath_homedir;
} // end rps_homedir
const std::string&
rps_get_loaddir(void)
{
return rps_my_load_dir;
} // end rps_get_loaddir
const char*
rps_hostname(void)
{
static char hnambuf[64];
if (RPS_UNLIKELY(!hnambuf[0]))
gethostname(hnambuf, sizeof(hnambuf)-1);
return hnambuf;
} // end rps_hostname
void
rps_emit_gplv3_copyright_notice(std::ostream&outs, std::string path, std::string linprefix, std::string linsuffix)
{
outs << linprefix
<< "GENERATED file " << path << " / DO NOT EDIT!"
<< linsuffix << std::endl;
outs << linprefix
<< "This file is part of the Reflective Persistent System."
<< linsuffix << std::endl;
{
time_t nowtime = time(nullptr);
struct tm nowtm = {};
localtime_r(&nowtime, &nowtm);
outs << linprefix << " © Copyright " << RPS_INITIAL_COPYRIGHT_YEAR
<< " - " << (nowtm.tm_year+1900)
<< " The Reflective Persistent System Team."
<< linsuffix << std::endl;
outs << linprefix
<< " see refpersys.org and contact team@refpersys.org for more."
<< linsuffix << std::endl;
}
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "This program is free software: you can redistribute it and/or modify"
<< linsuffix << std::endl;
outs << linprefix << "it under the terms of the GNU General Public License as published by"
<< linsuffix << std::endl;
outs << linprefix << "the Free Software Foundation, either version 3 of the License, or"
<< linsuffix << std::endl;
outs << linprefix << "(at your option) any later version."
<< linsuffix << std::endl;
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "This program is distributed in the hope that it will be useful,"
<< linsuffix << std::endl;
outs << linprefix << "but WITHOUT ANY WARRANTY; without even the implied warranty of"
<< linsuffix << std::endl;
outs << linprefix << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
<< linsuffix << std::endl;
outs << linprefix << "GNU General Public License for more details."
<< linsuffix << std::endl;
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "You should have received a copy of the GNU "
"General Public License"
<< linsuffix << std::endl;
outs << linprefix << "along with this program. If not, see <http://www.gnu.org/licenses/>."
<< linsuffix << std::endl;
} // end rps_emit_gplv3_copyright_notice
////////////////
void
rps_print_types_info(void)
{
#define TYPEFMT_rps "%-58s:"
printf(TYPEFMT_rps " size align (bytes)\n", "**TYPE**");
#define EXPLAIN_TYPE(Ty) printf(TYPEFMT_rps " %5d %5d\n", #Ty, \
(int)sizeof(Ty), (int)alignof(Ty))
#define EXPLAIN_TYPE2(Ty1,Ty2) printf(TYPEFMT_rps " %5d %5d\n", \
#Ty1 "," #Ty2, \
(int)sizeof(Ty1,Ty2), \
(int)alignof(Ty1,Ty2))
#define EXPLAIN_TYPE3(Ty1,Ty2,Ty3) printf(TYPEFMT_rps " %5d %5d\n", \
#Ty1 "," #Ty2 "," #Ty3, \
(int)sizeof(Ty1,Ty2,Ty3), \
(int)alignof(Ty1,Ty2,Ty3))
#define EXPLAIN_TYPE4(Ty1,Ty2,Ty3,Ty4) printf(TYPEFMT_rps " %5d %5d\n", \
#Ty1 "," #Ty2 "," #Ty3 "," #Ty4, \
(int)sizeof(Ty1,Ty2,Ty3,Ty4), \
(int)alignof(Ty1,Ty2,Ty3,Ty4))
EXPLAIN_TYPE(int);
EXPLAIN_TYPE(double);
EXPLAIN_TYPE(char);
EXPLAIN_TYPE(bool);
EXPLAIN_TYPE(void*);
EXPLAIN_TYPE(std::mutex);
EXPLAIN_TYPE(std::shared_mutex);
EXPLAIN_TYPE(std::recursive_mutex);
EXPLAIN_TYPE(std::atomic<void*>);
EXPLAIN_TYPE(std::lock_guard<std::shared_mutex>);
EXPLAIN_TYPE(std::lock_guard<std::recursive_mutex>);
EXPLAIN_TYPE(std::lock_guard<std::shared_mutex>);
EXPLAIN_TYPE(std::string);
EXPLAIN_TYPE(std::vector<std::string>);
EXPLAIN_TYPE(std::set<std::string>);
EXPLAIN_TYPE2(std::map<Rps_ObjectRef, Rps_Value>);
EXPLAIN_TYPE2(std::unordered_map<std::string, Rps_ObjectRef*>);
EXPLAIN_TYPE3(std::unordered_map<Rps_Id,Rps_ObjectZone*,Rps_Id::Hasher>);
EXPLAIN_TYPE3(std::variant<unsigned, std::function<Rps_Value(void*)>,
std::function<int(void*,Rps_ObjectRef)>>);
EXPLAIN_TYPE(Rps_Backtracer);
EXPLAIN_TYPE(Rps_ClosureValue);
EXPLAIN_TYPE(Rps_ClosureZone);
EXPLAIN_TYPE(Rps_Double);
EXPLAIN_TYPE(Rps_DoubleValue);
EXPLAIN_TYPE(Rps_GarbageCollector);
EXPLAIN_TYPE(Rps_HashInt);
EXPLAIN_TYPE(Rps_Id);
EXPLAIN_TYPE(Rps_ObjectRef);
EXPLAIN_TYPE(Rps_ObjectValue);
EXPLAIN_TYPE(Rps_ObjectZone);
EXPLAIN_TYPE(Rps_Payload);
EXPLAIN_TYPE(Rps_PayloadClassInfo);
EXPLAIN_TYPE(Rps_PayloadSetOb);
EXPLAIN_TYPE(Rps_PayloadVectOb);
EXPLAIN_TYPE(Rps_QuasiZone);
EXPLAIN_TYPE(Rps_SetOb);
EXPLAIN_TYPE(Rps_SetValue);
EXPLAIN_TYPE(Rps_String);
EXPLAIN_TYPE(Rps_StringValue);
EXPLAIN_TYPE(Rps_TupleOb);
EXPLAIN_TYPE(Rps_TupleValue);
EXPLAIN_TYPE(Rps_Type);
EXPLAIN_TYPE(Rps_Value);
EXPLAIN_TYPE(Rps_ZoneValue);
#undef EXPLAIN_TYPE4
#undef EXPLAIN_TYPE3
#undef EXPLAIN_TYPE
#undef TYPEFMT_rps
putchar('\n');
fflush(nullptr);
std::cout << "@@°°@@ The tagged integer one hundred is "
<< Rps_Value::make_tagged_int(100)
<< std::endl
<< "... and the tagged integer minus one billion is "
<< Rps_Value::make_tagged_int(-1000000000)
<< " !!! " << std::endl;
} // end rps_print_types_info
////////////////////////////////////////////////////////////////
// TIME ROUTINES
////////////////////////////////////////////////////////////////
int rps_nbjobs = RPS_NBJOBS_MIN + 1;
static double rps_start_monotonic_time;
static double rps_start_wallclock_real_time;
double rps_elapsed_real_time(void)
{
return rps_monotonic_real_time() - rps_start_monotonic_time;
}
double rps_get_start_wallclock_real_time()
{
return rps_start_wallclock_real_time;
}
static void
rps_check_mtime_files(void)
{
struct stat selfstat = {};
if (stat("/proc/self/exe", &selfstat))
RPS_FATAL("stat /proc/self/exe: %m");
char exebuf[128];
memset (exebuf, 0, sizeof(exebuf));
if (readlink("/proc/self/exe", exebuf, sizeof(exebuf)-1)<0)
RPS_FATAL("readlink /proc/self/exe: %m");
for (const char*const*curpath = rps_files; *curpath; curpath++)
{
int lencurpath = strlen(*curpath);
if (lencurpath < 6 || strstr(*curpath, "attic/"))
continue;
std::string curpathstr(*curpath);
/// Files under webroot could be sent to browser, so we don't
/// care about them being newer than executable....
auto wrp = curpathstr.find("webroot/");
if (wrp < curpathstr.size())
continue;
std::string curfullpathstr= std::string{rps_topdirectory} + "/" + curpathstr;
struct stat curstat = {};
if (stat(curfullpathstr.c_str(), &curstat))
{
RPS_WARNOUT("rps_check_mtime_files: stat " << curfullpathstr << " failed: " << strerror(errno));
continue;
};
if (curstat.st_mtime > (time_t) rps_timelong)
RPS_WARNOUT("rps_check_mtime_files: " << curfullpathstr.c_str()
<< " is younger by "
<< (curstat.st_mtime - (time_t) rps_timelong)
<< " seconds than current executable " << exebuf
<< ", so consider rebuilding with make");
}
char makecmd [128];
memset (makecmd, 0, sizeof(makecmd));
if (snprintf(makecmd, sizeof(makecmd), "make -t -C %s -q objects", rps_topdirectory) < (int)sizeof(makecmd)-1)
{
int bad = system(makecmd);
if (bad)
RPS_WARNOUT("rps_check_mtime_files: " << makecmd
<< " failed with status# " << bad);
else
RPS_INFORMOUT("rps_check_mtime_files: did " << std::string(makecmd) << " successfully");
}
else
RPS_FATAL("rps_check_mtime_files failed to construct makecmd in %s: %m",
rps_topdirectory);
} // end rps_check_mtime_files
/// In a format string passed to strftime, replace .__ with the
/// centisecond fractional part of the time. See of course
/// http://man7.org/linux/man-pages/man3/strftime.3.html etc... Notice
/// that debugging facilities use that function, e.g. it gets called
/// from rps_debug_printf_at used by RPS_DEBUG_LOG and RPS_DEBUG_PRINTF
/// macros.
char *
rps_strftime_centiseconds(char *bfr, size_t len, const char *fmt, double tm)
{
if (!bfr || !fmt || len<4)
return nullptr;
//
memset (bfr, 0, len);
//
struct tm tmstruct;
memset(&tmstruct, 0, sizeof (tmstruct));
//
time_t time = static_cast<time_t>(tm);
strftime(bfr, len, fmt, localtime_r(&time, &tmstruct));
//
char *dotdunder = strstr(bfr, ".__");
if (dotdunder)
{
double intpart = 0.0;
double fraction = modf(tm, &intpart);
char minibuf[16];
memset(minibuf, 0, sizeof (minibuf));
assert(fraction >= 0.0 && fraction < 1.0);
snprintf(minibuf, sizeof (minibuf), "%.02f", fraction);
minibuf[4] = (char)0;
const char* dotminib = strchr(minibuf, '.');
if (dotminib && dotminib<minibuf+sizeof(minibuf)-4)
{
strncpy(dotdunder, dotminib, 3);
}
}
return bfr;
} // end rps_strftime_centiseconds
////////////////////////////////////////////////////////////////
void
rps_extend_env(void)
{
static char pidenv[64];
snprintf(pidenv, sizeof(pidenv), "REFPERSYS_PID=%d", (int)getpid());
putenv(pidenv);
static char gitenv[64];
snprintf(gitenv, sizeof(gitenv), "REFPERSYS_GITID=%s", rps_gitid);
putenv(gitenv);
static char topdirenv[384];
snprintf(topdirenv, sizeof(topdirenv), "REFPERSYS_TOPDIR=%s", rps_topdirectory);
putenv(topdirenv);
static char fifoenv[256];
if (!rps_fifo_prefix.empty()) {
snprintf(fifoenv, sizeof(fifoenv), "REFPERSYS_FIFO_PREFIX=%s", rps_fifo_prefix.c_str());
putenv(fifoenv);
}
} // end rps_extend_env
////////////////////////////////////////////////////////////////
int
main (int argc, char** argv)
{
rps_start_monotonic_time = rps_monotonic_real_time();
rps_start_wallclock_real_time = rps_wallclock_real_time();
rps_stderr_istty = isatty(STDERR_FILENO);
rps_stdout_istty = isatty(STDOUT_FILENO);
rps_progname = argv[0];
rps_proghdl = dlopen(nullptr, RTLD_NOW|RTLD_GLOBAL);
if (!rps_proghdl)
{
fprintf(stderr, "%s failed to dlopen whole program (%s)\n", rps_progname,
dlerror());
exit(EXIT_FAILURE);
};
rps_main_thread_handle = pthread_self();
/// handle early a debug flag request
if (argc > 1
&& !strncmp(argv[1], "--debug=", strlen("--debug=")))
{
rps_set_debug(argv[1]+strlen("--debug="));
}
else if (argc > 1 && argv[1][0]=='-' && argv[1][1]==RPSPROGOPT_DEBUG)
{
rps_set_debug(argv[1]+2);
};
// also use REFPERSYS_DEBUG
{
const char*debugenv = getenv("REFPERSYS_DEBUG");
if (debugenv)
rps_set_debug(debugenv);
}
// For weird reasons, the program arguments are parsed more than
// once... We don't care that much in practice...
RPS_ASSERT(argc>0);
// we forcibly set the REFPERSYS_PID environment variable
{
static char envpid[32];
if (snprintf(envpid, sizeof(envpid), "REFPERSYS_PID=%d", (int)getpid()) < 1)
RPS_FATAL("failed to snprintf buffer for REFPERSYS_PID: %m");
if (putenv(envpid))
RPS_FATAL("failed to putenv %s %m", envpid);
}
/// disable ASLR programmatically if --no-aslr is passed ; this
/// should ease low-level debugging with GDB
/// https://en.wikipedia.org/wiki/Address_space_layout_randomization
/// see https://askubuntu.com/a/507954/64680
rps_disable_aslr = false;
{
for (int ix=1; ix<argc; ix++)
{
if (!strcmp(argv[ix], "--no-aslr"))
rps_disable_aslr = true;
else if (!strcmp(argv[ix], "-B") || !strcmp(argv[ix], "--batch"))
rps_batch = true;
else if (!strcmp(argv[ix], "--without-terminal"))
rps_without_terminal_escape = true;
}
if (rps_disable_aslr)
{
if (personality(ADDR_NO_RANDOMIZE) == -1)
RPS_FATAL("%s failed to disable ASLR: %m", rps_progname);
else
RPS_INFORM("%s disabled ASLR (git %s).", rps_progname, rps_gitid);
}
}
Rps_Agenda::initialize();
unsetenv("LANG");
unsetenv("LC_ADDRESS");
unsetenv("LC_ALL");
unsetenv("LC_IDENTIFICATION");
unsetenv("LC_MEASUREMENT");
unsetenv("LC_MONETARY");
unsetenv("LC_NAME");
unsetenv("LC_NUMERIC");
unsetenv("LC_NUMERIC");
unsetenv("LC_PAPER");
unsetenv("LC_TELEPHONE");
unsetenv("LC_TIME");
setenv("LANG", "C", (int)true);
setenv("LC_ALL", "C.UTF-8", (int)true);
std::setlocale(LC_ALL, "C.UTF-8");
rps_backtrace_common_state =
backtrace_create_state(rps_progname, (int)true,
Rps_Backtracer::bt_error_cb,
nullptr);
if (!rps_backtrace_common_state)
{
fprintf(stderr, "%s failed to make backtrace state.\n", rps_progname);
exit(EXIT_FAILURE);
}
pthread_setname_np(pthread_self(), "rps-main");
// hack to handle debug flag as first program argument
if (argc>1 && !strncmp(argv[1], "--debug=", strlen("--debug=")))
rps_set_debug(std::string(argv[1]+strlen("--debug=")));
if (argc>1 && !strncmp(argv[1], "-d", strlen("-d")))
rps_set_debug(std::string(argv[1]+strlen("-d")));
///
if (rps_syslog_enabled && rps_debug_flags != 0)
openlog("RefPerSys", LOG_PERROR|LOG_PID, LOG_USER);
rps_parse_program_arguments(argc, argv);
///
RPS_INFORM("%s%s" "!-!-! starting RefPerSys !-!-!" "%s" " %s process %d on host %s (stdout %s, stderr %s)\n"
"... gitid %.16s built %s (main@%p) %s mode (%d jobs)",
RPS_TERMINAL_BOLD_ESCAPE, RPS_TERMINAL_BLINK_ESCAPE,
RPS_TERMINAL_NORMAL_ESCAPE,
argv[0], (int)getpid(), rps_hostname(),
rps_stdout_istty?"tty":"plain",
rps_stderr_istty?"tty":"plain",
rps_gitid, rps_timestamp,
(void*)main,
(rps_batch?"batch":"interactive"),
rps_nbjobs);
////
//// extend the environment if needed
rps_extend_env();
////
Rps_QuasiZone::initialize();
rps_check_mtime_files();
if (rps_my_load_dir.empty())
rps_my_load_dir = std::string(rps_topdirectory);
rps_load_from(rps_my_load_dir);
rps_run_application(argc, argv);
////
if (!rps_dumpdir_str.empty())
{
char cwdbuf[128];
memset (cwdbuf, 0, sizeof(cwdbuf));
if (!getcwd(cwdbuf, sizeof(cwdbuf)-1))
strcpy(cwdbuf, "./");
RPS_INFORM("RefPerSys (pid %d on %s shortgit %s) will dump into %s\n"
"... from current directory %s\n",
(int)getpid(), rps_hostname(), rps_shortgitid,
rps_dumpdir_str.c_str(), cwdbuf);
rps_dump_into(rps_dumpdir_str);
}
asm volatile (".globl rps_end_of_main; .type rps_end_of_main, @function");
asm volatile ("rps_end_of_main: nop; nop; nop; nop; nop; nop");
asm volatile (".size rps_end_of_main, . - rps_end_of_main");
asm volatile ("nop; nop; nop;");
if (rps_debug_file)
fflush(rps_debug_file);
RPS_INFORM("end of RefPerSys process %d on host %s\n"
"... gitid %.16s built %s elapsed %.3f sec, process %.3f sec",
(int)getpid(), rps_hostname(), rps_gitid, rps_timestamp,
rps_elapsed_real_time(), rps_process_cpu_time());
return 0;
} // end of main
// Parse a single program option, skipping side effects when state is empty.
error_t
rps_parse1opt (int key, char *arg, struct argp_state *state)
{
bool side_effect = state && (void*)state != RPS_EMPTYSLOT;
switch (key)
{
case RPSPROGOPT_DEBUG:
{
if (side_effect)
rps_set_debug(std::string(arg));
}
return 0;
case RPSPROGOPT_DEBUG_PATH:
{
if (side_effect)
rps_set_debug_output_path(arg);
}
return 0;
case RPSPROGOPT_LOADDIR:
{
rps_my_load_dir = std::string(arg);
}
return 0;
case RPSPROGOPT_COMMAND:
{
rps_command_vec.push_back(std::string(arg));
}
return 0;
case RPSPROGOPT_INTERFACEFIFO:
{
rps_fifo_prefix = std::string(arg);
}
return 0;
case RPSPROGOPT_BATCH:
{
rps_batch = true;
}
return 0;
case RPSPROGOPT_JOBS:
{
int nbjobs = atoi(arg);
if (nbjobs <= RPS_NBJOBS_MIN)
nbjobs = RPS_NBJOBS_MIN;
else if (nbjobs > RPS_NBJOBS_MAX)
nbjobs = RPS_NBJOBS_MAX;
rps_nbjobs = nbjobs;
}
return 0;
case RPSPROGOPT_DUMP:
{
if (side_effect)
rps_dumpdir_str = std::string(arg);
}
return 0;
case RPSPROGOPT_HOMEDIR:
{
struct stat rhomstat;
memset (&rhomstat, 0, sizeof(rhomstat));
if (stat(arg, &rhomstat))
RPS_FATAL("failed to stat --refpersys-home %s: %m",
arg);
if (!S_ISDIR(rhomstat.st_mode))
RPS_FATAL("given --refpersys-home %s is not a directory",
arg);
if ((rhomstat.st_mode & (S_IRUSR|S_IXUSR)) != (S_IRUSR|S_IXUSR))
RPS_FATAL("given --refpersys-home %s is not user readable and executable",
arg);
if (side_effect)
{
char*rhomrp = realpath(arg, nullptr);
if (!rhomrp)
RPS_FATAL("realpath failed on given --refpersys-home %s - %m",
arg);
if (strlen(rhomrp) >= sizeof(rps_bufpath_homedir) -1)
RPS_FATAL("too long realpath %s on given --refpersys-home %s - %m",
rhomrp, arg);
strncpy(rps_bufpath_homedir, rhomrp, sizeof(rps_bufpath_homedir) -1);
free (rhomrp), rhomrp = nullptr;
RPS_INFORMOUT("set RefPerSys home directory to " << rps_bufpath_homedir);
};
}
return 0;
case RPSPROGOPT_RANDOMOID:
{
int nbrand = atoi(arg);
if (nbrand <= 0) nbrand = 2;
else if (nbrand > 100) nbrand = 100;
if (side_effect)
{
RPS_INFORM("output of %d random objids generated on %.2f\n", nbrand,
rps_wallclock_real_time());
printf("* %-20s" "\t %-19s" " %-12s" "\t %-10s\n",
" objid", "hi", "lo", "hash");
printf("========================================================"
"===========================\n");
for (int ix = 0; ix<nbrand; ix++)
{
auto rid = Rps_Id::random();
printf("! %22s" "\t %19lld" " %12lld" "\t %10u\n",
rid.to_string().c_str(),
(long long) rid.hi(),
(long long) rid.lo(),
(unsigned) rid.hash());
}
printf("--------------------------------------------------------"
"---------------------------\n");
fflush(nullptr);
}
}
return 0;
case RPSPROGOPT_TYPEINFO:
{
if (side_effect)
rps_print_types_info ();
rps_batch = true;
}
return 0;
case RPSPROGOPT_SYSLOG:
{
if (side_effect)
{
rps_syslog_enabled = true;
openlog("RefPerSys", LOG_PERROR|LOG_PID, LOG_USER);
RPS_INFORM("using syslog");
}
}
return 0;
case RPSPROGOPT_NO_TERMINAL:
{
rps_without_terminal_escape = true;
}
return 0;
case RPSPROGOPT_NO_ASLR:
{
// was already handled
RPS_ASSERT(rps_disable_aslr);
}
return 0;
case RPSPROGOPT_NO_QUICK_TESTS:
{
rps_without_quick_tests = true;
}
return 0;
case RPSPROGOPT_TEST_REPL_LEXER:
{
rps_test_repl_lexer = true;
if (side_effect)
RPS_DEBUG_LOG(REPL, "will run with a textual Read-Eval-Print-Loop lexer GNU readline");
}
return 0;
case RPSPROGOPT_DEBUG_AFTER_LOAD:
{
if (side_effect)
rps_debugflags_after_load = arg;
}
return 0;
case RPSPROGOPT_RUN_AFTER_LOAD:
{
if (rps_run_command_after_load)
RPS_FATALOUT("only one --run-after-load command can be given, not both " << rps_run_command_after_load
<< " and " << arg);
rps_run_command_after_load = arg;
}
return 0;
case RPSPROGOPT_PLUGIN_AFTER_LOAD:
{
void* dlh = dlopen(arg, RTLD_NOW|RTLD_GLOBAL);
if (!dlh)
RPS_FATALOUT("failed to dlopen plugin " << arg << " : " << dlerror());
Rps_Plugin curplugin(arg, dlh);
rps_plugins_vector.push_back(curplugin);
}
return 0;
case RPSPROGOPT_CPLUSPLUSEDITOR_AFTER_LOAD:
{
RPS_DEBUG_LOG(CMD, "option --cplusplus-editor "
<< (arg?" with '":" without ")
<< (arg?arg:" argument")
<< (arg?"'":" !!!")