sys-rs 0.1.1

ptrace-based Linux system tool reimplementations: strace, gcov, addr2line, debugger
Documentation
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
use std::collections::BTreeMap;

use crate::{
    diag::{Error, Result},
    handler::{self, CommandFn},
    param::{Extend, Join, Type, Value},
    progress::State,
};

#[derive(PartialEq, Debug)]
enum Status {
    Handled,
    NotHandled,
}

enum Node {
    Command(CommandFn, Vec<Type>),
    Subcommands(Registry),
    Alias(&'static str),
    HelpCommand,
}

impl Node {
    fn command_no_params(f: CommandFn) -> Self {
        Node::Command(f, vec![])
    }
}

/// Command registry describing the available REPL commands and their
/// parameter signatures.
///
/// The `Registry` stores command nodes (concrete commands, subcommand groups
/// and aliases) in a deterministic `BTreeMap` so help/usage output is stable.
pub struct Registry {
    // Using a BTreeMap to have a deterministic order for display purposes
    nodes: BTreeMap<&'static str, Vec<Node>>,
}

impl Registry {
    fn new() -> Self {
        Self {
            nodes: BTreeMap::new(),
        }
    }

    fn register(mut self, name: &'static str, node: Node) -> Self {
        self.nodes.entry(name).or_default().push(node);
        self
    }

    fn alias(mut self, alias: &'static str, target: &'static str) -> Self {
        self.nodes
            .entry(alias)
            .or_default()
            .push(Node::Alias(target));
        self
    }

