/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=8:tabstop=8:
*
- * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
+ * GPL HEADER START
*
- * This file is part of Portals, http://www.sf.net/projects/lustre/
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
- * Portals is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
*
- * Portals is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
*
- * You should have received a copy of the GNU General Public License
- * along with Portals; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
*
- * Some day I'll split all of this functionality into a cfs_debug module
- * of its own. That day is not today.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
*
+ * GPL HEADER END
+ */
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lnet/utils/debug.c
+ * Some day I'll split all of this functionality into a cfs_debug module
+ * of its own. That day is not today.
*/
#define __USE_FILE_OFFSET64
-
-#include <stdio.h>
-#include <netdb.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <time.h>
-#ifndef __CYGWIN__
-# include <syscall.h>
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
#endif
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-
-#include <linux/version.h>
+#include <libcfs/libcfsutil.h>
+#include <lnet/lnetctl.h>
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-#define BUG() /* workaround for module.h includes */
-#include <linux/module.h>
-#endif
-
-#include <portals/api-support.h>
-#include <portals/ptlctl.h>
-#include "parser.h"
static char rawbuf[8192];
static char *buf = rawbuf;
static int max = 8192;
-//static int g_pfd = -1;
+/*static int g_pfd = -1;*/
static int subsystem_mask = ~0;
static int debug_mask = ~0;
-#define MAX_MARK_SIZE 100
-
-static const char *portal_debug_subsystems[] =
- {"undefined", "mdc", "mds", "osc", "ost", "class", "log", "llite",
- "rpc", "mgmt", "portals", "libcfs", "socknal", "qswnal", "pinger",
- "filter", "ptlbd", "echo", "ldlm", "lov", "gmnal", "router", "cobd",
- "ibnal", "lmv", "smfs", "cmobd", NULL};
-static const char *portal_debug_masks[] =
- {"trace", "inode", "super", "ext2", "malloc", "cache", "info", "ioctl",
- "blocks", "net", "warning", "buffs", "other", "dentry", "portals",
- "page", "dlmtrace", "error", "emerg", "ha", "rpctrace", "vfstrace",
- "reada", NULL};
-
-struct debug_daemon_cmd {
- char *cmd;
- unsigned int cmdv;
-};
+#define MAX_MARK_SIZE 256
+
+static const char *libcfs_debug_subsystems[] =
+ {"undefined", "mdc", "mds", "osc",
+ "ost", "class", "log", "llite",
+ "rpc", "mgmt", "lnet", "lnd",
+ "pinger", "filter", "", "echo",
+ "ldlm", "lov", "lquota", "",
+ "", "", "", "lmv",
+ "", "sec", "gss", "",
+ "mgc", "mgs", "fid", "fld", NULL};
+static const char *libcfs_debug_masks[] =
+ {"trace", "inode", "super", "ext2",
+ "malloc", "cache", "info", "ioctl",
+ "neterror", "net", "warning", "buffs",
+ "other", "dentry", "nettrace", "page",
+ "dlmtrace", "error", "emerg", "ha",
+ "rpctrace", "vfstrace", "reada", "mmap",
+ "config", "console", "quota", "sec", NULL};
+
+#ifdef __linux__
+
+#define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
+#define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
+#define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
+#define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
+
+static int
+dbg_open_ctlhandle(const char *str)
+{
+ int fd;
+ fd = open(str, O_WRONLY);
+ if (fd < 0) {
+ fprintf(stderr, "open %s failed: %s\n", str,
+ strerror(errno));
+ return -1;
+ }
+ return fd;
+}
-static const struct debug_daemon_cmd portal_debug_daemon_cmd[] = {
- {"start", DEBUG_DAEMON_START},
- {"stop", DEBUG_DAEMON_STOP},
- {"pause", DEBUG_DAEMON_PAUSE},
- {"continue", DEBUG_DAEMON_CONTINUE},
- {0, 0}
-};
+static void
+dbg_close_ctlhandle(int fd)
+{
+ close(fd);
+}
+
+static int
+dbg_write_cmd(int fd, char *str, int len)
+{
+ int rc = write(fd, str, len);
+
+ return (rc == len ? 0 : 1);
+}
+
+#elif defined(__DARWIN__)
+
+#define DAEMON_CTL_NAME "lnet.trace_daemon"
+#define SUBSYS_DEBUG_CTL_NAME "lnet.subsystem_debug"
+#define DEBUG_CTL_NAME "lnet.debug"
+#define DUMP_KERNEL_CTL_NAME "lnet.trace_dumpkernel"
+
+static char sysctl_name[128];
+static int
+dbg_open_ctlhandle(const char *str)
+{
+
+ if (strlen(str)+1 > 128) {
+ fprintf(stderr, "sysctl name is too long: %s.\n", str);
+ return -1;
+ }
+ strcpy(sysctl_name, str);
+
+ return 0;
+}
+
+static void
+dbg_close_ctlhandle(int fd)
+{
+ sysctl_name[0] = '\0';
+ return;
+}
+
+static int
+dbg_write_cmd(int fd, char *str, int len)
+{
+ int rc;
+
+ rc = sysctlbyname(sysctl_name, NULL, NULL, str, len+1);
+ if (rc != 0) {
+ fprintf(stderr, "sysctl %s with cmd (%s) error: %d\n",
+ sysctl_name, str, errno);
+ }
+ return (rc == 0 ? 0: 1);
+}
+
+#elif defined(__WINNT__)
+
+#define DAEMON_CTL_NAME "/proc/sys/lnet/daemon_file"
+#define SUBSYS_DEBUG_CTL_NAME "/proc/sys/lnet/subsystem_debug"
+#define DEBUG_CTL_NAME "/proc/sys/lnet/debug"
+#define DUMP_KERNEL_CTL_NAME "/proc/sys/lnet/dump_kernel"
+
+static int
+dbg_open_ctlhandle(const char *str)
+{
+ int fd;
+ fd = cfs_proc_open((char *)str, (int)O_WRONLY);
+ if (fd < 0) {
+ fprintf(stderr, "open %s failed: %s\n", str,
+ strerror(errno));
+ return -1;
+ }
+ return fd;
+}
+
+static void
+dbg_close_ctlhandle(int fd)
+{
+ cfs_proc_close(fd);
+}
+
+static int
+dbg_write_cmd(int fd, char *str, int len)
+{
+ int rc = cfs_proc_write(fd, str, len);
+
+ return (rc == len ? 0 : 1);
+}
+
+#else
+#error - Unknown sysctl convention.
+#endif
static int do_debug_mask(char *name, int enable)
{
int found = 0, i;
- for (i = 0; portal_debug_subsystems[i] != NULL; i++) {
- if (strcasecmp(name, portal_debug_subsystems[i]) == 0 ||
+ for (i = 0; libcfs_debug_subsystems[i] != NULL; i++) {
+ if (strcasecmp(name, libcfs_debug_subsystems[i]) == 0 ||
strcasecmp(name, "all_subs") == 0) {
printf("%s output from subsystem \"%s\"\n",
enable ? "Enabling" : "Disabling",
- portal_debug_subsystems[i]);
+ libcfs_debug_subsystems[i]);
if (enable)
subsystem_mask |= (1 << i);
else
found = 1;
}
}
- for (i = 0; portal_debug_masks[i] != NULL; i++) {
- if (strcasecmp(name, portal_debug_masks[i]) == 0 ||
+ for (i = 0; libcfs_debug_masks[i] != NULL; i++) {
+ if (strcasecmp(name, libcfs_debug_masks[i]) == 0 ||
strcasecmp(name, "all_types") == 0) {
printf("%s output of type \"%s\"\n",
enable ? "Enabling" : "Disabling",
- portal_debug_masks[i]);
+ libcfs_debug_masks[i]);
if (enable)
debug_mask |= (1 << i);
else
char buf[64];
int len = snprintf(buf, 64, "%d", value);
- int fd = open(procpath, O_WRONLY);
+ int fd = dbg_open_ctlhandle(procpath);
if (fd == -1) {
fprintf(stderr, "Unable to open %s: %s\n",
procpath, strerror(errno));
return fd;
}
- rc = write(fd, buf, len+1);
- if (rc<0) {
+ rc = dbg_write_cmd(fd, buf, len+1);
+ if (rc != 0) {
fprintf(stderr, "Write to %s failed: %s\n",
procpath, strerror(errno));
- return rc;
}
- close(fd);
- return 0;
+
+ dbg_close_ctlhandle(fd);
+
+ return rc;
}
static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
{
if (!dump_filename) {
- applymask("/proc/sys/portals/subsystem_debug", subs_mask);
- applymask("/proc/sys/portals/debug", debug_mask);
+ applymask(SUBSYS_DEBUG_CTL_NAME, subs_mask);
+ applymask(DEBUG_CTL_NAME, debug_mask);
} else {
- struct portals_debug_ioctl_data data;
+ struct libcfs_debug_ioctl_data data;
data.hdr.ioc_len = sizeof(data);
data.hdr.ioc_version = 0;
data.subs = subs_mask;
data.debug = debug_mask;
- dump(OBD_DEV_ID, PTL_IOC_DEBUG_MASK, &data);
+ dump(OBD_DEV_ID, LIBCFS_IOC_DEBUG_MASK, &data);
}
- printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/portals\n",
+ printf("Applied subsystem_debug=%d, debug=%d to /proc/sys/lnet\n",
subs_mask, debug_mask);
}
if (strcasecmp(argv[1], "subs") == 0) {
printf("Subsystems: all_subs");
- for (i = 0; portal_debug_subsystems[i] != NULL; i++)
- printf(", %s", portal_debug_subsystems[i]);
+ for (i = 0; libcfs_debug_subsystems[i] != NULL; i++)
+ if (libcfs_debug_subsystems[i][0])
+ printf(", %s", libcfs_debug_subsystems[i]);
printf("\n");
} else if (strcasecmp(argv[1], "types") == 0) {
printf("Types: all_types");
- for (i = 0; portal_debug_masks[i] != NULL; i++)
- printf(", %s", portal_debug_masks[i]);
+ for (i = 0; libcfs_debug_masks[i] != NULL; i++)
+ printf(", %s", libcfs_debug_masks[i]);
printf("\n");
} else if (strcasecmp(argv[1], "applymasks") == 0) {
applymask_all(subsystem_mask, debug_mask);
return 0;
}
-/* if 'raw' is true, don't strip the debug information from the front of the
- * lines */
-static void dump_buffer(FILE *fd, char *buf, int size, int raw)
+/* all strings nul-terminated; only the struct and hdr need to be freed */
+struct dbg_line {
+ struct ptldebug_header *hdr;
+ char *file;
+ char *fn;
+ char *text;
+};
+
+static int cmp_rec(const void *p1, const void *p2)
+{
+ struct dbg_line *d1 = *(struct dbg_line **)p1;
+ struct dbg_line *d2 = *(struct dbg_line **)p2;
+
+ if (d1->hdr->ph_sec < d2->hdr->ph_sec)
+ return -1;
+ if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
+ d1->hdr->ph_usec < d2->hdr->ph_usec)
+ return -1;
+ if (d1->hdr->ph_sec == d2->hdr->ph_sec &&
+ d1->hdr->ph_usec == d2->hdr->ph_usec)
+ return 0;
+ return 1;
+}
+
+static void print_rec(struct dbg_line ***linevp, int used, int fdout)
+{
+ struct dbg_line **linev = *linevp;
+ int i;
+
+ qsort(linev, used, sizeof(struct dbg_line *), cmp_rec);
+ for (i = 0; i < used; i++) {
+ struct dbg_line *line = linev[i];
+ struct ptldebug_header *hdr = line->hdr;
+ char out[4097];
+ int bytes;
+
+ bytes = sprintf(out, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
+ hdr->ph_subsys, hdr->ph_mask, hdr->ph_cpu_id,
+ hdr->ph_sec, (unsigned long long)hdr->ph_usec,
+ hdr->ph_stack, hdr->ph_pid, hdr->ph_extern_pid,
+ line->file, hdr->ph_line_num, line->fn, line->text);
+
+ write(fdout, out, bytes);
+ free(line->hdr);
+ free(line);
+ }
+ free(linev);
+ *linevp = NULL;
+}
+
+static int add_rec(struct dbg_line *line, struct dbg_line ***linevp, int *lenp,
+ int used)
{
- char *p, *z;
- unsigned long subsystem, debug, dropped = 0, kept = 0;
+ struct dbg_line **linev = *linevp;
+
+ if (used == *lenp) {
+ int nlen = *lenp + 4096;
+ int nsize = nlen * sizeof(struct dbg_line *);
+
+ linev = realloc(*linevp, nsize);
+ if (!linev)
+ return -ENOMEM;
+
+ *linevp = linev;
+ *lenp = nlen;
+ }
+ linev[used] = line;
- while (size) {
- p = memchr(buf, '\n', size);
- if (!p)
+ return 0;
+}
+
+static void dump_hdr(unsigned long long offset, struct ptldebug_header *hdr)
+{
+ fprintf(stderr, "badly-formed record at offset = %llu\n", offset);
+ fprintf(stderr, " len = %u\n", hdr->ph_len);
+ fprintf(stderr, " flags = %x\n", hdr->ph_flags);
+ fprintf(stderr, " subsystem = %x\n", hdr->ph_subsys);
+ fprintf(stderr, " mask = %x\n", hdr->ph_mask);
+ fprintf(stderr, " cpu_id = %u\n", hdr->ph_cpu_id);
+ fprintf(stderr, " seconds = %u\n", hdr->ph_sec);
+ fprintf(stderr, " microseconds = %lu\n", (long)hdr->ph_usec);
+ fprintf(stderr, " stack = %u\n", hdr->ph_stack);
+ fprintf(stderr, " pid = %u\n", hdr->ph_pid);
+ fprintf(stderr, " host pid = %u\n", hdr->ph_extern_pid);
+ fprintf(stderr, " line number = %u\n", hdr->ph_line_num);
+}
+
+#define HDR_SIZE sizeof(*hdr)
+
+static int parse_buffer(int fdin, int fdout)
+{
+ struct dbg_line *line;
+ struct ptldebug_header *hdr;
+ char buf[4097], *ptr;
+ unsigned long dropped = 0, kept = 0, bad = 0;
+ struct dbg_line **linev = NULL;
+ int linev_len = 0;
+ int rc;
+
+ hdr = (void *)buf;
+
+ while (1) {
+ int first_bad = 1;
+ int count;
+
+ count = HDR_SIZE;
+ ptr = buf;
+ readhdr:
+ rc = read(fdin, ptr, count);
+ if (rc <= 0)
+ goto print;
+
+ ptr += rc;
+ count -= rc;
+ if (count > 0)
+ goto readhdr;
+
+ if (hdr->ph_len > 4094 || /* is this header bogus? */
+ hdr->ph_cpu_id > 65536 ||
+ hdr->ph_stack > 65536 ||
+ hdr->ph_sec < (1 << 30) ||
+ hdr->ph_usec > 1000000000 ||
+ hdr->ph_line_num > 65536) {
+ if (first_bad)
+ dump_hdr(lseek(fdin, 0, SEEK_CUR), hdr);
+ bad += first_bad;
+ first_bad = 0;
+
+ /* try to restart on next line */
+ while (count < HDR_SIZE && buf[count] != '\n')
+ count++;
+ if (buf[count] == '\n')
+ count++; /* move past '\n' */
+ if (HDR_SIZE - count > 0) {
+ int left = HDR_SIZE - count;
+
+ memmove(buf, buf + count, left);
+ ptr = buf + left;
+
+ goto readhdr;
+ }
+
+ continue;
+ }
+
+ if (hdr->ph_len == 0)
+ continue;
+
+ count = hdr->ph_len - HDR_SIZE;
+ readmore:
+ rc = read(fdin, ptr, count);
+ if (rc <= 0)
break;
- subsystem = strtoul(buf, &z, 16);
- debug = strtoul(z + 1, &z, 16);
-
- z++;
- /* for some reason %*s isn't working. */
- *p = '\0';
- if ((subsystem_mask & subsystem) &&
- (!debug || (debug_mask & debug))) {
- if (raw)
- fprintf(fd, "%s\n", buf);
- else
- fprintf(fd, "%s\n", z);
- //printf("%s\n", buf);
- kept++;
- } else {
- //fprintf(stderr, "dropping line (%lx:%lx): %s\n", subsystem, debug, buf);
+
+ ptr += rc;
+ count -= rc;
+ if (count > 0)
+ goto readmore;
+
+ first_bad = 1;
+
+ if ((hdr->ph_subsys && !(subsystem_mask & hdr->ph_subsys)) ||
+ (hdr->ph_mask && !(debug_mask & hdr->ph_mask))) {
dropped++;
+ continue;
}
- *p = '\n';
- p++;
- size -= (p - buf);
- buf = p;
+
+ retry_alloc:
+ line = malloc(sizeof(*line));
+ if (line == NULL) {
+ if (linev) {
+ fprintf(stderr, "error: line malloc(%u): "
+ "printing accumulated records\n",
+ (unsigned int)sizeof(*line));
+ print_rec(&linev, kept, fdout);
+
+ goto retry_alloc;
+ }
+ fprintf(stderr, "error: line malloc(%u): exiting\n",
+ (unsigned int)sizeof(*line));
+ break;
+ }
+
+ line->hdr = malloc(hdr->ph_len + 1);
+ if (line->hdr == NULL) {
+ free(line);
+ if (linev) {
+ fprintf(stderr, "error: hdr malloc(%u): "
+ "printing accumulated records\n",
+ hdr->ph_len + 1);
+ print_rec(&linev, kept, fdout);
+
+ goto retry_alloc;
+ }
+ fprintf(stderr, "error: hdr malloc(%u): exiting\n",
+ hdr->ph_len + 1);
+ break;
+ }
+
+ ptr = (void *)line->hdr;
+ memcpy(line->hdr, buf, hdr->ph_len);
+ ptr[hdr->ph_len] = '\0';
+
+ ptr += sizeof(*hdr);
+ line->file = ptr;
+ ptr += strlen(line->file) + 1;
+ line->fn = ptr;
+ ptr += strlen(line->fn) + 1;
+ line->text = ptr;
+
+ retry_add:
+ if (add_rec(line, &linev, &linev_len, kept) < 0) {
+ if (linev) {
+ fprintf(stderr, "error: add_rec[%u] failed; "
+ "print accumulated records\n",
+ linev_len);
+ print_rec(&linev, kept, fdout);
+
+ goto retry_add;
+ }
+ fprintf(stderr, "error: add_rec[0] failed; exiting\n");
+ break;
+ }
+ kept++;
}
- printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
- dropped + kept, kept, dropped);
+print:
+ if (linev)
+ print_rec(&linev, kept, fdout);
+
+ printf("Debug log: %lu lines, %lu kept, %lu dropped, %lu bad.\n",
+ dropped + kept + bad, kept, dropped, bad);
+
+ return 0;
}
int jt_dbg_debug_kernel(int argc, char **argv)
{
- int rc, raw = 1;
- FILE *fd = stdout;
- const int databuf_size = (6 << 20);
- struct portal_ioctl_data data, *newdata;
- char *databuf = NULL;
+ char filename[4096];
+ struct stat st;
+ int raw = 0;
+ int save_errno;
+ int fdin;
+ int fdout;
+ int rc;
if (argc > 3) {
fprintf(stderr, "usage: %s [file] [raw]\n", argv[0]);
return 0;
}
- if (argc > 1) {
- fd = fopen(argv[1], "w");
- if (fd == NULL) {
- fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
- strerror(errno));
- return -1;
- }
- }
- if (argc > 2)
+ if (argc > 2) {
raw = atoi(argv[2]);
-
- databuf = malloc(databuf_size);
- if (!databuf) {
- fprintf(stderr, "No memory for buffer.\n");
- goto out;
+ } else if (argc > 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
+ raw = atoi(argv[1]);
+ argc--;
+ }
+
+ /* If we are dumping raw (which means no conversion step to ASCII)
+ * then dump directly to any supplied filename, otherwise this is
+ * just a temp file and we dump to the real file at convert time. */
+ if (argc > 1 && raw)
+ strcpy(filename, argv[1]);
+ else
+ sprintf(filename, "%s"CFS_TIME_T".%u",
+ DEBUG_FILE_PATH_DEFAULT, time(NULL), getpid());
+
+ if (stat(filename, &st) == 0 && S_ISREG(st.st_mode))
+ unlink(filename);
+
+ fdin = dbg_open_ctlhandle(DUMP_KERNEL_CTL_NAME);
+ if (fdin < 0) {
+ fprintf(stderr, "open(dump_kernel) failed: %s\n",
+ strerror(errno));
+ return 1;
}
- memset(&data, 0, sizeof(data));
- data.ioc_plen1 = databuf_size;
- data.ioc_pbuf1 = databuf;
-
- if (portal_ioctl_pack(&data, &buf, max) != 0) {
- fprintf(stderr, "portal_ioctl_pack failed.\n");
- goto out;
+ rc = dbg_write_cmd(fdin, filename, strlen(filename));
+ save_errno = errno;
+ dbg_close_ctlhandle(fdin);
+ if (rc != 0) {
+ fprintf(stderr, "write(%s) failed: %s\n", filename,
+ strerror(save_errno));
+ return 1;
}
- rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_DEBUG, buf);
- if (rc) {
- fprintf(stderr, "IOC_PORTAL_GET_DEBUG failed: %s\n",
+ if (raw)
+ return 0;
+
+ fdin = open(filename, O_RDONLY);
+ if (fdin < 0) {
+ if (errno == ENOENT) /* no dump file created */
+ return 0;
+ fprintf(stderr, "fopen(%s) failed: %s\n", filename,
strerror(errno));
- goto out;
+ return 1;
}
+ if (argc > 1) {
+ fdout = open(argv[1], O_WRONLY|O_CREAT);
+ if (fdout < 0) {
+ fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
+ strerror(errno));
+ close(fdin);
+ return 1;
+ }
+ } else {
+ fdout = fileno(stdout);
+ }
- newdata = (struct portal_ioctl_data *)buf;
- if (newdata->ioc_size > 0)
- dump_buffer(fd, databuf, newdata->ioc_size, raw);
+ rc = parse_buffer(fdin, fdout);
+ close(fdin);
+ if (argc > 1)
+ close(fdout);
+ if (rc) {
+ fprintf(stderr, "parse_buffer failed; leaving tmp file %s "
+ "behind.\n", filename);
+ } else {
+ rc = unlink(filename);
+ if (rc)
+ fprintf(stderr, "dumped successfully, but couldn't "
+ "unlink tmp file %s: %s\n", filename,
+ strerror(errno));
+ }
- out:
- if (databuf)
- free(databuf);
- if (fd != stdout)
- fclose(fd);
- return 0;
+ return rc;
}
-int jt_dbg_debug_daemon(int argc, char **argv)
+int jt_dbg_debug_file(int argc, char **argv)
{
- int i, rc;
- unsigned int cmd = 0;
- FILE *fd = stdout;
- struct portal_ioctl_data data;
+ int fdin;
+ int fdout;
+ int rc;
- if (argc <= 1) {
- fprintf(stderr, "usage: %s [start file <#MB>|stop|pause|"
- "continue]\n", argv[0]);
+ if (argc > 3 || argc < 2) {
+ fprintf(stderr, "usage: %s <input> [output]\n", argv[0]);
return 0;
}
- for (i = 0; portal_debug_daemon_cmd[i].cmd != NULL; i++) {
- if (strcasecmp(argv[1], portal_debug_daemon_cmd[i].cmd) == 0) {
- cmd = portal_debug_daemon_cmd[i].cmdv;
- break;
- }
- }
- if (portal_debug_daemon_cmd[i].cmd == NULL) {
- fprintf(stderr, "usage: %s [start file <#MB>|stop|pause|"
- "continue]\n", argv[0]);
- return 0;
- }
- memset(&data, 0, sizeof(data));
- if (cmd == DEBUG_DAEMON_START) {
- if (argc < 3) {
- fprintf(stderr, "usage: %s [start file <#MB>|stop|"
- "pause|continue]\n", argv[0]);
- return 0;
- }
- if (access(argv[2], F_OK) != 0) {
- fd = fopen(argv[2], "w");
- if (fd != NULL) {
- fclose(fd);
- remove(argv[2]);
- goto ok;
- }
- }
- if (access(argv[2], W_OK) == 0)
- goto ok;
- fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
+
+ fdin = open(argv[1], O_RDONLY | O_LARGEFILE);
+ if (fdin < 0) {
+ fprintf(stderr, "open(%s) failed: %s\n", argv[1],
strerror(errno));
- return -1;
-ok:
- data.ioc_inllen1 = strlen(argv[2]) + 1;
- data.ioc_inlbuf1 = argv[2];
- data.ioc_misc = 0;
- if (argc == 4) {
- unsigned long size;
- errno = 0;
- size = strtoul(argv[3], NULL, 0);
- if (errno) {
- fprintf(stderr, "file size(%s): error %s\n",
- argv[3], strerror(errno));
- return -1;
- }
- data.ioc_misc = size;
- }
- }
- data.ioc_count = cmd;
- if (portal_ioctl_pack(&data, &buf, max) != 0) {
- fprintf(stderr, "portal_ioctl_pack failed.\n");
- return -1;
- }
- rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_SET_DAEMON, buf);
- if (rc < 0) {
- fprintf(stderr, "IOC_PORTAL_SET_DEMON failed: %s\n",
+ return 1;
+ }
+ if (argc > 2) {
+ fdout = open(argv[2],
+ O_CREAT | O_TRUNC | O_WRONLY | O_LARGEFILE,
+ 0600);
+ if (fdout < 0) {
+ fprintf(stderr, "open(%s) failed: %s\n", argv[2],
strerror(errno));
- return rc;
+ close(fdin);
+ return 1;
+ }
+ } else {
+ fdout = fileno(stdout);
}
- return 0;
+
+ rc = parse_buffer(fdin, fdout);
+
+ close(fdin);
+ if (fdout != fileno(stdout))
+ close(fdout);
+
+ return rc;
}
-int jt_dbg_debug_file(int argc, char **argv)
+const char debug_daemon_usage[] = "usage: %s {start file [MB]|stop}\n";
+
+int jt_dbg_debug_daemon(int argc, char **argv)
{
- int rc, fd = -1, raw = 1;
- FILE *output = stdout;
- char *databuf = NULL;
- struct stat statbuf;
+ int rc;
+ int fd;
- if (argc > 4 || argc < 2) {
- fprintf(stderr, "usage: %s <input> [output] [raw]\n", argv[0]);
- return 0;
+ if (argc <= 1) {
+ fprintf(stderr, debug_daemon_usage, argv[0]);
+ return 1;
}
- fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "fopen(%s) failed: %s\n", argv[1],
- strerror(errno));
+ fd = dbg_open_ctlhandle(DAEMON_CTL_NAME);
+ if (fd < 0)
return -1;
- }
- rc = fstat(fd, &statbuf);
- if (rc < 0) {
- fprintf(stderr, "fstat failed: %s\n", strerror(errno));
+ rc = -1;
+ if (strcasecmp(argv[1], "start") == 0) {
+ if (argc < 3 || argc > 4 ||
+ (argc == 4 && strlen(argv[3]) > 5)) {
+ fprintf(stderr, debug_daemon_usage, argv[0]);
+ goto out;
+ }
+ if (argc == 4) {
+ char buf[12];
+ const long min_size = 10;
+ const long max_size = 20480;
+ long size;
+ char *end;
+
+ size = strtoul(argv[3], &end, 0);
+ if (size < min_size ||
+ size > max_size ||
+ *end != 0) {
+ fprintf(stderr, "size %s invalid, must be in "
+ "the range %ld-%ld MB\n", argv[3],
+ min_size, max_size);
+ goto out;
+ }
+ snprintf(buf, sizeof(buf), "size=%ld", size);
+ rc = dbg_write_cmd(fd, buf, strlen(buf));
+
+ if (rc != 0) {
+ fprintf(stderr, "set %s failed: %s\n",
+ buf, strerror(errno));
+ goto out;
+ }
+ }
+
+ rc = dbg_write_cmd(fd, argv[2], strlen(argv[2]));
+ if (rc != 0) {
+ fprintf(stderr, "start debug_daemon on %s failed: %s\n",
+ argv[2], strerror(errno));
+ goto out;
+ }
+ rc = 0;
goto out;
}
-
- if (argc >= 3) {
- output = fopen(argv[2], "w");
- if (output == NULL) {
- fprintf(stderr, "fopen(%s) failed: %s\n", argv[2],
+ if (strcasecmp(argv[1], "stop") == 0) {
+ rc = dbg_write_cmd(fd, "stop", 4);
+ if (rc != 0) {
+ fprintf(stderr, "stopping debug_daemon failed: %s\n",
strerror(errno));
goto out;
}
- }
-
- if (argc == 4)
- raw = atoi(argv[3]);
- databuf = mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE,
- MAP_PRIVATE, fd, 0);
- if (databuf == NULL) {
- fprintf(stderr, "mmap failed: %s\n", strerror(errno));
+ rc = 0;
goto out;
}
- dump_buffer(output, databuf, statbuf.st_size, raw);
-
- out:
- if (databuf)
- munmap(databuf, statbuf.st_size);
- if (output != stdout)
- fclose(output);
- if (fd > 0)
- close(fd);
- return 0;
+ fprintf(stderr, debug_daemon_usage, argv[0]);
+ rc = -1;
+out:
+ dbg_close_ctlhandle(fd);
+ return rc;
}
int jt_dbg_clear_debug_buf(int argc, char **argv)
{
int rc;
- struct portal_ioctl_data data;
+ struct libcfs_ioctl_data data;
if (argc != 1) {
fprintf(stderr, "usage: %s\n", argv[0]);
}
memset(&data, 0, sizeof(data));
- if (portal_ioctl_pack(&data, &buf, max) != 0) {
- fprintf(stderr, "portal_ioctl_pack failed.\n");
+ if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
+ fprintf(stderr, "libcfs_ioctl_pack failed.\n");
return -1;
}
- rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_CLEAR_DEBUG, buf);
+ rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLEAR_DEBUG, buf);
if (rc) {
- fprintf(stderr, "IOC_PORTAL_CLEAR_DEBUG failed: %s\n",
+ fprintf(stderr, "IOC_LIBCFS_CLEAR_DEBUG failed: %s\n",
strerror(errno));
return -1;
}
int jt_dbg_mark_debug_buf(int argc, char **argv)
{
+ static char scratch[MAX_MARK_SIZE] = { '\0' };
int rc, max_size = MAX_MARK_SIZE-1;
- struct portal_ioctl_data data;
+ struct libcfs_ioctl_data data = { 0 };
char *text;
time_t now = time(NULL);
if (argc > 1) {
- int counter;
- text = malloc(MAX_MARK_SIZE);
+ int count;
+ text = scratch;
strncpy(text, argv[1], max_size);
max_size-=strlen(argv[1]);
- for(counter = 2; (counter < argc) && (max_size > 0) ; counter++){
- strncat(text, " ", 1);
- max_size-=1;
- strncat(text, argv[counter], max_size);
- max_size-=strlen(argv[counter]);
+ for (count = 2; (count < argc) && (max_size > 0); count++){
+ strncat(text, " ", max_size);
+ max_size -= 1;
+ strncat(text, argv[count], max_size);
+ max_size -= strlen(argv[count]);
}
} else {
text = ctime(&now);
- text[strlen(text) - 1] = '\0'; /* stupid \n */
- }
- if (!max_size) {
- text[MAX_MARK_SIZE - 1] = '\0';
}
- memset(&data, 0, sizeof(data));
data.ioc_inllen1 = strlen(text) + 1;
data.ioc_inlbuf1 = text;
- if (portal_ioctl_pack(&data, &buf, max) != 0) {
- fprintf(stderr, "portal_ioctl_pack failed.\n");
+ if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
+ fprintf(stderr, "libcfs_ioctl_pack failed.\n");
return -1;
}
- rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MARK_DEBUG, buf);
+ rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MARK_DEBUG, buf);
if (rc) {
- fprintf(stderr, "IOC_PORTAL_MARK_DEBUG failed: %s\n",
+ fprintf(stderr, "IOC_LIBCFS_MARK_DEBUG failed: %s\n",
strerror(errno));
return -1;
}
static struct mod_paths {
char *name, *path;
} mod_paths[] = {
- {"libcfs", "lustre/portals/libcfs"},
- {"portals", "lustre/portals/portals"},
- {"ksocknal", "lustre/portals/knals/socknal"},
- {"kptlrouter", "lustre/portals/router"},
+ {"libcfs", "libcfs/libcfs"},
+ {"lnet", "lnet/lnet"},
+ {"kciblnd", "lnet/klnds/ciblnd"},
+ {"kgmlnd", "lnet/klnds/gmlnd"},
+ {"kmxlnd", "lnet/klnds/mxlnd"},
+ {"kiiblnd", "lnet/klnds/iiblnd"},
+ {"ko2iblnd", "lnet/klnds/o2iblnd"},
+ {"kopeniblnd", "lnet/klnds/openiblnd"},
+ {"kptllnd", "lnet/klnds/ptllnd"},
+ {"kqswlnd", "lnet/klnds/qswlnd"},
+ {"kralnd", "lnet/klnds/ralnd"},
+ {"ksocklnd", "lnet/klnds/socklnd"},
+ {"ktdilnd", "lnet/klnds/tdilnd"},
+ {"kviblnd", "lnet/klnds/viblnd"},
{"lvfs", "lustre/lvfs"},
{"obdclass", "lustre/obdclass"},
{"llog_test", "lustre/obdclass"},
+ {"ptlrpc_gss", "lustre/ptlrpc/gss"},
{"ptlrpc", "lustre/ptlrpc"},
+ {"gks", "lustre/sec/gks"},
+ {"gkc", "lustre/sec/gks"},
{"ost", "lustre/ost"},
{"osc", "lustre/osc"},
{"mds", "lustre/mds"},
{"mdc", "lustre/mdc"},
{"llite", "lustre/llite"},
+ {"lustre", "lustre/llite"},
+ {"llite_lloop", "lustre/llite"},
+ {"ldiskfs", "ldiskfs/ldiskfs"},
{"smfs", "lustre/smfs"},
{"obdecho", "lustre/obdecho"},
{"ldlm", "lustre/ldlm"},
{"obdfilter", "lustre/obdfilter"},
- {"extN", "lustre/extN"},
{"lov", "lustre/lov"},
{"lmv", "lustre/lmv"},
{"fsfilt_ext3", "lustre/lvfs"},
- {"fsfilt_extN", "lustre/lvfs"},
{"fsfilt_reiserfs", "lustre/lvfs"},
{"fsfilt_smfs", "lustre/lvfs"},
- {"ptlbd", "lustre/ptlbd"},
- {"mgmt_svc", "lustre/mgmt"},
- {"mgmt_cli", "lustre/mgmt"},
+ {"fsfilt_ldiskfs", "lustre/lvfs"},
+ {"mds_ext3", "lustre/mds"},
{"cobd", "lustre/cobd"},
{"cmobd", "lustre/cmobd"},
+ {"lquota", "lustre/quota"},
+ {"mgs", "lustre/mgs"},
+ {"mgc", "lustre/mgc"},
+ {"mdt", "lustre/mdt"},
+ {"mdd", "lustre/mdd"},
+ {"osd", "lustre/osd"},
+ {"cmm", "lustre/cmm"},
+ {"fid", "lustre/fid"},
+ {"fld", "lustre/fld"},
{NULL, NULL}
};
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
-int jt_dbg_modules(int argc, char **argv)
+static int jt_dbg_modules_2_4(int argc, char **argv)
{
- struct mod_paths *mp;
- char *path = "..";
- char *kernel = "linux";
-
- if (argc >= 2)
- path = argv[1];
- if (argc == 3)
- kernel = argv[2];
- if (argc > 3) {
- printf("%s [path] [kernel]\n", argv[0]);
- return 0;
- }
-
- for (mp = mod_paths; mp->name != NULL; mp++) {
- struct module_info info;
- int rc;
- size_t crap;
- int query_module(const char *name, int which, void *buf,
- size_t bufsize, size_t *ret);
-
- rc = query_module(mp->name, QM_INFO, &info, sizeof(info),
- &crap);
- if (rc < 0) {
- if (errno != ENOENT)
- printf("query_module(%s) failed: %s\n",
- mp->name, strerror(errno));
- } else {
- printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
- mp->path, mp->name,
- info.addr + sizeof(struct module));
- }
- }
-
- return 0;
+ return -EINVAL;
}
-#else
-int jt_dbg_modules(int argc, char **argv)
+
+static int jt_dbg_modules_2_5(int argc, char **argv)
{
struct mod_paths *mp;
- char *path = "..";
+ char *path = "";
char *kernel = "linux";
const char *proc = "/proc/modules";
- char modname[128], others[128];
+ char modname[128], buf[4096];
long modaddr;
- int rc;
FILE *file;
if (argc >= 2)
return 0;
}
- while ((rc = fscanf(file, "%s %s %s %s %s %lx\n",
- modname, others, others, others, others, &modaddr)) == 6) {
- for (mp = mod_paths; mp->name != NULL; mp++) {
- if (!strcmp(mp->name, modname))
- break;
- }
- if (mp->name) {
- printf("add-symbol-file %s/%s/%s.o 0x%0lx\n", path,
- mp->path, mp->name, modaddr);
+ while (fgets(buf, sizeof(buf), file) != NULL) {
+ if (sscanf(buf, "%s %*s %*s %*s %*s %lx", modname, &modaddr) == 2) {
+ for (mp = mod_paths; mp->name != NULL; mp++) {
+ if (!strcmp(mp->name, modname))
+ break;
+ }
+ if (mp->name) {
+ printf("add-symbol-file %s%s%s/%s.o 0x%0lx\n",
+ path, path[0] ? "/" : "",
+ mp->path, mp->name, modaddr);
+ }
}
}
+ fclose(file);
+ return 0;
+}
+
+int jt_dbg_modules(int argc, char **argv)
+{
+ int rc = 0;
+ struct utsname sysinfo;
+
+ rc = uname(&sysinfo);
+ if (rc) {
+ printf("uname() failed: %s\n", strerror(errno));
+ return 0;
+ }
+
+ if (sysinfo.release[2] > '4') {
+ return jt_dbg_modules_2_5(argc, argv);
+ } else {
+ return jt_dbg_modules_2_4(argc, argv);
+ }
+
return 0;
}
-#endif /* linux 2.5 */
int jt_dbg_panic(int argc, char **argv)
{
int rc;
- struct portal_ioctl_data data;
+ struct libcfs_ioctl_data data;
if (argc != 1) {
fprintf(stderr, "usage: %s\n", argv[0]);
}
memset(&data, 0, sizeof(data));
- if (portal_ioctl_pack(&data, &buf, max) != 0) {
- fprintf(stderr, "portal_ioctl_pack failed.\n");
+ if (libcfs_ioctl_pack(&data, &buf, max) != 0) {
+ fprintf(stderr, "libcfs_ioctl_pack failed.\n");
return -1;
}
- rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PANIC, buf);
+ rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PANIC, buf);
if (rc) {
- fprintf(stderr, "IOC_PORTAL_PANIC failed: %s\n",
+ fprintf(stderr, "IOC_LIBCFS_PANIC failed: %s\n",
strerror(errno));
return -1;
}