Skip to contentSkip to conversation

monitor: add memory search commands s, sp

RFC Applied [email protected] 5 participants 11 messages May 18, 2015 – Jun 12, 2015

v6 of 7 Applied Latest is v7 →

lore
Share & tools
lore
#1 lore raw

RFC v6 1/2 util: add memmem replacement function

In reply to [email protected]

From: Claudio Fontana <[email protected]>

if the memmem function is missing, provide a trivial replacement.

---
 configure            | 15 +++++++++++++
 include/qemu/osdep.h |  4 ++++
 util/Makefile.objs   |  1 +
 util/memmem.c        | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 82 insertions(+)
 create mode 100644 util/memmem.c
configure +15 -0
index 1f0f485..feb55b1 100755
@@ -3078,6 +3078,17 @@ if compile_prog "" "" ; then
 fi
 
 ##########################################
+# memmem probe
+cat > $TMPC <<EOF
+#include <string.h>
+int main(int argc, char *argv[]) { return memmem(argv[0], 0, argv[0], 0) != argv[0]; }
+EOF
+memmem=no
+if compile_prog "" "" ; then
+  memmem=yes
+fi
+
+##########################################
 # fdt probe
 # fdt support is mandatory for at least some target architectures,
 # so insist on it if we're building those system emulators.
@@ -4431,6 +4442,7 @@ echo "RDMA support      $rdma"
 echo "TCG interpreter   $tcg_interpreter"
 echo "fdt support       $fdt"
 echo "preadv support    $preadv"
+echo "memmem support    $memmem"
 echo "fdatasync         $fdatasync"
 echo "madvise           $madvise"
 echo "posix_madvise     $posix_madvise"
@@ -4780,6 +4792,9 @@ fi
 if test "$preadv" = "yes" ; then
   echo "CONFIG_PREADV=y" >> $config_host_mak
 fi
+if test "$memmem" = "yes" ; then
+  echo "CONFIG_MEMMEM=y" >> $config_host_mak
+fi
 if test "$fdt" = "yes" ; then
   echo "CONFIG_FDT=y" >> $config_host_mak
 fi
include/qemu/osdep.h +4 -0
index b3300cc..d74ddff 100644
@@ -201,6 +201,10 @@ ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
 #include <sys/uio.h>
 #endif
 
