ptools 0.2.23

Utilities for inspecting Linux processes
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
//
//   Copyright (c) 2026 Basil Crow
//
//   Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   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.
//

mod common;

fn shell_quote(arg: &str) -> String {
    if arg.is_empty() {
        return "''".to_string();
    }

    let is_shell_safe = arg
        .bytes()
        .all(|b| b.is_ascii_alphanumeric() || b"_@%+=:,./-".contains(&b));
    if is_shell_safe {
        arg.to_string()
    } else {
        format!("'{}'", arg.replace('\'', "'\"'\"'"))
    }
}

#[test]
fn pargs_matches_started_process_arguments() {
    let expected_args = [
        "simple",
        "contains spaces",
        "quote\"inside",
        "unicode-✓",
        "tabs\tinside",
    ];

    let output = common::run_ptool(
        "pargs",
        &[],
        "examples/pargs_penv",
        &expected_args,
        &[],
        false,
    );
    let stdout = common::assert_success_and_get_stdout(output);

    for arg in expected_args {
        let expected_line = arg.to_string();
        if !stdout.contains(&expected_line) {
            panic!("Argument '{arg}' not found in pargs output:\n\n{stdout}\n\n");
        }
    }

    for (i, arg) in expected_args.iter().enumerate() {
        let expected_line = format!("argv[{}]: {}", i + 1, arg);
        if !stdout.contains(&expected_line) {
            panic!("Expected line '{expected_line}' not found in pargs output:\n\n{stdout}\n\n");
        }
    }
}

#[test]
fn pargs_l_matches_started_process_arguments_as_shell_command_line() {
    let expected_args = [
        "simple",
        "contains spaces",
        "quote\"inside",
        "unicode-✓",
        "tabs\tinside",
    ];

    let output = common::run_ptool(
        "pargs",
        &["-l"],
        "examples/pargs_penv",
        &expected_args,
        &[],
        false,
    );
    let stdout = common::assert_success_and_get_stdout(output);

    let mut lines = stdout.lines();
    let command_line = lines.next().unwrap_or("");
    assert_eq!(
        lines.next(),
        None,
        "Expected a single command-line output line, got:\n\n{stdout}\n\n"
    );

    // pargs -l resolves argv[0] to the real executable path via /proc/[pid]/exe,
    // so the first token should be the canonical path to the example binary.
    let exe_path = common::find_exec("examples/pargs_penv")
        .canonicalize()
        .unwrap();
    let expected_line = std::iter::once(exe_path.to_str().unwrap())
        .chain(expected_args.iter().copied())
        .map(shell_quote)
        .collect::<Vec<_>>()
        .join(" ");
    assert_eq!(command_line, expected_line);
}

#[test]
fn penv_matches_started_process_environment() {
    let expected_env = [
        ("PTOOLS_TEST_SIMPLE", "value"),
        ("PTOOLS_TEST_WITH_SPACES", "value with spaces"),
        ("PTOOLS_TEST_WITH_EQUALS", "key=value"),
        ("PTOOLS_TEST_UNICODE", ""),
    ];

    let output = common::run_ptool(
        "penv",
        &[],
        "examples/pargs_penv",
        &[],
        &expected_env,
        false,
    );
    let stdout = common::assert_success_and_get_stdout_allow_warnings(output);

    for (key, value) in expected_env {
        let expected_line = format!("{key}={value}");
        if !stdout.contains(&expected_line) {
            panic!(
                "Environment variable '{expected_line}' not found in penv output:\n\n{stdout}\n\n"
            );
        }
    }
}

#[test]
fn pargs_e_alias_matches_started_process_environment() {
    let expected_env = [
        ("PTOOLS_TEST_SIMPLE", "value"),
        ("PTOOLS_TEST_WITH_SPACES", "value with spaces"),
        ("PTOOLS_TEST_WITH_EQUALS", "key=value"),
        ("PTOOLS_TEST_UNICODE", ""),
    ];

    let output = common::run_ptool(
        "pargs",
        &["-e"],
        "examples/pargs_penv",
        &[],
        &expected_env,
        false,
    );
    let stdout = common::assert_success_and_get_stdout_allow_warnings(output);

    for (key, value) in expected_env {
        let expected_line = format!("{key}={value}");
        if !stdout.contains(&expected_line) {
            panic!(
                "Environment variable '{expected_line}' not found in pargs -e output:\n\n{stdout}\n\n"
            );
        }
    }
}

