Skip to content

Commit

Permalink
rename debugger.rs , added trap enable / disable
Browse files Browse the repository at this point in the history
  • Loading branch information
pm100 committed Jan 27, 2024
1 parent 4ffdff1 commit ce835a3
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 84 deletions.
22 changes: 22 additions & 0 deletions samples/heap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{

char *small_str;
char *big_str;
small_str = malloc(10);

// write off the end
strcpy(small_str, "Hello World!");

// invalid_free

free(small_str+1);

big_str = realloc(small_str, 20);
strcpy(big_str, "Hello World");

// leak
}
22 changes: 22 additions & 0 deletions samples/segviol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buff[10];
int main(int argc, char **argv)
{
int i ;
int *p;
// read random location
i = *(int *)0xdead;
// write to random location
*(int *)0xbeef = 0;

p = malloc(20);

// read unitialized data
i = p[4];

strcpy(buff, "Hello");
printf("buff=%s\n", buff);

}
2 changes: 1 addition & 1 deletion src/about.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use crate::log::say;


pub struct About {
db: HashMap<String, String>,
Expand Down
35 changes: 7 additions & 28 deletions src/db/debugdb.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{anyhow, bail, Result};
use evalexpr::Value;

//pub const NO_PARAMS: = [];
use crate::db::util::Extract;
use crate::debugger::debugger::{HLSym, SegChunk, Segment, Symbol, SymbolType};
use crate::debugger::core::{HLSym, SegChunk, Segment, Symbol, SymbolType};
use crate::log::say;
use rusqlite::{
params,
Expand Down Expand Up @@ -34,7 +34,7 @@ pub struct SourceFile {
}
pub struct DebugData {
pub conn: Connection,
name: String,

pub cc65_dir: Option<PathBuf>,
}

Expand All @@ -46,7 +46,6 @@ impl DebugData {
let mut ret = Self {
conn: Connection::open(name)?,
cc65_dir: None,
name: name.to_string(),
};
ret.create_tables()?;
Ok(ret)
Expand Down Expand Up @@ -116,26 +115,6 @@ impl DebugData {
}
Ok(v)
}
pub fn load_expr_symbols(&mut self, sym_tab: &mut HashMap<String, Value>) -> Result<()> {
let mut stmt = self
.conn
.prepare_cached("select name, val , module from symbol")?;
let rows = stmt.query_map([], |row| {
let name = row.get::<usize, String>(0)?;
let val = row.get::<usize, i64>(1)? as u16;

let module = row.get::<usize, Option<String>>(2)?;

Ok((name, val, module))
})?;
sym_tab.clear();
for row in rows {
let (name, val, _module) = row?;
sym_tab.insert(name, Value::Int(val as i64));
}

Ok(())
}

pub fn get_symbol(&self, name: &str) -> Result<Vec<(String, u16, String)>> {
let mut v = Vec::new();
Expand Down Expand Up @@ -207,10 +186,10 @@ impl DebugData {
let name = row[0].vto_string()?;
// let addr = row[1].vto_i64()?;
// let module = row[2].vto_string()?;
let scope = row[1].vto_i64()?;
let _scope = row[1].vto_i64()?;
let type_ = row[2].vto_string()?;
let val = row[3].vto_i64()? as u16;
let seg = row[4].vto_i64()? as u8;
let _seg = row[4].vto_i64()? as u8;
v.push(Symbol {
name,
value: val,
Expand Down Expand Up @@ -480,7 +459,7 @@ impl DebugData {
offset: start,
module: id as i32,
module_name: name,
size: size,
size,
});
} else {
bail!("bad segid {}", segid);
Expand Down Expand Up @@ -572,7 +551,7 @@ impl DebugData {
// offset integer
let scope = row.get::<usize, i64>(0)?;
let sc = row.get::<usize, String>(1)?;
let sym = row.get::<usize, i64>(2)?;
let _sym = row.get::<usize, i64>(2)?;
let offset = row.get::<usize, i64>(3)?;
Ok(HLSym {
name: name.to_string(),
Expand Down
17 changes: 7 additions & 10 deletions src/db/parsedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type StringRecord = Vec<String>;
//pub const NO_PARAMS: = [];
use crate::db::util::Extract;
use crate::log::say;
use crate::{db::debugdb::DebugData, debugger::debugger::SegmentType};
use crate::{db::debugdb::DebugData, debugger::core::SegmentType};
use rusqlite::params;
use std::{
fs::File,
Expand Down Expand Up @@ -101,11 +101,10 @@ impl DebugData {
let tx = self.conn.transaction()?;
let mut symcount = 0;
let mut fcount = 0;
let mut lcount = 0;
let mut scount = 0;

let mut mcount = 0;
let mut segcount = 0;
let mut spcount = 0;

let mut ccount = 0;

let start = SystemTime::now();
Expand Down Expand Up @@ -152,7 +151,7 @@ impl DebugData {
"lib\tid" => {}
"line\tid" => {
let line = Self::parse_line(&record)?;
lcount += 1;

let ltype = if let Some(lt) = line.type_ { lt } else { 0 };
tx.execute(
"insert into line
Expand Down Expand Up @@ -225,7 +224,6 @@ impl DebugData {
}
"span\tid" => {
let span = Self::parse_span(&record)?;
spcount += 1;
tx.execute(
"insert into span
(id, seg, start, size, type)
Expand All @@ -237,7 +235,6 @@ impl DebugData {
}
"scope\tid" => {
let scope = Self::parse_scope(record)?;
scount += 1;
tx.execute(
"insert into scope
(id, name, module, type, size, parent, sym)
Expand Down Expand Up @@ -318,7 +315,7 @@ impl DebugData {
say(&format!("segments: {}", segcount));
say(&format!("symbols: {}", ccount + symcount));
let end = SystemTime::now();
let duration = end.duration_since(start).unwrap();
let _duration = end.duration_since(start).unwrap();
tx.commit()?;
self.merge_csymbols(symcount)?;
Ok(())
Expand All @@ -329,13 +326,13 @@ impl DebugData {
let mut statics = Vec::new();

for row in rows {
let id = row[0].vto_i64()?;
let _id = row[0].vto_i64()?;
let name = row[1].vto_string()?;
let scope = row[2].vto_i64()?;
let type_ = row[3].vto_i64()?;
let sc = row[4].vto_string()?;
let sym = row[5].vto_i64()?;
let offset = row[6].vto_i64()?;
let _offset = row[6].vto_i64()?;

match sc.as_str() {
"ext" => {}
Expand Down
24 changes: 16 additions & 8 deletions src/debugger/debugger.rs → src/debugger/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub struct Debugger {
pub(crate) stack_frames: Vec<StackFrame>,
pub(crate) enable_stack_check: bool,
pub(crate) enable_mem_check: bool,
pub(crate) enable_heap_check: bool,
pub(crate) load_name: String,
pub(crate) run_done: bool,
pub(crate) dbgdb: DebugData,
Expand Down Expand Up @@ -174,8 +175,9 @@ impl Debugger {
dis_line: String::new(),
ticks: 0,
stack_frames: Vec::new(),
enable_stack_check: false,
enable_mem_check: false,
enable_stack_check: true,
enable_mem_check: true,
enable_heap_check: true,
next_bp: None,
load_name: String::new(),
run_done: false,
Expand Down Expand Up @@ -245,6 +247,9 @@ impl Debugger {
pub fn enable_mem_check(&mut self, enable: bool) {
self.enable_mem_check = enable;
}
pub fn enable_heap_check(&mut self, enable: bool) {
self.enable_heap_check = enable;
}
pub fn set_cc65_dir(&mut self, dir: &str) -> Result<()> {
let path = Path::new(dir);
if !path.exists() {
Expand Down Expand Up @@ -290,6 +295,9 @@ impl Debugger {
pub fn get_source(&self, file: i64, from: i64, to: i64) -> Result<Vec<String>> {
self.dbgdb.get_source(file, from, to)
}

// because the intention is clearer my way :-)
#[allow(clippy::needless_range_loop)]
fn init_shadow(&self) -> Result<()> {
let shadow = Cpu::get_shadow();
for seg in self.seg_list.iter().filter(|s| s.name != "EXEHDR") {
Expand Down Expand Up @@ -350,11 +358,11 @@ impl Debugger {
self.dbgdb.load_files(&mut self.file_table)?;

let regbank = self.dbgdb.get_symbol("zeropage.regbank")?;
if regbank.len() != 0 {
if !regbank.is_empty() {
self.regbank_addr = Some(regbank[0].1);
}
let regbanksize = self.dbgdb.get_symbol("regbanksize")?;
if regbanksize.len() != 0 {
if !regbanksize.is_empty() {
self.regbank_size = Some(regbanksize[0].1);
}
self.dbg_file = Some(file.to_path_buf());
Expand Down Expand Up @@ -541,7 +549,7 @@ impl Debugger {

// no - take first matching eq

if syms.len() > 0 {
if !syms.is_empty() {
return Ok(syms[0].name.clone());
}
Ok(format!("${:04x}", addr))
Expand All @@ -552,7 +560,7 @@ impl Debugger {
if let Some(sym) = syms.iter().find(|s| s.sym_type == SymbolType::Label) {
return Ok(sym.name.clone());
}
if syms.len() > 0 {
if !syms.is_empty() {
return Ok(syms[0].name.clone());
}

Expand Down Expand Up @@ -600,7 +608,7 @@ impl Debugger {
&self.stack_frames
}

pub fn find_module(&self, addr: u16) -> Option<&SegChunk> {
pub fn _find_module(&self, addr: u16) -> Option<&SegChunk> {
if self.seg_list.is_empty() {
return None;
}
Expand All @@ -625,7 +633,7 @@ impl Debugger {
}
Some(&seg.modules[current_mod])
}
pub fn find_csym(&self, name: &str, scope: i64) -> Result<Option<HLSym>> {
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> {
Expand Down
4 changes: 2 additions & 2 deletions src/debugger/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ pub enum BugType {
SegCheck(u16),
}
use crate::{
debugger::core::{Debugger, FrameType, SourceDebugMode, StackFrame, WatchType},
debugger::cpu::{Cpu, MemCheck},
debugger::debugger::{Debugger, FrameType, SourceDebugMode, StackFrame, WatchType},
};
use anyhow::anyhow;

use super::debugger::JsrData;
use super::core::JsrData;
impl Debugger {
pub fn execute(&mut self, mut count: u16) -> Result<StopReason> {
let counting = count > 0;
Expand Down
22 changes: 15 additions & 7 deletions src/debugger/intercepts.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{debugger::cpu::Cpu, debugger::debugger::Debugger, trace};
use crate::{debugger::core::Debugger, debugger::cpu::Cpu, trace};
use anyhow::{bail, Result};

use super::{
core::HeapBlock,
cpu::ShadowFlags,
debugger::HeapBlock,
execute::{BugType, StopReason},
};

Expand Down Expand Up @@ -111,7 +111,11 @@ impl Debugger {
(hb.size, hb.addr)
} else {
// not found -> double or invalid free
return Ok(Some(StopReason::Bug(BugType::HeapCheck)));
if self.enable_heap_check {
return Ok(Some(StopReason::Bug(BugType::HeapCheck)));
} else {
return Ok(None);
}
};
self.heap_blocks.remove(&addr);

Expand Down Expand Up @@ -183,7 +187,11 @@ impl Debugger {
hb.realloc_size = Some(size);
} else {
// not found -> realloc of non heap block
return Ok(Some(StopReason::Bug(BugType::HeapCheck)));
if self.enable_heap_check {
return Ok(Some(StopReason::Bug(BugType::HeapCheck)));
} else {
return Ok(None);
}
};

// realloc is privileged - it can write to unalloacted memory
Expand All @@ -200,8 +208,8 @@ impl Debugger {
}
fn read_arg(offset: u16) -> u16 {
let sp65_addr = Cpu::get_sp65_addr() as u16;
let sp65 = Cpu::read_word((sp65_addr + offset) as u16);
let val = Cpu::read_word(sp65);
val
let sp65 = Cpu::read_word(sp65_addr + offset);

Cpu::read_word(sp65)
}
}
2 changes: 1 addition & 1 deletion src/debugger/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::Path;

use crate::debugger::cpu::Cpu;
use crate::log::say;
use crate::trace;


static HEADER: &[u8] = &[0x73, 0x69, 0x6D, 0x36, 0x35];
pub fn load_code(file: &Path) -> Result<(u8, u16, u8, u16)> {
Expand Down
2 changes: 1 addition & 1 deletion src/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Disassembler portion of db65. Placed in a separate file for clarity
liberally copied from pm100/v65
*/

use crate::debugger::debugger::Debugger;
use crate::debugger::core::Debugger;

impl Debugger {
// reads one instruction and loads its interpretation into
Expand Down
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mem =@(ptr + (0x20*xr)) // more math
*/

use crate::{debugger::cpu::Cpu, debugger::debugger::Debugger};
use crate::{debugger::core::Debugger, debugger::cpu::Cpu};
use anyhow::{anyhow, Result};
use evalexpr::{eval_int_with_context, Context, EvalexprResult, Value};
use std::ops::RangeInclusive;
Expand Down
Loading

0 comments on commit ce835a3

Please sign in to comment.