Skip to content

Commit 79482e5

Browse files
bozutafvivier
authored andcommitted
linux-user: Add strace support for printing arguments of ioctl()
This patch implements functionality for strace argument printing for ioctls. When running ioctls through qemu with "-strace", they get printed in format: "ioctl(fd_num,0x*,0x*) = ret_value" where the request code an the ioctl's third argument get printed in a hexadicemal format. This patch changes that by enabling strace to print both the request code name and the contents of the third argument. For example, when running ioctl RTC_SET_TIME with "-strace", with changes from this patch, it gets printed in this way: "ioctl(3,RTC_SET_TIME,{12,13,15,20,10,119,0,0,0}) = 0" In case of IOC_R type ioctls, the contents of the third argument get printed after the return value, and the argument inside the ioctl call gets printed as pointer in hexadecimal format. For example, when running RTC_RD_TIME with "-strace", with changes from this patch, it gets printed in this way: "ioctl(3,RTC_RD_TIME,0x40800374) = 0 ({22,9,13,11,5,120,0,0,0})" In case of IOC_RW type ioctls, the contents of the third argument get printed both inside the ioctl call and after the return value. Implementation notes: Functions "print_ioctl()" and "print_syscall_ret_ioctl()", that are defined in "strace.c", are listed in file "strace.list" as "call" and "result" value for ioctl. Structure definition "IOCTLEntry" as well as predefined values for IOC_R, IOC_W and IOC_RW were cut and pasted from file "syscall.c" to file "qemu.h" so that they can be used by these functions to print the contents of the third ioctl argument. Also, the "static" identifier for array "ioctl_entries[]" was removed and this array was declared as "extern" in "qemu.h" so that it can also be used by these functions. To decode the structure type of the ioctl third argument, function "thunk_print()" was defined in file "thunk.c" and its definition is somewhat simillar to that of function "thunk_convert()". Signed-off-by: Filip Bozuta <[email protected]> Reviewed-by: Laurent Vivier <[email protected]> Message-Id: <[email protected]> [lv: fix close-bracket] Signed-off-by: Laurent Vivier <[email protected]>
1 parent a20a7c2 commit 79482e5

File tree

6 files changed

+285
-20
lines changed

6 files changed

+285
-20
lines changed

include/exec/user/thunk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void thunk_register_struct_direct(int id, const char *name,
7373
const StructEntry *se1);
7474
const argtype *thunk_convert(void *dst, const void *src,
7575
const argtype *type_ptr, int to_host);
76+
const argtype *thunk_print(void *arg, const argtype *type_ptr);
7677

7778
extern StructEntry *struct_entries;
7879

linux-user/qemu.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,26 @@ struct linux_binprm {
184184
int (*core_dump)(int, const CPUArchState *); /* coredump routine */
185185
};
186186

187+
typedef struct IOCTLEntry IOCTLEntry;
188+
189+
typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
190+
int fd, int cmd, abi_long arg);
191+
192+
struct IOCTLEntry {
193+
int target_cmd;
194+
unsigned int host_cmd;
195+
const char *name;
196+
int access;
197+
do_ioctl_fn *do_ioctl;
198+
const argtype arg_type[5];
199+
};
200+
201+
extern IOCTLEntry ioctl_entries[];
202+
203+
#define IOC_R 0x0001
204+
#define IOC_W 0x0002
205+
#define IOC_RW (IOC_R | IOC_W)
206+
187207
void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
188208
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
189209
abi_ulong stringp, int push_ptr);