    fn walk<'a, F>(&'a self, prefix: &mut Vec<&'a str>, f: &mut F)
    where
        F: FnMut(&[&'a str], Option<&'a [Type]>),
    {
        for (name, nodes) in &self.nodes {
            if nodes.iter().all(|n| matches!(n, Node::Alias(_))) {
                continue;
            }

            if !name.is_empty() {
                prefix.push(name);
            }

            for entry in nodes {
                match entry {
                    Node::Command(_, param_types) => f(prefix, Some(param_types)),
                    Node::Subcommands(sub) => sub.walk(prefix, f),
                    Node::Alias(_) => {}
                    Node::HelpCommand => f(&[*name], None),
                }
            }

            if !name.is_empty() {
                prefix.pop();
            }
        }
    }

    fn collect<F>(&self, mut f: F)
    where
        F: FnMut(&[&str], Option<&[Type]>),
    {
        let mut prefix = Vec::new();
        self.walk(&mut prefix, &mut f);
    }

    #[must_use]
    /// Return a list of concrete command spellings suitable for completion.
    ///
    /// Each entry is a space-separated command spelling (including parent
    /// subcommands). The results are deduplicated and are intended to be used
    /// by the REPL completion helper.
    ///
    /// # Returns
    ///
    /// A deduplicated `Vec<String>` containing space-separated command
    /// spellings (including parent subcommands) suitable for use by the
    /// REPL completion helper.
    pub fn completions(&self) -> Vec<String> {
        let mut out = Vec::new();

        self.collect(|names, _| {
            out.push(names.join(" "));
        });

        out.dedup();
        out
    }

    #[must_use]
    /// Return human-friendly usage lines for every command.
    ///
    /// Each usage entry contains the full command spelling (including parent
    /// subcommands) and the parameter signature (e.g. `breakpoint <address>`).
    /// This is suitable for printing when the user requests help.
    ///
    /// # Returns
    ///
    /// A `Vec<String>` where each entry contains a command spelling and its
    /// parameter signature (for example `breakpoint <address>`), intended
    /// for help output.
    pub fn usages(&self) -> Vec<String> {
        let mut out = Vec::new();

        self.collect(|names, param_types| {
            let mut usage = names.join(" ");
            if let Some(params) = param_types {
                if !params.is_empty() {
                    let params_str = params
                        .iter()
                        .map(std::string::ToString::to_string)
                        .collect::<Vec<_>>()
                        .join(" ");
                    usage.push(' ');
                    usage.push_str(&params_str);
                }
            }

            out.push(usage);
        });

        out
    }

    fn parse_params<'a>(
        param_types: &[Type],
        params: &'a [&'a str],
    ) -> Vec<Value<'a>> {
        param_types
            .iter()
            .enumerate()
            .filter_map(|(i, param_type)| Value::new(param_type, params[i]).ok())
            .collect()
    }

    fn handle_command(
        handler: CommandFn,
        param_types: &[Type],
        rest: &[&str],
        state: &mut State,
    ) -> Result<Status> {
        let mut status = Status::NotHandled;

        let num_params = param_types.len();
        if rest.len() == num_params {
            let parsed = Self::parse_params(param_types, rest);
            if parsed.len() == num_params {
                handler(&parsed, state)?;
                status = Status::Handled;
            }
        }

        Ok(status)
    }

    fn handle_subcommands(
        registry: &Registry,
        path: &[Value],
        first: &str,
        rest: &[&str],
        state: &mut State,
    ) -> Result<()> {
        let path = path.extend(first, &[]);
        if rest.is_empty() {
            handler::do_ambiguous(&path, state)
        } else {
            registry.dispatch(rest, &path, state)
        }
    }

    fn handle_help(
        &self,
        path: &[Value],
        first: &str,
        rest: &[&str],
        state: &mut State,
    ) -> Result<()> {
        if rest.is_empty() {
            let commands = self.usages();
            let commands: Vec<Value> =
                commands.iter().map(|s| Value::String(s)).collect();
            handler::do_help(&commands, state)
        } else {
            let path = path.extend(first, rest);
            handler::do_unknown(&path, state)
        }
    }

    fn handle_no_entry(
        &self,
        path: &[Value],
        first: &str,
        rest: &[&str],
        state: &mut State,
    ) -> Result<()> {
        let matches: Vec<_> = self
            .nodes
            .keys()
            .filter(|name| name.starts_with(first))
            .collect();

        match matches.len() {
            1 => {
                let mut args = vec![*matches[0]];
                args.extend_from_slice(rest);
                self.dispatch(&args, path, state)
            }
            n if n > 1 => {
                let path = path.extend(first, &[]);
                handler::do_ambiguous(&path, state)
            }
            _ => {
                let path = path.extend(first, rest);
                handler::do_unknown(&path, state)
            }
        }
    }

    fn handle_node(
        &self,
        node: &Node,
        path: &[Value],
        first: &str,
        rest: &[&str],
        state: &mut State,
    ) -> Result<bool> {
        match node {
            Node::Command(handler, param_types) => {
                match Self::handle_command(*handler, param_types, rest, state)? {
                    Status::Handled => Ok(true),
                    Status::NotHandled => Ok(false),
                }
            }
            Node::Subcommands(registry) => {
                let res =
                    Self::handle_subcommands(registry, path, first, rest, state);
                Ok(res.is_ok())
            }
            Node::Alias(target) => {
                let mut new_args = vec![*target];
                new_args.extend_from_slice(rest);
                let res = self.dispatch(&new_args, path, state);
                Ok(res.is_ok())
            }
            Node::HelpCommand => {
                let res = self.handle_help(path, first, rest, state);
                Ok(res.is_ok())
            }
        }
    }

    fn dispatch(
        &self,
        args: &[&str],
        path: &[Value],
        state: &mut State,
    ) -> Result<()> {
        match args.split_first() {
            Some((first, rest)) => match self.nodes.get(*first) {
                Some(nodes) => {
                    let handled =
                        nodes.iter().try_fold(false, |handled, node| {
                            Ok::<bool, Error>(
                                handled
                                    | self.handle_node(
                                        node, path, first, rest, state,
                                    )?,
                            )
                        })?;
                    if !handled {
                        let path = path.extend(first, &[]);
                        let args = format!("{}: {}", path.join(" "), rest.join(" "));
                        handler::do_invalid_arguments(
                            &[Value::String(&args)],
                            state,
                        )?;
                    }
                    Ok(())
                }
                None => self.handle_no_entry(path, first, rest, state),
            },
            None => handler::do_nothing(&[], state),
        }
    }

    /// Parse `input` and dispatch the corresponding command handler.
    ///
    /// # Arguments
    ///
    /// * `input` - The raw user input line (e.g. `breakpoint 0x400123`).
    /// * `state` - Mutable reference to runtime `State` used by handlers.
    ///
    /// # Errors
    ///
    /// Returns an error if dispatching a command fails; handlers return
    /// `Result` which is propagated to the caller.
    pub fn run(&self, input: &str, state: &mut State) -> Result<()> {
        let args: Vec<&str> = input.split_whitespace().collect();
        self.dispatch(&args, &[], state)?;
        Ok(())
    }
}

