-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_util.cpp
149 lines (124 loc) · 3.61 KB
/
mem_util.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2007-2008 ZioNz - zionztp@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <wx/wx.h>
#include "lsearchFrm.h"
#include "mem_util.h"
#include <sys/ptrace.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
#define MAX_BLOCK_SIZE 200*1024*1024 //200MB
//Pointer to access main frame
extern lsearchFrm *main_frame;
//Helpers
bool find_str(string datos, string buscar){
int i=0;
int bt=buscar.size();
while(i+bt < datos.size()){
if(datos.substr(i,bt) == buscar){
return true;
}
i++;
}
return false;
}
//Read and store all the memory of the process
bool Cmemutil::SaveMemory(int pid)
{
Tmemblock mb;
char line[2048];
char fname[1024];
string sline,a,b;
unsigned int i;
unsigned int max_mem = 0;
unsigned int block_size;
unsigned int t;
int progress=0;
unsigned int memcount=0;
int tmp;
FILE *entrada;
//Read the mem maps
sprintf(fname, "/proc/%i/maps", pid);
entrada = fopen(fname, "rb");
if(!entrada){
return false;
}
FreeMemBlocks();
do{
fgets(line, 2000, entrada);
//Skip libraries and others
if(!find_str(line, "/lib/") && !find_str(line, "/dev")){
sline = line;
a = sline.substr(0,8);
b = sline.substr(9,8);
mb.start = strtoll(a.c_str(), NULL, 16);
mb.end = strtoll(b.c_str(), NULL, 16);
memblocks.push_back(mb);
}else{
//printf("SKIP!\n");
}
}while(!feof(entrada));
//Calculate total memory to read
for(i=0; i<memblocks.size(); i++){
block_size = memblocks[i].end - memblocks[i].start;
max_mem += block_size;
}
size = max_mem;
if(ptrace(PTRACE_ATTACH, pid, NULL, NULL)!=0){
return false;
}
wait(NULL);
//Fill memory blocks
for(i=0; i<memblocks.size(); i++){
block_size = memblocks[i].end - memblocks[i].start;
if(block_size < 1 || block_size > MAX_BLOCK_SIZE){
return false;
}
memblocks[i].size = block_size;
memblocks[i].data = (char*) malloc(block_size);
//Read memblock
for(t=0; t<=block_size; t+=4){
tmp = ptrace(PTRACE_PEEKDATA, pid, memblocks[i].start + t, NULL);
memcpy(&memblocks[i].data[t], &tmp, 4);
/*
memblocks[i].data[t] = (char) (tmp);
memblocks[i].data[t+1] = (char) (tmp >> 8);
memblocks[i].data[t+2] = (char) (tmp >> 16);
memblocks[i].data[t+3] = (char) (tmp >> 24);
*/
memcount+=4;
if( (memcount % 100000) == 0){
wxYield();
progress = (50 * memcount) / max_mem;
main_frame->Wxgauge1->SetValue(progress);
}
}
printf("%02i - %p to %p , size: %d\n", i, memblocks[i].start, memblocks[i].end, memblocks[i].end - memblocks[i].start);
}
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return true;
}
void Cmemutil::FreeMemBlocks()
{
unsigned int i;
for(i=0; i<memblocks.size(); i++){
free(memblocks[i].data);
}
memblocks.clear();
}