aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/genksyms
Commit message (Expand)AuthorAge
* blackfin architectureBryan Wu2007-05-07
* kbuild: replace abort() with exit(1)Sam Ravnborg2006-06-24
* kbuild: support for %.symtypes filesAndreas Gruenbacher2006-06-24
* Merge master.kernel.org:/pub/scm/linux/kernel/git/sam/kbuildLinus Torvalds2006-03-25
|\
| * kbuild: clean-up genksymsSam Ravnborg2006-03-12
| * kbuild: Lindent genksyms.cSam Ravnborg2006-03-12
| * kbuild: fix genksyms build errorSam Ravnborg2006-03-12
| * kbuild: Fix bug in crc symbol generating of kernel and modulesLuke Yang2006-03-08
* | [PATCH] add EXPORT_SYMBOL_GPL_FUTURE()Greg Kroah-Hartman2006-03-20
|/
* gitignore: misc filesBrian Gerst2006-01-01
* kbuild: Create _shipped files for genksymsSam Ravnborg2005-12-26
* kbuild: Fix genksyms handling of DEFINE_PER_CPU(struct foo_s *, bar);Robin Holt2005-12-26
* Linux-2.6.12-rc2v2.6.12-rc2Linus Torvalds2005-04-16
18 11:48:36 -0500 trace-graph: Add trace_graph_task_list()' href='/cgit/cgit.cgi/trace-cmd.git/commit/trace-graph.c?id=1c9d1b374d5c592c550b064f036bcb6b32f7a762'>1c9d1b3
b7f1796






a645eb6













84d8f97



cc99a8f


84d8f97



fbcffdc
























7d2d3fd
752cec9
e16a1ed
7d2d3fd
e16a1ed

a05d33d





7d2d3fd
e16a1ed
7d2d3fd
e16a1ed
e16a1ed


7d2d3fd
e16a1ed
7d2d3fd
e16a1ed
a05d33d



















7d2d3fd

e16a1ed
e16a1ed

7d2d3fd
e16a1ed
7d2d3fd
a05d33d
e16a1ed
e16a1ed
0a0bfaa


a96c7ce
0efdbd6
ad813ef
0efdbd6






ad813ef

0efdbd6

a96c7ce
0a0bfaa



0efdbd6
0a0bfaa
a05d33d



0a0bfaa


e16a1ed

7fe507f
b7f1796









a1ad5b4





















































50cc614



a1ad5b4




50cc614



a645eb6
50cc614




a1ad5b4























7fe507f



7fe507f
50cc614
a1ad5b4


7fe507f

b7f1796











5e524c5

b7f1796
5e524c5

b7f1796

a1ad5b4
50cc614
50cc614


a1ad5b4
50cc614

805794b





0a0bfaa










752cec9
e16a1ed
e16a1ed
0a0bfaa

e16a1ed


752cec9
e16a1ed

752cec9






2bd6ed2
7d2d3fd
e16a1ed
752cec9
e16a1ed

7d2d3fd
e16a1ed
7d2d3fd
0a0bfaa
80020d4



2bd6ed2
0a0bfaa
e16a1ed

a05d33d












80020d4



a05d33d



e16a1ed
752cec9
e16a1ed

752cec9



a05d33d







752cec9

0a0bfaa
e16a1ed
7d2d3fd
0a0bfaa
80020d4



0a0bfaa

e16a1ed

752cec9







5863859



6ad565c






5863859
6ad565c
7f6c3bd
3f06bd2
5863859


e0b069a



dba6a8c
e0b069a
dba6a8c
e0b069a

dba6a8c
7f6c3bd
3f06bd2
e0b069a


dba6a8c












e16a1ed






a05d33d
e16a1ed
5863859
e0b069a
e16a1ed
b391d4e
e16a1ed





e16a1ed





















a05d33d







e16a1ed







5863859






e0b069a







e16a1ed













a05d33d

e16a1ed



a645eb6
e16a1ed
dba6a8c

e0b069a
dba6a8c

e0b069a
dba6a8c

e0b069a
e16a1ed
69624fc
a96c7ce
69624fc


e16a1ed





752cec9
e16a1ed







a05d33d








5863859



e16a1ed


a05d33d
5863859
e16a1ed





a05d33d



5863859

e16a1ed









a1ad5b4










a1ad5b4
a1ad5b4
855f4ee





a1ad5b4





b7f1796




aa89353


e16a1ed


5e524c5

b7f1796
50cc614
e16a1ed
50cc614

a1ad5b4

50cc614


a645eb6
50cc614
a1ad5b4
50cc614

a645eb6
50cc614
5e54d49

50cc614


a1ad5b4
b7f1796
5e524c5
b7f1796

805794b










a1ad5b4


805794b

a1ad5b4
805794b












01ccb48




aa89353


01ccb48






a1ad5b4
01ccb48










aa89353


01ccb48





805794b

01ccb48












805794b


01ccb48



805794b
01ccb48






aa89353


01ccb48

805794b
01ccb48



d5b2f5b
b7f1796
a96c7ce


c7840ed
c7840ed




fbcffdc
c7840ed






fbcffdc


c7840ed






fbcffdc


c7840ed






fbcffdc
c7840ed
fbcffdc
c7840ed

fbcffdc
c7840ed




fbcffdc
c7840ed
fbcffdc
c7840ed

fbcffdc
c7840ed







a96c7ce


bedeaf4
bedeaf4
1d43a5a
1c9d1b3
bedeaf4
c748e01
bedeaf4
1c9d1b3





fbcffdc
bedeaf4
c7840ed



fbcffdc
6cbbc27
fbcffdc

c7840ed

bedeaf4

fbcffdc


bedeaf4
bedeaf4


fbcffdc

bedeaf4
fbcffdc
bedeaf4

c748e01
bedeaf4

fbcffdc

bedeaf4
fbcffdc
bedeaf4

c748e01












bedeaf4

463a6c4
bedeaf4

805794b

b7f1796

03ca5aa
b7f1796


fc48712

b7f1796











805794b
b7f1796

d5b2f5b

b7f1796
2f80043

03ca5aa






b7f1796
09f0fff



b7f1796


















d5b2f5b
b7f1796







805794b




































a1ad5b4

805794b


















b7f1796





b7f1796
aa89353


805794b

b7f1796







805794b
b7f1796



e44ef8e
84d8f97
e44ef8e














84d8f97



bd33669







e44ef8e
84d8f97

e44ef8e
84d8f97
bd33669

84d8f97










5e524c5

e44ef8e
5e524c5

84d8f97
5e524c5
84d8f97


5e524c5

2f80043
5e524c5
e44ef8e

84d8f97
2f80043
84d8f97

5e524c5
e44ef8e




5e524c5

84d8f97
5e524c5
5e524c5
cc99a8f
91e8a12
5e524c5
84d8f97











5e524c5

84d8f97

e44ef8e
84d8f97

















e44ef8e
84d8f97
e44ef8e
84d8f97


e44ef8e
84d8f97



e44ef8e
84d8f97
cc99a8f
84d8f97

cc99a8f
84d8f97
2f80043
e44ef8e





2f80043
a645eb6
84d8f97
2f80043

5e524c5
cc99a8f
84d8f97

0a0bfaa


fa243a0
5e524c5
2f80043
84d8f97

cc99a8f
a645eb6
cc99a8f
84d8f97





84d8f97

e44ef8e
84d8f97



5e524c5




fd51f23
5e524c5
fd51f23
84d8f97



5e524c5

2f80043
5e524c5
2f80043

84d8f97

a645eb6
5e524c5
fd51f23
5e524c5

fd51f23
5e524c5
e44ef8e

84d8f97
cc99a8f
5e524c5


cc99a8f
84d8f97






















5e524c5
cc99a8f
84d8f97

0a0bfaa


fa243a0
5e524c5
a645eb6
84d8f97



2f80043
5e524c5
805794b
01ccb48

805794b
01ccb48

805794b
01ccb48








5e524c5
805794b

855f4ee
805794b



a1ad5b4







805794b



855f4ee











805794b


b7f1796




aa89353


805794b
5e524c5
b7f1796


1d38003




aa89353


805794b
01ccb48
1d38003


a96c7ce
b7f1796

b7f1796
a96c7ce


b7f1796



7d359f8


0e861e7


a96c7ce
0e861e7



7d359f8
0e861e7
a96c7ce
0e861e7
7d359f8









0e861e7
7d359f8
0e861e7
a96c7ce









7d359f8
0e861e7


0e861e7

7d359f8







0e861e7
7d359f8
0e861e7

7d359f8

03585d0
a96c7ce
0e861e7


0e861e7
a96c7ce
0e861e7

7d359f8





0e861e7

a96c7ce














d8c1aed

a96c7ce







d8c1aed
a96c7ce



05e14e9

b7f1796
0e861e7

0e861e7
05e14e9
a96c7ce
0e861e7














b7f1796
05e14e9
b7f1796
05e14e9




b7f1796
d8c1aed

05e14e9
b7f1796
05e14e9




bedeaf4
05e14e9
bedeaf4
05e14e9
0e861e7
05e14e9
540cf9c
05e14e9


540cf9c
05e14e9



b7f1796
05e14e9


ae5af61
05e14e9



0e861e7
05e14e9








f2b06ad
05e14e9


0e861e7
05e14e9





0e861e7
05e14e9

0e861e7
05e14e9
0a0bfaa
05e14e9
a96c7ce
0a0bfaa
bdeeb2a


05e14e9







bdeeb2a







05e14e9



0a0bfaa
05e14e9
b7f1796

05e14e9
bdeeb2a







05e14e9



a96c7ce
05e14e9








a96c7ce
540cf9c
05e14e9







b7f1796










5e524c5
b7f1796







5e524c5
b7f1796

















b7f1796
84d8f97
b7f1796






5e524c5
b7f1796



84d8f97
b7f1796






5e524c5
b7f1796

b7f1796
5e524c5
2f80043
b7f1796
5e524c5

a645eb6
b7f1796
5e524c5













b7f1796

84d8f97

b7f1796
aa89353


b7f1796


91e8a12

b7f1796

05e14e9
b7f1796
fbcffdc
b7f1796

bd33669
b7f1796
91a4a56

bd33669




38e4c3f

bd33669
b7f1796
2f80043
91e8a12
bd33669












bd33669
91e8a12
bd33669









2f80043
a645eb6
bd33669





2f80043
bd33669
a645eb6
bd33669
a1ad5b4
bd33669


91a4a56




38e4c3f



91a4a56
91a4a56
38e4c3f
91a4a56

d5b2f5b
91a4a56



38e4c3f

91a4a56

38e4c3f

91a4a56
38e4c3f

bd33669

0efdbd6






0efdbd6



0efdbd6

667fae1

0efdbd6





667fae1
0efdbd6
ad813ef

0efdbd6



d1416b8









































3095089


















bd33669


a1ad5b4
97495e5
b7f1796

84d8f97

bd33669
84d8f97
bd33669


b7f1796


bd33669
b7f1796

bd33669

b7f1796
bd33669
b7f1796
a1ad5b4






b7f1796
a1ad5b4
b7f1796
b7f1796
2f80043


bd33669

5e524c5
bd33669





84d8f97
bd33669
84d8f97

2f80043


7fe507f
b7f1796







aa89353

a05d33d


b7f1796


5863859







aabc0ac




5863859

aabc0ac


d5b2f5b
aabc0ac

aabc0ac
d5b2f5b
aabc0ac
d5b2f5b
aabc0ac
d5b2f5b
aabc0ac


d5b2f5b

aabc0ac


d5b2f5b
aabc0ac


d5b2f5b
aabc0ac



d5b2f5b
aabc0ac
d5b2f5b
aa89353


aabc0ac
5863859

d5b2f5b

aabc0ac

d5b2f5b
aabc0ac
aabc0ac














d5b2f5b
aabc0ac
d5b2f5b
aabc0ac
5863859


aa89353






d5b2f5b
aa89353
aabc0ac













01ccb48











aabc0ac


b7f1796
aa89353
b7f1796
4dca43b
ad813ef
4dca43b

1c9d1b3
a1ad5b4

4dca43b
aa89353






b7f1796


aa89353


4dca43b
d092a07
b7f1796

