-
Notifications
You must be signed in to change notification settings - Fork 165
/
parsers.cpp
5864 lines (5166 loc) · 143 KB
/
parsers.cpp
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
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifdef _WIN32
#define NOMINMAX
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#endif // _WIN32
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits>
#include "container_engine/mesos.h"
#include "sinsp.h"
#include "sinsp_int.h"
#include "tracers.h"
#include "parsers.h"
#include "sinsp_errno.h"
#include "filter.h"
#include "filterchecks.h"
#include "protodecoder.h"
#include "strlcpy.h"
#ifdef SIMULATE_DROP_MODE
bool should_drop(sinsp_evt *evt);
#endif
#include "sinsp_int.h"
#if !defined(MINIMAL_BUILD)
#include "container_engine/docker/async_source.h"
#endif
extern sinsp_protodecoder_list g_decoderlist;
extern sinsp_evttables g_infotables;
sinsp_parser::sinsp_parser(sinsp *inspector) :
m_inspector(inspector),
m_tmp_evt(m_inspector),
m_fd_listener(NULL)
{
m_fake_userevt = (scap_evt*)m_fake_userevt_storage;
//
// Note: allocated here instead of in the sinsp constructor because sinsp_partial_tracer
// is not defined in sinsp.cpp
//
m_inspector->m_partial_tracers_pool = new simple_lifo_queue<sinsp_partial_tracer>(128);
init_metaevt(m_k8s_metaevents_state, PPME_K8S_E, SP_EVT_BUF_SIZE);
init_metaevt(m_mesos_metaevents_state, PPME_MESOS_E, SP_EVT_BUF_SIZE);
}
sinsp_parser::~sinsp_parser()
{
for(uint32_t j = 0; j < m_protodecoders.size(); j++)
{
delete m_protodecoders[j];
}
while(!m_tmp_events_buffer.empty())
{
auto ptr = m_tmp_events_buffer.top();
free(ptr);
m_tmp_events_buffer.pop();
}
m_protodecoders.clear();
free(m_k8s_metaevents_state.m_piscapevt);
free(m_mesos_metaevents_state.m_piscapevt);
if(m_inspector->m_partial_tracers_pool != NULL)
{
delete m_inspector->m_partial_tracers_pool;
}
}
void sinsp_parser::init_scapevt(metaevents_state& evt_state, uint16_t evt_type, uint16_t buf_size)
{
scap_evt *new_piscapevt = (scap_evt*) realloc(evt_state.m_piscapevt, buf_size);
if(new_piscapevt == NULL)
{
throw sinsp_exception("memory reallocation error in sinsp_parser::init_scapevt.");
}
evt_state.m_piscapevt = new_piscapevt;
evt_state.m_scap_buf_size = buf_size;
evt_state.m_piscapevt->type = evt_type;
evt_state.m_metaevt.m_pevt = evt_state.m_piscapevt;
}
void sinsp_parser::init_metaevt(metaevents_state& evt_state, uint16_t evt_type, uint16_t buf_size)
{
evt_state.m_piscapevt = 0;
init_scapevt(evt_state, evt_type, buf_size);
evt_state.m_metaevt.m_inspector = m_inspector;
evt_state.m_metaevt.m_info = &(g_infotables.m_event_info[PPME_SCAPEVENT_X]);
evt_state.m_metaevt.m_cpuid = 0;
evt_state.m_metaevt.m_evtnum = 0;
evt_state.m_metaevt.m_fdinfo = NULL;
}
void sinsp_parser::set_track_connection_status(bool enabled)
{
m_track_connection_status = enabled;
}
///////////////////////////////////////////////////////////////////////////////
// PROCESSING ENTRY POINT
///////////////////////////////////////////////////////////////////////////////
void sinsp_parser::process_event(sinsp_evt *evt)
{
uint16_t etype = evt->m_pevt->type;
bool is_live = m_inspector->is_live();
//
// Cleanup the event-related state
//
reset(evt);
//
// When debug mode is not enabled, filter out events about itself
//
#if defined(HAS_CAPTURE)
if(is_live && !m_inspector->is_debug_enabled())
{
if(evt->get_tid() == m_inspector->m_self_pid &&
etype != PPME_SCHEDSWITCH_1_E &&
etype != PPME_SCHEDSWITCH_6_E &&
etype != PPME_DROP_E &&
etype != PPME_DROP_X &&
etype != PPME_SCAPEVENT_E &&
etype != PPME_PROCINFO_E &&
etype != PPME_CPU_HOTPLUG_E &&
m_inspector->m_self_pid)
{
evt->m_filtered_out = true;
return;
}
}
#endif
//
// Filtering
//
bool do_filter_later = false;
if(m_inspector->m_filter)
{
ppm_event_flags eflags = evt->get_info_flags();
if(etype == PPME_SYSCALL_WRITE_X)
{
//
// Check if this is a tracer
//
sinsp_fdinfo_t* fdinfo = evt->m_fdinfo;
if(fdinfo == NULL && evt->m_tinfo != nullptr)
{
fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd);
evt->m_fdinfo = fdinfo;
}
if(fdinfo && (fdinfo->m_flags & (sinsp_fdinfo_t::FLAGS_IS_TRACER_FD | sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE)))
{
eflags = (ppm_event_flags)(((uint64_t)eflags) | EF_MODIFIES_STATE);
}
else
{
if(!m_inspector->is_live())
{
if((evt->get_dump_flags() & SCAP_DF_TRACER) != 0)
{
evt->m_fdinfo = NULL;
eflags = (ppm_event_flags)(((uint64_t)eflags) | EF_MODIFIES_STATE);
}
}
}
}
if(eflags & EF_MODIFIES_STATE)
{
do_filter_later = true;
}
else
{
if(m_inspector->run_filters_on_evt(evt) == false)
{
if(evt->m_tinfo != NULL)
{
if(!(eflags & EF_SKIPPARSERESET || etype == PPME_SCHEDSWITCH_6_E))
{
evt->m_tinfo->m_lastevent_type = PPM_EVENT_MAX;
}
}
evt->m_filtered_out = true;
return;
}
}
}
evt->m_filtered_out = false;
//
// Route the event to the proper function
//
switch(etype)
{
case PPME_SOCKET_SENDTO_E:
if((evt->m_fdinfo == nullptr) && (evt->m_tinfo != nullptr))
{
infer_sendto_fdinfo(evt);
}
// FALLTHRU
case PPME_SYSCALL_OPEN_E:
case PPME_SYSCALL_CREAT_E:
case PPME_SYSCALL_OPENAT_E:
case PPME_SYSCALL_OPENAT_2_E:
case PPME_SYSCALL_OPENAT2_E:
case PPME_SOCKET_SOCKET_E:
case PPME_SYSCALL_EVENTFD_E:
case PPME_SYSCALL_EVENTFD2_E:
case PPME_SYSCALL_CHDIR_E:
case PPME_SYSCALL_FCHDIR_E:
case PPME_SOCKET_SHUTDOWN_E:
case PPME_SYSCALL_GETRLIMIT_E:
case PPME_SYSCALL_SETRLIMIT_E:
case PPME_SYSCALL_PRLIMIT_E:
case PPME_SOCKET_SENDMSG_E:
case PPME_SYSCALL_SENDFILE_E:
case PPME_SYSCALL_SETRESUID_E:
case PPME_SYSCALL_SETRESGID_E:
case PPME_SYSCALL_SETUID_E:
case PPME_SYSCALL_SETGID_E:
case PPME_SYSCALL_SETPGID_E:
store_event(evt);
break;
case PPME_SYSCALL_EXECVE_18_E:
case PPME_SYSCALL_EXECVE_19_E:
case PPME_SYSCALL_EXECVEAT_E:
parse_execve_enter(evt);
break;
case PPME_SYSCALL_WRITE_E:
if(!m_inspector->m_is_dumping && evt->m_tinfo != nullptr)
{
evt->m_fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd);
if(evt->m_fdinfo)
{
if(evt->m_fdinfo->m_flags & sinsp_fdinfo_t::FLAGS_IS_TRACER_FD)
{
evt->m_filtered_out = true;
return;
}
}
}
break;
case PPME_SYSCALL_READ_X:
case PPME_SYSCALL_WRITE_X:
case PPME_SOCKET_RECV_X:
case PPME_SOCKET_SEND_X:
case PPME_SOCKET_RECVFROM_X:
case PPME_SOCKET_RECVMSG_X:
case PPME_SOCKET_SENDTO_X:
case PPME_SOCKET_SENDMSG_X:
case PPME_SYSCALL_READV_X:
case PPME_SYSCALL_WRITEV_X:
case PPME_SYSCALL_PREAD_X:
case PPME_SYSCALL_PWRITE_X:
case PPME_SYSCALL_PREADV_X:
case PPME_SYSCALL_PWRITEV_X:
parse_rw_exit(evt);
break;
case PPME_SYSCALL_SENDFILE_X:
parse_sendfile_exit(evt);
break;
case PPME_SYSCALL_OPEN_X:
case PPME_SYSCALL_CREAT_X:
case PPME_SYSCALL_OPENAT_X:
case PPME_SYSCALL_OPENAT_2_X:
case PPME_SYSCALL_OPENAT2_X:
case PPME_SYSCALL_OPEN_BY_HANDLE_AT_X:
parse_open_openat_creat_exit(evt);
break;
case PPME_SYSCALL_SELECT_E:
case PPME_SYSCALL_POLL_E:
case PPME_SYSCALL_PPOLL_E:
case PPME_SYSCALL_EPOLLWAIT_E:
parse_select_poll_epollwait_enter(evt);
break;
case PPME_SYSCALL_UNSHARE_E:
case PPME_SYSCALL_SETNS_E:
store_event(evt);
break;
case PPME_SYSCALL_UNSHARE_X:
case PPME_SYSCALL_SETNS_X:
parse_unshare_setns_exit(evt);
break;
case PPME_SYSCALL_CLONE_11_X:
case PPME_SYSCALL_CLONE_16_X:
case PPME_SYSCALL_CLONE_17_X:
case PPME_SYSCALL_CLONE_20_X:
case PPME_SYSCALL_FORK_X:
case PPME_SYSCALL_FORK_17_X:
case PPME_SYSCALL_FORK_20_X:
case PPME_SYSCALL_VFORK_X:
case PPME_SYSCALL_VFORK_17_X:
case PPME_SYSCALL_VFORK_20_X:
case PPME_SYSCALL_CLONE3_X:
parse_clone_exit(evt);
break;
case PPME_SYSCALL_EXECVE_8_X:
case PPME_SYSCALL_EXECVE_13_X:
case PPME_SYSCALL_EXECVE_14_X:
case PPME_SYSCALL_EXECVE_15_X:
case PPME_SYSCALL_EXECVE_16_X:
case PPME_SYSCALL_EXECVE_17_X:
case PPME_SYSCALL_EXECVE_18_X:
case PPME_SYSCALL_EXECVE_19_X:
case PPME_SYSCALL_EXECVEAT_X:
parse_execve_exit(evt);
break;
case PPME_PROCEXIT_E:
case PPME_PROCEXIT_1_E:
parse_thread_exit(evt);
break;
case PPME_SYSCALL_PIPE_X:
case PPME_SYSCALL_PIPE2_X:
parse_pipe_exit(evt);
break;
case PPME_SOCKET_SOCKET_X:
parse_socket_exit(evt);
break;
case PPME_SOCKET_BIND_X:
parse_bind_exit(evt);
break;
case PPME_SOCKET_CONNECT_E:
parse_connect_enter(evt);
break;
case PPME_SOCKET_CONNECT_X:
parse_connect_exit(evt);
break;
case PPME_SOCKET_ACCEPT_X:
case PPME_SOCKET_ACCEPT_5_X:
case PPME_SOCKET_ACCEPT4_X:
case PPME_SOCKET_ACCEPT4_5_X:
case PPME_SOCKET_ACCEPT4_6_X:
parse_accept_exit(evt);
break;
case PPME_SYSCALL_CLOSE_E:
parse_close_enter(evt);
break;
case PPME_SYSCALL_CLOSE_X:
parse_close_exit(evt);
break;
case PPME_SYSCALL_FCNTL_E:
parse_fcntl_enter(evt);
break;
case PPME_SYSCALL_FCNTL_X:
parse_fcntl_exit(evt);
break;
case PPME_SYSCALL_EVENTFD_X:
case PPME_SYSCALL_EVENTFD2_X:
parse_eventfd_exit(evt);
break;
case PPME_SYSCALL_CHDIR_X:
parse_chdir_exit(evt);
break;
case PPME_SYSCALL_FCHDIR_X:
parse_fchdir_exit(evt);
break;
case PPME_SYSCALL_GETCWD_X:
parse_getcwd_exit(evt);
break;
case PPME_SOCKET_SHUTDOWN_X:
parse_shutdown_exit(evt);
break;
case PPME_SYSCALL_DUP_X:
case PPME_SYSCALL_DUP_1_X:
case PPME_SYSCALL_DUP2_X:
case PPME_SYSCALL_DUP3_X:
parse_dup_exit(evt);
break;
case PPME_SYSCALL_SIGNALFD_X:
case PPME_SYSCALL_SIGNALFD4_X:
parse_single_param_fd_exit(evt, SCAP_FD_SIGNALFD);
break;
case PPME_SYSCALL_TIMERFD_CREATE_X:
parse_single_param_fd_exit(evt, SCAP_FD_TIMERFD);
break;
case PPME_SYSCALL_INOTIFY_INIT_X:
case PPME_SYSCALL_INOTIFY_INIT1_X:
parse_single_param_fd_exit(evt, SCAP_FD_INOTIFY);
break;
case PPME_SYSCALL_BPF_2_X:
parse_single_param_fd_exit(evt, SCAP_FD_BPF);
break;
case PPME_SYSCALL_USERFAULTFD_X:
parse_single_param_fd_exit(evt, SCAP_FD_USERFAULTFD);
break;
case PPME_SYSCALL_IO_URING_SETUP_X:
parse_single_param_fd_exit(evt, SCAP_FD_IOURING);
break;
case PPME_SYSCALL_EPOLL_CREATE_X:
case PPME_SYSCALL_EPOLL_CREATE1_X:
parse_single_param_fd_exit(evt, SCAP_FD_EVENTPOLL);
break;
case PPME_SYSCALL_GETRLIMIT_X:
case PPME_SYSCALL_SETRLIMIT_X:
parse_getrlimit_setrlimit_exit(evt);
break;
case PPME_SYSCALL_PRLIMIT_X:
parse_prlimit_exit(evt);
break;
case PPME_SOCKET_SOCKETPAIR_X:
parse_socketpair_exit(evt);
break;
case PPME_SCHEDSWITCH_1_E:
case PPME_SCHEDSWITCH_6_E:
parse_context_switch(evt);
break;
case PPME_SYSCALL_BRK_4_X:
case PPME_SYSCALL_MMAP_X:
case PPME_SYSCALL_MMAP2_X:
case PPME_SYSCALL_MUNMAP_X:
parse_brk_munmap_mmap_exit(evt);
break;
case PPME_SYSCALL_SETRESUID_X:
parse_setresuid_exit(evt);
break;
case PPME_SYSCALL_SETRESGID_X:
parse_setresgid_exit(evt);
break;
case PPME_SYSCALL_SETUID_X:
parse_setuid_exit(evt);
break;
case PPME_SYSCALL_SETGID_X:
parse_setgid_exit(evt);
break;
case PPME_CONTAINER_E:
parse_container_evt(evt); // deprecated, only here for backwards compatibility
break;
case PPME_CONTAINER_JSON_E:
case PPME_CONTAINER_JSON_2_E:
parse_container_json_evt(evt);
break;
case PPME_CPU_HOTPLUG_E:
parse_cpu_hotplug_enter(evt);
break;
#if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD)
case PPME_K8S_E:
if(!m_inspector->is_live())
{
parse_k8s_evt(evt);
}
break;
case PPME_MESOS_E:
if(!m_inspector->is_live())
{
parse_mesos_evt(evt);
}
break;
#endif // #if !defined(CYGWING_AGENT) && !defined(MINIMAL_BUILD)
case PPME_SYSCALL_CHROOT_X:
parse_chroot_exit(evt);
break;
case PPME_SYSCALL_SETSID_X:
parse_setsid_exit(evt);
break;
case PPME_SOCKET_GETSOCKOPT_X:
if(evt->get_num_params() > 0)
{
parse_getsockopt_exit(evt);
}
break;
case PPME_SYSCALL_CAPSET_X:
parse_capset_exit(evt);
break;
case PPME_USER_ADDED_E:
case PPME_USER_DELETED_E:
parse_user_evt(evt);
break;
case PPME_GROUP_ADDED_E:
case PPME_GROUP_DELETED_E:
parse_group_evt(evt);
break;
default:
break;
}
//
// With some state-changing events like clone, execve and open, we do the
// filtering after having updated the state
//
if(do_filter_later)
{
if(m_inspector->run_filters_on_evt(evt) == false)
{
evt->m_filtered_out = true;
return;
}
evt->m_filtered_out = false;
}
//
// Offline captures can produce events with the SCAP_DF_STATE_ONLY. They are
// supposed to go through the engine, but they must be filtered out before
// reaching the user.
//
if(!is_live)
{
if(evt->get_dump_flags() & SCAP_DF_STATE_ONLY)
{
evt->m_filtered_out = true;
}
}
// Check to see if the name changed as a side-effect of
// parsing this event. Try to avoid the overhead of a string
// compare for every event.
if(evt->m_fdinfo)
{
evt->set_fdinfo_name_changed(evt->m_fdinfo->m_name != evt->m_fdinfo->m_oldname);
}
}
void sinsp_parser::event_cleanup(sinsp_evt *evt)
{
if(evt->get_direction() == SCAP_ED_OUT &&
evt->m_tinfo && evt->m_tinfo->m_lastevent_data)
{
free_event_buffer(evt->m_tinfo->m_lastevent_data);
evt->m_tinfo->m_lastevent_data = NULL;
evt->m_tinfo->set_lastevent_data_validity(false);
}
}
///////////////////////////////////////////////////////////////////////////////
// HELPERS
///////////////////////////////////////////////////////////////////////////////
//
// Called before starting the parsing.
// Returns false in case of issues resetting the state.
//
bool sinsp_parser::reset(sinsp_evt *evt)
{
uint16_t etype = evt->get_type();
//
// Before anything can happen, the event needs to be
// initialized.
//
// For events created by the container resolvers and pushed
// onto the inspector's pending_container_events queue, the
// event has a threadinfo pointer that points to the process
// that created the container.
//
// However, for container events that are at the beginning of
// trace files and only describe the set of current
// containers, the threadinfo pointer is junk and must be
// cleared in init(). So only keep the threadinfo for "live"
// containers.
//
if (m_inspector->is_live() && (etype == PPME_CONTAINER_JSON_E || etype == PPME_CONTAINER_JSON_2_E) && evt->m_tinfo_ref != nullptr)
{
// this is a synthetic event generated by the container manager
// the threadinfo should already be set properly
evt->init_keep_threadinfo();
return true;
}
else
{
evt->init();
}
ppm_event_flags eflags = evt->get_info_flags();
evt->m_fdinfo = NULL;
evt->m_errorcode = 0;
//
// Ignore scheduler events
//
if(eflags & EF_SKIPPARSERESET)
{
if(etype == PPME_PROCINFO_E)
{
evt->m_tinfo = m_inspector->get_thread_ref(evt->m_pevt->tid, false, false).get();
}
else
{
evt->m_tinfo = NULL;
}
return false;
}
//
// Find the thread info
//
//
// If we're exiting a clone or if we have a scheduler event
// (many kernel thread), we don't look for /proc
//
bool query_os;
if(etype == PPME_SYSCALL_CLONE_11_X ||
etype == PPME_SYSCALL_CLONE_16_X ||
etype == PPME_SYSCALL_CLONE_17_X ||
etype == PPME_SYSCALL_CLONE_20_X ||
etype == PPME_SYSCALL_FORK_X ||
etype == PPME_SYSCALL_FORK_17_X ||
etype == PPME_SYSCALL_FORK_20_X ||
etype == PPME_SYSCALL_VFORK_X ||
etype == PPME_SYSCALL_VFORK_17_X ||
etype == PPME_SYSCALL_VFORK_20_X ||
etype == PPME_SYSCALL_CLONE3_X ||
etype == PPME_SCHEDSWITCH_6_E)
{
query_os = false;
}
else
{
query_os = true;
}
if(etype == PPME_CONTAINER_JSON_E ||
etype == PPME_CONTAINER_JSON_2_E ||
etype == PPME_USER_ADDED_E ||
etype == PPME_USER_DELETED_E ||
etype == PPME_GROUP_ADDED_E ||
etype == PPME_GROUP_DELETED_E)
{
evt->m_tinfo = nullptr;
return true;
}
else
{
evt->m_tinfo = m_inspector->get_thread_ref(evt->m_pevt->tid, query_os, false).get();
}
if(etype == PPME_SCHEDSWITCH_6_E)
{
return false;
}
if(!evt->m_tinfo)
{
if(etype == PPME_SYSCALL_CLONE_11_X ||
etype == PPME_SYSCALL_CLONE_16_X ||
etype == PPME_SYSCALL_CLONE_17_X ||
etype == PPME_SYSCALL_CLONE_20_X ||
etype == PPME_SYSCALL_FORK_X ||
etype == PPME_SYSCALL_FORK_17_X ||
etype == PPME_SYSCALL_FORK_20_X ||
etype == PPME_SYSCALL_VFORK_X ||
etype == PPME_SYSCALL_VFORK_17_X ||
etype == PPME_SYSCALL_VFORK_20_X ||
etype == PPME_SYSCALL_CLONE3_X)
{
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_thread_manager->m_failed_lookups->decrement();
#endif
}
else
{
ASSERT(false);
}
return false;
}
if(query_os)
{
evt->m_tinfo->m_flags |= PPM_CL_ACTIVE;
}
if(PPME_IS_ENTER(etype))
{
evt->m_tinfo->m_lastevent_fd = -1;
evt->m_tinfo->m_lastevent_type = etype;
if(eflags & EF_USES_FD)
{
sinsp_evt_param *parinfo;
//
// Get the fd.
// The fd is always the first parameter of the enter event.
//
parinfo = evt->get_param(0);
ASSERT(parinfo->m_len == sizeof(int64_t));
ASSERT(evt->get_param_info(0)->type == PT_FD);
evt->m_tinfo->m_lastevent_fd = *(int64_t *)parinfo->m_val;
evt->m_fdinfo = evt->m_tinfo->get_fd(evt->m_tinfo->m_lastevent_fd);
}
evt->m_tinfo->m_latency = 0;
evt->m_tinfo->m_last_latency_entertime = evt->get_ts();
}
else
{
sinsp_threadinfo* tinfo = evt->m_tinfo;
//
// event latency
//
if(tinfo->m_last_latency_entertime != 0)
{
tinfo->m_latency = evt->get_ts() - tinfo->m_last_latency_entertime;
ASSERT((int64_t)tinfo->m_latency >= 0);
}
if((etype==PPME_SYSCALL_EXECVE_18_X ||
etype==PPME_SYSCALL_EXECVE_19_X)
&&
tinfo->m_lastevent_type == PPME_SYSCALL_EXECVEAT_E)
{
tinfo->set_lastevent_data_validity(true);
}
else if(etype == tinfo->m_lastevent_type + 1)
{
tinfo->set_lastevent_data_validity(true);
}
else
{
tinfo->set_lastevent_data_validity(false);
if(tinfo->m_lastevent_type != PPME_TRACER_E)
{
return false;
}
}
//
// Error detection logic
//
if(evt->get_num_params() != 0 &&
((evt->m_info->params[0].name[0] == 'r' &&
evt->m_info->params[0].name[1] == 'e' &&
evt->m_info->params[0].name[2] == 's' &&
evt->m_info->params[0].name[3] == '\0') ||
(evt->m_info->params[0].name[0] == 'f' &&
evt->m_info->params[0].name[1] == 'd' &&
evt->m_info->params[0].name[2] == '\0')))
{
sinsp_evt_param *parinfo;
parinfo = evt->get_param(0);
ASSERT(parinfo->m_len == sizeof(int64_t));
int64_t res = *(int64_t *)parinfo->m_val;
if(res < 0)
{
evt->m_errorcode = -(int32_t)res;
}
}
//
// Retrieve the fd
//
if(eflags & EF_USES_FD)
{
//
// The copy_file_range syscall has the peculiarity of using two fds
// Set as m_lastevent_fd the output fd
//
if(etype == PPME_SYSCALL_COPY_FILE_RANGE_X)
{
sinsp_evt_param *parinfo;
parinfo = evt->get_param(1);
ASSERT(parinfo->m_len == sizeof(int64_t));
tinfo->m_lastevent_fd = *(int64_t *)parinfo->m_val;
}
evt->m_fdinfo = tinfo->get_fd(tinfo->m_lastevent_fd);
if(evt->m_fdinfo == NULL)
{
return false;
}
if(evt->m_errorcode != 0 && m_fd_listener)
{
m_fd_listener->on_error(evt);
}
if(evt->m_fdinfo->m_flags & sinsp_fdinfo_t::FLAGS_CLOSE_CANCELED)
{
//
// A close gets canceled when the same fd is created successfully between
// close enter and close exit.
// If that happens
//
erase_fd_params eparams;
evt->m_fdinfo->m_flags &= ~sinsp_fdinfo_t::FLAGS_CLOSE_CANCELED;
eparams.m_fd = CANCELED_FD_NUMBER;
eparams.m_fdinfo = tinfo->get_fd(CANCELED_FD_NUMBER);
//
// Remove the fd from the different tables
//
eparams.m_remove_from_table = true;
eparams.m_tinfo = tinfo;
eparams.m_ts = evt->get_ts();
erase_fd(&eparams);
}
}
}
return true;
}
void sinsp_parser::store_event(sinsp_evt *evt)
{
if(evt->m_tinfo == nullptr)
{
//
// No thread in the table. We won't store this event, which mean that
// we won't be able to parse the corresponding exit event and we'll have
// to drop the information it carries.
//
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_store_drops++;
#endif
return;
}
uint32_t elen;
//
// Make sure the event data is going to fit
//
elen = scap_event_getlen(evt->m_pevt);
if(elen > SP_EVT_BUF_SIZE)
{
ASSERT(false);
return;
}
//
// Copy the data
//
auto tinfo = evt->m_tinfo;
if(tinfo->m_lastevent_data == NULL)
{
tinfo->m_lastevent_data = reserve_event_buffer();
if(tinfo->m_lastevent_data == NULL)
{
throw sinsp_exception("cannot reserve event buffer in sinsp_parser::store_event.");
return;
}
}
memcpy(tinfo->m_lastevent_data, evt->m_pevt, elen);
tinfo->m_lastevent_cpuid = evt->get_cpuid();
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_stored_evts++;
#endif
}
bool sinsp_parser::retrieve_enter_event(sinsp_evt *enter_evt, sinsp_evt *exit_evt)
{
//
// Make sure there's a valid thread info
//
if(!exit_evt->m_tinfo)
{
return false;
}
//
// Retrieve the copy of the enter event and initialize it
//
if(!(exit_evt->m_tinfo->is_lastevent_data_valid() && exit_evt->m_tinfo->m_lastevent_data))
{
//
// This happen especially at the beginning of trace files, where events
// can be truncated
//
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_retrieve_drops++;
#endif
return false;
}
enter_evt->init(exit_evt->m_tinfo->m_lastevent_data, exit_evt->m_tinfo->m_lastevent_cpuid);
/* The `execveat` syscall is a wrapper of `execve`, when the call
* succeeds the event returned is simply an `execve` exit event.
* So if an `execveat` is correctly executed we will have, a
* `PPME_SYSCALL_EXECVEAT_E` as enter event and a
* `PPME_SYSCALL_EXECVE_..._X` as exit one. So when we retrieve
* the enter event in the `parse_execve_exit` method we cannot
* only check for the same syscall event, so `PPME_SYSCALL_EXECVE_..._E`,
* we have also to check for the `PPME_SYSCALL_EXECVEAT_E`.
*/
if((exit_evt->get_type() == PPME_SYSCALL_EXECVE_18_X ||
exit_evt->get_type() == PPME_SYSCALL_EXECVE_19_X)
&&
enter_evt->get_type() == PPME_SYSCALL_EXECVEAT_E)
{
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_retrieved_evts++;
#endif
return true;
}
//
// Make sure that we're using the right enter event, to prevent inconsistencies when events
// are dropped
//
if(enter_evt->get_type() != (exit_evt->get_type() - 1))
{
//ASSERT(false);
exit_evt->m_tinfo->set_lastevent_data_validity(false);
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_retrieve_drops++;
#endif
return false;
}
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_retrieved_evts++;
#endif
return true;
}
sinsp_protodecoder* sinsp_parser::add_protodecoder(std::string decoder_name)
{
//
// Make sure this decoder is not present yet
//
std::vector<sinsp_protodecoder*>::iterator it;
for(it = m_protodecoders.begin(); it != m_protodecoders.end(); ++it)
{
if((*it)->get_name() == decoder_name)
{
return (*it);
}
}
sinsp_protodecoder* nd = g_decoderlist.new_protodecoder_from_name(decoder_name,
m_inspector);
nd->init();
m_protodecoders.push_back(nd);
return nd;
}
void sinsp_parser::register_event_callback(sinsp_pd_callback_type etype, sinsp_protodecoder* dec)
{
switch(etype)
{
case CT_OPEN:
m_open_callbacks.push_back(dec);
break;
case CT_CONNECT:
m_connect_callbacks.push_back(dec);
break;
default:
ASSERT(false);
break;
}
return;
}
///////////////////////////////////////////////////////////////////////////////
// PARSERS
///////////////////////////////////////////////////////////////////////////////
void sinsp_parser::parse_clone_exit(sinsp_evt *evt)
{
sinsp_evt_param* parinfo;