Improve reliablitity of psxreadmap

This commit is contained in:
Jaby Blubb 2023-08-27 02:43:01 +02:00
parent 6c181cd3d2
commit 6ff067587d
4 changed files with 61 additions and 28 deletions

View File

@ -67,36 +67,43 @@ fn scan_symbols<F: FnMut() -> Option<String>>(next_line: &mut F, memory_map: &mu
}
while let Some(line) = next_line() {
fn read_symbol(line: String) -> Result<Option<(Symbol, String)>, Error> {
let split_count = line.split_whitespace().count();
let mut split_line = line.split_whitespace();
let adr = split_line_radix(&mut split_line, 16, "Address of Symbol")?;
let _type = split_line.next().ok_or(Error::from_str("Failed obtaining type of symbol"))?;
if split_count > 5 {
let _flag = split_line.next().ok_or(Error::from_str("Failed obtaining flag of symbol"))?;
}
let section = split_line.next().ok_or(Error::from_str("Failed obtaining section of symbol"))?;
let size = split_line_radix(&mut split_line, 16, "Size of Symbol")? as usize;
let name = split_line.next().ok_or(Error::from_str("Failed obtaining name of symbol"))?;
if skip_symbol(name, section) {
// Not of interest
Ok(None)
}
else {
Ok(Some((Symbol::new(name.to_string(), adr, size), section.to_owned())))
}
}
if line.chars().all(char::is_whitespace) {
return Ok(line);
}
let split_count = line.split_whitespace().count();
let mut split_line = line.split_whitespace();
let adr = split_line_radix(&mut split_line, 16, "Address of Symbol")?;
let _type = split_line.next().ok_or(Error::from_str("Failed obtaining type of symbol"))?;
if split_count > 5 {
let _flag = split_line.next().ok_or(Error::from_str("Failed obtaining flag of symbol"))?;
}
let section = split_line.next().ok_or(Error::from_str("Failed obtaining section of symbol"))?;
let size = split_line_radix(&mut split_line, 16, "Size of Symbol")? as usize;
let name = split_line.next().ok_or(Error::from_str("Failed obtaining name of symbol"))?;
if skip_symbol(name, section) {
// Not of interest
continue;
}
let symbol = Symbol::new(name.to_string(), adr, size);
if let Some(section) = find_section(section, memory_map) {
if section.contains_adr(adr) {
section.symbols.push(symbol);
// Do not add to global cause they have a section
continue;
if let Ok(Some((symbol, section))) = read_symbol(line) {
if let Some(section) = find_section(section.as_str(), memory_map) {
if section.contains_adr(symbol.adr) {
section.symbols.push(symbol);
// Do not add to global cause they have a section
continue;
}
}
}
memory_map.global.push(symbol);
memory_map.global.push(symbol);
}
}
Err(Error::from_str("Failed obtaining all Symbols"))
@ -124,8 +131,9 @@ fn scan_sections<F: FnMut() -> Option<String>>(next_line: &mut F, sections: &mut
}
fn split_line_radix<'a>(split_line: &mut std::str::SplitWhitespace<'a>, radix: u32, value_name: &str) -> Result<u64, Error> {
match u64::from_str_radix(split_line.next().ok_or(Error::from_text(format!("Failed reading: {}", value_name)))?, radix) {
let text = split_line.next().ok_or(Error::from_text(format!("Failed reading: {}", value_name)))?;
match u64::from_str_radix(text, radix) {
Ok(value) => Ok(value),
Err(error) => Err(Error::from_text(format!("Converting value for {} failed with: {}", value_name, error)))
Err(error) => Err(Error::from_text(format!("Converting value ({}) for {} failed with: {}", text, value_name, error)))
}
}

View File

@ -10,6 +10,8 @@ use ratatui::{
widgets::{Block, Borders, BorderType, Gauge, List, ListItem, ListState, Paragraph, Tabs, block::Title}
};
pub use readmap_helper::get_tool_output;
pub type EventReceiver = std::sync::mpsc::Receiver<Event<crossterm::event::KeyEvent>>;
pub type Terminal = ratatui::Terminal<ratatui::backend::CrosstermBackend<std::io::Stdout>>;
type ConsoleFrame<'a> = ratatui::Frame<'a, ratatui::backend::CrosstermBackend<std::io::Stdout>>;

View File

@ -2,7 +2,7 @@ use clap::Parser;
use crossterm::{event::{self, Event as CEvent, KeyboardEnhancementFlags, KeyEventKind, PushKeyboardEnhancementFlags}, execute, terminal};
use psxreadmap::{ConsoleUI, Event, EventReceiver, Terminal, load_memory_map, UIState};
use std::{io::{self, Stdout}, path::PathBuf, sync::mpsc, thread, time::{Duration, Instant}};
use tool_helper::{Error, exit_with_error};
use tool_helper::{Error, exit_with_error, open_output};
use ratatui::backend::CrosstermBackend;
pub const RUN_DUMP_TOOL_IN_WSL:bool = cfg!(windows);
@ -14,6 +14,8 @@ struct CommandLine {
input: PathBuf,
#[clap(long="wsl", help="Run \"objdump\" in WSL", default_value_t=RUN_DUMP_TOOL_IN_WSL)]
use_wsl: bool,
#[clap(long="raw", default_value_t=false)]
raw_dump: bool,
#[clap(short='o', help="Output a memory map file with running the tool")]
output: Option<PathBuf>
}
@ -33,6 +35,11 @@ pub fn main() {
}
fn run_main(cmd: CommandLine) -> Result<(), Error> {
if cmd.raw_dump {
psxreadmap::get_tool_output(cmd.use_wsl, cmd.input, open_output(cmd.output)?)?;
return Ok(());
}
let memory_map = load_memory_map(cmd.use_wsl, cmd.input)?; dump_memory_map(cmd.output, &memory_map)?;
let rx = start_event_loop();
let terminal = setup_console()?;

View File

@ -1,4 +1,4 @@
use tool_helper::Error;
use tool_helper::{Error, Output};
use readmap::types::MemoryMap;
use std::{io::{BufRead, BufReader}, path::PathBuf, process::{Child, Command, Stdio}};
@ -17,6 +17,22 @@ pub fn generate_memory_map(use_wsl: bool, input: PathBuf) -> Result<MemoryMap, E
}
}
pub fn get_tool_output(use_wsl: bool, input: PathBuf, mut output: Output) -> Result<Output, Error> {
let mut child = run_objdump(use_wsl, input)?;
if let Some(stdout) = &mut child.stdout {
let line_iter = BufReader::new(stdout).lines().map(|l| l.unwrap()).into_iter();
for line in line_iter {
writeln!(output, "{}", line)?;
}
Ok(output)
}
else {
Err(Error::from_str("Failed opening \"stdout\" for objdump"))
}
}
fn run_objdump(use_wsl: bool, input: PathBuf) -> Result<Child, Error> {
let command_list = ["wsl", "objdump", "-x"];
let start_idx = {