bb8ee16
aa89353
fbcffdc

b7f1796

0efdbd6

ad813ef

b7f1796


d5b2f5b


aa89353

























a1ad5b4







aa89353


df49e6a

d56f0e3

df49e6a



aa89353






d5b2f5b
aa89353









a1ad5b4


8ed155d

0c4b06b
aa89353










7d2d3fd
a05d33d
bdd0561
a1ad5b4
d4c9337

a1ad5b4








8ed155d



a1ad5b4






8ed155d




a1ad5b4









8ed155d




a1ad5b4





0c4b06b


















a1ad5b4

0c4b06b

a1ad5b4
8ed155d




a1ad5b4





0c4b06b





















a1ad5b4


8ed155d




a1ad5b4










8ed155d




a1ad5b4







d4c9337




aa89353
aabc0ac







a1ad5b4
aabc0ac






a1ad5b4
d4c9337
2f80043
84d8f97

b7f1796











1d38003

b7f1796







1d38003

b7f1796
d4c9337



2105698
b7f1796
5e54d49

d4c9337
5e54d49
d4c9337
5e54d49
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
  
                                                                             






















                                                                             

                    
                         


                        
                       
                         
 

                 
                         






                                                 
                                                                  

      
                             
 


                                 


                                                                    



                                                        
                                                           
                                         

                         
                               
 


                    
                                                            
                                                          
 




















































































                                                                                  






                                                                     













                                                                         



                                               


                         



                                        
























                                                            
                         
                                                                    
 
                                                             

 





                                                                  
                                                                         
 
                                                     
 


                                    
                                                                            
 
                                                        
 



















                                                                          

                                            
         

 
                                                             
 
                                              
                                             
 
                                    


                                  
                                                                                     
 
                






                              

                                                               

 
                                                                       



                        
 
                                    



                                                               


                              

 
                                                           









                                                                                   





















































                                                              



                                                 




                             



                                                     
                                                    




                                                                         























                                                              



                                                         
                                                          
 


                           

 











                                                                     

                                                                 
 

                                                              

 
                                                        
 


                    
                                                                              

 





                                                                          










                                                        
                                                        
 
                                   

                            


           
                                     

                                        






                                                                 
                                                    
                                      
 
                                                            

                 
                                                               
            
                                                      
 



                                                                   
                           
                                    

 












                                                                



                                                                   



                                    
           
                                       

                                        



                                                                               







                                                                              

                                                      
                                                    
 
                                       
 



                                                                   

                                    

 







                                          



                                        






                                                      
 
                                                                 
                                                      
                                                                                        


                                   



                                        
                                                      
 
                  

                       
                                             
                                                      
                                                                                        


                                   












                                                                          






                                                                  
                                                
                                                  
                                         
                                           
                                     
                                





                         





















                                                                                      







                                                                                       







                                                                                            






                                                                                







                                                                                  













                                                                          

                                                    



                                                                         
                                            
 

                                        
 

                                                                         
                                                                 

                                                                  
 
                     
 
                                                                                  


                                                                             





                                        
                                                                 







                                                                                 








                                                                               



                                                                      


                                                                     
                                                                      
                                                





                                                                            



                                                                             

                                                









                                                                 










                                                                       
                                          
                                                                               





                                                                                       





                                   




                                                                           


                             


                                                      

                               
 
                                    
                                               

                                                   

                                                          


                                                              
                                                                                
                                          
                                                         

                 
                                                                   
                                   

                                                                       


                            
                                                    
 
                    

 










                                                                             


                                         

                                  
                                                         












                                                              




                                                                                


                             






                                             
                                                                                 










                                                                                 


                             





                                

                              












                                                                                  


                                                   



                     
                                                             






                                                                                  


                             

                                                                                            
                                 



                     
                      
 


                                                            
 




                                   
                                         






                                                                           


                                                                                          






                                                                               


                                                                                              






                                                     
                                           
                                                                       
                                                                                          

                                 
                                                                                      




                                   
                                               
                                                                       
                                                                                              

                                 
                                                                                          







                                   


                                                                
 
                               
                                   
                      
                
                    
 





                                                                  
                                               
                                                                



                                                                           
                                                         
                                                                                 

                                                                                

                                                                

                                                                              


                                                                                         
                 


                                                     

                                                                                     
                         
                                                                               

                                   
                         

         

                                                                                      
                         
                                                                                

                                   












                                                                   

         
                   

 

                                                                        

                            
                            


                             

                        











                                                                              
                                                                     

                                                             

                                   
 

                                                               






                                                                                          
 



                                    


















                                                          
                                                            







                                                                                   




































                                                                             

                               


















                                                                                





                                                                            
 


                             

                              







                                                                      
                                 



                    
                                                                  
 














                                                                                



                                                             







                                                           
                                                                              

                                  
                                                                   
                                       

                                                            










                                                                     

                                                                          
                           

                           
                             
                           


                                           

                              
                              
 

                                                             
 
                                                               

                                            
 




                                                                

                                                   
                                         
 
                                      
                                                           
                                                     
 











                                                                     

                                              

                                                
                                      

















                                                                     
 
                                    
                                         


                                                                 
                                                           



                                                    
                                           
 
                                                                       

                                                              
                                 
 
                                                                  





                                                           
                                  
                                                                 
 

                                                                   
 
                                                        

                                         


                                           
                                                                                                
 
                                                      

 
                                           
                                                                
                         





                                               

                                                    
                                  



                                              




                                                                           
                        
                           
                          



                                           

                              
                              
 

                                                               

                                       
                                             
 
                              

                                                   
                                         
 

                                                 
 
                                                                


                                      
                                                                                          






















                                                                        
 
                                                        

                                         


                                           
                                                                                                
 
                                             



                                       
                                    
 
 

                                                           
                         

                       
                            








                                                          
 

                                                            
                   



                                







                                          



                                   











                                                                                 


                                




                                                                             


                             
                                        
 


                    




                                                                             


                             
                              
 


                     
                                                           

                       
 


                                                     



                                                                 


                                                              


                                                                      
                                                       



                            
                        
                  
                 
 









                                                            
 
                                       
                           









                                                                                  
                          


                                                                       

                                                                       







                                                
                                       
                          

         

                              
 
                                            


                                                                         
                                                                       
                                                                

                               





                                                                       

 














                                                              

                                                   







                                                  
                                



                                                   

                                                                        
 

                                          
                             
                              
               














                                                                             
 
                                                           
 




                                                                           
 

                                                                       
         
 




                                                                           
 
                                                                           
 
                                                                                
 
                             
 


                                                                      
 



                                                                                                  
 


                                    
 



                                                                                        
 








                                                                
 


                                            
 





                                                                   
 

                                                                               
 
                                                                            
 
                                                                   
         
 


                                                                   







                                                                                   







                                                                          



                                                                                 
                 
                         

         
                                                                         







                                                          



                                                                         
                 








                                                                         
         
 







                                                  










                                                               
                        







                                                                          
                        

















                                                                         
                                        
                                                          






                                                                         
                                              



                                      
                                                        






                                                                         
                                                            

                               
 
                                            
                                                               
 

                                                                       
                                                     
 













                                                                                 

 

                                               
 


                           


                                                                                   

                                                                                      

                                        
                                     
 
                                  

 
                                                                       
 

                            




                        

                
                                                        
 
                                                               
                                                        












                                                                
                                     
                                        









                                                     
                                                          
                                             





                                           
                                                     
 
                                                                
                          
                                         


                                                                    




                                                                 



                                                                              
                              
         
                              

                       
                                       



                                                                                          

                                                     

                       

                                                                
 

                                                                                   

 






                                                           



                       

                                         

                                                         





                                    
                                                                              
 

                                                                



                            









































                                                                                   


















                                                               


                                                           
                             
 

                                     

                                             
                                                                  
 


                                                                           


                                                
                                                        

                                

                                                           
 
                                                        
 






                                                          
 
                    
                                        
 


                                                                         

         
 





                                                                                   
 
                                     

                   


                                                                  
 







                                               

                                     


                                                  


                    







                                                                                   




                                                                          

                                                                   


                     
                                                                  

                            
                           
                    
 
                                            
 
                                                                    


                                                             

                                           


                                                        
                                                   


                                                     
                                                     



                                
                                                           
 
               


                           
 

                               

                                               

 
                                                         
 














                                                                           
                                     
 
                                                                         
                                                        


                                                                       






                                                                             
                                   
 













                                                                        











                                                                             


                    
 
                                                    
 
                            
                                                        

                                              
                                      

                                  
         






                                                     


                                


                          
                                     
                                     

                               
                             
 

                                

                                                    

                                 

                                                                 


                                  


                                                      

























                                                              







                                 


                 

                                                  

                                                                                        



                                   






                                                          
                                   









                                                                


                         

                            
                         










                                           
                                                      
                                                     
 
                                               

                                       








                                                                                        



                                          






                                                                                            




                                                                        









                                                                                            




                                                                        





                                                       


















                                                                     

                               

                                                                                            
                                                  




                                                                       





                                                       





















                                                                                             


                                                  




                                                                       










                                                                                              




                                                                       







                                                                        




                                                                             
                                                                                                 







                                                                                            
                                                                                  






                                                                                         
                                                                            
 
                                                                    

                                                                 











                                                                           

                                                                         







                                                                 

                                                            
 



                                                                                    
                     
 

                   
                                                 
 
                                                               
 
/*
 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * 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; version 2 of the License (not later!)
 *
 * 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
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <gtk/gtk.h>

#include "trace-compat.h"
#include "trace-cmd.h"
#include "trace-local.h"
#include "trace-graph.h"
#include "trace-hash.h"
#include "trace-filter.h"

#include "util.h"

#define DEBUG_LEVEL	0
#if DEBUG_LEVEL > 0
# define dprintf(l, x...)			\
	do {					\
		if (l <= DEBUG_LEVEL)		\
			printf(x);		\
	} while (0)
#else
# define dprintf(l, x...)	do { if (0) printf(x); } while (0)
#endif

#define MAX_WIDTH	10000

#define PLOT_SIZE	10
#define PLOT_BOX_SIZE	PLOT_SIZE
#define PLOT_GIVE	2
#define PLOT_BEGIN	80
#define PLOT_SEP	50
#define PLOT_LINE(plot) (PLOT_SEP * (plot) + PLOT_BEGIN + PLOT_SIZE)
#define PLOT_TOP(plot) (PLOT_LINE(plot) - PLOT_SIZE * 2)
#define PLOT_BOX_TOP(plot) (PLOT_LINE(plot) - PLOT_SIZE)
#define PLOT_BOTTOM(plot) (PLOT_LINE(plot)-1)
#define PLOT_BOX_BOTTOM(plot) (PLOT_LINE(plot))
#define PLOT_SPACE(plots) (PLOT_SEP * (plots) + PLOT_BEGIN)
#define PLOT_LABEL(plot) (PLOT_TOP(plot))
#define PLOT_X		5

static gint largest_plot_label;

static GdkGC *green;
static GdkGC *red;

static void redraw_pixmap_backend(struct graph_info *ginfo);
static void update_label_window(struct graph_info *ginfo);

struct task_list {
	struct task_list	*next;
	gint			pid;
};

static guint get_task_hash_key(gint pid)
{
	return trace_hash(pid) % TASK_HASH_SIZE;
}

static struct task_list *find_task_hash(struct graph_info *ginfo,
					gint key, gint pid)
{
	struct task_list *list;

	for (list = ginfo->tasks[key]; list; list = list->next) {
		if (list->pid == pid)
			return list;
	}

	return NULL;
}

static struct task_list *add_task_hash(struct graph_info *ginfo,
				       int pid)
{
	struct task_list *list;
	guint key = get_task_hash_key(pid);

	list = find_task_hash(ginfo, key, pid);
	if (list)
		return list;

	list = malloc_or_die(sizeof(*list));
	list->pid = pid;
	list->next = ginfo->tasks[key];
	ginfo->tasks[key] = list;

	return list;
}

static void free_task_hash(struct graph_info *ginfo)
{
	struct task_list *list;
	int i;

	for (i = 0; i < TASK_HASH_SIZE; i++) {
		while (ginfo->tasks[i]) {
			list = ginfo->tasks[i];
			ginfo->tasks[i] = list->next;
			free(list);
		}
	}
}

/**
 * trace_graph_task_list - return an allocated list of all found tasks
 * @ginfo: The graph info structure
 *
 * Returns an allocated list of pids found in the graph, ending
 * with a -1. This array must be freed with free().
 */
