Skip to content

Commit 7d6b715

Browse files
committed
add --wait switch
1 parent 7acef95 commit 7d6b715

4 files changed

Lines changed: 55 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "windbgmsg"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2024"
55

66
[dependencies]

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,31 @@ This is a Rust console application that reads a process name from the user via t
1010
```
1111
2. Run the project with the process name as an argument (optional):
1212
```pwsh
13-
cargo run -- <process_name>
13+
cargo run -- <process_name> [--wait]
1414
```
1515
Replace `<process_name>` with the name of the executable you want to monitor (e.g., `notepad.exe`).
16-
If you omit the argument, debug output from all processes will be captured:
17-
```pwsh
18-
cargo run --
19-
```
16+
- If you omit the argument, debug output from all processes will be captured:
17+
```pwsh
18+
cargo run --
19+
```
20+
- You can add the `--wait` switch to wait for the process to appear if it is not running yet:
21+
```pwsh
22+
cargo run -- notepad.exe --wait
23+
```
24+
The application will wait until the process starts, then attach and capture debug output.
25+
- If you use `--wait` without specifying a process name, the application will print an error and exit.
2026
2127
## Features
2228
- Finds the process ID by name (case-insensitive)
29+
- Optionally waits for the process to appear using the `--wait` switch
2330
- Captures and prints debug output from the target process, or from all processes if no name is given
2431
- Returns Windows error codes on failure for easier troubleshooting
2532
2633
## Examples
2734
```pwsh
28-
cargo run -- notepad.exe # Capture output from notepad.exe only
29-
cargo run -- # Capture output from all processes
35+
cargo run -- notepad.exe # Capture output from notepad.exe only
36+
cargo run -- notepad.exe --wait # Wait for notepad.exe to start, then capture output
37+
cargo run -- # Capture output from all processes
3038
```
3139

3240
If the process is not found or a Windows API call fails, an error message and the error code will be displayed.

src/main.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,45 @@ use winproc::find_process_id_by_name;
88

99
use crate::winproc::capture_debug_output;
1010

11-
fn get_app_name_from_args() -> Option<String> {
11+
struct AppArgs {
12+
app_name: Option<String>,
13+
wait: bool,
14+
}
15+
16+
fn get_args() -> AppArgs {
17+
let mut app_name = None;
18+
let mut wait = false;
1219
let mut args = env::args();
1320
args.next(); // skip program name
14-
args.next()
21+
for arg in args {
22+
if arg == "--wait" {
23+
wait = true;
24+
} else if app_name.is_none() {
25+
app_name = Some(arg);
26+
}
27+
}
28+
AppArgs { app_name, wait }
1529
}
1630

1731
fn main() {
18-
match get_app_name_from_args() {
19-
Some(app_name) => match find_process_id_by_name(&app_name) {
32+
let args = get_args();
33+
match (args.app_name, args.wait) {
34+
(Some(app_name), true) => {
35+
// Wait for process to appear
36+
let pid = loop {
37+
if let Some(pid) = find_process_id_by_name(&app_name) {
38+
break pid;
39+
}
40+
std::thread::sleep(std::time::Duration::from_secs(1));
41+
};
42+
println!("Process ID: {}", pid);
43+
if let Err(e) = capture_debug_output(Some(pid)) {
44+
eprintln!("Error capturing debug output: {}", e);
45+
process::exit(1);
46+
}
47+
}
48+
(Some(app_name), false) => match find_process_id_by_name(&app_name) {
2049
Some(pid) => {
21-
// Print the process ID and capture debug output
2250
println!("Process ID: {}", pid);
2351
if let Err(e) = capture_debug_output(Some(pid)) {
2452
eprintln!("Error capturing debug output: {}", e);
@@ -30,7 +58,11 @@ fn main() {
3058
process::exit(1);
3159
}
3260
},
33-
None => {
61+
(None, true) => {
62+
eprintln!("Error: --wait switch requires an app name.");
63+
process::exit(1);
64+
}
65+
(None, false) => {
3466
println!("No app name provided. Capturing debug output from all processes.");
3567
if let Err(e) = capture_debug_output(None) {
3668
eprintln!("Error capturing debug output: {}", e);

0 commit comments

Comments
 (0)