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);
973 kfree ((void *) us_proc_info.p_libs);
974 us_proc_info.p_libs = 0;
978 /* kfree ((void *) path); */
979 /* //putname(path); */
982 us_proc_info.tgid = 0;
984 /* OTG probes list cleaning */
985 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
986 list_del_rcu(&p->list);
992 get_my_uprobes_info_t get_uprobes = NULL;
993 inst_us_proc_t *my_uprobes_info = 0;
994 inst_us_proc_t empty_uprobes_info =
999 char *p = bundle; /* read pointer for bundle */
1003 us_proc_lib_t *d_lib, *pd_lib;
1004 dex_proc_ip_t *dex_proc;
1005 ioctl_usr_space_lib_t s_lib;
1006 ioctl_usr_space_vtp_t *s_vtp;
1007 us_proc_vtp_t *mvtp;
1008 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
1010 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1011 struct nameidata nd;
1012 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1016 struct cond *c, *c_tmp, *p_cond;
1021 /* Get user-defined us handlers (if they are provided) */
1022 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
1024 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
1026 if (my_uprobes_info == 0)
1027 my_uprobes_info = &empty_uprobes_info;
1029 DPRINTF("Going to release us_proc_info");
1030 if (us_proc_info.path)
1033 /* Skip size - it has been used before */
1034 p += sizeof(u_int32_t);
1037 if (SetECMode(*(u_int32_t *)p) == -1)
1039 EPRINTF("Cannot set mode!\n");
1043 p += sizeof(u_int32_t);
1046 if (SetBufferSize(*(u_int32_t *)p) == -1)
1048 EPRINTF("Cannot set buffer size!\n");
1052 p += sizeof(u_int32_t);
1055 if (SetPid(*(u_int32_t *)p) == -1)
1057 EPRINTF("Cannot set pid!\n");
1061 p += sizeof(u_int32_t);
1064 nr_kern_probes = *(u_int32_t *)p;
1065 p += sizeof(u_int32_t);
1066 for (i = 0; i < nr_kern_probes; i++)
1068 if (add_probe(*(u_int32_t *)p))
1070 EPRINTF("Cannot add kernel probe at 0x%x!\n", *(u_int32_t *)p);
1073 p += sizeof(u_int32_t);
1077 len = *(u_int32_t *)p; /* App path len */
1078 p += sizeof(u_int32_t);
1082 us_proc_info.path = NULL;
1089 us_proc_info.path = (char *)p;
1090 DPRINTF("app path = %s", us_proc_info.path);
1093 if (strcmp(us_proc_info.path, "*")) {
1094 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
1095 if (kern_path(us_proc_info.path, LOOKUP_FOLLOW, &path) != 0) {
1096 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1097 if (path_lookup(us_proc_info.path, LOOKUP_FOLLOW, &nd) != 0) {
1098 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1099 EPRINTF("failed to lookup dentry for path %s!", us_proc_info.path);
1102 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
1103 us_proc_info.m_f_dentry = nd.dentry;
1106 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
1107 us_proc_info.m_f_dentry = path.dentry;
1109 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1110 us_proc_info.m_f_dentry = nd.path.dentry;
1112 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1117 us_proc_info.m_f_dentry = NULL;
1120 us_proc_info.libs_count = *(u_int32_t *)p;
1121 DPRINTF("nr of libs = %d", us_proc_info.libs_count);
1122 p += sizeof(u_int32_t);
1123 us_proc_info.p_libs =
1124 kmalloc(us_proc_info.libs_count * sizeof(us_proc_lib_t), GFP_KERNEL);
1126 if (!us_proc_info.p_libs)
1128 EPRINTF("Cannot alloc p_libs!");
1131 memset(us_proc_info.p_libs, 0,
1132 us_proc_info.libs_count * sizeof(us_proc_lib_t));
1134 for (i = 0; i < us_proc_info.libs_count; i++)
1136 int abs_handler_idx = 0;
1138 d_lib = &us_proc_info.p_libs[i];
1140 lib_name_len = *(u_int32_t *)p;
1141 p += sizeof(u_int32_t);
1142 d_lib->path = (char *)p;
1143 DPRINTF("d_lib->path = %s", d_lib->path);
1147 lib_name_len = *(u_int32_t *)p;
1148 p += sizeof(u_int32_t);
1149 d_lib->path_dyn = (char *)p;
1150 DPRINTF("d_lib->path_dyn = %s", d_lib->path_dyn);
1154 d_lib->ips_count = *(u_int32_t *)p;
1155 DPRINTF("d_lib->ips_count = %d", d_lib->ips_count);
1156 p += sizeof(u_int32_t);
1158 /* If there are any probes for "*" app we have to drop them */
1159 if (strcmp(d_lib->path, "*") == 0)
1161 p += d_lib->ips_count * 3 * sizeof(u_int32_t);
1162 d_lib->ips_count = 0;
1166 if (strcmp(us_proc_info.path, d_lib->path) == 0)
1171 DPRINTF("Searching path for lib %s", d_lib->path);
1172 d_lib->path = find_lib_path(d_lib->path);
1175 if (strcmp(d_lib->path_dyn, "") == 0) {
1176 EPRINTF("Cannot find path for lib %s!", d_lib->path);
1177 /* Just skip all the IPs and go to next lib */
1178 p += d_lib->ips_count * 3 * sizeof(u_int32_t);
1179 d_lib->ips_count = 0;
1183 d_lib->path = d_lib->path_dyn;
1184 DPRINTF("Assign path for lib as %s (in suggestion of dyn lib", d_lib->path);
1189 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
1190 if (kern_path(d_lib->path, LOOKUP_FOLLOW, &path) != 0) {
1191 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1192 if (path_lookup(d_lib->path, LOOKUP_FOLLOW, &nd) != 0) {
1193 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1194 EPRINTF ("failed to lookup dentry for path %s!", d_lib->path);
1195 /* Just skip all the IPs and go to next lib */
1196 p += d_lib->ips_count * 3 * sizeof(u_int32_t);
1197 d_lib->ips_count = 0;
1200 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
1201 d_lib->m_f_dentry = nd.dentry;
1204 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
1205 d_lib->m_f_dentry = path.dentry;
1206 d_lib->m_vfs_mount = path.mnt;
1208 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1209 d_lib->m_f_dentry = nd.path.dentry;
1210 d_lib->m_vfs_mount = nd.path.mnt;
1212 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1216 ptr = strrchr(d_lib->path, '/');
1222 for (l = 0; l < my_uprobes_info->libs_count; l++)
1224 if ((strcmp(ptr, my_uprobes_info->p_libs[l].path) == 0) ||
1225 (is_app && *(my_uprobes_info->p_libs[l].path) == '\0'))
1227 pd_lib = &my_uprobes_info->p_libs[l];
1230 abs_handler_idx += my_uprobes_info->p_libs[l].ips_count;
1232 if (d_lib->ips_count > 0)
1234 us_proc_info.unres_ips_count += d_lib->ips_count;
1235 d_lib->p_ips = vmalloc(d_lib->ips_count * sizeof(us_proc_ip_t));
1236 DPRINTF("d_lib[%i]->p_ips=%p/%u [%s]", i, d_lib->p_ips,
1237 us_proc_info.unres_ips_count, d_lib->path);
1241 EPRINTF("Cannot alloc p_ips!\n");
1245 memset (d_lib->p_ips, 0, d_lib->ips_count * sizeof(us_proc_ip_t));
1246 for (k = 0; k < d_lib->ips_count; k++)
1248 d_ip = &d_lib->p_ips[k];
1249 d_ip->offset = *(u_int32_t *)p;
1250 p += sizeof(u_int32_t);
1251 p += sizeof(u_int32_t); /* Skip inst type */
1252 handler_index = *(u_int32_t *)p;
1253 p += sizeof(u_int32_t);
1257 DPRINTF("pd_lib->ips_count = 0x%x", pd_lib->ips_count);
1258 if (handler_index != -1)
1260 DPRINTF("found handler for 0x%x", d_ip->offset);
1261 d_ip->jprobe.pre_entry =
1262 pd_lib->p_ips[handler_index - abs_handler_idx].jprobe.pre_entry;
1263 d_ip->jprobe.entry =
1264 pd_lib->p_ips[handler_index - abs_handler_idx].jprobe.entry;
1265 d_ip->retprobe.handler =
1266 pd_lib->p_ips[handler_index - abs_handler_idx].retprobe.handler;
1274 lib_path_len = *(u_int32_t *)p;
1275 DPRINTF("lib_path_len = %d", lib_path_len);
1276 p += sizeof(u_int32_t);
1278 DPRINTF("lib_path = %s", lib_path);
1282 d_lib = &us_proc_info.p_libs[0];
1283 s_lib.vtps_count = *(u_int32_t *)p;
1284 DPRINTF("s_lib.vtps_count = %d", s_lib.vtps_count);
1285 p += sizeof(u_int32_t);
1286 if (s_lib.vtps_count > 0)
1288 unsigned long ucount = 1, pre_addr;
1289 unsigned long *addrs;
1291 s_lib.p_vtps = kmalloc(s_lib.vtps_count
1292 * sizeof(ioctl_usr_space_vtp_t), GFP_KERNEL);
1299 for (i = 0; i < s_lib.vtps_count; i++)
1301 int var_name_len = *(u_int32_t *)p;
1302 p += sizeof(u_int32_t);
1303 s_lib.p_vtps[i].name = p;
1305 s_lib.p_vtps[i].addr = *(u_int32_t *)p;
1306 p += sizeof(u_int32_t);
1307 s_lib.p_vtps[i].type = *(u_int32_t *)p;
1308 p += sizeof(u_int32_t);
1309 s_lib.p_vtps[i].size = *(u_int32_t *)p;
1310 p += sizeof(u_int32_t);
1311 s_lib.p_vtps[i].reg = *(u_int32_t *)p;
1312 p += sizeof(u_int32_t);
1313 s_lib.p_vtps[i].off = *(u_int32_t *)p;
1314 p += sizeof(u_int32_t);
1317 // array containing elements like (addr, index)
1318 addrs = kmalloc (s_lib.vtps_count * 2 * sizeof (unsigned long), GFP_KERNEL);
1319 // DPRINTF ("addrs=%p/%u", addrs, s_lib.vtps_count);
1322 //note: storage will released next time or at clean-up moment
1325 memset (addrs, 0, s_lib.vtps_count * 2 * sizeof (unsigned long));
1326 // fill the array in
1327 for (k = 0; k < s_lib.vtps_count; k++)
1329 s_vtp = &s_lib.p_vtps[k];
1330 addrs[2 * k] = s_vtp->addr;
1331 addrs[2 * k + 1] = k;
1333 // sort by VTP addresses, i.e. make VTPs with the same addresses adjacent;
1334 // organize them into bundles
1335 sort (addrs, s_lib.vtps_count, 2 * sizeof (unsigned long), addr_cmp, generic_swap);
1337 // calc number of VTPs with unique addresses
1338 for (k = 1, pre_addr = addrs[0]; k < s_lib.vtps_count; k++)
1340 if (addrs[2 * k] != pre_addr)
1341 ucount++; // count different only
1342 pre_addr = addrs[2 * k];
1344 us_proc_info.unres_vtps_count += ucount;
1345 d_lib->vtps_count = ucount;
1346 d_lib->p_vtps = kmalloc (ucount * sizeof (us_proc_vtp_t), GFP_KERNEL);
1347 DPRINTF ("d_lib[%i]->p_vtps=%p/%lu", i, d_lib->p_vtps, ucount); //, d_lib->path);
1350 //note: storage will released next time or at clean-up moment
1354 memset (d_lib->p_vtps, 0, d_lib->vtps_count * sizeof (us_proc_vtp_t));
1355 // go through sorted VTPS.
1356 for (k = 0, j = 0, pre_addr = 0, mvtp = NULL; k < s_lib.vtps_count; k++)
1358 us_proc_vtp_data_t *vtp_data;
1360 s_vtp = &s_lib.p_vtps[addrs[2 * k + 1]];
1361 // if this is the first VTP in bundle (master VTP)
1362 if (addrs[2 * k] != pre_addr)
1364 // data are in the array of master VTPs
1365 mvtp = &d_lib->p_vtps[j++];
1366 mvtp->addr = s_vtp->addr;
1367 INIT_LIST_HEAD (&mvtp->list);
1369 // data are in the list of slave VTPs
1370 vtp_data = kmalloc (sizeof (us_proc_vtp_data_t), GFP_KERNEL);
1373 //note: storage will released next time or at clean-up moment
1378 /*len = strlen_user (s_vtp->name);
1379 vtp_data->name = kmalloc (len, GFP_KERNEL);
1380 if (!vtp_data->name)
1382 //note: storage will released next time or at clean-up moment
1387 if (strncpy_from_user (vtp_data->name, s_vtp->name, len) != (len-1))
1389 //note: storage will released next time or at clean-up moment
1390 EPRINTF ("strncpy_from_user VTP name failed %p (%ld)", vtp_data->name, len);
1391 kfree (vtp_data->name);
1396 //vtp_data->name[len] = 0;*/
1397 vtp_data->name = s_vtp->name;
1398 vtp_data->type = s_vtp->type;
1399 vtp_data->size = s_vtp->size;
1400 vtp_data->reg = s_vtp->reg;
1401 vtp_data->off = s_vtp->off;
1402 list_add_tail_rcu (&vtp_data->list, &mvtp->list);
1403 pre_addr = addrs[2 * k];
1406 kfree(s_lib.p_vtps);
1410 // ================================================================================
1412 // ================================================================================
1413 len = *(u_int32_t *)p; /* App path len */
1414 p += sizeof(u_int32_t);
1418 dex_proc_info.path = NULL;
1422 dex_proc_info.path = p;
1423 DPRINTF("dex path = %s", dex_proc_info.path);
1426 dex_proc_info.ips_count = *(u_int32_t *)p;
1427 DPRINTF("nr of dex probes = %d", dex_proc_info.ips_count);
1428 p += sizeof(u_int32_t);
1430 dex_proc_info.p_ips =
1431 kmalloc(dex_proc_info.ips_count * sizeof(dex_proc_ip_t), GFP_KERNEL);
1433 if (!dex_proc_info.p_ips)
1435 EPRINTF("Cannot alloc dex probes!");
1439 memset(dex_proc_info.p_ips, 0,
1440 dex_proc_info.ips_count * sizeof(dex_proc_ip_t));
1442 for (i = 0; i < dex_proc_info.ips_count; i++)
1444 dex_proc = &dex_proc_info.p_ips[i];
1448 dex_proc->addr = *(u_int32_t *)p;
1449 p += sizeof(u_int32_t);
1451 dex_proc->inst_type = *(u_int32_t *)p;
1452 p += sizeof(u_int32_t);
1455 lib_name_len = *(u_int32_t *)p;
1456 p += sizeof(u_int32_t);
1457 dex_proc->name = (char *)p;
1461 lib_name_len = *(u_int32_t *)p;
1462 p += sizeof(u_int32_t);
1463 dex_proc->class_name = (char *)p;
1467 lib_name_len = *(u_int32_t *)p;
1468 p += sizeof(u_int32_t);
1469 dex_proc->method_name = (char *)p;
1473 lib_name_len = *(u_int32_t *)p;
1474 p += sizeof(u_int32_t);
1475 dex_proc->prototype = (char *)p;
1480 // ================================================================================
1481 // END OF DEX Probes
1482 // ================================================================================
1486 /* first, delete all the conds */
1487 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
1491 /* second, add new conds */
1492 /* This can be improved (by placing conds into array) */
1493 nr_conds = *(u_int32_t *)p;
1494 DPRINTF("nr_conds = %d", nr_conds);
1495 p += sizeof(u_int32_t);
1496 for (i = 0; i < nr_conds; i++) {
1497 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
1499 EPRINTF("Cannot alloc cond!\n");
1503 memcpy(&p_cond->tmpl, p, sizeof(struct event_tmpl));
1504 p_cond->applied = 0;
1505 list_add(&(p_cond->list), &(cond_list.list));
1506 p += sizeof(struct event_tmpl);
1510 if (set_event_mask(*(u_int32_t *)p)) {
1511 EPRINTF("Cannot set event mask!");
1515 p += sizeof(u_int32_t);
1520 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
1521 int storage_init (void)
1523 unsigned long spinlock_flags = 0L;
1525 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1526 ec_info.m_nMode = 0; // MASK IS CLEAR (SINGLE NON_CONTINUOUS BUFFER)
1527 // ec_info.m_nMode |= ECMODEMASK_MULTIPLE_BUFFER;
1528 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
1530 #ifndef __DISABLE_RELAYFS
1533 gl_pdirRelay = _dir_create (DEFAULT_RELAY_BASE_DIR, _get_proc_root(), &alt_pde);
1534 if(gl_pdirRelay == NULL) {
1535 EPRINTF("Cannot create procfs directory for relay buffer!");
1539 gl_pdirRelay = debugfs_create_dir(DEFAULT_RELAY_BASE_DIR, NULL);
1540 if(gl_pdirRelay == NULL) {
1541 EPRINTF("Cannot create directory for relay buffer!");
1545 #endif // __USE_PROCFS
1547 #endif //__DISABLE_RELAYFS
1549 if(InitializeBuffer(EC_BUFFER_SIZE_DEFAULT) == -1) {
1550 EPRINTF("Cannot initialize buffer! [Size=%u KB]", EC_BUFFER_SIZE_DEFAULT / 1024 );
1554 INIT_HLIST_HEAD(&kernel_probes);
1555 INIT_HLIST_HEAD(&otg_kernel_probes);
1556 INIT_LIST_HEAD(&otg_us_proc_info);
1557 spin_lock_init(&dbi_mh.lock);
1558 INIT_LIST_HEAD(&dbi_mh.modules_handlers);
1563 Shuts down "storage".
1564 Assumes that all probes are already deactivated.
1566 void storage_down (void)
1568 if(UninitializeBuffer() == -1)
1569 EPRINTF("Cannot uninitialize buffer!");
1571 #ifndef __DISABLE_RELAYFS
1574 // remove_buf(gl_pdirRelay);
1576 debugfs_remove(gl_pdirRelay);
1577 #endif // __USE_PROCFS
1579 #endif //__DISABLE_RELAYFS
1581 if (ec_info.collision_count)
1582 EPRINTF ("ec_info.collision_count=%d", ec_info.collision_count);
1583 if (ec_info.lost_events_count)
1584 EPRINTF ("ec_info.lost_events_count=%d", ec_info.lost_events_count);
1587 u_int32_t get_probe_func_addr(const char *fmt, va_list args)
1592 return va_arg(args, u_int32_t);
1595 void pack_event_info (probe_id_t probe_id, record_type_t record_type, const char *fmt, ...)
1597 unsigned long spinlock_flags = 0L;
1598 static char buf[EVENT_MAX_SIZE] = "";
1599 TYPEOF_EVENT_LENGTH event_len = 0L;
1600 struct timeval tv = { 0, 0 };
1601 TYPEOF_THREAD_ID current_pid = current->pid;
1602 TYPEOF_PROCESS_ID current_tgid = current->tgid;
1603 unsigned current_cpu = task_cpu(current);
1605 unsigned long addr = 0;
1606 struct cond *p_cond;
1607 struct event_tmpl *p_tmpl;
1609 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1610 memset(buf, 0, EVENT_MAX_SIZE);
1611 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1613 do_gettimeofday (&tv);
1615 if (probe_id == KS_PROBE_ID) {
1616 va_start(args, fmt);
1617 addr = get_probe_func_addr(fmt, args);
1619 if( ((addr == pf_addr) && !(probes_flags & PROBE_FLAG_PF_INSTLD)) ||
1620 ((addr == cp_addr) && !(probes_flags & PROBE_FLAG_CP_INSTLD)) ||
1621 ((addr == mr_addr) && !(probes_flags & PROBE_FLAG_MR_INSTLD)) ||
1622 ((addr == unmap_addr) && !(probes_flags & PROBE_FLAG_UNMAP_INSTLD)) ||
1623 ((addr == exit_addr) && !(probes_flags & PROBE_FLAG_EXIT_INSTLD)) ) {
1627 if (probe_id == US_PROBE_ID) {
1628 va_start(args, fmt);
1629 addr = get_probe_func_addr(fmt, args);
1633 /* Checking for all the conditions
1634 * except stop condition that we process after saving the event */
1635 list_for_each_entry(p_cond, &cond_list.list, list) {
1636 p_tmpl = &p_cond->tmpl;
1637 switch (p_tmpl->type) {
1638 case ET_TYPE_START_COND:
1639 if ((!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_ADDR) ||
1640 (addr == p_tmpl->addr)) &&
1641 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_PID) ||
1642 (current_tgid == p_tmpl->pid)) &&
1643 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TID) ||
1644 (current_pid == p_tmpl->tid)) &&
1645 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_CPU_NUM) ||
1646 (current_cpu == p_tmpl->cpu_num)) &&
1647 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_BIN_NAME) ||
1648 (strcmp(current->comm, p_tmpl->bin_name) == 0)) &&
1649 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TIME) ||
1650 (tv.tv_sec > last_attach_time.tv_sec + p_tmpl->sec) ||
1651 (tv.tv_sec == last_attach_time.tv_sec + p_tmpl->sec &&
1652 tv.tv_usec >= last_attach_time.tv_usec + p_tmpl->usec)) &&
1654 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1656 p_cond->applied = 1;
1657 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1660 case ET_TYPE_IGNORE_COND:
1661 /* if (probe_id == PROBE_SCHEDULE) */
1663 if ((!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_ADDR) ||
1664 (addr == p_tmpl->addr)) &&
1665 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_PID) ||
1666 (current_tgid == p_tmpl->pid)) &&
1667 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TID) ||
1668 (current_pid == p_tmpl->tid)) &&
1669 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_CPU_NUM) ||
1670 (current_cpu == p_tmpl->cpu_num)) &&
1671 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_BIN_NAME) ||
1672 (strcmp(current->comm, p_tmpl->bin_name) == 0))) {
1673 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1674 ec_info.ignored_events_count++;
1675 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1682 /* Save only not masked entry or return kernel and user space events */
1683 if (likely(!((probe_id == KS_PROBE_ID || probe_id == US_PROBE_ID)
1684 && ((record_type == RECORD_ENTRY && (event_mask & IOCTL_EMASK_ENTRY))
1685 || (record_type == RECORD_RET && (event_mask & IOCTL_EMASK_EXIT)))))) {
1687 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1689 if (paused && (!(probe_id == EVENT_FMT_PROBE_ID || probe_id == DYN_LIB_PROBE_ID))) {
1690 ec_info.ignored_events_count++;
1691 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1695 va_start (args, fmt);
1696 event_len = VPackEvent(buf, sizeof(buf), event_mask, probe_id, record_type, (TYPEOF_TIME *)&tv,
1697 current_tgid, current_pid, current_cpu, fmt, args);
1700 if(event_len == 0) {
1701 EPRINTF ("ERROR: failed to pack event!");
1702 ++ec_info.lost_events_count;
1704 } else if(WriteEventIntoBuffer(buf, event_len) == -1) {
1705 EPRINTF("Cannot write event into buffer!");
1706 ++ec_info.lost_events_count;
1708 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1712 /* Check for stop condition. We pause collecting the trace right after
1713 * storing this event */
1714 list_for_each_entry(p_cond, &cond_list.list, list) {
1715 p_tmpl = &p_cond->tmpl;
1716 switch (p_tmpl->type) {
1717 case ET_TYPE_STOP_COND:
1718 if ((!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_ADDR) ||
1719 (addr == p_tmpl->addr)) &&
1720 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_PID) ||
1721 (current_tgid == p_tmpl->pid)) &&
1722 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TID) ||
1723 (current_pid == p_tmpl->tid)) &&
1724 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_CPU_NUM) ||
1725 (current_cpu == p_tmpl->cpu_num)) &&
1726 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_BIN_NAME) ||
1727 (strcmp(current->comm, p_tmpl->bin_name) == 0)) &&
1728 (!ET_FIELD_ISSET(p_tmpl->flags, ET_MATCH_TIME) ||
1729 (tv.tv_sec > last_attach_time.tv_sec + p_tmpl->sec) ||
1730 (tv.tv_sec == last_attach_time.tv_sec + p_tmpl->sec &&
1731 tv.tv_usec >= last_attach_time.tv_usec + p_tmpl->usec)) &&
1733 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1735 p_cond->applied = 1;
1736 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1742 EXPORT_SYMBOL_GPL(pack_event_info);
1744 kernel_probe_t* find_probe (unsigned long addr)
1747 struct hlist_node *node;
1749 //check if such probe does exist
1750 hlist_for_each_entry_rcu (p, node, &kernel_probes, hlist)
1751 if (p->addr == addr)
1754 return node ? p : NULL;
1758 int add_probe_to_list (unsigned long addr, kernel_probe_t ** pprobe)
1760 kernel_probe_t *new_probe;
1761 kernel_probe_t *probe;
1765 //check if such probe does already exist
1766 probe = find_probe(addr);
1768 /* It is not a problem if we have already registered
1769 this probe before */
1772 new_probe = kmalloc (sizeof (kernel_probe_t), GFP_KERNEL);
1775 EPRINTF ("no memory for new probe!");
1778 memset (new_probe, 0, sizeof (kernel_probe_t));
1779 new_probe->addr = addr;
1780 new_probe->jprobe.kp.addr = new_probe->retprobe.kp.addr = (kprobe_opcode_t *)addr;
1781 new_probe->jprobe.priv_arg = new_probe->retprobe.priv_arg = new_probe;
1782 //new_probe->jprobe.pre_entry = (kprobe_pre_entry_handler_t) def_jprobe_event_pre_handler;
1783 dbi_find_and_set_handler_for_probe(new_probe);
1784 INIT_HLIST_NODE (&new_probe->hlist);
1785 hlist_add_head_rcu (&new_probe->hlist, &kernel_probes);
1787 *pprobe = new_probe;
1791 int remove_probe_from_list (unsigned long addr)
1795 //check if such probe does exist
1796 p = find_probe (addr);
1798 /* We do not care about it. Nothing bad. */
1802 hlist_del_rcu (&p->hlist);
1810 int put_us_event (char *data, unsigned long len)
1812 unsigned long spinlock_flags = 0L;
1814 SWAP_TYPE_EVENT_HEADER *pEventHeader = (SWAP_TYPE_EVENT_HEADER *)data;
1815 char *cur = data + sizeof(TYPEOF_EVENT_LENGTH) + sizeof(TYPEOF_EVENT_TYPE)
1816 + sizeof(TYPEOF_PROBE_ID);
1817 TYPEOF_NUMBER_OF_ARGS nArgs = pEventHeader->m_nNumberOfArgs;
1818 TYPEOF_PROBE_ID probe_id = pEventHeader->m_nProbeID;
1821 /*if(probe_id == US_PROBE_ID){
1822 printk("esrc %p/%d[", data, len);
1823 for(i = 0; i < len; i++)
1824 printk("%02x ", data[i]);
1828 // set pid/tid/cpu/time i
1829 //pEventHeader->m_time.tv_sec = tv.tv_sec;
1830 //pEventHeader->m_time.tv_usec = tv.tv_usec;
1832 #ifdef MEMORY_CHECKER
1833 //TODO: move this part to special MEC event posting routine, new IOCTL is needed
1834 if((probe_id >= MEC_PROBE_ID_MIN) && (probe_id <= MEC_PROBE_ID_MAX))
1836 if(mec_post_event != NULL)
1838 int res = mec_post_event(data, len);
1846 mec_post_event = lookup_name("mec_post_event");
1847 if(mec_post_event == NULL)
1849 EPRINTF ("Failed to find function 'mec_post_event' from mec_handlers.ko. Memory Error Checker will work incorrectly.");
1853 int res = mec_post_event(data, len);
1863 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_TIME)){
1864 struct timeval tv = { 0, 0 };
1865 do_gettimeofday (&tv);
1866 memcpy(cur, &tv, sizeof(TYPEOF_TIME));
1867 cur += sizeof(TYPEOF_TIME);
1869 //pEventHeader->m_nProcessID = current_tgid;
1870 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_PID)){
1871 //TYPEOF_PROCESS_ID current_tgid = current->tgid;
1872 (*(TYPEOF_PROCESS_ID *)cur) = current->tgid;
1873 cur += sizeof(TYPEOF_PROCESS_ID);
1875 //pEventHeader->m_nThreadID = current_pid;
1876 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_TID)){
1877 //TYPEOF_THREAD_ID current_pid = current->pid;
1878 (*(TYPEOF_THREAD_ID *)cur) = current->pid;
1879 cur += sizeof(TYPEOF_THREAD_ID);
1881 //pEventHeader->m_nCPU = current_cpu;
1882 if((probe_id == EVENT_FMT_PROBE_ID) || !(event_mask & IOCTL_EMASK_CPU)){
1883 //TYPEOF_CPU_NUMBER current_cpu = task_cpu(current);
1884 (*(TYPEOF_CPU_NUMBER *)cur) = task_cpu(current);
1885 cur += sizeof(TYPEOF_CPU_NUMBER);
1887 //printk("%d %x", probe_id, event_mask);
1888 // dyn lib event should have all args, it is for internal use and not visible to user
1889 if((probe_id == EVENT_FMT_PROBE_ID) || (probe_id == DYN_LIB_PROBE_ID) || !(event_mask & IOCTL_EMASK_ARGS)){
1890 // move only if any of prev fields has been skipped
1891 if(event_mask & (IOCTL_EMASK_TIME|IOCTL_EMASK_PID|IOCTL_EMASK_TID|IOCTL_EMASK_CPU)){
1892 memmove(cur, data+sizeof(SWAP_TYPE_EVENT_HEADER)-sizeof(TYPEOF_NUMBER_OF_ARGS),
1893 len-sizeof(SWAP_TYPE_EVENT_HEADER)+sizeof(TYPEOF_NUMBER_OF_ARGS)
1894 -sizeof(TYPEOF_EVENT_LENGTH));
1896 cur += len-sizeof(SWAP_TYPE_EVENT_HEADER)+sizeof(TYPEOF_NUMBER_OF_ARGS)
1897 -sizeof(TYPEOF_EVENT_LENGTH);
1900 // user space probes should have at least one argument to identify them
1901 if((probe_id == US_PROBE_ID) || (probe_id == VTP_PROBE_ID)){
1903 (*(TYPEOF_NUMBER_OF_ARGS *)cur) = 1;
1904 cur += sizeof(TYPEOF_NUMBER_OF_ARGS);
1905 // pack args using format string for the 1st arg only
1906 memset(cur, 0, ALIGN_VALUE(2));
1907 cur[0] = 'p'; cur[1] = '\0';
1908 cur += ALIGN_VALUE(2);
1909 pArg1 = data + sizeof(SWAP_TYPE_EVENT_HEADER)+ALIGN_VALUE(nArgs+1);
1910 memmove(cur, pArg1, sizeof(unsigned long));
1911 cur += sizeof(unsigned long);
1914 (*(TYPEOF_NUMBER_OF_ARGS *)cur) = 0;
1915 cur += sizeof(TYPEOF_NUMBER_OF_ARGS);
1918 pEventHeader->m_nLength = cur - data + sizeof(TYPEOF_EVENT_LENGTH);
1919 *((TYPEOF_EVENT_LENGTH *)cur) = pEventHeader->m_nLength;
1920 len = pEventHeader->m_nLength;
1922 if(WriteEventIntoBuffer(data, len) == -1) {
1923 EPRINTF("Cannot write event into buffer!");
1925 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1926 ++ec_info.lost_events_count;
1927 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
1933 int set_predef_uprobes (ioctl_predef_uprobes_info_t *data)
1935 int i, k, size = 0, probe_size, result, j;
1936 char *buf, *sep1, *sep2;
1937 get_my_uprobes_info_t get_uprobes = NULL;
1938 inst_us_proc_t *my_uprobes_info = NULL;
1940 inst_us_proc_t empty_uprobes_info =
1946 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
1948 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
1950 DPRINTF("my_uprobes_info lookup result: 0x%p", my_uprobes_info);
1951 if (my_uprobes_info == 0)
1952 my_uprobes_info = &empty_uprobes_info;
1954 for(j = 0; j < data->probes_count; j++)
1956 probe_size = strlen_user(data->p_probes+size);
1957 buf = kmalloc(probe_size, GFP_KERNEL);
1961 EPRINTF("failed to alloc mem!");
1965 result = strncpy_from_user(buf, data->p_probes+size, probe_size);
1966 if (result != (probe_size-1))
1968 EPRINTF("failed to copy from user!");
1972 //DPRINTF("%s", buf);
1973 sep1 = strchr(buf, ':');
1976 EPRINTF("skipping invalid predefined uprobe string '%s'!", buf);
1981 sep2 = strchr(sep1+1, ':');
1982 if(!sep2 || (sep2 == sep1) || (sep2+2 == buf+probe_size))
1984 EPRINTF("skipping invalid predefined uprobe string '%s'!", buf);
1989 for(i = 0; i < my_uprobes_info->libs_count; i++)
1991 if(strncmp(buf, my_uprobes_info->p_libs[i].path, sep1-buf) != 0)
1993 for(k = 0; k < my_uprobes_info->p_libs[i].ips_count; k++)
1995 if(strncmp(sep1+1, my_uprobes_info->p_libs[i].p_ips[k].name, sep2-sep1-1) != 0)
1997 my_uprobes_info->p_libs[i].p_ips[k].offset = simple_strtoul(sep2+1, NULL, 16);
2007 int get_predef_uprobes_size(int *size)
2010 get_my_uprobes_info_t get_uprobes = NULL;
2011 inst_us_proc_t *my_uprobes_info = NULL;
2013 inst_us_proc_t empty_uprobes_info =
2019 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
2021 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
2023 if (my_uprobes_info == 0)
2024 my_uprobes_info = &empty_uprobes_info;
2027 for(i = 0; i < my_uprobes_info->libs_count; i++)
2029 int lib_size = strlen(my_uprobes_info->p_libs[i].path);
2030 for(k = 0; k < my_uprobes_info->p_libs[i].ips_count; k++)
2032 // libc.so.6:printf:
2033 *size += lib_size + 1 + strlen(my_uprobes_info->p_libs[i].p_ips[k].name) + 2;
2040 int get_predef_uprobes(ioctl_predef_uprobes_info_t *udata)
2042 ioctl_predef_uprobes_info_t data;
2043 int i, k, size, lib_size, func_size, result;
2047 inst_us_proc_t empty_uprobes_info =
2053 get_my_uprobes_info_t get_uprobes = NULL;
2054 inst_us_proc_t *my_uprobes_info = NULL;
2056 get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
2058 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
2060 if (my_uprobes_info == 0)
2061 my_uprobes_info = &empty_uprobes_info;
2063 // get addr of array
2064 if (copy_from_user ((void *)&data, udata, sizeof (data)))
2066 EPRINTF("failed to copy from user!");
2071 for(i = 0; i < my_uprobes_info->libs_count; i++)
2073 lib_size = strlen(my_uprobes_info->p_libs[i].path);
2074 for(k = 0; k < my_uprobes_info->p_libs[i].ips_count; k++)
2077 result = copy_to_user ((void *)(data.p_probes+size), my_uprobes_info->p_libs[i].path, lib_size);
2080 EPRINTF("failed to copy to user!");
2085 result = copy_to_user ((void *)(data.p_probes+size), sep, 1);
2088 EPRINTF("failed to copy to user!");
2093 //DPRINTF("'%s'", my_uprobes_info->p_libs[i].p_ips[k].name);
2094 func_size = strlen(my_uprobes_info->p_libs[i].p_ips[k].name);
2095 result = copy_to_user ((void *)(data.p_probes+size), my_uprobes_info->p_libs[i].p_ips[k].name, func_size);
2098 EPRINTF("failed to copy to user!");
2103 result = copy_to_user ((void *)(data.p_probes+size), sep, 2);
2106 EPRINTF("failed to copy to user!");
2115 result = copy_to_user ((void *)&(udata->probes_count), &count, sizeof(count));
2118 EPRINTF("failed to copy to user!");