+#ifndef CONFIG_MEMMEM
+void *memmem(const void *hay, size_t hay_len, const void *s, size_t s_len);
+#endif /* !CONFIG_MEMMEM */
+
 #ifdef _WIN32
 static inline void qemu_timersub(const struct timeval *val1,
                                  const struct timeval *val2,
util/Makefile.objs +1 -0
index ceaba30..628242f 100644
@@ -1,6 +1,7 @@
 util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o
 util-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o event_notifier-win32.o
 util-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o event_notifier-posix.o qemu-openpty.o
+util-obj-$(call lnot,$(CONFIG_MEMMEM)) += memmem.o
 util-obj-y += envlist.o path.o module.o
 util-obj-$(call lnot,$(CONFIG_INT128)) += host-utils.o
 util-obj-y += bitmap.o bitops.o hbitmap.o
util/memmem.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#3 lore raw

RFC v6 2/2 monitor: add memory search commands s, sp

In reply to [email protected]

From: Claudio Fontana <[email protected]>

usage is similar to the commands x, xp.

Example with string: looking for "ELF" header in memory:

(qemu) s/1000000cb 0x40001000 "ELF"
searching memory area [0000000040001000-00000000400f5240] 0000000040090001
(qemu) x/20b 0x40090000
0000000040090000: '\x7f' 'E' 'L' 'F' '\x02' '\x01' '\x01' '\x03' 0000000040090008: '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' '\x00' 0000000040090010: '\x02' '\x00' '\xb7' '\x00'

Example with value: looking for 64bit variable value 0x990088

(qemu) s/1000000xg 0xffff900042000000 0x990088 searching memory area [ffff900042000000-ffff9000427a1200] ffff9000424b3000
ffff9000424c1000

---
 hmp-commands.hx |  28 ++++++++++++
 monitor.c       | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 168 insertions(+)
hmp-commands.hx +28 -0
index e864a6c..badf1f5 100644
@@ -430,6 +430,34 @@ Start gdbserver session (default @var{port}=1234)
 ETEXI
 
     {
+        .name       = "s",
+        .args_type  = "fmt:/,addr:l,data:s",
+        .params     = "/fmt addr data",
+        .help       = "search virtual memory starting at 'addr' for 'data'",
+        .mhandler.cmd = hmp_memory_search,
+    },
+
+STEXI
+@item s/fmt @var{addr} @var{data}
+@findex s
+Virtual memory search starting at @var{addr} for data described by @var{data}.
+ETEXI
+
+    {
+        .name       = "sp",
+        .args_type  = "fmt:/,addr:l,data:s",
+        .params     = "/fmt addr data",
+        .help       = "search physical memory starting at 'addr' for 'data'",
+        .mhandler.cmd = hmp_physical_memory_search,
+    },
+
+STEXI
+@item sp/fmt @var{addr} @var{data}
+@findex sp
+Physical memory search starting at @var{addr} for data described by @var{data}.
+ETEXI
+
+    {
         .name       = "x",
         .args_type  = "fmt:/,addr:l",
         .params     = "/fmt addr",
monitor.c +140 -0

152 lines not on this page · raw message

Signature · 1 line
1.8.5.3
#0 lore raw

v6 cover monitor: add memory search commands s, sp

From: Claudio Fontana <[email protected]>

This is the latest iteration of the memory search patch, including a trivial replacement for the memmem function for systems which don't provide one (notably Windows).

It detects the presence of memmem in configure and sets CONFIG_MEMMEM, providing a trivial implementation for the !CONFIG_MEMMEM case.

The new code is MIT licensed, following usage of other files in the same directory dealing with replacement functions (osdep, oslib, getauxval etc), and to maximize reusability.

I have tested this in both CONFIG_MEMMEM defined/undefined scenarios, but more feedback and testing is welcome of course.

changes from v5:
dropped the import from gnulib and implemented a trivial replacement.

changes from v4:
made into a series of two patches.
Introduced a memmem replacement function (import from gnulib) and detection code in configure.

changes from v3:
initialize pointer variable to NULL to finally get rid of spurious warning

changes from v2:
move code to try to address spurious warning

changes from v1:
make checkpatch happy by adding braces here and there.

Claudio Fontana (2):
util: add memmem replacement function
monitor: add memory search commands s, sp

 configure            |  15 ++++++
 hmp-commands.hx      |  28 +++++++++++
 include/qemu/osdep.h |   4 ++
 monitor.c            | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++
 util/Makefile.objs   |   1 +
 util/memmem.c        |  62 +++++++++++++++++++++++
 6 files changed, 250 insertions(+)
 create mode 100644 util/memmem.c
Signature · 1 line
1.8.5.3
Reviewed-by on v6 1/2
#2 lore raw

Re: [Qemu-devel] [RFC v6 1/2] util: add memmem replacement function

In reply to [email protected]
Quoted 14 lines · On 05/18/2015 05:22 AM, [email protected] wrote:

From: Claudio Fontana <[email protected]>

if the memmem function is missing, provide a trivial replacement.

---
 configure            | 15 +++++++++++++
 include/qemu/osdep.h |  4 ++++
 util/Makefile.objs   |  1 +
 util/memmem.c        | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 82 insertions(+)
 create mode 100644 util/memmem.c

util/memmem.c line 56

Quoted 6 lines
util/memmem.c new file +6 -0
+    if (s_len == 1) {
+        return memchr(hay, s[0], hay_len);
+    }
+
+    for (; hay <= last; hay++) {
+        if (hay[0] == s[0] && memcmp(hay, s, s_len) == 0) {

An obvious optimization would be:

if (hay[0] == s[0] && memcmp(hay + 1, s + 1, s_len - 1) == 0)

since you already compared the first byte and know that the needle is more than one byte.

But it's not worth the churn; this version is sufficient for the job as-is.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw
In reply to [email protected]
Quoted 30 lines · On Mon, 18 May 2015 13:22:16 +0200 [email protected] wrote:

From: Claudio Fontana <[email protected]>

This is the latest iteration of the memory search patch, including a trivial replacement for the memmem function for systems which don't provide one (notably Windows).

It detects the presence of memmem in configure and sets CONFIG_MEMMEM, providing a trivial implementation for the !CONFIG_MEMMEM case.

The new code is MIT licensed, following usage of other files in the same directory dealing with replacement functions (osdep, oslib, getauxval etc), and to maximize reusability.

I have tested this in both CONFIG_MEMMEM defined/undefined scenarios, but more feedback and testing is welcome of course.

changes from v5:
dropped the import from gnulib and implemented a trivial replacement.

changes from v4:
made into a series of two patches.
Introduced a memmem replacement function (import from gnulib) and detection code in configure.

changes from v3:
initialize pointer variable to NULL to finally get rid of spurious warning

changes from v2:
move code to try to address spurious warning

Quoted 7 lines

changes from v1:
make checkpatch happy by adding braces here and there.

Claudio Fontana (2):
util: add memmem replacement function
monitor: add memory search commands s, sp

Applied to the qmp branch, thanks.

Quoted 10 lines
 configure            |  15 ++++++
 hmp-commands.hx      |  28 +++++++++++
 include/qemu/osdep.h |   4 ++
 monitor.c            | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++
 util/Makefile.objs   |   1 +
 util/memmem.c        |  62 +++++++++++++++++++++++
 6 files changed, 250 insertions(+)
 create mode 100644 util/memmem.c
#5 lore raw
Quoted 37 lines · On Thu, 28 May 2015 16:18:41 -0400 Luiz Capitulino <[email protected]>…
Quoted 34 lines · On Mon, 18 May 2015 13:22:16 +0200 [email protected] wrote:

From: Claudio Fontana <[email protected]>

This is the latest iteration of the memory search patch, including a trivial replacement for the memmem function for systems which don't provide one (notably Windows).

It detects the presence of memmem in configure and sets CONFIG_MEMMEM, providing a trivial implementation for the !CONFIG_MEMMEM case.

The new code is MIT licensed, following usage of other files in the same directory dealing with replacement functions (osdep, oslib, getauxval etc), and to maximize reusability.

I have tested this in both CONFIG_MEMMEM defined/undefined scenarios, but more feedback and testing is welcome of course.

changes from v5:
dropped the import from gnulib and implemented a trivial replacement.

changes from v4:
made into a series of two patches.
Introduced a memmem replacement function (import from gnulib) and detection code in configure.

changes from v3:
initialize pointer variable to NULL to finally get rid of spurious warning

changes from v2:
move code to try to address spurious warning

changes from v1:
make checkpatch happy by adding braces here and there.

Quoted 5 lines
Quoted 3 lines

Claudio Fontana (2):
util: add memmem replacement function
monitor: add memory search commands s, sp

Applied to the qmp branch, thanks.

Unfortunately, I'm quite busy and won't have time to push this through my tree. Markus is going to pick up this series soon.

Quoted 12 lines
Quoted 11 lines

 configure            |  15 ++++++
 hmp-commands.hx      |  28 +++++++++++
 include/qemu/osdep.h |   4 ++
 monitor.c            | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++
 util/Makefile.objs   |   1 +
 util/memmem.c        |  62 +++++++++++++++++++++++
 6 files changed, 250 insertions(+)
 create mode 100644 util/memmem.c
#6 lore raw
Quoted 44 lines · Luiz Capitulino
Quoted 41 lines · On Thu, 28 May 2015 16:18:41 -0400 Luiz Capitulino <[email protected]>…
Quoted 37 lines · On Mon, 18 May 2015 13:22:16 +0200 [email protected] wrote:

From: Claudio Fontana <[email protected]>

This is the latest iteration of the memory search patch, including a trivial replacement for the memmem function for systems which don't provide one (notably Windows).

It detects the presence of memmem in configure and sets CONFIG_MEMMEM, providing a trivial implementation for the !CONFIG_MEMMEM case.

The new code is MIT licensed, following usage of other files in the same directory dealing with replacement functions (osdep, oslib, getauxval etc), and to maximize reusability.

I have tested this in both CONFIG_MEMMEM defined/undefined scenarios, but more feedback and testing is welcome of course.

changes from v5:
dropped the import from gnulib and implemented a trivial replacement.

changes from v4:
made into a series of two patches.
Introduced a memmem replacement function (import from gnulib) and detection code in configure.

changes from v3:
initialize pointer variable to NULL to finally get rid of spurious warning

changes from v2:
move code to try to address spurious warning

changes from v1:
make checkpatch happy by adding braces here and there.

Claudio Fontana (2):
util: add memmem replacement function
monitor: add memory search commands s, sp

Quoted 7 lines
Quoted 1 line

Applied to the qmp branch, thanks.

Unfortunately, I'm quite busy and won't have time to push this through my tree. Markus is going to pick up this series soon.

This series is marked RFC. Is it intended for merging anyway?

Semantic conflict with
[PATCH v2 0/2] monitor+disas: Remove uses of ENV_GET_CPU needs to be resolved:

CC x86_64-softmmu/monitor.o
/work/armbru/qemu/monitor.c: In function ‘memory_search’:
/work/armbru/qemu/monitor.c:1222:9: warning: passing argument 1 of ‘x86_env_get_cpu’ from incompatible pointer type [enabled by default]

         } else if (cpu_memory_rw_debug(ENV_GET_CPU(mon_get_cpu()), addr,
         ^

In file included from /work/armbru/qemu/target-i386/cpu.h:982:0,
from /work/armbru/qemu/include/qemu-common.h:124,
from /work/armbru/qemu/include/hw/hw.h:5,
from /work/armbru/qemu/monitor.c:25:
/work/armbru/qemu/target-i386/cpu-qom.h:119:23: note: expected ‘struct CPUX86State *’ but argument is of type ‘struct CPUState *’
static inline X86CPU *x86_env_get_cpu(CPUX86State *env)

                       ^
#7 lore raw
Quoted 52 lines · Markus Armbruster
Quoted 50 lines · Luiz Capitulino
Quoted 37 lines · On Thu, 28 May 2015 16:18:41 -0400 Luiz Capitulino <[email protected]>…
Quoted 34 lines · On Mon, 18 May 2015 13:22:16 +0200 [email protected] wrote:

From: Claudio Fontana <[email protected]>

This is the latest iteration of the memory search patch, including a trivial replacement for the memmem function for systems which don't provide one (notably Windows).

It detects the presence of memmem in configure and sets CONFIG_MEMMEM, providing a trivial implementation for the !CONFIG_MEMMEM case.

The new code is MIT licensed, following usage of other files in the same directory dealing with replacement functions (osdep, oslib, getauxval etc), and to maximize reusability.

I have tested this in both CONFIG_MEMMEM defined/undefined scenarios, but more feedback and testing is welcome of course.

changes from v5:
dropped the import from gnulib and implemented a trivial replacement.

changes from v4:
made into a series of two patches.
Introduced a memmem replacement function (import from gnulib) and detection code in configure.

changes from v3:
initialize pointer variable to NULL to finally get rid of spurious warning

changes from v2:
move code to try to address spurious warning

changes from v1:
make checkpatch happy by adding braces here and there.

Quoted 5 lines
Quoted 3 lines

Claudio Fontana (2):
util: add memmem replacement function
monitor: add memory search commands s, sp

Applied to the qmp branch, thanks.

Unfortunately, I'm quite busy and won't have time to push this through my tree. Markus is going to pick up this series soon.

Quoted 7 lines
Quoted 1 line

This series is marked RFC. Is it intended for merging anyway?

Semantic conflict with
[PATCH v2 0/2] monitor+disas: Remove uses of ENV_GET_CPU needs to be resolved:

Hello Markus,

the two series conflict, but the resolution is quite simple.
I would suggest applying the "Remove uses of ENV_GET_CPU" stuff first, and then fixing up my patch, I can do it for you if you need.

Thanks!

Claudio

Quoted 14 lines

CC x86_64-softmmu/monitor.o
/work/armbru/qemu/monitor.c: In function ‘memory_search’:
/work/armbru/qemu/monitor.c:1222:9: warning: passing argument 1 of ‘x86_env_get_cpu’ from incompatible pointer type [enabled by default]

         } else if (cpu_memory_rw_debug(ENV_GET_CPU(mon_get_cpu()), addr,
         ^

In file included from /work/armbru/qemu/target-i386/cpu.h:982:0,
from /work/armbru/qemu/include/qemu-common.h:124,
from /work/armbru/qemu/include/hw/hw.h:5,
from /work/armbru/qemu/monitor.c:25:
/work/armbru/qemu/target-i386/cpu-qom.h:119:23: note: expected ‘struct CPUX86State *’ but argument is of type ‘struct CPUState *’
static inline X86CPU *x86_env_get_cpu(CPUX86State *env)

                       ^
Signature · 4 lines
Claudio Fontana
Server Virtualization Architect
Huawei Technologies Duesseldorf GmbH
Riesstraße 25 - 80992 München
#8 lore raw
Quoted 7 lines · Claudio Fontana
Quoted 5 lines · Markus Armbruster

This series is marked RFC. Is it intended for merging anyway?

Semantic conflict with
[PATCH v2 0/2] monitor+disas: Remove uses of ENV_GET_CPU needs to be resolved:

Quoted 5 lines

Hello Markus,

the two series conflict, but the resolution is quite simple. I would suggest applying the "Remove uses of ENV_GET_CPU" stuff first, and then fixing up my patch, I can do it for you if you need.

Please do, and make sure to drop the RFC on your respin if you want it merged.

#9 lore raw

Hello Markus,

the merge conflict resolution is the following:

-----

monitor.c +0 -0
index 4a5ac4e..fc6e15b 100644
@@ -1219,7 +1219,7 @@ static void memory_search(Monitor *mon, int

count, int format, int wsize,

         }
         if (is_physical) {
             cpu_physical_memory_read(addr, hay, l);
-        } else if (cpu_memory_rw_debug(ENV_GET_CPU(mon_get_cpu()), addr,
+        } else if (cpu_memory_rw_debug(mon_get_cpu(), addr,
                                        (uint8_t *)hay, l, 0) < 0) {
             monitor_printf(mon, " Cannot access memory\n");
             break;

-----

You can also fetch this from the following repo and branch:

https://github.com/hw-claudio/qemu-aarch64-queue "memsearch_merge_conflict"

Or do you want me to respin based on an uncommited state of master + "remove ENV_GET_CPU" series?

Thanks,

Claudio

Quoted 17 lines · Markus Armbruster
Quoted 7 lines · Claudio Fontana
Quoted 5 lines · Markus Armbruster

This series is marked RFC. Is it intended for merging anyway?

Semantic conflict with
[PATCH v2 0/2] monitor+disas: Remove uses of ENV_GET_CPU needs to be resolved:

Quoted 5 lines

Hello Markus,

the two series conflict, but the resolution is quite simple. I would suggest applying the "Remove uses of ENV_GET_CPU" stuff first, and then fixing up my patch, I can do it for you if you need.

Please do, and make sure to drop the RFC on your respin if you want it merged.

#10 lore raw
Quoted 8 lines · Claudio Fontana

Hello Markus,

the merge conflict resolution is the following:

Or do you want me to respin based on an uncommited state of master + "remove ENV_GET_CPU" series?

Probably easiest to just post a v7 with RFC dropped and all resolution performed.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org