gint *trace_graph_task_list(struct graph_info *ginfo)
{
	struct task_list *list;
	gint *pids;
	gint count = 0;
	gint i;

	for (i = 0; i < TASK_HASH_SIZE; i++) {
		list = ginfo->tasks[i];
		while (list) {
			if (count)
				pids = realloc(pids, sizeof(*pids) * (count + 2));
			else
				pids = malloc(sizeof(*pids) * 2);
			pids[count++] = list->pid;
			pids[count] = -1;
			list = list->next;
		}
	}

	return pids;
}

static void convert_nano(unsigned long long time, unsigned long *sec,
			 unsigned long *usec)
{
	*sec = time / 1000000000ULL;
	*usec = (time / 1000) % 1000000;
}

static int convert_time_to_x(struct graph_info *ginfo, guint64 time)
{
	if (time < ginfo->view_start_time)
		return 0;
	return (time - ginfo->view_start_time) * ginfo->resolution;
}

static guint64 convert_x_to_time(struct graph_info *ginfo, gint x)
{
	double d = x;

	return (guint64)(d / ginfo->resolution) + ginfo->view_start_time;
}

static void print_time(unsigned long long time)
{
	unsigned long sec, usec;

	if (!DEBUG_LEVEL)
		return;

	convert_nano(time, &sec, &usec);
	printf("%lu.%06lu", sec, usec);
}

static void init_event_cache(struct graph_info *ginfo)
{
	ginfo->ftrace_sched_switch_id = -1;
	ginfo->event_sched_switch_id = -1;
	ginfo->event_wakeup_id = -1;
	ginfo->event_wakeup_new_id = -1;

	ginfo->event_pid_field = NULL;
	ginfo->event_comm_field = NULL;
	ginfo->ftrace_pid_field = NULL;
	ginfo->ftrace_comm_field = NULL;

	ginfo->wakeup_pid_field = NULL;
	ginfo->wakeup_success_field = NULL;
	ginfo->wakeup_new_pid_field = NULL;
	ginfo->wakeup_new_success_field = NULL;

	/*
	 * The first time reading the through the list
	 * test the sched_switch for comms that did not make
	 * it into the pevent command line list.
	 */
	ginfo->read_comms = TRUE;
}

struct filter_task_item *
trace_graph_filter_task_find_pid(struct graph_info *ginfo, gint pid)
{
	return filter_task_find_pid(ginfo->task_filter, pid);
}

struct filter_task_item *
trace_graph_hide_task_find_pid(struct graph_info *ginfo, gint pid)
{
	return filter_task_find_pid(ginfo->hide_tasks, pid);
}

static void graph_filter_task_add_pid(struct graph_info *ginfo, gint pid)
{
	filter_task_add_pid(ginfo->task_filter, pid);

	ginfo->filter_available = 1;
}

static void graph_filter_task_remove_pid(struct graph_info *ginfo, gint pid)
{
	filter_task_remove_pid(ginfo->task_filter, pid);

	if (!filter_task_count(ginfo->task_filter) &&
	    !filter_task_count(ginfo->hide_tasks)) {
		ginfo->filter_available = 0;
		ginfo->filter_enabled = 0;
	}
}

static void graph_hide_task_add_pid(struct graph_info *ginfo, gint pid)
{
	filter_task_add_pid(ginfo->hide_tasks, pid);

	ginfo->filter_available = 1;
}

static void graph_hide_task_remove_pid(struct graph_info *ginfo, gint pid)
{
	filter_task_remove_pid(ginfo->hide_tasks, pid);

	if (!filter_task_count(ginfo->task_filter) &&
	    !filter_task_count(ginfo->hide_tasks)) {
		ginfo->filter_available = 0;
		ginfo->filter_enabled = 0;
	}
}

static void graph_filter_task_clear(struct graph_info *ginfo)
{
	filter_task_clear(ginfo->task_filter);
	filter_task_clear(ginfo->hide_tasks);

	ginfo->filter_available = 0;
	ginfo->filter_enabled = 0;
}

gboolean trace_graph_filter_on_event(struct graph_info *ginfo, struct record *record)
{
	int ret;

	if (!record)
		return TRUE;

	if (ginfo->all_events)
		return FALSE;

	ret = pevent_filter_match(ginfo->event_filter, record);
	return ret == FILTER_MATCH ? FALSE : TRUE;
}

gboolean trace_graph_filter_on_task(struct graph_info *ginfo, gint pid)
{
	gboolean filter;

	filter = FALSE;

	if (ginfo->filter_enabled &&
	    ((filter_task_count(ginfo->task_filter) &&
	      !trace_graph_filter_task_find_pid(ginfo, pid)) ||
	     (filter_task_count(ginfo->hide_tasks) &&
	      trace_graph_hide_task_find_pid(ginfo, pid))))
		filter = TRUE;

	return filter;
}

static void __update_with_backend(struct graph_info *ginfo,
				gint x, gint y,
				gint width, gint height)
{
	gdk_draw_drawable(ginfo->draw->window,
			  ginfo->draw->style->fg_gc[GTK_WIDGET_STATE(ginfo->draw)],
			  ginfo->curr_pixmap,
			  x, y, x, y,
			  width, height);
}

static void update_label_time(GtkWidget *label, gint64 time)
{
	unsigned long sec, usec;
	struct trace_seq s;
	char *min = "";

	if (time < 0) {
		time *= -1;
		min = "-";
	}

	convert_nano(time, &sec, &usec);

	trace_seq_init(&s);
	trace_seq_printf(&s, "%s%lu.%06lu", min, sec, usec);

	gtk_label_set_text(GTK_LABEL(label), s.buffer);
}

static void update_cursor(struct graph_info *ginfo)
{
	update_label_time(ginfo->cursor_label, ginfo->cursor);
}

static void update_pointer(struct graph_info *ginfo, gint x)
{
	guint64 time;

	time = convert_x_to_time(ginfo, x);
	update_label_time(ginfo->pointer_time, time);
}

static void update_marka(struct graph_info *ginfo, gint x)
{
	guint64 timeA;

	timeA = convert_x_to_time(ginfo, x);
	ginfo->marka_time = timeA;

	update_label_time(ginfo->marka_label, timeA);
}

static void update_markb(struct graph_info *ginfo, guint x)
{
	gint64 timeA, timeB;

	timeA = ginfo->marka_time;
	timeB = convert_x_to_time(ginfo, x);
	ginfo->markb_time = timeB;

	update_label_time(ginfo->markb_label, timeB);
	update_label_time(ginfo->delta_label, timeB - timeA);
}

static void draw_cursor(struct graph_info *ginfo)
{
	gint x;

	if (!ginfo->cursor)
		return;

	update_cursor(ginfo);

	if (ginfo->cursor < ginfo->view_start_time ||
	    ginfo->cursor > ginfo->view_end_time)
		return;

	x = convert_time_to_x(ginfo, ginfo->cursor);

	gdk_draw_line(ginfo->draw->window, ginfo->draw->style->mid_gc[3],
		      x, 0, x, ginfo->draw->allocation.width);
}

static void draw_marka(struct graph_info *ginfo)
{
	gint x;

	if (!ginfo->show_marka)
		return;

	x = convert_time_to_x(ginfo, ginfo->marka_time);
	gdk_draw_line(ginfo->draw->window, green,
		      x, 0, x, ginfo->draw->allocation.width);
}

static void draw_markb(struct graph_info *ginfo)
{
	gint x;

	if (!ginfo->show_markb)
		return;

	x = convert_time_to_x(ginfo, ginfo->markb_time);
	gdk_draw_line(ginfo->draw->window, red,
		      x, 0, x, ginfo->draw->allocation.width);
}

static void update_with_backend(struct graph_info *ginfo,
				gint x, gint y,
				gint width, gint height)
{
	__update_with_backend(ginfo, x, y, width, height);

	draw_cursor(ginfo);
	draw_markb(ginfo);
	draw_marka(ginfo);
}

static gboolean
expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
	struct graph_info *ginfo = data;

	update_with_backend(ginfo,
			    event->area.x, event->area.y,
			    event->area.width, event->area.height);

	return FALSE;
}

static void
draw_line(GtkWidget *widget, gdouble x, struct graph_info *ginfo)
{
	gdk_draw_line(widget->window, widget->style->black_gc,
		      x, 0, x, widget->allocation.width);
}

static void clear_line(struct graph_info *ginfo, gint x)
{
	if (x)
		x--;

	update_with_backend(ginfo, x, 0, x+2, ginfo->draw->allocation.height);
}

static void clear_info_box(struct graph_info *ginfo)
{
	update_with_backend(ginfo, ginfo->plot_data_x, ginfo->plot_data_y,
			    ginfo->plot_data_w, ginfo->plot_data_h);
}

static void redraw_graph(struct graph_info *ginfo)
{
	gdouble height;
	gdouble width;

	redraw_pixmap_backend(ginfo);
	width = ginfo->draw->allocation.width;
	height = ginfo->draw->allocation.height;
	update_with_backend(ginfo, 0, 0, width, height);
}

void trace_graph_filter_toggle(struct graph_info *ginfo)
{
	ginfo->filter_enabled ^= 1;

	redraw_graph(ginfo);
}

static void
filter_enable_clicked (gpointer data)
{
	struct graph_info *ginfo = data;

	trace_graph_filter_toggle(ginfo);
}

void trace_graph_filter_add_remove_task(struct graph_info *ginfo,
					gint pid)
{
	gint filter_enabled = ginfo->filter_enabled;
	struct filter_task_item *task;

	task = trace_graph_filter_task_find_pid(ginfo, pid);

	if (task)
		graph_filter_task_remove_pid(ginfo, task->pid);
	else
		graph_filter_task_add_pid(ginfo, pid);

	if (ginfo->callbacks && ginfo->callbacks->filter)
		ginfo->callbacks->filter(ginfo, ginfo->task_filter,
					 ginfo->hide_tasks);

	if (filter_enabled)
		redraw_graph(ginfo);
}

void trace_graph_filter_hide_show_task(struct graph_info *ginfo,
				       gint pid)
{
	gint filter_enabled = ginfo->filter_enabled;
	struct filter_task_item *task;

	task = trace_graph_hide_task_find_pid(ginfo, pid);

	if (task)
		graph_hide_task_remove_pid(ginfo, task->pid);
	else
		graph_hide_task_add_pid(ginfo, pid);

	if (ginfo->callbacks && ginfo->callbacks->filter)
		ginfo->callbacks->filter(ginfo, ginfo->task_filter,
					 ginfo->hide_tasks);

	if (filter_enabled)
		redraw_graph(ginfo);
}

static void
filter_add_task_clicked (gpointer data)
{
	struct graph_info *ginfo = data;

	trace_graph_filter_add_remove_task(ginfo, ginfo->filter_task_selected);
}

static void
filter_hide_task_clicked (gpointer data)
{
	struct graph_info *ginfo = data;

	trace_graph_filter_hide_show_task(ginfo, ginfo->filter_task_selected);
}

void trace_graph_clear_tasks(struct graph_info *ginfo)
{
	gint filter_enabled = ginfo->filter_enabled;

	graph_filter_task_clear(ginfo);

	if (ginfo->callbacks && ginfo->callbacks->filter)
		ginfo->callbacks->filter(ginfo, ginfo->task_filter,
					 ginfo->hide_tasks);

	if (filter_enabled)
		redraw_graph(ginfo);
}

static void
filter_clear_tasks_clicked (gpointer data)
{
	struct graph_info *ginfo = data;

	trace_graph_clear_tasks(ginfo);
}

static void
plot_task_clicked (gpointer data)
{
	struct graph_info *ginfo = data;
	struct graph_plot *plot = ginfo->plot_clicked;
	int pos;

	if (plot)
		pos = plot->pos + 1;
	else
		pos = ginfo->plots + 1;

	graph_plot_task(ginfo, ginfo->filter_task_selected, pos);
	ginfo->draw_height = PLOT_SPACE(ginfo->plots);
	gtk_widget_set_size_request(ginfo->draw, ginfo->draw_width, ginfo->draw_height);
	update_label_window(ginfo);
}

