python: Expose blockPeek and memoryPeek in Python binding

A simple example to show how to use it:

\#! /usr/bin/python

import os
import sys
import libvirt

disk = "/var/lib/libvirt/images/test.img"

conn = libvirt.open(None)
dom = conn.lookupByName('test')

mem_contents = dom.memoryPeek(0, 32, libvirt.VIR_MEMORY_VIRTUAL);
sys.stdout.write(mem_contents)

% python test.py | hexdump
0000000 1660 0209 0000 0000 0000 0000 0000 0000
0000010 0000 0000 0000 0000 d3a0 01d0 0000 0000
0000020
This commit is contained in:
Osier Yang 2011-12-15 21:01:33 +08:00
parent 6948b725e7
commit d758e0cb64
3 changed files with 91 additions and 0 deletions

View File

@ -262,6 +262,7 @@ py_types = {
'unsigned char *': ('z', None, "charPtr", "char *"),
'char *': ('z', None, "charPtr", "char *"),
'const char *': ('z', None, "charPtrConst", "const char *"),
'size_t': ('n', None, "size_t", "size_t"),
'virDomainPtr': ('O', "virDomain", "virDomainPtr", "virDomainPtr"),
'const virDomainPtr': ('O', "virDomain", "virDomainPtr", "virDomainPtr"),

View File

@ -405,5 +405,22 @@
<arg name='flags' type='unsigned int' info='an OR&apos;ed set of virDomainModificationImpact'/>
<return type='int' info='0 in case of success, -1 in case of failure'/>
</function>
<function name='virDomainBlockPeek' file='python'>
<info>Read the contents of domain's disk device</info>
<arg name='dom' type='virDomainPtr' info='pointer to the domain'/>
<arg name='disk' type='const char *' info='disk name'/>
<arg name='offset' type='unsigned long long' info='offset within block device'/>
<arg name='size' type='size_t' info='size to read'/>
<arg name='flags' type='unsigned int' info='unused, always pass 0'/>
<return type='char *' info='the returned buffer or None in case of error'/>
</function>
<function name='virDomainMemoryPeek' file='python'>
<info>Read the contents of domain's memory</info>
<arg name='dom' type='virDomainPtr' info='pointer to the domain'/>
<arg name='start' type='unsigned long long' info='start of memory to peek'/>
<arg name='size' type='size_t' info='size of memory to peek'/>
<arg name='flags' type='unsigned int' info='an OR&apos;ed set of virDomainMemoryFlags'/>
<return type='char *' info='the returned buffer or None in case of error'/>
</function>
</symbols>
</api>

View File

@ -5017,6 +5017,77 @@ libvirt_virDomainMigrateGetMaxSpeed(PyObject *self ATTRIBUTE_UNUSED, PyObject *a
return(py_retval);
}
static PyObject *
libvirt_virDomainBlockPeek(PyObject *self ATTRIBUTE_UNUSED,
PyObject *args) {
PyObject *py_retval = NULL;
int c_retval;
virDomainPtr domain;
PyObject *pyobj_domain;
const char *disk;
unsigned long long offset;
size_t size;
char *buf;
unsigned int flags;
if (!PyArg_ParseTuple(args, (char *)"OzLni:virDomainBlockPeek", &pyobj_domain,
&disk, &offset, &size, &flags))
return(NULL);
domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
if ((buf = malloc(size)) == NULL)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virDomainBlockPeek(domain, disk, offset, size, buf, flags);
LIBVIRT_END_ALLOW_THREADS;
if (c_retval < 0)
goto cleanup;
py_retval = PyString_FromStringAndSize(buf, size);
cleanup:
free(buf);
return py_retval;
}
static PyObject *
libvirt_virDomainMemoryPeek(PyObject *self ATTRIBUTE_UNUSED,
PyObject *args) {
PyObject *py_retval = NULL;
int c_retval;
virDomainPtr domain;
PyObject *pyobj_domain;
unsigned long long start;
size_t size;
char *buf;
unsigned int flags;
if (!PyArg_ParseTuple(args, (char *)"OLni:virDomainMemoryPeek", &pyobj_domain,
&start, &size, &flags))
return(NULL);
domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
if ((buf = malloc(size)) == NULL)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virDomainMemoryPeek(domain, start, size, buf, flags);
LIBVIRT_END_ALLOW_THREADS;
if (c_retval < 0)
goto cleanup;
py_retval = PyString_FromStringAndSize(buf, size);
cleanup:
free(buf);
return py_retval;
}
/************************************************************************
* *
* The registration stuff *
@ -5113,6 +5184,8 @@ static PyMethodDef libvirtMethods[] = {
{(char *) "virDomainGetBlockIoTune", libvirt_virDomainGetBlockIoTune, METH_VARARGS, NULL},
{(char *) "virDomainSendKey", libvirt_virDomainSendKey, METH_VARARGS, NULL},
{(char *) "virDomainMigrateGetMaxSpeed", libvirt_virDomainMigrateGetMaxSpeed, METH_VARARGS, NULL},
{(char *) "virDomainBlockPeek", libvirt_virDomainBlockPeek, METH_VARARGS, NULL},
{(char *) "virDomainMemoryPeek", libvirt_virDomainMemoryPeek, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};