summaryrefslogtreecommitdiff
blob: 899977dda137964128712a29e3d62c07ae6ad955 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
diff -Nrup gdb-6.4.orig/gdb/printcmd.c gdb-6.4/gdb/printcmd.c
--- gdb-6.4.orig/gdb/printcmd.c	2005-02-24 08:51:34.000000000 -0500
+++ gdb-6.4/gdb/printcmd.c	2005-12-08 23:17:09.000000000 -0500
@@ -942,6 +942,195 @@ print_command (char *exp, int from_tty)
   print_command_1 (exp, 0, 1);
 }
 
+static void 
+scanmem_command(char *exp, int from_tty)
+{
+   struct expression *expr;
+   struct value *val;
+   struct format_data fmt;
+   CORE_ADDR addr;
+   unsigned char *data;
+   unsigned char *size;
+   unsigned char *pattern;
+   unsigned char typ;
+   unsigned char typc;
+   unsigned char my_buf[4];
+   unsigned int nfetch;
+   unsigned int len = 10;
+   unsigned int memsize;
+   unsigned int i;
+   int typi;
+   int errcode;
+
+   if (exp && *exp) {
+      struct type *type;
+      char *str_num = NULL;
+      char *string_to_seek = NULL;
+      char *tmp = strdup(exp);
+      char *c;
+
+      c = tmp;
+      if (*c == ' ')
+	 while (*c && *c == ' ')
+	    c++;
+
+      while (*c && *c != ' ')
+	 c++;
+
+      *c = '\0';
+      c++;
+
+      while (*c && *c == ' ')
+	 c++;
+
+      size = c;
+
+      while (*c && *c != ' ')
+	 c++;
+
+      *c = '\0';
+      c++;
+
+      while (*c && *c == ' ')
+	 c++;
+
+      typ = *c;
+      c++;
+
+      *c = '\0';
+      c++;
+
+      while (*c && *c == ' ')
+	 c++;
+      pattern = c;
+
+      printf_filtered(">>addr %s: size %s: type:%c pattern:%s<< \n",
+		      tmp, size, typ, pattern);
+
+      expr = parse_expression(tmp);
+      val = evaluate_expression(expr);
+
+      if (TYPE_CODE(value_type(val)) == TYPE_CODE_REF)
+	 val = value_ind(val);
+
+      if (TYPE_CODE(value_type(val)) == TYPE_CODE_FUNC
+	  && VALUE_LVAL(val) == lval_memory)
+	 addr = VALUE_ADDRESS(val);
+      else
+	 addr = value_as_address(val);
+
+      len = atoi(size);
+      data = xmalloc(len);
+      nfetch = partial_memory_read(addr, data, len, &errcode);
+
+      if (nfetch != len)
+	 printf_filtered("we can read only %i bytes\n", nfetch);
+
+      switch (typ) {
+	 case 's':
+	    memsize = strlen(pattern);
+	    if (nfetch < memsize) {
+	       printf_filtered
+		   ("we read only %i bytes and we seek for a pattern of %i bytes\n",
+		    nfetch, memsize);
+	       free(data);
+	       free(tmp);
+	       return;
+	    }
+	    for (i = 0; i <= (nfetch - memsize); i++, addr++) {
+	       if (memcmp(data + i, pattern, memsize) == 0) {
+		  printf_filtered("pattern match at ");
+		  deprecated_print_address_numeric((addr), 1, gdb_stdout);
+		  printf_filtered("\n");
+	       }
+	    }
+	    break;
+	 case 'i':
+	    memsize = sizeof(int);
+	    typi = atoi(pattern);
+
+	    if (nfetch < memsize) {
+	       printf_filtered
+		   ("we read only %i bytes and we seek for a pattern of %i bytes\n",
+		    nfetch, memsize);
+	       free(data);
+	       free(tmp);
+	       return;
+	    }
+	    for (i = 0; i <= (nfetch - memsize); i++, addr++) {
+	       int *pint;
+	       pint = (unsigned char *) (data + i);
+	       if (*pint == typi) {
+		  printf_filtered("pattern match at ");
+		  deprecated_print_address_numeric((addr), 1, gdb_stdout);
+		  printf_filtered("\n");
+	       }
+	    }
+	    break;
+	 case 'a':
+	    memsize = sizeof(unsigned long);
+	    if (sscanf(pattern, "0x%x", &i) == 0) {
+	       printf_filtered("cant convert to hexa %s\n", pattern);
+	       break;
+	    }
+	    my_buf[0] = (unsigned char) ((i & 0x000000FF));
+	    my_buf[1] = (unsigned char) ((i & 0x0000FF00) >> 8);
+	    my_buf[2] = (unsigned char) ((i & 0x00FF0000) >> 16);
+	    my_buf[3] = (unsigned char) ((i & 0xFF000000) >> 24);
+	    if (nfetch < memsize) {
+	       printf_filtered
+		   ("we read only %i bytes and we seek for a pattern of %i bytes\n",
+		    nfetch, memsize);
+	       free(data);
+	       free(tmp);
+	       return;
+	    }
+	    for (i = 0; i <= (nfetch - memsize); i++, addr++) {
+	       if (memcmp((data + i), my_buf, memsize) == 0) {
+		  printf_filtered("pattern match at ");
+		  deprecated_print_address_numeric((addr), 1, gdb_stdout);
+		  printf_filtered("\n");
+	       }
+	    }
+	    break;
+	 case 'b':
+	 case 'c':
+	    memsize = sizeof(char);
+	    if (typ == 'c')
+	       typc = *pattern;
+	    else {
+	       if (strncmp("0x", pattern, 2) == 0) {
+		  sscanf(pattern, "0x%02x", &typc);
+	       } else
+		  typc = (unsigned char)atoi(pattern);
+	    }
+	    printf_filtered(">>>%0x2\n", (int) typc);
+	    if (nfetch < memsize) {
+	       printf_filtered
+		   ("we read only %i bytes and we seek for a pattern of %i bytes\n",
+		    nfetch, memsize);
+	       free(data);
+	       free(tmp);
+	       return;
+	    }
+	    for (i = 0; i <= (nfetch - memsize); i++, addr++) {
+	       if (*(data + i) == typc) {
+		  printf_filtered("pattern match at ");
+		  deprecated_print_address_numeric((addr), 1, gdb_stdout);
+		  printf_filtered("\n");
+	       }
+	    }
+	    break;
+	 default:
+	    printf_filtered("'%c' is not a valid type\n", typ);
+	    break;
+      }
+      free(data);
+      free(tmp);
+      return;
+   }
+}
+
 /* Same as print, except in epoch, it gets its own window */
 static void
 inspect_command (char *exp, int from_tty)
