CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/MIPSTracer.h
Views: 1401
1
// Copyright (c) 2024- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <unordered_map>
21
#include <vector>
22
#include <string>
23
24
#include "Common/CommonTypes.h"
25
#include "Core/Opcode.h"
26
#include "Core/MIPS/IR/IRJit.h"
27
#include "Common/Log.h"
28
#include "Common/File/Path.h"
29
#include "Common/Data/Collections/CyclicBuffer.h"
30
31
32
struct TraceBlockInfo {
33
u32 virt_address;
34
u32 storage_index;
35
};
36
37
struct TraceBlockStorage {
38
std::vector<u32> raw_instructions;
39
u32 cur_index;
40
u32* cur_data_ptr;
41
42
TraceBlockStorage(u32 capacity):
43
raw_instructions(capacity, 0),
44
cur_index(0),
45
cur_data_ptr(raw_instructions.data())
46
{}
47
48
TraceBlockStorage(): raw_instructions(), cur_index(0), cur_data_ptr(nullptr) {}
49
50
bool save_block(const u32* instructions, u32 size);
51
52
void initialize(u32 capacity);
53
void clear();
54
55
u32 operator[](u32 index) {
56
return raw_instructions[index];
57
}
58
Memory::Opcode read_asm(u32 index) {
59
return Memory::Opcode(raw_instructions[index]);
60
}
61
};
62
63
64
65
// This system is meant for trace recording.
66
// A trace here stands for a sequence of instructions and their respective addresses in RAM.
67
// The register/memory changes (or thread switches) are not included!
68
// Note: the tracer stores the basic blocks inside, which causes the last block to be dumped as a whole,
69
// despite the fact that it may not have executed to its end by the time the tracer is stopped.
70
struct MIPSTracer {
71
std::vector<TraceBlockInfo> trace_info;
72
73
// The trace might be very big, in that case I don't mind losing the oldest entries.
74
CyclicBuffer<u32> executed_blocks;
75
76
std::unordered_map<u64, u32> hash_to_storage_index;
77
78
TraceBlockStorage storage;
79
80
Path logging_path;
81
FILE* output;
82
bool tracing_enabled = false;
83
84
int in_storage_capacity = 0x10'0000;
85
int in_max_trace_size = 0x10'0000;
86
87
void start_tracing();
88
void stop_tracing();
89
90
void prepare_block(const MIPSComp::IRBlock* block, MIPSComp::IRBlockCache& blocks);
91
void set_logging_path(std::string path) {
92
logging_path = Path(path);
93
}
94
std::string get_logging_path() const {
95
return logging_path.ToString();
96
}
97
98
bool flush_to_file();
99
void flush_block_to_file(const TraceBlockInfo& block);
100
101
void initialize(u32 storage_capacity, u32 max_trace_size);
102
void clear();
103
104
inline void print_stats() const;
105
106
MIPSTracer(): trace_info(), executed_blocks(), hash_to_storage_index(), storage(), logging_path() {}
107
};
108
109
extern MIPSTracer mipsTracer;
110
111