<commit_before>#include "debug.h"
#include "fs.h"


struct map_node *map_list = NULL;


void debug_map_push(const void *addr, uint32_t sect_num, uint32_t sect_cnt) {
	fprintf(stderr, "\e[31;1m  MAP %p %" PRIx32 " %" PRIx32 "\n\e[0m",
		addr, sect_num, sect_cnt);
	
	if (map_list == NULL) {
		map_list = malloc(sizeof(struct map_node));
		map_list->next = NULL;
	} else {
		struct map_node *new_head = malloc(sizeof(struct map_node));
		new_head->next = map_list;
		map_list = new_head;
	}
	
	map_list->payload.addr = addr;
	
	map_list->payload.sect_num = sect_num;
	map_list->payload.sect_cnt = sect_cnt;
}

void debug_map_pop(const void *addr, uint32_t sect_num, uint32_t sect_cnt) {
	fprintf(stderr, "\e[32;1mUNMAP %p %" PRIx32 " %" PRIx32 "\n\e[0m",
		addr, sect_num, sect_cnt);
	
	struct map_node **prev = &map_list, *node = map_list;
	while (node != NULL) {
		if (node->payload.addr == addr &&
			node->payload.sect_num == sect_num &&
			node->payload.sect_cnt == sect_cnt) {
			*prev = node->next;
			free(node);
			
			return;
		}
		
		prev = &node->next;
		node = node->next;
	}
	
	errx("%s: this block doesn't seem to have been mapped", __func__);
}

void debug_map_dump(void) {
	
}

void dump_mem(const void *ptr, size_t len) {
	warnx("%s: %zu bytes @ %p", __func__, len, ptr);
	
	uintptr_t begin = (uintptr_t)ptr;
	uintptr_t end   = (uintptr_t)((uint8_t *)ptr + len);
	
	/* round down to 16 bytes */
	begin &= ~0xf;
	
	/* round up to 16 bytes */
	if ((end & 0xf) != 0) {
		end &= ~0xf;
		end += 0x10;
	}
	
	/* get log2 of len for display purposes */
	len = (end - begin);
	uint8_t log2_len = 0;
	while (len != 0) {
		len >>= 1;
		++log2_len;
	}
	log2_len = CEIL(log2_len, 4);
	
	/* set len correctly */
	len = (end - begin);
	
	const uint8_t *begin_b = (const uint8_t *)begin;
	const uint8_t *end_b   = (const uint8_t *)end;
	
	const uint8_t *ptr_b = begin_b;
	
	bool skip = false, skip_prev = false;
	while (ptr_b <= end_b) {
		if (ptr_b - begin_b >= 0x10 && end_b - ptr_b >= 0x10) {
			if (memcmp(ptr_b - 0x10, ptr_b, 0x10) == 0) {
				if (!skip_prev) {
					fputs("         *\n", stderr);
					skip = true;
				}
			}
		} else {
			skip = false;
		}
		
		if (!skip) {
			fprintf(stderr, "        %0*" PRIxPTR ": ",
				log2_len, ptr_b - begin_b);<commit_msg>lib: fix unending skip in dump_mem<commit_after>#include "debug.h"
#include "fs.h"


struct map_node *map_list = NULL;


void debug_map_push(const void *addr, uint32_t sect_num, uint32_t sect_cnt) {
	fprintf(stderr, "\e[31;1m  MAP %p %" PRIx32 " %" PRIx32 "\n\e[0m",
		addr, sect_num, sect_cnt);
	
	if (map_list == NULL) {
		map_list = malloc(sizeof(struct map_node));
		map_list->next = NULL;
	} else {
		struct map_node *new_head = malloc(sizeof(struct map_node));
		new_head->next = map_list;
		map_list = new_head;
	}
	
	map_list->payload.addr = addr;
	
	map_list->payload.sect_num = sect_num;
	map_list->payload.sect_cnt = sect_cnt;
}

void debug_map_pop(const void *addr, uint32_t sect_num, uint32_t sect_cnt) {
	fprintf(stderr, "\e[32;1mUNMAP %p %" PRIx32 " %" PRIx32 "\n\e[0m",
		addr, sect_num, sect_cnt);
	
	struct map_node **prev = &map_list, *node = map_list;
	while (node != NULL) {
		if (node->payload.addr == addr &&
			node->payload.sect_num == sect_num &&
			node->payload.sect_cnt == sect_cnt) {
			*prev = node->next;
			free(node);
			
			return;
		}
		
		prev = &node->next;
		node = node->next;
	}
	
	errx("%s: this block doesn't seem to have been mapped", __func__);
}

void debug_map_dump(void) {
	
}

void dump_mem(const void *ptr, size_t len) {
	warnx("%s: %zu bytes @ %p", __func__, len, ptr);
	
	uintptr_t begin = (uintptr_t)ptr;
	uintptr_t end   = (uintptr_t)((uint8_t *)ptr + len);
	
	/* round down to 16 bytes */
	begin &= ~0xf;
	
	/* round up to 16 bytes */
	if ((end & 0xf) != 0) {
		end &= ~0xf;
		end += 0x10;
	}
	
	/* get log2 of len for display purposes */
	len = (end - begin);
	uint8_t log2_len = 0;
	while (len != 0) {
		len >>= 1;
		++log2_len;
	}
	log2_len = CEIL(log2_len, 4);
	
	/* set len correctly */
	len = (end - begin);
	
	const uint8_t *begin_b = (const uint8_t *)begin;
	const uint8_t *end_b   = (const uint8_t *)end;
	
	const uint8_t *ptr_b = begin_b;
	
	bool skip = false, skip_prev = false;
	while (ptr_b <= end_b) {
		if (ptr_b - begin_b >= 0x10 && end_b - ptr_b >= 0x10) {
			if (memcmp(ptr_b - 0x10, ptr_b, 0x10) == 0) {
				if (!skip_prev) {
					fputs("         *\n", stderr);
					skip = true;
				}
			} else {
				skip = false;
			}
		} else {
			skip = false;
		}
		
		if (!skip) {
			fprintf(stderr, "        %0*" PRIxPTR ": ",
				log2_len, ptr_b - begin_b);
{'fraction_non_alphanumeric': 0.16126224156692057, 'fraction_numerical': 0.028726877040261153, 'mean_word_length': 3.0999107939339874, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 33, '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': '1112426', 'n_tokens_mistral': 2162, 'n_tokens_neox': 1910, 'n_words': 461}