forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_rps.cc
More file actions
2094 lines (1978 loc) · 80 KB
/
Copy pathdump_rps.cc
File metadata and controls
2094 lines (1978 loc) · 80 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
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 dump_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has the code for dumping the persistent store, in JSON
* format, and also emit some C++ files. See also http://json.org/ &
* https://github.com/open-source-parsers/jsoncpp/
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright (C) 2019 - 2025 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/>.
*
* Notice:
* Same code used to be in store_rps.cc file in july 2022.
******************************************************************************/
#include "refpersys.hh"
/// GNU lightning implementation header.
/// See https://www.gnu.org/software/lightning/
/// a GNU lightning library after 2.2.2 (or GNU lightning commit
/// 3b0fff9206a458d7e11db of August 21, 2023) is required.
/// GNU lightning
extern "C" {
#include "lightning.h"
};
extern "C" const char rps_dump_gitid[];
const char rps_dump_gitid[]= RPS_GITID;
extern "C" const char rps_dump_date[];
const char rps_dump_date[]= __DATE__;
extern "C" const char rps_dump_shortgitid[];
const char rps_dump_shortgitid[]= RPS_SHORTGITID;
// comment for our do-scan-refpersys-pkgconfig.c utility
//@@PKGCONFIG jsoncpp
std::string
rps_dump_json_to_string(const Json::Value&jv)
{
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = " ";
auto str = Json::writeString(builder, jv);
return str;
} // end rps_dump_json_to_string
//////////////////////////////////////////////// dumper
class Rps_Dumper
{
friend class Rps_PayloadSpace;
friend double rps_dump_start_elapsed_time(Rps_Dumper*);
friend double rps_dump_start_process_time(Rps_Dumper*);
friend double rps_dump_start_wallclock_time(Rps_Dumper*);
friend double rps_dump_start_monotonic_time(Rps_Dumper*);
friend std::string rps_dumper_temporary_path(Rps_Dumper*du, std::string shortpath);
friend void rps_dump_into (const std::string dirpath, Rps_CallFrame*);
friend void rps_dump_scan_code_addr(Rps_Dumper*, const void*);
friend void rps_dump_scan_object(Rps_Dumper*, Rps_ObjectRef obr);
friend void rps_dump_scan_space_component(Rps_Dumper*, Rps_ObjectRef obrspace, Rps_ObjectRef obrcomp);
friend void rps_dump_scan_value(Rps_Dumper*, Rps_Value obr, unsigned depth);
friend Json::Value rps_dump_json_value(Rps_Dumper*, Rps_Value val);
friend Json::Value rps_dump_json_objectref(Rps_Dumper*, Rps_ObjectRef obr);
std::string du_topdir;
std::string du_curworkdir;
Json::StreamWriterBuilder du_jsonwriterbuilder;
std::recursive_mutex du_mtx;
std::unordered_map<Rps_Id, Rps_ObjectRef,Rps_Id::Hasher> du_mapobjects;
std::deque<Rps_ObjectRef> du_scanque;
std::string du_tempsuffix;
long du_newobcount; // counter for new dumped objects
bool du_is_dumping_into_topdir;
double du_startelapsedtime;
double du_startprocesstime;
double du_startwallclockrealtime;
double du_startmonotonictime;
Rps_CallFrame* du_callframe;
struct du_space_st
{
Rps_Id sp_id;
std::set<Rps_ObjectRef> sp_setob;
du_space_st(Rps_Id id) : sp_id(id), sp_setob() {};
};
std::map<Rps_ObjectRef,std::shared_ptr<du_space_st>> du_spacemap; // map from spaces to objects inside
std::set<Rps_ObjectRef> du_pluginobset;
std::set<Rps_ObjectRef> du_constantobset;
// we maintain the set of opened file paths, since they are opened
// with the temporary suffix above, and renamed by
// rename_opened_files below.
std::set<std::string> du_openedpathset;
// a random temporary suffix for written files
static std::string make_temporary_suffix(void)
{
Rps_Id randid(nullptr);
char buf[32];
snprintf(buf, sizeof(buf), "%.7s-p%d%%",
randid.to_string().c_str(), (int)getpid());
return std::string(buf);
};
private:
std::string temporary_opened_path(const std::string& relpath) const
{
RPS_ASSERT(relpath.size()>0 && relpath[0] != '/');
return du_topdir + "/" + relpath + du_tempsuffix;
};
void scan_roots(void);
Rps_ObjectRef pop_object_to_scan(void);
void scan_loop_pass(void);
void add_constants_known_from_RefPerSys_system(void);
/// returned number of found constants
int scan_source_file_for_constants(const std::string&relfilename);
void scan_every_source_file_for_constants(void);
void copy_one_source_file(const std::string& relsrcpath);
void make_source_directory(const std::string&relsrcdir);
void write_all_space_files(void);
void write_all_generated_files(void);
void write_generated_roots_file(void);
void write_generated_names_file(void);
void write_generated_constants_file(void);
void write_generated_data_file(void);
void write_generated_rgb_colors_file(void);
void write_generated_parser_decl_file(Rps_CallFrame*, Rps_ObjectRef);
void write_generated_parser_impl_file(Rps_CallFrame*, Rps_ObjectRef);
void write_manifest_file(void);
void write_space_file(Rps_ObjectRef spacobr);
void scan_object_contents(Rps_ObjectRef obr);
std::unique_ptr<std::ofstream> open_output_file(const std::string& relpath);
void rename_opened_files(void);
void scan_code_addr(const void*);
public:
bool is_dumping_into_topdir() const
{
return du_is_dumping_into_topdir;
};
std::string get_temporary_suffix(void) const
{
return du_tempsuffix;
};
std::string get_top_dir() const
{
return du_topdir;
};
Rps_Dumper(const std::string&topdir, Rps_CallFrame*callframe);
~Rps_Dumper();
Rps_CallFrame* dumper_call_frame(void) const
{
return du_callframe;
};
void scan_object(const Rps_ObjectRef obr);
void scan_value(const Rps_Value val, unsigned depth);
Json::Value json_value(const Rps_Value val);
Json::Value json_objectref(const Rps_ObjectRef obr);
bool is_dumpable_objref(const Rps_ObjectRef obr);
bool is_dumpable_objattr(const Rps_ObjectRef obr);
bool is_dumpable_value(const Rps_Value val);
void scan_space_component(Rps_ObjectRef obrspace, Rps_ObjectRef obrcomp)
{
RPS_ASSERT(obrspace);
RPS_ASSERT(obrcomp);
std::lock_guard<std::recursive_mutex> gu(du_mtx);
auto itspace = du_spacemap.find(obrspace);
if (itspace == du_spacemap.end())
{
auto p = du_spacemap.emplace(obrspace,std::make_shared<du_space_st>(obrspace->oid()));
itspace = p.first;
};
itspace->second->sp_setob.insert(obrcomp);
};
#warning perhaps keep some temporary dump object inside Rps_Dumper? see rps_dump_into
}; // end class Rps_Dumper
Rps_Dumper::Rps_Dumper(const std::string&topdir, Rps_CallFrame*callframe) :
du_topdir(), du_curworkdir(), du_jsonwriterbuilder(), du_mtx(), du_mapobjects(), du_scanque(),
du_tempsuffix(make_temporary_suffix()),
du_newobcount(0),
du_is_dumping_into_topdir(false),
du_startelapsedtime(rps_elapsed_real_time()),
du_startprocesstime(rps_process_cpu_time()),
du_startwallclockrealtime(rps_wallclock_real_time()),
du_startmonotonictime(rps_monotonic_real_time()),
du_callframe(callframe), du_openedpathset()
{
{
char topdirpath[PATH_MAX];
char loadirpath[PATH_MAX];
memset(topdirpath, 0, sizeof(topdirpath));
memset(loadirpath, 0, sizeof(loadirpath));
char *toprealpath = realpath(topdir.c_str(), topdirpath);
du_topdir.assign(toprealpath);
char *realoadirpath = realpath(rps_topdirectory, loadirpath);
du_is_dumping_into_topdir = !strcmp(toprealpath, realoadirpath);
}
{
char cwdbuf[rps_path_byte_size];
memset(cwdbuf, 0, sizeof(cwdbuf));
if (!getcwd(cwdbuf, sizeof(cwdbuf)) || cwdbuf[0] == (char)0)
strcpy(cwdbuf, "./");
du_curworkdir.assign(cwdbuf);
}
du_jsonwriterbuilder["commentStyle"] = "None";
du_jsonwriterbuilder["indentation"] = " ";
RPS_DEBUG_LOG(DUMP, "Rps_Dumper constr topdir=" << topdir
<< " this@" << (void*)this
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_Dumper constr"));
RPS_ASSERT(rps_is_main_thread());
} // end Rps_Dumper::Rps_Dumper
Rps_Dumper::~Rps_Dumper()
{
RPS_DEBUG_LOG(DUMP, "Rps_Dumper destr topdir=" << du_topdir
<< " this@" << (void*)this
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_Dumper destr"));
RPS_ASSERT(rps_is_main_thread());
du_callframe = nullptr;
} // end Rps_Dumper::~Rps_Dumper
double
rps_dump_start_elapsed_time(Rps_Dumper*du)
{
if (!du)
return NAN;
return du->du_startelapsedtime;
} // end rps_dump_start_elapsed_time
double
rps_dump_start_process_time(Rps_Dumper*du)
{
if (!du)
return NAN;
return du->du_startprocesstime;
} // end rps_dump_start_process_time
double
rps_dump_start_wallclock_time(Rps_Dumper*du)
{
if (!du)
return NAN;
return du->du_startwallclockrealtime;
} // end rps_dump_start_wallclock_time
double
rps_dump_start_monotonic_time(Rps_Dumper*du)
{
if (!du)
return NAN;
return du->du_startmonotonictime;
} // end rps_dump_start_monotonic_time
std::string
rps_dumper_temporary_path(Rps_Dumper*du, std::string shortpath)
{
RPS_ASSERT(du != nullptr);
std::string restemp;
std::lock_guard<std::recursive_mutex> gu(du->du_mtx);
if (shortpath.empty())
{
RPS_WARNOUT("empty shortpath"
<< RPS_FULL_BACKTRACE_HERE(1, "rps_dumper_temporary_path/E"));
throw RPS_RUNTIME_ERROR_OUT("empty shortpath");
}
int splen = shortpath.length();
if (splen<4)
{
RPS_WARNOUT("too small shortpath=" << Rps_Cjson_String(shortpath)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_dumper_temporary_path/S"));
throw RPS_RUNTIME_ERROR_OUT("too small shortpath");
};
bool goodshortpath = true;
if (shortpath[0] == '.'
&& !(isalnum(shortpath[1]) || shortpath[1]=='_'))
goodshortpath = false;
for (int i=0; goodshortpath && i<splen; i++)
{
if (i>0 && shortpath[i]=='.' && shortpath[i-1]=='.')
goodshortpath= false;
else if (isalnum(shortpath[i]) || shortpath[i]=='_')
continue;
else
goodshortpath= false;
};
if (!goodshortpath)
{
RPS_WARNOUT("bad shortpath=" << Rps_Cjson_String(shortpath)
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_dumper_temporary_path/D"));
throw RPS_RUNTIME_ERROR_OUT("bad shortpath");
};
restemp = du->temporary_opened_path(shortpath);
return restemp;
} // end rps_dumper_temporary_path
void
Rps_Dumper::scan_object(const Rps_ObjectRef obr)
{
if (!obr)
return;
std::lock_guard<std::recursive_mutex> gu(du_mtx);
if (du_mapobjects.find(obr->oid()) != du_mapobjects.end())
return;
if (!obr->get_space()) // transient
return;
du_mapobjects.insert({obr->oid(), obr});
if (obr->get_mtime() > rps_get_start_wallclock_real_time())
{
du_newobcount++;
RPS_DEBUG_LOG(DUMP, "new object #" << du_newobcount << ": " << obr
<< " with mtime " << obr->get_mtime()
<< " and start wallclock " << rps_get_start_wallclock_real_time());
}
du_scanque.push_back(obr);
// RPS_INFORMOUT("Rps_Dumper::scan_object adding oid " << obr->oid());
} // end Rps_Dumper::scan_object
void
Rps_Dumper::scan_value(const Rps_Value val, unsigned depth)
{
if (!val || val.is_empty() || !val.is_ptr())
return;
val.to_ptr()->dump_scan(this,depth);
} // end Rps_Dumper::scan_value
Json::Value
Rps_Dumper::json_value(Rps_Value val)
{
if (!val || val.is_empty())
return Json::Value(Json::nullValue);
else if (val.is_int())
return Json::Value(Json::Int64(val.as_int()));
else if (val.is_ptr() && is_dumpable_value(val))
{
return val.to_ptr()->dump_json(this);
}
else
return Json::Value(Json::nullValue);
} // end Rps_Dumper::json_value
bool
Rps_Dumper::is_dumpable_objref(const Rps_ObjectRef obr)
{
if (!obr)
return false;
std::lock_guard<std::recursive_mutex> gu(du_mtx);
if (du_mapobjects.find(obr->oid()) != du_mapobjects.end())
return true;
auto obrspace = obr->get_space();
if (!obrspace) // transient
return false;
return true;
} // end Rps_Dumper::is_dumpable_objref
bool
Rps_Dumper::is_dumpable_objattr(const Rps_ObjectRef obr)
{
if (!obr)
return false;
if (!is_dumpable_objref(obr))
return false;
std::lock_guard<std::recursive_mutex> gu(du_mtx);
#warning incomplete Rps_Dumper::is_dumpable_objattr
return true;
} // end Rps_Dumper::is_dumpable_objattr
bool
Rps_Dumper::is_dumpable_value(const Rps_Value val)
{
if (!val) return true;
if (val.is_int() || val.is_string() || val.is_set() || val.is_tuple())
return true;
if (val.is_closure())
{
auto closv = val.as_closure();
if (closv->is_transient()) return false;
else
return is_dumpable_objref(closv->conn());
}
else if (val.is_instance())
{
auto instv = val.as_instance();
if (instv->is_transient()) return false;
else
return is_dumpable_objref(instv->conn());
}
if (val.is_object())
return is_dumpable_objref(val.to_object());
RPS_FATALOUT("Rps_Dumper::is_dumpable_value partly unimplemented for " << val);
#warning Rps_Dumper::is_dumpable_value partly unimplemented
} // end Rps_Dumper::is_dumpable_value
std::unique_ptr<std::ofstream>
Rps_Dumper::open_output_file(const std::string& relpath)
{
RPS_ASSERT(relpath.size()>1 && relpath[0] != '/');
std::lock_guard<std::recursive_mutex> gu(du_mtx);
if (RPS_UNLIKELY(du_openedpathset.find(relpath) != du_openedpathset.end()))
{
RPS_WARNOUT("duplicate opened dump file " << relpath);
throw std::runtime_error(std::string{"duplicate opened dump file "} + relpath);
}
std::string tempathstr = temporary_opened_path(relpath);
auto poutf= std::make_unique<std::ofstream>(tempathstr);
if (!poutf || !poutf->is_open())
{
RPS_WARNOUT("dump failed to open " << tempathstr);
throw std::runtime_error(std::string{"duplicate failed to open "} + tempathstr + ":" + strerror(errno));
}
du_openedpathset.insert(relpath);
return poutf;
} // end Rps_Dumper::open_output_file
int
Rps_Dumper::scan_source_file_for_constants(const std::string&relfilename)
{
int nbconst = 0;
RPS_DEBUG_LOG(DUMP, "start dumper scan_source_file_for_constants file " << relfilename
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_Dumper::scan_source_file_for_constants"));
RPS_ASSERT(relfilename.size()>2 && isalpha(relfilename[0]));
std::string fullpath = std::string(rps_topdirectory) + "/" + relfilename;
std::ifstream ins(fullpath);
unsigned lincnt = 0;
for (std::string linbuf; std::getline(ins, linbuf); )
{
lincnt++;
if (u8_check(reinterpret_cast<const uint8_t*> (linbuf.c_str()),
linbuf.size()))
{
RPS_WARNOUT("file " << fullpath << ", line " << lincnt
<< " non UTF8:" << linbuf);
continue;
};
const char*curpos = linbuf.c_str();
char*foundpos = nullptr;
while ((foundpos = strstr((char*)curpos, RPS_CONSTANTOBJ_PREFIX)) != nullptr)
{
const char*endpos=nullptr;
bool ok=false;
Rps_Id oid(foundpos + strlen(RPS_CONSTANTOBJ_PREFIX), &endpos, &ok);
if (ok)
{
Rps_ObjectZone* curobz = Rps_ObjectZone::find(oid);
Rps_ObjectRef obr(curobz);
if (obr)
{
scan_object(obr);
nbconst++;
std::lock_guard<std::recursive_mutex> gu(du_mtx);
if (du_constantobset.find(obr) != du_constantobset.end())
RPS_DEBUG_LOG(DUMP, "scan_source_file_for_constants const#" << nbconst
<< " is " << obr);
du_constantobset.insert(obr);
}
else
RPS_WARNOUT("unknown object of oid " << oid
<< " in file " << fullpath << " line " << lincnt);
curpos = endpos;
}
else break;
};
}
if (nbconst>0)
RPS_INFORMOUT("found " << nbconst
<< " constant[s] prefixed by " << RPS_CONSTANTOBJ_PREFIX
<< " in file " << fullpath
<< " of " << lincnt << " lines.");
else
RPS_DEBUG_LOG(DUMP, "scan_source_file_for_constants no constants in " << fullpath);
return nbconst;
} // end Rps_Dumper::scan_source_file_for_constants
void
Rps_Dumper::add_constants_known_from_RefPerSys_system(void)
{
Rps_ObjectRef obsystem = RPS_ROOT_OB(_1Io89yIORqn02SXx4p); //RefPerSys_system∈the_system_class);
//RPS_ASSERT(obsystem == rpsKob_1Io89yIORqn02SXx4p);
std::lock_guard<std::recursive_mutex> gudump(du_mtx);
std::set<Rps_ObjectRef> constset;
std::lock_guard<std::recursive_mutex> gusystem(*obsystem->objmtxptr());
/// see code of rps_add_constant_object in utilities_rps.cc
Rps_Value oldset = obsystem->get_physical_attr
(RPS_ROOT_OB(_2aNcYqKwdDR01zp0Xp)); // //"constant"∈named_attribute
RPS_ASSERT(oldset.is_set());
const Rps_SetOb* constoldset = oldset.as_set();
/// TODO: please code review; Basile S. consider in start of july
/// 2024 that in rare cases the garbage collector might move
/// constoldset in the below for loop.
for (auto it : *constoldset)
{
Rps_ObjectRef oldelem = *it;
RPS_ASSERT(oldelem);
RPS_DEBUG_LOG(DUMP, "add_constants_known_from_RefPerSys_system with oldelem=" << oldelem);
this->du_constantobset.insert(oldelem);
};
} // end Rps_Dumper::add_constants_known_from_RefPerSys_system
void
Rps_Dumper::scan_code_addr(const void*ad)
{
/**
* If we wanted to be more efficient, we should parse the
* pseudo-file /proc/self/maps at start of dump. See
* http://man7.org/linux/man-pages/man5/proc.5.html for more....
**/
if (!ad)
return;
std::lock_guard<std::recursive_mutex> gu(du_mtx);
static char rpsmodfmt[80];
static size_t rpsmodfmtlen;
if (!rpsmodfmt[0])
{
/// see GNUmakefile near its comment line containing:
/// # **generated binary modules.
/// and the rps-generate-timestamp.sh script.
snprintf(rpsmodfmt, sizeof(rpsmodfmt),
"__rps_%s_%s_%%-mod.so",
rps_building_machname, rps_building_opersysname);
rpsmodfmtlen = strlen(rpsmodfmt);
RPS_ASSERT(rpsmodfmtlen<sizeof(rpsmodfmt)-4);
};
Dl_info di;
memset(&di, 0, sizeof(di));
if (!dladdr(ad, &di))
return;
if (!di.dli_fname)
return;
const char*lastslash = strrchr(di.dli_fname, '/');
if (!lastslash)
return;
char idbuf[32];
memset (idbuf, 0, sizeof(idbuf));
int endpos = -1;
if (sscanf(lastslash+1, rpsmodfmt, idbuf, &endpos) >= 1
&& endpos>(int) rpsmodfmtlen)
{
const char* endid=nullptr;
bool okid=false;
Rps_Id plugid (idbuf, &endid, &okid);
if (plugid.valid() && *endid == (char)0 && okid)
{
Rps_ObjectZone* obz = Rps_ObjectZone::find(plugid);
if (obz)
{
Rps_ObjectRef plugobr(obz);
if (du_pluginobset.find(plugobr)
== du_pluginobset.end())
{
du_pluginobset.insert(plugobr);
scan_object(plugobr);
}
};
}
}
} // end of Rps_Dumper::scan_code_addr
void
Rps_Dumper::rename_opened_files(void)
{
std::lock_guard<std::recursive_mutex> gu(du_mtx);
for (std::string curelpath: du_openedpathset)
{
std::string curpath = du_topdir + "/" + curelpath;
if (!access(curpath.c_str(), F_OK))
{
std::string bak0path = curpath + "~";
if (!access(bak0path.c_str(), F_OK))
{
std::string bak1path = bak0path + "~";
(void) rename(bak0path.c_str(), bak1path.c_str());
};
if (rename(curpath.c_str(), bak0path.c_str()))
RPS_WARNOUT("dump failed to backup " << curpath << " to " << bak0path << ":" << strerror(errno));
};
std::string tempath = temporary_opened_path(curelpath);
if (rename(tempath.c_str(), curpath.c_str()))
RPS_FATALOUT("dump failed to rename " << tempath << " as " << curpath);
};
du_openedpathset.clear();
} // end Rps_Dumper::rename_opened_files
//////////////// public interface to dumper::::
bool rps_is_dumpable_objref(Rps_Dumper*du, const Rps_ObjectRef obr)
{
RPS_ASSERT(du != nullptr);
return du->is_dumpable_objref(obr);
} // end rps_is_dumpable_objref
bool rps_is_dumpable_objattr(Rps_Dumper*du, const Rps_ObjectRef obr)
{
RPS_ASSERT(du != nullptr);
return du->is_dumpable_objattr(obr);
} // end rps_is_dumpable_objattr
bool rps_is_dumpable_value(Rps_Dumper*du, const Rps_Value val)
{
RPS_ASSERT(du != nullptr);
return du->is_dumpable_value(val);
} // end rps_is_dumpable_value
void
rps_dump_scan_code_addr(Rps_Dumper*du, const void* ad)
{
RPS_ASSERT(du != nullptr);
if (ad)
du->scan_code_addr(ad);
} // end rps_dump_scan_code_addr
void
rps_dump_scan_space_component(Rps_Dumper*du, Rps_ObjectRef obrspace, Rps_ObjectRef obrcomp)
{
RPS_ASSERT(du != nullptr);
du->scan_space_component(obrspace, obrcomp);
} // end rps_dump_scan_space_component
void
rps_dump_scan_object(Rps_Dumper*du, const Rps_ObjectRef obr)
{
RPS_ASSERT(du != nullptr);
du->scan_object(obr);
} // end rps_dump_scan_object
void rps_dump_scan_value(Rps_Dumper*du, const Rps_Value val, unsigned depth)
{
RPS_ASSERT(du != nullptr);
du->scan_value(val, depth);
} // end rps_dump_scan_value
Json::Value
rps_dump_json_value(Rps_Dumper*du, Rps_Value val)
{
RPS_ASSERT(du != nullptr);
if (!val || !rps_is_dumpable_value(du,val))
return Json::Value(Json::nullValue);
else return du->json_value(val);
} // end rps_dump_json_value
Json::Value
rps_dump_json_objectref(Rps_Dumper*du, Rps_ObjectRef obr)
{
RPS_ASSERT(du != nullptr);
if (!obr || !rps_is_dumpable_objref(du,obr))
return Json::Value(Json::nullValue);
else
return Json::Value(obr->oid().to_string());
} // end rps_dump_json_objectref
void
Rps_TupleOb::dump_scan(Rps_Dumper*du, unsigned) const
{
RPS_ASSERT(du != nullptr);
for (auto obr: *this)
du->scan_object(obr);
}
Json::Value
Rps_TupleOb::dump_json(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
auto jtup = Json::Value(Json::objectValue);
jtup["vtype"] = Json::Value("tuple");
auto jvec = Json::Value(Json::arrayValue);
jvec.resize(cnt()); // reservation
int ix=0;
for (auto obr: *this)
if (rps_is_dumpable_objref(du,obr))
jvec[ix++] = rps_dump_json_objectref(du,obr);
jvec.resize(ix); // shrink to fit
jtup["comp"] = jvec;
return jtup;
} // end Rps_TupleOb::dump_json
////////////////
void
Rps_SetOb::dump_scan(Rps_Dumper*du, unsigned) const
{
RPS_ASSERT(du != nullptr);
for (auto obr: *this)
du->scan_object(obr);
}
Json::Value
Rps_SetOb::dump_json(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
auto jset = Json::Value(Json::objectValue);
jset["vtype"] = Json::Value("set");
auto jvec = Json::Value(Json::arrayValue);
for (auto obr: *this)
if (rps_is_dumpable_objref(du,obr))
jvec.append(rps_dump_json_objectref(du,obr));
jset["elem"] = jvec;
return jset;
} // end Rps_SetOb::dump_json
////////////////
void
Rps_ClosureZone::dump_scan(Rps_Dumper*du, unsigned depth) const
{
RPS_ASSERT(du != nullptr);
auto obrcon = conn();
du->scan_object(obrcon);
if (du->is_dumpable_objref(obrcon))
{
for (auto v: *this)
du->scan_value(v, depth+1);
}
if (!is_metatransient())
du->scan_object(metaobject());
} // end Rps_ClosureZone::dump_scan
Json::Value
Rps_ClosureZone::dump_json(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
if (!rps_is_dumpable_objref(du,conn()) || is_transient())
return Json::Value(Json::nullValue);
auto hjclo = Json::Value(Json::objectValue);
hjclo["vtype"] = Json::Value("closure");
hjclo["fn"] = rps_dump_json_objectref(du,conn());
auto jvec = Json::Value(Json::arrayValue);
for (Rps_Value sonval: *this)
jvec.append(rps_dump_json_value(du,sonval));
hjclo["env"] = jvec;
if (!is_metatransient())
{
auto md = get_metadata();
Rps_ObjectRef metaobr = md.first;
int32_t metarank = md.second;
if (metaobr)
{
hjclo["metaobj"] = rps_dump_json_objectref(du,metaobr);
hjclo["metarank"] = Json::Value(metarank);
}
}
return hjclo;
} // end Rps_ClosureZone::dump_json
////////////////
void
Rps_InstanceZone::dump_scan(Rps_Dumper*du, unsigned depth) const
{
RPS_ASSERT(du != nullptr);
auto obrcon = conn();
du->scan_object(obrcon);
if (du->is_dumpable_objref(obrcon))
{
for (auto v: *this)
du->scan_value(v, depth+1);
}
if (!is_metatransient())
du->scan_object(metaobject());
} // end Rps_InstanceZone::dump_scan
Json::Value
Rps_InstanceZone::dump_json(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
if (!rps_is_dumpable_objref(du,conn()) || is_transient())
return Json::Value(Json::nullValue);
auto hjins = Json::Value(Json::objectValue);
hjins["vtype"] = Json::Value("instance");
hjins["isize"] = Json::Value(cnt());
hjins["iclass"] = rps_dump_json_objectref(du,get_class());
auto atset = set_attributes();
const Rps_Value* csons = const_sons();
unsigned nbsons = cnt();
int atlen = 0;
int nbattrs = 0;
if (atset)
{
atlen = atset->cardinal();
auto jvattrs = Json::Value(Json::arrayValue);
jvattrs.resize(atlen); // reservation
int attrix = 0;
for (Rps_ObjectRef obattr: *atset)
{
attrix++;
if (attrix > (int)nbsons)
break;
if (!rps_is_dumpable_objref(du, obattr))
{
jvattrs.append(Json::Value(Json::nullValue));
continue;
}
auto jent = Json::Value(Json::objectValue);
jent["iat"] = rps_dump_json_objectref(du,obattr);
jent["iva"] = rps_dump_json_value(du,csons[attrix-1]);
jvattrs.append(jent);
nbattrs++;
}
jvattrs.resize(nbattrs); // reservation
hjins["iattrs"] = jvattrs;
auto jvcomps = Json::Value(Json::arrayValue);
jvcomps.resize(nbsons-nbattrs); // reservation
for (int compix = attrix; compix<(int)nbsons; compix++)
{
jvcomps.append(rps_dump_json_value(du,csons[compix]));
}
hjins["icomps"] = jvcomps;
}
if (!is_metatransient())
{
auto md = get_metadata();
Rps_ObjectRef metaobr = md.first;
int32_t metarank = md.second;
if (metaobr)
{
hjins["metaobj"] = rps_dump_json_objectref(du,metaobr);
hjins["metarank"] = Json::Value(metarank);
}
}
return hjins;
} // end Rps_InstanceZone::dump_json
////////////////
void
Rps_ObjectZone::dump_scan(Rps_Dumper*du, unsigned) const
{
RPS_ASSERT(du != nullptr);
rps_dump_scan_object(du,Rps_ObjectRef(this));
}
Json::Value
Rps_ObjectZone::dump_json(Rps_Dumper*du) const
{
RPS_ASSERT(du != nullptr);
return rps_dump_json_objectref(du,Rps_ObjectRef(this));
} // end Rps_ObjectZone::dump_json
//////////////////////////////////////////////////////////////// dump
void
Rps_Dumper::scan_roots(void)
{
std::lock_guard<std::recursive_mutex> gu(du_mtx);
int nbroots = 0;
RPS_DEBUG_LOG(DUMP, "dumper: scan_roots begin");
rps_each_root_object([&](Rps_ObjectRef obr)
{
rps_dump_scan_object(this,obr);
nbroots++;
});
RPS_DEBUG_LOG(DUMP, "dumper: scan_roots ends nbroots#" << nbroots);
} // end Rps_Dumper::scan_roots
Rps_ObjectRef
Rps_Dumper::pop_object_to_scan(void)
{
std::lock_guard<std::recursive_mutex> gu(du_mtx);
if (du_scanque.empty())
return Rps_ObjectRef(nullptr);
auto obr = du_scanque.front();
du_scanque.pop_front();
return obr;
} // end Rps_Dumper::pop_object_to_scan
void
Rps_Dumper::scan_loop_pass(void)
{
Rps_ObjectRef curobr;
int count=0;
// no locking here, it happens in pop_object_to_scan & scan_object_contents
// RPS_INFORMOUT("scan_loop_pass start");
while ((curobr=pop_object_to_scan()))
{
count++;
// RPS_INFORMOUT("scan_loop_pass count#" << count
// << " curobr=" << curobr->oid());
scan_object_contents(curobr);
};
RPS_DEBUG_LOG(DUMP, "dumper: scan_loop_pass end count#" << count);
} // end Rps_Dumper::scan_loop_pass
void
Rps_Dumper::scan_object_contents(Rps_ObjectRef obr)
{
std::lock_guard<std::recursive_mutex> gu(du_mtx);
obr->dump_scan_contents(this);
Rps_ObjectRef spacobr(obr->get_space());
rps_dump_scan_object(this,spacobr);
} // end Rps_Dumper::scan_object_contents
void
Rps_Dumper::scan_every_source_file_for_constants(void)
{
int nbscanedfiles = 0;
int nbtotconsts = 0;
for (const char*const*pcurfilename = rps_files; *pcurfilename; pcurfilename++)
{
const char*curpath = *pcurfilename;
int lencurpath = strlen(curpath);
if (lencurpath < 8 || strstr(curpath, "generated/") || strstr(curpath, "attic/"))
continue;
if (!strcmp(curpath+lencurpath-3, ".cc")
|| !strcmp(curpath+lencurpath-3, ".hh")
|| !strcmp(curpath+lencurpath-3, ".yy")
|| !strcmp(curpath+lencurpath-4, ".yyp")
|| (!strcmp(curpath+lencurpath-5, ".cbrt")))
{
std::string relfilname = curpath;
nbtotconsts += scan_source_file_for_constants(relfilname);
nbscanedfiles++;
};
};
RPS_INFORMOUT("scanned " << nbscanedfiles << " source files for constants." << std::endl
<< "found " << nbtotconsts << " occurrences.");
} // end of scan_every_source_file_for_constants
void
Rps_Dumper::copy_one_source_file(const std::string& relsrcpath)
{
/// This function should copy the given relative source path,
/// e.g. refpersys.hh to the dump directory. It should not do
/// anything if the dump directory is the source one
/// rps_topdirectory....
/// See also the rps_files constant array.
std::lock_guard<std::recursive_mutex> gu(du_mtx);
if (is_dumping_into_topdir())
return;
#warning unimplemented Rps_Dumper::copy_one_source_file
RPS_FATALOUT("unimplemented Rps_Dumper::copy_one_source_file relsrcpath="
<< relsrcpath << " to dumpdir " << du_topdir);
} // end Rps_Dumper::copy_one_source_file
void
Rps_Dumper::make_source_directory(const std::string& relsrcdir)
{
std::lock_guard<std::recursive_mutex> gu(du_mtx);
/// This function should make a given source directory
/// e.g. refpersys.hh to the dump directory. It should not do
/// anything if the dump directory is the source one
/// rps_topdirectory....
/// See also the rps_subdirectories constant array.
if (is_dumping_into_topdir())
return;
#warning unimplemented Rps_Dumper::make_source_directory
RPS_FATALOUT("unimplemented Rps_Dumper::make_source_directory relsrcdir="
<< relsrcdir << " to dumpdir " << du_topdir);
} // end Rps_Dumper::make_source_directory
void
Rps_Dumper::write_all_space_files(void)
{
RPS_DEBUG_LOG(DUMP, "dumper write_all_space_files start");
std::set<Rps_ObjectRef> spaceset;
{
std::lock_guard<std::recursive_mutex> gu(du_mtx);
for (auto it: du_spacemap)