added monitoring function 'do_mnumap'
[kernel/swap-modules.git] / driver / storage.c
1 ////////////////////////////////////////////////////////////////////////////////////
2 //
3 //      FILE:           storage.c
4 //
5 //      DESCRIPTION:
6 //      This file is C source for SWAP.
7 //
8 //      SEE ALSO:       storage.h
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
13 //      VERSION:        1.0
14 //      REVISION DATE:  2008.12.03
15 //
16 ////////////////////////////////////////////////////////////////////////////////////
17
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>
25 #include "module.h"
26 #include "storage.h"
27 #include "handlers_core.h"
28 #include "CProfile.h"
29
30 #define after_buffer ec_info.buffer_size
31
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;
36 char *deps;
37 char *bundle;
38 unsigned int inst_pid = 0;
39 struct hlist_head kernel_probes;
40 struct hlist_head otg_kernel_probes;
41 int event_mask = 0L;
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};
45
46 struct dbi_modules_handlers dbi_mh;
47
48 //
49 // Mr_Nobody: should we use centralized definition of this structure??
50 //
51 struct handler_map {
52         unsigned long func_addr;
53         unsigned long jp_handler_addr;
54         unsigned long rp_handler_addr;
55 };
56
57 struct dbi_modules_handlers *get_dbi_modules_handlers(void)
58 {
59         return &dbi_mh;
60 }
61 EXPORT_SYMBOL_GPL(get_dbi_modules_handlers);
62
63 inline unsigned long find_dbi_jp_handler(unsigned long p_addr, struct dbi_modules_handlers_info *mhi)
64 {
65         int i;
66
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;
72                 }
73         }
74         return 0;
75 }
76
77 inline unsigned long find_dbi_rp_handler(unsigned long p_addr, struct dbi_modules_handlers_info *mhi)
78 {
79         int i;
80
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;
86                 }
87         }
88         return 0;
89 }
90
91 /**
92  * Search of handler in global list of modules for defined probe
93  */
94 void dbi_find_and_set_handler_for_probe(kernel_probe_t *p)
95 {
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;
100
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);
110                         }
111                         else {
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);
117                                         else
118                                                 printk("Module %s in use now\n",
119                                                                 (local_mhi->dbi_module)->name);
120                                 }
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);
124                         }
125                 }
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);
130                         }
131                         else {
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);
137                                         else
138                                                 printk("Module %s in use now\n",
139                                                                 (local_mhi->dbi_module)->name);
140                                 }
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);
144                         }
145                 }
146         }
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);
151         }
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);
156         }
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);
161         }
162         spin_unlock_irqrestore(&dbi_mh.lock, dbi_flags);
163 }
164
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)
168 {
169         unsigned long dbi_flags;
170 //      struct dbi_modules_handlers_info *local_mhi;
171
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);
177         return 0;
178 }
179 EXPORT_SYMBOL_GPL(dbi_register_handlers_module);
180
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)
184 {
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)
188         /*kernel_probe_t *p;
189         struct hlist_node *node;
190         unsigned long jp_handler_addr, rp_handler_addr, pre_handler_addr;*/
191
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);
209                         }
210                 }
211         }*/
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);
214         return 0;
215 }
216 EXPORT_SYMBOL_GPL(dbi_unregister_handlers_module);
217
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;
222
223 unsigned copy_into_cyclic_buffer (char *buffer, unsigned dst_offset, char *src, unsigned size)
224 {
225         unsigned nOffset = dst_offset;
226         char* pSource = src;
227         while (size--)
228                 buffer[nOffset++] = *pSource++;
229         return nOffset;
230 }
231
232 unsigned copy_from_cyclic_buffer (char *dst, char *buffer, unsigned src_offset, unsigned size)
233 {
234         unsigned nOffset = src_offset;
235         char* pDestination = dst;
236         while (size--)
237                 *pDestination++ = buffer[nOffset++];
238         return nOffset;
239 }
240
241 int CheckBufferSize (unsigned int nSize)
242 {
243         if (nSize < EC_BUFFER_SIZE_MIN) {
244                 EPRINTF("Too small buffer size! [Size=%u KB]", nSize / 1024);
245                 return -1;
246         }
247         if (nSize > EC_BUFFER_SIZE_MAX) {
248                 EPRINTF("Too big buffer size! [Size=%u KB]", nSize / 1024);
249                 return -1;
250         }
251         return 0;
252 }
253
254 int AllocateSingleBuffer(unsigned int nSize)
255 {
256         unsigned long spinlock_flags = 0L;
257
258         unsigned int nSubbufferSize = ec_info.m_nSubbufSize;
259         unsigned int nNumOfSubbufers = GetNumOfSubbuffers(nSize);
260         unsigned long nAllocatedSize = nSubbufferSize * nNumOfSubbufers;
261
262         p_buffer = vmalloc_user(nAllocatedSize);
263         if(!p_buffer) {
264                 EPRINTF("Memory allocation error! [Size=%lu KB]", nAllocatedSize / 1024);
265                 return -1;
266         }
267
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);
272
273         return 0;
274 }
275
276 void FreeSingleBuffer (void)
277 {
278         VFREE_USER(p_buffer, ec_info.buffer_size);
279         CleanECInfo();
280 }
281
282 //////////////////////////////////////////////////////////////////////////////////////////////////
283
284 int EnableContinuousRetrieval() {
285         unsigned long spinlock_flags = 0L;
286
287         spin_lock_irqsave (&ec_spinlock, spinlock_flags);
288         ec_info.m_nMode |= MODEMASK_CONTINUOUS_RETRIEVAL;
289         spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
290
291         return 0;
292 }
293
294 int DisableContinuousRetrieval() {
295         unsigned long spinlock_flags = 0L;
296
297         spin_lock_irqsave (&ec_spinlock, spinlock_flags);
298         ec_info.m_nMode &= ~MODEMASK_CONTINUOUS_RETRIEVAL;
299         spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
300
301         return 0;
302 }
303
304 //////////////////////////////////////////////////////////////////////////////////////////////////
305
306 #ifndef __DISABLE_RELAYFS
307
308 struct rchan* gl_pRelayChannel = NULL;
309 struct rchan* GetRelayChannel(void) { return gl_pRelayChannel; };
310
311 struct dentry* gl_pdirRelay = NULL;
312 struct dentry* GetRelayDir(void) { return gl_pdirRelay; };
313
314 #ifdef __USE_PROCFS
315
316 struct proc_dir_entry* alt_pde = NULL;
317
318 static inline struct dentry *_dir_create (const char *dirname, struct dentry *parent, struct proc_dir_entry **p2pde)
319 {
320     struct dentry *dir;
321     struct proc_dir_entry *pde;
322
323     pde = proc_mkdir (dirname, PDE (parent->d_inode));
324     if (pde == NULL)
325     {
326         dir = NULL;
327     }
328     else
329     {
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);
333
334         if (IS_ERR (dir))
335         {
336             dir = NULL;
337             remove_proc_entry (dirname, PDE (parent->d_inode));
338         }
339
340         *p2pde = pde;
341     }
342
343     return dir;
344 }
345
346 static inline struct dentry *_get_proc_root (void)
347 {
348     struct file_system_type *procfs_type;
349     struct super_block *procfs_sb;
350
351     procfs_type = get_fs_type ("proc");
352
353     if (!procfs_type || list_empty (&procfs_type->fs_supers))
354         return NULL;
355
356     procfs_sb = list_entry (procfs_type->fs_supers.next, \
357         struct super_block, s_instances);
358
359     return procfs_sb->s_root;
360
361 }
362
363 static struct dentry *create_buf (const char *filename, struct dentry *parent, int mode, struct rchan_buf *buf, int *is_global)
364 {
365     struct proc_dir_entry *pde;
366     struct proc_dir_entry *parent_pde = NULL;
367     struct dentry *dentry;
368
369     if (parent)
370         parent_pde = PDE (parent->d_inode);
371     else
372         parent = _get_proc_root ();
373
374     pde = create_proc_entry (filename, S_IFREG|S_IRUSR, parent_pde);
375
376     if(unlikely(!pde))
377         return NULL;
378
379     pde->proc_fops = &relay_file_operations;
380
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);
384
385     if (IS_ERR(dentry)) {
386         remove_proc_entry (filename, parent_pde);
387         }
388
389 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
390         dentry->d_inode->i_private = buf;
391 #else
392         dentry->d_inode->u.generic_ip = buf;
393 #endif
394
395     return dentry;
396
397 }
398
399 static int remove_buf (struct dentry *dentry)
400 {
401     if (dentry != NULL)
402     {
403         struct proc_dir_entry *pde = PDE (dentry->d_inode);
404         dput (dentry);
405         remove_proc_entry (pde->name, pde->parent);
406     }
407
408     return 0;
409 }
410
411 #endif // __USE_PROCFS
412         /*
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
418           *
419           * The client should return 1 to continue logging, 0 to stop
420           * logging.
421           *
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.
425           *
426           * NOTE: the client can reserve bytes at the beginning of the new
427           *       sub-buffer by calling subbuf_start_reserve() in this callback.
428           */
429 int RelayCallbackSubbufStart(struct rchan_buf *buf,
430                               void *subbuf,
431                               void *prev_subbuf,
432                               size_t prev_padding)
433 {
434         struct rchan* pRelayChannel = NULL;
435         unsigned int nNumOfSubbufs = 0;
436
437         unsigned long spinlock_flags = 0L;
438         spin_lock_irqsave (&ec_spinlock, spinlock_flags);
439
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;
443
444         if(prev_subbuf == NULL) {
445                 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
446                 return 1;
447         }
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!");
455                 return 0;
456         }
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
482         }
483         spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
484         return 1;
485 }
486
487         /*
488           * buf_mapped - relay buffer mmap notification
489           * @buf: the channel buffer
490           * @filp: relay file pointer
491           *
492           * Called when a relay file is successfully mmapped
493           */
494 void RelayCallbackBufMapped(struct rchan_buf *buf,
495                             struct file *filp)
496 {
497 }
498
499         /*
500           * buf_unmapped - relay buffer unmap notification
501           * @buf: the channel buffer
502           * @filp: relay file pointer
503           *
504           * Called when a relay file is successfully unmapped
505           */
506 void RelayCallbackBufUnmapped(struct rchan_buf *buf,
507                              struct file *filp)
508 {
509 }
510         /*
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
517           *
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
522           * that filesystem.
523           *
524           * The callback should return the dentry of the file created
525           * to represent the relay buffer.
526           *
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.
530           *
531           * See Documentation/filesystems/relayfs.txt for more info.
532           */
533 struct dentry * RelayCallbackCreateBufFile(const char *filename,
534                                            struct dentry *parent,
535                                            int mode,
536                                            struct rchan_buf *buf,
537                                            int *is_global)
538 {
539         *is_global = 1;
540 #ifdef __USE_PROCFS
541         DPRINTF("\"%s\" is creating in procfs...!", filename);
542         return create_buf(filename, parent, mode, buf, is_global);
543 #else
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
547 }
548
549         /*
550           * remove_buf_file - remove file representing a relay channel buffer
551           * @dentry: the dentry of the file to remove
552           *
553           * Called during relay_close(), once for each per-cpu buffer,
554           * to allow the client to remove a file used to represent a
555           * channel buffer.
556           *
557           * The callback should return 0 if successful, negative if not.
558           */
559 int RelayCallbackRemoveBufFile(struct dentry *dentry)
560 {
561 #ifdef __USE_PROCFS
562         remove_buf(dentry);
563 #else
564         debugfs_remove(dentry);
565 #endif // __USE_PROCFS
566         return 0;
567 }
568
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
575 };
576 #endif //__DISABLE_RELAYFS
577
578 int AllocateMultipleBuffer(unsigned int nSize) {
579 #ifndef __DISABLE_RELAYFS
580         unsigned long spinlock_flags = 0L;
581
582         unsigned int nSubbufferSize = ec_info.m_nSubbufSize;
583         unsigned int nNumOfSubbufers = GetNumOfSubbuffers(nSize);
584
585         gl_pRelayChannel = relay_open(DEFAULT_RELAY_BASE_FILENAME,
586                                         GetRelayDir(),
587                                         nSubbufferSize,
588                                         nNumOfSubbufers,
589                                         &gl_RelayCallbacks
590 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
591                                         ,NULL
592 #endif
593                                         );
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);
597                 return -1;
598         }
599
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);
604
605         return 0;
606 #else
607         EPRINTF("RelayFS not supported!");
608         return -1;
609 #endif //__DISABLE_RELAYFS
610 }
611
612 void FreeMultipleBuffer(void) {
613 #ifndef __DISABLE_RELAYFS
614         relay_close(gl_pRelayChannel);
615         CleanECInfo();
616 #else
617         EPRINTF("RelayFS not supported!");
618 #endif //__DISABLE_RELAYFS
619 }
620
621 int InitializeBuffer(unsigned int nSize) {
622         if(IsMultipleBuffer())
623                 return AllocateMultipleBuffer(nSize);
624         return AllocateSingleBuffer(nSize);
625 }
626
627 int UninitializeBuffer(void) {
628         if(IsMultipleBuffer())
629                 FreeMultipleBuffer();
630         FreeSingleBuffer();
631         return 0;
632 }
633
634 int EnableMultipleBuffer() {
635         unsigned long spinlock_flags = 0L;
636
637         if(IsMultipleBuffer())
638                 return 0;
639
640         if(UninitializeBuffer() == -1)
641                 EPRINTF("Cannot uninitialize buffer!");
642
643         spin_lock_irqsave (&ec_spinlock, spinlock_flags);
644         ec_info.m_nMode |= MODEMASK_MULTIPLE_BUFFER;
645         spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
646
647         if(InitializeBuffer(GetBufferSize()) == -1) {
648                 EPRINTF("Cannot initialize buffer!");
649                 return -1;
650         }
651         return 0;
652 }
653
654 int DisableMultipleBuffer() {
655         unsigned long spinlock_flags = 0L;
656
657         if(!IsMultipleBuffer())
658                 return 0;
659
660         if(UninitializeBuffer() == -1)
661                 EPRINTF("Cannot uninitialize buffer!");
662
663         spin_lock_irqsave (&ec_spinlock, spinlock_flags);
664         ec_info.m_nMode &= ~MODEMASK_MULTIPLE_BUFFER;
665         spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
666
667         if(InitializeBuffer(GetBufferSize()) == -1) {
668                 EPRINTF("Cannot initialize buffer!");
669                 return -1;
670         }
671         return 0;
672 }
673
674 unsigned int GetBufferSize(void) { return ec_info.buffer_size; };
675
676 int SetBufferSize(unsigned int nSize) {
677         if (GetECState() != EC_STATE_IDLE) {
678                 EPRINTF("Buffer changes are allowed in IDLE state only (%d)!", GetECState());
679                 return -1;
680         }
681         if(GetBufferSize() == nSize)
682                 return 0;
683         if(CheckBufferSize(nSize) == -1) {
684                 EPRINTF("Invalid buffer size!");
685                 return -1;
686         }
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);
692                 return -1;
693         }
694         return 0;
695 }
696
697 int SetPid(unsigned int pid)
698 {
699         if (GetECState() != EC_STATE_IDLE)
700         {
701                 EPRINTF("PID changes are allowed in IDLE state only (%d)!", GetECState());
702                 return -1;
703         }
704
705         inst_pid = pid;
706         DPRINTF("SetPid pid:%d\n", pid);
707         return 0;
708 }
709
710 void ResetSingleBuffer(void) {
711 }
712
713 void ResetMultipleBuffer(void) {
714 #ifndef __DISABLE_RELAYFS
715         relay_reset(gl_pRelayChannel);
716 #else
717         EPRINTF("RelayFS not supported!");
718 #endif //__DISABLE_RELAYFS
719 }
720
721 int ResetBuffer(void) {
722         unsigned long spinlock_flags = 0L;
723
724         if (GetECState() != EC_STATE_IDLE) {
725                 EPRINTF("Buffer changes are allowed in IDLE state only!");
726                 return -1;
727         }
728
729         if(IsMultipleBuffer())
730                 ResetMultipleBuffer();
731         else
732                 ResetSingleBuffer();
733
734         detach_selected_probes ();
735
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);
739
740         ResetECInfo();
741
742         return 0;
743 }
744
745 int WriteEventIntoSingleBuffer(char* pEvent, unsigned long nEventSize) {
746         unsigned int unused_space;
747
748         if(!p_buffer) {
749                 EPRINTF("Invalid pointer to buffer!");
750                 ++ec_info.lost_events_count;
751                 return -1;
752         }
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,
757                                                                                                                  ec_info.after_last,
758                                                                                                                  pEvent,
759                                                                                                                  nEventSize);
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;
763                 } else {
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,
767                                                                                                                          0,
768                                                                                                                          pEvent,
769                                                                                                                          nEventSize);
770                                 ec_info.saved_events_count++;
771                                 ec_info.trace_size = ec_info.buffer_effect
772                                         - ec_info.first
773                                         + ec_info.after_last;
774                         } else {
775                                 // TODO: consider two variants!
776                                 // Do nothing
777                                 ec_info.discarded_events_count++;
778                         }
779                 }
780         } else {
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,
784                                                                                                                  ec_info.after_last,
785                                                                                                                  pEvent,
786                                                                                                                  nEventSize);
787                         ec_info.saved_events_count++;
788                         ec_info.trace_size = ec_info.buffer_effect
789                                 - ec_info.first
790                                 + ec_info.after_last;
791                 } else {
792                         // Do nothing
793                         ec_info.discarded_events_count++;
794                 }
795         }
796         return 0;
797 }
798
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++;
808         return 0;
809 #else
810         EPRINTF("RelayFS not supported!");
811         return -1;
812 #endif //__DISABLE_RELAYFS
813 }
814
815 int WriteEventIntoBuffer(char* pEvent, unsigned long nEventSize) {
816
817         /*unsigned long i;
818         for(i = 0; i < nEventSize; i++)
819                 printk("%02X ", pEvent[i]);
820         printk("\n");*/
821
822         if(IsMultipleBuffer())
823                 return WriteEventIntoMultipleBuffer(pEvent, nEventSize);
824         return WriteEventIntoSingleBuffer(pEvent, nEventSize);
825 }
826
827 //////////////////////////////////////////////////////////////////////////////////////////////////
828
829 int set_event_mask (int new_mask)
830 {
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);
835         return 0;
836 }
837
838 int
839 get_event_mask (int *mask)
840 {
841         *mask = event_mask;
842         return 0;
843 }
844
845 static void
846 generic_swap (void *a, void *b, int size)
847 {
848         char t;
849         do {
850                 t = *(char *) a;
851                 *(char *) a++ = *(char *) b;
852                 *(char *) b++ = t;
853         } while (--size > 0);
854 }
855
856 static void sort (void *base, size_t num, size_t size, int (*cmp) (const void *, const void *), void (*fswap) (void *, void *, int size))
857 {
858         /* pre-scale counters for performance */
859         int i = (num / 2) * size, n = num * size, c, r;
860
861         /* heapify */
862         for (; i >= 0; i -= size)
863         {
864                 for (r = i; r * 2 < n; r = c)
865                 {
866                         c = r * 2;
867                         if (c < n - size && cmp (base + c, base + c + size) < 0)
868                                 c += size;
869                         if (cmp (base + r, base + c) >= 0)
870                                 break;
871                         fswap (base + r, base + c, size);
872                 }
873         }
874
875         /* sort */
876         for (i = n - size; i >= 0; i -= size)
877         {
878                 fswap (base, base + i, size);
879                 for (r = 0; r * 2 < i; r = c)
880                 {
881                         c = r * 2;
882                         if (c < i - size && cmp (base + c, base + c + size) < 0)
883                                 c += size;
884                         if (cmp (base + r, base + c) >= 0)
885                                 break;
886                         fswap (base + r, base + c, size);
887                 }
888         }
889 }
890
891 static int addr_cmp (const void *a, const void *b)
892 {
893         return *(unsigned long *) a > *(unsigned long *) b ? -1 : 1;
894 }
895
896 char *find_lib_path(const char *lib_name)
897 {
898         char *p = deps + sizeof(size_t);
899         char *match;
900         size_t len;
901
902         while (*p != '\0') {
903                 DPRINTF("p is at %s", p);
904                 len = strlen(p) + 1;
905                 match = strstr(p, lib_name);
906                 p += len;
907                 len = strlen(p) + 1; /* we are at path now */
908                 if (!match) {
909                         p += len;
910                 } else {
911                         DPRINTF("Found match: %s", match);
912                         return p;
913                 }
914         }
915
916         return NULL;
917 }
918
919 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27)
920 #define list_for_each_rcu(pos, head) __list_for_each_rcu(pos, head)
921 #endif
922
923 void unlink_bundle(void)
924 {
925         int i, k;
926         us_proc_lib_t *d_lib;
927         char *path;
928         struct list_head *pos;  //, *tmp;
929         us_proc_otg_ip_t *p;
930
931         path = us_proc_info.path;
932         us_proc_info.path = 0;
933
934         // first make sure "d_lib" is not used any more and only
935         // then release storage
936         if (us_proc_info.p_libs)
937         {
938                 int count1 = us_proc_info.libs_count;
939                 us_proc_info.libs_count = 0;
940                 for (i = 0; i < count1; i++)
941                 {
942                         d_lib = &us_proc_info.p_libs[i];
943                         if (d_lib->p_ips)
944                         {
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);
952                         }
953                         if (d_lib->p_vtps)
954                         {
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++)
960                                 {
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)
963                                         {
964                                                 us_proc_vtp_data_t *vtp = list_entry (pos, us_proc_vtp_data_t, list);
965                                                 list_del_rcu (pos);
966                                                 //kfree (vtp->name);
967                                                 kfree (vtp);
968                                         }
969                                 }
970                                 kfree ((void *) d_lib->p_vtps);
971                         }
972                 }
973                 kfree ((void *) us_proc_info.p_libs);
974                 us_proc_info.p_libs = 0;
975         }
976         /* if (path) */
977         /* { */
978         /*      kfree ((void *) path); */
979         /*      //putname(path); */
980         /* } */
981
982         us_proc_info.tgid = 0;
983
984         /* OTG probes list cleaning */
985         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
986                 list_del_rcu(&p->list);
987         }
988 }
989
990 int link_bundle()
991 {
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 =
995         {
996                 .libs_count = 0,
997                 .p_libs = NULL,
998         };
999         char *p = bundle; /* read pointer for bundle */
1000         int nr_kern_probes;
1001         int i, j, l, k;
1002         int len;
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)
1009         struct path path;
1010 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1011         struct nameidata nd;
1012 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1013         int is_app = 0;
1014         char *ptr;
1015         us_proc_ip_t *d_ip;
1016         struct cond *c, *c_tmp, *p_cond;
1017         size_t nr_conds;
1018         int lib_name_len;
1019         int handler_index;
1020
1021         /* Get user-defined us handlers (if they are provided) */
1022         get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
1023         if (get_uprobes)
1024                 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
1025
1026         if (my_uprobes_info == 0)
1027                 my_uprobes_info = &empty_uprobes_info;
1028
1029         DPRINTF("Going to release us_proc_info");
1030         if (us_proc_info.path)
1031                 unlink_bundle();
1032
1033         /* Skip size - it has been used before */
1034         p += sizeof(u_int32_t);
1035
1036         /* Set mode */
1037         if (SetECMode(*(u_int32_t *)p) == -1)
1038         {
1039                 EPRINTF("Cannot set mode!\n");
1040                 return -1;
1041         }
1042
1043         p += sizeof(u_int32_t);
1044
1045         /* Buffer size */
1046         if (SetBufferSize(*(u_int32_t *)p) == -1)
1047         {
1048                 EPRINTF("Cannot set buffer size!\n");
1049                 return -1;
1050         }
1051
1052         p += sizeof(u_int32_t);
1053
1054         /* Pid */
1055         if (SetPid(*(u_int32_t *)p) == -1)
1056         {
1057                 EPRINTF("Cannot set pid!\n");
1058                 return -1;
1059         }
1060
1061         p += sizeof(u_int32_t);
1062
1063         /* Kernel probes */
1064         nr_kern_probes = *(u_int32_t *)p;
1065         p += sizeof(u_int32_t);
1066         for (i = 0; i < nr_kern_probes; i++)
1067         {
1068                 if (add_probe(*(u_int32_t *)p))
1069                 {
1070                         EPRINTF("Cannot add kernel probe at 0x%x!\n", *(u_int32_t *)p);
1071                         return -1;
1072                 }
1073                 p += sizeof(u_int32_t);
1074         }
1075
1076         /* Us probes */
1077         len = *(u_int32_t *)p; /* App path len */
1078         p += sizeof(u_int32_t);
1079
1080         if ( len == 0 )
1081         {
1082             us_proc_info.path = NULL;
1083         }
1084         else
1085         {
1086                 int lib_path_len;
1087                 char *lib_path;
1088
1089                 us_proc_info.path = (char *)p;
1090                 DPRINTF("app path = %s", us_proc_info.path);
1091                 p += len;
1092
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);
1100                                 return -1;
1101                         }
1102 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
1103             us_proc_info.m_f_dentry = nd.dentry;
1104             path_release(&nd);
1105 #else
1106 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
1107             us_proc_info.m_f_dentry = path.dentry;
1108             path_put(&path);
1109 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1110             us_proc_info.m_f_dentry = nd.path.dentry;
1111             path_put(&nd.path);
1112 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1113 #endif
1114                 }
1115                 else
1116                 {
1117                         us_proc_info.m_f_dentry = NULL;
1118                 }
1119
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);
1125
1126                 if (!us_proc_info.p_libs)
1127                 {
1128                         EPRINTF("Cannot alloc p_libs!");
1129                         return -1;
1130                 }
1131                 memset(us_proc_info.p_libs, 0,
1132                            us_proc_info.libs_count * sizeof(us_proc_lib_t));
1133
1134                 for (i = 0; i < us_proc_info.libs_count; i++)
1135                 {
1136                         int abs_handler_idx = 0;
1137
1138                         d_lib = &us_proc_info.p_libs[i];
1139
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);
1144                         p += lib_name_len;
1145
1146                         if ( i != 0 ) {
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);
1151                                 p += lib_name_len;
1152                         }
1153
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);
1157
1158                         /* If there are any probes for "*" app we have to drop them */
1159                         if (strcmp(d_lib->path, "*") == 0)
1160                         {
1161                                 p += d_lib->ips_count * 3 * sizeof(u_int32_t);
1162                                 d_lib->ips_count = 0;
1163                                 continue;
1164                         }
1165
1166                         if (strcmp(us_proc_info.path, d_lib->path) == 0)
1167                                 is_app = 1;
1168                         else
1169                         {
1170                                 is_app = 0;
1171                                 DPRINTF("Searching path for lib %s", d_lib->path);
1172                                 d_lib->path = find_lib_path(d_lib->path);
1173                                 if (!d_lib->path)
1174                                 {
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;
1180                                                 continue;
1181                                         }
1182                                         else {
1183                                                 d_lib->path = d_lib->path_dyn;
1184                                                 DPRINTF("Assign path for lib as %s (in suggestion of dyn lib", d_lib->path);
1185                                         }
1186                                 }
1187                         }
1188
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;
1198                                 continue;
1199                         }
1200         #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
1201                         d_lib->m_f_dentry = nd.dentry;
1202                         path_release(&nd);
1203         #else
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;
1207                         path_put(&path);
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;
1211                         path_put(&nd.path);
1212 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1213         #endif
1214
1215                         pd_lib = NULL;
1216                         ptr = strrchr(d_lib->path, '/');
1217                         if (ptr)
1218                                 ptr++;
1219                         else
1220                                 ptr = d_lib->path;
1221
1222                         for (l = 0; l < my_uprobes_info->libs_count; l++)
1223                         {
1224                                 if ((strcmp(ptr, my_uprobes_info->p_libs[l].path) == 0) ||
1225                                         (is_app && *(my_uprobes_info->p_libs[l].path) == '\0'))
1226                                 {
1227                                         pd_lib = &my_uprobes_info->p_libs[l];
1228                                         break;
1229                                 }
1230                                 abs_handler_idx += my_uprobes_info->p_libs[l].ips_count;
1231                         }
1232                         if (d_lib->ips_count > 0)
1233                         {
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);
1238
1239                                 if (!d_lib->p_ips)
1240                                 {
1241                                         EPRINTF("Cannot alloc p_ips!\n");
1242                                         return -1;
1243                                 }
1244
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++)
1247                                 {
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);
1254
1255                                         if (pd_lib)
1256                                         {
1257                                                 DPRINTF("pd_lib->ips_count = 0x%x", pd_lib->ips_count);
1258                                                 if (handler_index != -1)
1259                                                 {
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;
1267                                                 }
1268                                         }
1269                                 }
1270                         }
1271                 }
1272
1273                 /* Lib path */
1274                 lib_path_len = *(u_int32_t *)p;
1275                 DPRINTF("lib_path_len = %d", lib_path_len);
1276                 p += sizeof(u_int32_t);
1277                 lib_path = p;
1278                 DPRINTF("lib_path = %s", lib_path);
1279                 p += lib_path_len;
1280
1281                 /* Link FBI info */
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)
1287                 {
1288                         unsigned long ucount = 1, pre_addr;
1289                         unsigned long *addrs;
1290
1291                         s_lib.p_vtps = kmalloc(s_lib.vtps_count
1292                                                                    * sizeof(ioctl_usr_space_vtp_t), GFP_KERNEL);
1293                         if (!s_lib.p_vtps)
1294                         {
1295                                 //kfree (addrs);
1296                                 return -1;
1297                         }
1298
1299                         for (i = 0; i < s_lib.vtps_count; i++)
1300                         {
1301                                 int var_name_len = *(u_int32_t *)p;
1302                                 p += sizeof(u_int32_t);
1303                                 s_lib.p_vtps[i].name = p;
1304                                 p += var_name_len;
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);
1315                         }
1316
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);
1320                         if (!addrs)
1321                         {
1322                                 //note: storage will released next time or at clean-up moment
1323                                 return -ENOMEM;
1324                         }
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++)
1328                         {
1329                                 s_vtp = &s_lib.p_vtps[k];
1330                                 addrs[2 * k] = s_vtp->addr;
1331                                 addrs[2 * k + 1] = k;
1332                         }
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);
1336
1337                         // calc number of VTPs with unique addresses
1338                         for (k = 1, pre_addr = addrs[0]; k < s_lib.vtps_count; k++)
1339                         {
1340                                 if (addrs[2 * k] != pre_addr)
1341                                         ucount++;       // count different only
1342                                 pre_addr = addrs[2 * k];
1343                         }
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);
1348                         if (!d_lib->p_vtps)
1349                         {
1350                                 //note: storage will released next time or at clean-up moment
1351                                 kfree (addrs);
1352                                 return -ENOMEM;
1353                         }
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++)
1357                         {
1358                                 us_proc_vtp_data_t *vtp_data;
1359                                 // copy 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)
1363                                 {
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);
1368                                 }
1369                                 // data are in the list of slave VTPs
1370                                 vtp_data = kmalloc (sizeof (us_proc_vtp_data_t), GFP_KERNEL);
1371                                 if (!vtp_data)
1372                                 {
1373                                         //note: storage will released next time or at clean-up moment
1374                                         kfree (addrs);
1375                                         return -ENOMEM;
1376                                 }
1377
1378                                 /*len = strlen_user (s_vtp->name);
1379                                   vtp_data->name = kmalloc (len, GFP_KERNEL);
1380                                   if (!vtp_data->name)
1381                                   {
1382                                   //note: storage will released next time or at clean-up moment
1383                                   kfree (vtp_data);
1384                                   kfree (addrs);
1385                                   return -ENOMEM;
1386                                   }
1387                                   if (strncpy_from_user (vtp_data->name, s_vtp->name, len) != (len-1))
1388                                   {
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);
1392                                   kfree (vtp_data);
1393                                   kfree (addrs);
1394                                   return -EFAULT;
1395                                   }
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];
1404                         }
1405                         kfree (addrs);
1406                         kfree(s_lib.p_vtps);
1407                 }
1408         }
1409
1410         // ================================================================================
1411         // DEX Probes
1412         // ================================================================================
1413         len = *(u_int32_t *)p; /* App path len */
1414         p += sizeof(u_int32_t);
1415
1416         if ( len == 0 )
1417         {
1418             dex_proc_info.path = NULL;
1419         }
1420         else
1421         {
1422                 dex_proc_info.path = p;
1423                 DPRINTF("dex path = %s", dex_proc_info.path);
1424                 p += len;
1425
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);
1429
1430                 dex_proc_info.p_ips =
1431                         kmalloc(dex_proc_info.ips_count * sizeof(dex_proc_ip_t), GFP_KERNEL);
1432
1433                 if (!dex_proc_info.p_ips)
1434                 {
1435                         EPRINTF("Cannot alloc dex probes!");
1436                         return -1;
1437                 }
1438
1439                 memset(dex_proc_info.p_ips, 0,
1440                            dex_proc_info.ips_count * sizeof(dex_proc_ip_t));
1441
1442                 for (i = 0; i < dex_proc_info.ips_count; i++)
1443                 {
1444                         dex_proc = &dex_proc_info.p_ips[i];
1445
1446                         // fill up dex proc
1447
1448                         dex_proc->addr = *(u_int32_t *)p;
1449                         p += sizeof(u_int32_t);
1450
1451                         dex_proc->inst_type = *(u_int32_t *)p;
1452                         p += sizeof(u_int32_t);
1453
1454                         // name
1455                         lib_name_len = *(u_int32_t *)p;
1456                         p += sizeof(u_int32_t);
1457                         dex_proc->name = (char *)p;
1458                         p += lib_name_len;
1459
1460                         // class name
1461                         lib_name_len = *(u_int32_t *)p;
1462                         p += sizeof(u_int32_t);
1463                         dex_proc->class_name = (char *)p;
1464                         p += lib_name_len;
1465
1466                         // method name
1467                         lib_name_len = *(u_int32_t *)p;
1468                         p += sizeof(u_int32_t);
1469                         dex_proc->method_name = (char *)p;
1470                         p += lib_name_len;
1471
1472                         // prototype
1473                         lib_name_len = *(u_int32_t *)p;
1474                         p += sizeof(u_int32_t);
1475                         dex_proc->prototype = (char *)p;
1476                         p += lib_name_len;
1477                 }
1478
1479         }
1480         // ================================================================================
1481         // END OF DEX Probes
1482         // ================================================================================
1483
1484
1485         /* Conds */
1486         /* first, delete all the conds */
1487         list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
1488                 list_del(&c->list);
1489                 kfree(c);
1490         }
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);
1498                 if (!p_cond) {
1499                         EPRINTF("Cannot alloc cond!\n");
1500                         return -1;
1501                         break;
1502                 }
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);
1507         }
1508
1509         /* Event mask */
1510         if (set_event_mask(*(u_int32_t *)p)) {
1511                 EPRINTF("Cannot set event mask!");
1512                 return -1;
1513         }
1514
1515         p += sizeof(u_int32_t);
1516
1517         return 0;
1518 }
1519
1520 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
1521 int storage_init (void)
1522 {
1523         unsigned long spinlock_flags = 0L;
1524
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);
1529
1530 #ifndef __DISABLE_RELAYFS
1531
1532 #ifdef __USE_PROCFS
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!");
1536                 return -1;
1537         }
1538 #else
1539         gl_pdirRelay = debugfs_create_dir(DEFAULT_RELAY_BASE_DIR, NULL);
1540         if(gl_pdirRelay == NULL) {
1541                 EPRINTF("Cannot create directory for relay buffer!");
1542                 return -1;
1543         }
1544
1545 #endif // __USE_PROCFS
1546
1547 #endif //__DISABLE_RELAYFS
1548
1549         if(InitializeBuffer(EC_BUFFER_SIZE_DEFAULT) == -1) {
1550                 EPRINTF("Cannot initialize buffer! [Size=%u KB]", EC_BUFFER_SIZE_DEFAULT / 1024 );
1551                 return -1;
1552         }
1553
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);
1559         return 0;
1560 }
1561
1562 /*
1563     Shuts down "storage".
1564     Assumes that all probes are already deactivated.
1565 */
1566 void storage_down (void)
1567 {
1568         if(UninitializeBuffer() == -1)
1569                 EPRINTF("Cannot uninitialize buffer!");
1570
1571 #ifndef __DISABLE_RELAYFS
1572
1573 #ifdef __USE_PROCFS
1574 //      remove_buf(gl_pdirRelay);
1575 #else
1576         debugfs_remove(gl_pdirRelay);
1577 #endif // __USE_PROCFS
1578
1579 #endif //__DISABLE_RELAYFS
1580
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);
1585 }
1586
1587 u_int32_t get_probe_func_addr(const char *fmt, va_list args)
1588 {
1589         if (fmt[0] != 'p')
1590                 return 0;
1591
1592         return va_arg(args, u_int32_t);
1593 }
1594
1595 void pack_event_info (probe_id_t probe_id, record_type_t record_type, const char *fmt, ...)
1596 {
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);
1604         va_list args;
1605         unsigned long addr = 0;
1606         struct cond *p_cond;
1607         struct event_tmpl *p_tmpl;
1608
1609         spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1610         memset(buf, 0, EVENT_MAX_SIZE);
1611         spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1612
1613         do_gettimeofday (&tv);
1614
1615         if (probe_id == KS_PROBE_ID) {
1616                 va_start(args, fmt);
1617                 addr = get_probe_func_addr(fmt, args);
1618                 va_end(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)) ) {
1624                         return;
1625                 }
1626         }
1627         if (probe_id == US_PROBE_ID) {
1628                 va_start(args, fmt);
1629                 addr = get_probe_func_addr(fmt, args);
1630                 va_end(args);
1631         }
1632
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)) &&
1653                                 !p_cond->applied) {
1654                                 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1655                                 paused = 0;
1656                                 p_cond->applied = 1;
1657                                 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1658                         }
1659                         break;
1660                 case ET_TYPE_IGNORE_COND:
1661                         /* if (probe_id == PROBE_SCHEDULE) */
1662                         /*      break; */
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);
1676                                 return;
1677                         }
1678                         break;
1679                 }
1680         }
1681
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)))))) {
1686
1687                 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1688
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);
1692                         return;
1693                 }
1694
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);
1698                 va_end (args);
1699
1700                 if(event_len == 0) {
1701                         EPRINTF ("ERROR: failed to pack event!");
1702                         ++ec_info.lost_events_count;
1703
1704                 } else if(WriteEventIntoBuffer(buf, event_len) == -1) {
1705                         EPRINTF("Cannot write event into buffer!");
1706                         ++ec_info.lost_events_count;
1707                 }
1708                 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1709
1710         }
1711
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)) &&
1732                                 !p_cond->applied) {
1733                                 spin_lock_irqsave(&ec_spinlock, spinlock_flags);
1734                                 paused = 1;
1735                                 p_cond->applied = 1;
1736                                 spin_unlock_irqrestore(&ec_spinlock, spinlock_flags);
1737                         }
1738                         break;
1739                 }
1740         }
1741 }
1742 EXPORT_SYMBOL_GPL(pack_event_info);
1743
1744 kernel_probe_t* find_probe (unsigned long addr)
1745 {
1746         kernel_probe_t *p;
1747         struct hlist_node *node;
1748
1749         //check if such probe does exist
1750         hlist_for_each_entry_rcu (p, node, &kernel_probes, hlist)
1751                 if (p->addr == addr)
1752                         break;
1753
1754         return node ? p : NULL;
1755 }
1756
1757
1758 int add_probe_to_list (unsigned long addr, kernel_probe_t ** pprobe)
1759 {
1760         kernel_probe_t *new_probe;
1761         kernel_probe_t *probe;
1762
1763         if (pprobe)
1764                 *pprobe = NULL;
1765         //check if such probe does already exist
1766         probe = find_probe(addr);
1767         if (probe) {
1768                 /* It is not a problem if we have already registered
1769                    this probe before */
1770                 return 0;
1771         }
1772         new_probe = kmalloc (sizeof (kernel_probe_t), GFP_KERNEL);
1773         if (!new_probe)
1774         {
1775                 EPRINTF ("no memory for new probe!");
1776                 return -ENOMEM;
1777         }
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);
1786         if (pprobe)
1787                 *pprobe = new_probe;
1788         return 0;
1789 }
1790
1791 int remove_probe_from_list (unsigned long addr)
1792 {
1793         kernel_probe_t *p;
1794
1795         //check if such probe does exist
1796         p = find_probe (addr);
1797         if (!p) {
1798                 /* We do not care about it. Nothing bad. */
1799                 return 0;
1800         }
1801
1802         hlist_del_rcu (&p->hlist);
1803
1804         kfree (p);
1805
1806         return 0;
1807 }
1808
1809
1810 int put_us_event (char *data, unsigned long len)
1811 {
1812         unsigned long spinlock_flags = 0L;
1813
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;
1819         //int i;
1820
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]);
1825                 printk("]\n");
1826         }*/
1827
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;
1831
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))
1835         {
1836                 if(mec_post_event != NULL)
1837                 {
1838                         int res = mec_post_event(data, len);
1839                         if(res == -1)
1840                         {
1841                                 return -1;
1842                         }
1843                 }
1844                 else
1845                 {
1846                         mec_post_event = lookup_name("mec_post_event");
1847                         if(mec_post_event == NULL)
1848                         {
1849                                 EPRINTF ("Failed to find function 'mec_post_event' from mec_handlers.ko. Memory Error Checker will work incorrectly.");
1850                         }
1851                         else
1852                         {
1853                                 int res = mec_post_event(data, len);
1854                                 if(res == -1)
1855                                 {
1856                                         return -1;
1857                                 }
1858                         }
1859                 }
1860         }
1861 #endif
1862
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);
1868         }
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);
1874         }
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);
1880         }
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);
1886         }
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));
1895                 }
1896                 cur += len-sizeof(SWAP_TYPE_EVENT_HEADER)+sizeof(TYPEOF_NUMBER_OF_ARGS)
1897                                 -sizeof(TYPEOF_EVENT_LENGTH);
1898         }
1899         else{
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)){
1902                         char *pArg1;
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);
1912                 }
1913                 else {
1914                         (*(TYPEOF_NUMBER_OF_ARGS *)cur) = 0;
1915                         cur += sizeof(TYPEOF_NUMBER_OF_ARGS);
1916                 }
1917         }
1918         pEventHeader->m_nLength = cur - data + sizeof(TYPEOF_EVENT_LENGTH);
1919         *((TYPEOF_EVENT_LENGTH *)cur) = pEventHeader->m_nLength;
1920         len = pEventHeader->m_nLength;
1921
1922         if(WriteEventIntoBuffer(data, len) == -1) {
1923                 EPRINTF("Cannot write event into buffer!");
1924
1925                 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
1926                 ++ec_info.lost_events_count;
1927                 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
1928         }
1929
1930         return 0;
1931 }
1932
1933 int set_predef_uprobes (ioctl_predef_uprobes_info_t *data)
1934 {
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;
1939
1940         inst_us_proc_t empty_uprobes_info =
1941         {
1942                 .libs_count = 0,
1943                 .p_libs = NULL,
1944         };
1945
1946         get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
1947         if (get_uprobes)
1948                 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
1949
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;
1953
1954         for(j = 0; j < data->probes_count; j++)
1955         {
1956                 probe_size = strlen_user(data->p_probes+size);
1957                 buf = kmalloc(probe_size, GFP_KERNEL);
1958
1959                 if(!buf)
1960                 {
1961                         EPRINTF("failed to alloc mem!");
1962                         return -EFAULT;
1963                 }
1964
1965                 result = strncpy_from_user(buf, data->p_probes+size, probe_size);
1966                 if (result != (probe_size-1))
1967                 {
1968                         EPRINTF("failed to copy from user!");
1969                         kfree(buf);
1970                         return -EFAULT;
1971                 }
1972                 //DPRINTF("%s", buf);
1973                 sep1 = strchr(buf, ':');
1974                 if(!sep1)
1975                 {
1976                         EPRINTF("skipping invalid predefined uprobe string '%s'!", buf);
1977                         kfree(buf);
1978                         size += probe_size;
1979                         continue;
1980                 }
1981                 sep2 = strchr(sep1+1, ':');
1982                 if(!sep2 || (sep2 == sep1) || (sep2+2 == buf+probe_size))
1983                 {
1984                         EPRINTF("skipping invalid predefined uprobe string '%s'!", buf);
1985                         kfree(buf);
1986                         size += probe_size;
1987                         continue;
1988                 }
1989                 for(i = 0; i < my_uprobes_info->libs_count; i++)
1990                 {
1991                         if(strncmp(buf, my_uprobes_info->p_libs[i].path, sep1-buf) != 0)
1992                                 continue;
1993                         for(k = 0; k < my_uprobes_info->p_libs[i].ips_count; k++)
1994                         {
1995                                 if(strncmp(sep1+1, my_uprobes_info->p_libs[i].p_ips[k].name, sep2-sep1-1) != 0)
1996                                         continue;
1997                                 my_uprobes_info->p_libs[i].p_ips[k].offset = simple_strtoul(sep2+1, NULL, 16);
1998                         }
1999                 }
2000
2001                 kfree(buf);
2002                 size += probe_size;
2003         }
2004         return 0;
2005 }
2006
2007 int get_predef_uprobes_size(int *size)
2008 {
2009         int i, k;
2010         get_my_uprobes_info_t get_uprobes = NULL;
2011         inst_us_proc_t *my_uprobes_info = NULL;
2012
2013         inst_us_proc_t empty_uprobes_info =
2014         {
2015                 .libs_count = 0,
2016                 .p_libs = NULL,
2017         };
2018
2019         get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
2020         if (get_uprobes)
2021                 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
2022
2023         if (my_uprobes_info == 0)
2024                 my_uprobes_info = &empty_uprobes_info;
2025
2026         *size = 0;
2027         for(i = 0; i < my_uprobes_info->libs_count; i++)
2028         {
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++)
2031                 {
2032                         // libc.so.6:printf:
2033                         *size += lib_size + 1 + strlen(my_uprobes_info->p_libs[i].p_ips[k].name) + 2;
2034                 }
2035         }
2036
2037         return 0;
2038 }
2039
2040 int get_predef_uprobes(ioctl_predef_uprobes_info_t *udata)
2041 {
2042         ioctl_predef_uprobes_info_t data;
2043         int i, k, size, lib_size, func_size, result;
2044         unsigned count = 0;
2045         char sep[] = ":";
2046
2047         inst_us_proc_t empty_uprobes_info =
2048         {
2049                 .libs_count = 0,
2050                 .p_libs = NULL,
2051         };
2052
2053         get_my_uprobes_info_t get_uprobes = NULL;
2054         inst_us_proc_t *my_uprobes_info = NULL;
2055
2056         get_uprobes = (get_my_uprobes_info_t)lookup_name("get_my_uprobes_info");
2057         if (get_uprobes)
2058                 my_uprobes_info = (inst_us_proc_t *)get_uprobes();
2059
2060         if (my_uprobes_info == 0)
2061                 my_uprobes_info = &empty_uprobes_info;
2062
2063         // get addr of array
2064         if (copy_from_user ((void *)&data, udata, sizeof (data)))
2065         {
2066                 EPRINTF("failed to copy from user!");
2067                 return -EFAULT;
2068         }
2069
2070         size = 0;
2071         for(i = 0; i < my_uprobes_info->libs_count; i++)
2072         {
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++)
2075                 {
2076                         // libname
2077                         result = copy_to_user ((void *)(data.p_probes+size), my_uprobes_info->p_libs[i].path, lib_size);
2078                         if (result)
2079                         {
2080                                 EPRINTF("failed to copy to user!");
2081                                 return -EFAULT;
2082                         }
2083                         size += lib_size;
2084                         // ":"
2085                         result = copy_to_user ((void *)(data.p_probes+size), sep, 1);
2086                         if (result)
2087                         {
2088                                 EPRINTF("failed to copy to user!");
2089                                 return -EFAULT;
2090                         }
2091                         size++;
2092                         // probename
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);
2096                         if (result)
2097                         {
2098                                 EPRINTF("failed to copy to user!");
2099                                 return -EFAULT;
2100                         }
2101                         size += func_size;
2102                         // ":\0"
2103                         result = copy_to_user ((void *)(data.p_probes+size), sep, 2);
2104                         if (result)
2105                         {
2106                                 EPRINTF("failed to copy to user!");
2107                                 return -EFAULT;
2108                         }
2109                         size += 2;
2110                         count++;
2111                 }
2112         }
2113
2114         // set probes_count
2115         result = copy_to_user ((void *)&(udata->probes_count), &count, sizeof(count));
2116         if (result)
2117         {
2118                 EPRINTF("failed to copy to user!");
2119                 return -EFAULT;
2120         }
2121
2122         return 0;
2123 }