Skip to content

Commit

Permalink
kmemleak: Dump object information on request
Browse files Browse the repository at this point in the history
By writing dump=<addr> to the kmemleak file, kmemleak will look up an
object with that address and dump the information it has about it to
syslog. This is useful in debugging memory leaks.

Signed-off-by: Catalin Marinas <[email protected]>
  • Loading branch information
ctmarinas committed Aug 27, 2009
1 parent af98603 commit 189d84e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions Documentation/kmemleak.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Memory scanning parameters can be modified at run-time by writing to the
scan=<secs> - set the automatic memory scanning period in seconds
(default 600, 0 to stop the automatic scanning)
scan - trigger a memory scan
dump=<addr> - dump information about the object found at <addr>

Kmemleak can also be disabled at boot-time by passing "kmemleak=off" on
the kernel command line.
Expand Down
25 changes: 25 additions & 0 deletions mm/kmemleak.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ static void dump_object_info(struct kmemleak_object *object)
object->comm, object->pid, object->jiffies);
pr_notice(" min_count = %d\n", object->min_count);
pr_notice(" count = %d\n", object->count);
pr_notice(" flags = 0x%lx\n", object->flags);
pr_notice(" backtrace:\n");
print_stack_trace(&trace, 4);
}
Expand Down Expand Up @@ -1307,6 +1308,27 @@ static int kmemleak_release(struct inode *inode, struct file *file)
return seq_release(inode, file);
}

static int dump_str_object_info(const char *str)
{
unsigned long flags;
struct kmemleak_object *object;
unsigned long addr;

addr= simple_strtoul(str, NULL, 0);
object = find_and_get_object(addr, 0);
if (!object) {
pr_info("Unknown object at 0x%08lx\n", addr);
return -EINVAL;
}

spin_lock_irqsave(&object->lock, flags);
dump_object_info(object);
spin_unlock_irqrestore(&object->lock, flags);

put_object(object);
return 0;
}

/*
* File write operation to configure kmemleak at run-time. The following
* commands can be written to the /sys/kernel/debug/kmemleak file:
Expand All @@ -1318,6 +1340,7 @@ static int kmemleak_release(struct inode *inode, struct file *file)
* scan=... - set the automatic memory scanning period in seconds (0 to
* disable it)
* scan - trigger a memory scan
* dump=... - dump information about the object found at the given address
*/
static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
size_t size, loff_t *ppos)
Expand Down Expand Up @@ -1358,6 +1381,8 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
}
} else if (strncmp(buf, "scan", 4) == 0)
kmemleak_scan();
else if (strncmp(buf, "dump=", 5) == 0)
ret = dump_str_object_info(buf + 5);
else
ret = -EINVAL;

Expand Down

0 comments on commit 189d84e

Please sign in to comment.