#[test]
fn pauxv_prints_auxv_entries() {
    let output = common::run_ptool("pauxv", &[], "examples/pargs_penv", &[], &[], false);
    let stdout = common::assert_success_and_get_stdout(output);

    assert!(
        !stdout.contains("argv["),
        "pauxv should not print argv lines:\n\n{stdout}\n\n"
    );
    assert!(
        !stdout.contains("envp["),
        "pauxv should not print env lines:\n\n{stdout}\n\n"
    );

    let mut saw_auxv_line = false;
    for line in stdout.lines() {
        if !line.starts_with("AT_") {
            continue;
        }
        saw_auxv_line = true;
        let mut parts = line.split_whitespace();
        let _key = parts.next().unwrap();
        let value = parts.next().unwrap_or("");
        assert!(
            value.starts_with("0x") && (value.len() == 10 || value.len() == 18),
            "Expected auxv value to be fixed-width hex, got '{value}' in line '{line}'"
        );
    }

    assert!(
        saw_auxv_line,
        "Expected at least one auxv AT_* line:\n\n{stdout}\n\n"
    );
    let pagesz_line = stdout
        .lines()
        .find(|line| line.starts_with("AT_PAGESZ"))
        .unwrap_or_else(|| panic!("Expected AT_PAGESZ in pauxv output:\n\n{stdout}\n\n"));
    let pagesz_hex = pagesz_line
        .split_whitespace()
        .nth(1)
        .unwrap_or_else(|| panic!("Expected AT_PAGESZ value in line '{pagesz_line}'"));
    let pagesz = u64::from_str_radix(pagesz_hex.trim_start_matches("0x"), 16)
        .unwrap_or_else(|_| panic!("Expected AT_PAGESZ hex value, got '{pagesz_hex}'"));
    let allowed_page_sizes = [0x1000_u64, 0x4000_u64, 0x10000_u64];
    assert!(
        allowed_page_sizes.contains(&pagesz),
        "Unexpected AT_PAGESZ value 0x{pagesz:x}; expected one of {allowed_page_sizes:?}"
    );

    // AT_EXECFN should show the dereferenced string (an absolute path) when
    // process_vm_readv is permitted.  On Ubuntu (yama ptrace_scope=1) the tool
    // process is a sibling, not a parent, of the target so the read is denied
    // and only the hex address is printed.
    let execfn_line = stdout
        .lines()
        .find(|line| line.starts_with("AT_EXECFN"))
        .unwrap_or_else(|| panic!("Expected AT_EXECFN in pauxv output:\n\n{stdout}\n\n"));
    let execfn_tokens: Vec<&str> = execfn_line.split_whitespace().collect();
    if execfn_tokens.len() >= 3 {
        assert!(
            execfn_tokens[2].starts_with('/'),
            "AT_EXECFN string should be an absolute path, got '{}' in '{}'",
            execfn_tokens[2],
            execfn_line
        );
    }

    // AT_PLATFORM: same caveat -- only validate the string when present.
    let platform_line = stdout.lines().find(|line| line.starts_with("AT_PLATFORM "));
    if let Some(platform_line) = platform_line {
        let platform_tokens: Vec<&str> = platform_line.split_whitespace().collect();
        if platform_tokens.len() >= 3 {
            assert!(
                !platform_tokens[2].starts_with("0x"),
                "AT_PLATFORM string should not be a hex address, got '{}' in '{}'",
                platform_tokens[2],
                platform_line
            );
        }
    }
}

#[test]
fn pargs_x_alias_matches_pauxv_output() {
    let pauxv_output = common::run_ptool("pauxv", &[], "examples/pargs_penv", &[], &[], false);
    let pauxv_stdout = common::assert_success_and_get_stdout(pauxv_output);

    let pargs_output = common::run_ptool("pargs", &["-x"], "examples/pargs_penv", &[], &[], false);
    let pargs_stdout = common::assert_success_and_get_stdout(pargs_output);

    // Both should contain AT_PAGESZ
    assert!(
        pauxv_stdout.contains("AT_PAGESZ"),
        "pauxv should contain AT_PAGESZ:\n\n{pauxv_stdout}\n\n"
    );
    assert!(
        pargs_stdout.contains("AT_PAGESZ"),
        "pargs -x should contain AT_PAGESZ:\n\n{pargs_stdout}\n\n"
    );

    // Both should have the same set of AT_* keys
    let pauxv_keys: Vec<&str> = pauxv_stdout
        .lines()
        .filter(|line| line.starts_with("AT_"))
        .map(|line| line.split_whitespace().next().unwrap())
        .collect();
    let pargs_keys: Vec<&str> = pargs_stdout
        .lines()
        .filter(|line| line.starts_with("AT_"))
        .map(|line| line.split_whitespace().next().unwrap())
        .collect();
    assert_eq!(
        pauxv_keys, pargs_keys,
        "pauxv and pargs -x should produce the same AT_* keys"
    );
}

#[test]
fn penv_reflects_runtime_setenv() {
    let output = common::run_ptool(
        "penv",
        &[],
        "examples/penv_setenv",
        &[],
        &[("PTOOLS_TEST_OVERWRITE_VAR", "before")],
        false,
    );
    let stdout = common::assert_success_and_get_stdout_allow_warnings(output);

    // PTOOLS_TEST_SETENV_VAR was added at runtime via setenv() and should
    // only be visible when reading the environ symbol (not /proc/pid/environ).
    assert!(
        stdout.contains("PTOOLS_TEST_SETENV_VAR=runtime_value"),
        "Runtime setenv variable not found in penv output:\n\n{stdout}\n\n"
    );

    // PTOOLS_TEST_OVERWRITE_VAR was passed as "before" at spawn time and then
    // overwritten to "after" via setenv().
    assert!(
        stdout.contains("PTOOLS_TEST_OVERWRITE_VAR=after"),
        "Overwritten env variable should show new value in penv output:\n\n{stdout}\n\n"
    );
    assert!(
        !stdout.contains("PTOOLS_TEST_OVERWRITE_VAR=before"),
        "Overwritten env variable should not show old value in penv output:\n\n{stdout}\n\n"
    );
}

