Skip to content

Commit f4ce632

Browse files
committed
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/ Signed-off-by: Rob Norris <robn@despairlabs.com>
1 parent 8846154 commit f4ce632

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

lib/libzfs_core/libzfs_core.c

Lines changed: 36 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,36 @@ 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 errno=%d\n",
204+
ioc, zc->zc_name[0] ? zc->zc_name : "[none]", errno);
205+
if (!rc && zc->zc_nvlist_dst) {
206+
nvl = fnvlist_unpack(
207+
(void *)(uintptr_t)zc->zc_nvlist_dst,
208+
zc->zc_nvlist_dst_size);
209+
nvlist_print(stderr, nvl);
210+
fnvlist_free(nvl);
211+
}
212+
213+
errno = err;
214+
return (rc);
180215
}
181216

182217
static int

0 commit comments

Comments
 (0)