1 ////////////////////////////////////////////////////////////////////////////////////
3 // FILE: us_proc_inst.c
6 // This file is C source for SWAP driver.
8 // SEE ALSO: us_proc_inst.h
9 // AUTHOR: A.Gerenkov, E. Gorelkina
10 // COMPANY NAME: Samsung Research Center in Moscow
11 // DEPT NAME: Advanced Software Group
12 // CREATED: 2008.06.02
14 // REVISION DATE: 2008.12.02
16 ////////////////////////////////////////////////////////////////////////////////////
19 #include "us_proc_inst.h"
21 #include "../kprobe/dbi_kprobes_deps.h"
22 #include "../kprobe/dbi_uprobes.h"
24 #define mm_read_lock(task, mm, atomic, lock) \
25 mm = atomic ? task->active_mm : get_task_mm(task); \
28 panic("ERRR mm_read_lock: mm == NULL\n"); \
32 lock = down_read_trylock(&mm->mmap_sem); \
35 down_read(&mm->mmap_sem); \
38 #define mm_read_unlock(mm, atomic, lock) \
40 up_read(&mm->mmap_sem); \
47 DEFINE_PER_CPU (us_proc_vtp_t *, gpVtp) = NULL;
48 DEFINE_PER_CPU (struct pt_regs *, gpCurVtpRegs) = NULL;
50 #if defined(CONFIG_MIPS)
51 # define ARCH_REG_VAL(regs, idx) regs->regs[idx]
52 #elif defined(CONFIG_ARM)
53 # define ARCH_REG_VAL(regs, idx) regs->uregs[idx]
55 # define ARCH_REG_VAL(regs, idx) 0
56 # warning ARCH_REG_VAL is not implemented for this architecture. FBI will work improperly or even crash!!!
59 unsigned long ujprobe_event_pre_handler (struct us_ip *ip, struct pt_regs *regs);
60 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6);
61 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, struct us_ip *ip);
63 static int register_usprobe(struct task_struct *task, struct us_ip *ip, int atomic);
64 static int unregister_usprobe(struct task_struct *task, struct us_ip *ip, int atomic, int no_rp2);
70 LIST_HEAD(proc_probes_list);
73 struct dentry *launchpad_daemon_dentry = NULL;
74 EXPORT_SYMBOL_GPL(launchpad_daemon_dentry);
78 unsigned long android_app_vma_start = 0;
79 unsigned long android_app_vma_end = 0;
80 struct dentry *app_process_dentry = NULL;
81 #endif /* ANDROID_APP */
84 struct dentry *libdvm_dentry = NULL;
85 /* Defines below are for libdvm.so with md5sum:
86 * 5941c87b49198368e7db726c2977bf1d */
87 #define LIBDVM_ENTRY 0x30a64
88 #define LIBDVM_RETURN 0x30bdc
89 #endif /* __ANDROID */
93 #define print_event(fmt, args...) \
96 sprintf(buf, fmt, ##args); \
97 pack_event_info(US_PROBE_ID, RECORD_ENTRY, "ds", 0x0badc0de, buf); \
100 static inline int is_libonly(void)
102 return !strcmp(us_proc_info.path,"*");
105 // is user-space instrumentation
106 static inline int is_us_instrumentation(void)
108 return !!us_proc_info.path;
111 struct proc_probes *get_proc_probes_by_task(struct task_struct *task)
113 struct proc_probes *proc_p, *tmp;
116 if (task != current) {
117 printk("ERROR get_proc_probes_by_task: \'task != current\'\n");
121 return us_proc_info.pp;
124 list_for_each_entry_safe(proc_p, tmp, &proc_probes_list, list) {
125 if (proc_p->tgid == task->tgid) {
133 void add_proc_probes(struct task_struct *task, struct proc_probes *proc_p)
135 list_add_tail(&proc_p->list, &proc_probes_list);
138 struct proc_probes *get_proc_probes_by_task_or_new(struct task_struct *task)
140 struct proc_probes *proc_p = get_proc_probes_by_task(task);
141 if (proc_p == NULL) {
142 proc_p = proc_p_copy(us_proc_info.pp, task);
143 add_proc_probes(task, proc_p);
150 static int is_slp_app_with_dentry(struct vm_area_struct *vma,
151 struct dentry *dentry)
153 struct vm_area_struct *slp_app_vma = NULL;
155 if (vma->vm_file->f_dentry == launchpad_daemon_dentry) {
157 while (slp_app_vma) {
158 if (slp_app_vma->vm_file) {
159 if (slp_app_vma->vm_file->f_dentry == dentry &&
160 slp_app_vma->vm_pgoff == 0) {
164 slp_app_vma = slp_app_vma->vm_next;
173 static int is_android_app_with_dentry(struct vm_area_struct *vma,
174 struct dentry *dentry)
176 struct vm_area_struct *android_app_vma = NULL;
178 if (vma->vm_file->f_dentry == app_process_dentry) {
179 android_app_vma = vma;
180 while (android_app_vma) {
181 if (android_app_vma->vm_file) {
182 if (android_app_vma->vm_file->f_dentry == dentry) {
183 android_app_vma_start = android_app_vma->vm_start;
184 android_app_vma_end = android_app_vma->vm_end;
188 android_app_vma = android_app_vma->vm_next;
194 #endif /* ANDROID_APP */
197 void find_libdvm_for_task(struct task_struct *task, inst_us_proc_t *info)
199 struct vm_area_struct *vma = NULL;
200 struct mm_struct *mm = NULL;
202 mm = get_task_mm(task);
207 if (vma->vm_file->f_dentry == libdvm_dentry) {
208 info->libdvm_start = vma->vm_start;
209 info->libdvm_end = vma->vm_end;
218 #endif /* __ANDROID */
220 struct dentry *dentry_by_path(const char *path)
222 struct dentry *dentry;
223 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
225 if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
226 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
228 if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
229 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
230 EPRINTF("failed to lookup dentry for path %s!", path);
234 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
237 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
238 dentry = nd.path.dentry;
240 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
241 dentry = st_path.dentry;
243 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
247 static int find_task_by_path (const char *path, struct task_struct **p_task, struct list_head *tids)
250 struct task_struct *task;
251 struct vm_area_struct *vma;
252 struct mm_struct *mm;
253 struct dentry *dentry = dentry_by_path(path);
257 /* find corresponding dir entry, this is also check for valid path */
258 // TODO: test - try to instrument process with non-existing path
259 // TODO: test - try to instrument process with existing path and delete file just after start
260 if (dentry == NULL) {
265 for_each_process (task) {
267 if ( 0 != inst_pid && ( inst_pid != task->pid ) )
270 mm = get_task_mm(task);
275 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
276 if (vma->vm_file->f_dentry == dentry) {
279 get_task_struct (task);
285 if (is_slp_app_with_dentry(vma, dentry)) {
287 get_task_struct(task);
293 if (is_android_app_with_dentry(vma, dentry)) {
295 get_task_struct(task);
298 #endif /* ANDROID_APP */
302 // only decrement usage count on mm since we cannot sleep here
303 atomic_dec(&mm->mm_users);
310 DPRINTF ("found pid %d for %s.", (*p_task)->pid, path);
311 *p_task = (*p_task)->group_leader;
312 gl_nNotifyTgid = (*p_task)->tgid;
314 DPRINTF ("pid for %s not found!", path);
321 static void us_vtp_event_pre_handler (us_proc_vtp_t * vtp, struct pt_regs *regs)
323 __get_cpu_var(gpVtp) = vtp;
324 __get_cpu_var(gpCurVtpRegs) = regs;
327 static void us_vtp_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
329 us_proc_vtp_t *vtp = __get_cpu_var(gpVtp);
330 #if !defined(CONFIG_X86)
331 struct pt_regs *regs = __get_cpu_var(gpCurVtpRegs);
337 us_proc_vtp_data_t *vtp_data;
343 list_for_each_entry_rcu (vtp_data, &vtp->list, list) {
344 // DPRINTF ("[%d]proc %s(%d): %lx", nCount++, current->comm, current->pid, vtp->addr);
345 fmt[1] = vtp_data->type;
346 if (vtp_data->reg == -1)
347 vaddr = vtp_data->off;
349 vaddr = ARCH_REG_VAL (regs, vtp_data->reg) + vtp_data->off;
350 // DPRINTF ("VTP type '%c'", vtp_data->type);
351 switch (vtp_data->type)
356 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
357 EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
359 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
362 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
363 EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
365 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
368 if (read_proc_vm_atomic (current, vaddr, &cval, sizeof (cval)) < sizeof (cval))
369 EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
371 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, cval, vtp_data->name);
374 if (current->active_mm) {
376 struct vm_area_struct *vma;
379 if (get_user_pages_atomic (current, current->active_mm, vaddr, 1, 0, 1, &page, &vma) <= 0) {
380 EPRINTF ("get_user_pages_atomic failed for proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
383 maddr = kmap_atomic (page, KM_USER0);
384 len = strlen (maddr + (vaddr & ~PAGE_MASK));
385 sval = kmalloc (len + 1, GFP_KERNEL);
387 EPRINTF ("failed to alloc memory for string in proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
389 copy_from_user_page (vma, page, vaddr, sval, maddr + (vaddr & ~PAGE_MASK), len + 1);
390 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, sval, vtp_data->name);
393 kunmap_atomic (maddr, KM_USER0);
394 page_cache_release (page);
397 EPRINTF ("task %s/%u has no mm!", current->comm, current->pid);
400 EPRINTF ("unknown variable type '%c'", vtp_data->type);
403 dbi_uprobe_return ();
406 static int install_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
408 struct vm_area_struct *vma;
411 unsigned int old_ips_count, old_vtps_count;
412 struct task_struct *t;
413 struct mm_struct *mm;
415 mm = atomic ? task->active_mm : get_task_mm (task);
417 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
419 old_ips_count = task_inst_info->unres_ips_count;
420 old_vtps_count = task_inst_info->unres_vtps_count;
422 down_read (&mm->mmap_sem);
425 // skip non-text section
427 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file || (vma->vm_flags & VM_ACCOUNT) ||
428 !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
429 !(vma->vm_flags & (VM_READ | VM_MAYREAD))) {
431 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file) {
437 * After process was forked, some time it inherits parent process environment.
438 * We need to renew instrumentation when we detect that process gets own environment.
440 for (i = 0; i < task_inst_info->libs_count; i++) {
441 // struct path tmp_path;
442 // tmp_path.dentry = task_inst_info->p_libs[i].m_f_dentry;
443 // tmp_path.mnt = task_inst_info->p_libs[i].m_vfs_mount;
444 // char* p_path = d_path ( &tmp_path, path_buffer, 255 );
445 // DPRINTF("f_dentry:%x m_f_dentry:%x path:%s", vma->vm_file->f_dentry,
446 // task_inst_info->p_libs[i].m_f_dentry, p_path );
448 //TODO: test - try to instrument non-existing libs
449 if (vma->vm_file->f_dentry == task_inst_info->p_libs[i].m_f_dentry) {
450 // DPRINTF("vm_flags:%x loaded:%x ips_count:%d vtps_count:%d",
451 // vma->vm_flags, task_inst_info->p_libs[i].loaded,
452 // task_inst_info->p_libs[i].ips_count, task_inst_info->p_libs[i].vtps_count );
453 if (!task_inst_info->p_libs[i].loaded) {
454 // DPRINTF("!VM_EXECUTABLE && !loaded");
456 int app_flag = (vma->vm_file->f_dentry == task_inst_info->m_f_dentry);
457 DPRINTF ("post dyn lib event %s/%s", current->comm, task_inst_info->p_libs[i].path);
458 // if we installed something, post library info for those IPs
459 p = strrchr(task_inst_info->p_libs[i].path, '/');
461 p = task_inst_info->p_libs[i].path;
464 task_inst_info->p_libs[i].loaded = 1;
465 task_inst_info->p_libs[i].vma_start = vma->vm_start;
466 task_inst_info->p_libs[i].vma_end = vma->vm_end;
467 task_inst_info->p_libs[i].vma_flag = vma->vm_flags;
468 pack_event_info (DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
469 task->tgid, p, vma->vm_start, vma->vm_end-vma->vm_start, app_flag);
471 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++) {
472 DPRINTF("ips_count current:%d", k);
473 if (!task_inst_info->p_libs[i].p_ips[k].installed) {
474 DPRINTF("!installed");
475 addr = task_inst_info->p_libs[i].p_ips[k].offset;
476 addr += vma->vm_start;
477 if (page_present (mm, addr)) {
478 DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
479 task->pid, task_inst_info->p_libs[i].path,
480 task_inst_info->p_libs[i].p_ips[k].offset, addr);
481 task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
482 task_inst_info->p_libs[i].p_ips[k].retprobe.kp.addr = (kprobe_opcode_t *) addr;
483 task_inst_info->unres_ips_count--;
484 err = register_usprobe(task, &task_inst_info->p_libs[i].p_ips[k], atomic);
486 DPRINTF ("failed to install IP at %lx/%p. Error %d!",
487 task_inst_info->p_libs[i].p_ips[k].offset,
488 task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
493 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++) {
494 DPRINTF("vtps_count current:%d", k);
495 if (!task_inst_info->p_libs[i].p_vtps[k].installed) {
496 DPRINTF("!installed");
497 addr = task_inst_info->p_libs[i].p_vtps[k].addr;
498 if (!(vma->vm_flags & VM_EXECUTABLE))
499 addr += vma->vm_start;
500 if (page_present (mm, addr)) {
501 DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
502 task->pid, task_inst_info->p_libs[i].path,
503 task_inst_info->p_libs[i].p_ips[k].offset, addr);
504 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.tgid = task_inst_info->tgid;
505 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
506 task_inst_info->p_libs[i].p_vtps[k].jprobe.entry = (kprobe_opcode_t *) us_vtp_event_handler;
507 task_inst_info->p_libs[i].p_vtps[k].jprobe.pre_entry = (kprobe_pre_entry_handler_t) us_vtp_event_pre_handler;
508 task_inst_info->p_libs[i].p_vtps[k].jprobe.priv_arg = &task_inst_info->p_libs[i].p_vtps[k];
509 task_inst_info->p_libs[i].p_vtps[k].installed = 1;
510 task_inst_info->unres_vtps_count--;
511 err = dbi_register_ujprobe(task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
513 EPRINTF ("failed to install VTP at %p. Error %d!",
514 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr, err);
522 if (is_java_inst_enabled()
523 && vma->vm_file->f_dentry == libdvm_dentry) {
524 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
526 && task_inst_info->libdvm_start) {
527 unsigned long addr = LIBDVM_ENTRY + task_inst_info->libdvm_start;
528 if (page_present(mm, addr)) {
529 entp->jprobe.kp.tgid = task->tgid;
530 entp->jprobe.pre_entry = ujprobe_event_pre_handler;
531 entp->jprobe.entry = ujprobe_event_handler;
532 entp->jprobe.priv_arg = entp;
533 entp->jprobe.kp.addr = addr;
534 entp->retprobe.kp.tgid = task->tgid;
535 entp->retprobe.handler = uretprobe_event_handler;
536 entp->retprobe.priv_arg = entp;
537 entp->retprobe.kp.addr = addr;
538 err = register_usprobe(task, mm, entp, atomic, 0);
540 DPRINTF("failed to install IP at %p", addr);
545 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
547 && task_inst_info->libdvm_start) {
548 unsigned long addr = LIBDVM_RETURN + task_inst_info->libdvm_start;
549 if (page_present(mm, addr)) {
550 retp->jprobe.kp.tgid = task->tgid;
551 retp->jprobe.pre_entry = ujprobe_event_pre_handler;
552 retp->jprobe.entry = ujprobe_event_handler;
553 retp->jprobe.priv_arg = retp;
554 retp->jprobe.kp.addr = addr;
555 retp->retprobe.kp.tgid = task->tgid;
556 retp->retprobe.handler = uretprobe_event_handler;
557 retp->retprobe.priv_arg = retp;
558 retp->retprobe.kp.addr = addr;
559 err = register_usprobe(task, mm, retp, atomic, 0);
561 DPRINTF("failed to install IP at %p", addr);
567 #endif /* __ANDROID */
572 up_read (&mm->mmap_sem);
575 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
578 static void set_mapping_file(struct file_probes *file_p,
579 const struct proc_probes *proc_p,
580 const struct task_struct *task,
581 const struct vm_area_struct *vma);
583 int install_otg_ip(unsigned long addr,
584 kprobe_pre_entry_handler_t pre_handler,
585 unsigned long jp_handler,
586 kretprobe_handler_t rp_handler)
589 struct task_struct *task = current->group_leader;
590 struct mm_struct *mm = task->mm;
593 struct vm_area_struct *vma = find_vma(mm, addr);
594 if (vma && (vma->vm_flags & VM_EXEC) &&
595 vma->vm_file && vma->vm_file->f_dentry) {
596 unsigned long offset_addr = addr - vma->vm_start;
597 struct dentry *dentry = vma->vm_file->f_dentry;
598 char *name = dentry->d_iname;
599 struct proc_probes *proc_p = get_proc_probes_by_task(task);
600 struct probe_data pd = {
601 .offset = offset_addr,
602 .pre_handler = pre_handler,
603 .jp_handler = jp_handler,
604 .rp_handler = rp_handler,
608 struct file_probes *file_p = proc_p_find_file_p_by_dentry(proc_p, name, dentry);
609 struct page_probes *page_p = get_page_p(file_p, offset_addr);
610 struct us_ip *ip = page_p_find_ip(page_p, offset_addr & ~PAGE_MASK);
612 if (!file_p->loaded) {
613 set_mapping_file(file_p, proc_p, task, vma);
618 struct file_probes *file_p = proc_p_find_file_p_by_dentry(proc_p, name, dentry);
619 file_p_add_probe(file_p, &pd);
621 /* if addr mapping, that probe install, else it be installed in do_page_fault handler */
622 if (page_present(mm, addr)) {
623 ip = page_p_find_ip(page_p, offset_addr & ~PAGE_MASK);
624 set_ip_kp_addr(ip, page_p, file_p);
627 ret = register_usprobe_my(task, ip);
629 page_p_installed(page_p);
631 printk("ERROR install_otg_ip: ret=%d\n", ret);
642 EXPORT_SYMBOL_GPL(install_otg_ip);
645 static int uninstall_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
649 for (i = 0; i < task_inst_info->libs_count; i++)
651 DPRINTF ("clear lib %s.", task_inst_info->p_libs[i].path);
652 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++)
654 if (task_inst_info->p_libs[i].p_ips[k].installed)
656 DPRINTF ("remove IP at %p.", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr);
657 err = unregister_usprobe (task, &task_inst_info->p_libs[i].p_ips[k], atomic, 0);
660 EPRINTF ("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
663 task_inst_info->unres_ips_count++;
666 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++)
668 if (task_inst_info->p_libs[i].p_vtps[k].installed)
670 dbi_unregister_ujprobe (task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
671 task_inst_info->unres_vtps_count++;
672 task_inst_info->p_libs[i].p_vtps[k].installed = 0;
675 task_inst_info->p_libs[i].loaded = 0;
678 if (is_java_inst_enabled()) {
679 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
680 if (entp->installed) {
681 unregister_usprobe(task, entp, atomic);
684 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
685 if (retp->installed) {
686 unregister_usprobe(task, retp, atomic);
690 #endif /* __ANDROID */
692 DPRINTF ("Ures IPs %d.", task_inst_info->unres_ips_count);
693 DPRINTF ("Ures VTPs %d.", task_inst_info->unres_vtps_count);
697 static int uninstall_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
699 kernel_probe_t *probe = NULL;
701 if (probes_flags & kflag) {
702 probe = find_probe(addr);
704 iRet = remove_probe_from_list (addr);
706 EPRINTF ("remove_probe_from_list(0x%lx) result=%d!", addr, iRet);
710 probes_flags &= ~kflag;
712 if (us_proc_probes & uflag) {
713 if (!(probes_flags & uflag)) {
715 iRet = unregister_kernel_probe(probe);
717 EPRINTF ("unregister_kernel_probe(0x%lx) result=%d!",
723 us_proc_probes &= ~uflag;
728 static int uninstall_us_proc_probes(struct task_struct *task, struct proc_probes *proc_p, enum US_FLAGS flag);
730 int deinst_usr_space_proc (void)
732 int iRet = 0, found = 0;
733 struct task_struct *task = 0;
734 inst_us_proc_t *task_inst_info = NULL;
736 if (!is_us_instrumentation()) {
740 iRet = uninstall_kernel_probe (pf_addr, US_PROC_PF_INSTLD,
743 EPRINTF ("uninstall_kernel_probe(do_page_fault) result=%d!", iRet);
745 iRet = uninstall_kernel_probe (cp_addr, US_PROC_CP_INSTLD,
748 EPRINTF ("uninstall_kernel_probe(copy_process) result=%d!", iRet);
750 iRet = uninstall_kernel_probe (mr_addr, US_PROC_MR_INSTLD,
753 EPRINTF ("uninstall_kernel_probe(mm_release) result=%d!", iRet);
755 iRet = uninstall_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD,
758 EPRINTF ("uninstall_kernel_probe(do_exit) result=%d!", iRet);
760 iRet = uninstall_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD,
763 EPRINTF ("uninstall_kernel_probe(do_munmap) result=%d!", iRet);
766 struct proc_probes *proc_p;
768 for_each_process(task) {
769 proc_p = get_proc_probes_by_task(task);
771 int ret = uninstall_us_proc_probes(task, proc_p, US_UNREGS_PROBE);
773 EPRINTF ("failed to uninstall IPs (%d)!", ret);
776 dbi_unregister_all_uprobes(task, 1);
782 if (us_proc_info.tgid == 0)
785 for_each_process (task)
787 if (task->tgid == us_proc_info.tgid)
790 get_task_struct (task);
799 ret = uninstall_us_proc_probes(task, us_proc_info.pp, US_UNREGS_PROBE);
801 EPRINTF ("failed to uninstall IPs %d!", ret);
804 put_task_struct (task);
806 printk("### 1 ### dbi_unregister_all_uprobes:\n");
807 dbi_unregister_all_uprobes(task, 1);
808 us_proc_info.tgid = 0;
809 for(i = 0; i < us_proc_info.libs_count; i++)
810 us_proc_info.p_libs[i].loaded = 0;
816 static int install_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
818 kernel_probe_t *probe = NULL;
821 DPRINTF("us_proc_probes = 0x%x, uflag = 0x%x, "
822 "probes_flags = 0x%x, kflag = 0x%x",
823 us_proc_probes, uflag, probes_flags, kflag);
825 if (!(probes_flags & kflag)) {
826 iRet = add_probe_to_list (addr, &probe);
828 EPRINTF ("add_probe_to_list(0x%lx) result=%d!", addr, iRet);
831 probes_flags |= kflag;
833 if (!(us_proc_probes & uflag)) {
834 if (!(probes_flags & uflag)) {
835 iRet = register_kernel_probe (probe);
837 EPRINTF ("register_kernel_probe(0x%lx) result=%d!", addr, iRet);
841 us_proc_probes |= uflag;
850 static void install_proc_probes(struct task_struct *task, struct proc_probes *proc_p, int atomic);
852 int inst_usr_space_proc (void)
855 struct task_struct *task = 0;
857 if (!is_us_instrumentation()) {
861 DPRINTF("User space instr");
864 launchpad_daemon_dentry = dentry_by_path("/usr/bin/launchpad_preloading_preinitializing_daemon");
865 if (launchpad_daemon_dentry == NULL) {
872 app_process_dentry = dentry_by_path("/system/bin/app_process");
873 if (app_process_dentry == NULL) {
877 android_app_vma_start = 0;
878 android_app_vma_end = 0;
879 #endif /* ANDROID_APP */
882 if (is_java_inst_enabled()) {
883 libdvm_dentry = dentry_by_path("/system/lib/libdvm.so");
884 if (libdvm_dentry == NULL) {
888 memset(&us_proc_info.libdvm_entry_ip, 0, sizeof(us_proc_ip_t));
889 memset(&us_proc_info.libdvm_return_ip, 0, sizeof(us_proc_ip_t));
890 us_proc_info.libdvm_start = 0;
891 us_proc_info.libdvm_end = 0;
893 #endif /* __ANDROID */
895 for (i = 0; i < us_proc_info.libs_count; i++) {
896 us_proc_info.p_libs[i].loaded = 0;
898 /* check whether process is already running
899 * 1) if process is running - look for the libraries in the process maps
900 * 1.1) check if page for symbol does exist
901 * 1.1.1) if page exists - instrument it
902 * 1.1.2) if page does not exist - make sure that do_page_fault handler is installed
903 * 2) if process is not running - make sure that do_page_fault handler is installed
908 // FIXME: clear_task_inst_info();
909 for_each_process (task) {
910 struct proc_probes *proc_p;
912 if (task->flags & PF_KTHREAD){
913 DPRINTF("ignored kernel thread %d\n",
918 proc_p = get_proc_probes_by_task_or_new(task);
919 DPRINTF("trying process");
921 if (is_java_inst_enabled()) {
922 find_libdvm_for_task(task, task_inst_info);
924 #endif /* __ANDROID */
925 install_proc_probes(task, proc_p, 1);
926 //put_task_struct (task);
931 ret = find_task_by_path (us_proc_info.path, &task, NULL);
934 DPRINTF("task found. installing probes");
935 us_proc_info.tgid = task->pid;
937 if (is_java_inst_enabled()) {
938 find_libdvm_for_task(task, &us_proc_info);
940 #endif /* __ANDROID */
941 install_proc_probes(task, us_proc_info.pp, 0);
942 put_task_struct (task);
946 // enable 'do_page_fault' probe to detect when they will be loaded
947 ret = install_kernel_probe (pf_addr, US_PROC_PF_INSTLD, 0, &pf_probe);
950 EPRINTF ("install_kernel_probe(do_page_fault) result=%d!", ret);
953 // enable 'do_exit' probe to detect for remove task_struct
954 ret = install_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD, 0, &exit_probe);
957 EPRINTF ("install_kernel_probe(do_exit) result=%d!", ret);
960 /* enable 'copy_process' */
961 ret = install_kernel_probe (cp_addr, US_PROC_CP_INSTLD, 0, &cp_probe);
964 EPRINTF ("instpall_kernel_probe(copy_process) result=%d!", ret);
968 // enable 'mm_release' probe to detect when for remove user space probes
969 ret = install_kernel_probe (mr_addr, US_PROC_MR_INSTLD, 0, &mr_probe);
972 EPRINTF ("install_kernel_probe(mm_release) result=%d!", ret);
976 // enable 'do_munmap' probe to detect when for remove user space probes
977 ret = install_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD, 0, &unmap_probe);
980 EPRINTF ("install_kernel_probe(do_munmap) result=%d!", ret);
986 #include "../../tools/gpmu/probes/entry_data.h"
988 extern storage_arg_t sa_dpf;
990 void do_page_fault_j_pre_code(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
992 struct task_struct *task = current->group_leader;
994 if (task->flags & PF_KTHREAD) {
995 DPRINTF("ignored kernel thread %d\n", task->pid);
999 if (is_us_instrumentation()) {
1000 swap_put_entry_data((void *)addr, &sa_dpf);
1003 EXPORT_SYMBOL_GPL(do_page_fault_j_pre_code);
1006 unsigned long imi_sum_time = 0;
1007 unsigned long imi_sum_hit = 0;
1008 EXPORT_SYMBOL_GPL (imi_sum_time);
1009 EXPORT_SYMBOL_GPL (imi_sum_hit);
1011 static void set_mapping_file(struct file_probes *file_p,
1012 const struct proc_probes *proc_p,
1013 const struct task_struct *task,
1014 const struct vm_area_struct *vma)
1016 int app_flag = (vma->vm_file->f_dentry == proc_p->dentry);
1018 // if we installed something, post library info for those IPs
1019 p = strrchr(file_p->path, '/');
1026 file_p->vm_start = vma->vm_start;
1027 file_p->vm_end = vma->vm_end;
1029 pack_event_info(DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
1030 task->tgid, p, vma->vm_start,
1031 vma->vm_end - vma->vm_start, app_flag);
1034 void print_vma(struct mm_struct *mm);
1036 static int register_us_page_probe(struct page_probes *page_p,
1037 const struct file_probes *file_p,
1038 const struct task_struct *task)
1043 spin_lock(&page_p->lock);
1045 if (page_p_is_install(page_p)) {
1046 printk("page %x in %s task[tgid=%u, pid=%u] already installed\n",
1047 page_p->offset, file_p->dentry->d_iname, task->tgid, task->pid);
1048 print_vma(task->mm);
1052 page_p_assert_install(page_p);
1053 page_p_set_all_kp_addr(page_p, file_p);
1055 list_for_each_entry(ip, &page_p->ip_list, list) {
1056 err = register_usprobe_my(task, ip);
1063 page_p_installed(page_p);
1065 spin_unlock(&page_p->lock);
1070 static int unregister_us_page_probe(const struct task_struct *task,
1071 struct page_probes *page_p, enum US_FLAGS flag)
1076 spin_lock(&page_p->lock);
1077 if (!page_p_is_install(page_p)) {
1078 spin_unlock(&page_p->lock);
1082 list_for_each_entry(ip, &page_p->ip_list, list) {
1083 err = unregister_usprobe_my(task, ip, flag);
1090 if (flag != US_DISARM) {
1091 page_p_uninstalled(page_p);
1093 spin_unlock(&page_p->lock);
1098 static int check_vma(struct vm_area_struct *vma)
1101 return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || (vma->vm_flags & VM_ACCOUNT) ||
1102 !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
1103 !(vma->vm_flags & (VM_READ | VM_MAYREAD)));
1105 return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC));
1110 static void install_page_probes(unsigned long page, struct task_struct *task, struct proc_probes *proc_p, int atomic)
1113 struct mm_struct *mm;
1114 struct vm_area_struct *vma;
1116 mm_read_lock(task, mm, atomic, lock);
1118 vma = find_vma(mm, page);
1119 if (vma && check_vma(vma)) {
1120 struct file_probes *file_p = proc_p_find_file_p(proc_p, vma);
1122 struct page_probes *page_p;
1123 if (!file_p->loaded) {
1124 set_mapping_file(file_p, proc_p, task, vma);
1128 page_p = file_p_find_page_p_mapped(file_p, page);
1130 register_us_page_probe(page_p, file_p, task);
1135 mm_read_unlock(mm, atomic, lock);
1138 static void install_file_probes(struct task_struct *task, struct mm_struct *mm, struct file_probes *file_p)
1140 struct page_probes *page_p = NULL;
1141 struct hlist_node *node = NULL;
1142 struct hlist_head *head = NULL;
1143 int i, table_size = (1 << file_p->page_probes_hash_bits);
1145 for (i = 0; i < table_size; ++i) {
1146 head = &file_p->page_probes_table[i];
1147 hlist_for_each_entry_rcu(page_p, node, head, hlist) {
1148 if (page_present(mm, page_p->offset)) {
1149 register_us_page_probe(page_p, file_p, task);
1155 static void install_proc_probes(struct task_struct *task, struct proc_probes *proc_p, int atomic)
1158 struct vm_area_struct *vma;
1159 struct mm_struct *mm;
1161 mm_read_lock(task, mm, atomic, lock);
1163 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1164 if (check_vma(vma)) {
1165 struct file_probes *file_p = proc_p_find_file_p(proc_p, vma);
1167 if (!file_p->loaded) {
1168 set_mapping_file(file_p, proc_p, task, vma);
1172 install_file_probes(task, mm, file_p);
1177 mm_read_unlock(mm, atomic, lock);
1180 static int check_install_pages_in_file(struct task_struct *task, struct file_probes *file_p)
1183 int table_size = (1 << file_p->page_probes_hash_bits);
1184 struct page_probes *page_p;
1185 struct hlist_node *node, *tmp;
1186 struct hlist_head *head;
1188 for (i = 0; i < table_size; ++i) {
1189 head = &file_p->page_probes_table[i];
1190 hlist_for_each_entry_safe (page_p, node, tmp, head, hlist) {
1191 if (page_p->install) {
1200 static int unregister_us_file_probes(struct task_struct *task, struct file_probes *file_p, enum US_FLAGS flag)
1203 int table_size = (1 << file_p->page_probes_hash_bits);
1204 struct page_probes *page_p;
1205 struct hlist_node *node, *tmp;
1206 struct hlist_head *head;
1208 for (i = 0; i < table_size; ++i) {
1209 head = &file_p->page_probes_table[i];
1210 hlist_for_each_entry_safe (page_p, node, tmp, head, hlist) {
1211 err = unregister_us_page_probe(task, page_p, flag);
1219 if (flag != US_DISARM) {
1226 static int uninstall_us_proc_probes(struct task_struct *task, struct proc_probes *proc_p, enum US_FLAGS flag)
1229 struct file_probes *file_p;
1231 list_for_each_entry_rcu(file_p, &proc_p->file_list, list) {
1232 err = unregister_us_file_probes(task, file_p, flag);
1242 static pid_t find_proc_by_task(const struct task_struct *task, const struct dentry *dentry)
1244 struct vm_area_struct *vma;
1245 struct mm_struct *mm = task->active_mm;
1250 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1251 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
1252 if (vma->vm_file->f_dentry == dentry) {
1256 if (is_slp_app_with_dentry(vma, dentry)) {
1259 #endif /* SLP_APP */
1261 if (is_android_app_with_dentry(vma, dentry)) {
1264 #endif /* ANDROID_APP */
1271 void do_page_fault_ret_pre_code (void)
1273 struct task_struct *task = current->group_leader;
1274 struct mm_struct *mm = task->mm;
1275 struct vm_area_struct *vma = 0;
1276 struct proc_probes *proc_p = NULL;
1278 * Because process threads have same address space
1279 * we instrument only group_leader of all this threads
1281 unsigned long addr = 0;
1285 struct timeval imi_tv1;
1286 struct timeval imi_tv2;
1287 #define USEC_IN_SEC_NUM 1000000
1289 if (task->flags & PF_KTHREAD) {
1290 DPRINTF("ignored kernel thread %d\n", task->pid);
1294 if (!is_us_instrumentation()) {
1298 addr = (unsigned long)swap_get_entry_data(&sa_dpf);
1301 printk("WARNING: do_page_fault_ret_pre_code addr = 0\n");
1308 valid_addr = mm && page_present(mm, addr);
1314 proc_p = get_proc_probes_by_task_or_new(task);
1317 if (us_proc_info.tgid == 0) {
1318 pid_t tgid = find_proc_by_task(task, us_proc_info.m_f_dentry);
1320 us_proc_info.tgid = gl_nNotifyTgid = tgid;
1324 if (us_proc_info.tgid == task->tgid) {
1325 proc_p = us_proc_info.pp;
1330 unsigned long page = addr & PAGE_MASK;
1333 if (is_java_inst_enabled()) {
1334 find_libdvm_for_task(task, &us_proc_info);
1336 #endif /* __ANDROID */
1339 do_gettimeofday(&imi_tv1);
1340 install_page_probes(page, task, proc_p, 1);
1341 do_gettimeofday(&imi_tv2);
1343 imi_sum_time += ((imi_tv2.tv_sec - imi_tv1.tv_sec) * USEC_IN_SEC_NUM +
1344 (imi_tv2.tv_usec - imi_tv1.tv_usec));
1348 EXPORT_SYMBOL_GPL(do_page_fault_ret_pre_code);
1351 void do_exit_probe_pre_code (void)
1353 // TODO: remove task
1355 EXPORT_SYMBOL_GPL(do_exit_probe_pre_code);
1357 int check_vma_area(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1359 return (vma->vm_start >= start && vma->vm_end <= end);
1362 void print_vma(struct mm_struct *mm)
1364 struct vm_area_struct *vma;
1365 printk("### print_vma: START\n");\
1366 printk("### print_vma: START\n");
1368 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1369 char *x = vma->vm_flags & VM_EXEC ? "x" : "-";
1370 char *r = vma->vm_flags & VM_READ ? "r" : "-";
1371 char *w = vma->vm_flags & VM_WRITE ? "w" : "-";
1372 char *name = vma->vm_file ? vma->vm_file->f_dentry->d_iname : "N/A";
1374 printk("### [%8x..%8x] %s%s%s pgoff=\'%8u\' %s\n",
1375 vma->vm_start, vma->vm_end, x, r, w, vma->vm_pgoff, name);
1377 printk("### print_vma: END\n");
1380 static int remove_unmap_probes(struct task_struct *task, struct proc_probes *proc_p, unsigned long start, size_t len)
1382 struct mm_struct *mm = task->mm;
1383 struct vm_area_struct *vma;
1384 unsigned long end, pointer, step;
1386 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
1390 if ((len = PAGE_ALIGN(len)) == 0) {
1394 vma = find_vma(mm, start);
1395 if (vma && check_vma(vma)) {
1396 struct file_probes *file_p;
1397 unsigned long end = start + len;
1399 file_p = proc_p_find_file_p(proc_p, vma);
1401 if (vma->vm_start == start || vma->vm_end == end) {
1402 unregister_us_file_probes(task, file_p, US_NOT_RP2);
1406 struct page_probes *page_p;
1408 for (page = vma->vm_start; page < vma->vm_end; page += PAGE_SIZE) {
1409 page_p = file_p_find_page_p_mapped(file_p, page);
1411 unregister_us_page_probe(task, page_p, US_NOT_RP2);
1415 if (check_install_pages_in_file(task, file_p)) {
1425 void do_munmap_probe_pre_code(struct mm_struct *mm, unsigned long start, size_t len)
1427 struct proc_probes *proc_p = NULL;
1428 struct task_struct *task = current;
1430 //if user-space instrumentation is not set
1431 if (!is_us_instrumentation()) {
1436 proc_p = get_proc_probes_by_task(task);
1438 if (task->tgid == us_proc_info.tgid) {
1439 proc_p = us_proc_info.pp;
1444 if (remove_unmap_probes(task, proc_p, start, len)) {
1445 printk("ERROR do_munmap: start=%x, len=%x\n", start, len);
1449 EXPORT_SYMBOL_GPL(do_munmap_probe_pre_code);
1451 void mm_release_probe_pre_code(void)
1453 struct task_struct *task = current;
1454 struct proc_probes *proc_p = NULL;
1456 if (!is_us_instrumentation() || task->tgid != task->pid) {
1461 proc_p = get_proc_probes_by_task(task);
1463 if (task->tgid == us_proc_info.tgid) {
1464 proc_p = get_proc_probes_by_task(task);
1465 us_proc_info.tgid = 0;
1470 int ret = uninstall_us_proc_probes(task, proc_p, US_NOT_RP2);
1472 EPRINTF ("failed to uninstall IPs (%d)!", ret);
1475 dbi_unregister_all_uprobes(task, 1);
1478 EXPORT_SYMBOL_GPL(mm_release_probe_pre_code);
1481 static void recover_child(struct task_struct *child_task, struct proc_probes *proc_p)
1483 uninstall_us_proc_probes(child_task, proc_p, US_DISARM);
1486 static void rm_uprobes_child(struct task_struct *new_task)
1489 struct proc_probes *proc_p = get_proc_probes_by_task(current);
1491 recover_child(new_task, proc_p);
1494 if(us_proc_info.tgid == current->tgid) {
1495 recover_child(new_task, us_proc_info.pp);
1500 void copy_process_ret_pre_code(struct task_struct *p)
1505 if(p->mm != current->mm) // check flags CLONE_VM
1506 rm_uprobes_child(p);
1510 DEFINE_PER_CPU(struct us_ip *, gpCurIp) = NULL;
1511 EXPORT_PER_CPU_SYMBOL_GPL(gpCurIp);
1512 DEFINE_PER_CPU(struct pt_regs *, gpUserRegs) = NULL;
1513 EXPORT_PER_CPU_SYMBOL_GPL(gpUserRegs);
1516 unsigned long ujprobe_event_pre_handler(struct us_ip *ip, struct pt_regs *regs)
1518 __get_cpu_var (gpCurIp) = ip;
1519 __get_cpu_var (gpUserRegs) = regs;
1524 int handle_java_event(unsigned long addr)
1526 unsigned long start = 0;
1527 struct pt_regs *regs = __get_cpu_var(gpUserRegs);
1530 /* TODO: some stuff here */
1532 start = us_proc_info.libdvm_start;
1534 unsigned long end = us_proc_info.libdvm_end;
1536 if (addr == start + LIBDVM_ENTRY) {
1537 unsigned long *p_met = (unsigned long *)regs->ARM_r0;
1538 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1539 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1540 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1541 if (!cl_name || !met_name) {
1542 EPRINTF("warn: class name or method name null\n");
1544 pack_event_info(JAVA_PROBE_ID, RECORD_ENTRY, "pss", addr, cl_name, met_name);
1546 dbi_uprobe_return ();
1550 if (addr == start + LIBDVM_RETURN) {
1551 unsigned long *p_th = (unsigned long *)regs->ARM_r6;
1552 unsigned long *p_st = p_th;
1553 unsigned long *p_met = p_st ? (unsigned long *)p_st[2] : 0;
1554 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1555 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1556 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1557 if (!cl_name || !met_name) {
1558 EPRINTF("warn: class name or method name null\n");
1560 pack_event_info(JAVA_PROBE_ID, RECORD_RET, "pss", addr, cl_name, met_name);
1562 dbi_uprobe_return ();
1568 #endif /* __ANDROID */
1570 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
1572 struct us_ip *ip = __get_cpu_var(gpCurIp);
1573 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1576 if (is_java_inst_enabled() && handle_java_event(addr)) {
1579 #endif /* __ANDROID */
1582 #if defined(CONFIG_ARM)
1583 if (ip->offset & 0x01)
1585 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr | 0x01, arg1, arg2, arg3, arg4, arg5, arg6);
1587 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1590 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1592 // Mr_Nobody: uncomment for valencia
1593 //unregister_usprobe(current, ip, 1);
1594 dbi_uprobe_return ();
1597 void send_plt(struct us_ip *ip)
1599 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1600 struct vm_area_struct *vma = find_vma(current->mm, addr);
1602 if (vma && check_vma(vma)) {
1604 unsigned long real_addr;
1605 unsigned long real_got = vma->vm_flags & VM_EXECUTABLE ?
1607 ip->got_addr + vma->vm_start;
1609 if (!read_proc_vm_atomic(current, real_got, &real_addr, sizeof(real_addr))) {
1610 printk("Failed to read got %p at memory address %p!\n", ip->got_addr, real_got);
1614 vma = find_vma(current->mm, real_addr);
1615 if (vma && (vma->vm_start <= real_addr) && (vma->vm_end > real_addr)) {
1616 name = vma->vm_file ? vma->vm_file->f_dentry->d_iname : NULL;
1618 printk("Failed to get vma, includes %x address\n", real_addr);
1623 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppsp", addr, real_addr, name, real_addr - vma->vm_start);
1625 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppp", addr, real_addr, real_addr - vma->vm_start);
1630 int uretprobe_event_handler(struct kretprobe_instance *probe, struct pt_regs *regs, struct us_ip *ip)
1632 int retval = regs_return_value(regs);
1633 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1635 if (ip->got_addr && ip->flag_got == 0) {
1640 #if defined(CONFIG_ARM)
1641 if (ip->offset & 0x01)
1643 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr | 0x01, retval);
1645 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1648 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1650 // Mr_Nobody: uncomment for valencia
1651 //unregister_usprobe(current, ip, 1);
1655 static int register_usprobe(struct task_struct *task, struct us_ip *ip, int atomic)
1658 ip->jprobe.kp.tgid = task->tgid;
1660 if (ip->jprobe.entry == NULL) {
1661 ip->jprobe.entry = (kprobe_opcode_t *)ujprobe_event_handler;
1662 DPRINTF("Set default event handler for %x\n", ip->offset);
1665 if (ip->jprobe.pre_entry == NULL) {
1666 ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t)ujprobe_event_pre_handler;
1667 DPRINTF("Set default pre handler for %x\n", ip->offset);
1670 ip->jprobe.priv_arg = ip;
1671 ret = dbi_register_ujprobe(task, &ip->jprobe, atomic);
1673 DPRINTF ("dbi_register_ujprobe() failure %d", ret);
1677 if (ip->flag_retprobe) {
1678 // Mr_Nobody: comment for valencia
1679 ip->retprobe.kp.tgid = task->tgid;
1680 if (ip->retprobe.handler == NULL) {
1681 ip->retprobe.handler = (kretprobe_handler_t)uretprobe_event_handler;
1682 DPRINTF("Set default ret event handler for %x\n", ip->offset);
1685 ip->retprobe.priv_arg = ip;
1686 ret = dbi_register_uretprobe(task, &ip->retprobe, atomic);
1688 EPRINTF ("dbi_register_uretprobe() failure %d", ret);
1696 static int unregister_usprobe(struct task_struct *task, struct us_ip *ip, int atomic, int not_rp2)
1698 dbi_unregister_ujprobe(task, &ip->jprobe, atomic);
1700 if (ip->flag_retprobe) {
1701 dbi_unregister_uretprobe(task, &ip->retprobe, atomic, not_rp2);
1707 unsigned long get_stack_size(struct task_struct *task,
1708 struct pt_regs *regs)
1710 #ifdef CONFIG_ADD_THREAD_STACK_INFO
1711 return (task->stack_start - dbi_get_stack_ptr(regs));
1713 struct vm_area_struct *vma = NULL;
1714 struct mm_struct *mm = NULL;
1715 unsigned long result = 0;
1716 int atomic = in_atomic();
1718 mm = (atomic ? task->active_mm: get_task_mm(task));
1722 down_read(&mm->mmap_sem);
1724 vma = find_vma(mm, dbi_get_stack_ptr(regs));
1727 result = vma->vm_end - dbi_get_stack_ptr(regs);
1732 up_read(&mm->mmap_sem);
1740 EXPORT_SYMBOL_GPL(get_stack_size);
1742 unsigned long get_stack(struct task_struct *task, struct pt_regs *regs,
1743 char *buf, unsigned long sz)
1745 unsigned long stack_sz = get_stack_size(task, regs);
1746 unsigned long real_sz = (stack_sz > sz ? sz: stack_sz);
1747 int res = read_proc_vm_atomic(task, dbi_get_stack_ptr(regs), buf, real_sz);
1750 EXPORT_SYMBOL_GPL(get_stack);
1752 int dump_to_trace(probe_id_t probe_id, void *addr, const char *buf,
1755 unsigned long rest_sz = sz;
1756 const char *data = buf;
1758 while (rest_sz >= EVENT_MAX_SIZE) {
1759 pack_event_info(probe_id, RECORD_ENTRY, "pa",
1760 addr, EVENT_MAX_SIZE, data);
1761 rest_sz -= EVENT_MAX_SIZE;
1762 data += EVENT_MAX_SIZE;
1766 pack_event_info(probe_id, RECORD_ENTRY, "pa", addr, rest_sz, data);
1770 EXPORT_SYMBOL_GPL(dump_to_trace);
1772 int dump_backtrace(probe_id_t probe_id, struct task_struct *task,
1773 void *addr, struct pt_regs *regs, unsigned long sz)
1775 unsigned long real_sz = 0;
1778 buf = (char *)kmalloc(sz, GFP_ATOMIC);
1781 real_sz = get_stack(task, regs, buf, sz);
1783 dump_to_trace(probe_id, addr, buf, real_sz);
1790 EXPORT_SYMBOL_GPL(dump_backtrace);
1792 unsigned long get_ret_addr(struct task_struct *task, struct us_ip *ip)
1794 unsigned long retaddr = 0;
1795 struct hlist_node *item, *tmp_node;
1796 struct kretprobe_instance *ri;
1799 hlist_for_each_safe (item, tmp_node, &ip->retprobe.used_instances) {
1800 ri = hlist_entry (item, struct kretprobe_instance, uflist);
1802 if (ri->task && ri->task->pid == task->pid &&
1803 ri->task->tgid == task->tgid)
1804 retaddr = (unsigned long)ri->ret_addr;
1811 return dbi_get_ret_addr(task_pt_regs(task));
1813 EXPORT_SYMBOL_GPL(get_ret_addr);