Skip to content

Commit b9f5227

Browse files
robntonyhutter
authored andcommitted
lzc_ioctl_fd: add ZFS_IOC_TRACE envvar to enable ioctl tracing
When set, dumps all ZFS ioctl calls and returns and their nvlists to STDERR, to make debugging and understanding a lot easier. Sponsored-by: https://despairlabs.com/sponsor/ Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Alexander Motin <[email protected]> Signed-off-by: Rob Norris <[email protected]> Closes #17344 (cherry picked from commit a387b75)
1 parent b7d1426 commit b9f5227

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

lib/libzfs_core/libzfs_core.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ static int g_fd = -1;
102102
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
103103
static int g_refcount;
104104

105+
static int g_ioc_trace = 0;
106+
105107
#ifdef ZFS_DEBUG
106108
static zfs_ioc_t fail_ioc_cmd = ZFS_IOC_LAST;
107109
static zfs_errno_t fail_ioc_err;
@@ -154,6 +156,10 @@ libzfs_core_init(void)
154156
#ifdef ZFS_DEBUG
155157
libzfs_core_debug_ioc();
156158
#endif
159+
160+
if (getenv("ZFS_IOC_TRACE"))
161+
g_ioc_trace = 1;
162+
157163
(void) pthread_mutex_unlock(&g_lock);
158164
return (0);
159165
}
@@ -176,7 +182,37 @@ libzfs_core_fini(void)
176182
int
177183
lzc_ioctl_fd(int fd, unsigned long ioc, zfs_cmd_t *zc)
178184
{
179-
return (lzc_ioctl_fd_os(fd, ioc, zc));
185+
if (!g_ioc_trace)
186+
return (lzc_ioctl_fd_os(fd, ioc, zc));
187+
188+
nvlist_t *nvl;
189+
190+
fprintf(stderr, "=== lzc_ioctl: call: ioc=0x%lx name=%s\n",
191+
ioc, zc->zc_name[0] ? zc->zc_name : "[none]");
192+
if (zc->zc_nvlist_src) {
193+
nvl = fnvlist_unpack(
194+
(void *)(uintptr_t)zc->zc_nvlist_src,
195+
zc->zc_nvlist_src_size);
196+
nvlist_print(stderr, nvl);
197+
fnvlist_free(nvl);
198+
}
199+
200+
int rc = lzc_ioctl_fd_os(fd, ioc, zc);
201+
int err = errno;
202+
203+
fprintf(stderr, "=== lzc_ioctl: result: ioc=0x%lx name=%s "
204+
"rc=%d errno=%d\n", ioc, zc->zc_name[0] ? zc->zc_name : "[none]",
205+
rc, (rc < 0 ? err : 0));
206+
if (rc >= 0 && zc->zc_nvlist_dst) {
207+
nvl = fnvlist_unpack(
208+
(void *)(uintptr_t)zc->zc_nvlist_dst,
209+
zc->zc_nvlist_dst_size);
210+
nvlist_print(stderr, nvl);
211+
fnvlist_free(nvl);
212+
}
213+
214+
errno = err;
215+
return (rc);
180216
}
181217

182218
static int

0 commit comments

Comments
 (0)