static void
remove_plot_clicked (gpointer data)
{
	struct graph_info *ginfo = data;
	struct graph_plot *plot = ginfo->plot_clicked;

	if (!plot)
		return;

	trace_graph_plot_remove(ginfo, plot);
	ginfo->draw_height = PLOT_SPACE(ginfo->plots);
	gtk_widget_set_size_request(ginfo->draw, ginfo->draw_width, ginfo->draw_height);
	update_label_window(ginfo);
}

static struct graph_plot *find_plot_by_y(struct graph_info *ginfo, gint y)
{
	gint i;

	for (i = 0; i < ginfo->plots; i++) {
		if (y >= (PLOT_TOP(i) - PLOT_GIVE) &&
		    y <= (PLOT_BOTTOM(i) + PLOT_GIVE)) {
			return ginfo->plot_array[i];
		}
	}
	return NULL;
}

static gboolean
do_pop_up(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	struct graph_info *ginfo = data;
	static GtkWidget *menu;
	static GtkWidget *menu_filter_enable;
	static GtkWidget *menu_filter_add_task;
	static GtkWidget *menu_filter_hide_task;
	static GtkWidget *menu_filter_clear_tasks;
	static GtkWidget *menu_plot_task;
	static GtkWidget *menu_remove_plot;
	struct record *record = NULL;
	struct graph_plot *plot;
	const char *comm;
	guint64 time;
	gchar *text;
	gint pid;
	gint len;
	gint x, y;

	x = event->x;
	y = event->y;

	if (!menu) {
		menu = gtk_menu_new();
		menu_filter_enable = gtk_menu_item_new_with_label("Enable Filter");
		gtk_widget_show(menu_filter_enable);
		gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_filter_enable);

		g_signal_connect_swapped (G_OBJECT (menu_filter_enable), "activate",
					  G_CALLBACK (filter_enable_clicked),
					  data);

		menu_filter_add_task = gtk_menu_item_new_with_label("Add Task");
		gtk_widget_show(menu_filter_add_task);
		gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_filter_add_task);

		g_signal_connect_swapped (G_OBJECT (menu_filter_add_task), "activate",
					  G_CALLBACK (filter_add_task_clicked),
					  data);

		menu_filter_hide_task = gtk_menu_item_new_with_label("Hide Task");
		gtk_widget_show(menu_filter_hide_task);
		gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_filter_hide_task);

		g_signal_connect_swapped (G_OBJECT (menu_filter_hide_task), "activate",
					  G_CALLBACK (filter_hide_task_clicked),
					  data);

		menu_filter_clear_tasks = gtk_menu_item_new_with_label("Clear Task Filter");
		gtk_widget_show(menu_filter_clear_tasks);
		gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_filter_clear_tasks);

		g_signal_connect_swapped (G_OBJECT (menu_filter_clear_tasks), "activate",
					  G_CALLBACK (filter_clear_tasks_clicked),
					  data);

		menu_plot_task = gtk_menu_item_new_with_label("Plot task");
		gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_plot_task);

		g_signal_connect_swapped (G_OBJECT (menu_plot_task), "activate",
					  G_CALLBACK (plot_task_clicked),
					  data);

		menu_remove_plot = gtk_menu_item_new_with_label("Remove Plot");
		gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_remove_plot);
		gtk_widget_show(menu_remove_plot);

		g_signal_connect_swapped (G_OBJECT (menu_remove_plot), "activate",
					  G_CALLBACK (remove_plot_clicked),
					  data);

	}

	if (ginfo->filter_enabled)
		gtk_menu_item_set_label(GTK_MENU_ITEM(menu_filter_enable),
					"Disable Filter");
	else
		gtk_menu_item_set_label(GTK_MENU_ITEM(menu_filter_enable),
					"Enable Filter");

	if (ginfo->filter_available)
		gtk_widget_set_sensitive(menu_filter_enable, TRUE);
	else
		gtk_widget_set_sensitive(menu_filter_enable, FALSE);

	if (filter_task_count(ginfo->task_filter) ||
	    filter_task_count(ginfo->hide_tasks))
		gtk_widget_set_sensitive(menu_filter_clear_tasks, TRUE);
	else
		gtk_widget_set_sensitive(menu_filter_clear_tasks, FALSE);

	time =  convert_x_to_time(ginfo, x);

	plot = find_plot_by_y(ginfo, y);
	ginfo->plot_clicked = plot;

	if (plot) {
		record = trace_graph_plot_find_record(ginfo, plot, time);
		gtk_widget_set_sensitive(menu_remove_plot, TRUE);
	} else
		gtk_widget_set_sensitive(menu_remove_plot, FALSE);

	if (record) {

		if (!trace_graph_check_sched_switch(ginfo, record, &pid, &comm)) {
			pid = pevent_data_pid(ginfo->pevent, record);
			comm = pevent_data_comm_from_pid(ginfo->pevent, pid);
		}

		len = strlen(comm) + 50;

		text = g_malloc(len);
		g_assert(text);

		if (trace_graph_filter_task_find_pid(ginfo, pid))
			snprintf(text, len, "Remove %s-%d to filter", comm, pid);
		else
			snprintf(text, len, "Add %s-%d to filter", comm, pid);

		ginfo->filter_task_selected = pid;

		gtk_menu_item_set_label(GTK_MENU_ITEM(menu_filter_add_task),
					text);

		if (trace_graph_hide_task_find_pid(ginfo, pid))
			snprintf(text, len, "Show %s-%d to filter", comm, pid);
		else
			snprintf(text, len, "Hide %s-%d to filter", comm, pid);

		gtk_menu_item_set_label(GTK_MENU_ITEM(menu_filter_hide_task),
					text);

		snprintf(text, len, "Plot %s-%d", comm, pid);
		gtk_menu_item_set_label(GTK_MENU_ITEM(menu_plot_task),
					text);

		g_free(text);

		gtk_widget_set_sensitive(menu_filter_add_task, TRUE);
		gtk_widget_set_sensitive(menu_filter_hide_task, TRUE);
		gtk_widget_show(menu_plot_task);

		free_record(record);
	} else {
		gtk_menu_item_set_label(GTK_MENU_ITEM(menu_filter_add_task),
					"Add task to filter");
		gtk_widget_set_sensitive(menu_filter_add_task, FALSE);

		gtk_menu_item_set_label(GTK_MENU_ITEM(menu_filter_hide_task),
					"Hide task to filter");
		gtk_widget_set_sensitive(menu_filter_hide_task, FALSE);

		gtk_widget_hide(menu_plot_task);
	}

		
	gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3,
		       gtk_get_current_event_time());


	return TRUE;
}

static void button_press(struct graph_info *ginfo, gint x, guint state)
{
	ginfo->press_x = x;
	ginfo->last_x = 0;

	draw_line(ginfo->draw, x, ginfo);

	ginfo->line_active = TRUE;
	ginfo->line_time = convert_x_to_time(ginfo, x);

	if (state & GDK_SHIFT_MASK) {
		ginfo->show_markb = FALSE;
		clear_line(ginfo, convert_time_to_x(ginfo, ginfo->markb_time));
		/* We only update A if it hasn't been made yet */
		if (!ginfo->marka_time) {
			ginfo->show_marka = FALSE;
			clear_line(ginfo, convert_time_to_x(ginfo, ginfo->marka_time));
			update_marka(ginfo, x);
		}
	} else
		ginfo->zoom = TRUE;

	return;
}

static gboolean
button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	struct graph_info *ginfo = data;

	if (!ginfo->handle)
		return FALSE;

	if (event->button == 3)
		return do_pop_up(widget, event, data);

	if (event->button != 1)
		return TRUE;

	/* check for double click */
	if (event->type == GDK_2BUTTON_PRESS) {
		if (ginfo->line_active) {
			ginfo->line_active = FALSE;
			clear_line(ginfo, ginfo->last_x);
			clear_line(ginfo, ginfo->press_x);
		}
		if (ginfo->cursor >= ginfo->view_start_time &&
		    ginfo->cursor <= ginfo->view_end_time) {
			ginfo->last_x = convert_time_to_x(ginfo, ginfo->cursor);
			ginfo->cursor = 0;
			clear_line(ginfo, ginfo->last_x);
		}

		ginfo->cursor = convert_x_to_time(ginfo, event->x);
		draw_cursor(ginfo);
		if (ginfo->callbacks && ginfo->callbacks->select)
			ginfo->callbacks->select(ginfo, ginfo->cursor);
		return TRUE;
	}

	button_press(ginfo, event->x, event->state);

	return TRUE;
}

static void draw_latency(struct graph_info *ginfo, gint x, gint y);
static void draw_plot_info(struct graph_info *ginfo, struct graph_plot *plot,
			   gint x, gint y);

static void motion_plot(struct graph_info *ginfo, gint x, gint y)
{
	struct graph_plot *plot;

	if (!ginfo->curr_pixmap)
		return;

	if (ginfo->pointer_time)
		update_pointer(ginfo, x);

	if (ginfo->line_active) {
		if (ginfo->last_x)
			clear_line(ginfo, ginfo->last_x);
		ginfo->last_x = x;
		draw_line(ginfo->draw, ginfo->press_x, ginfo);
		draw_line(ginfo->draw, x, ginfo);
		if (!ginfo->zoom)
			draw_latency(ginfo, x, y);
		return;
	}

	plot = find_plot_by_y(ginfo, y);
	if (plot)
		draw_plot_info(ginfo, plot, x, y);
}

static gboolean
info_button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	struct graph_info *ginfo = data;

	if (!ginfo->handle)
		return FALSE;

	if (event->button != 1)
		return FALSE;

	/* check for double click */
	if (event->type == GDK_2BUTTON_PRESS)
		return FALSE;

	button_press(ginfo, gtk_adjustment_get_value(ginfo->hadj), event->state);

	return FALSE;
}

static gboolean
info_motion_notify_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
	struct graph_info *ginfo = data;
	GdkModifierType state;
	gint x, y;

	if (!ginfo->handle)
		return FALSE;

	if (!ginfo->line_active)
		return FALSE;

	if (!ginfo->curr_pixmap)
		return FALSE;

	clear_info_box(ginfo);

	if (event->is_hint)
		gdk_window_get_pointer(event->window, &x, &y, &state);
	else {
		x = event->x;
		y = event->y;
	}

	/* Position x relative to the location in the drawing area */
	x -= ginfo->scrollwin->allocation.x - ginfo->info_scrollwin->allocation.x;

	if (x < 0)
		return FALSE;

	x += gtk_adjustment_get_value(ginfo->hadj);

	motion_plot(ginfo, x, y);

	return FALSE;
}

static void button_release(struct graph_info *ginfo, gint x);

static gboolean
info_button_release_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
	struct graph_info *ginfo = data;
	gint x;

	if (!ginfo->handle)
		return FALSE;

	x = event->x - ginfo->scrollwin->allocation.x - ginfo->info_scrollwin->allocation.x;

	button_release(ginfo, x);

	return FALSE;
}

#define PLOT_BOARDER 5

int trace_graph_check_sched_wakeup(struct graph_info *ginfo,
				   struct record *record,
				   gint *pid)
{
	struct event_format *event;
	unsigned long long val;
	gboolean found;
	gint id;

	if (ginfo->event_wakeup_id < 0) {

		found = FALSE;

		event = pevent_find_event_by_name(ginfo->pevent,
						  "sched", "sched_wakeup");
		if (event) {
			found = TRUE;
			ginfo->event_wakeup_id = event->id;
			ginfo->wakeup_pid_field = pevent_find_field(event, "pid");
			ginfo->wakeup_success_field = pevent_find_field(event, "success");
		}


		event = pevent_find_event_by_name(ginfo->pevent,
						  "sched", "sched_wakeup_new");
		if (event) {
			found = TRUE;
			ginfo->event_wakeup_new_id = event->id;
			ginfo->wakeup_new_pid_field = pevent_find_field(event, "pid");
			ginfo->wakeup_new_success_field = pevent_find_field(event, "success");
		}
		if (!found)
			return 0;
	}

	id = pevent_data_type(ginfo->pevent, record);

	if (id == ginfo->event_wakeup_id) {
		/* We only want those that actually woke up the task */
		pevent_read_number_field(ginfo->wakeup_success_field, record->data, &val);
		if (!val)
			return 0;
		pevent_read_number_field(ginfo->wakeup_pid_field, record->data, &val);
		if (pid)
			*pid = val;
		return 1;
	}

	if (id == ginfo->event_wakeup_new_id) {
		/* We only want those that actually woke up the task */
		pevent_read_number_field(ginfo->wakeup_new_success_field, record->data, &val);
		if (!val)
			return 0;
		pevent_read_number_field(ginfo->wakeup_new_pid_field, record->data, &val);
		if (pid)
			*pid = val;
		return 1;
	}

	return 0;
}

