Skip to content

Commit

Permalink
local variables working
Browse files Browse the repository at this point in the history
  • Loading branch information
pm100 committed Jan 23, 2024
1 parent a7ce6e4 commit 266847f
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 27 deletions.
44 changes: 43 additions & 1 deletion src/db/debugdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, bail, Result};
use evalexpr::Value;
//pub const NO_PARAMS: = [];
use crate::db::util::Extract;
use crate::debugger::debugger::{SegChunk, Segment, Symbol, SymbolType};
use crate::debugger::debugger::{HLSym, SegChunk, Segment, Symbol, SymbolType};
use rusqlite::{
params,
types::{Null, Value as SqlValue},
Expand Down Expand Up @@ -503,4 +503,46 @@ impl DebugData {
}
Ok(si)
}

pub fn find_scope(&self, seg: i64, addr: u16) -> Result<Option<i64>> {
let sql = "select span.scope from span where span.scope not null and span.seg=?1 and span.start <= ?2 and span.start + span.size > ?2";
let mut stmt = self.conn.prepare_cached(sql)?;
match stmt.query_row(params![seg, addr], |row| {
let id = row.get::<usize, i64>(0)?;
Ok(id)
}) {
Ok(id) => Ok(Some(id)),
Err(e) => match e {
rusqlite::Error::QueryReturnedNoRows => Ok(None),
_ => Err(e.into()),
},
}
}
pub fn find_csym(&self, name: &str, scope: i64) -> Result<Option<HLSym>> {
let sql = "select scope, sc,sym,offset from csymbol where csymbol.scope =?1 and name = ?2";
let mut stmt = self.conn.prepare_cached(sql)?;
match stmt.query_row(params![scope, name], |row| {
// type integer,
// sc text,
// sym integer,
// offset integer
let scope = row.get::<usize, i64>(0)?;
let sc = row.get::<usize, String>(1)?;
let sym = row.get::<usize, i64>(2)?;
let offset = row.get::<usize, i64>(3)?;
Ok(HLSym {
name: name.to_string(),
type_: sc,
scope,
seg: 0,
value: offset,
})
}) {
Ok(id) => Ok(Some(id)),
Err(e) => match e {
rusqlite::Error::QueryReturnedNoRows => Ok(None),
_ => Err(e.into()),
},
}
}
}
59 changes: 44 additions & 15 deletions src/db/parsedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ impl DebugData {
scount += 1;
tx.execute(
"insert into scope
(id, name, module, type, size, parent, sym, span)
values (?1,?2,?3,?4,?5,?6,?7,?8)",
(id, name, module, type, size, parent, sym)
values (?1,?2,?3,?4,?5,?6,?7)",
params![
scope.id,
scope.name,
Expand All @@ -249,13 +249,21 @@ impl DebugData {
scope.size,
scope.parent,
scope.sym,
if !scope.span.is_empty() {
scope.span[0]
} else {
-1
}
// if !scope.span.is_empty() {
// scope.span[0]
// } else {
// -1
// }
],
)?;
for span in scope.span {
tx.execute(
"
insert into span (id, scope) values(?1, ?2)
on conflict(id) do update set scope = ?2",
params![span, scope.id],
)?;
}
}
"sym\tid" => {
let sym = Self::parse_sym(record)?;
Expand Down Expand Up @@ -316,13 +324,14 @@ impl DebugData {
let duration = end.duration_since(start).unwrap();
println!("it took {} milli seconds", duration.as_millis());
tx.commit()?;
//self.merge_csymbols(symcount)?;
self.merge_csymbols(symcount)?;
Ok(())
}
fn merge_csymbols(&mut self, mut symcount: i64) -> Result<()> {
let sql = "select * from csymbol";
let rows = self.query_db(params![], sql)?;
let tx = self.conn.transaction()?;
let mut statics = Vec::new();

for row in rows {
let id = row[0].vto_i64()?;
let name = row[1].vto_string()?;
Expand All @@ -349,16 +358,36 @@ impl DebugData {
// seg integer,
// size integer,
// parent integer,
symcount += 1;
let sql = "insert into symdef (id,name, scope, def, type, exp, val, seg, size, parent, addrsize) values (?1,?2,?3,?4,?5,?6,?7,?8,?9, ?10,?11)";
tx.execute(
sql,
params![symcount, name, scope, sym, type_, 0, 0, 0, 0, 0, "static"],
)?;
// symcount += 1;
// let sql = "insert into symdef (id,name, scope, def, type, exp, val, seg, size, parent, addrsize) values (?1,?2,?3,?4,?5,?6,?7,?8,?9, ?10,?11)";
// tx.execute(
// sql,
// params![symcount, name, scope, sym, type_, 0, 0, 0, 0, 0, "static"],
// )?;
let row = self
.conn
.prepare_cached("select val,size,def from symdef where id = ?1")?
.query_row(params![sym], |row| {
Ok((
row.get::<usize, i64>(0)?,
row.get::<usize, i64>(1)?,
row.get::<usize, i64>(2)?,
))
})?;
statics.push((row.0, row.1, row.2, name, scope, type_));
}
_ => bail!("bad sc: {}", sc),
}
}
let tx = self.conn.transaction()?;
for st in statics {
symcount += 1;
let sql = "insert into symdef (id,name, scope, def, type, exp, val, seg, size, parent, addrsize) values (?1,?2,?3,?4,?5,?6,?7,?8,?9, ?10,?11)";
tx.execute(
sql,
params![symcount, st.3, st.4, st.2, st.5, 0, st.0, 0, st.1, 0, "static"],
)?;
}
tx.commit()?;
Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions src/db/setupdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl DebugData {
seg integer,
size integer,
parent integer
)",
Expand Down Expand Up @@ -111,8 +112,8 @@ id integer primary key,
type integer,
size integer,
parent integer,
sym integer,
span integer
sym integer
)",
[],
)?;
Expand Down Expand Up @@ -159,6 +160,7 @@ absaddr integer
left join line on symdef.def = line.id
left join file on line.file = file.id
left join module on file.id = module.file
",[],)?;

