The qemu_plugin_{read,write} register API previously was inconsistent
with regard to its docstring (where a return value of both -1 and 0
would indicate an error) and to the memory read/write APIs, which
already return a boolean value to indicate success or failure.
Returning the number of bytes read or written is superfluous, as the
GByteArray* passed to the API functions already encodes the length.
See the linked thread for more details.
This patch moves from returning an int (number of bytes read/written) to
returning a bool from the register read/write API, bumps the plugin API
version, and adjusts plugins and tests accordingly.
Signed-off-by: Florian Hofhammer <florian.hofhammer@fhofhammer.de>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Link: https://lore.kernel.org/qemu-devel/f877dd79-1285-4752-811e-f0d430ff27fe@fhofhammer.de
Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
{
for (int n = 0; n < cpu->registers->len; n++) {
Register *reg = cpu->registers->pdata[n];
- int sz;
+ bool success = false;
+ int sz = 0;
g_byte_array_set_size(reg->new, 0);
- sz = qemu_plugin_read_register(reg->handle, reg->new);
- g_assert(sz > 0);
+ success = qemu_plugin_read_register(reg->handle, reg->new);
+ g_assert(success);
+ sz = reg->new->len;
g_assert(sz == reg->last->len);
if (memcmp(reg->last->data, reg->new->data, sz)) {
{
Register *reg = g_new0(Register, 1);
g_autofree gchar *lower = g_utf8_strdown(desc->name, -1);
- int r;
+ bool success = false;
reg->handle = desc->handle;
reg->name = g_intern_string(lower);
reg->new = g_byte_array_new();
/* read the initial value */
- r = qemu_plugin_read_register(reg->handle, reg->last);
- g_assert(r > 0);
+ success = qemu_plugin_read_register(reg->handle, reg->last);
+ g_assert(success);
return reg;
}
{
GByteArray *buf = cpu->buf;
g_byte_array_set_size(buf, 0);
- size_t sz = qemu_plugin_read_register(reg, buf);
- g_assert(sz == 8);
+ bool success = qemu_plugin_read_register(reg, buf);
+ g_assert(success);
g_assert(buf->len == 8);
return *((uint64_t *) buf->data);
}
{
GByteArray *buf = cpu->buf;
g_byte_array_set_size(buf, 0);
- size_t sz = qemu_plugin_read_register(reg, buf);
- g_assert(sz == 4);
+ bool success = qemu_plugin_read_register(reg, buf);
+ g_assert(success);
g_assert(buf->len == 4);
return *((uint32_t *) buf->data);
}
* - added qemu_plugin_write_memory_hwaddr
* - added qemu_plugin_write_register
* - added qemu_plugin_translate_vaddr
+ *
+ * version 6:
+ * - changed return value of qemu_plugin_{read,write}_register from int to bool
*/
extern QEMU_PLUGIN_EXPORT int qemu_plugin_version;
-#define QEMU_PLUGIN_VERSION 5
+#define QEMU_PLUGIN_VERSION 6
/**
* struct qemu_info_t - system information for plugins
* qemu_plugin_register_vcpu_init_cb(), except for callbacks registered with
* qemu_plugin_register_atexit_cb() and qemu_plugin_register_flush_cb().
*
- * Returns the size of the read register. The content of @buf is in target byte
- * order. On failure returns -1.
+ * Returns true on success, false on failure. The content of @buf is in target
+ * byte order.
*/
QEMU_PLUGIN_API
-int qemu_plugin_read_register(struct qemu_plugin_register *handle,
- GByteArray *buf);
+bool qemu_plugin_read_register(struct qemu_plugin_register *handle,
+ GByteArray *buf);
/**
* qemu_plugin_write_register() - write register for current vCPU
* Attempting to write a register with @buf smaller than the register size
* will result in a crash or other undesired behavior.
*
- * Returns the number of bytes written. On failure returns 0.
+ * Returns true on sucess, false on failure.
*/
QEMU_PLUGIN_API
-int qemu_plugin_write_register(struct qemu_plugin_register *handle,
- GByteArray *buf);
+bool qemu_plugin_write_register(struct qemu_plugin_register *handle,
+ GByteArray *buf);
/**
* qemu_plugin_read_memory_vaddr() - read from memory using a virtual address
return create_register_handles(regs);
}
-int qemu_plugin_read_register(struct qemu_plugin_register *reg, GByteArray *buf)
+bool qemu_plugin_read_register(struct qemu_plugin_register *reg,
+ GByteArray *buf)
{
g_assert(current_cpu);
if (qemu_plugin_get_cb_flags() == QEMU_PLUGIN_CB_NO_REGS) {
- return -1;
+ return false;
}
- return gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1);
+ return (gdb_read_register(current_cpu, buf, GPOINTER_TO_INT(reg) - 1) > 0);
}
-int qemu_plugin_write_register(struct qemu_plugin_register *reg,
- GByteArray *buf)
+bool qemu_plugin_write_register(struct qemu_plugin_register *reg,
+ GByteArray *buf)
{
g_assert(current_cpu);
if (buf->len == 0 || qemu_plugin_get_cb_flags() != QEMU_PLUGIN_CB_RW_REGS) {
- return -1;
+ return false;
}
- return gdb_write_register(current_cpu, buf->data, GPOINTER_TO_INT(reg) - 1);
+ return (gdb_write_register(current_cpu, buf->data, GPOINTER_TO_INT(reg) - 1) > 0);
}
bool qemu_plugin_read_memory_vaddr(uint64_t addr, GByteArray *data, size_t len)
for (int i = 0; i < reg_list->len; i++) {
qemu_plugin_reg_descriptor *rd = &g_array_index(
reg_list, qemu_plugin_reg_descriptor, i);
- int count = qemu_plugin_read_register(rd->handle, reg_value);
- g_assert(count > 0);
+ bool success = qemu_plugin_read_register(rd->handle, reg_value);
+ g_assert(success);
}
}
}