-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlib.rs
More file actions
225 lines (198 loc) · 7.87 KB
/
lib.rs
File metadata and controls
225 lines (198 loc) · 7.87 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
// Keep library clippy-clean without allow attributes
// New modular organization
pub mod ebpf;
pub mod script; // New instruction generator
// Legacy codegen - kept for reference, not compiled
// pub mod codegen_legacy;
// pub mod codegen_new;
use crate::script::compiler::AstCompiler;
use ebpf::context::CodeGenError;
pub use ghostscope_process::{PidFilterSpec, PidNamespaceId};
use script::parser::ParseError;
use tracing::info;
pub fn hello() -> &'static str {
"Hello from ghostscope-compiler!"
}
#[derive(Debug, thiserror::Error)]
pub enum CompileError {
#[error("Parse error: {0}")]
Parse(#[from] Box<ParseError>),
#[error("Code generation error: {0}")]
CodeGen(#[from] CodeGenError),
#[error("LLVM error: {0}")]
LLVM(String),
#[error("{0}")]
Other(String),
}
pub type Result<T> = std::result::Result<T, CompileError>;
impl From<ParseError> for CompileError {
fn from(err: ParseError) -> Self {
CompileError::Parse(Box::new(err))
}
}
// Public re-exports from script::compiler module
pub use script::compiler::{CompilationResult, UProbeConfig};
/// Event output map type for eBPF tracing
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventMapType {
/// BPF_MAP_TYPE_RINGBUF (requires kernel >= 5.8)
RingBuf,
/// BPF_MAP_TYPE_PERF_EVENT_ARRAY (kernel >= 4.3, fallback)
PerfEventArray,
}
/// Compilation options including save options and eBPF map configuration
#[derive(Debug, Clone)]
pub struct CompileOptions {
pub save_llvm_ir: bool,
pub save_ebpf: bool,
pub save_ast: bool,
pub binary_path_hint: Option<String>,
pub ringbuf_size: u64,
pub proc_module_offsets_max_entries: u64,
pub perf_page_count: u32,
pub event_map_type: EventMapType,
/// Max bytes to read per memory-dump argument (format {:x}/{:s}).
pub mem_dump_cap: u32,
/// Max bytes to compare for string/memory comparisons (strncmp/starts_with/memcmp)
pub compare_cap: u32,
/// Max total bytes in a single trace event (used for PerfEventArray accumulation buffer size).
pub max_trace_event_size: u32,
/// Optional single-address filter: if set, only the Nth (1-based) address
/// resolved for a target will be compiled. When None, compile all.
pub selected_index: Option<usize>,
/// Optional PID filter strategy override.
/// When None, compiler falls back to HostTgid using compile_script(pid).
pub pid_filter_spec: Option<PidFilterSpec>,
/// Optional PID namespace context used by special vars like `$pid`/`$tid`.
/// This is independent of PID filtering and is primarily for `-t` mode.
pub special_pid_ns: Option<PidNamespaceId>,
/// Optional PID namespace context used by `proc_module_offsets` lookups.
///
/// In `-p` mode this only switches to the target PID-namespace view when
/// GhostScope has an explicit namespace-local target PID that can be
/// aliased back to the `/proc` key used to populate
/// `proc_module_offsets`. Otherwise it stays on GhostScope's current
/// `/proc`-visible PID view.
///
/// In `-t` mode we continue to use GhostScope's own `/proc` view, because
/// offsets are discovered from `/proc/<pid>/maps` in that namespace.
pub proc_offsets_pid_ns: Option<PidNamespaceId>,
/// Optional original `-p` input PID for `$input_pid`.
/// This is only available in `-p` mode.
pub input_pid: Option<u32>,
}
impl Default for CompileOptions {
fn default() -> Self {
Self {
save_llvm_ir: false,
save_ebpf: false,
save_ast: false,
binary_path_hint: None,
ringbuf_size: 262144, // 256KB
proc_module_offsets_max_entries: 4096, // Default
perf_page_count: 64, // 64 pages = 256KB per CPU
event_map_type: EventMapType::RingBuf, // Default to RingBuf
mem_dump_cap: 256, // Default per-arg dump cap (bytes)
compare_cap: 64, // Default compare cap for strncmp/memcmp (bytes)
max_trace_event_size: 32768, // Default event size cap (32KB)
selected_index: None,
pid_filter_spec: None,
special_pid_ns: None,
proc_offsets_pid_ns: None,
input_pid: None,
}
}
}
/// Main compilation interface with DwarfAnalyzer (multi-module support)
///
/// This is the new multi-module interface that uses DwarfAnalyzer
/// to perform compilation across main executable and dynamic libraries
pub fn compile_script(
script_source: &str,
process_analyzer: &ghostscope_dwarf::DwarfAnalyzer,
pid: Option<u32>,
trace_id: Option<u32>,
compile_options: &CompileOptions,
) -> Result<CompilationResult> {
info!("Starting unified script compilation with DwarfAnalyzer (multi-module support)");
// Step 1: Parse script to AST
let program = script::parser::parse(script_source)?;
info!("Parsed script with {} statements", program.statements.len());
// Step 2: Use AstCompiler with full DwarfAnalyzer integration
let mut compiler = AstCompiler::new(
Some(process_analyzer),
compile_options.binary_path_hint.clone(),
trace_id.unwrap_or(0), // Default starting trace_id is 0 if not provided
compile_options.clone(),
);
// Step 3: Compile using unified interface
let result = compiler.compile_program(&program, pid)?;
if result.uprobe_configs.is_empty() {
if !result.failed_targets.is_empty() {
tracing::warn!(
"Compilation produced 0 uprobe configs; {} target(s) failed to compile",
result.failed_targets.len()
);
} else {
tracing::warn!(
"Compilation completed with 0 uprobe configs (no attachable targets resolved)"
);
}
} else {
info!(
"Successfully compiled script: {} trace points, {} uprobe configs",
result.trace_count,
result.uprobe_configs.len()
);
}
// Concise summary for downstream logs
info!(
"Compilation summary: trace_points={}, uprobe_configs={}, failed_targets={}",
result.trace_count,
result.uprobe_configs.len(),
result.failed_targets.len()
);
Ok(result)
}
/// Print AST for debugging
pub fn print_ast(program: &crate::script::Program) {
info!("\n=== AST Tree ===");
info!("Program:");
for (i, stmt) in program.statements.iter().enumerate() {
info!(" Statement {}: {:?}", i, stmt);
}
info!("=== End AST Tree ===\n");
}
/// Save AST to file
pub fn save_ast_to_file(program: &crate::script::Program, filename: &str) -> Result<()> {
let mut ast_content = String::new();
ast_content.push_str("=== AST Tree ===\n");
ast_content.push_str("Program:\n");
for (i, stmt) in program.statements.iter().enumerate() {
ast_content.push_str(&format!(" Statement {i}: {stmt:?}\n"));
}
ast_content.push_str("=== End AST Tree ===\n");
let file_path = format!("{filename}.txt");
std::fs::write(&file_path, ast_content)
.map_err(|e| CompileError::Other(format!("Failed to save AST file '{file_path}': {e}")))?;
Ok(())
}
/// Format eBPF bytecode as hexadecimal string for inspection
pub fn format_ebpf_bytecode(bytecode: &[u8]) -> String {
bytecode
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<Vec<String>>()
.join(" ")
}
/// Generate filename for AST files
pub fn generate_file_name_for_ast(pid: Option<u32>, binary_path: Option<&str>) -> String {
let pid_part = pid
.map(|p| p.to_string())
.unwrap_or_else(|| "unknown".to_string());
let exec_part = binary_path
.and_then(|path| std::path::Path::new(path).file_name())
.and_then(|name| name.to_str())
.unwrap_or("unknown");
format!("gs_{pid_part}_{exec_part}_ast")
}