1 ////////////////////////////////////////////////////////////////////////////////////
6 // This file is C source for SWAP.
9 // AUTHOR: L.Komkov, S.Dianov, A.Gerenkov, S.Andreev
10 // COMPANY NAME: Samsung Research Center in Moscow
11 // DEPT NAME: Advanced Software Group
12 // CREATED: 2008.02.15
14 // REVISION DATE: 2008.12.03
16 ////////////////////////////////////////////////////////////////////////////////////
18 #include <linux/types.h>
19 #include <linux/hash.h>
20 #include <linux/list.h>
21 #include <linux/unistd.h>
22 #include <linux/spinlock.h>
23 #include <linux/kernel.h>
24 #include <linux/time.h>
27 #include "handlers_core.h"
30 #define after_buffer ec_info.buffer_size
32 char *p_buffer = NULL;
33 inst_us_proc_t us_proc_info;
34 struct list_head otg_us_proc_info;
35 inst_dex_proc_t dex_proc_info;
38 unsigned int inst_pid = 0;
39 struct hlist_head kernel_probes;
40 struct hlist_head otg_kernel_probes;
42 struct cond cond_list;
43 int paused = 0; /* a state after a stop condition (events are not collected) */
44 struct timeval last_attach_time = {0, 0};
46 struct dbi_modules_handlers dbi_mh;
49 // Mr_Nobody: should we use centralized definition of this structure??
52 unsigned long func_addr;
53 unsigned long jp_handler_addr;
54 unsigned long rp_handler_addr;
57 struct dbi_modules_handlers *get_dbi_modules_handlers(void)
61 EXPORT_SYMBOL_GPL(get_dbi_modules_handlers);
63 inline unsigned long find_dbi_jp_handler(unsigned long p_addr, struct dbi_modules_handlers_info *mhi)
67 /* Possibly we can find less expensive way */
68 for (i = 0; i < mhi->dbi_nr_handlers; i++) {
69 if (mhi->dbi_handlers[i].func_addr == p_addr) {
70 printk("Found jp_handler for %0lX address of %s module\n", p_addr, mhi->dbi_module->name);
71 return mhi->dbi_handlers[i].jp_handler_addr;
77 inline unsigned long find_dbi_rp_handler(unsigned long p_addr, struct dbi_modules_handlers_info *mhi)
81 /* Possibly we can find less expensive way */
82 for (i = 0; i < mhi->dbi_nr_handlers; i++) {
83 if (mhi->dbi_handlers[i].func_addr == p_addr) {
84 printk("Found rp_handler for %0lX address of %s module\n", p_addr, mhi->dbi_module->name);
85 return mhi->dbi_handlers[i].rp_handler_addr;
92 * Search of handler in global list of modules for defined probe
94 void dbi_find_and_set_handler_for_probe(kernel_probe_t *p)
96 unsigned long jp_handler_addr, rp_handler_addr;
97 struct dbi_modules_handlers_info *local_mhi;
98 unsigned long dbi_flags;
99 unsigned int local_module_refcount = 0;
101 spin_lock_irqsave(&dbi_mh.lock, dbi_flags);
102 list_for_each_entry_rcu(local_mhi, &dbi_mh.modules_handlers, dbi_list_head) {
103 printk("Searching handlers in %s module for %0lX address\n",
104 (local_mhi->dbi_module)->name, p->addr);
105 // XXX: absent code for pre_handlers because we suppose that they are not used
106 if ((jp_handler_addr = find_dbi_jp_handler(p->addr, local_mhi)) != 0) {
107 if (p->jprobe.entry != 0) {
108 printk("Skipping jp_handler for %s module (address %0lX)\n",
109 (local_mhi->dbi_module)->name, p->addr);
112 local_module_refcount = module_refcount(local_mhi->dbi_module);
113 if (local_module_refcount == 0) {
114 if (!try_module_get(local_mhi->dbi_module))
115 printk("Error of try_module_get() for module %s\n",
116 (local_mhi->dbi_module)->name);
118 printk("Module %s in use now\n",
119 (local_mhi->dbi_module)->name);
121 p->jprobe.entry = (kprobe_opcode_t *)jp_handler_addr;
122 printk("Set jp_handler for %s module (address %0lX)\n",
123 (local_mhi->dbi_module)->name, p->addr);
126 if ((rp_handler_addr = find_dbi_rp_handler(p->addr, local_mhi)) != 0) {
127 if (p->retprobe.handler != 0) {
128 printk("Skipping kretprobe_handler for %s module (address %0lX)\n",
129 (local_mhi->dbi_module)->name, p->addr);
132 local_module_refcount = module_refcount(local_mhi->dbi_module);
133 if (local_module_refcount == 0) {
134 if (!try_module_get(local_mhi->dbi_module))
135 printk("Error of try_module_get() for module %s\n",
136 (local_mhi->dbi_module)->name);
138 printk("Module %s in use now\n",
139 (local_mhi->dbi_module)->name);
141 p->retprobe.handler = (kretprobe_handler_t)rp_handler_addr;
142 printk("Set rp_handler for %s module (address %0lX)\n",
143 (local_mhi->dbi_module)->name, p->addr);
147 // not found pre_handler - set default (always true for now since pre_handlers not used)
148 if (p->jprobe.pre_entry == 0) {
149 p->jprobe.pre_entry = (kprobe_pre_entry_handler_t) def_jprobe_event_pre_handler;
150 printk("Set default pre_handler (address %0lX)\n", p->addr);
152 // not found jp_handler - set default
153 if (p->jprobe.entry == 0) {
154 p->jprobe.entry = (kprobe_opcode_t *) def_jprobe_event_handler;
155 printk("Set default jp_handler (address %0lX)\n", p->addr);
157 // not found kretprobe_handler - set default
158 if (p->retprobe.handler == 0) {
159 p->retprobe.handler = (kretprobe_handler_t) def_retprobe_event_handler;
160 printk("Set default rp_handler (address %0lX)\n", p->addr);
162 spin_unlock_irqrestore(&dbi_mh.lock, dbi_flags);
165 // XXX TODO: possible mess when start-register/unregister-stop operation
166 // so we should refuse register/unregister operation while we are in unsafe state
167 int dbi_register_handlers_module(struct dbi_modules_handlers_info *dbi_mhi)
169 unsigned long dbi_flags;
170 // struct dbi_modules_handlers_info *local_mhi;
172 spin_lock_irqsave(&dbi_mh.lock, dbi_flags);
173 // local_mhi = container_of(&dbi_mhi->dbi_list_head, struct dbi_modules_handlers_info, dbi_list_head);
174 list_add_rcu(&dbi_mhi->dbi_list_head, &dbi_mh.modules_handlers);
175 printk("Added module %s (head is %p)\n", (dbi_mhi->dbi_module)->name, &dbi_mhi->dbi_list_head);
176 spin_unlock_irqrestore(&dbi_mh.lock, dbi_flags);
179 EXPORT_SYMBOL_GPL(dbi_register_handlers_module);
181 // XXX TODO: possible mess when start-register/unregister-stop operation
182 // so we should refuse register/unregister operation while we are in unsafe state
183 int dbi_unregister_handlers_module(struct dbi_modules_handlers_info *dbi_mhi)
185 unsigned long dbi_flags;
186 // Next code block is for far future possible usage in case when removing will be implemented for unsafe state
187 // (i.e. between attach and stop)
189 struct hlist_node *node;
190 unsigned long jp_handler_addr, rp_handler_addr, pre_handler_addr;*/
192 spin_lock_irqsave(&dbi_mh.lock, dbi_flags);
193 list_del_rcu(&dbi_mhi->dbi_list_head);
194 // Next code block is for far future possible usage in case when removing will be implemented for unsafe state
195 // (i.e. between attach and stop)
196 /*hlist_for_each_entry_rcu (p, node, &kernel_probes, hlist) {
197 // XXX: absent code for pre_handlers because we suppose that they are not used
198 if ((p->jprobe.entry != ((kprobe_pre_entry_handler_t )def_jprobe_event_pre_handler)) ||
199 (p->retprobe.handler != ((kretprobe_handler_t )def_retprobe_event_handler))) {
200 printk("Searching handlers for %p address for removing in %s registered module...\n",
201 p->addr, (dbi_mhi->dbi_module)->name);
202 jp_handler_addr = find_dbi_jp_handler(p->addr, dbi_mhi);
203 rp_handler_addr = find_dbi_rp_handler(p->addr, dbi_mhi);
204 if ((jp_handler_addr != 0) || (rp_handler_addr != 0)) {
205 // search and set to another handlers or default
206 dbi_find_and_set_handler_for_probe(p);
207 printk("Removed handler(s) for %s module (address %p)\n",
208 (dbi_mhi->dbi_module)->name, p->addr);
212 printk("Removed module %s (head was %p)\n", (dbi_mhi->dbi_module)->name, &dbi_mhi->dbi_list_head);
213 spin_unlock_irqrestore(&dbi_mh.lock, dbi_flags);
216 EXPORT_SYMBOL_GPL(dbi_unregister_handlers_module);
218 EXPORT_SYMBOL_GPL(us_proc_info);
219 EXPORT_SYMBOL_GPL(dex_proc_info);
220 typedef void *(*get_my_uprobes_info_t)(void);
221 int (*mec_post_event)(char *data, unsigned long len) = NULL;
223 unsigned copy_into_cyclic_buffer (char *buffer, unsigned dst_offset, char *src, unsigned size)
225 unsigned nOffset = dst_offset;
228 buffer[nOffset++] = *pSource++;
232 unsigned copy_from_cyclic_buffer (char *dst, char *buffer, unsigned src_offset, unsigned size)
234 unsigned nOffset = src_offset;
235 char* pDestination = dst;
237 *pDestination++ = buffer[nOffset++];
241 int CheckBufferSize (unsigned int nSize)
243 if (nSize < EC_BUFFER_SIZE_MIN) {
244 EPRINTF("Too small buffer size! [Size=%u KB]", nSize / 1024);
247 if (nSize > EC_BUFFER_SIZE_MAX) {
248 EPRINTF("Too big buffer size! [Size=%u KB]", nSize / 1024);
254 int AllocateSingleBuffer(unsigned int nSize)
256 unsigned long spinlock_flags = 0L;
258 unsigned int nSubbufferSize = ec_info.m_nSubbufSize;
259 unsigned int nNumOfSubbufers = GetNumOfSubbuffers(nSize);
260 unsigned long nAllocatedSize = nSubbufferSize * nNumOfSubbufers;
262 p_buffer = vmalloc_user(nAllocatedSize);
264 EPRINTF("Memory allocation error! [Size=%lu KB]", nAllocatedSize / 1024);
268 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
269 ec_info.m_nNumOfSubbuffers = nNumOfSubbufers;
270 ec_info.buffer_effect = ec_info.buffer_size = nAllocatedSize;
271 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
276 void FreeSingleBuffer (void)
278 VFREE_USER(p_buffer, ec_info.buffer_size);
282 //////////////////////////////////////////////////////////////////////////////////////////////////
284 int EnableContinuousRetrieval() {
285 unsigned long spinlock_flags = 0L;
287 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
288 ec_info.m_nMode |= MODEMASK_CONTINUOUS_RETRIEVAL;
289 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
294 int DisableContinuousRetrieval() {
295 unsigned long spinlock_flags = 0L;
297 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
298 ec_info.m_nMode &= ~MODEMASK_CONTINUOUS_RETRIEVAL;
299 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
304 //////////////////////////////////////////////////////////////////////////////////////////////////
306 #ifndef __DISABLE_RELAYFS
308 struct rchan* gl_pRelayChannel = NULL;
309 struct rchan* GetRelayChannel(void) { return gl_pRelayChannel; };
311 struct dentry* gl_pdirRelay = NULL;
312 struct dentry* GetRelayDir(void) { return gl_pdirRelay; };
316 struct proc_dir_entry* alt_pde = NULL;
318 static inline struct dentry *_dir_create (const char *dirname, struct dentry *parent, struct proc_dir_entry **p2pde)
321 struct proc_dir_entry *pde;
323 pde = proc_mkdir (dirname, PDE (parent->d_inode));
330 mutex_lock (&parent->d_inode->i_mutex);
331 dir = lookup_one_len (dirname, parent, strlen (dirname));
332 mutex_unlock (&parent->d_inode->i_mutex);
337 remove_proc_entry (dirname, PDE (parent->d_inode));
346 static inline struct dentry *_get_proc_root (void)
348 struct file_system_type *procfs_type;
349 struct super_block *procfs_sb;
351 procfs_type = get_fs_type ("proc");
353 if (!procfs_type || list_empty (&procfs_type->fs_supers))
356 procfs_sb = list_entry (procfs_type->fs_supers.next, \
357 struct super_block, s_instances);
359 return procfs_sb->s_root;
363 static struct dentry *create_buf (const char *filename, struct dentry *parent, int mode, struct rchan_buf *buf, int *is_global)
365 struct proc_dir_entry *pde;
366 struct proc_dir_entry *parent_pde = NULL;
367 struct dentry *dentry;
370 parent_pde = PDE (parent->d_inode);
372 parent = _get_proc_root ();
374 pde = create_proc_entry (filename, S_IFREG|S_IRUSR, parent_pde);
379 pde->proc_fops = &relay_file_operations;
381 mutex_lock (&parent->d_inode->i_mutex);
382 dentry = lookup_one_len (filename, parent, strlen (filename));
383 mutex_unlock (&parent->d_inode->i_mutex);
385 if (IS_ERR(dentry)) {
386 remove_proc_entry (filename, parent_pde);
389 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
390 dentry->d_inode->i_private = buf;
392 dentry->d_inode->u.generic_ip = buf;
399 static int remove_buf (struct dentry *dentry)
403 struct proc_dir_entry *pde = PDE (dentry->d_inode);
405 remove_proc_entry (pde->name, pde->parent);
411 #endif // __USE_PROCFS
413 * subbuf_start - called on buffer-switch to a new sub-buffer
414 * @buf: the channel buffer containing the new sub-buffer
415 * @subbuf: the start of the new sub-buffer
416 * @prev_subbuf: the start of the previous sub-buffer
417 * @prev_padding: unused space at the end of previous sub-buffer
419 * The client should return 1 to continue logging, 0 to stop
422 * NOTE: subbuf_start will also be invoked when the buffer is
423 * created, so that the first sub-buffer can be initialized
424 * if necessary. In this case, prev_subbuf will be NULL.
426 * NOTE: the client can reserve bytes at the beginning of the new
427 * sub-buffer by calling subbuf_start_reserve() in this callback.
429 int RelayCallbackSubbufStart(struct rchan_buf *buf,
434 struct rchan* pRelayChannel = NULL;
435 unsigned int nNumOfSubbufs = 0;
437 unsigned long spinlock_flags = 0L;
438 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
440 subbuf_start_reserve(buf, RELAY_SUBBUF_HEADER_SIZE);
441 ec_info.buffer_effect += RELAY_SUBBUF_HEADER_SIZE;
442 ec_info.m_nEndOffset = RELAY_SUBBUF_HEADER_SIZE;
444 if(prev_subbuf == NULL) {
445 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
448 memcpy(prev_subbuf, &prev_padding, sizeof(unsigned int));
449 memcpy(prev_subbuf + sizeof(unsigned int), &ec_info.m_nSubbufSavedEvents, sizeof(unsigned int));
450 ec_info.m_nSubbufSavedEvents = 0;
451 pRelayChannel = GetRelayChannel();
452 if(pRelayChannel == NULL) {
453 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
454 EPRINTF("Null pointer to relay channel!");
457 nNumOfSubbufs = pRelayChannel->n_subbufs;
458 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
459 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
460 if(relay_buf_full(buf)) {
461 void* pConsume = NULL;
462 unsigned int nPaddingLength = 0;
463 unsigned int nSubbufSize = 0;
464 unsigned int nDataSize = 0;
465 unsigned int nEffectSize = 0;
466 unsigned int nSubbufDiscardedCount = 0;
467 nSubbufSize = pRelayChannel->subbuf_size;
468 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
469 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
470 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
471 nEffectSize = nSubbufSize - nPaddingLength;
472 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
473 ec_info.discarded_events_count += nSubbufDiscardedCount;
474 relay_subbufs_consumed(pRelayChannel, 0, 1);
475 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
476 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
477 ec_info.buffer_effect -= nEffectSize;
478 ec_info.trace_size -= nDataSize;
479 buf->dentry->d_inode->i_size = ec_info.trace_size;
480 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
481 return 1; // Overwrite mode
483 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
488 * buf_mapped - relay buffer mmap notification
489 * @buf: the channel buffer
490 * @filp: relay file pointer
492 * Called when a relay file is successfully mmapped
494 void RelayCallbackBufMapped(struct rchan_buf *buf,
500 * buf_unmapped - relay buffer unmap notification
501 * @buf: the channel buffer
502 * @filp: relay file pointer
504 * Called when a relay file is successfully unmapped
506 void RelayCallbackBufUnmapped(struct rchan_buf *buf,
511 * create_buf_file - create file to represent a relay channel buffer
512 * @filename: the name of the file to create
513 * @parent: the parent of the file to create
514 * @mode: the mode of the file to create
515 * @buf: the channel buffer
516 * @is_global: outparam - set non-zero if the buffer should be global
518 * Called during relay_open(), once for each per-cpu buffer,
519 * to allow the client to create a file to be used to
520 * represent the corresponding channel buffer. If the file is
521 * created outside of relay, the parent must also exist in
524 * The callback should return the dentry of the file created
525 * to represent the relay buffer.
527 * Setting the is_global outparam to a non-zero value will
528 * cause relay_open() to create a single global buffer rather
529 * than the default set of per-cpu buffers.
531 * See Documentation/filesystems/relayfs.txt for more info.
533 struct dentry * RelayCallbackCreateBufFile(const char *filename,
534 struct dentry *parent,
536 struct rchan_buf *buf,
541 DPRINTF("\"%s\" is creating in procfs...!", filename);
542 return create_buf(filename, parent, mode, buf, is_global);
544 DPRINTF("\"%s\" is creating in debugfs...!", filename);
545 return debugfs_create_file(filename, (mode_t)mode, parent, buf, &relay_file_operations);
546 #endif // __USE_PROCFS
550 * remove_buf_file - remove file representing a relay channel buffer
551 * @dentry: the dentry of the file to remove
553 * Called during relay_close(), once for each per-cpu buffer,
554 * to allow the client to remove a file used to represent a
557 * The callback should return 0 if successful, negative if not.
559 int RelayCallbackRemoveBufFile(struct dentry *dentry)
564 debugfs_remove(dentry);
565 #endif // __USE_PROCFS
569 struct rchan_callbacks gl_RelayCallbacks = {
570 .subbuf_start = RelayCallbackSubbufStart,
571 .buf_mapped = RelayCallbackBufMapped,
572 .buf_unmapped = RelayCallbackBufUnmapped,
573 .create_buf_file = RelayCallbackCreateBufFile,
574 .remove_buf_file = RelayCallbackRemoveBufFile
576 #endif //__DISABLE_RELAYFS
578 int AllocateMultipleBuffer(unsigned int nSize) {
579 #ifndef __DISABLE_RELAYFS
580 unsigned long spinlock_flags = 0L;
582 unsigned int nSubbufferSize = ec_info.m_nSubbufSize;
583 unsigned int nNumOfSubbufers = GetNumOfSubbuffers(nSize);
585 gl_pRelayChannel = relay_open(DEFAULT_RELAY_BASE_FILENAME,
590 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
594 if(gl_pRelayChannel == NULL) {
595 EPRINTF("Cannot create relay buffer channel! [%d subbufers by %u Kb = %u Kb]",
596 nNumOfSubbufers, nSubbufferSize / 1024, nSize / 1024);
600 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
601 ec_info.m_nNumOfSubbuffers = nNumOfSubbufers;
602 ec_info.buffer_effect = ec_info.buffer_size = nSubbufferSize * nNumOfSubbufers;
603 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
607 EPRINTF("RelayFS not supported!");
609 #endif //__DISABLE_RELAYFS
612 void FreeMultipleBuffer(void) {
613 #ifndef __DISABLE_RELAYFS
614 relay_close(gl_pRelayChannel);
617 EPRINTF("RelayFS not supported!");
618 #endif //__DISABLE_RELAYFS
621 int InitializeBuffer(unsigned int nSize) {
622 if(IsMultipleBuffer())
623 return AllocateMultipleBuffer(nSize);
624 return AllocateSingleBuffer(nSize);
627 int UninitializeBuffer(void) {
628 if(IsMultipleBuffer())
629 FreeMultipleBuffer();
634 int EnableMultipleBuffer() {
635 unsigned long spinlock_flags = 0L;
637 if(IsMultipleBuffer())
640 if(UninitializeBuffer() == -1)
641 EPRINTF("Cannot uninitialize buffer!");
643 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
644 ec_info.m_nMode |= MODEMASK_MULTIPLE_BUFFER;
645 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
647 if(InitializeBuffer(GetBufferSize()) == -1) {
648 EPRINTF("Cannot initialize buffer!");
654 int DisableMultipleBuffer() {
655 unsigned long spinlock_flags = 0L;
657 if(!IsMultipleBuffer())
660 if(UninitializeBuffer() == -1)
661 EPRINTF("Cannot uninitialize buffer!");
663 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
664 ec_info.m_nMode &= ~MODEMASK_MULTIPLE_BUFFER;
665 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
667 if(InitializeBuffer(GetBufferSize()) == -1) {
668 EPRINTF("Cannot initialize buffer!");
674 unsigned int GetBufferSize(void) { return ec_info.buffer_size; };
676 int SetBufferSize(unsigned int nSize) {
677 if (GetECState() != EC_STATE_IDLE) {
678 EPRINTF("Buffer changes are allowed in IDLE state only (%d)!", GetECState());
681 if(GetBufferSize() == nSize)
683 if(CheckBufferSize(nSize) == -1) {
684 EPRINTF("Invalid buffer size!");
687 detach_selected_probes ();
688 if(UninitializeBuffer() == -1)
689 EPRINTF("Cannot uninitialize buffer!");
690 if(InitializeBuffer(nSize) == -1) {
691 EPRINTF("Cannot initialize buffer! [Size=%u KB]", nSize / 1024);
697 int SetPid(unsigned int pid)
699 if (GetECState() != EC_STATE_IDLE)
701 EPRINTF("PID changes are allowed in IDLE state only (%d)!", GetECState());
706 DPRINTF("SetPid pid:%d\n", pid);
710 void ResetSingleBuffer(void) {
713 void ResetMultipleBuffer(void) {
714 #ifndef __DISABLE_RELAYFS
715 relay_reset(gl_pRelayChannel);
717 EPRINTF("RelayFS not supported!");
718 #endif //__DISABLE_RELAYFS
721 int ResetBuffer(void) {
722 unsigned long spinlock_flags = 0L;
724 if (GetECState() != EC_STATE_IDLE) {
725 EPRINTF("Buffer changes are allowed in IDLE state only!");
729 if(IsMultipleBuffer())
730 ResetMultipleBuffer();
734 detach_selected_probes ();
736 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
737 ec_info.buffer_effect = ec_info.buffer_size;
738 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
745 int WriteEventIntoSingleBuffer(char* pEvent, unsigned long nEventSize) {
746 unsigned int unused_space;
749 EPRINTF("Invalid pointer to buffer!");
750 ++ec_info.lost_events_count;
753 if (ec_info.trace_size == 0 || ec_info.after_last > ec_info.first) {
754 unused_space = ec_info.buffer_size - ec_info.after_last;
755 if (unused_space > nEventSize) {
756 ec_info.after_last = copy_into_cyclic_buffer(p_buffer,
760 ec_info.saved_events_count++;
761 ec_info.buffer_effect = ec_info.buffer_size;
762 ec_info.trace_size = ec_info.after_last - ec_info.first;
764 if (ec_info.first > nEventSize) {
765 ec_info.buffer_effect = ec_info.after_last;
766 ec_info.after_last = copy_into_cyclic_buffer(p_buffer,
770 ec_info.saved_events_count++;
771 ec_info.trace_size = ec_info.buffer_effect
773 + ec_info.after_last;
775 // TODO: consider two variants!
777 ec_info.discarded_events_count++;
781 unused_space = ec_info.first - ec_info.after_last;
782 if (unused_space > nEventSize) {
783 ec_info.after_last = copy_into_cyclic_buffer(p_buffer,
787 ec_info.saved_events_count++;
788 ec_info.trace_size = ec_info.buffer_effect
790 + ec_info.after_last;
793 ec_info.discarded_events_count++;
799 int WriteEventIntoMultipleBuffer(char* pEvent, unsigned long nEventSize) {
800 #ifndef __DISABLE_RELAYFS
801 unsigned long spinlock_flags = 0L;
802 __relay_write(GetRelayChannel(), pEvent, nEventSize);
803 ec_info.buffer_effect += nEventSize;
804 ec_info.trace_size += nEventSize;
805 ec_info.saved_events_count++;
806 ec_info.m_nEndOffset += nEventSize;
807 ec_info.m_nSubbufSavedEvents++;
810 EPRINTF("RelayFS not supported!");
812 #endif //__DISABLE_RELAYFS
815 int WriteEventIntoBuffer(char* pEvent, unsigned long nEventSize) {
818 for(i = 0; i < nEventSize; i++)
819 printk("%02X ", pEvent[i]);
822 if(IsMultipleBuffer())
823 return WriteEventIntoMultipleBuffer(pEvent, nEventSize);
824 return WriteEventIntoSingleBuffer(pEvent, nEventSize);
827 //////////////////////////////////////////////////////////////////////////////////////////////////
829 int set_event_mask (int new_mask)
831 unsigned long spinlock_flags = 0L;
832 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
833 event_mask = new_mask;
834 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
839 get_event_mask (int *mask)
846 generic_swap (void *a, void *b, int size)
851 *(char *) a++ = *(char *) b;
853 } while (--size > 0);
856 static void sort (void *base, size_t num, size_t size, int (*cmp) (const void *, const void *), void (*fswap) (void *, void *, int size))
858 /* pre-scale counters for performance */
859 int i = (num / 2) * size, n = num * size, c, r;
862 for (; i >= 0; i -= size)
864 for (r = i; r * 2 < n; r = c)
867 if (c < n - size && cmp (base + c, base + c + size) < 0)
869 if (cmp (base + r, base + c) >= 0)
871 fswap (base + r, base + c, size);
876 for (i = n - size; i >= 0; i -= size)
878 fswap (base, base + i, size);
879 for (r = 0; r * 2 < i; r = c)
882 if (c < i - size && cmp (base + c, base + c + size) < 0)
884 if (cmp (base + r, base + c) >= 0)
886 fswap (base + r, base + c, size);
891 static int addr_cmp (const void *a, const void *b)
893 return *(unsigned long *) a > *(unsigned long *) b ? -1 : 1;
896 char *find_lib_path(const char *lib_name)
898 char *p = deps + sizeof(size_t);
903 DPRINTF("p is at %s", p);
905 match = strstr(p, lib_name);
907 len = strlen(p) + 1; /* we are at path now */
911 DPRINTF("Found match: %s", match);
919 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27)
920 #define list_for_each_rcu(pos, head) __list_for_each_rcu(pos, head)
923 void unlink_bundle(void)
926 us_proc_lib_t *d_lib;
928 struct list_head *pos; //, *tmp;
931 path = us_proc_info.path;
932 us_proc_info.path = 0;
934 // first make sure "d_lib" is not used any more and only
935 // then release storage
936 if (us_proc_info.p_libs)
938 int count1 = us_proc_info.libs_count;
939 us_proc_info.libs_count = 0;
940 for (i = 0; i < count1; i++)
942 d_lib = &us_proc_info.p_libs[i];
945 // first make sure "d_lib->p_ips" is not used any more and only
946 // then release storage
947 //int count2 = d_lib->ips_count;
948 d_lib->ips_count = 0;
949 /*for (k = 0; k < count2; k++)
950 kfree ((void *) d_lib->p_ips[k].name);*/
951 vfree ((void *) d_lib->p_ips);
955 // first make sure "d_lib->p_vtps" is not used any more and only
956 // then release storage
957 int count2 = d_lib->vtps_count;
958 d_lib->vtps_count = 0;
959 for (k = 0; k < count2; k++)
961 //list_for_each_safe_rcu(pos, tmp, &d_lib->p_vtps[k].list) {
962 list_for_each (pos, &d_lib->p_vtps[k].list)
964 us_proc_vtp_data_t *vtp = list_entry (pos, us_proc_vtp_data_t, list);
970 kfree ((void *) d_lib->p_vtps);
972 d_lib->plt_count = 0;
973 kfree((void*) d_lib->p_plt);
974 us_proc_info.is_plt = 0;
976 kfree ((void *) us_proc_info.p_libs);
977 us_proc_info.p_libs = 0;
981 /* kfree ((void *) path); */
982 /* //putname(path); */
985 us_proc_info.tgid = 0;
987 /* OTG probes list cleaning */
988 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
989 list_del_rcu(&p->list);
993 struct proc_probes *get_file_probes(const inst_us_proc_t *task_inst_info);
994 void print_inst_us_proc(const inst_us_proc_t *task_inst_info);
996 extern struct dentry *dentry_by_path(const char *path);
1000 get_my_uprobes_info_t get_uprobes = NULL;
1001 inst_us_proc_t *my_uprobes_info = 0;
1002 inst_us_proc_t empty_uprobes_info =
1007 char *p = bundle; /* read pointer for bundle */
1011 us_proc_lib_t *d_lib, *pd_lib;
1012 dex_proc_ip_t *dex_proc;
1013 ioctl_usr_space_lib_t s_lib;
1014 ioctl_usr_space_vtp_t *s_vtp;
1015 us_proc_vtp_t *mvtp;
1019 struct cond *c, *c_tmp, *p_cond;
1025 /* Get user-defined us handlers (if they are provided) */
1026 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
1028 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
1030 if (my_uprobes_info == 0)
1031 my_uprobes_info = &empty_uprobes_info;
1033 DPRINTF("Going to release us_proc_info");
1034 if (us_proc_info.path)
1037 /* Skip size - it has been used before */
1038 p += sizeof(u_int32_t);
1041 if (SetECMode(*(u_int32_t *)p) == -1)
1043 EPRINTF("Cannot set mode!\n");
1047 p += sizeof(u_int32_t);
1050 if (SetBufferSize(*(u_int32_t *)p) == -1)
1052 EPRINTF("Cannot set buffer size!\n");
1056 p += sizeof(u_int32_t);
1059 if (SetPid(*(u_int32_t *)p) == -1)
1061 EPRINTF("Cannot set pid!\n");
1065 p += sizeof(u_int32_t);
1068 nr_kern_probes = *(u_int32_t *)p;
1069 p += sizeof(u_int32_t);
1070 for (i = 0; i < nr_kern_probes; i++)
1072 if (add_probe(*(u_int32_t *)p))
1074 EPRINTF("Cannot add kernel probe at 0x%x!\n", *(u_int32_t *)p);
1077 p += sizeof(u_int32_t);
1081 len = *(u_int32_t *)p; /* App path len */
1082 p += sizeof(u_int32_t);
1084 us_proc_info.is_plt = 0;
1087 us_proc_info.path = NULL;
1094 us_proc_info.path = (char *)p;
1095 DPRINTF("app path = %s", us_proc_info.path);
1098 if (strcmp(us_proc_info.path, "*")) {
1099 us_proc_info.m_f_dentry = dentry_by_path(us_proc_info.path);
1100 if (us_proc_info.m_f_dentry == NULL) {
1106 us_proc_info.m_f_dentry = NULL;
1109 us_proc_info.libs_count = *(u_int32_t *)p;
1110 DPRINTF("nr of libs = %d", us_proc_info.libs_count);
1111 p += sizeof(u_int32_t);
1112 us_proc_info.p_libs =
1113 kmalloc(us_proc_info.libs_count * sizeof(us_proc_lib_t), GFP_KERNEL);
1115 if (!us_proc_info.p_libs)
1117 EPRINTF("Cannot alloc p_libs!");
1120 memset(us_proc_info.p_libs, 0,
1121 us_proc_info.libs_count * sizeof(us_proc_lib_t));
1123 for (i = 0; i < us_proc_info.libs_count; i++)
1125 int abs_handler_idx = 0;
1127 d_lib = &us_proc_info.p_libs[i];
1129 lib_name_len = *(u_int32_t *)p;
1130 p += sizeof(u_int32_t);
1131 d_lib->path = (char *)p;
1132 DPRINTF("d_lib->path = %s", d_lib->path);
1136 lib_name_len = *(u_int32_t *)p;
1137 p += sizeof(u_int32_t);
1138 d_lib->path_dyn = (char *)p;
1139 DPRINTF("d_lib->path_dyn = %s", d_lib->path_dyn);
1143 d_lib->ips_count = *(u_int32_t *)p;
1144 DPRINTF("d_lib->ips_count = %d", d_lib->ips_count);
1145 p += sizeof(u_int32_t);
1147 /* If there are any probes for "*" app we have to drop them */
1148 if (strcmp(d_lib->path, "*") == 0)
1150 p += d_lib->ips_count * 3 * sizeof(u_int32_t);
1151 d_lib->ips_count = 0;
1152 d_lib->plt_count = *(u_int32_t*)p;
1153 p += sizeof(u_int32_t);
1154 p += d_lib->plt_count * 2 * sizeof(u_int32_t);
1155 d_lib->plt_count = 0;
1159 if (strcmp(us_proc_info.path, d_lib->path) == 0)
1164 DPRINTF("Searching path for lib %s", d_lib->path);
1165 d_lib->path = find_lib_path(d_lib->path);
1168 if (strcmp(d_lib->path_dyn, "") == 0) {
1169 EPRINTF("Cannot find path for lib %s!", d_lib->path);
1170 /* Just skip all the IPs and go to next lib */
1171 p += d_lib->ips_count * 3 * sizeof(u_int32_t);
1172 d_lib->ips_count = 0;
1173 d_lib->plt_count = *(u_int32_t*)p;
1174 p += sizeof(u_int32_t);
1175 p += d_lib->plt_count * 2 * sizeof(u_int32_t);
1176 d_lib->plt_count = 0;
1180 d_lib->path = d_lib->path_dyn;
1181 DPRINTF("Assign path for lib as %s (in suggestion of dyn lib)", d_lib->path);
1186 d_lib->m_f_dentry = dentry_by_path(d_lib->path);
1187 if (d_lib->m_f_dentry == NULL) {
1188 EPRINTF ("failed to lookup dentry for path %s!", d_lib->path);
1189 /* Just skip all the IPs and go to next lib */
1190 p += d_lib->ips_count * 3 * sizeof(u_int32_t);
1191 d_lib->ips_count = 0;
1192 d_lib->plt_count = *(u_int32_t*)p;
1193 p += sizeof(u_int32_t);
1194 p += d_lib->plt_count * 2 * sizeof(u_int32_t);
1195 d_lib->plt_count = 0;
1200 ptr = strrchr(d_lib->path, '/');
1206 for (l = 0; l < my_uprobes_info->libs_count; l++)
1208 if ((strcmp(ptr, my_uprobes_info->p_libs[l].path) == 0) ||
1209 (is_app && *(my_uprobes_info->p_libs[l].path) == '\0'))
1211 pd_lib = &my_uprobes_info->p_libs[l];
1214 abs_handler_idx += my_uprobes_info->p_libs[l].ips_count;
1217 if (d_lib->ips_count > 0)
1219 us_proc_info.unres_ips_count += d_lib->ips_count;
1220 d_lib->p_ips = vmalloc(d_lib->ips_count * sizeof(us_proc_ip_t));
1221 DPRINTF("d_lib[%i]->p_ips=%p/%u [%s]", i, d_lib->p_ips,
1222 us_proc_info.unres_ips_count, d_lib->path);
1226 EPRINTF("Cannot alloc p_ips!\n");
1230 memset (d_lib->p_ips, 0, d_lib->ips_count * sizeof(us_proc_ip_t));
1231 for (k = 0; k < d_lib->ips_count; k++)
1233 d_ip = &d_lib->p_ips[k];
1234 d_ip->offset = *(u_int32_t *)p;
1235 p += sizeof(u_int32_t);
1236 p += sizeof(u_int32_t); /* Skip inst type */
1237 handler_index = *(u_int32_t *)p;
1238 p += sizeof(u_int32_t);
1242 DPRINTF("pd_lib->ips_count = 0x%x", pd_lib->ips_count);
1243 if (handler_index != -1)
1245 DPRINTF("found handler for 0x%x", d_ip->offset);
1246 d_ip->jprobe.pre_entry =
1247 pd_lib->p_ips[handler_index - abs_handler_idx].jprobe.pre_entry;
1248 d_ip->jprobe.entry =
1249 pd_lib->p_ips[handler_index - abs_handler_idx].jprobe.entry;
1250 d_ip->retprobe.handler =
1251 pd_lib->p_ips[handler_index - abs_handler_idx].retprobe.handler;
1257 d_lib->plt_count = *(u_int32_t*)p;
1258 p += sizeof(u_int32_t);
1259 if (d_lib->plt_count > 0)
1262 us_proc_info.is_plt = 1;
1263 d_lib->p_plt = kmalloc(d_lib->plt_count * sizeof(us_proc_plt_t), GFP_KERNEL);
1266 EPRINTF("Cannot alloc p_plt!");
1269 memset(d_lib->p_plt, 0, d_lib->plt_count * sizeof(us_proc_plt_t));
1270 for (j = 0; j < d_lib->plt_count; j++)
1272 d_lib->p_plt[j].func_addr = *(u_int32_t*)p;
1273 p += sizeof(u_int32_t);
1274 d_lib->p_plt[j].got_addr = *(u_int32_t*)p;
1275 p += sizeof(u_int32_t);
1276 d_lib->p_plt[j].real_func_addr = 0;
1282 lib_path_len = *(u_int32_t *)p;
1283 DPRINTF("lib_path_len = %d", lib_path_len);
1284 p += sizeof(u_int32_t);
1286 DPRINTF("lib_path = %s", lib_path);
1290 d_lib = &us_proc_info.p_libs[0];
1291 s_lib.vtps_count = *(u_int32_t *)p;
1292 DPRINTF("s_lib.vtps_count = %d", s_lib.vtps_count);
1293 p += sizeof(u_int32_t);
1294 if (s_lib.vtps_count > 0)
1296 unsigned long ucount = 1, pre_addr;
1297 unsigned long *addrs;
1299 s_lib.p_vtps = kmalloc(s_lib.vtps_count
1300 * sizeof(ioctl_usr_space_vtp_t), GFP_KERNEL);
1307 for (i = 0; i < s_lib.vtps_count; i++)
1309 int var_name_len = *(u_int32_t *)p;
1310 p += sizeof(u_int32_t);
1311 s_lib.p_vtps[i].name = p;
1313 s_lib.p_vtps[i].addr = *(u_int32_t *)p;
1314 p += sizeof(u_int32_t);
1315 s_lib.p_vtps[i].type = *(u_int32_t *)p;
1316 p += sizeof(u_int32_t);
1317 s_lib.p_vtps[i].size = *(u_int32_t *)p;
1318 p += sizeof(u_int32_t);
1319 s_lib.p_vtps[i].reg = *(u_int32_t *)p;
1320 p += sizeof(u_int32_t);
1321 s_lib.p_vtps[i].off = *(u_int32_t *)p;
1322 p += sizeof(u_int32_t);
1325 // array containing elements like (addr, index)
1326 addrs = kmalloc (s_lib.vtps_count * 2 * sizeof (unsigned long), GFP_KERNEL);
1327 // DPRINTF ("addrs=%p/%u", addrs, s_lib.vtps_count);
1330 //note: storage will released next time or at clean-up moment
1333 memset (addrs, 0, s_lib.vtps_count * 2 * sizeof (unsigned long));
1334 // fill the array in
1335 for (k = 0; k < s_lib.vtps_count; k++)
1337 s_vtp = &s_lib.p_vtps[k];
1338 addrs[2 * k] = s_vtp->addr;
1339 addrs[2 * k + 1] = k;
1341 // sort by VTP addresses, i.e. make VTPs with the same addresses adjacent;
1342 // organize them into bundles
1343 sort (addrs, s_lib.vtps_count, 2 * sizeof (unsigned long), addr_cmp, generic_swap);
1345 // calc number of VTPs with unique addresses
1346 for (k = 1, pre_addr = addrs[0]; k < s_lib.vtps_count; k++)
1348 if (addrs[2 * k] != pre_addr)
1349 ucount++; // count different only
1350 pre_addr = addrs[2 * k];
1352 us_proc_info.unres_vtps_count += ucount;
1353 d_lib->vtps_count = ucount;
1354 d_lib->p_vtps = kmalloc (ucount * sizeof (us_proc_vtp_t), GFP_KERNEL);
1355 DPRINTF ("d_lib[%i]->p_vtps=%p/%lu", i, d_lib->p_vtps, ucount); //, d_lib->path);
1358 //note: storage will released next time or at clean-up moment
1362 memset (d_lib->p_vtps, 0, d_lib->vtps_count * sizeof (us_proc_vtp_t));
1363 // go through sorted VTPS.
1364 for (k = 0, j = 0, pre_addr = 0, mvtp = NULL; k < s_lib.vtps_count; k++)
1366 us_proc_vtp_data_t *vtp_data;
1368 s_vtp = &s_lib.p_vtps[addrs[2 * k + 1]];
1369 // if this is the first VTP in bundle (master VTP)
1370 if (addrs[2 * k] != pre_addr)
1372 // data are in the array of master VTPs
1373 mvtp = &d_lib->p_vtps[j++];
1374 mvtp->addr = s_vtp->addr;
1375 INIT_LIST_HEAD (&mvtp->list);
1377 // data are in the list of slave VTPs
1378 vtp_data = kmalloc (sizeof (us_proc_vtp_data_t), GFP_KERNEL);
1381 //note: storage will released next time or at clean-up moment
1386 /*len = strlen_user (s_vtp->name);
1387 vtp_data->name = kmalloc (len, GFP_KERNEL);
1388 if (!vtp_data->name)
1390 //note: storage will released next time or at clean-up moment
1395 if (strncpy_from_user (vtp_data->name, s_vtp->name, len) != (len-1))
1397 //note: storage will released next time or at clean-up moment
1398 EPRINTF ("strncpy_from_user VTP name failed %p (%ld)", vtp_data->name, len);
1399 kfree (vtp_data->name);
1404 //vtp_data->name[len] = 0;*/
1405 vtp_data->name = s_vtp->name;
1406 vtp_data->type = s_vtp->type;
1407 vtp_data->size = s_vtp->size;
1408 vtp_data->reg = s_vtp->reg;
1409 vtp_data->off = s_vtp->off;
1410 list_add_tail_rcu (&vtp_data->list, &mvtp->list);
1411 pre_addr = addrs[2 * k];
1414 kfree(s_lib.p_vtps);
1418 // ================================================================================
1420 // ================================================================================
1421 len = *(u_int32_t *)p; /* App path len */
1422 p += sizeof(u_int32_t);
1426 dex_proc_info.path = NULL;
1430 dex_proc_info.path = p;
1431 DPRINTF("dex path = %s", dex_proc_info.path);
1434 dex_proc_info.ips_count = *(u_int32_t *)p;
1435 DPRINTF("nr of dex probes = %d", dex_proc_info.ips_count);
1436 p += sizeof(u_int32_t);
1438 dex_proc_info.p_ips =
1439 kmalloc(dex_proc_info.ips_count * sizeof(dex_proc_ip_t), GFP_KERNEL);
1441 if (!dex_proc_info.p_ips)
1443 EPRINTF("Cannot alloc dex probes!");
1447 memset(dex_proc_info.p_ips, 0,
1448 dex_proc_info.ips_count * sizeof(dex_proc_ip_t));
1450 for (i = 0; i < dex_proc_info.ips_count; i++)
1452 dex_proc = &dex_proc_info.p_ips[i];
1456 dex_proc->addr = *(u_int32_t *)p;
1457 p += sizeof(u_int32_t);
1459 dex_proc->inst_type = *(u_int32_t *)p;
1460 p += sizeof(u_int32_t);
1463 lib_name_len = *(u_int32_t *)p;
1464 p += sizeof(u_int32_t);
1465 dex_proc->name = (char *)p;
1469 lib_name_len = *(u_int32_t *)p;
1470 p += sizeof(u_int32_t);
1471 dex_proc->class_name = (char *)p;
1475 lib_name_len = *(u_int32_t *)p;
1476 p += sizeof(u_int32_t);
1477 dex_proc->method_name = (char *)p;
1481 lib_name_len = *(u_int32_t *)p;
1482 p += sizeof(u_int32_t);
1483 dex_proc->prototype = (char *)p;
1488 // ================================================================================
1489 // END OF DEX Probes
1490 // ================================================================================
1494 /* first, delete all the conds */
1495 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
1499 /* second, add new conds */
1500 /* This can be improved (by placing conds into array) */
1501 nr_conds = *(u_int32_t *)p;
1502 DPRINTF("nr_conds = %d", nr_conds);
1503 p += sizeof(u_int32_t);
1504 for (i = 0; i < nr_conds; i++) {
1505 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
1507 EPRINTF("Cannot alloc cond!\n");
1511 memcpy(&p_cond->tmpl, p, sizeof(struct event_tmpl));
1512 p_cond->applied = 0;
1513 list_add(&(p_cond->list), &(cond_list.list));
1514 p += sizeof(struct event_tmpl);
1518 if (set_event_mask(*(u_int32_t *)p)) {
1519 EPRINTF("Cannot set event mask!");
1523 p += sizeof(u_int32_t);
1526 // print_inst_us_proc(&us_proc_info);
1528 us_proc_info.pp = get_file_probes(&us_proc_info);
1533 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
1534 int storage_init (void)
1536 unsigned long spinlock_flags = 0L;
1538 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1539 ec_info.m_nMode = 0; // MASK IS CLEAR (SINGLE NON_CONTINUOUS BUFFER)
1540 // ec_info.m_nMode |= ECMODEMASK_MULTIPLE_BUFFER;
1541 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
1543 #ifndef __DISABLE_RELAYFS
1546 gl_pdirRelay = _dir_create (DEFAULT_RELAY_BASE_DIR, _get_proc_root(), &alt_pde);
1547 if(gl_pdirRelay == NULL) {
1548 EPRINTF("Cannot create procfs directory for relay buffer!");
1552 gl_pdirRelay = debugfs_create_dir(DEFAULT_RELAY_BASE_DIR, NULL);
1553 if(gl_pdirRelay == NULL) {
1554 EPRINTF("Cannot create directory for relay buffer!");
1558 #endif // __USE_PROCFS
1560 #endif //__DISABLE_RELAYFS
1562 if(InitializeBuffer(EC_BUFFER_SIZE_DEFAULT) == -1) {
1563 EPRINTF("Cannot initialize buffer! [Size=%u KB]", EC_BUFFER_SIZE_DEFAULT / 1024 );
1567 INIT_HLIST_HEAD(&kernel_probes);
1568 INIT_HLIST_HEAD(&otg_kernel_probes);
1569 INIT_LIST_HEAD(&otg_us_proc_info);
1570 spin_lock_init(&dbi_mh.lock);
1571 INIT_LIST_HEAD(&dbi_mh.modules_handlers);
1576 Shuts down "storage".
1577 Assumes that all probes are already deactivated.
1579 void storage_down (void)
1581 if(UninitializeBuffer() == -1)
1582 EPRINTF("Cannot uninitialize buffer!");
1584 #ifndef __DISABLE_RELAYFS
1587 // remove_buf(gl_pdirRelay);
1589 debugfs_remove(gl_pdirRelay);
1590 #endif // __USE_PROCFS
1592 #endif //__DISABLE_RELAYFS
1594 if (ec_info.collision_count)
1595 EPRINTF ("ec_info.collision_count=%d", ec_info.collision_count);
1596 if (ec_info.lost_events_count)
1597 EPRINTF ("ec_info.lost_events_count=%d", ec_info.lost_events_count);
1600 u_int32_t get_probe_func_addr(const char *fmt, va_list args)
1605 return va_arg(args, u_int32_t);
1608 void pack_event_info (probe_id_t probe_id, record_type_t record_type, const char *fmt, ...)
1610 unsigned long spinlock_flags = 0L;
1611 static char buf[EVENT_MAX_SIZE] = "";
1612 TYPEOF_EVENT_LENGTH event_len = 0L;
1613 struct timeval tv = { 0, 0 };
1614 TYPEOF_THREAD_ID current_pid = current->pid;
1615 TYPEOF_PROCESS_ID current_tgid = current->tgid;
1616 unsigned current_cpu = task_cpu(current);
1618 unsigned long addr = 0;
1619 struct cond *p_cond;
1620 struct event_tmpl *p_tmpl;
1622 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1623 memset(buf, 0, EVENT_MAX_SIZE);
1624 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1626 do_gettimeofday (&tv);
1628 if (probe_id == KS_PROBE_ID) {
1629 va_start(args, fmt);
1630 addr = get_probe_func_addr(fmt, args);
1632 if( ((addr == pf_addr) && !(probes_flags & PROBE_FLAG_PF_INSTLD)) ||
1633 ((addr == cp_addr) && !(probes_flags & PROBE_FLAG_CP_INSTLD)) ||
1634 ((addr == mr_addr) && !(probes_flags & PROBE_FLAG_MR_INSTLD)) ||
1635 ((addr == unmap_addr) && !(probes_flags & PROBE_FLAG_UNMAP_INSTLD)) ||
1636 ((addr == exit_addr) && !(probes_flags & PROBE_FLAG_EXIT_INSTLD)) ) {
1640 if (probe_id == US_PROBE_ID) {
1641 va_start(args, fmt);
1642 addr = get_probe_func_addr(fmt, args);
1646 /* Checking for all the conditions
1647 * except stop condition that we process after saving the event */
1648 list_for_each_entry(p_cond, &cond_list.list, list) {
1649 p_tmpl = &p_cond->tmpl;
1650 switch (p_tmpl->type) {
1651 case ET_TYPE_START_COND:
1652 if ((!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_ADDR) ||
1653 (addr == p_tmpl->addr)) &&
1654 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_PID) ||
1655 (current_tgid == p_tmpl->pid)) &&
1656 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TID) ||
1657 (current_pid == p_tmpl->tid)) &&
1658 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_CPU_NUM) ||
1659 (current_cpu == p_tmpl->cpu_num)) &&
1660 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_BIN_NAME) ||
1661 (strcmp(current->comm, p_tmpl->bin_name) == 0)) &&
1662 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TIME) ||
1663 (tv.tv_sec > last_attach_time.tv_sec + p_tmpl->sec) ||
1664 (tv.tv_sec == last_attach_time.tv_sec + p_tmpl->sec &&
1665 tv.tv_usec >= last_attach_time.tv_usec + p_tmpl->usec)) &&
1667 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1669 p_cond->applied = 1;
1670 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1673 case ET_TYPE_IGNORE_COND:
1674 /* if (probe_id == PROBE_SCHEDULE) */
1676 if ((!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_ADDR) ||
1677 (addr == p_tmpl->addr)) &&
1678 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_PID) ||
1679 (current_tgid == p_tmpl->pid)) &&
1680 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TID) ||
1681 (current_pid == p_tmpl->tid)) &&
1682 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_CPU_NUM) ||
1683 (current_cpu == p_tmpl->cpu_num)) &&
1684 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_BIN_NAME) ||
1685 (strcmp(current->comm, p_tmpl->bin_name) == 0))) {
1686 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1687 ec_info.ignored_events_count++;
1688 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1695 /* Save only not masked entry or return kernel and user space events */
1696 if (likely(!((probe_id == KS_PROBE_ID || probe_id == US_PROBE_ID)
1697 && ((record_type == RECORD_ENTRY && (event_mask & IOCTL_EMASK_ENTRY))
1698 || (record_type == RECORD_RET && (event_mask & IOCTL_EMASK_EXIT)))))) {
1700 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1702 if (paused && (!(probe_id == EVENT_FMT_PROBE_ID || probe_id == DYN_LIB_PROBE_ID))) {
1703 ec_info.ignored_events_count++;
1704 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1708 va_start (args, fmt);
1709 event_len = VPackEvent(buf, sizeof(buf), event_mask, probe_id, record_type, (TYPEOF_TIME *)&tv,
1710 current_tgid, current_pid, current_cpu, fmt, args);
1713 if(event_len == 0) {
1714 EPRINTF ("ERROR: failed to pack event!");
1715 ++ec_info.lost_events_count;
1717 } else if(WriteEventIntoBuffer(buf, event_len) == -1) {
1718 EPRINTF("Cannot write event into buffer!");
1719 ++ec_info.lost_events_count;
1721 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1725 /* Check for stop condition. We pause collecting the trace right after
1726 * storing this event */
1727 list_for_each_entry(p_cond, &cond_list.list, list) {
1728 p_tmpl = &p_cond->tmpl;
1729 switch (p_tmpl->type) {
1730 case ET_TYPE_STOP_COND:
1731 if ((!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_ADDR) ||
1732 (addr == p_tmpl->addr)) &&
1733 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_PID) ||
1734 (current_tgid == p_tmpl->pid)) &&
1735 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TID) ||
1736 (current_pid == p_tmpl->tid)) &&
1737 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_CPU_NUM) ||
1738 (current_cpu == p_tmpl->cpu_num)) &&
1739 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_BIN_NAME) ||
1740 (strcmp(current->comm, p_tmpl->bin_name) == 0)) &&
1741 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TIME) ||
1742 (tv.tv_sec > last_attach_time.tv_sec + p_tmpl->sec) ||
1743 (tv.tv_sec == last_attach_time.tv_sec + p_tmpl->sec &&
1744 tv.tv_usec >= last_attach_time.tv_usec + p_tmpl->usec)) &&
1746 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1748 p_cond->applied = 1;
1749 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1755 EXPORT_SYMBOL_GPL(pack_event_info);
1757 kernel_probe_t* find_probe (unsigned long addr)
1760 struct hlist_node *node;
1762 //check if such probe does exist
1763 hlist_for_each_entry_rcu (p, node, &kernel_probes, hlist)
1764 if (p->addr == addr)
1767 return node ? p : NULL;
1771 int add_probe_to_list (unsigned long addr, kernel_probe_t ** pprobe)
1773 kernel_probe_t *new_probe;
1774 kernel_probe_t *probe;
1778 //check if such probe does already exist
1779 probe = find_probe(addr);
1781 /* It is not a problem if we have already registered
1782 this probe before */
1785 new_probe = kmalloc (sizeof (kernel_probe_t), GFP_KERNEL);
1788 EPRINTF ("no memory for new probe!");
1791 memset (new_probe, 0, sizeof (kernel_probe_t));
1792 new_probe->addr = addr;
1793 new_probe->jprobe.kp.addr = new_probe->retprobe.kp.addr = (kprobe_opcode_t *)addr;
1794 new_probe->jprobe.priv_arg = new_probe->retprobe.priv_arg = new_probe;
1795 //new_probe->jprobe.pre_entry = (kprobe_pre_entry_handler_t) def_jprobe_event_pre_handler;
1796 dbi_find_and_set_handler_for_probe(new_probe);
1797 INIT_HLIST_NODE (&new_probe->hlist);
1798 hlist_add_head_rcu (&new_probe->hlist, &kernel_probes);
1800 *pprobe = new_probe;
1804 int remove_probe_from_list (unsigned long addr)
1808 //check if such probe does exist
1809 p = find_probe (addr);
1811 /* We do not care about it. Nothing bad. */
1815 hlist_del_rcu (&p->hlist);
1823 int put_us_event (char *data, unsigned long len)
1825 unsigned long spinlock_flags = 0L;
1827 SWAP_TYPE_EVENT_HEADER *pEventHeader = (SWAP_TYPE_EVENT_HEADER *)data;
1828 char *cur = data + sizeof(TYPEOF_EVENT_LENGTH) + sizeof(TYPEOF_EVENT_TYPE)
1829 + sizeof(TYPEOF_PROBE_ID);
1830 TYPEOF_NUMBER_OF_ARGS nArgs = pEventHeader->m_nNumberOfArgs;
1831 TYPEOF_PROBE_ID probe_id = pEventHeader->m_nProbeID;
1834 /*if(probe_id == US_PROBE_ID){
1835 printk("esrc %p/%d[", data, len);
1836 for(i = 0; i < len; i++)
1837 printk("%02x ", data[i]);
1841 // set pid/tid/cpu/time i
1842 //pEventHeader->m_time.tv_sec = tv.tv_sec;
1843 //pEventHeader->m_time.tv_usec = tv.tv_usec;
1845 #ifdef MEMORY_CHECKER
1846 //TODO: move this part to special MEC event posting routine, new IOCTL is needed
1847 if((probe_id >= MEC_PROBE_ID_MIN) && (probe_id <= MEC_PROBE_ID_MAX))
1849 if(mec_post_event != NULL)
1851 int res = mec_post_event(data, len);
1859 mec_post_event = lookup_name("mec_post_event");
1860 if(mec_post_event == NULL)
1862 EPRINTF ("Failed to find function 'mec_post_event' from mec_handlers.ko. Memory Error Checker will work incorrectly.");
1866 int res = mec_post_event(data, len);
1876 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_TIME)){
1877 struct timeval tv = { 0, 0 };
1878 do_gettimeofday (&tv);
1879 memcpy(cur, &tv, sizeof(TYPEOF_TIME));
1880 cur += sizeof(TYPEOF_TIME);
1882 //pEventHeader->m_nProcessID = current_tgid;
1883 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_PID)){
1884 //TYPEOF_PROCESS_ID current_tgid = current->tgid;
1885 (*(TYPEOF_PROCESS_ID *)cur) = current->tgid;
1886 cur += sizeof(TYPEOF_PROCESS_ID);
1888 //pEventHeader->m_nThreadID = current_pid;
1889 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_TID)){
1890 //TYPEOF_THREAD_ID current_pid = current->pid;
1891 (*(TYPEOF_THREAD_ID *)cur) = current->pid;
1892 cur += sizeof(TYPEOF_THREAD_ID);
1894 //pEventHeader->m_nCPU = current_cpu;
1895 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_CPU)){
1896 //TYPEOF_CPU_NUMBER current_cpu = task_cpu(current);
1897 (*(TYPEOF_CPU_NUMBER *)cur) = task_cpu(current);
1898 cur += sizeof(TYPEOF_CPU_NUMBER);
1900 //printk("%d %x", probe_id, event_mask);
1901 // dyn lib event should have all args, it is for internal use and not visible to user
1902 if((probe_id == EVENT_FMT_PROBE_ID) || (probe_id == DYN_LIB_PROBE_ID) || !(event_mask & IOCTL_EMASK_ARGS)){
1903 // move only if any of prev fields has been skipped
1904 if(event_mask & (IOCTL_EMASK_TIME|IOCTL_EMASK_PID|IOCTL_EMASK_TID|IOCTL_EMASK_CPU)){
1905 memmove(cur, data+sizeof(SWAP_TYPE_EVENT_HEADER)-sizeof(TYPEOF_NUMBER_OF_ARGS),
1906 len-sizeof(SWAP_TYPE_EVENT_HEADER)+sizeof(TYPEOF_NUMBER_OF_ARGS)
1907 -sizeof(TYPEOF_EVENT_LENGTH));
1909 cur += len-sizeof(SWAP_TYPE_EVENT_HEADER)+sizeof(TYPEOF_NUMBER_OF_ARGS)
1910 -sizeof(TYPEOF_EVENT_LENGTH);
1913 // user space probes should have at least one argument to identify them
1914 if((probe_id == US_PROBE_ID) || (probe_id == VTP_PROBE_ID)){
1916 (*(TYPEOF_NUMBER_OF_ARGS *)cur) = 1;
1917 cur += sizeof(TYPEOF_NUMBER_OF_ARGS);
1918 // pack args using format string for the 1st arg only
1919 memset(cur, 0, ALIGN_VALUE(2));
1920 cur[0] = 'p'; cur[1] = '\0';
1921 cur += ALIGN_VALUE(2);
1922 pArg1 = data + sizeof(SWAP_TYPE_EVENT_HEADER)+ALIGN_VALUE(nArgs+1);
1923 memmove(cur, pArg1, sizeof(unsigned long));
1924 cur += sizeof(unsigned long);
1927 (*(TYPEOF_NUMBER_OF_ARGS *)cur) = 0;
1928 cur += sizeof(TYPEOF_NUMBER_OF_ARGS);
1931 pEventHeader->m_nLength = cur - data + sizeof(TYPEOF_EVENT_LENGTH);
1932 *((TYPEOF_EVENT_LENGTH *)cur) = pEventHeader->m_nLength;
1933 len = pEventHeader->m_nLength;
1935 if(WriteEventIntoBuffer(data, len) == -1) {
1936 EPRINTF("Cannot write event into buffer!");
1938 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1939 ++ec_info.lost_events_count;
1940 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
1946 int set_predef_uprobes (ioctl_predef_uprobes_info_t *data)
1948 int i, k, size = 0, probe_size, result, j;
1949 char *buf, *sep1, *sep2;
1950 get_my_uprobes_info_t get_uprobes = NULL;
1951 inst_us_proc_t *my_uprobes_info = NULL;
1953 inst_us_proc_t empty_uprobes_info =
1959 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
1961 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
1963 DPRINTF("my_uprobes_info lookup result: 0x%p", my_uprobes_info);
1964 if (my_uprobes_info == 0)
1965 my_uprobes_info = &empty_uprobes_info;
1967 for(j = 0; j < data->probes_count; j++)
1969 probe_size = strlen_user(data->p_probes+size);
1970 buf = kmalloc(probe_size, GFP_KERNEL);
1974 EPRINTF("failed to alloc mem!");
1978 result = strncpy_from_user(buf, data->p_probes+size, probe_size);
1979 if (result != (probe_size-1))
1981 EPRINTF("failed to copy from user!");
1985 //DPRINTF("%s", buf);
1986 sep1 = strchr(buf, ':');
1989 EPRINTF("skipping invalid predefined uprobe string '%s'!", buf);
1994 sep2 = strchr(sep1+1, ':');
1995 if(!sep2 || (sep2 == sep1) || (sep2+2 == buf+probe_size))
1997 EPRINTF("skipping invalid predefined uprobe string '%s'!", buf);
2002 for(i = 0; i < my_uprobes_info->libs_count; i++)
2004 if(strncmp(buf, my_uprobes_info->p_libs[i].path, sep1-buf) != 0)
2006 for(k = 0; k < my_uprobes_info->p_libs[i].ips_count; k++)
2008 if(strncmp(sep1+1, my_uprobes_info->p_libs[i].p_ips[k].name, sep2-sep1-1) != 0)
2010 my_uprobes_info->p_libs[i].p_ips[k].offset = simple_strtoul(sep2+1, NULL, 16);
2020 int get_predef_uprobes_size(int *size)
2023 get_my_uprobes_info_t get_uprobes = NULL;
2024 inst_us_proc_t *my_uprobes_info = NULL;
2026 inst_us_proc_t empty_uprobes_info =
2032 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
2034 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
2036 if (my_uprobes_info == 0)
2037 my_uprobes_info = &empty_uprobes_info;
2040 for(i = 0; i < my_uprobes_info->libs_count; i++)
2042 int lib_size = strlen(my_uprobes_info->p_libs[i].path);
2043 for(k = 0; k < my_uprobes_info->p_libs[i].ips_count; k++)
2045 // libc.so.6:printf:
2046 *size += lib_size + 1 + strlen(my_uprobes_info->p_libs[i].p_ips[k].name) + 2;
2053 int get_predef_uprobes(ioctl_predef_uprobes_info_t *udata)
2055 ioctl_predef_uprobes_info_t data;
2056 int i, k, size, lib_size, func_size, result;
2060 inst_us_proc_t empty_uprobes_info =
2066 get_my_uprobes_info_t get_uprobes = NULL;
2067 inst_us_proc_t *my_uprobes_info = NULL;
2069 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
2071 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
2073 if (my_uprobes_info == 0)
2074 my_uprobes_info = &empty_uprobes_info;
2076 // get addr of array
2077 if (copy_from_user ((void *)&data, udata, sizeof (data)))
2079 EPRINTF("failed to copy from user!");
2084 for(i = 0; i < my_uprobes_info->libs_count; i++)
2086 lib_size = strlen(my_uprobes_info->p_libs[i].path);
2087 for(k = 0; k < my_uprobes_info->p_libs[i].ips_count; k++)
2090 result = copy_to_user ((void *)(data.p_probes+size), my_uprobes_info->p_libs[i].path, lib_size);
2093 EPRINTF("failed to copy to user!");
2098 result = copy_to_user ((void *)(data.p_probes+size), sep, 1);
2101 EPRINTF("failed to copy to user!");
2106 //DPRINTF("'%s'", my_uprobes_info->p_libs[i].p_ips[k].name);
2107 func_size = strlen(my_uprobes_info->p_libs[i].p_ips[k].name);
2108 result = copy_to_user ((void *)(data.p_probes+size), my_uprobes_info->p_libs[i].p_ips[k].name, func_size);
2111 EPRINTF("failed to copy to user!");
2116 result = copy_to_user ((void *)(data.p_probes+size), sep, 2);
2119 EPRINTF("failed to copy to user!");
2128 result = copy_to_user ((void *)&(udata->probes_count), &count, sizeof(count));
2131 EPRINTF("failed to copy to user!");