int trace_graph_check_sched_switch(struct graph_info *ginfo,
				   struct record *record,
				   gint *pid, const char **comm)
{
	unsigned long long val;
	struct event_format *event;
	gint this_pid;
	gint id;
	int ret = 1;

	if (ginfo->read_comms) {
		/* record all pids, for task plots */
		this_pid = pevent_data_pid(ginfo->pevent, record);
		add_task_hash(ginfo, this_pid);
	}

	if (ginfo->event_sched_switch_id < 0) {
		event = pevent_find_event_by_name(ginfo->pevent,
						  "sched", "sched_switch");
		if (!event)
			return 0;

		ginfo->event_sched_switch_id = event->id;
		ginfo->event_prev_state = pevent_find_field(event, "prev_state");
		ginfo->event_pid_field = pevent_find_field(event, "next_pid");
		ginfo->event_comm_field = pevent_find_field(event, "next_comm");

		event = pevent_find_event_by_name(ginfo->pevent,
						  "ftrace", "context_switch");
		if (event) {
			ginfo->ftrace_sched_switch_id = event->id;
			ginfo->ftrace_pid_field = pevent_find_field(event, "next_pid");
			ginfo->ftrace_comm_field = pevent_find_field(event, "next_comm");
		}
	}

	id = pevent_data_type(ginfo->pevent, record);
	if (id == ginfo->event_sched_switch_id) {
		pevent_read_number_field(ginfo->event_pid_field, record->data, &val);
		if (comm)
			*comm = record->data + ginfo->event_comm_field->offset;
		if (pid)
			*pid = val;
		goto out;
	}

	if (id == ginfo->ftrace_sched_switch_id) {
		pevent_read_number_field(ginfo->ftrace_pid_field, record->data, &val);
		if (comm)
			*comm = record->data + ginfo->ftrace_comm_field->offset;
		if (pid)
			*pid = val;
		goto out;
	}

	ret = 0;
 out:
	if (ret && comm && ginfo->read_comms) {
		/*
		 * First time through, register any missing
		 *  comm / pid mappings.
		 */
		if (!pevent_pid_is_registered(ginfo->pevent, *pid))
			pevent_register_comm(ginfo->pevent,
					     strdup(*comm), *pid);
	}

	return ret;
}

static void draw_info_box(struct graph_info *ginfo, const gchar *buffer,
			  gint x, gint y)
{
	PangoLayout *layout;
	GtkAdjustment *vadj;
	gint width, height;
	GdkPixmap *pix;
	static GdkGC *pix_bg;
	gint view_width;
	gint view_start;

	if (!pix_bg) {
		GdkColor color;

		pix_bg = gdk_gc_new(ginfo->draw->window);
		color.red = (0xff) *(65535/255);
		color.green = (0xfa) *(65535/255);
		color.blue = (0xcd) *(65535/255);
		gdk_color_alloc(gtk_widget_get_colormap(ginfo->draw), &color);
		gdk_gc_set_foreground(pix_bg, &color);
	}

	layout = gtk_widget_create_pango_layout(ginfo->draw, buffer);
	pango_layout_get_pixel_size(layout, &width, &height);

	width += PLOT_BOARDER * 2;
	height += PLOT_BOARDER * 2;

	view_start = gtk_adjustment_get_value(ginfo->hadj);
	view_width = gtk_adjustment_get_page_size(ginfo->hadj);
	if (x > view_start + width)
		x -= width;

	vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ginfo->scrollwin));
	view_start = gtk_adjustment_get_value(vadj);
	if (y > view_start + height)
		y -= height;

	ginfo->plot_data_x = x;
	ginfo->plot_data_y = y;
	ginfo->plot_data_w = width;
	ginfo->plot_data_h = height;

	pix = gdk_pixmap_new(ginfo->draw->window,
			     width,
			     height,
			     -1);

	gdk_draw_rectangle(pix,
			   pix_bg,
			   TRUE,
			   0, 0,
			   width, height);
	
	gdk_draw_rectangle(pix,
			   ginfo->draw->style->black_gc,
			   FALSE,
			   0, 0,
			   width-1, height-1);

	gdk_draw_layout(pix, ginfo->draw->style->black_gc,
			PLOT_BOARDER, PLOT_BOARDER, layout);
	gdk_draw_drawable(ginfo->draw->window,
			  ginfo->draw->style->fg_gc[GTK_WIDGET_STATE(ginfo->draw)],
			  pix, 0, 0, x, y, width, height);

	g_object_unref(layout);
	g_object_unref(pix);
}

static void draw_plot_info(struct graph_info *ginfo, struct graph_plot *plot,
			   gint x, gint y)
{
	struct pevent *pevent;
	guint64 time;
	unsigned long sec, usec;
	struct trace_seq s;

	time =  convert_x_to_time(ginfo, x);
	convert_nano(time, &sec, &usec);

	pevent = ginfo->pevent;

	trace_seq_init(&s);

	dprintf(3, "start=%llu end=%llu time=%llu\n",
		(u64)ginfo->start_time, (u64)ginfo->end_time, (u64)time);

	if (!trace_graph_plot_display_info(ginfo, plot, &s, time)) {
		/* Just display the current time */
		trace_seq_init(&s);
		trace_seq_printf(&s, "%lu.%06lu", sec, usec);
	}

	trace_seq_putc(&s, 0);

	draw_info_box(ginfo, s.buffer, x, y);
}

static void draw_latency(struct graph_info *ginfo, gint x, gint y)
{
	struct pevent *pevent;
	unsigned long sec, usec;
	struct trace_seq s;
	gboolean neg;
	gint64 time;

	update_markb(ginfo, x);

	time =  convert_x_to_time(ginfo, x);
	time -= ginfo->line_time;

	if (time < 0) {
		neg = TRUE;
		time *= -1;
	} else
		neg = FALSE;

	convert_nano(time, &sec, &usec);

	pevent = ginfo->pevent;

	trace_seq_init(&s);
	trace_seq_printf(&s, "Diff: %s%ld.%06lu secs", neg ? "-":"", sec, usec);

	draw_info_box(ginfo, s.buffer, x, y);
}

static gboolean
motion_notify_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
	struct graph_info *ginfo = data;
	GdkModifierType state;
	gint x, y;

	if (!ginfo->handle)
		return FALSE;

	clear_info_box(ginfo);

	if (event->is_hint)
		gdk_window_get_pointer(event->window, &x, &y, &state);
	else {
		x = event->x;
		y = event->y;
		state = event->state;
	}

	motion_plot(ginfo, x, y);

	return TRUE;
}

static int update_graph(struct graph_info *ginfo, gdouble percent)
{
	gint full_width = ginfo->full_width * percent;
	gdouble resolution = (gdouble)full_width / (gdouble)(ginfo->end_time -
							     ginfo->start_time);

	/* Check if we are too big */
	if (!resolution || full_width <= 0)
		return -1;

	ginfo->full_width = full_width;
	ginfo->resolution = resolution;
	ginfo->start_x = (ginfo->view_start_time - ginfo->start_time) *
		ginfo->resolution;

	dprintf(1, "new resolution = %f\n", resolution);
	return 0;
}

static void update_graph_to_start_x(struct graph_info *ginfo)
{
	gint width = ginfo->draw_width;;

	if (!width) {
		ginfo->view_start_time = ginfo->start_time;
		ginfo->view_end_time = ginfo->end_time;
		return;
	}

	ginfo->view_start_time = (gdouble)ginfo->start_x / ginfo->resolution +
		ginfo->start_time;

	ginfo->view_end_time = (gdouble)width / ginfo->resolution +
		ginfo->view_start_time;

	g_assert (ginfo->view_start_time < ginfo->end_time);
}

static void reset_graph(struct graph_info *ginfo, gdouble view_width)
{
	ginfo->full_width = view_width;
	ginfo->draw_width = 0;
	ginfo->view_start_time = ginfo->start_time;
	ginfo->view_end_time = ginfo->end_time;
	ginfo->start_x = 0;
}

static void zoom_in_window(struct graph_info *ginfo, gint start, gint end)
{
	guint64 start_time;
	gdouble view_width;
	gdouble new_width;
	gdouble select_width;
	gdouble curr_width;
	gdouble mid;
	gdouble percent;
	gint old_width = ginfo->draw_width;

	g_assert(start < end);
	g_assert(ginfo->hadj);

	start_time = ginfo->start_time +
		(ginfo->start_x + start) / ginfo->resolution;

	view_width = gtk_adjustment_get_page_size(ginfo->hadj);
	select_width = end - start;
	percent = view_width / select_width;

	dprintf(1, "view width=%f select width=%f percent=%f\n",
		view_width, select_width, percent);

	if (update_graph(ginfo, percent) < 0)
		return;

	curr_width = ginfo->draw->allocation.width;
	new_width = curr_width * percent;

	ginfo->draw_width = new_width;
	dprintf(1, "zoom in draw_width=%d full_width=%d\n",
	       ginfo->draw_width, ginfo->full_width);

	if (ginfo->draw_width > MAX_WIDTH) {
		gint new_start;
		gint new_end;

		/*
		 * The drawing is now greater than our max. We must
		 * limit the maximum size of the drawing area or
		 * we risk running out of X resources.
		 *
		 * We will now shorten the trace to that of what will
		 * fit in this zoomed area.
		 */
		ginfo->draw_width = MAX_WIDTH;

		mid = start + (end - start) / 2;
		mid *= percent;
		mid += ginfo->start_x;

		/*
		 * mid now points to the center of the viewable area
		 * if the draw area was of new_width.
		 *
		 *       new_start           new_end
		 * +------------------------------------------------+
		 * |        |                 |                     |
		 * |        |                 |                     |
		 * +------------------------------------------------+
		 * ^                ^
		 * |               mid
		 * old view start
		 *
		 */

		new_start = mid - MAX_WIDTH / 2;
		new_end = new_start + MAX_WIDTH;

		if (new_start < 0) {
			mid += new_start;
			new_start = 0;
		} else if (new_end > ginfo->full_width) {
			new_start -= new_end - ginfo->full_width;
			mid += new_end - ginfo->full_width;
			new_end = ginfo->full_width;
			g_assert(new_start >= 0);
		}

		ginfo->start_x = new_start;

		dprintf(1, "new start/end =%d/%d full:%d  start_time:",
		       new_start, new_end, ginfo->full_width);
		print_time(ginfo->view_start_time);
		dprintf(1, "\n");

		/* Adjust start to be the location for the hadj */
		start = (mid - new_start) - view_width / 2;
	} else
		start *= percent;

	update_graph_to_start_x(ginfo);

	ginfo->hadj_value = start;
	ginfo->hadj_value = convert_time_to_x(ginfo, start_time);

	if (ginfo->hadj_value > (ginfo->draw_width - view_width))
		ginfo->hadj_value = ginfo->draw_width - view_width;

	dprintf(1, "new width=%d\n", ginfo->draw_width);

	/* make sure the width is sent */
	if (ginfo->draw_width == old_width)
		redraw_graph(ginfo);
	else
		gtk_widget_set_size_request(ginfo->draw, ginfo->draw_width, ginfo->draw_height);

	dprintf(1, "set val %f\n", ginfo->hadj_value);


	dprintf(1, "*** ended with with ");
	print_time(convert_x_to_time(ginfo, ginfo->hadj_value));
	dprintf(1, "\n");

}

static gboolean
value_changed(GtkWidget *widget, gpointer data)
{
	GtkAdjustment *adj = GTK_ADJUSTMENT(widget);

	dprintf(2, "value = %f\n",
	       gtk_adjustment_get_value(adj));

	return TRUE;

}

