<commit_before>//! Heapless debugging functions for Tock troubleshooting

use crate::console::Console;

pub fn print_as_hex(value: usize) {
    let mut buffer = [b'\n'; 11];
    write_as_hex(&mut buffer, value);
    Console::new().write(buffer);
}

pub fn print_stack_pointer() {
    let stack_pointer;
    unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") };

    let mut buffer = [b'\n'; 15];
    buffer[0..4].clone_from_slice(b"SP: ");
    write_as_hex(&mut buffer[4..15], stack_pointer);
    Console::new().write(buffer);
}

#[inline(always)] // Initial stack size is too small (64 bytes currently)
pub fn dump_address(address: *const usize) {
    let mut buffer = [b'\n'; 23];
    write_as_hex(&mut buffer[0..10], address as usize);
    buffer[10..12].clone_from_slice(b": ");
    write_as_hex(&mut buffer[12..22], unsafe { *address });
    Console::new().write(&buffer);
}

fn write_as_hex(buffer: &mut [u8], value: usize) {
    write_formatted(buffer, value, 0x10_00_00_00, 0x10);
}

fn write_formatted(buffer: &mut [u8], value: usize, start: usize, base: usize) {<commit_msg>Better debugging<commit_after>//! Heapless debugging functions for Tock troubleshooting

use crate::console::Console;

pub fn println() {
    let buffer = [b'\n'];
    Console::new().write(&buffer);
}

pub fn print_as_hex(value: usize) {
    let mut buffer = [b'\n'; 11];
    write_as_hex(&mut buffer, value);
    Console::new().write(buffer);
}

pub fn print_stack_pointer() {
    let stack_pointer;
    unsafe { asm!("mov $0, sp" : "=r"(stack_pointer) : : : "volatile") };

    let mut buffer = [b'\n'; 15];
    buffer[0..4].clone_from_slice(b"SP: ");
    write_as_hex(&mut buffer[4..15], stack_pointer);
    Console::new().write(buffer);
}

#[inline(always)] // Initial stack size is too small (128 bytes currently)
pub fn dump_address(address: *const usize) {
    let mut buffer = [b' '; 28];
    write_as_hex(&mut buffer[0..10], address as usize);
    buffer[10] = b':';
    write_as_hex(&mut buffer[12..22], unsafe { *address });
    for index in 0..4 {
        let byte = unsafe { *(address as *const u8).offset(index) };
        let byte_is_printable_char = byte >= 0x20 && byte < 0x80;
        if byte_is_printable_char {
            buffer[23 + index as usize] = byte;
        }
    }
    buffer[27] = b'\n';
    Console::new().write(&buffer);
}

pub fn dump_memory(start_address: *const usize, count: isize) {
    let range = if count < 0 { count..0 } else { 0..count };

    for offset in range {
        dump_address(unsafe { start_address.offset(offset) });
    }
}

fn write_as_hex(buffer: &mut [u8], value: usize) {
    write_formatted(buffer, value, 0x10_00_00_00, 0x10);
}

fn write_formatted(buffer: &mut [u8], value: usize, start: usize, base: usize) {
{'fraction_non_alphanumeric': 0.1673881673881674, 'fraction_numerical': 0.03354978354978355, 'mean_word_length': 3.692047377326565, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7606825', 'n_tokens_mistral': 1024, 'n_tokens_neox': 978, 'n_words': 244}