linux-user/strace.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,44 @@ print_syscall_ret_listxattr(const struct syscallname *name, abi_long ret,
860860
#define print_syscall_ret_flistxattr print_syscall_ret_listxattr
861861
#endif
862862

863+
#ifdef TARGET_NR_ioctl
864+
static void
865+
print_syscall_ret_ioctl(const struct syscallname *name, abi_long ret,
866+
abi_long arg0, abi_long arg1, abi_long arg2,
867+
abi_long arg3, abi_long arg4, abi_long arg5)
868+
{
869+
print_syscall_err(ret);
870+
871+
if (ret >= 0) {
872+
qemu_log(TARGET_ABI_FMT_ld, ret);
873+
874+
const IOCTLEntry *ie;
875+
const argtype *arg_type;
876+
void *argptr;
877+
int target_size;
878+
879+
for (ie = ioctl_entries; ie->target_cmd != 0; ie++) {
880+
if (ie->target_cmd == arg1) {
881+
break;
882+
}
883+
}
884+
885+
if (ie->target_cmd == arg1 &&
886+
(ie->access == IOC_R || ie->access == IOC_RW)) {
887+
arg_type = ie->arg_type;
888+
qemu_log(" (");
889+
arg_type++;
890+
target_size = thunk_type_size(arg_type, 0);
891+
argptr = lock_user(VERIFY_READ, arg2, target_size, 1);
892+
thunk_print(argptr, arg_type);
893+
unlock_user(argptr, arg2, target_size);
894+
qemu_log(")");
895+
}
896+
}
897+
qemu_log("\n");
898+
}
899+
#endif
900+
863901
UNUSED static struct flags access_flags[] = {
864902
FLAG_GENERIC(F_OK),
865903
FLAG_GENERIC(R_OK),
@@ -3026,6 +3064,75 @@ print_statx(const struct syscallname *name,
30263064
}
30273065
#endif
30283066

3067+
#ifdef TARGET_NR_ioctl
3068+
static void
3069+
print_ioctl(const struct syscallname *name,
3070+
abi_long arg0, abi_long arg1, abi_long arg2,
3071+
abi_long arg3, abi_long arg4, abi_long arg5)
3072+
{
3073+
print_syscall_prologue(name);
3074+
print_raw_param("%d", arg0, 0);
3075+
3076+
const IOCTLEntry *ie;
3077+
const argtype *arg_type;
3078+
void *argptr;
3079+
int target_size;
3080+
3081+
for (ie = ioctl_entries; ie->target_cmd != 0; ie++) {
3082+
if (ie->target_cmd == arg1) {
3083+
break;
3084+
}
3085+
}
3086+
3087+
if (ie->target_cmd == 0) {
3088+
print_raw_param("%#x", arg1, 0);
3089+
print_raw_param("%#x", arg2, 1);
3090+
} else {
3091+
qemu_log("%s", ie->name);
3092+
arg_type = ie->arg_type;
3093+
3094+
if (arg_type[0] != TYPE_NULL) {
3095+
qemu_log(",");
3096+
3097+
switch (arg_type[0]) {
3098+
case TYPE_PTRVOID:
3099+
print_pointer(arg2, 1);
3100+
break;
3101+
case TYPE_CHAR:
3102+
case TYPE_SHORT:
3103+
case TYPE_INT:
3104+
print_raw_param("%d", arg2, 1);
3105+
break;
3106+
case TYPE_LONG:
3107+
print_raw_param(TARGET_ABI_FMT_ld, arg2, 1);
3108+
break;
3109+
case TYPE_ULONG:
3110+
print_raw_param(TARGET_ABI_FMT_lu, arg2, 1);
3111+
break;
3112+
case TYPE_PTR:
3113+
switch (ie->access) {
3114+
case IOC_R:
3115+
print_pointer(arg2, 1);
3116+
break;
3117+
case IOC_W:
3118+
case IOC_RW:
3119+
arg_type++;
3120+
target_size = thunk_type_size(arg_type, 0);
3121+
argptr = lock_user(VERIFY_READ, arg2, target_size, 1);
3122+
thunk_print(argptr, arg_type);
3123+
unlock_user(argptr, arg2, target_size);
3124+
break;
3125+
}
3126+
break;
3127+
default:
3128+
g_assert_not_reached();
3129+
}
3130+
}
3131+
}
3132+
print_syscall_epilogue(name);
3133+
}
3134+
#endif
3135+
30293136
/*
30303137
* An array of all of the syscalls we know about
30313138
*/

linux-user/strace.list

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@
433433
{ TARGET_NR_io_cancel, "io_cancel" , NULL, NULL, NULL },
434434
#endif
435435
#ifdef TARGET_NR_ioctl
436-
{ TARGET_NR_ioctl, "ioctl" , "%s(%d,%#x,%#x)", NULL, NULL },
436+
{ TARGET_NR_ioctl, "ioctl" , NULL, print_ioctl,
437+
print_syscall_ret_ioctl},
437438
#endif
438439
#ifdef TARGET_NR_io_destroy
439440
{ TARGET_NR_io_destroy, "io_destroy" , NULL, NULL, NULL },

linux-user/syscall.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4481,24 +4481,6 @@ STRUCT_MAX
44814481
#undef STRUCT
44824482
#undef STRUCT_SPECIAL
44834483

4484-
typedef struct IOCTLEntry IOCTLEntry;
4485-
4486-
typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
4487-
int fd, int cmd, abi_long arg);
4488-
4489-
struct IOCTLEntry {
4490-
int target_cmd;
4491-
unsigned int host_cmd;
4492-
const char *name;
4493-
int access;
4494-
do_ioctl_fn *do_ioctl;
4495-
const argtype arg_type[5];
4496-
};
4497-
4498-
#define IOC_R 0x0001
4499-
#define IOC_W 0x0002
4500-
#define IOC_RW (IOC_R | IOC_W)
4501-
45024484
#define MAX_STRUCT_SIZE 4096
45034485

45044486
#ifdef CONFIG_FIEMAP
@@ -5374,7 +5356,7 @@ static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
53745356

53755357
#endif
53765358

5377-
static IOCTLEntry ioctl_entries[] = {
5359+
IOCTLEntry ioctl_entries[] = {
53785360
#define IOCTL(cmd, access, ...) \
53795361
{ TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
53805362
#define IOCTL_SPECIAL(cmd, access, dofn, ...) \

thunk.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,160 @@ const argtype *thunk_convert(void *dst, const void *src,
271271
return type_ptr;
272272
}
273273

274+
const argtype *thunk_print(void *arg, const argtype *type_ptr)
275+
{
276+
int type;
277+
278+
type = *type_ptr++;
279+
280+
switch (type) {
281+
case TYPE_CHAR:
282+
qemu_log("%c", *(uint8_t *)arg);
283+
break;
284+
case TYPE_SHORT:
285+
qemu_log("%" PRId16, tswap16(*(uint16_t *)arg));
286+
break;
287+
case TYPE_INT:
288+
qemu_log("%" PRId32, tswap32(*(uint32_t *)arg));
289+
break;
290+
case TYPE_LONGLONG:
291+
qemu_log("%" PRId64, tswap64(*(uint64_t *)arg));
292+
break;
293+
case TYPE_ULONGLONG:
294+
qemu_log("%" PRIu64, tswap64(*(uint64_t *)arg));
295+
break;
296+
#if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
297+
case TYPE_PTRVOID:
298+
qemu_log("0x%" PRIx32, tswap32(*(uint32_t *)arg));
299+
break;
300+
case TYPE_LONG:
301+
qemu_log("%" PRId32, tswap32(*(uint32_t *)arg));
302+
break;
303+
case TYPE_ULONG:
304+
qemu_log("%" PRIu32, tswap32(*(uint32_t *)arg));
305+
break;
306+
#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
307+
case TYPE_PTRVOID:
308+
qemu_log("0x%" PRIx32, tswap32(*(uint64_t *)arg & 0xffffffff));
309+
break;
310+
case TYPE_LONG:
311+
qemu_log("%" PRId32, tswap32(*(uint64_t *)arg & 0xffffffff));
312+
break;
313+
case TYPE_ULONG:
314+
qemu_log("%" PRIu32, tswap32(*(uint64_t *)arg & 0xffffffff));
315+
break;
316+
#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
317+
case TYPE_PTRVOID:
318+
qemu_log("0x%" PRIx64, tswap64(*(uint64_t *)arg));
319+
break;
320+
case TYPE_LONG:
321+
qemu_log("%" PRId64, tswap64(*(uint64_t *)arg));
322+
break;
323+
case TYPE_ULONG:
324+
qemu_log("%" PRIu64, tswap64(*(uint64_t *)arg));
325+
break;
326+
#else
327+
case TYPE_PTRVOID:
328+
qemu_log("0x%" PRIx64, tswap64(*(uint64_t *)arg));
329+
break;
330+
case TYPE_LONG:
331+
qemu_log("%" PRId64, tswap64(*(uint64_t *)arg));
332+
break;
333+
case TYPE_ULONG:
334+
qemu_log("%" PRIu64, tswap64(*(uint64_t *)arg));
335+
break;
336+
#endif
337+
case TYPE_OLDDEVT:
338+
{
339+
uint64_t val = 0;
340+
switch (thunk_type_size(type_ptr - 1, 1)) {
341+
case 2:
342+
val = *(uint16_t *)arg;
343+
break;
344+
case 4:
345+
val = *(uint32_t *)arg;
346+
break;
347+
case 8:
348+
val = *(uint64_t *)arg;
349+
break;
350+
}
351+
switch (thunk_type_size(type_ptr - 1, 0)) {
352+
case 2:
353+
qemu_log("%" PRIu16, tswap16(val));
354+
break;
355+
case 4:
356+
qemu_log("%" PRIu32, tswap32(val));
357+
break;
358+
case 8:
359+
qemu_log("%" PRIu64, tswap64(val));
360+
break;
361+
}
362+
}
363+
break;
364+
case TYPE_ARRAY:
365+
{
366+
int i, array_length, arg_size;
367+
uint8_t *a;
368+
int is_string = 0;
369+
370+
array_length = *type_ptr++;
371+
arg_size = thunk_type_size(type_ptr, 0);
372+
a = arg;
373+
374+
if (*type_ptr == TYPE_CHAR) {
375+
qemu_log("\"");
376+
is_string = 1;
377+
} else {
378+
qemu_log("[");
379+
}
380+
381+
for (i = 0; i < array_length; i++) {
382+
if (i > 0 && !is_string) {
383+
qemu_log(",");
384+
}
385+
thunk_print(a, type_ptr);
386+
a += arg_size;
387+
}
388+
389+
if (is_string) {
390+
qemu_log("\"");
391+
} else {
392+
qemu_log("]");
393+
}
394+
395+
type_ptr = thunk_type_next(type_ptr);
396+
}
397+
break;
398+
case TYPE_STRUCT:
399+
{
400+
int i;
401+
const StructEntry *se;
402+
uint8_t *a;
403+
const argtype *field_types;
404+
const int *arg_offsets;
405+
406+
se = struct_entries + *type_ptr++;
407+
a = arg;
408+
409+
field_types = se->field_types;
410+
arg_offsets = se->field_offsets[0];
411+
412+
qemu_log("{");
413+
for (i = 0; i < se->nb_fields; i++) {
414+
if (i > 0) {
415+
qemu_log(",");
416+
}
417+
field_types = thunk_print(a + arg_offsets[i], field_types);
418+
}
419+
qemu_log("}");
420+
}
421+
break;
422+
default:
423+
g_assert_not_reached();
424+
}
425+
return type_ptr;
426+
}
427+
274428
/* from em86 */
275429

276430
/* Utility function: Table-driven functions to translate bitmasks

0 commit comments

Comments
 (0)