Expand Down
56 changes: 54 additions & 2 deletions src/debugger/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ pub enum SourceDebugMode {
Step,
}

pub struct HLSym {
pub name: String,
pub value: i64,
pub type_: String,
pub seg: u8,
pub scope: i64,
}
pub enum DebugMode {
Unknown,
Source,
Expand All @@ -46,6 +53,7 @@ pub struct CodeLocation {
pub seg: u8,
pub offset: u16,
pub absaddr: u16,
pub scope: Option<i64>,
}
pub enum SymbolType {
Unknown,
Expand Down Expand Up @@ -458,7 +466,9 @@ impl Debugger {
if addr_str.chars().next().unwrap().is_ascii_digit() {
return Ok((addr_str.parse::<u16>()?, String::new()));
}

if let Some(caddr) = self.find_csym_address(addr_str)? {
return Ok((caddr, addr_str.to_string()));
}
let syms = self.dbgdb.get_symbol(addr_str)?;
match syms.len() {
0 => bail!("Symbol '{}' not found", addr_str),
Expand Down Expand Up @@ -555,7 +565,9 @@ impl Debugger {
}
Some(&seg.modules[current_mod])
}

pub fn find_csym(&self, name: &str, scope: i64) -> Result<Option<HLSym>> {
self.dbgdb.find_csym(name, scope)
}
pub fn where_are_we(&self, addr: u16) -> Result<CodeLocation> {
let mut location = CodeLocation {
module: None,
Expand All @@ -567,6 +579,7 @@ impl Debugger {
seg: 0,
offset: 0,
absaddr: addr,
scope: None,
};
// do we have any debug data at all?
if self.seg_list.is_empty() {
Expand Down Expand Up @@ -606,6 +619,9 @@ impl Debugger {
location.seg = seg.id;
location.module = Some(seg.modules[current_mod].module);

// find scope
location.scope = self.dbgdb.find_scope(seg.id as i64, rel_addr)?;

// now find the assembly line
if let Some(aline) = self.dbgdb.find_assembly_line(addr)? {
location.aline = aline.line_no;
Expand Down Expand Up @@ -645,4 +661,40 @@ impl Debugger {
}
})
}
pub fn find_csym_address(&self, name: &str) -> Result<Option<u16>> {
let addr = self.read_pc();
let waw = self.where_are_we(addr)?;
if let Some(scope) = waw.scope {
if let Some(csym) = self.dbgdb.find_csym(name, scope)? {
match csym.type_.as_str() {
"auto" => {
//let stack = self.debugger.read_stack();
let mut sp65 = 0;
for i in (0..self.stack_frames.len()).rev() {
if let FrameType::Jsr(jsr) = &self.stack_frames[i].frame_type {
sp65 = jsr.sp65;
if i == 0 {
// the call main stack frame is out by 4 (argc,argv)
sp65 -= 4;
}
break;
}
}
return Ok(Some((sp65 as i64 + csym.value) as u16));
}
"reg" => {
let regbank = self.dbgdb.get_symbol("zeropage.regbank")?;
if regbank.len() == 1 {
return Ok(Some((regbank[0].1 as i64 + csym.value) as u16));
}
}
"static" => {}
_ => {
return Ok(None);
}
}
};
}
Ok(None)
}
}
10 changes: 5 additions & 5 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ the expr command evaluates an expression and prints the result
so you can do
dis =pc
mem =xr+0x20
mem .ptr // no need for expression, symbols just work anyway
mem =@(.ptr) // deference a pointer
mem '=@(.ptr + 0x20)' // do math on a pointer
mem =@(.ptr + (0x20*xr)) // more math
mem =.xr+0x20
mem ptr // no need for expression, symbols just work anyway
mem =@(ptr) // deference a pointer
mem '=@(ptr + 0x20)' // do math on a pointer
mem =@(ptr + (0x20*xr)) // more math
Expand Down
7 changes: 5 additions & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl Shell {
let symt = match symbol.sym_type {
SymbolType::Equate => "equ",
SymbolType::Label => "lab",
SymbolType::CSymbol => "c",
_ => "???",
};
println!(
Expand Down Expand Up @@ -292,6 +293,7 @@ impl Shell {
}
Some(("print", args)) => {
let addr_str = args.get_one::<String>("address").unwrap();

let addr_str = self.expand_expr(addr_str)?;
let (addr, _) = self.debugger.convert_addr(&addr_str)?;
self.print(addr, args)?;
Expand Down Expand Up @@ -543,10 +545,11 @@ impl Shell {
if let Some(cf) = self.waw.cfile {
let file_name = self.debugger.lookup_file_by_id(cf).unwrap();
println!(
"{}:{}\t\t{}",
"{}:{}\t\t{} {}",
file_name.short_name,
self.waw.cline,
self.waw.ctext.as_ref().unwrap()
self.waw.ctext.as_ref().unwrap(),
self.waw.scope.unwrap_or(0)
);
} else {
// disassemble the current instruction
Expand Down

0 comments on commit 266847f

Please sign in to comment.