2 * drivers/misc/tegra-profiler/mmap.c
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/sched.h>
25 #include <linux/tegra_profiler.h>
32 put_mmap_sample(struct quadd_mmap_data *s, char *filename,
33 size_t length, unsigned long pgoff)
35 struct quadd_record_data r;
36 struct quadd_iovec vec[2];
37 u64 pgoff_val = pgoff << PAGE_SHIFT;
39 r.record_type = QUADD_RECORD_TYPE_MMAP;
41 memcpy(&r.mmap, s, sizeof(*s));
42 r.mmap.filename_length = length;
44 vec[0].base = &pgoff_val;
45 vec[0].len = sizeof(pgoff_val);
47 vec[1].base = filename;
50 pr_debug("MMAP: pid: %u, file_name: '%s', addr: %#llx - %#llx, len: %llx, pgoff: %#lx\n",
51 s->pid, filename, s->addr, s->addr + s->len, s->len, pgoff);
53 quadd_put_sample(&r, vec, ARRAY_SIZE(vec));
56 void quadd_process_mmap(struct vm_area_struct *vma, pid_t pid)
60 char *file_name, *tmp_buf;
61 struct quadd_mmap_data sample;
62 size_t length, length_aligned;
67 if (!(vma->vm_flags & VM_EXEC))
70 vm_file = vma->vm_file;
74 path = &vm_file->f_path;
76 tmp_buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
80 file_name = d_path(path, tmp_buf, PATH_MAX);
81 if (IS_ERR(file_name))
84 if (strstr(file_name, " (deleted)"))
90 sample.addr = vma->vm_start;
91 sample.len = vma->vm_end - vma->vm_start;
93 length = strlen(file_name) + 1;
94 length_aligned = ALIGN(length, sizeof(u64));
96 put_mmap_sample(&sample, file_name, length_aligned, vma->vm_pgoff);
102 int quadd_get_current_mmap(pid_t pid)
104 struct vm_area_struct *vma;
105 struct file *vm_file;
108 struct task_struct *task;
109 struct mm_struct *mm;
110 struct quadd_mmap_data sample;
111 size_t length, length_aligned;
115 task = pid_task(find_vpid(pid), PIDTYPE_PID);
118 pr_err("Process not found: %d\n", pid);
124 pr_warn("mm is not existed for task: %d\n", pid);
128 pr_info("Get mapped memory objects\n");
130 tmp_buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
134 for (vma = mm->mmap; vma; vma = vma->vm_next) {
135 if (!(vma->vm_flags & VM_EXEC))
138 vm_file = vma->vm_file;
142 path = &vm_file->f_path;
144 file_name = d_path(path, tmp_buf, PATH_MAX);
145 if (IS_ERR(file_name))
148 if (strstr(file_name, " (deleted)"))
151 length = strlen(file_name) + 1;
152 length_aligned = ALIGN(length, sizeof(u64));
155 sample.user_mode = 1;
157 sample.addr = vma->vm_start;
158 sample.len = vma->vm_end - vma->vm_start;
160 put_mmap_sample(&sample, file_name, length_aligned,