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
use std::cmp::Ordering;
use crate::fast_hash_map::FastHashMap;
use crate::frame_table::InternalFrameLocation;
use crate::global_lib_table::{GlobalLibIndex, GlobalLibTable};
use crate::lib_info::Lib;
use crate::library_info::LibraryInfo;
use crate::Timestamp;
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct ThreadHandle(pub(crate) usize);
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct ProcessLibIndex(usize);
#[derive(Debug)]
pub struct Process {
pid: u32,
name: String,
threads: Vec<ThreadHandle>,
start_time: Timestamp,
end_time: Option<Timestamp>,
libs: Vec<Lib>,
sorted_lib_ranges: Vec<ProcessLibRange>,
used_lib_map: FastHashMap<ProcessLibIndex, GlobalLibIndex>,
}
impl Process {
pub fn new(name: &str, pid: u32, start_time: Timestamp) -> Self {
Self {
pid,
threads: Vec::new(),
sorted_lib_ranges: Vec::new(),
used_lib_map: FastHashMap::default(),
libs: Vec::new(),
start_time,
end_time: None,
name: name.to_owned(),
}
}
pub fn set_start_time(&mut self, start_time: Timestamp) {
self.start_time = start_time;
}
pub fn start_time(&self) -> Timestamp {
self.start_time
}
pub fn set_end_time(&mut self, end_time: Timestamp) {
self.end_time = Some(end_time);
}
pub fn end_time(&self) -> Option<Timestamp> {
self.end_time
}
pub fn set_name(&mut self, name: &str) {
self.name = name.to_string();
}
pub fn name(&self) -> &str {
&self.name
}
pub fn add_thread(&mut self, thread: ThreadHandle) {
self.threads.push(thread);
}
pub fn pid(&self) -> u32 {
self.pid
}
pub fn cmp_for_json_order(&self, other: &Process) -> Ordering {
if let Some(ordering) = self.start_time.partial_cmp(&other.start_time) {
if ordering != Ordering::Equal {
return ordering;
}
}
self.pid.cmp(&other.pid)
}
pub fn threads(&self) -> Vec<ThreadHandle> {
self.threads.clone()
}
pub fn convert_address(
&mut self,
global_libs: &mut GlobalLibTable,
address: u64,
) -> InternalFrameLocation {
let ranges = &self.sorted_lib_ranges[..];
let index = match ranges.binary_search_by_key(&address, |r| r.start) {
Err(0) => return InternalFrameLocation::UnknownAddress(address),
Ok(exact_match) => exact_match,
Err(insertion_index) => {
let range_index = insertion_index - 1;
if address < ranges[range_index].end {
range_index
} else {
return InternalFrameLocation::UnknownAddress(address);
}
}
};
let range = &ranges[index];
let process_lib = range.lib_index;
let relative_address = (address - range.base) as u32;
let lib_index = self.convert_lib_index(process_lib, global_libs);
InternalFrameLocation::AddressInLib(relative_address, lib_index)
}
pub fn convert_lib_index(
&mut self,
process_lib: ProcessLibIndex,
global_libs: &mut GlobalLibTable,
) -> GlobalLibIndex {
let libs = &self.libs;
*self
.used_lib_map
.entry(process_lib)
.or_insert_with(|| global_libs.index_for_lib(libs[process_lib.0].clone()))
}
pub fn add_lib(&mut self, lib: LibraryInfo) {
let lib_index = ProcessLibIndex(self.libs.len());
self.libs.push(Lib {
name: lib.name,
debug_name: lib.debug_name,
path: lib.path,
debug_path: lib.debug_path,
arch: lib.arch,
debug_id: lib.debug_id,
code_id: lib.code_id,
symbol_table: lib.symbol_table,
});
let insertion_index = match self
.sorted_lib_ranges
.binary_search_by_key(&lib.avma_range.start, |r| r.start)
{
Ok(i) => {
self.sorted_lib_ranges.remove(i);
i
}
Err(i) => i,
};
self.sorted_lib_ranges.insert(
insertion_index,
ProcessLibRange {
lib_index,
base: lib.base_avma,
start: lib.avma_range.start,
end: lib.avma_range.end,
},
);
}
pub fn unload_lib(&mut self, base_address: u64) {
self.sorted_lib_ranges.retain(|r| r.base != base_address);
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)]
struct ProcessLibRange {
start: u64,
end: u64,
lib_index: ProcessLibIndex,
base: u64,
}