summaryrefslogtreecommitdiffstats
path: root/system_cmds/mslutil
diff options
context:
space:
mode:
Diffstat (limited to 'system_cmds/mslutil')
-rw-r--r--system_cmds/mslutil/mslutil.146
-rw-r--r--system_cmds/mslutil/mslutil.c96
2 files changed, 142 insertions, 0 deletions
diff --git a/system_cmds/mslutil/mslutil.1 b/system_cmds/mslutil/mslutil.1
new file mode 100644
index 0000000..ea4f068
--- /dev/null
+++ b/system_cmds/mslutil/mslutil.1
@@ -0,0 +1,46 @@
+.\" Copyright (c) 2017, Apple Computer, Inc. All rights reserved.
+.\"
+.Dd March 31, 2017
+.Dt MSLUTIL 1
+.Os "Mac OS X"
+.Sh NAME
+.Nm mslutil
+.Nd Tool to enable / disable malloc stack logging on a specific proces
+.Sh SYNOPSIS
+.Nm mslutil pid [--enable flavor] | [--disable]
+.Sh DESCRIPTION
+The
+.Nm mslutil
+utility enables/disables malloc stack logging on the process specified by
+.Nm pid.
+It requires root privileges.
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.\" ==========
+.It Fl -enable
+Specifying the
+.Fl -enable
+option enables malloc stack logging, using the
+.Pa flavor
+provided.
+The supported flavors are:
+.Pp
+.Pa full
+Standard malloc stack logging that records both vm and malloc calls
+.Pp
+.Pa malloc
+Standard malloc stack logging that records only malloc calls
+.Pp
+.Pa vm
+Standard malloc stack logging that records only vm calls
+.Pp
+.Pa lite
+Lite mode of malloc stack logging.
+.\" ==========
+.It Fl -disable
+Specifying the
+.Fl -disable
+option disables any current mode of malloc stack logging.
+.\" ==========
+.El
diff --git a/system_cmds/mslutil/mslutil.c b/system_cmds/mslutil/mslutil.c
new file mode 100644
index 0000000..5738d4a
--- /dev/null
+++ b/system_cmds/mslutil/mslutil.c
@@ -0,0 +1,96 @@
+//
+// mslutil.c
+// mslutil
+//
+// Created by Christopher Deppe on 3/31/17.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/sysctl.h>
+#include <stack_logging.h>
+
+#define BSD_PID_MAX 99999 /* Copy of PID_MAX from sys/proc_internal.h. */
+
+static void print_usage()
+{
+ printf("usage: mslutil pid [--disable] | [--enable malloc | vm | full | lite | vmlite]\n");
+}
+
+static int send_msl_command(uint64_t pid, uint64_t flavor)
+{
+ uint64_t flags = flavor;
+ flags <<= 32;
+
+ flags |= (pid & 0xFFFFFFFF);
+
+ int ret = sysctlbyname("kern.memorystatus_vm_pressure_send", 0, 0, &flags, sizeof(flags));
+
+ if (ret) {
+ printf("send_msl_command - sysctl: kern.memorystatus_vm_pressure_send failed %s\n", strerror(errno));
+ } else {
+ printf("send_msl_command - success!\n");
+ }
+
+ return ret;
+}
+
+int main(int argc, const char * argv[])
+{
+ if (argc < 3) {
+ print_usage();
+ exit(1);
+ }
+
+ int ret = -1;
+
+ pid_t pid = atoi(argv[1]);
+
+ if (pid <= 0 || pid > BSD_PID_MAX) {
+ printf("Invalid pid\n");
+ exit(1);
+ }
+
+ if (strcmp(argv[2], "--enable") == 0) {
+ if (argc < 4) {
+ print_usage();
+ exit(1);
+ }
+
+ uint64_t flavor = 0;
+
+ if (strcmp(argv[3], "full") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_MALLOC | MEMORYSTATUS_ENABLE_MSL_VM;
+ } else if (strcmp(argv[3], "malloc") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_MALLOC;
+ } else if (strcmp(argv[3], "vm") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_VM;
+ } else if (strcmp(argv[3], "lite") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_LITE_FULL;
+ } else if (strcmp(argv[3], "vmlite") == 0) {
+ flavor = MEMORYSTATUS_ENABLE_MSL_LITE_VM;
+ }
+
+ if (flavor == 0) {
+ print_usage();
+ exit(1);
+ }
+
+ ret = send_msl_command(pid, flavor);
+ } else if (strcmp(argv[2], "--disable") == 0) {
+ ret = send_msl_command(pid, MEMORYSTATUS_DISABLE_MSL);
+ } else {
+ print_usage();
+ exit(1);
+ }
+
+ if (ret != 0) {
+ exit(1);
+ } else {
+ exit(0);
+ }
+}
+
+