@@ -2138,6 +2327,17 @@ EXP may be preceded with /FMT, where FMT
 but no count or size letter (see \"x\" command)."));
   set_cmd_completer (c, location_completer);
   add_com_alias ("p", "print", class_vars, 1);
+  c =
+  add_com ("scanmem", class_vars, scanmem_command,
+	"scanmem  <addr> <num> <type> <string>\n"
+	"example: scanmem  $esp 100 a 0x8048434\n"
+	" scan for this addr into the stack\n\n"
+	" scanmem  0x08048434 100 s fsck\n"
+	" seek for a string\n"
+	" scanmem  0x8048434 100 b 0x75\n"
+	" scan for the char 0x75\n"
+	" a = address c = char b = byte i = int  s = string\n"
+	"\n"); /* antilove@zolo.freelsd.net */
 
   c = add_com ("inspect", class_vars, inspect_command, _("\
 Same as \"print\" command, except that if you are running in the epoch\n\
diff -Nrup gdb-6.4.orig/gdb/valprint.c gdb-6.4/gdb/valprint.c
--- gdb-6.4.orig/gdb/valprint.c	2005-10-09 21:03:59.000000000 -0400
+++ gdb-6.4/gdb/valprint.c	2005-12-08 23:17:09.000000000 -0500
@@ -39,7 +39,7 @@
 
 /* Prototypes for local functions */
 
-static int partial_memory_read (CORE_ADDR memaddr, char *myaddr,
+int partial_memory_read (CORE_ADDR memaddr, char *myaddr,
 				int len, int *errnoptr);
 
 static void show_print (char *, int);
@@ -1037,7 +1037,7 @@ val_print_array_elements (struct type *t
 /* FIXME: cagney/1999-10-14: Only used by val_print_string.  Can this
    function be eliminated.  */
 
-static int
+int
 partial_memory_read (CORE_ADDR memaddr, char *myaddr, int len, int *errnoptr)
 {
   int nread;			/* Number of bytes actually read. */