[PATCH V3] Add -C option for search

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello Dave,

The new patch is attached.

I  simplified the display_with_pre_and_post() function by calling the
currently-existing display_memory() function, and made the output
readable according to your advice.

Thanks
Zhang Yanfei
>From 82c67ad11d87a727fe7767b4d025f3380820ce69 Mon Sep 17 00:00:00 2001
From: zhangyanfei <zhangyanfei@xxxxxxxxxxxxxx>
Date: Thu, 16 Feb 2012 13:53:30 +0800
Subject: [PATCH] Add option -C for sub-command search.

Signed-off-by: zhangyanfei <zhangyanfei@xxxxxxxxxxxxxx>
---
 help.c   |    5 ++-
 memory.c |  182 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 165 insertions(+), 22 deletions(-)

diff --git a/help.c b/help.c
index f752283..d3317f4 100755
--- a/help.c
+++ b/help.c
@@ -2343,7 +2343,7 @@ char *help_search[] = {
 "seach",
 "search memory",
 "[-s start] [ -[kKV] | -u | -p ] [-e end | -l length] [-m mask]\n"
-"         -[cwh] value ...",
+"         [-C num] -[cwh] value ...",
 "  This command searches for a given value within a range of user virtual, kernel",
 "  virtual, or physical memory space.  If no end nor length value is entered, ",
 "  then the search stops at the end of user virtual, kernel virtual, or physical",
@@ -2374,6 +2374,9 @@ char *help_search[] = {
 "             appropriate for the memory type specified.",
 "  -l length  Length in bytes of address range to search.",
 "    -m mask  Ignore the bits that are set in the hexadecimal mask value.",
+"     -C num  Also display memory contents just before and after value.  The size",
+"             of the memory displayed before(after) value is num * sizeof(value).",
+"             Note, this option is ignored when -c is specified.",
 "         -c  Search for character string values instead of unsigned longs.  If",
 "             the string contains any space(s), it must be encompassed by double",
 "             quotes.",
diff --git a/memory.c b/memory.c
index 55a184b..f480d32 100755
--- a/memory.c
+++ b/memory.c
@@ -87,6 +87,8 @@ struct meminfo {           /* general purpose memory information structure */
 struct searchinfo {
 	int mode;
 	int vcnt;
+	int c_num;
+	int memtype;
 	union {
 		/* default ulong search */
 		struct {
@@ -176,6 +178,7 @@ static int dump_page_lists(struct meminfo *);
 static void dump_kmeminfo(void);
 static int page_to_phys(ulong, physaddr_t *); 
 static void display_memory(ulonglong, long, ulong, int, void *); 
+static void display_with_pre_and_post(void *, ulonglong, struct searchinfo *);
 static ulong search_ulong(ulong *, ulong, int, struct searchinfo *);
 static ulong search_uint(ulong *, ulong, int, struct searchinfo *);
 static ulong search_ushort(ulong *, ulong, int, struct searchinfo *);
@@ -266,6 +269,7 @@ static ulong kmem_cache_nodelists(ulong);
 #define DISPLAY_ASCII (0x2000)
 #define NET_ENDIAN    (0x4000)
 #define DISPLAY_RAW   (0x8000)
+#define NO_ERROR      (0x10000)
 #define DISPLAY_TYPES (DISPLAY_RAW|DISPLAY_ASCII|DISPLAY_8|\
 		       DISPLAY_16|DISPLAY_32|DISPLAY_64)
 
@@ -1243,13 +1247,14 @@ display_memory(ulonglong addr, long count, ulong flag, int memtype, void *opt)
 	char *addrtype;
 	struct memloc mem;
 	int displayed, per_line;
-	int hx;
+	int hx, lost;
 	char hexchars[MAX_HEXCHARS_PER_LINE+1];
 	char ch;
 	int linelen;
 	char buf[BUFSIZE];
 	char slab[BUFSIZE];
 	int ascii_start;
+	ulong error_handle = FAULT_ON_ERROR;
 	char *hex_64_fmt = BITS32() ? "%.*llx " : "%.*lx ";
 	char *dec_64_fmt = BITS32() ? "%12lld " : "%15ld ";
 	char *dec_u64_fmt = BITS32() ? "%12llu " : "%20lu ";
@@ -1301,7 +1306,7 @@ display_memory(ulonglong addr, long count, ulong flag, int memtype, void *opt)
 	}
 
 	BZERO(&mem, sizeof(struct memloc));
-	hx = linelen = typesz = per_line = ascii_start = 0;
+	hx = lost = linelen = typesz = per_line = ascii_start = 0;
 	location = NULL;
 
 	switch (flag & (DISPLAY_TYPES))
@@ -1350,11 +1355,17 @@ display_memory(ulonglong addr, long count, ulong flag, int memtype, void *opt)
 	}
 
 	for (i = a = 0; i < count; i++) {
-		readmem(addr, memtype, location, typesz, 
-			readtype, FAULT_ON_ERROR);
+		if (flag & NO_ERROR)
+			error_handle = RETURN_ON_ERROR|QUIET;
+		if(!readmem(addr, memtype, location, typesz,
+			readtype, error_handle)) {
+			addr += typesz;
+			lost += 1;
+			continue;
+		}
 
-                if (!(flag & DISPLAY_ASCII) && ((i % per_line) == 0)) {
-                        if (i) {
+                if (!(flag & DISPLAY_ASCII) && (((i - lost) % per_line) == 0)) {
+                        if ((i - lost)) {
 				if (flag & ASCII_ENDLINE) {
 					fprintf(fp, "  %s", hexchars);
 				}
@@ -1551,7 +1562,8 @@ display_memory(ulonglong addr, long count, ulong flag, int memtype, void *opt)
 		fprintf(fp, "  %s", hexchars);
 	}
 
-	fprintf(fp,"\n");
+	if (lost != count )
+		fprintf(fp,"\n");
 }
 
 /*
@@ -11617,7 +11629,7 @@ generic_get_kvaddr_ranges(struct vaddr_range *rp)
 void
 cmd_search(void)
 {
-        int i, c, ranges;
+        int i, c, ranges, c_num;
 	ulonglong start, end;
 	ulong mask, memtype, len;
 	ulong uvaddr_start, uvaddr_end;
@@ -11631,6 +11643,7 @@ cmd_search(void)
 
 #define vaddr_overflow(ADDR) (BITS32() && ((ADDR) > 0xffffffffULL))
 
+	c_num = 0;
 	start = end = mask = sflag = pflag = Kflag = Vflag = memtype = len = 0;
 	kvaddr_start = kvaddr_end = 0;
 	uvaddr_start = UNINITIALIZED;
@@ -11668,7 +11681,7 @@ cmd_search(void)
 
 	searchinfo.mode = SEARCH_ULONG;	/* default search */
 
-        while ((c = getopt(argcnt, args, "l:ukKVps:e:v:m:hwc")) != EOF) {
+        while ((c = getopt(argcnt, args, "l:ukKVps:e:v:m:hwcC:")) != EOF) {
                 switch(c)
                 {
 		case 'u':
@@ -11769,6 +11782,10 @@ cmd_search(void)
 			searchinfo.mode = SEARCH_CHARS;
 			break;
 
+		case 'C':
+			c_num = dtoi(optarg, FAULT_ON_ERROR, NULL);
+			break;
+
                 default:
                         argerrs++;
                         break;
@@ -11790,6 +11807,8 @@ cmd_search(void)
         if (argerrs || !sflag || !args[optind] || (len && end) || !memtype)
                 cmd_usage(pc->curcmd, SYNOPSIS);
 
+	searchinfo.memtype = memtype;
+
 	/*
 	 *  Verify starting address.
 	 */
@@ -11916,6 +11935,37 @@ cmd_search(void)
 		}
 	}
 			
+	if (c_num) {
+		switch (searchinfo.mode)
+		{
+		case SEARCH_ULONG:
+			if (c_num > PAGESIZE()/sizeof(long)) {
+				error(INFO, "WARNING: too many numbers and"
+					" -C option is ignored\n");
+				c_num = 0;
+			}
+			break;
+		case SEARCH_UINT:
+			if (c_num > PAGESIZE()/sizeof(int)) {
+				error(INFO, "WARNING: too many numbers and"
+					" -C option is ignored\n");
+				c_num = 0;
+			}
+			break;
+		case SEARCH_USHORT:
+			if (c_num > PAGESIZE()/sizeof(short)) {
+				error(INFO, "WARNING: too many numbers and"
+					" -C option is ignored\n");
+				c_num = 0;
+			}
+			break;
+		case SEARCH_CHARS:
+			error(INFO, "-C option ignored on string search\n");
+			c_num = 0;
+			break;
+		}
+	}
+	searchinfo.c_num = c_num;
 		
 	searchinfo.vcnt = 0;
 	while (args[optind]) {
@@ -12013,6 +12063,72 @@ cmd_search(void)
 
 #define SEARCHMASK(X) ((X) | mask) 
 
+static void
+display_with_pre_and_post(void *bufptr, ulonglong addr, struct searchinfo *si)
+{
+	int c_num, memtype, t, amount;
+	ulonglong addr_d;
+	ulong flag;
+	char buf[BUFSIZE];
+
+	c_num = si->c_num;
+	memtype = si->memtype;
+	flag = HEXADECIMAL|NO_ERROR|ASCII_ENDLINE;
+
+	switch (si->mode)
+	{
+	case SEARCH_USHORT:
+		t = sizeof(ushort);
+		break;
+	case SEARCH_UINT:
+		t = sizeof(uint);
+		break;
+	case SEARCH_ULONG:
+	default:
+		t = sizeof(ulong);
+		break;
+	}
+
+	switch (t)
+	{
+	case 8:
+		flag |= DISPLAY_64;
+		break;
+	case 4:
+		flag |= DISPLAY_32;
+		break;
+	case 2:
+		flag |= DISPLAY_16;
+		break;
+	}
+
+	amount = c_num * t;
+	addr_d = addr - amount < 0 ? 0 : addr - amount;
+
+	display_memory(addr_d, c_num, flag, memtype, NULL);
+
+	BZERO(buf, BUFSIZE);
+	fprintf(fp, "%s:  ", mkstring(buf, VADDR_PRLEN,
+				RJUST|LONGLONG_HEX, MKSTR(&addr)));
+
+	switch(si->mode)
+	{
+	case SEARCH_ULONG:
+		fprintf(fp, "%lx\n", *((ulong *)bufptr));
+		break;
+	case SEARCH_UINT:
+		fprintf(fp, "%x\n", *((uint *)bufptr));
+		break;
+	case SEARCH_USHORT:
+		fprintf(fp, "%x\n", *((ushort *)bufptr));
+		break;
+	}
+
+	addr_d = addr + t;
+	display_memory(addr_d, c_num, flag, memtype, NULL);
+	fprintf(fp, "\n");
+}
+
 static ulong
 search_ulong(ulong *bufptr, ulong addr, int longcnt, struct searchinfo *si)
 {
@@ -12020,8 +12136,12 @@ search_ulong(ulong *bufptr, ulong addr, int longcnt, struct searchinfo *si)
 	ulong mask = si->s_parms.s_ulong.mask;
 	for (i = 0; i < longcnt; i++, bufptr++, addr += sizeof(long)) {
 		for (j = 0; j < si->vcnt; j++) {
-			if (SEARCHMASK(*bufptr) == SEARCHMASK(si->s_parms.s_ulong.value[j]))
-				fprintf(fp, "%lx: %lx\n", addr, *bufptr);
+			if (SEARCHMASK(*bufptr) == SEARCHMASK(si->s_parms.s_ulong.value[j])) {
+				if (si->c_num)
+					display_with_pre_and_post(bufptr, addr, si);
+				else
+					fprintf(fp, "%lx: %lx\n", addr, *bufptr);
+			}
                 }
 	}
 	return addr;
@@ -12035,8 +12155,12 @@ search_ulong_p(ulong *bufptr, ulonglong addr, int longcnt, struct searchinfo *si
 	ulong mask = si->s_parms.s_ulong.mask;
 	for (i = 0; i < longcnt; i++, bufptr++, addr += sizeof(long)) {
 		for (j = 0; j < si->vcnt; j++) {
-			if (SEARCHMASK(*bufptr) == SEARCHMASK(si->s_parms.s_ulong.value[j]))
-				fprintf(fp, "%llx: %lx\n", addr, *bufptr);
+			if (SEARCHMASK(*bufptr) == SEARCHMASK(si->s_parms.s_ulong.value[j])) {
+				if (si->c_num)
+					display_with_pre_and_post(bufptr, addr, si);
+				else
+					fprintf(fp, "%llx: %lx\n", addr, *bufptr);
+			}
                 }
 	}
 	return addr;
@@ -12052,8 +12176,12 @@ search_uint(ulong *bufptr, ulong addr, int longcnt, struct searchinfo *si)
 
 	for (i = 0; i < cnt; i++, ptr++, addr += sizeof(int)) {
 		for (j = 0; j < si->vcnt; j++) {
-			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_uint.value[j]))
-				fprintf(fp, "%lx: %x\n", addr, *ptr);
+			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_uint.value[j])) {
+				if (si->c_num)
+					display_with_pre_and_post(ptr, addr, si);
+				else
+					fprintf(fp, "%lx: %x\n", addr, *ptr);
+			}
                 }
 	}
 	return addr;
@@ -12070,8 +12198,12 @@ search_uint_p(ulong *bufptr, ulonglong addr, int longcnt, struct searchinfo *si)
 
 	for (i = 0; i < cnt; i++, ptr++, addr += sizeof(int)) {
 		for (j = 0; j < si->vcnt; j++) {
-			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_uint.value[j]))
-				fprintf(fp, "%llx: %x\n", addr, *ptr);
+			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_uint.value[j])) {
+				if (si->c_num)
+					display_with_pre_and_post(ptr, addr, si);
+				else
+					fprintf(fp, "%llx: %x\n", addr, *ptr);
+			}
                 }
 	}
 	return addr;
@@ -12087,8 +12219,12 @@ search_ushort(ulong *bufptr, ulong addr, int longcnt, struct searchinfo *si)
 
 	for (i = 0; i < cnt; i++, ptr++, addr += sizeof(short)) {
 		for (j = 0; j < si->vcnt; j++) {
-			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_ushort.value[j]))
-				fprintf(fp, "%lx: %x\n", addr, *ptr);
+			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_ushort.value[j])) {
+				if (si->c_num)
+					display_with_pre_and_post(ptr, addr, si);
+				else
+					fprintf(fp, "%lx: %x\n", addr, *ptr);
+			}
                 }
 	}
 	return addr;
@@ -12105,8 +12241,12 @@ search_ushort_p(ulong *bufptr, ulonglong addr, int longcnt, struct searchinfo *s
 
 	for (i = 0; i < cnt; i++, ptr++, addr += sizeof(short)) {
 		for (j = 0; j < si->vcnt; j++) {
-			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_ushort.value[j]))
-				fprintf(fp, "%llx: %x\n", addr, *ptr);
+			if (SEARCHMASK(*ptr) == SEARCHMASK(si->s_parms.s_ushort.value[j])) {
+				if (si->c_num)
+					display_with_pre_and_post(ptr, addr, si);
+				else
+					fprintf(fp, "%llx: %x\n", addr, *ptr);
+			}
                 }
 	}
 	return addr;
-- 
1.7.1

--
Crash-utility mailing list
Crash-utility@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/crash-utility

[Index of Archives]     [Fedora Development]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [KDE Users]     [Fedora Tools]

 

Powered by Linux