-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.c
executable file
·80 lines (68 loc) · 1.75 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Created by Paul on 3/2/2018.
*/
#define _LARGEFILE64_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <libgen.h>
#include "readaccess.h"
#include "debug.h"
/* used for pointer validation testing */
/* set using alloca() on entry to debugInit() */
void * gStackTop;
const char * gExecName;
void debugInit( int UNUSED(argc), char *argv[] )
{
gStackTop = alloca( 0 );
gExecName = basename( argv[0] );
}
#define kRowLength 32
/**
*
* @param displayOffset
* @param ptr
* @param length
*/
void dumpMemory(unsigned long displayOffset, void * ptr, size_t length)
{
if (isValidPtr(ptr))
{
for (size_t i = 0; i < length; i += kRowLength)
{
size_t rowLength = (kRowLength < length - i) ? kRowLength : length - i;
DebugOut("%08lx: ", displayOffset + i );
for (size_t j = 0; j < rowLength; ++j)
{
DebugOut("%02x ", ((unsigned char *)ptr)[i + j]);
}
DebugOut(" |");
for (size_t j = 0; j < rowLength; ++j)
{
char c = ((unsigned char *)ptr)[i + j];
DebugOut("%c", (isgraph(c) || c == ' ') ? c : '.');
}
DebugOut("|\n");
}
}
}
/**
*
* @param drive
* @param offset
* @param length
*/
void dumpDisk( tDrive * drive, off64_t offset, size_t length )
{
char * block = calloc( length, 1 );
if ( isHeapPtr( block ) )
{
ssize_t rdLen = readDrive( drive, offset, block, length );
if ( rdLen == (ssize_t) length )
{
dumpMemory( offset, block, length );
}
free( block );
}
}