forked from CTSRD-CHERI/cheritrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamtrace.cc
2050 lines (2009 loc) · 51.9 KB
/
streamtrace.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
/*-
* Copyright (c) 2015 David T. Chisnall
*
* All rights reserved.
*
* This software was developed by SRI International and the University of
* Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
* ("CTSRD"), as part of the DARPA CRASH research programme.
*
* This software was developed by SRI International and the University of
* Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249
* ("MRC2"), as part of the DARPA MRC research programme.
*
* @BERI_LICENSE_HEADER_START@
*
* Licensed to BERI Open Systems C.I.C. (BERI) under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. BERI licenses this
* file to you under the BERI Hardware-Software License, Version 1.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.beri-open-systems.org/legal/license-1-0.txt
*
* Unless required by applicable law or agreed to in writing, Work distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @BERI_LICENSE_HEADER_END@
*/
#include "streamtrace.hh"
#include "disassembler.hh"
#include <thread>
#include <mutex>
#include <future>
#include <atomic>
#include <algorithm>
#include <limits>
#include <condition_variable>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <lzma.h>
#include <string.h>
#include <assert.h>
#include <sys/mman.h>
#define expect(x, y) __builtin_expect(!!(x), y)
using namespace cheri;
using namespace streamtrace;
trace::~trace() {}
namespace {
/**
* State object for fast enumeration, modelled on NSFastEnumerationState. This
* allows the enumerated object to provide direct access to internal values.
*/
template<typename T, int buffer_size=4096>
struct fast_enumeration_state
{
/**
* Size of the buffer inside this structure. This allows external users to
* determine the template parameter easily.
*/
static const int internal_buffer_size = buffer_size;
/**
* State for use by the enumerated object.
*/
uintptr_t state[2] = {0,0};
/**
* The number of elements that have been returned.
*/
size_t size = 0;
/**
* A pointer to `size` objects of type `T`. This is used by callers to
* look up entries and can be set by callees to either:
*
* - The internal buffer.
* - The shared buffer.
* - An buffer internal to the callee.
*
* In all cases, the contents of the buffer must not be immutable.
*/
T *ptr = nullptr;
/**
* A pointer to a buffer that the callee can use to store some other value.
*/
std::shared_ptr<T> shared_buffer;
/**
* An internal buffer that the callee can copy data into, if required.
*/
T buffer[buffer_size];
/**
* Begin method allowing this to be used with range-based for loops.
*/
T* begin()
{
return ptr;
}
/**
* End method allowing this to be used with range-based for loops.
*/
T* end()
{
return ptr + size;
}
/**
* Assignment operator.
*/
fast_enumeration_state<T,buffer_size>& operator=(const fast_enumeration_state<T,buffer_size>& o)
{
size = o.size;
ptr = o.ptr;
shared_buffer = o.shared_buffer;
memcpy(buffer, o.buffer, sizeof(buffer));
return *this;
}
};
/**
* Interface for classes that support fast enumeration.
*/
template<class T, int buffer_size=4096>
struct fast_enumeration
{
/**
* The state for this form of fast enumeration.
*/
typedef fast_enumeration_state<T,buffer_size> enumerator;
/**
* Method that fills in fast enumeration state starting at a specified
* object.
*/
virtual bool enumerate(enumerator &e, size_t start) = 0;
virtual ~fast_enumeration() {}
};
/**
* Method for fast enumeration within a range.
*/
template<typename T, typename B>
void fast_enumerate(B &o,
std::function<bool(size_t,T&)> fn,
size_t start=0,
size_t end=std::numeric_limits<size_t>::max())
{
typename B::enumerator e;
while ((start < end) && o.enumerate(e, start))
{
for (T &v : e)
{
fn(start++, v);
if (start >= end)
{
break;
}
}
}
}
/**
* Class encapsulating the machine state at a specific point in the trace. A
* trace object stores instances of this class at points along the trace,
* allowing the state in between to be quickly inferred.
*/
struct keyframe
{
/**
* The total number of elapsed cycles in the trace.
*/
uint64_t cycles = 0;
/**
* Program counter for the current location.
*/
uint64_t pc = 0;
/**
* The current value of the 10-bit cycle counter.
*/
uint16_t cycle_counter = 0;
/**
* The register set.
*/
register_set regs;
/**
* Updates the state for a new trace entry. The disassembler is used to
* determine the target register for the instruction.
*/
void update(const debug_trace_entry &, disassembler::disassembler &);
};
/**
* An in-memory segment of a trace. To speed up random access (assuming some
* locality of reference), the `concrete_streamtrace` class loads the trace in
* segments.
*/
class trace_segment {
/**
* Adds a new entry to the end of this trace segment. This exists so that
* the code doesn't need duplicating between template variants.
*/
void add_entry(disassembler::disassembler &d,
keyframe &,
const debug_trace_entry &);
public:
/**
* The register sets within this segment, one per step in the trace.
*/
std::vector<register_set> regs;
/**
* The trace entries for this segment.
*/
std::vector<debug_trace_entry> entries;
/**
* Construct a trace segment from a sequence of trace entries.
*/
template<class T>
trace_segment(disassembler::disassembler &d, keyframe rs, T &&begin, T &&end)
{
assert(begin != end);
while (begin != end)
{
debug_trace_entry entry(*begin, d);
add_entry(d, rs, entry);
++begin;
}
}
};
/**
* A map from a range of indexes 0-n to an increasing set of indexes to another
* range of linear indexes. This is used to densely store the mapping from
* indexes in trace views to indexes in the underlying trace.
*/
class index_map
{
/**
* A range of indexes.
*/
struct range
{
/**
* The first index in the range.
*/
uint64_t start;
/**
* The last index in the range (not one after).
*/
uint64_t end;
};
/**
* An entry in the range map.
*/
struct range_map_entry
{
/**
* The source range in a range map entry.
*/
range from;
/**
* The range that the from entry maps to.
*/
// Note: The end can always be calculated from the from field, so if
// profiling shows that these consume a lot of RAM then we can save
// memory by 25% quite easily.
range to;
};
/**
* The type used to store the range map entries. This needs fast random
* access on a sequential store, as we will do a binary search.
*/
typedef std::vector<range_map_entry> range_map;
/**
* The vector of ranges stored by this index map.
*/
range_map ranges;
/**
* Private constructor, takes ownership of a temporary range map
* constructed by the caller.
*/
index_map(range_map &&m) : ranges(m) {}
/**
* Look up a source index and return an iterator to the range containing
* it.
*/
decltype(ranges)::iterator find_idx(uint64_t idx)
{
auto r = std::lower_bound(ranges.begin(), ranges.end(), idx,
[](const range_map_entry e, const uint64_t v) {
return e.from.end < v;
});
return r;
}
public:
/**
* default constructor.
*/
index_map() {}
/**
* Range map iterator. This is a ForwardIterator.
*/
class iterator
{
friend class index_map;
/**
* The current index that we're looking at.
*/
uint64_t idx;
/**
* The start of the contiguous range of source addresses that we're
* inspecting.
*/
uint64_t from_base = 0;
/**
* The start of the contiguous range of destination addresses that
* corresponds to `from_base`.
*/
uint64_t to_base = 0;
/**
* The top of the range. One after the maximum input value in this
* contiguous range.
*/
uint64_t top = 0;
/**
* The container that this iterator refers to.
*/
index_map &container;
/**
* After updating the index, recalculate the base and top values used
* to compute the target range.
*/
inline void recalculate()
{
if ((idx >= from_base) && (idx < top))
{
return;
}
auto i = container.find_idx(idx);
if (i == container.ranges.end())
{
top = 0;
return;
}
from_base = i->from.start;
to_base = i->to.start;
top = i->from.end+1;
}
iterator(index_map &m, uint64_t i) : idx(i), container(m)
{
recalculate();
}
public:
iterator &operator++() {
idx++;
recalculate();
return *this;
}
iterator &operator+=(uint64_t off) {
idx+=off;
recalculate();
return *this;
}
iterator operator+(uint64_t off) {
iterator n(container, idx+off);
return n;
}
/**
* Difference with another operator.
*/
uint64_t operator-(const iterator& other) {
assert(&container == &other.container);
return idx - other.idx;
}
/**
* Dereference the iterator, giving a destination index.
*/
uint64_t operator*() const {
if (top < idx)
{
return -1;
}
return idx - from_base + to_base;
}
/**
* Compare iterators for equality. Used to determine the end of iteration.
*/
bool operator!=(iterator &other)
{
assert(&container == &other.container);
return idx != other.idx;
}
};
/**
* Add a new destination address. The new destination address is
* automatically associated with a new source address that immediately
* follow the last one already in the map.
*/
void push_back(uint64_t v)
{
if (ranges.size() == 0)
{
range_map_entry dst = { {0,0}, {v, v} };
ranges.push_back(dst);
return;
}
auto &e = ranges.back();
// If we're adding the next entry, then just stick it in the range map.
if (e.to.end+ 1 == v)
{
e.from.end++;
e.to.end++;
return;
}
// If there's a gap, then insert a new range.
range_map_entry n;
n.from.start = n.from.end = e.from.end+1;
n.to.start = n.to.end = v;
ranges.push_back(n);
}
/**
* Returns a new map that contains all of the destination indexes that are
* not in the source. The `length` parameter gives the number of
* destination indexes that exist.
*/
index_map inverted_map(uint64_t length)
{
range_map inverted;
if (ranges.size() == 0)
{
range_map_entry dst = { {0,length}, {0, length} };
inverted.push_back(dst);
index_map inverted_map(std::move(inverted));
return inverted_map;
}
const auto src = ranges.front();
range_map_entry dst { {0,0},{0,0}};
if (src.to.start == 0)
{
dst.to.start = dst.to.end = src.to.end+1;
}
else
{
dst.to.end = src.to.start-1;
}
for (auto i=ranges.begin()+1, e=ranges.end() ; i!=e ; ++i)
{
dst.to.end = i->to.start - 1;
dst.from.end = dst.from.start + dst.to.end - dst.to.start;
inverted.push_back(dst);
dst.from.start = dst.from.end + 1;
dst.from.end = dst.from.end;
dst.to.start = i->to.end + 1;
}
if (dst.from.start < length)
{
dst.to.end = length - 1;
dst.from.end = dst.from.start + dst.to.end - dst.to.start;
inverted.push_back(dst);
}
index_map inverted_map(std::move(inverted));
return inverted_map;
}
/**
* Return the destination index that corresponds to this source index.
* Undefined if the source index is not in this map.
*/
uint64_t operator [](uint64_t idx)
{
auto i = find_idx(idx);
if (i == ranges.end())
{
return -1;
}
return idx - i->from.start + i->to.start;
}
/**
* Returns the number of elements in this map.
*/
uint64_t size()
{
if (ranges.size() == 0)
{
return 0;
}
return ranges.back().from.end+1;
}
/**
* Iterator to the start of the map. Iterators are dereferenced to give
* *destination* addresses and so can be used to index into the target.
*/
iterator begin()
{
iterator i(*this, 0);
return i;
}
/**
* End iterator.
*/
iterator end()
{
iterator i(*this, size());
return i;
}
};
template<class T>
class concrete_traceview;
bool scan_range(uint64_t &start, uint64_t &scan_end, int opts, int &outinc, uint64_t len)
{
scan_end = std::min(scan_end+1, len);
if (scan_end < start)
{
return false;
}
outinc = 1;
// If we're scanning forwards
if (opts & cheri::streamtrace::trace::backwards)
{
outinc = -1;
scan_end--;
start--;
std::swap(start, scan_end);
}
return true;
}
/**
* Number of entries between keyframes. Note that each keyframe is over 1KB
* (including the complete capability register set size) and making this value
* larger can cause significant memory use.
*/
const uint64_t keyframe_interval = 1<<11;
/**
* Concrete subclass of the streamtrace. Manages trace segments.
*/
template<class T>
class concrete_streamtrace : public trace,
public std::enable_shared_from_this<concrete_streamtrace<T>>
{
friend class concrete_traceview<T>;
/**
* Iterators to the beginning and end of the stream.
*/
T begin, end;
/**
* Start of the current cached segment.
*/
uint64_t segment_start = -1;
/**
* The offset within the current segment of the trace entry currently being
* inspected.
*/
uint64_t segment_offset = 0;
/**
* Decoded segment of the trace.
*/
std::unique_ptr<trace_segment> cache;
/**
* Keyframes, used for random access into the stream.
* Note: For streamtraces that we're going to want to look at a lot, we
* could dump these to disk.
*/
std::vector<keyframe> keyframes;
/**
* Lock that protects the keyframes field while the preloading thread is
* running. This is not used once finished_loading is true.
*/
std::mutex keyframe_lock;
/**
* Flag that we have completed computing keyframes for the entire trace.
*/
std::atomic<bool> finished_loading = { false };
/**
* Flag to kill the perload thread if the trace is finished.
*/
std::atomic<bool> cancel = { false };
/**
* Condition variable used to allow the main thread to wait for the
* preloading thread to catch up.
*/
std::condition_variable notify;
/**
* The thread that computes the keyframes for faster random access.
*/
std::thread preload_thread;
/**
* The disassembler used when materialising trace entries.
*/
disassembler::disassembler disass;
/**
* The callback that will be invoked when preloading.
*/
notifier callback;
/**
* Scans the entire streamtrace and records keyframes for faster seeking.
*
* This method is expected to be invoked precisely once, from another
* thread.
*/
void preload()
{
assert(!finished_loading);
keyframe kf;
disassembler::disassembler d;
// On the first loop iteration, we want to push the keyframe into the
// list
int frame = 1;
uint64_t frames_loaded = 0;
for (T i=begin ; i!=end ; ++i)
{
frames_loaded++;
if (cancel)
{
return;
}
debug_trace_entry e(*i, d);
kf.update(e, d);
if (--frame == 0)
{
frame = keyframe_interval;
std::lock_guard<std::mutex> lock(keyframe_lock);
keyframes.push_back(kf);
notify.notify_all();
if (callback && callback(this, frames_loaded, false))
{
break;
}
}
}
if (callback)
{
callback(this, frames_loaded, true);
}
finished_loading = true;
notify.notify_all();
}
/**
* Returns the keyframe associated with a specific offset. This is
* thread-safe with respect to a thread running the `preload()` method. It
* will block until preloading is finished.
*/
keyframe get_keyframe(uint64_t offset)
{
offset /= keyframe_interval;
// This looks racy, but is permitted because finished_loading never
// transition from true to false. Once it has become true, we do not
// need to do any locking to access the keyframes vector.
if (finished_loading)
{
return keyframes[offset];
}
std::unique_lock<std::mutex> lock(keyframe_lock);
if (finished_loading || keyframes.size() > offset)
{
return keyframes[offset];
}
notify.wait(lock, [&]() { return finished_loading || keyframes.size() > offset; });
return keyframes[offset];
}
/**
* Load a cached segment that we can iterate over. This constructs a new
* segment and so is safe to call from multiple threads (e.g. in the
* `trace` methods).
*/
std::unique_ptr<trace_segment> create_segment_for_index(uint64_t offset)
{
auto kf = get_keyframe(offset);
uint64_t segstart = (offset / keyframe_interval) * keyframe_interval;
uint64_t length = std::min(keyframe_interval, (end - begin) - segstart);
T segment_begin = begin + segstart;
T segment_end = segment_begin + length;
return std::unique_ptr<trace_segment>(new trace_segment(disass, kf,
std::move(segment_begin), std::move(segment_end)));
}
bool cache_segment(uint64_t offset)
{
if (offset / keyframe_interval == (segment_start / keyframe_interval))
{
return true;
}
if (offset > end-begin)
{
return false;
}
segment_start = (offset / keyframe_interval) * keyframe_interval;
cache = std::move(create_segment_for_index(offset));
return true;
}
public:
/**
* Construct a streamtrace from two iterators.
*/
concrete_streamtrace(T &&b, T &&e, notifier fn) : begin(b), end(e), callback(fn)
{
preload_thread = std::thread([&] { preload(); });
}
~concrete_streamtrace()
{
cancel = true;
preload_thread.join();
}
uint64_t size() override
{
return end-begin;
}
uint64_t instruction_number_for_index(uint64_t idx) override
{
return idx;
}
void scan(scanner fn, uint64_t start, uint64_t scan_end, int opts) override
{
int inc;
disassembler::disassembler d;
if (!scan_range(start, scan_end, opts, inc, end-begin))
{
return;
}
for (T i=begin+start,e=begin+scan_end ; i!=e ; i+=inc)
{
debug_trace_entry te(*i, d);
if (fn(te, start))
{
return;
}
start += inc;
}
}
void scan(detailed_scanner fn, uint64_t start, uint64_t scan_end, int opts=0) override
{
int inc;
if (!scan_range(start, scan_end, opts, inc, end-begin))
{
return;
}
uint64_t segstart = -1;
std::unique_ptr<trace_segment> segment;
for (; start!=scan_end ; start+=inc)
{
if (segstart != (start / keyframe_interval))
{
segment = std::move(create_segment_for_index(start));
segstart = (start / keyframe_interval);
}
uint64_t offset = start % keyframe_interval;
auto ®s = segment->regs[offset];
auto &entry = segment->entries[offset];
if (fn(entry, regs, start))
{
break;
}
}
}
void scan(scanner fn) override
{
uint64_t count = 0;
disassembler::disassembler d;
for (T i=begin ; i!=end ; ++i)
{
debug_trace_entry e(*i, d);
if (fn(e, count++))
{
return;
}
}
}
std::shared_ptr<trace_view> filter(filter_predicate fn) override
{
uint64_t idx = 0;
index_map m;
disassembler::disassembler d;
for (T i=begin ; i!=end ; ++i)
{
debug_trace_entry e(*i, d);
if (fn(e))
{
m.push_back(idx);
}
idx++;
}
return std::make_shared<concrete_traceview<T>>(std::enable_shared_from_this<concrete_streamtrace<T>>::shared_from_this(), std::move(m));
}
bool seek_to(uint64_t offset) override
{
if (!cache_segment(offset))
{
return false;
}
assert(segment_start != 0xffffffffffffffff);
segment_offset = (offset - segment_start);
return true;
}
virtual debug_trace_entry get_entry() override
{
assert(cache);
assert(cache->entries.size() > segment_offset);
return cache->entries[segment_offset];
}
virtual register_set get_regs() override
{
assert(cache);
assert(cache->regs.size() > segment_offset);
return cache->regs[segment_offset];
}
};
/**
* A `trace` subclass that refers to another trace. This is the result of
* calling `trace::filter()`.
*/
template<class T>
class concrete_traceview : public trace_view
{
index_map indexes;
std::shared_ptr<concrete_streamtrace<T>> t;
public:
/**
* Construct a new trace view from a concrete trace and a range of indexes
* in it. This is used even when constructing a view from a view.
*/
concrete_traceview(decltype(t) tr, index_map &&i) : indexes(i), t(tr) {}
uint64_t instruction_number_for_index(uint64_t idx) override
{
return indexes[idx];
}
uint64_t size() override
{
return indexes.size();
}
bool seek_to(uint64_t offset) override
{
return t->seek_to(indexes[offset]);
}
debug_trace_entry get_entry() override
{
return t->get_entry();
}
register_set get_regs() override
{
return t->get_regs();
}
void scan(scanner fn) override
{
scan(fn, 0, size()-1, forewards);
}
void scan(detailed_scanner fn, uint64_t start, uint64_t scan_end, int opts=0) override
{
int inc;
if (!scan_range(start, scan_end, opts, inc, indexes.size()))
{
return;
}
uint64_t segstart = -1;
std::unique_ptr<trace_segment> segment;
for (; start!=scan_end ; start+=inc)
{
auto i = indexes[start];
if (segstart != (i / keyframe_interval))
{
segment = std::move(t->create_segment_for_index(i));
segstart = (i / keyframe_interval);
}
uint64_t offset = i % keyframe_interval;
auto ®s = segment->regs[offset];
auto &entry = segment->entries[offset];
if (fn(entry, regs, i))
{
break;
}
}
}
void scan(scanner fn, uint64_t start, uint64_t scan_end, int opts) override
{
int inc;
uint64_t loop_end = scan_end;
if (!scan_range(start, loop_end, opts, inc, indexes.size()))
{
return;
}
auto trace_iter = t->begin;
auto begin = indexes.begin();
disassembler::disassembler d;
size_t last_index = 0;
for (auto i=begin+start,e=begin+loop_end ; i!=e ; i+=inc)
{
trace_iter += (*i - last_index);
last_index = *i;
debug_trace_entry te(*trace_iter, d);
if (fn(te, (*i)))
{
return;
}
}
}
std::shared_ptr<trace_view> filter(filter_predicate fn) override
{
auto trace_iter = t->begin;
size_t last_index = 0;
index_map m;
disassembler::disassembler d;
for (uint64_t idx : indexes)
{
trace_iter += (idx - last_index);
last_index = idx;
debug_trace_entry te(*trace_iter, d);
if (fn(te))
{
m.push_back(idx);
}
}
return std::make_shared<concrete_traceview<T>>(t, std::move(m));
}
std::shared_ptr<trace_view> inverted_view() override
{
return std::make_shared<concrete_traceview<T>>(t, indexes.inverted_map(t->size()));
}
};
/**
* Metadata describing v1 trace files.
*/
struct trace_v1_traits {
/**
* v1 traces have no header
*/
__attribute__((unused)) // This is used, but only via template instantiation.
static const int offset = 0;
/**
* Format of the trace entries.
*/
typedef debug_trace_entry_disk_v1 format;
};
/**
* Metadata describing v2 trace files.
*/
struct trace_v2_traits {
/**
* v2 traces have one byte of version number then CheriStreamTrace as a
* string.
*/
static const int offset = sizeof(debug_trace_entry_disk);
/**
* Format of the trace entries.
*/
typedef debug_trace_entry_disk format;
};
/**
* Metadata describing v3 trace files.
*/
struct trace_v3_traits {
/**
* v3 traces have one byte of version number then CheriTraceV03 as a
* string.
*/
static const int offset = sizeof(debug_trace_entry_disk_v3);
/**
* Format of the trace entries.
*/
typedef debug_trace_entry_disk_v3 format;
};
/**
* Abstract class implementing file reading.
*
* Subclasses of this read from either raw files or compressed files.
*
* All subclasses of this must be thread safe. Any of the methods in this
* class can be called from any thread.
*/
struct file : public fast_enumeration<uint8_t>
{
/**
* Read part of the file. This reads `length` bytes from offset `start` in
* the file, writing the result into `buffer`. The return value is the
* number of bytes read. This is guaranteed to be `length` for any
* in-bounds reads.
*/
virtual size_t read(void *buffer, off_t start, size_t length) = 0;
/**
* Virtual destructor, allowing cleanup in subclasses.
*/
virtual ~file() {}
/**
* Returns the logical size of the file (for compressed files, this returns
* the size of the uncompressed file.
*/
virtual size_t size() = 0;
/**
* Opens a named file. This will automatically detect if the file is
* xz-compressed.
*/
static std::shared_ptr<file> open(const std::string &file);
bool enumerate(enumerator &e, size_t start) override