#include <limits.h>
#define NELEMS(arr) (sizeof(arr)/sizeof(arr[0]))
+#define BUF_SIZE (BUFSIZ)
enum {
OPT_HELP,
printf("%21d: %s\n", i, buf + pos);
}
+static char *fgets_fd(char *str, int len, int fd)
+{
+ char ch;
+ register char *cs;
+ int num = 0;
+
+ cs = str;
+ while (--len > 0 && (num = read(fd, &ch, 1) > 0)) {
+ if ((*cs++ = ch) == '\n')
+ break;
+ }
+ *cs = '\0';
+ return (num == 0 && cs == str) ? NULL : str;
+}
+
+static void meminfo_report(const char *pidstr){
+ char infoname[BUF_SIZE];
+ char memsize[BUF_SIZE];
+ char linebuf[BUF_SIZE];
+ char file_path[PATH_MAX];
+ int fd;
+
+ printf("\nMemory information\n");
+
+ if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) {
+ fprintf(stderr, "[crash-pipe] can't open %s\n", file_path);
+ } else {
+ while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) {
+ sscanf(linebuf, "%s %s %*s", infoname, memsize);
+ if (strcmp("MemTotal:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname, memsize);
+ } else if (strcmp("MemFree:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname, memsize);
+ } else if (strcmp("Buffers:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname, memsize);
+ } else if (strcmp("Cached:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname, memsize);
+ break;
+ }
+ }
+ close(fd);
+ }
+
+ snprintf(file_path, PATH_MAX, "/proc/%s/status", pidstr);
+ if ((fd = open(file_path, O_RDONLY)) < 0) {
+ fprintf(stderr, "[crash-pipe]can't open /proc/meminfo\n");
+ } else {
+ while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) {
+ sscanf(linebuf, "%s %s %*s", infoname, memsize);
+ if (strcmp("VmPeak:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname,
+ memsize);
+ } else if (strcmp("VmSize:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname,
+ memsize);
+ } else if (strcmp("VmLck:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname,
+ memsize);
+ } else if (strcmp("VmPin:", infoname) == 0) {
+ printf("%s %8s KB\n", infoname,
+ memsize);
+ } else if (strcmp("VmHWM:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ } else if (strcmp("VmRSS:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ } else if (strcmp("VmData:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ } else if (strcmp("VmStk:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ } else if (strcmp("VmExe:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ } else if (strcmp("VmLib:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ } else if (strcmp("VmPTE:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ } else if (strcmp("VmSwap:", infoname) == 0) {
+ printf("%s %8s KB\n",
+ infoname, memsize);
+ break;
+ }
+ }
+ close(fd);
+ }
+}
+
static void report(int argc, char *argv[])
{
const char *pidstr = argv[0];
printf("Executable File Path: %s\n", exe_file);
}
+ meminfo_report(pidstr);
+
printf(" - passed from kernel -\n"
"%16s: %s\n"
"%16s: %s\n"