forked from teawater/kgtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtp_2.6.20_to_2.6.32.patch
16166 lines (16161 loc) · 378 KB
/
gtp_2.6.20_to_2.6.32.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Index: linux/arch/arm/mm/flush.c
===================================================================
--- linux.orig/arch/arm/mm/flush.c 2014-04-05 05:55:52.240062739 +0800
+++ linux/arch/arm/mm/flush.c 2014-04-05 05:56:33.581698070 +0800
@@ -251,3 +251,4 @@
*/
__cpuc_flush_dcache_page(page_address(page));
}
+EXPORT_SYMBOL(__flush_anon_page);
Index: linux/include/linux/kprobes.h
===================================================================
--- linux.orig/include/linux/kprobes.h 2014-04-05 05:56:29.729961885 +0800
+++ linux/include/linux/kprobes.h 2014-04-05 05:56:33.591635366 +0800
@@ -1,5 +1,8 @@
#ifndef _LINUX_KPROBES_H
#define _LINUX_KPROBES_H
+
+#define KGTP_API_VERSION 20120917
+
/*
* Kernel Probes (KProbes)
* include/linux/kprobes.h
Index: linux/lib/Kconfig.debug
===================================================================
--- linux.orig/lib/Kconfig.debug 2014-04-05 05:56:30.109976109 +0800
+++ linux/lib/Kconfig.debug 2014-04-05 05:56:33.591635366 +0800
@@ -586,6 +586,16 @@
If unsure, say N.
+config GTP
+ tristate "GDB tracepoint support"
+ depends on X86 || ARM || MIPS
+ select KPROBES
+ select DEBUG_FS
+ ---help---
+ Supply GDB tracepoint interface in /sys/kernel/debug/gtp.
+ See Documentation/trace/gtp.txt or
+ https://code.google.com/p/kgtp/wiki/HOWTO for more info.
+
config DEBUG_VM
bool "Debug VM"
depends on DEBUG_KERNEL
Index: linux/lib/Makefile
===================================================================
--- linux.orig/lib/Makefile 2014-04-05 05:56:30.109976109 +0800
+++ linux/lib/Makefile 2014-04-05 05:56:33.591635366 +0800
@@ -97,6 +97,8 @@
obj-$(CONFIG_GENERIC_ATOMIC64) += atomic64.o
+obj-$(CONFIG_GTP) += gtp.o
+
hostprogs-y := gen_crc32table
clean-files := crc32table.h
Index: linux/lib/gtp.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ linux/lib/gtp.c 2014-04-05 05:56:33.731835864 +0800
@@ -0,0 +1,13524 @@
+/*
+ * Kernel GDB tracepoint module.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright(C) KGTP team (https://code.google.com/p/kgtp/), 2010-2013
+ *
+ */
+
+/* If "* 10" means that this is not a release version. */
+#define GTP_VERSION (20140510)
+
+#include <linux/version.h>
+#ifndef RHEL_RELEASE_VERSION
+#define RHEL_RELEASE_VERSION(a,b) (((a) << 8) + (b))
+#define RHEL_RELEASE_CODE 0
+#endif
+
+/* Sepcial config ------------------------------------------------ */
+#define GTP_RB
+
+#ifdef GTP_FRAME_SIMPLE
+/* This is a debug option.
+ This define is for simple frame alloc record, then we can get how many
+ memory are weste by FRAME_ALIGN. */
+/* #define FRAME_ALLOC_RECORD */
+#undef GTP_RB
+#endif
+
+#ifdef GTP_FTRACE_RING_BUFFER
+#undef GTP_RB
+#endif
+
+/* If define USE_PROC, KGTP will use ProcFS instead DebugFS. */
+#ifndef GTP_NO_AUTO_BUILD
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11))
+#define USE_PROC
+#endif
+#endif
+#ifndef USE_PROC
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11))
+#warning If got some build error about debugfs, you can use "USE_PROC=1" handle it.
+#endif
+#endif
+
+#ifdef GTP_FTRACE_RING_BUFFER
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
+#warning If got some build error about ring buffer, you can use "FRAME_SIMPLE=1" handle it.
+#endif
+#endif
+
+/* If define GTP_CLOCK_CYCLE, $clock will return rdtscll. */
+#ifndef GTP_NO_AUTO_BUILD
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
+#define GTP_CLOCK_CYCLE
+#endif
+#endif
+#ifndef GTP_CLOCK_CYCLE
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
+#warning If got some build error about cpu_clock or local_clock, you can use "CLOCK_CYCLE=1" handle it.
+#endif
+#endif
+
+#ifdef CONFIG_LOCKDEP
+#warning Current kernel open the runtime locking correctness validator (CONFIG_LOCKDEP) that will make KGTP trace functions about locks get deadlock. Please DO NOT trace function about locks.
+#endif
+
+#ifdef GTP_FTRACE_RING_BUFFER
+#ifndef CONFIG_RING_BUFFER
+#define CONFIG_RING_BUFFER
+#include "ring_buffer.h"
+#include "ring_buffer.c"
+#define GTP_SELF_RING_BUFFER
+#warning Use the ring buffer inside KGTP.
+#endif
+#endif
+/* Sepcial config ------------------------------------------------ */
+
+#include <linux/kernel.h>
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0))
+#undef MODULE
+#include <linux/preempt.h>
+#define MODULE
+#endif
+#include <linux/module.h>
+#include <linux/uaccess.h>
+#include <linux/vmalloc.h>
+#include <linux/poll.h>
+#include <linux/kprobes.h>
+#include <linux/interrupt.h>
+#include <linux/proc_fs.h>
+#include <linux/debugfs.h>
+#include <linux/highmem.h>
+#include <linux/pagemap.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/ctype.h>
+#include <asm/atomic.h>
+#ifdef CONFIG_X86
+#include <asm/debugreg.h>
+#endif
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
+#include <linux/kdebug.h>
+#else
+#include <asm/kdebug.h>
+#endif
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0))
+#include <linux/hw_breakpoint.h>
+#endif
+#include "gtp.h"
+
+#ifdef GTP_FTRACE_RING_BUFFER
+#ifndef GTP_SELF_RING_BUFFER
+#include <linux/ring_buffer.h>
+#endif
+#endif
+#ifdef CONFIG_PERF_EVENTS
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)) \
+ && (RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(6,1))
+#warning "Current Kernel is too old. Function of performance counters is not available."
+#else
+#include <linux/perf_event.h>
+#define GTP_PERF_EVENTS
+#endif
+#else
+#warning "Current Kernel doesn't open CONFIG_PERF_EVENTS. Function of performance counters is not available."
+#endif
+
+/* Handle KGTP_API_VERSION for the special kernel that include KGTP_API. */
+#ifdef KGTP_API_VERSION
+#define KGTP_API_VERSION_LOCAL KGTP_API_VERSION
+#else
+#define KGTP_API_VERSION_LOCAL 0
+#endif
+
+#ifndef __percpu
+#define __percpu
+#endif
+
+#ifndef this_cpu_ptr
+#define this_cpu_ptr(v) per_cpu_ptr(v, smp_processor_id())
+#endif
+
+#define KERN_NULL
+
+/* check ---------------------------------------------------------- */
+#ifndef CONFIG_KPROBES
+#warning "Cannot trace Linux kernel because Linux Kernel config doesn't open KPROBES. Please open it in 'General setup->Kprobes' if you need it."
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
+#ifndef CONFIG_UPROBES
+#warning "Cannot trace user program because Linux Kernel config doesn't open UPROBES. Please open it in 'Kernel hacking->Tracers->Enable uprobes-based dynamic events' if you need it."
+#endif
+#else
+#warning "Cannot trace user program because the Linux Kernel that older than 3.9 doesn't support UPROBES."
+#endif
+
+#ifdef USE_PROC
+#ifndef CONFIG_PROC_FS
+#error "Linux Kernel doesn't support procfs."
+#endif
+#else
+#ifndef CONFIG_DEBUG_FS
+#error "Linux Kernel doesn't support debugfs."
+#endif
+#endif
+
+#if !defined CONFIG_X86 && !defined CONFIG_MIPS && !defined CONFIG_ARM
+#error "KGTP support X86_32, X86_64, MIPS and ARM."
+#endif
+/* ---------------------------------------------------------------- */
+
+/* Following part is to support old Linux kernel ------------------ */
+#ifndef DEFINE_SEMAPHORE
+#define DEFINE_SEMAPHORE(name) DECLARE_MUTEX(name)
+#endif
+
+#ifndef list_first_entry
+#define list_first_entry(ptr, type, member) \
+ list_entry((ptr)->next, type, member)
+#endif
+
+/* ---------------------------------------------------------------- */
+
+#ifdef GTPDEBUG
+#define GTP_DEBUG KERN_WARNING
+#endif
+
+/* #define GTP_DEBUG_V */
+
+#define GTP_RW_MAX 16384
+#define GTP_RW_BUFP_MAX (GTP_RW_MAX - 4 - gtp_rw_size)
+
+#define FID_TYPE unsigned int
+#define FID_SIZE sizeof(FID_TYPE)
+#define FID(x) (*((FID_TYPE *)x))
+enum {
+ FID_HEAD = 0,
+ FID_REG,
+ FID_MEM,
+ FID_VAR,
+ FID_END,
+ FID_PAGE_BEGIN,
+ FID_PAGE_END,
+};
+
+/* GTP_FRAME_SIZE must align with FRAME_ALIGN_SIZE if use GTP_FRAME_SIMPLE. */
+#define GTP_FRAME_SIZE 5242880
+#if defined(GTP_FRAME_SIMPLE) || defined(GTP_RB)
+#define FRAME_ALIGN_SIZE sizeof(unsigned int)
+#define FRAME_ALIGN(x) ((x + FRAME_ALIGN_SIZE - 1) \
+ & (~(FRAME_ALIGN_SIZE - 1)))
+#endif
+#ifdef GTP_FRAME_SIMPLE
+#define GTP_FRAME_HEAD_SIZE (FID_SIZE + sizeof(char *) + sizeof(ULONGEST))
+#define GTP_FRAME_REG_SIZE (FID_SIZE + sizeof(char *) \
+ + sizeof(struct pt_regs))
+#define GTP_FRAME_MEM_SIZE (FID_SIZE + sizeof(char *) \
+ + sizeof(struct gtp_frame_mem))
+#define GTP_FRAME_VAR_SIZE (FID_SIZE + sizeof(char *) \
+ + sizeof(struct gtp_frame_var))
+#endif
+#ifdef GTP_RB
+/* The frame head size: FID_HEAD + count id + frame number + pointer to prev frem */
+#define GTP_FRAME_HEAD_SIZE (FID_SIZE + sizeof(u64) + sizeof(ULONGEST) + sizeof(void *))
+/* The frame head size: FID_PAGE_BEGIN + count id */
+#define GTP_FRAME_PAGE_BEGIN_SIZE (FID_SIZE + sizeof(u64))
+#endif
+#ifdef GTP_FTRACE_RING_BUFFER
+#define GTP_FRAME_HEAD_SIZE (FID_SIZE + sizeof(ULONGEST))
+#endif
+#if defined(GTP_FTRACE_RING_BUFFER) || defined(GTP_RB)
+#define GTP_FRAME_REG_SIZE (FID_SIZE + sizeof(struct pt_regs))
+#define GTP_FRAME_MEM_SIZE (FID_SIZE + sizeof(struct gtp_frame_mem))
+#define GTP_FRAME_VAR_SIZE (FID_SIZE + sizeof(struct gtp_frame_var))
+#endif
+
+#define INT2CHAR(h) ((h) > 9 ? (h) + 'a' - 10 : (h) + '0')
+
+enum {
+ op_check_add = 0xe0,
+ op_check_sub,
+ op_check_mul,
+ op_check_div_signed,
+ op_check_div_unsigned,
+ op_check_rem_signed,
+ op_check_rem_unsigned,
+ op_check_lsh,
+ op_check_rsh_signed,
+ op_check_rsh_unsigned,
+ op_check_trace,
+ op_check_bit_and,
+ op_check_bit_or,
+ op_check_bit_xor,
+ op_check_equal,
+ op_check_less_signed,
+ op_check_less_unsigned,
+ op_check_pop,
+ op_check_swap,
+ op_check_if_goto,
+ op_check_printf, /* XXX: still not used. */
+
+ op_trace_printk = 0xfd,
+ op_trace_quick_printk,
+ op_tracev_printk,
+};
+
+struct action_agent_exp {
+ unsigned int size;
+ uint8_t *buf;
+ int need_var_lock;
+};
+
+struct action_m {
+ int regnum;
+ CORE_ADDR offset;
+ size_t size;
+};
+
+struct action {
+ struct list_head node;
+ unsigned char type;
+ union {
+ ULONGEST reg_mask;
+ struct action_agent_exp exp;
+ struct action_m m;
+ } u;
+};
+
+struct gtpsrc {
+ struct gtpsrc *next;
+ char *src;
+};
+
+enum gtp_stop_type {
+ gtp_stop_normal = 0,
+ gtp_stop_frame_full,
+ gtp_stop_efault,
+ gtp_stop_access_wrong_reg,
+ gtp_stop_agent_expr_code_error,
+ gtp_stop_agent_expr_stack_overflow,
+};
+
+/* See $current. */
+#define GTP_ENTRY_FLAGS_CURRENT_TASK 1
+/* This gtp entry is registered inside the system. */
+#define GTP_ENTRY_FLAGS_REG 2
+/* See $no_self_trace. */
+#define GTP_ENTRY_FLAGS_SELF_TRACE 4
+/* This gtp entry has passcount. */
+#define GTP_ENTRY_FLAGS_HAVE_PASS 8
+/* See $printk_level. */
+#define GTP_ENTRY_FLAGS_HAVE_PRINTK 16
+/* See $kret. */
+#define GTP_ENTRY_FLAGS_IS_KRETPROBE 32
+
+enum gtp_entry_type {
+ /* Kernel tracepoint that use kprobes.
+ When the gtp_entry is alloced by function gtp_list_add, all
+ of gtp_entry will set to this type. */
+ gtp_entry_kprobe = 0,
+
+ /* User space program tracepoint that use uprobes.
+ When KGTP try to register the gtp_entry, when gtp_entry->pid
+ is not same with gtpd_task->pid (the pid of Kernel), this
+ gtp_entry will set to this type. */
+ gtp_entry_uprobe,
+
+ /* Watch tracepoint.
+ When tracepoint action is checked by function gtp_check_setv.
+ gtp_entry will be set to there types. */
+ gtp_entry_watch_static,
+ gtp_entry_watch_dynamic,
+};
+
+#define GTP_IS_WATCH(tpe) ((tpe)->type == gtp_entry_watch_static \
+ || (tpe)->type == gtp_entry_watch_dynamic)
+
+enum gtp_watch_type {
+ gtp_watch_exec = 0,
+ gtp_watch_write = 1,
+ gtp_watch_read_write = 2,
+};
+
+struct gtp_entry {
+ union gtp_entry_u {
+#ifdef CONFIG_KPROBES
+ /* For gtp_entry_kprobe. */
+ struct gtp_kp {
+ struct kretprobe kpret;
+ struct tasklet_struct stop_tasklet;
+ struct work_struct stop_work;
+ } kp;
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)) && defined CONFIG_UPROBES
+ /* For gtp_entry_uprobe. */
+ struct gtp_up {
+ struct inode *inode;
+ loff_t offset;
+ struct uprobe_consumer uc;
+ } up;
+#endif
+
+ /* For gtp_entry_watch_static and gtp_entry_watch_dynamic. */
+ struct {
+ int type;
+ int size;
+ } watch;
+ } u;
+ unsigned long flags;
+ enum gtp_entry_type type;
+ ULONGEST num;
+ ULONGEST addr;
+
+ /* The actions that set $current help tracepoint get right regs. */
+ struct action *get_regs;
+ /* The actions for the condition. */
+ struct action *cond;
+ struct list_head action_list;
+ int step;
+ struct list_head step_action_list;
+
+ atomic_t current_pass;
+ struct gtpsrc *printk_str;
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
+ /* This is to enable and disable an tracepoint. */
+ struct tasklet_struct enable_tasklet;
+ struct work_struct enable_work;
+ struct tasklet_struct disable_tasklet;
+ struct work_struct disable_work;
+#endif
+ enum gtp_stop_type reason;
+
+ struct gtp_entry *next;
+
+ /* Pid for this tracepoint.
+ KGTP will use this pid to select Kprobe or Uprobe.
+ And use this pid to get the inode for Uprobe if need.
+ In default, this pid will bet set to gtp_current_pid. */
+ pid_t pid;
+
+ int disable;
+ ULONGEST pass;
+ struct gtpsrc *src;
+ /* Sometime, it will not same with action
+ because action will be deleted. */
+ struct gtpsrc *action_cmd;
+};
+
+struct gtp_frame_mem {
+ CORE_ADDR addr;
+ size_t size;
+};
+
+struct gtp_frame_var {
+ unsigned int num;
+ int64_t val;
+};
+
+struct gtpro_entry {
+ struct gtpro_entry *next;
+ CORE_ADDR start;
+ CORE_ADDR end;
+};
+
+static pid_t gtp_gtp_pid;
+static unsigned int gtp_gtp_pid_count;
+static pid_t gtp_gtpframe_pid;
+static unsigned int gtp_gtpframe_pid_count;
+#if defined(GTP_FTRACE_RING_BUFFER) || defined(GTP_RB)
+static pid_t gtp_gtpframe_pipe_pid;
+#endif
+
+static struct gtp_entry *gtp_list;
+static struct gtp_entry *current_gtp;
+static struct gtpsrc *current_gtp_action_cmd;
+static struct gtpsrc *current_gtp_src;
+
+static struct workqueue_struct *gtp_wq;
+
+static int gtp_read_ack;
+static char *gtp_rw_buf;
+static char *gtp_rw_bufp;
+static size_t gtp_rw_size;
+
+static int gtp_start;
+
+static int gtp_disconnected_tracing;
+static int gtp_circular;
+#if defined(GTP_FTRACE_RING_BUFFER) \
+ && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)) \
+ && !defined(GTP_SELF_RING_BUFFER)
+static int gtp_circular_is_changed;
+#endif
+
+static int gtp_cpu_number;
+
+/* Current number in the frame. */
+static int gtp_frame_current_num;
+/* Current tracepoint id. */
+static ULONGEST gtp_frame_current_tpe;
+static atomic_t gtp_frame_create;
+static char *gtp_frame_file;
+static size_t gtp_frame_file_size;
+static DECLARE_WAIT_QUEUE_HEAD(gtpframe_wq);
+#ifdef GTP_FRAME_SIMPLE
+static DEFINE_SPINLOCK(gtp_frame_lock);
+static char *gtp_frame;
+static char *gtp_frame_r_start;
+static char *gtp_frame_w_start;
+static char *gtp_frame_end;
+static int gtp_frame_is_circular;
+static char *gtp_frame_current;
+#endif
+#ifdef GTP_FTRACE_RING_BUFFER
+static struct ring_buffer *gtp_frame;
+static struct ring_buffer_iter *gtp_frame_iter[NR_CPUS];
+static int gtp_frame_current_cpu;
+static u64 gtp_frame_current_clock;
+#endif
+
+#if defined(GTP_FTRACE_RING_BUFFER) || defined(GTP_RB)
+static DECLARE_WAIT_QUEUE_HEAD(gtpframe_pipe_wq);
+static atomic_t gtpframe_pipe_wq_v;
+static struct tasklet_struct gtpframe_pipe_wq_tasklet;
+#endif
+
+static struct gtpro_entry *gtpro_list;
+
+#define GTP_PRINTF_MAX 256
+static DEFINE_PER_CPU(char[GTP_PRINTF_MAX], gtp_printf);
+
+#ifdef CONFIG_X86
+static DEFINE_PER_CPU(u64, rdtsc_current);
+static DEFINE_PER_CPU(u64, rdtsc_offset);
+#endif
+static DEFINE_PER_CPU(u64, local_clock_current);
+static DEFINE_PER_CPU(u64, local_clock_offset);
+
+static uint64_t gtp_start_last_errno;
+static int gtp_start_ignore_error;
+
+static int gtp_pipe_trace;
+
+static int gtp_bt_size;
+
+static int gtp_noack_mode;
+
+static pid_t gtp_current_pid;
+
+/* gtpd_task->pid is used as the pid of Linux kernel. */
+static struct task_struct *gtpd_task;
+
+#ifdef CONFIG_X86
+/* Following part is for while-stepping. */
+struct gtp_step_s {
+ spinlock_t lock;
+ int step;
+ int irq_need_open;
+ struct gtp_entry *tpe;
+};
+static DEFINE_PER_CPU(struct gtp_step_s, gtp_step);
+#endif
+
+#ifdef CONFIG_X86
+static int gtp_have_watch_tracepoint;
+static int gtp_have_step;
+#endif
+
+#ifdef CONFIG_X86
+/* Following part is for watch tracepoint. */
+/* This part is X86 special. */
+#define HWB_NUM 4
+
+static unsigned long gtp_hwb_drx[HWB_NUM];
+static unsigned long gtp_hwb_dr7;
+
+#define GTP_HWB_DR7_DEF (0x400UL)
+#define GTP_HWB_DR6_MASK (0xe00fUL)
+
+/* This part is for all the arch. */
+struct gtp_hwb_s {
+ struct list_head node;
+
+ /* This is the number of this hardware breakpoint. */
+ int num;
+
+ /* This is the address, size and type of this
+ hardware breakpoint. */
+ CORE_ADDR addr;
+ int size;
+ int type;
+
+ /* The previous of the address that this watch tracepoint
+ watch on. */
+ int64_t prev_val;
+
+ /* This is the num and address that setup this hardware
+ breakpoints.
+ For the static watch, this is the num and address of this
+ tracepoint.
+ For the dynamic watch, this is the num and address of
+ the tracepoint that call $watch_start. */
+ ULONGEST trace_num;
+ ULONGEST trace_addr;
+
+ unsigned int count;
+
+ /* Point to the watchpoint struct.
+ If NULL, this hardware breakpoint is not used. */
+ struct gtp_entry *watch;
+};
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0))
+static struct hw_breakpoint {
+ int num;
+ struct perf_event * __percpu *pev;
+} breakinfo[HWB_NUM];
+#endif
+
+static struct gtp_hwb_s gtp_hwb[HWB_NUM];
+
+static LIST_HEAD(gtp_hwb_used_list);
+static LIST_HEAD(gtp_hwb_unused_list);
+
+static DEFINE_RWLOCK(gtp_hwb_lock);
+
+static unsigned int gtp_hwb_sync_count;
+static DEFINE_PER_CPU(unsigned int, gtp_hwb_sync_count_local);
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
+static DEFINE_PER_CPU(struct cpumask, gtp_hwb_sync_cpu_mask);
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25))
+#define gtp_get_debugreg(val, reg) get_debugreg(val, reg)
+#define gtp_set_debugreg(val, reg) set_debugreg(val, reg)
+#else
+#define gtp_get_debugreg(val, reg) \
+ do { \
+ switch(reg) { \
+ case 0: \
+ get_debugreg(val, 0); \
+ break; \
+ case 1: \
+ get_debugreg(val, 1); \
+ break; \
+ case 2: \
+ get_debugreg(val, 2); \
+ break; \
+ case 3: \
+ get_debugreg(val, 3); \
+ break; \
+ } \
+ } while (0)
+
+static void
+gtp_set_debugreg(unsigned long val, int reg)
+{
+ switch(reg) {
+ case 0:
+ gtp_set_debugreg(val, 0);
+ break;
+ case 1:
+ gtp_set_debugreg(val, 1);
+ break;
+ case 2:
+ gtp_set_debugreg(val, 2);
+ break;
+ case 3:
+ gtp_set_debugreg(val, 3);
+ break;
+ }
+}
+#endif
+
+static void
+gtp_hwb_stop(void *data)
+{
+ read_lock(>p_hwb_lock);
+ __get_cpu_var(gtp_hwb_sync_count_local) = gtp_hwb_sync_count;
+ gtp_set_debugreg(0UL, 0);
+ gtp_set_debugreg(0UL, 1);
+ gtp_set_debugreg(0UL, 2);
+ gtp_set_debugreg(0UL, 3);
+ gtp_set_debugreg(GTP_HWB_DR7_DEF, 7);
+ read_unlock(>p_hwb_lock);
+}
+
+static void
+gtp_hwb_sync_local(void)
+{
+ __get_cpu_var(gtp_hwb_sync_count_local) = gtp_hwb_sync_count;
+ gtp_set_debugreg(gtp_hwb_drx[0], 0);
+ gtp_set_debugreg(gtp_hwb_drx[1], 1);
+ gtp_set_debugreg(gtp_hwb_drx[2], 2);
+ gtp_set_debugreg(gtp_hwb_drx[3], 3);
+ gtp_set_debugreg(gtp_hwb_dr7, 7);
+}
+
+static void
+gtp_hwb_sync(void *data)
+{
+ gtp_hwb_sync_local();
+}
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
+static int
+gtp_ipi_handler(struct kprobe *p, struct pt_regs *regs)
+{
+ read_lock(>p_hwb_lock);
+
+ if (__get_cpu_var(gtp_hwb_sync_count_local) != gtp_hwb_sync_count)
+ gtp_hwb_sync_local();
+
+ read_unlock(>p_hwb_lock);
+
+ return 0;
+}
+#endif
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
+static struct kprobe gtp_ipi_kp;
+#endif
+#endif
+
+static char *
+string2hex(char *pkg, char *out)
+{
+ char *ret = out;
+
+ while (pkg[0]) {
+ sprintf(out, "%02x", pkg[0]);
+ pkg++;
+ out += 2;
+ }
+ out[0] = '\0';
+
+ return ret;
+}
+
+/* Strdup begin. If end is not NULL, it point to the end of this dup. */
+
+static char *
+gtp_strdup(char *begin, char *end)
+{
+ int len;
+ char *ret;
+
+ if (end)
+ len = end - begin;
+ else
+ len = strlen(begin);
+
+ ret = kmalloc(len + 1, GFP_KERNEL);
+ if (ret == NULL)
+ return NULL;
+
+ strncpy(ret, begin, len);
+ ret[len] = '\0';
+
+ return ret;
+}
+
+/* Following part is for GTP_LOCAL_CLOCK. */
+
+#define GTP_LOCAL_CLOCK gtp_local_clock()
+#ifdef GTP_CLOCK_CYCLE
+static unsigned long long
+gtp_local_clock(void)
+{
+#ifdef CONFIG_X86
+ unsigned long long a;
+ rdtscll(a);
+ return a;
+#else
+#error "This ARCH cannot get cycle."
+#endif
+}
+#else
+static unsigned long long
+gtp_local_clock(void)
+{
+#ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
+ unsigned long flags;
+ unsigned int cpu;
+
+ local_irq_save(flags);
+ cpu = smp_processor_id();
+ local_irq_restore(flags);
+
+ return cpu_clock(cpu);
+#else
+ return cpu_clock(0);
+#endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */
+}
+#endif
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
+static long
+probe_kernel_read(void *dst, void *src, size_t size)
+{
+ long ret;
+ mm_segment_t old_fs = get_fs();
+
+ set_fs(KERNEL_DS);
+
+ /* pagefault_disable();*/
+ inc_preempt_count();
+ barrier();
+
+ ret = __copy_from_user_inatomic(dst,
+ (__force const void __user *)src, size);
+
+ /* pagefault_enable(); */
+ barrier();
+ dec_preempt_count();
+ barrier();
+ preempt_check_resched();
+
+ set_fs(old_fs);
+
+ return ret ? -EFAULT : 0;
+}
+#endif
+
+#ifdef GTP_RB
+#include "gtp_rb.c"
+#endif
+
+/* Following part is for TSV. */
+
+/* getgtprsp.pl need the ID of TSV. */
+
+enum {
+ GTP_VAR_VERSION_ID = 1,
+ GTP_VAR_CPU_ID = 2,
+ GTP_VAR_CURRENT_TASK_ID = 3,
+ GTP_VAR_CURRENT_THREAD_INFO_ID = 4,
+ GTP_VAR_CLOCK_ID = 5,
+ GTP_VAR_COOKED_CLOCK_ID = 6,
+#ifdef CONFIG_X86
+ GTP_VAR_RDTSC_ID = 7,
+ GTP_VAR_COOKED_RDTSC_ID = 8,
+#endif
+#ifdef GTP_RB
+ GTP_VAR_GTP_RB_DISCARD_PAGE_NUMBER = 9,
+#endif
+ GTP_VAR_PRINTK_TMP_ID = 10,
+ GTP_VAR_PRINTK_LEVEL_ID = 11,
+ GTP_VAR_PRINTK_FORMAT_ID = 12,
+ GTP_VAR_DUMP_STACK_ID = 13,
+ GTP_VAR_SELF_TRACE_ID = 14,
+ GTP_VAR_CPU_NUMBER_ID = 15,
+ GTP_VAR_PC_PE_EN_ID = 16,
+ GTP_VAR_KRET_ID = 17,
+ GTP_VAR_XTIME_SEC_ID = 18,
+ GTP_VAR_XTIME_NSEC_ID = 19,
+ GTP_VAR_IGNORE_ERROR_ID = 20,
+ GTP_VAR_LAST_ERRNO_ID = 21,
+ GTP_VAR_HARDIRQ_COUNT_ID = 22,
+ GTP_VAR_SOFTIRQ_COUNT_ID = 23,
+ GTP_VAR_IRQ_COUNT_ID = 24,
+ GTP_VAR_PIPE_TRACE_ID = 25,
+ GTP_VAR_CURRENT_TASK_PID_ID = 26,
+ GTP_VAR_CURRENT_TASK_USER_ID = 27,
+ GTP_VAR_CURRENT_ID = 28,
+ GTP_VAR_BT_ID = 29,
+
+ GTP_VAR_ENABLE_ID = 30,
+ GTP_VAR_DISABLE_ID = 31,
+
+ GTP_WATCH_STATIC_ID = 32,
+ GTP_WATCH_TYPE_ID = 33,
+ GTP_WATCH_SIZE_ID = 34,
+ GTP_WATCH_SET_ID_ID = 35,
+ GTP_WATCH_SET_ADDR_ID = 36,
+ GTP_WATCH_START_ID = 37,
+ GTP_WATCH_STOP_ID = 38,
+ GTP_WATCH_TRACE_NUM_ID = 39,
+ GTP_WATCH_TRACE_ADDR_ID = 40,
+ GTP_WATCH_ADDR_ID = 41,
+ GTP_WATCH_VAL_ID = 42,
+ GTP_WATCH_PREV_VAL_ID = 46,
+ GTP_WATCH_COUNT_ID = 43,
+
+ GTP_STEP_COUNT_ID = 44,
+ GTP_STEP_ID_ID = 45,
+
+ GTP_INFERIOR_PID_ID = 47,
+
+ GTP_TASK_PT_REGS_ID = 48,
+
+ GTP_VAR_SPECIAL_MIN = GTP_VAR_VERSION_ID,
+ GTP_VAR_SPECIAL_MAX = GTP_TASK_PT_REGS_ID,
+};
+
+enum pe_tv_id {
+ pe_tv_unknown = 0,
+ pe_tv_cpu,
+ pe_tv_type,
+ pe_tv_config,
+ pe_tv_en,
+ pe_tv_val,
+ pe_tv_enabled,
+ pe_tv_running,
+};
+
+enum {
+ gtp_var_normal = 0,
+#ifdef GTP_PERF_EVENTS
+ gtp_var_perf_event,
+ gtp_var_perf_event_per_cpu,
+#endif
+ gtp_var_per_cpu,
+ gtp_var_special,
+};
+
+struct gtp_var;
+
+#ifdef GTP_PERF_EVENTS
+struct gtp_var_perf_event {
+ struct gtp_var_perf_event *pc_next;
+ int en;
+ struct perf_event *event;
+ int cpu;
+ u64 val;
+ u64 enabled; /* The perf inside timer */
+ u64 running; /* The perf inside timer */
+ char *name;
+ struct perf_event_attr attr;
+};
+
+struct gtp_var_pe {
+ enum pe_tv_id ptid;
+ struct gtp_var_perf_event *pe;
+};
+#endif
+
+struct gtp_var_per_cpu {
+ union {
+ int64_t val;
+#ifdef GTP_PERF_EVENTS
+ struct gtp_var_pe pe;
+#endif
+ } u;
+};
+
+struct gtp_var_pc {
+ int cpu;
+ struct gtp_var_per_cpu __percpu *pc;
+};
+
+struct gtp_var {
+ struct list_head node;
+ unsigned int type;
+ unsigned int num;
+ int64_t initial_val;
+ char *src;
+ union {
+ int64_t val;
+ struct gtp_var_pc pc;
+#ifdef GTP_PERF_EVENTS
+ struct gtp_var_pe pe;
+#endif