static void zoom_out_window(struct graph_info *ginfo, gint start, gint end)
{
	gdouble view_width;
	gdouble divider;
	gdouble curr_width;
	gdouble new_width;
	gdouble mid;
	gdouble start_x;
	unsigned long long time;
	gint old_width = ginfo->draw_width;

	g_assert(start > end);
	g_assert(ginfo->hadj);

	view_width = gtk_adjustment_get_page_size(ginfo->hadj);
	start_x = gtk_adjustment_get_value(ginfo->hadj);
	mid = start_x + view_width / 2;

	time = convert_x_to_time(ginfo, mid);

	divider = start - end;

	curr_width = ginfo->draw->allocation.width;
	new_width = curr_width / divider;

	if (update_graph(ginfo, 1 / divider) < 0)
		return;

	dprintf(1, "width=%d\n", ginfo->draw->allocation.width);

	ginfo->draw_width = new_width;

	dprintf(1, "draw_width=%d full_width=%d\n", ginfo->draw_width, ginfo->full_width);
	if (ginfo->full_width < view_width) {
		reset_graph(ginfo, view_width);
		time = ginfo->view_start_time;

	} else if (ginfo->draw_width < ginfo->full_width) {
		if (ginfo->full_width < MAX_WIDTH) {
			ginfo->draw_width = ginfo->full_width;
			ginfo->view_start_time = ginfo->start_time;
			ginfo->view_end_time = ginfo->end_time;
			ginfo->start_x = 0;
		} else {
			ginfo->draw_width = MAX_WIDTH;
			mid /= divider;
			mid += ginfo->start_x;

			/* mid now is the current mid with full_width */
			ginfo->start_x = mid - MAX_WIDTH / 2;
			if (ginfo->start_x < 0)
				ginfo->start_x = 0;

			update_graph_to_start_x(ginfo);
		}
	}

	dprintf(1, "new width=%d\n", ginfo->draw_width);

	/* make sure the width is sent */
	if (ginfo->draw_width == old_width)
		redraw_graph(ginfo);
	else
		gtk_widget_set_size_request(ginfo->draw, ginfo->draw_width, ginfo->draw_height);

	mid = convert_time_to_x(ginfo, time);
	start_x = mid - view_width / 2;
	if (start_x < 0)
		start_x = 0;

	ginfo->hadj_value = start_x;
}

static void activate_zoom(struct graph_info *ginfo, gint x)
{
	if (!ginfo->zoom)
		return;

	ginfo->zoom = FALSE;

	if (x > ginfo->press_x) {
		/* make a decent zoom */
		if (x - ginfo->press_x < 10)
			return;
		zoom_in_window(ginfo, ginfo->press_x, x);
	} else if (x < ginfo->press_x)
		zoom_out_window(ginfo, ginfo->press_x, x);
}

static void button_release(struct graph_info *ginfo, gint x)
{
	gint old_x;

	if (!ginfo->line_active)
		return;

	if (!ginfo->zoom) {
		ginfo->show_marka = TRUE;
		ginfo->show_markb = TRUE;
		update_markb(ginfo, x);
	}

	clear_line(ginfo, ginfo->last_x);
	clear_line(ginfo, ginfo->press_x);
	ginfo->line_active = FALSE;

	clear_info_box(ginfo);

	/* If button is released at same location, set A (without shift) */
	if (ginfo->zoom &&
	    x >= ginfo->press_x-1 && x <= ginfo->press_x+1) {
		old_x = convert_time_to_x(ginfo, ginfo->marka_time);
		ginfo->show_marka = TRUE;
		update_marka(ginfo, x);
		clear_line(ginfo, old_x);
		if (ginfo->markb_time)
			update_label_time(ginfo->delta_label,
					  ginfo->markb_time - ginfo->marka_time);
	}

	activate_zoom(ginfo, x);
}

static gboolean
button_release_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
	struct graph_info *ginfo = data;

	if (!ginfo->handle)
		return FALSE;

	button_release(ginfo, event->x);

	return TRUE;
}

static gboolean
leave_notify_event(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
{
	struct graph_info *ginfo = data;

	if (!ginfo->handle)
		return FALSE;

	clear_info_box(ginfo);

	return FALSE;
}

static void set_color(GtkWidget *widget, GdkGC *gc, gint c)
{
	GdkColor color;

	color.red = (c & 0xff)*(65535/255);
	color.blue = ((c >> 8) & 0xff)*(65535/255);
	color.green = ((c >> 16) & 0xff)*(65535/255);
	gdk_color_alloc(gtk_widget_get_colormap(widget), &color);
	gdk_gc_set_foreground(gc, &color);
}

#define LABEL_SPACE 3

static gint draw_event_label(struct graph_info *ginfo, gint i,
			    gint p1, gint p2, gint p3,
			    gint width_16, PangoFontDescription *font)
{
	struct graph_plot *plot = ginfo->plot_array[i];
	PangoLayout *layout;
	struct trace_seq s;
	gint text_width;
	gint text_height;
	gint start, end;
	gint x, y;
	gint ret;

	/*
	 * We are testing if we can print the label at p2.
	 * p1 has the start of the area that we can print.
	 * p3 is the location of the next label.
	 * We will not print any label unless we have enough
	 * room to print a minimum of 16 characters.
	 */
	if (p3 - p1 < width_16 ||
	    p3 - p2 < width_16 / 2)
		return p2;

	/* Now get p2's drawing size */
	trace_seq_init(&s);

	/*
	 * Display the event after p2 - 1. We use "-1" because we need to
	 * find the event at this pixel, and due to rounding, p2 time can
	 * be after the time of the event. Since tracecmd finds the next event
	 * after the time, we use this to find our next event.
	 */
	ret = trace_graph_plot_display_last_event(ginfo, plot, &s,
						  convert_x_to_time(ginfo, p2-1));
	if (!ret)
		return p2;

	layout = gtk_widget_create_pango_layout(ginfo->draw, s.buffer);
	pango_layout_set_font_description(layout, font);
	pango_layout_get_pixel_size(layout, &text_width, &text_height);

	/* Lets see if we can print this info */
	if (p2 < text_width)
		start = 1;
	else
		start = p2 - text_width / 2;
	end = start + text_width;

	if (start < p1 || end > p3) {
		g_object_unref(layout);
		return p2;
	}

	/* Display the info */
	x = start;

	y = (PLOT_TOP(i) - text_height + 5);
	gdk_draw_layout(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
			x, y, layout);

	gdk_draw_line(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
		      p2, PLOT_TOP(i) - 5, p2, PLOT_TOP(i) - 1);

	g_object_unref(layout);

	/*
	 * Set the next p1 to start after the end of what was displayed
	 * plus a little padding.
	 */
	return end + LABEL_SPACE;
}

static gint draw_plot_line(struct graph_info *ginfo, int i,
			   unsigned long long time, GdkGC *gc)
{
	gint x;

	x = convert_time_to_x(ginfo, time);

	gdk_draw_line(ginfo->curr_pixmap, gc,
		      x, PLOT_TOP(i), x, PLOT_BOTTOM(i));

	return x;
}

static void draw_plot_box(struct graph_info *ginfo, int i,
			  unsigned long long start,
			  unsigned long long end,
			  gboolean fill, GdkGC *gc)
{
	gint x1;
	gint x2;

	x1 = convert_time_to_x(ginfo, start);
	x2 = convert_time_to_x(ginfo, end);

	gdk_draw_rectangle(ginfo->curr_pixmap, gc,
			   fill,
			   x1, PLOT_BOX_TOP(i),
			   x2 - x1, PLOT_BOX_SIZE);
}

static void draw_plot(struct graph_info *ginfo, struct graph_plot *plot,
		      struct record *record)
{
	static PangoFontDescription *font;
	PangoLayout *layout;
	static gint width_16;
	struct plot_info info;
	gint x;

	/* Calculate the size of 16 characters */
	if (!width_16) {
		gchar buf[17];
		gint text_height;

		memset(buf, 'a', 16);
		buf[16] = 0;

		font = pango_font_description_from_string("Sans 8");
		layout = gtk_widget_create_pango_layout(ginfo->draw, buf);
		pango_layout_set_font_description(layout, font);
		pango_layout_get_pixel_size(layout, &width_16, &text_height);
		g_object_unref(layout);
	}

	trace_graph_plot_event(ginfo, plot, record, &info);

	if (info.box) {
		if (info.bcolor != plot->last_color) {
			plot->last_color = info.bcolor;
			set_color(ginfo->draw, plot->gc, plot->last_color);
		}

		draw_plot_box(ginfo, plot->pos, info.bstart, info.bend,
			      info.bfill, plot->gc);
	}

	if (info.line) {
		if (info.lcolor != plot->last_color) {
			plot->last_color = info.lcolor;
			set_color(ginfo->draw, plot->gc, plot->last_color);
		}

		x = draw_plot_line(ginfo, plot->pos, info.ltime, plot->gc);

		/* Figure out if we can show the text for the previous record */

		plot->p3 = x;

		/* Make sure p2 will be non-zero the next iteration */
		if (!plot->p3)
			plot->p3 = 1;

		/* first record, continue */
		if (plot->p2)
			plot->p2 = draw_event_label(ginfo, plot->pos,
						    plot->p1, plot->p2, plot->p3, width_16, font);

		plot->p1 = plot->p2;
		plot->p2 = plot->p3;
	}

	if (!record && plot->p2)
		draw_event_label(ginfo, plot->pos,
				 plot->p1, plot->p2, ginfo->draw_width, width_16, font);
}

static void draw_plots(struct graph_info *ginfo, gint new_width)
{
	struct plot_list *list;
	struct graph_plot *plot;
	struct record *record;
	struct plot_hash *hash;
	gint pid;
	gint cpu;
	gint i;

	/* Initialize plots */
	for (i = 0; i < ginfo->plots; i++) {
		plot = ginfo->plot_array[i];

		if (!plot->gc)
			plot->gc = gdk_gc_new(ginfo->draw->window);
		plot->p1 = 0;
		plot->p2 = 0;
		plot->p3 = 0;
		plot->last_color = -1;

		gdk_draw_line(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
			      0, PLOT_LINE(i), new_width, PLOT_LINE(i));

		trace_graph_plot_start(ginfo, plot, ginfo->view_start_time);

		set_color(ginfo->draw, plot->gc, plot->last_color);
	}

	tracecmd_set_all_cpus_to_timestamp(ginfo->handle,
					   ginfo->view_start_time);

	/* Shortcut if we don't have any task plots */
	if (!ginfo->nr_task_hash && !ginfo->all_recs) {
		for (cpu = 0; cpu < ginfo->cpus; cpu++) {
			hash = trace_graph_plot_find_cpu(ginfo, cpu);
			if (!hash)
				continue;

			while ((record = tracecmd_read_data(ginfo->handle, cpu))) {
				if (record->ts < ginfo->view_start_time) {
					free_record(record);
					continue;
				}
				if (record->ts > ginfo->view_end_time) {
					free_record(record);
					break;
				}
				for (list = hash->plots; list; list = list->next)
					draw_plot(ginfo, list->plot, record);
				free_record(record);
			}
		}
		goto out;
	}

	while ((record = tracecmd_read_next_data(ginfo->handle, &cpu))) {
		if (record->ts < ginfo->view_start_time) {
			free_record(record);
			continue;
		}
		if (record->ts > ginfo->view_end_time) {
			free_record(record);
			break;
		}
		hash = trace_graph_plot_find_cpu(ginfo, cpu);
		if (hash) {
			for (list = hash->plots; list; list = list->next)
				draw_plot(ginfo, list->plot, record);
		}
		pid = pevent_data_pid(ginfo->pevent, record);
		hash = trace_graph_plot_find_task(ginfo, pid);
		if (hash) {
			for (list = hash->plots; list; list = list->next)
				draw_plot(ginfo, list->plot, record);
		}
		for (list = ginfo->all_recs; list; list = list->next)
			draw_plot(ginfo, list->plot, record);
		free_record(record);
	}

out:
	for (i = 0; i < ginfo->plots; i++) {
		plot = ginfo->plot_array[i];
		draw_plot(ginfo, plot, NULL);
		trace_graph_plot_end(ginfo, plot);
		gdk_gc_unref(plot->gc);
		plot->gc = NULL;
	}
}


static void draw_timeline(struct graph_info *ginfo, gint width)
{
	PangoLayout *layout;
	struct trace_seq s;
	unsigned long sec, usec;
	unsigned long long time;
	gint mid;
	gint w, h, height;
	gint view_width;

	/* --- draw timeline text --- */

	layout = gtk_widget_create_pango_layout(ginfo->draw, "Time Line");
	pango_layout_get_pixel_size(layout, &w, &h);

	height = 10 + h;

	mid = width / 2;
	gdk_draw_layout(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
			mid - w / 2, 5, layout);
	g_object_unref(layout);


	/* --- draw time line lines --- */
	gdk_draw_line(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
		      0, height, width, height);

	gdk_draw_line(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
		      0, height, 0, height + 5);

	gdk_draw_line(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
		      width-1, height, width-1, height);

	gdk_draw_line(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
		      width-1, height, width-1, height + 5);

	/* --- draw starting time --- */
	convert_nano(ginfo->view_start_time, &sec, &usec);
	trace_seq_init(&s);
	trace_seq_printf(&s, "%lu.%06lu", sec, usec);

	layout = gtk_widget_create_pango_layout(ginfo->draw, s.buffer);
	pango_layout_get_pixel_size(layout, &w, &h);

	gdk_draw_layout(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
			1, height+10, layout);
	g_object_unref(layout);


	/* --- draw ending time --- */
	convert_nano(ginfo->view_end_time, &sec, &usec);
	trace_seq_init(&s);
	trace_seq_printf(&s, "%lu.%06lu", sec, usec);

	layout = gtk_widget_create_pango_layout(ginfo->draw, s.buffer);
	pango_layout_get_pixel_size(layout, &w, &h);

	gdk_draw_layout(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
			width - (w + 2), height+10, layout);
	g_object_unref(layout);


	/* --- draw time at intervals --- */
	view_width = gtk_adjustment_get_page_size(ginfo->hadj);

	for (mid = view_width / 2; mid < (width - view_width / 2 + 10);
	     mid += view_width / 2) {
		time = convert_x_to_time(ginfo, mid);

		convert_nano(time, &sec, &usec);
		trace_seq_init(&s);
		trace_seq_printf(&s, "%lu.%06lu", sec, usec);

		gdk_draw_line(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
			      mid, height, mid, height + 5);

		layout = gtk_widget_create_pango_layout(ginfo->draw, s.buffer);
		pango_layout_get_pixel_size(layout, &w, &h);

		gdk_draw_layout(ginfo->curr_pixmap, ginfo->draw->style->black_gc,
				mid - (w / 2), height+10, layout);
		g_object_unref(layout);
	}
}

static void draw_info(struct graph_info *ginfo,
		      gint new_width)
{
	if (!ginfo->handle)
		return;

	ginfo->resolution = (gdouble)new_width / (gdouble)(ginfo->view_end_time -
							   ginfo->view_start_time);

	ginfo->full_width = (ginfo->end_time - ginfo->start_time) * ginfo->resolution;

	draw_timeline(ginfo, new_width);

	draw_plots(ginfo, new_width);

	ginfo->read_comms = FALSE;
}

void trace_graph_select_by_time(struct graph_info *ginfo, guint64 time)
{
	GtkAdjustment *vadj;
	gint view_start;
	gint view_width;
	gint width;
	gint mid;
	gint start;
	gint end;
	int ret;
	gint i;
	guint64 old_start_time = ginfo->view_start_time;

	view_width = gtk_adjustment_get_page_size(ginfo->hadj);
	width = ginfo->draw_width ? : ginfo->full_width;

	mid = (time - ginfo->start_time) * ginfo->resolution;
	start = mid - width / 2;
	if (start < 0)
		start = 0;
	end = start + width;

	/*
	 * Readjust the drawing to be centered on the selection.
	 */

	if (end > ginfo->full_width) {
		start -= end - ginfo->full_width;
		g_assert(start >= 0);
		end = ginfo->full_width;
	}

	ginfo->start_x = start;

	update_graph_to_start_x(ginfo);

	/* force redraw if we changed the time*/
	if (old_start_time != ginfo->view_start_time)
		redraw_pixmap_backend(ginfo);

	/* Adjust start to be the location for the hadj */
	mid = convert_time_to_x(ginfo, time);
	start = mid - view_width / 2;
	if (start < 0)
		start = 0;

	if (start > (width - view_width))
		start = width - view_width;
	gtk_adjustment_set_value(ginfo->hadj, start);

	ginfo->last_x = convert_time_to_x(ginfo, ginfo->cursor);
	ginfo->cursor = 0;
	clear_line(ginfo, ginfo->last_x);
	ginfo->cursor = time;

	update_with_backend(ginfo, 0, 0, width, ginfo->draw_height);

	/*
	 * If a record exists at this exact time value, we should
	 * make sure that it is in view.
	 */
	for (i = 0; i < ginfo->plots; i++) {
		ret = trace_graph_plot_match_time(ginfo, ginfo->plot_array[i],
						  time);
		if (ret)
			break;
	}
	if (i == ginfo->plots)
		return;

	/* Make sure PLOT is visible */
	vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ginfo->scrollwin));
	view_start = gtk_adjustment_get_value(vadj);
	view_width = gtk_adjustment_get_page_size(vadj);

	if (PLOT_TOP(i) > view_start &&
	    PLOT_BOTTOM(i) < view_start + view_width)
		return;

	if (PLOT_TOP(i) < view_start)
		gtk_adjustment_set_value(vadj, PLOT_TOP(i) - 5);

	if (PLOT_BOTTOM(i) > view_start + view_width)
		gtk_adjustment_set_value(vadj, (PLOT_BOTTOM(i) - view_width) + 10);
}