impl Default for Registry {
    fn default() -> Self {
        let info_registry = Registry::new()
            .register(
                "breakpoints",
                Node::command_no_params(handler::do_info_breakpoints),
            )
            .register("memory", Node::command_no_params(handler::do_info_memory))
            .register(
                "registers",
                Node::command_no_params(handler::do_info_registers),
            );

        let layout_registry = Registry::new()
            .register("asm", Node::command_no_params(handler::do_layout_asm))
            .register("src", Node::command_no_params(handler::do_layout_src));

        Registry::new()
            .register("backtrace", Node::command_no_params(handler::do_backtrace))
            .alias("bt", "backtrace")
            .register(
                "breakpoint",
                Node::command_no_params(handler::do_breakpoint),
            )
            .register(
                "breakpoint",
                Node::Command(handler::do_breakpoint, vec![Type::Address]),
            )
            .alias("b", "breakpoint") // Remove ambiguity with backtrace
            .register("continue", Node::command_no_params(handler::do_continue))
            .register("delete", Node::Command(handler::do_delete, vec![Type::Id]))
            .register(
                "examine",
                Node::Command(
                    handler::do_examine,
                    vec![Type::Format, Type::Size, Type::Address],
                ),
            )
            .alias("x", "examine")
            .register("help", Node::HelpCommand)
            .register("info", Node::Subcommands(info_registry))
            .register("layout", Node::Subcommands(layout_registry))
            .register("list", Node::command_no_params(handler::do_list))
            .alias("l", "list") // Remove ambiguity with layout
            .register("quit", Node::command_no_params(handler::do_quit))
            .register("step", Node::command_no_params(handler::do_step))
            .register("next", Node::command_no_params(handler::do_next))
            .register(
                "tbreakpoint",
                Node::command_no_params(handler::do_tbreakpoint),
            )
            .register(
                "tbreakpoint",
                Node::Command(handler::do_tbreakpoint, vec![Type::Address]),
            )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use nix::unistd::Pid;

    use crate::{
        param::{Type, Value},
        progress::{Execution, State},
    };

    #[test]
    fn test_registry_completions_and_usages() {
        let reg = Registry::new()
            .register("foo", Node::command_no_params(handler::do_nothing))
            .register(
                "bar",
                Node::Command(handler::do_nothing, vec![Type::Address, Type::Id]),
            );

        let comps = reg.completions();
        assert!(comps.contains(&"foo".to_string()));
        assert!(comps.contains(&"bar".to_string()));

        let usages = reg.usages();
        assert!(usages.iter().any(|s| s.starts_with("bar ")));
    }

    #[test]
    fn test_handle_command_param_matching() {
        fn my_handler(_args: &[Value], _state: &mut State) -> Result<()> {
            Ok(())
        }

        let mut state = State::new(Pid::from_raw(1), None);
        let res = Registry::handle_command(
            my_handler,
            &[Type::Address],
            &["0x100"],
            &mut state,
        )
        .expect("handle_command failed");
        assert_eq!(res, Status::Handled);

        let res = Registry::handle_command(
            my_handler,
            &[Type::Address, Type::Id],
            &["0x100"],
            &mut state,
        )
        .expect("handle_command failed");
        assert_eq!(res, Status::NotHandled);
    }

    #[test]
    fn test_handle_no_entry_ambiguous() {
        let reg = Registry::new()
            .register("foo", Node::command_no_params(handler::do_nothing))
            .register("fop", Node::command_no_params(handler::do_nothing));

        let mut state = State::new(Pid::from_raw(1), None);
        reg.dispatch(&["fo"], &[], &mut state)
            .expect("dispatch failed");
        assert!(matches!(state.execution(), Execution::Skip));
    }

    #[test]
    fn test_alias_dispatch() {
        let reg = Registry::new()
            .register("target", Node::command_no_params(handler::do_nothing))
            .alias("a", "target");

        let mut state = State::new(Pid::from_raw(1), None);
        reg.dispatch(&["a"], &[], &mut state)
            .expect("dispatch failed");
        assert!(matches!(state.execution(), Execution::Skip));
    }

    #[test]
    fn test_handle_help_and_run() {
        let reg = Registry::new().register("help", Node::HelpCommand);

        let mut state = State::new(Pid::from_raw(1), None);
        reg.dispatch(&["help"], &[], &mut state)
            .expect("dispatch failed");
        assert!(matches!(state.execution(), Execution::Skip));

        let mut state2 = State::new(Pid::from_raw(1), None);
        reg.run("help", &mut state2).expect("run failed");
        assert!(matches!(state2.execution(), Execution::Skip));
    }
}