#[test]
fn pargs_e_reflects_runtime_setenv() {
    let output = common::run_ptool(
        "pargs",
        &["-e"],
        "examples/penv_setenv",
        &[],
        &[("PTOOLS_TEST_OVERWRITE_VAR", "before")],
        false,
    );
    let stdout = common::assert_success_and_get_stdout_allow_warnings(output);

    assert!(
        stdout.contains("PTOOLS_TEST_SETENV_VAR=runtime_value"),
        "Runtime setenv variable not found in pargs -e output:\n\n{stdout}\n\n"
    );

    assert!(
        stdout.contains("PTOOLS_TEST_OVERWRITE_VAR=after"),
        "Overwritten env variable should show new value in pargs -e output:\n\n{stdout}\n\n"
    );
    assert!(
        !stdout.contains("PTOOLS_TEST_OVERWRITE_VAR=before"),
        "Overwritten env variable should not show old value in pargs -e output:\n\n{stdout}\n\n"
    );
}

#[test]
fn pargs_x_prints_auxv_entries() {
    let output = common::run_ptool("pargs", &["-x"], "examples/pargs_penv", &[], &[], false);
    let stdout = common::assert_success_and_get_stdout(output);

    assert!(
        !stdout.contains("argv["),
        "pargs -x should not print argv lines:\n\n{stdout}\n\n"
    );
    assert!(
        !stdout.contains("envp["),
        "pargs -x should not print env lines:\n\n{stdout}\n\n"
    );

    let mut saw_auxv_line = false;
    for line in stdout.lines() {
        if !line.starts_with("AT_") {
            continue;
        }
        saw_auxv_line = true;
        let mut parts = line.split_whitespace();
        let _key = parts.next().unwrap();
        let value = parts.next().unwrap_or("");
        assert!(
            value.starts_with("0x") && (value.len() == 10 || value.len() == 18),
            "Expected auxv value to be fixed-width hex, got '{value}' in line '{line}'"
        );
    }

    assert!(
        saw_auxv_line,
        "Expected at least one auxv AT_* line:\n\n{stdout}\n\n"
    );
    let pagesz_line = stdout
        .lines()
        .find(|line| line.starts_with("AT_PAGESZ"))
        .unwrap_or_else(|| panic!("Expected AT_PAGESZ in pargs -x output:\n\n{stdout}\n\n"));
    let pagesz_hex = pagesz_line
        .split_whitespace()
        .nth(1)
        .unwrap_or_else(|| panic!("Expected AT_PAGESZ value in line '{pagesz_line}'"));
    let pagesz = u64::from_str_radix(pagesz_hex.trim_start_matches("0x"), 16)
        .unwrap_or_else(|_| panic!("Expected AT_PAGESZ hex value, got '{pagesz_hex}'"));
    let allowed_page_sizes = [0x1000_u64, 0x4000_u64, 0x10000_u64];
    assert!(
        allowed_page_sizes.contains(&pagesz),
        "Unexpected AT_PAGESZ value 0x{pagesz:x}; expected one of {allowed_page_sizes:?}"
    );

    // AT_EXECFN should show the dereferenced string (an absolute path) when
    // process_vm_readv is permitted.  On Ubuntu (yama ptrace_scope=1) the tool
    // process is a sibling, not a parent, of the target so the read is denied
    // and only the hex address is printed.
    let execfn_line = stdout
        .lines()
        .find(|line| line.starts_with("AT_EXECFN"))
        .unwrap_or_else(|| panic!("Expected AT_EXECFN in pargs -x output:\n\n{stdout}\n\n"));
    let execfn_tokens: Vec<&str> = execfn_line.split_whitespace().collect();
    if execfn_tokens.len() >= 3 {
        assert!(
            execfn_tokens[2].starts_with('/'),
            "AT_EXECFN string should be an absolute path, got '{}' in '{}'",
            execfn_tokens[2],
            execfn_line
        );
    }

    // AT_PLATFORM: same caveat -- only validate the string when present.
    let platform_line = stdout.lines().find(|line| line.starts_with("AT_PLATFORM "));
    if let Some(platform_line) = platform_line {
        let platform_tokens: Vec<&str> = platform_line.split_whitespace().collect();
        if platform_tokens.len() >= 3 {
            assert!(
                !platform_tokens[2].starts_with("0x"),
                "AT_PLATFORM string should not be a hex address, got '{}' in '{}'",
                platform_tokens[2],
                platform_line
            );
        }
    }
}