void trace_graph_event_filter_callback(gboolean accept,
				       gboolean all_events,
				       gchar **systems,
				       gint *events,
				       gpointer data)
{
	struct graph_info *ginfo = data;

	if (!accept)
		return;

	if (all_events) {
		ginfo->all_events = TRUE;
		/* filter is no longer used */
		pevent_filter_reset(ginfo->event_filter);
		redraw_graph(ginfo);
		return;
	}

	ginfo->all_events = FALSE;

	pevent_filter_clear_trivial(ginfo->event_filter, FILTER_TRIVIAL_BOTH);

	trace_filter_convert_char_to_filter(ginfo->event_filter,
					    systems, events);

	redraw_graph(ginfo);
}

void trace_graph_adv_filter_callback(gboolean accept,
				     const gchar *text,
				     gint *event_ids,
				     gpointer data)
{
	struct graph_info *ginfo = data;
	struct event_filter *event_filter;
	char *error_str;
	int ret;
	int i;

	if (!accept)
		return;

	if (!has_text(text) && !event_ids)
		return;

	event_filter = ginfo->event_filter;

	if (event_ids) {
		for (i = 0; event_ids[i] >= 0; i++)
			pevent_filter_remove_event(event_filter, event_ids[i]);
	}

	if (has_text(text)) {

		ginfo->all_events = FALSE;

		pevent_filter_clear_trivial(event_filter,
					    FILTER_TRIVIAL_BOTH);

		ret = pevent_filter_add_filter_str(event_filter, text, &error_str);
		if (ret < 0) {
			warning("filter failed due to: %s", error_str);
			free(error_str);
			return;
		}
	}

	redraw_graph(ginfo);
}

void trace_graph_copy_filter(struct graph_info *ginfo,
			     gboolean all_events,
			     struct event_filter *event_filter)
{
	if (all_events) {
		ginfo->all_events = TRUE;
		/* filter is no longer used */
		pevent_filter_reset(ginfo->event_filter);
		redraw_graph(ginfo);
		return;
	}

	ginfo->all_events = FALSE;

	pevent_filter_copy(ginfo->event_filter, event_filter);

	redraw_graph(ginfo);
}

static void redraw_pixmap_backend(struct graph_info *ginfo)
{
	GdkPixmap *old_pix;
	static gboolean init;

	old_pix = ginfo->curr_pixmap;

	/* initialize full width if needed */
	if (!ginfo->full_width)
		ginfo->full_width = ginfo->draw->allocation.width;

	ginfo->curr_pixmap = gdk_pixmap_new(ginfo->draw->window,
					    ginfo->draw->allocation.width,
					    ginfo->draw->allocation.height,
					    -1);

	gdk_draw_rectangle(ginfo->curr_pixmap,
			   ginfo->draw->style->white_gc,
			   TRUE,
			   0, 0,
			   ginfo->draw->allocation.width,
			   ginfo->draw->allocation.height);

	draw_info(ginfo, ginfo->draw->allocation.width);

	if (!init) {
		init = TRUE;
		green = gdk_gc_new(ginfo->draw->window);
		red = gdk_gc_new(ginfo->draw->window);
		set_color(ginfo->draw, green, (0xff<<16));
		set_color(ginfo->draw, red, 0xff);
	}

	if (old_pix)
		g_object_unref(old_pix);

	if (ginfo->hadj_value) {
//		gtk_adjustment_set_lower(ginfo->hadj, -100.0);
		gtk_adjustment_set_value(ginfo->hadj, ginfo->hadj_value);
	}
}

static gboolean
configure_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
	struct graph_info *ginfo = data;

	gtk_widget_set_size_request(widget, ginfo->draw_width, ginfo->draw_height);

	redraw_pixmap_backend(ginfo);

	/* debug */
	ginfo->hadj_value = gtk_adjustment_get_value(ginfo->hadj);
	dprintf(2, "get val %f\n", ginfo->hadj_value);
	ginfo->hadj_value = 0.0;

	return TRUE;
}

static gboolean
destroy_event(GtkWidget *widget, gpointer data)
{
	struct graph_info *ginfo = data;

	trace_graph_free_info(ginfo);

	filter_task_hash_free(ginfo->task_filter);
	filter_task_hash_free(ginfo->hide_tasks);

	return TRUE;
}

static void redraw_label_window(struct graph_info *ginfo, int x, int y,
				int w, int h)
{
	gdk_draw_drawable(ginfo->info->window,
			  ginfo->info->style->fg_gc[GTK_WIDGET_STATE(ginfo->info)],
			  ginfo->info_pixmap, x, y, x, y, w, h);
}

static gboolean
info_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
	struct graph_info *ginfo = data;

	redraw_label_window(ginfo, event->area.x, event->area.y,
			    event->area.width, event->area.height);
	return FALSE;
}

static void info_draw_plot_label(struct graph_info *ginfo, gint i)
{
	PangoLayout *layout;
	gint width, height;
	char *label;

	label = ginfo->plot_array[i]->label;

	layout = gtk_widget_create_pango_layout(ginfo->info, label);
	pango_layout_get_pixel_size(layout, &width, &height);
	width += 4;

	if (width > largest_plot_label)
		largest_plot_label = width;
	gdk_draw_rectangle(ginfo->info_pixmap,
			   ginfo->info->style->white_gc,
			   TRUE,
			   PLOT_X, PLOT_LABEL(i)+4,
			   width, height);
	gdk_draw_layout(ginfo->info_pixmap,
			ginfo->info->style->black_gc,
			PLOT_X+ 2, PLOT_LABEL(i) + 4,
			layout);
	g_object_unref(layout);
}

static void info_draw_plot_labels(struct graph_info *ginfo)
{
	gint i;

	if (!ginfo->handle)
		return;

	largest_plot_label = 0;

	for (i = 0; i < ginfo->plots; i++)
		info_draw_plot_label(ginfo, i);
}

static void update_label_window(struct graph_info *ginfo)
{
	if (ginfo->info_pixmap)
		g_object_unref(ginfo->info_pixmap);

	ginfo->info_pixmap = gdk_pixmap_new(ginfo->info->window,
					    ginfo->info->allocation.width,
					    ginfo->info->allocation.height,
					    -1);

	gdk_draw_rectangle(ginfo->info_pixmap,
			   ginfo->info->style->white_gc,
			   TRUE,
			   0, 0,
			   ginfo->info->allocation.width,
			   ginfo->info->allocation.height);

	info_draw_plot_labels(ginfo);

	gtk_widget_set_size_request(ginfo->info, largest_plot_label + 10,
				    ginfo->draw_height);

	redraw_label_window(ginfo, 0, 0, ginfo->info->allocation.width,
			    ginfo->info->allocation.height);
}

