ghostscope 0.1.1

Command-line entrypoint that drives GhostScope compiler, loader, and UI end-to-end.
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
//! -t (target path) mode globals tests
//! Includes both pre-start targets and late-start (GhostScope first) scenarios

mod common;

use common::{init, FIXTURES};
use ghostscope_process::is_shared_object;
use regex::Regex;
use std::process::Stdio;
use std::time::Duration;
use tokio::process::Command;

async fn run_ghostscope_with_script_for_target(
    script_content: &str,
    timeout_secs: u64,
    target_path: &std::path::Path,
) -> anyhow::Result<(i32, String, String)> {
    let enable_sysmon = is_shared_object(target_path);
    common::runner::GhostscopeRunner::new()
        .with_script(script_content)
        .with_target(target_path)
        .timeout_secs(timeout_secs)
        .enable_sysmon_shared_lib(enable_sysmon)
        .run()
        .await
}

// Late-start helper: run GhostScope first, then start the target process after a delay
async fn run_ghostscope_then_start_exe(
    script_content: &str,
    timeout_secs: u64,
    target_path: &std::path::Path,
    launcher_exe: &std::path::Path,
    launch_delay_ms: u64,
) -> anyhow::Result<(i32, String, String, tokio::process::Child, u32)> {
    // Spawn GhostScope in the background
    let runner = common::runner::GhostscopeRunner::new()
        .with_script(script_content)
        .with_target(target_path)
        .timeout_secs(timeout_secs);
    let gs_task = tokio::spawn(async move { runner.run().await });

    // Start target process after a small delay
    tokio::time::sleep(Duration::from_millis(launch_delay_ms)).await;
    let bin_dir = launcher_exe.parent().unwrap().to_path_buf();
    let prog = Command::new(launcher_exe)
        .current_dir(bin_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let pid = prog
        .id()
        .ok_or_else(|| anyhow::anyhow!("Failed to get PID for late-start target"))?;

    // Wait for GhostScope to finish (timeout-based)
    let (exit_code, stdout, stderr) = gs_task
        .await
        .map_err(|e| anyhow::anyhow!("GhostScope task join error: {e}"))??;

    Ok((exit_code, stdout, stderr, prog, pid))
}

// ---------------------------------
// Pre-start (-t) tests (target first)
// ---------------------------------

#[tokio::test]
async fn test_t_mode_executable_globals_prints() -> anyhow::Result<()> {
    init();

    let binary_path = FIXTURES.get_test_binary("globals_program")?;
    let bin_dir = binary_path.parent().unwrap().to_path_buf();
    let mut prog = Command::new(&binary_path)
        .current_dir(bin_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let pid = prog
        .id()
        .ok_or_else(|| anyhow::anyhow!("Failed to get PID for -t exec"))?;
    tokio::time::sleep(Duration::from_millis(500)).await;

    let script = r#"
trace globals_program.c:32 {
    print "PID:{} GY:{}", $pid, G_STATE.inner.y;
}
"#;
    let (exit_code, stdout, stderr) =
        run_ghostscope_with_script_for_target(script, 5, &binary_path).await?;
    let _ = prog.kill().await.is_ok();
    assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");

    let re = Regex::new(r"PID:([0-9]+) GY:([0-9]+(?:\.[0-9]+)?)").unwrap();
    let mut vals: Vec<f64> = Vec::new();
    for line in stdout.lines() {
        if let Some(c) = re.captures(line) {
            let ev_pid: u32 = c[1].parse().unwrap_or(0);
            if ev_pid == pid {
                vals.push(c[2].parse().unwrap_or(0.0));
            }
        }
    }
    let mut uniq: Vec<f64> = Vec::new();
    for v in vals.into_iter() {
        if uniq
            .last()
            .copied()
            .map(|u| (u - v).abs() < 1e-9)
            .unwrap_or(false)
        {
            continue;
        }
        uniq.push(v);
    }
    if uniq.len() >= 2 {
        let d = ((uniq[1] - uniq[0]) * 100.0).round() as i64;
        assert_eq!(
            d, 50,
            "G_STATE.inner.y should +0.5 per tick. STDOUT: {stdout}"
        );
    } else {
        assert!(
            !uniq.is_empty(),
            "No events for target PID. STDOUT: {stdout}"
        );
    }
    Ok(())
}

#[tokio::test]
async fn test_t_mode_library_globals_prints() -> anyhow::Result<()> {
    init();

    let binary_path = FIXTURES.get_test_binary("globals_program")?;
    let bin_dir = binary_path.parent().unwrap().to_path_buf();
    let lib_path = bin_dir.join("libgvars.so");
    let mut prog = Command::new(&binary_path)
        .current_dir(&bin_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let pid = prog
        .id()
        .ok_or_else(|| anyhow::anyhow!("Failed to get PID for -t lib"))?;
    tokio::time::sleep(Duration::from_millis(500)).await;

    let script = r#"
trace lib_tick {
    print "PID:{} LC:{}", $pid, LIB_STATE.counter;
}
"#;
    let (exit_code, stdout, stderr) =
        run_ghostscope_with_script_for_target(script, 2, &lib_path).await?;
    let _ = prog.kill().await.is_ok();
    assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");

    let re = Regex::new(r"PID:([0-9]+) LC:([0-9]+)").unwrap();
    let mut vals: Vec<i64> = Vec::new();
    for line in stdout.lines() {
        if let Some(c) = re.captures(line) {
            let ev_pid: u32 = c[1].parse().unwrap_or(0);
            if ev_pid == pid {
                vals.push(c[2].parse().unwrap_or(0));
            }
        }
    }
    let mut uniq: Vec<i64> = Vec::new();
    for v in vals.into_iter() {
        if uniq.last().copied() == Some(v) {
            continue;
        }
        uniq.push(v);
    }
    if uniq.len() >= 2 {
        let d = uniq[1] - uniq[0];
        assert_eq!(
            d, 2,
            "LIB_STATE.counter should +2 per tick. STDOUT: {stdout}"
        );
    } else {
        assert!(
            !uniq.is_empty(),
            "No events for target PID. STDOUT: {stdout}"
        );
    }
    Ok(())
}

#[tokio::test]
async fn test_t_mode_executable_rodata_and_struct_pretty() -> anyhow::Result<()> {
    init();

    let binary_path = FIXTURES.get_test_binary("globals_program")?;
    let bin_dir = binary_path.parent().unwrap().to_path_buf();
    let mut prog = Command::new(&binary_path)
        .current_dir(bin_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let pid = prog
        .id()
        .ok_or_else(|| anyhow::anyhow!("Failed to get PID for -t rodata/struct"))?;
    tokio::time::sleep(Duration::from_millis(500)).await;

    // At line 26, aliases (s, ls, gm, etc.) are initialized
    let script = r#"
trace globals_program.c:26 {
    print "PID:{} GM:{:s.32}", $pid, gm;   // rodata string (explicit string format)
    print *s;                              // struct pretty print (deref pointer)
}
"#;
    let (exit_code, stdout, stderr) =
        run_ghostscope_with_script_for_target(script, 2, &binary_path).await?;
    let _ = prog.kill().await.is_ok();
    assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");

    // Verify we saw our PID and the expected GM string for that PID (quoted or unquoted)
    let re = Regex::new(r#"PID:([0-9]+) GM:([^\r\n]+)"#).unwrap();
    let mut saw_gm_for_pid = false;
    for line in stdout.lines() {
        if let Some(c) = re.captures(line) {
            let ev_pid: u32 = c[1].parse().unwrap_or(0);
            if ev_pid == pid {
                let s = c[2].trim();
                if s.contains("Hello, Global!") {
                    saw_gm_for_pid = true;
                    break;
                }
            }
        }
    }
    assert!(
        saw_gm_for_pid,
        "Expected GM string for our PID. STDOUT: {stdout}"
    );

    assert!(
        stdout.contains("GlobalState {"),
        "Expected pretty struct output. STDOUT: {stdout}"
    );

    Ok(())
}

// ---------------------------------
// Late-start (-t) tests (GhostScope first)
// ---------------------------------

#[tokio::test]
async fn test_t_mode_executable_late_start_globals_prints() -> anyhow::Result<()> {
    init();

    // GhostScope starts first (-t points to the executable), then we start the process
    let binary_path = FIXTURES.get_test_binary("globals_program")?;
    let script = r#"
trace globals_program.c:32 {
    print "PID:{} GY:{}", $pid, G_STATE.inner.y;
}
"#;

    let (exit_code, stdout, stderr, mut prog, pid) = run_ghostscope_then_start_exe(
        script,
        3,            // allow some time for sysmon -> prefill -> events
        &binary_path, // -t target
        &binary_path, // launcher (the same executable)
        500,          // start target 0.5s after GhostScope
    )
    .await?;

    let _ = prog.kill().await.is_ok();
    assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");

    let re = Regex::new(r"PID:([0-9]+) GY:([0-9]+(?:\.[0-9]+)?)").unwrap();
    let mut vals: Vec<f64> = Vec::new();
    for line in stdout.lines() {
        if let Some(c) = re.captures(line) {
            let ev_pid: u32 = c[1].parse().unwrap_or(0);
            if ev_pid == pid {
                vals.push(c[2].parse().unwrap_or(0.0));
            }
        }
    }
    let mut uniq: Vec<f64> = Vec::new();
    for v in vals.into_iter() {
        if uniq
            .last()
            .copied()
            .map(|u| (u - v).abs() < 1e-9)
            .unwrap_or(false)
        {
            continue;
        }
        uniq.push(v);
    }
    if uniq.len() >= 2 {
        let d = ((uniq[1] - uniq[0]) * 100.0).round() as i64;
        assert_eq!(
            d, 50,
            "Late-start: G_STATE.inner.y should +0.5 per tick. STDOUT: {stdout}"
        );
    } else {
        let msg = format!("Late-start: No events for our PID {pid}. STDOUT: {stdout}");
        assert!(!uniq.is_empty(), "{}", msg);
    }
    Ok(())
}

#[tokio::test]
async fn test_t_mode_library_late_start_globals_prints() -> anyhow::Result<()> {
    init();

    // -t points to libgvars.so; GhostScope first, then we run the executable which loads the lib
    let binary_path = FIXTURES.get_test_binary("globals_program")?;
    let bin_dir = binary_path.parent().unwrap().to_path_buf();
    let lib_path = bin_dir.join("libgvars.so");
    let script = r#"
trace lib_tick {
    print "PID:{} LC:{}", $pid, LIB_STATE.counter;
}
"#;

    // Spawn GhostScope with saving + trace logs enabled
    let gs_task = {
        let target = lib_path.clone();
        let sc = script.to_string();
        tokio::spawn(async move {
            common::runner::GhostscopeRunner::new()
                .with_script(&sc)
                .with_target(&target)
                .timeout_secs(12)
                .with_log_level("trace")
                .enable_sysmon_shared_lib(true)
                .run()
                .await
        })
    };

    // Start the target process after a short delay
    tokio::time::sleep(Duration::from_millis(700)).await;
    let mut prog = Command::new(&binary_path)
        .current_dir(&bin_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let pid = prog
        .id()
        .ok_or_else(|| anyhow::anyhow!("Failed to get PID for -t lib late-start"))?;

    // Wait for GhostScope to finish
    let (exit_code, stdout, stderr) = gs_task
        .await
        .map_err(|e| anyhow::anyhow!("GhostScope task join error: {e}"))??;
    let _ = prog.kill().await.is_ok();
    assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");

    let re = Regex::new(r"PID:([0-9]+) LC:([0-9]+)").unwrap();
    let mut vals: Vec<i64> = Vec::new();
    for line in stdout.lines() {
        if let Some(c) = re.captures(line) {
            let ev_pid: u32 = c[1].parse().unwrap_or(0);
            if ev_pid == pid {
                vals.push(c[2].parse().unwrap_or(0));
            }
        }
    }
    let mut uniq: Vec<i64> = Vec::new();
    for v in vals.into_iter() {
        if uniq.last().copied() == Some(v) {
            continue;
        }
        uniq.push(v);
    }
    if uniq.len() >= 2 {
        let d = uniq[1] - uniq[0];
        assert_eq!(
            d, 2,
            "Late-start: LIB_STATE.counter should +2 per tick. STDOUT: {stdout}"
        );
    } else {
        let log_dump = tokio::fs::read_to_string("ghostscope.log")
            .await
            .unwrap_or_else(|_| "<ghostscope.log unavailable>".to_string());
        let msg =
            format!("Late-start: No events for our PID {pid}. STDOUT: {stdout}. LOG: {log_dump}");
        assert!(!uniq.is_empty(), "{}", msg);
    }
    let _ = tokio::fs::remove_file("ghostscope.log").await;
    Ok(())
}

#[tokio::test]
async fn test_t_mode_executable_late_start_rodata_and_struct_pretty() -> anyhow::Result<()> {
    init();

    let binary_path = FIXTURES.get_test_binary("globals_program")?;
    // At line 26, aliases (s, ls, gm, etc.) are initialized
    let script = r#"
trace globals_program.c:26 {
    print "PID:{} GM:{:s.32}", $pid, gm;   // rodata string
    print *s;                              // struct pretty print
}
"#;

    let (exit_code, stdout, stderr, mut prog, pid) =
        run_ghostscope_then_start_exe(script, 3, &binary_path, &binary_path, 500).await?;

    let _ = prog.kill().await.is_ok();
    assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");

    // Verify we saw our PID and the expected GM string for that PID
    let re = Regex::new(r#"PID:([0-9]+) GM:([^\r\n]+)"#).unwrap();
    let mut saw_gm_for_pid = false;
    for line in stdout.lines() {
        if let Some(c) = re.captures(line) {
            let ev_pid: u32 = c[1].parse().unwrap_or(0);
            if ev_pid == pid {
                let s = c[2].trim();
                if s.contains("Hello, Global!") {
                    saw_gm_for_pid = true;
                    break;
                }
            }
        }
    }
    assert!(
        saw_gm_for_pid,
        "Late-start: Expected GM string for our PID. STDOUT: {stdout}"
    );

    assert!(
        stdout.contains("GlobalState {"),
        "Late-start: Expected pretty struct output. STDOUT: {stdout}"
    );

    Ok(())
}

#[tokio::test]
async fn test_t_mode_library_late_start_without_sysmon_offsets_unavailable() -> anyhow::Result<()> {
    init();

    let binary_path = FIXTURES.get_test_binary("globals_program")?;
    let bin_dir = binary_path.parent().unwrap().to_path_buf();
    let lib_path = bin_dir.join("libgvars.so");
    let script = r#"
trace lib_tick {
    print "PID:{} SCALAR={}", $pid, LIB_STATE.counter;
    print "PID:{} STRUCT_Y={}", $pid, LIB_STATE.inner.y;
    print "PID:{} ARRAY0={}", $pid, LIB_STATE.array[0];
    print "PID:{} RODATA={}", $pid, lib_message;
    print "PID:{} MEMDUMP={:x.4}", $pid, lib_pattern;
    if memcmp(lib_message, hex("4c"), 1) { print "PID:{} CMP_OK", $pid; }
    print "PID:{} AFTER_CMP", $pid;
    print "PID:{} TAIL", $pid;
}
"#;

    let gs_task = {
        let target = lib_path.clone();
        let sc = script.to_string();
        tokio::spawn(async move {
            common::runner::GhostscopeRunner::new()
                .with_script(&sc)
                .with_target(&target)
                .timeout_secs(8)
                .enable_sysmon_shared_lib(false)
                .run()
                .await
        })
    };

    tokio::time::sleep(Duration::from_millis(500)).await;
    let mut prog = Command::new(&binary_path)
        .current_dir(&bin_dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()?;
    let pid = prog.id().ok_or_else(|| {
        anyhow::anyhow!("Failed to get PID for late-start shared lib without sysmon")
    })?;

    let (exit_code, stdout, stderr) = gs_task
        .await
        .map_err(|e| anyhow::anyhow!("GhostScope task join error: {e}"))??;
    let _ = prog.kill().await.is_ok();

    assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");

    let pid_marker = format!("PID:{pid}");
    let mut scalar_ok = false;
    let mut struct_ok = false;
    let mut array_ok = false;
    let mut rodata_ok = false;
    let mut memdump_ok = false;

    for line in stdout.lines() {
        if !line.contains(&pid_marker) {
            continue;
        }
        if line.contains("SCALAR=<proc offsets unavailable>") {
            scalar_ok = true;
        }
        if line.contains("STRUCT_Y=<proc offsets unavailable>") {
            struct_ok = true;
        }
        if line.contains("ARRAY0=<proc offsets unavailable>") {
            array_ok = true;
        }
        if line.contains("RODATA=<proc offsets unavailable>") {
            rodata_ok = true;
        }
        if line.contains("MEMDUMP=<proc offsets unavailable>") {
            memdump_ok = true;
        }
    }

    assert!(
        scalar_ok,
        "Expected SCALAR offsets unavailable for PID {pid}. STDOUT: {stdout}"
    );
    assert!(
        struct_ok,
        "Expected STRUCT_Y offsets unavailable for PID {pid}. STDOUT: {stdout}"
    );
    assert!(
        array_ok,
        "Expected ARRAY0 offsets unavailable for PID {pid}. STDOUT: {stdout}"
    );
    assert!(
        rodata_ok,
        "Expected RODATA offsets unavailable for PID {pid}. STDOUT: {stdout}"
    );
    assert!(
        memdump_ok,
        "Expected MEMDUMP offsets unavailable for PID {pid}. STDOUT: {stdout}"
    );

    assert!(
        stdout.contains("ExprError"),
        "Expected ExprError line from memcmp failure. STDOUT: {stdout}"
    );
    assert!(
        stdout.contains(&format!("PID:{pid} AFTER_CMP")),
        "Expected AFTER_CMP marker for PID {pid}. STDOUT: {stdout}"
    );
    assert!(
        stdout.contains(&format!("PID:{pid} TAIL")),
        "Expected TAIL marker for PID {pid}. STDOUT: {stdout}"
    );
    assert!(
        !stdout.contains(&format!("PID:{pid} CMP_OK")),
        "memcmp should not succeed without offsets. STDOUT: {stdout}"
    );
    assert!(
        !stdout.contains("read_user failed"),
        "Should not surface raw read_user errors. STDOUT: {stdout}"
    );

    Ok(())
}