static gboolean
info_configure_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
	struct graph_info *ginfo = data;

	update_label_window(ginfo);

	return TRUE;
}

static GtkWidget *
create_graph_info(struct graph_info *ginfo)
{
	GtkWidget *info;

	info = gtk_drawing_area_new();

	gtk_signal_connect(GTK_OBJECT(info), "expose_event",
			   (GtkSignalFunc) info_expose_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(info), "configure_event",
			   (GtkSignalFunc) info_configure_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(info), "button_press_event",
			   (GtkSignalFunc) info_button_press_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(info), "motion_notify_event",
			   (GtkSignalFunc) info_motion_notify_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(info), "button_release_event",
			   (GtkSignalFunc) info_button_release_event, ginfo);

	gtk_widget_set_events(info, GDK_EXPOSURE_MASK
			      | GDK_BUTTON_PRESS_MASK
			      | GDK_BUTTON_RELEASE_MASK
			      | GDK_POINTER_MOTION_MASK
			      | GDK_POINTER_MOTION_HINT_MASK);

	return info;
}

void trace_graph_free_info(struct graph_info *ginfo)
{
	if (ginfo->handle) {
		pevent_filter_free(ginfo->event_filter);
		trace_graph_plot_free(ginfo);
		tracecmd_close(ginfo->handle);
		free_task_hash(ginfo);

		ginfo->cursor = 0;
	}
	ginfo->handle = NULL;
}

static int load_handle(struct graph_info *ginfo,
		       struct tracecmd_input *handle)
{
	struct record *record;
	unsigned long sec, usec;
	gint cpu;

	if (!handle)
		return -1;

	trace_graph_free_info(ginfo);
	trace_graph_plot_init(ginfo);

	ginfo->handle = handle;
	tracecmd_ref(handle);

	init_event_cache(ginfo);

	ginfo->pevent = tracecmd_get_pevent(handle);
	ginfo->cpus = tracecmd_cpus(handle);
	ginfo->all_events = TRUE;

	ginfo->event_filter = pevent_filter_alloc(ginfo->pevent);

	ginfo->start_time = -1ULL;
	ginfo->end_time = 0;

	graph_plot_init_cpus(ginfo, ginfo->cpus);

	ginfo->draw_height = PLOT_SPACE(ginfo->plots);

	for (cpu = 0; cpu < ginfo->cpus; cpu++) {
		record = tracecmd_read_cpu_first(handle, cpu);
		if (!record)
			continue;

		if (record->ts < ginfo->start_time)
			ginfo->start_time = record->ts;

		free_record(record);
		record = tracecmd_read_cpu_last(handle, cpu);

		if (record->ts > ginfo->end_time)
			ginfo->end_time = record->ts;
		free_record(record);
	}

	convert_nano(ginfo->start_time, &sec, &usec);
	dprintf(1, "start=%lu.%06lu ", sec, usec);

	convert_nano(ginfo->end_time, &sec, &usec);
	dprintf(1, "end=%lu.%06lu\n", sec, usec);

	ginfo->view_start_time = ginfo->start_time;
	ginfo->view_end_time = ginfo->end_time;

	if (!ginfo->draw)
		return 0;

	update_cursor(ginfo);
	update_pointer(ginfo, 0);
	update_marka(ginfo, 0);
	update_markb(ginfo, 0);

	return 0;
}

void trace_graph_refresh(struct graph_info *ginfo)
{
	ginfo->draw_height = PLOT_SPACE(ginfo->plots);
	gtk_widget_set_size_request(ginfo->draw, ginfo->draw_width, ginfo->draw_height);
	update_label_window(ginfo);
	redraw_graph(ginfo);
}

int trace_graph_load_handle(struct graph_info *ginfo,
			    struct tracecmd_input *handle)
{

	if (load_handle(ginfo, handle) < 0)
		return -1;

	update_label_window(ginfo);
	redraw_graph(ginfo);

	return 0;
}

struct graph_info *
trace_graph_create_with_callbacks(struct tracecmd_input *handle,
				  struct graph_callbacks *cbs)
{
	struct graph_info *ginfo;
	GtkWidget *table;
	GtkWidget *hbox;
	GtkWidget *label;
	GtkWidget *eventbox;
	GdkColor color;
	GdkColor colorAB;

	ginfo = g_new0(typeof(*ginfo), 1);
	g_assert(ginfo != NULL);

	if (handle)
		load_handle(ginfo, handle);

	ginfo->handle = handle;

	ginfo->callbacks = cbs;

	ginfo->task_filter = filter_task_hash_alloc();
	ginfo->hide_tasks = filter_task_hash_alloc();

	ginfo->widget = gtk_vbox_new(FALSE, 0);
	gtk_widget_show(ginfo->widget);


	ginfo->status_hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(ginfo->widget), ginfo->status_hbox, FALSE, FALSE, 0);
	gtk_widget_show(ginfo->status_hbox);

	table = gtk_table_new(1, 23, FALSE);
	gtk_box_pack_start(GTK_BOX(ginfo->status_hbox), table, FALSE, FALSE, 0);
	gtk_widget_show(table);

	color.red = (0xff) *(65535/255);
	color.green = (0xff) *(65535/255);
	color.blue = (0xff) *(65535/255);

	/* --- Pointer --- */

	label = gtk_label_new("Pointer:");
	gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(label);

	ginfo->pointer_time = gtk_label_new("0.0");
	eventbox = gtk_event_box_new();
	gtk_widget_show(eventbox);
	gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &color);
	gtk_container_add(GTK_CONTAINER(eventbox), ginfo->pointer_time);
	gtk_table_attach(GTK_TABLE(table), eventbox, 1, 3, 0, 1,
			 GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(ginfo->pointer_time);

	/* --- Cursor --- */

	label = gtk_label_new("Cursor:");
	gtk_table_attach(GTK_TABLE(table), label, 4, 5, 0, 1, GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(label);

	ginfo->cursor_label = gtk_label_new("0.0");
	eventbox = gtk_event_box_new();
	gtk_widget_show(eventbox);
	gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &color);
	gtk_container_add(GTK_CONTAINER(eventbox), ginfo->cursor_label);
	gtk_table_attach(GTK_TABLE(table), eventbox, 6, 8, 0, 1,
			 GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(ginfo->cursor_label);


	/* --- Marker A --- */

	hbox = gtk_hbox_new(FALSE, 0);
	gtk_widget_show(hbox);

	label = gtk_label_new("Marker");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
	gtk_widget_show(label);

	label = gtk_label_new("A:");

	colorAB.red = 0;
	colorAB.green = (0xff) *(65535/255);
	colorAB.blue = 0;

	eventbox = gtk_event_box_new();
	gtk_widget_show(eventbox);
	gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &colorAB);
	gtk_container_add(GTK_CONTAINER(eventbox), label);

	gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, FALSE, 0);
	gtk_widget_show(label);

	gtk_table_attach(GTK_TABLE(table), hbox, 9, 10, 0, 1, GTK_EXPAND, GTK_EXPAND, 3, 3);

	ginfo->marka_label = gtk_label_new("0.0");
	eventbox = gtk_event_box_new();
	gtk_widget_show(eventbox);
	gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &color);
	gtk_container_add(GTK_CONTAINER(eventbox), ginfo->marka_label);
	gtk_table_attach(GTK_TABLE(table), eventbox, 11, 13, 0, 1,
			 GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(ginfo->marka_label);


	/* --- Marker B --- */

	hbox = gtk_hbox_new(FALSE, 0);
	gtk_widget_show(hbox);

	label = gtk_label_new("Marker");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
	gtk_widget_show(label);

	label = gtk_label_new("B:");

	colorAB.red = (0xff) *(65535/255);
	colorAB.green = 0;
	colorAB.blue = 0;

	eventbox = gtk_event_box_new();
	gtk_widget_show(eventbox);
	gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &colorAB);
	gtk_container_add(GTK_CONTAINER(eventbox), label);

	gtk_box_pack_start(GTK_BOX(hbox), eventbox, FALSE, FALSE, 0);
	gtk_widget_show(label);

	gtk_table_attach(GTK_TABLE(table), hbox, 14, 15, 0, 1, GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(label);

	ginfo->markb_label = gtk_label_new("0.0");
	eventbox = gtk_event_box_new();
	gtk_widget_show(eventbox);
	gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &color);
	gtk_container_add(GTK_CONTAINER(eventbox), ginfo->markb_label);
	gtk_table_attach(GTK_TABLE(table), eventbox, 16, 18, 0, 1,
			 GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(ginfo->markb_label);


	/* --- Delta --- */

	label = gtk_label_new("A,B Delta:");
	gtk_table_attach(GTK_TABLE(table), label, 19, 20, 0, 1, GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(label);

	ginfo->delta_label = gtk_label_new("0.0");
	eventbox = gtk_event_box_new();
	gtk_widget_show(eventbox);
	gtk_widget_modify_bg(eventbox, GTK_STATE_NORMAL, &color);
	gtk_container_add(GTK_CONTAINER(eventbox), ginfo->delta_label);
	gtk_table_attach(GTK_TABLE(table), eventbox, 21, 23, 0, 1,
			 GTK_EXPAND, GTK_EXPAND, 3, 3);
	gtk_widget_show(ginfo->delta_label);


	hbox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(ginfo->widget), hbox, TRUE, TRUE, 0);
	gtk_widget_show(hbox);

	ginfo->scrollwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ginfo->scrollwin),
				       GTK_POLICY_AUTOMATIC,
				       GTK_POLICY_AUTOMATIC);
	gtk_widget_show(ginfo->scrollwin);
	ginfo->hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(ginfo->scrollwin));

	ginfo->info_scrollwin = gtk_scrolled_window_new(NULL,
		gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(ginfo->scrollwin)));

	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ginfo->info_scrollwin),
				       GTK_POLICY_NEVER,
				       GTK_POLICY_NEVER);
	gtk_widget_show(ginfo->info_scrollwin);
	gtk_box_pack_start(GTK_BOX(hbox), ginfo->info_scrollwin, FALSE, FALSE, 0);

	ginfo->info = create_graph_info(ginfo);
	gtk_widget_show(ginfo->info);

	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(ginfo->info_scrollwin),
					      ginfo->info);

	gtk_box_pack_start(GTK_BOX (hbox), ginfo->scrollwin, TRUE, TRUE, 0);

	gtk_signal_connect(GTK_OBJECT(ginfo->hadj), "value_changed",
			   (GtkSignalFunc) value_changed, ginfo);

	ginfo->draw = gtk_drawing_area_new();

	gtk_signal_connect(GTK_OBJECT(ginfo->draw), "expose_event",
			   (GtkSignalFunc) expose_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(ginfo->draw), "button_press_event",
			   (GtkSignalFunc) button_press_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(ginfo->draw), "configure_event",
			   (GtkSignalFunc) configure_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(ginfo->draw), "motion_notify_event",
			   (GtkSignalFunc) motion_notify_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(ginfo->draw), "button_release_event",
			   (GtkSignalFunc) button_release_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(ginfo->draw), "leave-notify-event",
			   (GtkSignalFunc) leave_notify_event, ginfo);
	gtk_signal_connect(GTK_OBJECT(ginfo->draw), "destroy",
			   (GtkSignalFunc) destroy_event, ginfo);

	gtk_widget_set_events(ginfo->draw, GDK_EXPOSURE_MASK
			      | GDK_LEAVE_NOTIFY_MASK
			      | GDK_BUTTON_PRESS_MASK
			      | GDK_BUTTON_RELEASE_MASK
			      | GDK_POINTER_MOTION_MASK
			      | GDK_POINTER_MOTION_HINT_MASK
			      | GDK_LEAVE_NOTIFY_MASK);

	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(ginfo->scrollwin),
					      ginfo->draw);
	gtk_widget_show(ginfo->draw);

	return ginfo;
}

struct graph_info *
trace_graph_create(struct tracecmd_input *handle)
{
	return trace_graph_create_with_callbacks(handle, NULL);
}