-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevents.c
2035 lines (1740 loc) · 54.8 KB
/
events.c
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 PolySat, California Polytechnic State University, San Luis Obispo. [email protected]
* This file is part of libproc, a PolySat library.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file events.c Handles events.
*
* @author Dr. Bellardo
* @author Greg Eddington
*/
#include "events.h"
#include "priorityQueue.h"
#include "eventTimer.h"
#include "proclib.h"
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <sys/select.h>
#include <signal.h>
#include <assert.h>
#include <strings.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "debug.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "ipc.h"
#include "json.h"
#include <inttypes.h>
#include "pseudo_threads.h"
#define EDBG_ENV_VAR "LIBPROC_DEBUGGER"
#define EDBG_VCLK_ENV_VAR "LIBPROC_DEBUGGER_VCLK"
#define EDBG_GVCLK_ENV_VAR "LIBPROC_DEBUGGER_GVCLK"
#define RESP_WAIT_MS 300
// Structure representing a schedule callback
typedef struct _ScheduleCB
{
struct timeval scheduleTime;
struct timeval nextAwake;
EVT_sched_cb callback;
void *arg;
size_t pos;
struct timeval timeStep;
ps_pqueue_t *queue;
uint32_t count;
char breakpoint;
char critical;
char inCallback;
char name[128];
} ScheduleCB;
// Structure which defines a file callback
typedef struct EventCB
{
EVT_fd_cb cb[EVENT_MAX]; // An array of function callbacks to call
EVT_fd_cb cleanup[EVENT_MAX]; // An array of cleanup callback to call
void *arg[EVENT_MAX]; // An array of arguments to pass to callbacks
uint32_t counts[EVENT_MAX];
char breakpoint[EVENT_MAX];
char inCallback[EVENT_MAX];
char pausable;
char critical;
int fd; // The file descriptor which will launch the event
char name[128];
struct EventCB *next; // The next signal callback
} *EventCBPtr;
struct GPIOInterruptCBList {
EVT_sched_cb cb;
void *arg;
struct GPIOInterruptCBList *next;
};
struct GPIOInterruptDesc {
const char *filename;
int fd, tripped;
struct GPIOInterruptCBList *callbacks;
};
struct EDBGClient {
int fd;
EVTHandler *ctx;
struct sockaddr_in addr;
char *rxbuff;
size_t rxlen, rxmax;
struct EDBGClient *next;
};
struct DeferredEvent {
EVT_sched_cb cb;
void *arg;
struct DeferredEvent *next;
};
// A structure which contains information regarding the state of the event handler
struct EventState
{
fd_set eventSet[EVENT_MAX]; // File descriptor sets to watch
fd_set blockedSet[EVENT_MAX]; // File descriptor sets to watch
int maxFd, maxFds[EVENT_MAX], eventCnt[EVENT_MAX]; // fd information
int hashSize; // The hash size of the event handler
int keepGoing; // Whether the handler should loop or not
struct GPIOInterruptDesc gpio_intrs[2]; // GPIO interrupt state
ps_pqueue_t *queue, *dbg_queue; // The schedule queue
struct EventTimer *evt_timer;
char custom_timer;
enum EVTDebuggerState initialDebuggerState;
enum EVTDebuggerState debuggerState;
int dbgPort;
struct ZMQLServer *dbgServer;
struct IPCBuffer *dbgBuffer;
unsigned long long loop_counter;
unsigned long long timed_event_counter;
unsigned long long fd_event_counter;
int steps_to_break;
EVT_debug_state_cb debuggerStateCB;
void *debuggerStateArg;
ScheduleCB *next_timed_event;
EventCBPtr next_fd_event;
int next_fd_event_evt;
int dbg_step;
void *dump_evt;
void *dbg_reply_evt;
void *breakpoint_evt;
ScheduleCB null_evt;
long critical_sched_count, critical_fd_count;
uint8_t break_on_next:1;
uint8_t dump_every_loop:1;
uint8_t full_dump_format:1;
uint8_t in_loop:1;
struct DeferredEvent *deferred;
int (*cmds_pending)(void*);
void *cmds_pending_arg;
// MUST be last entry in struct
EventCBPtr events[1]; // List of pointers to event callbacks
};
// Static global for virtual time
static EVTHandler *global_evt = NULL;
static void edbg_init(EVTHandler *ctx);
static void edbg_report_state(EVTHandler *ctx, uint8_t full_format);
void evt_fd_set_pausable(EVTHandler *ctx, int fd, char pausable);
extern int ET_default_monotonic(struct EventTimer *et, struct timeval *tv);
extern char EVT_sched_move_to_mono(EVTHandler *handler, void *eventId);
extern void EVT_sched_make_breakpoint(EVTHandler *handler, void *eventId);
void evt_fd_set_paused(EVTHandler *ctx, int fd, char paused);
#ifndef timercmp
# define timercmp(a, b, CMP) \
(((a)->tv_sec == (b)->tv_sec) ? \
((a)->tv_usec CMP (b)->tv_usec) : \
((a)->tv_sec CMP (b)->tv_sec))
#endif
#ifndef timeradd
#define timeradd(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
if ((vvp)->tv_usec >= 1000000) { \
(vvp)->tv_sec++; \
(vvp)->tv_usec -= 1000000; \
} \
} while (0)
#endif
#ifndef timersub
#define timersub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
if ((vvp)->tv_usec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \
} \
} while (0)
#endif
#ifndef FD_COPY
#define FD_COPY(f, t) bcopy(f, t, sizeof(*(f)))
#endif
// Subracts y timeval struct from x timeval struct and stores the result.
int timeval_subtract(struct timeval *result, struct timeval *x,
struct timeval *y)
{
int nsec;
if (x->tv_usec < 0){
return 1;
}
if (y->tv_usec < 0){
return 1;
}
// Perform the carry for the later subtraction by updating y.
if (x->tv_usec < y->tv_usec){
nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000){
nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
// Compute the time remaining to wait. tv_usec is certainly positive.
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
// Return 1 if result is negative.
return x->tv_sec < y->tv_sec;
}
// Compare priority callback
static int cmp_pri(struct timeval next, struct timeval curr)
{
return timercmp(&next, &curr, >=);
}
// Get priority callback
static struct timeval get_pri(void *a)
{
return ((ScheduleCB *) a)->nextAwake;
}
// Set priority callback
static void set_pri(void *a, struct timeval pri)
{
((ScheduleCB *) a)->nextAwake = pri;
}
// Get position callback
static size_t get_pos(void *a)
{
return ((ScheduleCB *) a)->pos;
}
// Set position callback
static void set_pos(void *a, size_t pos)
{
((ScheduleCB *) a)->pos = pos;
}
int null_evt_callback(void *arg)
{
return EVENT_REMOVE;
}
void EVT_set_cmds_pending(struct EventState *state, int (*cb)(void*), void *arg)
{
state->cmds_pending = cb;
state->cmds_pending_arg = arg;
}
/* Initializes an EventState with a given hash size.
* @param hashSize The hash size of the event handler.
* @return A pointer to the new EventState
*/
struct EventState *EVT_initWithSize(int hashSize, EVT_debug_state_cb debug_cb,
void *arg)
{
struct EventState *res = NULL;
int i;
const char *dbg_state;
res = (struct EventState*)malloc(
sizeof(struct EventState) + hashSize * sizeof(EventCBPtr));
if (!res)
return NULL;
memset(res, 0, sizeof(struct EventState));
memset(&res->gpio_intrs, 0, sizeof(res->gpio_intrs));
res->debuggerStateCB = debug_cb;
res->debuggerStateArg = arg;
// Configure the initial debugger state
dbg_state = getenv(EDBG_ENV_VAR);
res->initialDebuggerState = EDBG_DISABLED;
res->debuggerState = EDBG_DISABLED;
if (dbg_state) {
if (!strcasecmp(dbg_state, "ENABLED"))
res->initialDebuggerState = EDBG_ENABLED;
else if (!strcasecmp(dbg_state, "STOPPED"))
res->initialDebuggerState = EDBG_STOPPED;
}
if (res->initialDebuggerState != EDBG_DISABLED)
res->debuggerState = EDBG_ENABLED;
res->keepGoing = 1;
for ( i = 0; i < EVENT_MAX; i++) {
FD_ZERO(&res->eventSet[i]);
FD_ZERO(&res->blockedSet[i]);
res->maxFds[i] = 0;
res->eventCnt[i] = 0;
}
res->hashSize = hashSize;
res->maxFd = 0;
for (i = 0; i < res->hashSize; i++)
res->events[i] = NULL;
res->queue = ps_pqueue_init(hashSize, cmp_pri, get_pri, set_pri,
get_pos, set_pos);
if (res->queue == NULL){
free(res);
return NULL;
}
res->dbg_queue = ps_pqueue_init(hashSize, cmp_pri, get_pri, set_pri,
get_pos, set_pos);
if (res->dbg_queue == NULL){
free(res->queue);
free(res);
return NULL;
}
res->evt_timer = ET_default_init();
if (!res->evt_timer) {
free(res->queue);
free(res);
return NULL;
}
DBG_set_timer(res->evt_timer);
global_evt = res;
res->loop_counter = 0;
res->timed_event_counter = 0;
res->fd_event_counter = 0;
res->steps_to_break = 0;
res->next_timed_event = NULL;
res->next_fd_event = NULL;
res->custom_timer = 0;
res->dump_evt = NULL;
res->dbg_reply_evt = NULL;
res->breakpoint_evt = NULL;
res->dump_every_loop = 0;
res->full_dump_format = 1;
res->dbg_step = 0;
memset(&res->null_evt, 0, sizeof(res->null_evt));
res->null_evt.callback = null_evt_callback;
return res;
}
/* Initializes an EventState with a hash size of 19
* @return A pointer to the new EventState
*/
EVTHandler *EVT_create_handler(EVT_debug_state_cb debug_cb, void *arg)
{
return (EVTHandler *)EVT_initWithSize(19, debug_cb, arg);
}
struct timeval EVT_sched_remaining(EVTHandler *handler, void *eventId)
{
ScheduleCB *evt = (ScheduleCB*)eventId;
struct timeval now, result;
handler->evt_timer->get_monotonic_time(handler->evt_timer, &now);
timersub(&evt->nextAwake, &now, &result);
return result;
}
static int EVT_remove_internal(struct EventState *ctx, struct EventCB **curr,
int event)
{
struct EventCB *tmp;
int i, deleteIt = 1;
if (!curr || !*curr || !(*curr)->cb[event]){
return 0;
}
tmp = *curr;
if (tmp->cleanup[event]){
(*tmp->cleanup[event])(-1, event, tmp->arg[event]);
}
ctx->eventCnt[event]--;
tmp->cb[event] = NULL;
tmp->cleanup[event] = NULL;
tmp->arg[event] = NULL;
FD_CLR(tmp->fd, &ctx->eventSet[event]);
FD_CLR(tmp->fd, &ctx->blockedSet[event]);
if (tmp->fd == ctx->maxFds[event]) {
ctx->maxFds[event] = 0;;
if (ctx->eventCnt[event] > 0) {
//Find next highest
for(i = 1; i < tmp->fd; i++)
if (FD_ISSET(i, &ctx->eventSet[event])){
ctx->maxFds[event] = i;
}
ctx->maxFd = ctx->maxFds[0];
for (i = 1; i < EVENT_MAX; i++){
if (ctx->maxFds[i] > ctx->maxFd){
ctx->maxFd = ctx->maxFds[i];
}
}
}
}
for(i = 0; i < EVENT_MAX; i++){
if (tmp->cb[i]){
deleteIt = 0;
}
}
if (deleteIt) {
if (tmp->critical)
ctx->critical_fd_count--;
*curr = tmp->next;
free(tmp);
}
return deleteIt;
}
void EVT_free_handler(EVTHandler *ctx)
{
int i, event;
ScheduleCB *curProc;
struct DeferredEvent *def;
if (!ctx)
return;
while ((def = ctx->deferred)) {
ctx->deferred = def->next;
if (def->cb)
def->cb(def->arg);
free(def);
}
if (ctx->dbgBuffer)
ipc_destroy_buffer(&ctx->dbgBuffer);
if (ctx->dbgServer)
zmql_destroy_tcp_server(&ctx->dbgServer);
if (ctx->evt_timer) {
DBG_set_timer(NULL);
ctx->evt_timer->cleanup(ctx->evt_timer);
}
global_evt = NULL;
if (ctx->breakpoint_evt)
EVT_sched_remove(ctx, ctx->breakpoint_evt);
if (ctx->dump_evt)
EVT_sched_remove(ctx, ctx->dump_evt);
if (ctx->dbg_reply_evt)
EVT_sched_remove(ctx, ctx->dbg_reply_evt);
for(i = 0; i < ctx->hashSize; i++){
while(ctx->events[i]){
for(event = 0; event < EVENT_MAX; event++){
EVT_remove_internal(ctx, &ctx->events[i], event);
}
}
}
while ((curProc = ps_pqueue_peek(ctx->queue))) {
ps_pqueue_pop(ctx->queue);
// Call the callback and see if it wants to be kept
if (curProc != &ctx->null_evt)
free(curProc);
}
while ((curProc = ps_pqueue_peek(ctx->dbg_queue))) {
ps_pqueue_pop(ctx->dbg_queue);
// Call the callback and see if it wants to be kept
if (curProc != &ctx->null_evt)
free(curProc);
}
ps_pqueue_free(ctx->queue);
ps_pqueue_free(ctx->dbg_queue);
free(ctx);
}
void EVT_fd_remove(EVTHandler *ctx, int fd, int event)
{
struct EventCB **curr;
for (curr = &ctx->events[fd % ctx->hashSize]; *curr; )
if ((*curr)->fd == fd && !(*curr)->inCallback[event]) {
if (!EVT_remove_internal(ctx, curr, event))
curr = &(*curr)->next;
}
else {
if ((*curr)->fd == fd && (*curr)->inCallback[event])
DBG_print(DBG_LEVEL_WARN, "Calling EVT_fd_remove within a fd "
"event callback is a bug. See EVT_fd_force_remove as an "
"alternative");
curr = &(*curr)->next;
}
}
void EVT_fd_force_remove(EVTHandler *ctx, int fd, int event)
{
struct EventCB **curr;
for (curr = &ctx->events[fd % ctx->hashSize]; *curr; ) {
if ((*curr)->fd == fd && !(*curr)->inCallback[event]) {
if (!EVT_remove_internal(ctx, curr, event))
curr = &(*curr)->next;
}
else {
if ((*curr)->fd == fd && (*curr)->inCallback[event])
(*curr)->inCallback[event] = 3;
curr = &(*curr)->next;
}
}
}
char EVT_fd_add(EVTHandler *ctx, int fd, int event, EVT_fd_cb cb, void *p)
{
return EVT_fd_add_with_cleanup(ctx, fd, event, cb, NULL, p);
}
char EVT_fd_add_with_cleanup(EVTHandler *ctx, int fd, int event,
EVT_fd_cb cb, EVT_fd_cb cleanup_cb, void *p)
{
struct EventCB *curr, **currP, *tail;
int i;
if (!cb) {
EVT_fd_remove(ctx, fd, event);
return 0;
}
for (currP = &ctx->events[fd % ctx->hashSize];
*currP && (*currP)->fd != fd;
currP = &(*currP)->next)
;
curr = *currP;
if (!curr) {
curr = (struct EventCB*)malloc(sizeof(struct EventCB));
if (!curr)
return -1;
memset(curr, 0, sizeof(*curr));
curr->critical = 1;
curr->pausable = 1;
curr->fd = fd;
// Add to end of linked list to avoid corruption when adding a callback
// from within another callback in the same collision bucket
for (tail = ctx->events[fd % ctx->hashSize]; tail && tail->next;
tail = tail->next);
if (!tail)
ctx->events[fd % ctx->hashSize] = curr;
else
tail->next = curr;
ctx->critical_fd_count++;
}
if (!curr->cb[event])
ctx->eventCnt[event]++;
else if (curr->cb[event] != cb || curr->arg[event] != p) {
const char *ename = "READ";
if (event == EVENT_FD_WRITE)
ename = "WRITE";
if (event == EVENT_FD_ERROR)
ename = "ERROR";
DBG_print(DBG_LEVEL_WARN, "Warning: Only one event handler can be "
"registered for a fd at a time. Overwriting event %s (%d) on "
"fd %d. %p:%p -> %p:%p\n", ename,
event, curr->fd, curr->cb[event], curr->arg[event], cb, p);
if (curr->cleanup[event] && curr->arg[event] != p)
(*curr->cleanup[event])(-1, event, curr->arg[event]);
}
curr->cb[event] = cb;
curr->cleanup[event] = cleanup_cb;
curr->arg[event] = p;
if (curr->inCallback[event])
curr->inCallback[event] = 2;
else {
FD_SET(fd, &ctx->eventSet[event]);
if (!curr->pausable || !curr->breakpoint[event])
FD_SET(fd, &ctx->blockedSet[event]);
if (fd > ctx->maxFds[event]){
ctx->maxFds[event] = fd;
}
ctx->maxFd = ctx->maxFds[0];
for (i = 1; i < EVENT_MAX; i++)
if (ctx->maxFds[i] > ctx->maxFd)
ctx->maxFd = ctx->maxFds[i];
}
return 1;
}
static int EVT_clean_fdsets(struct EventState *ctx)
{
fd_set testSet;
int i, evt, used, res, max;
struct timeval poll;
int foundOne = 0;
DBG_print(DBG_LEVEL_WARN, "Warning: cleaning file descriptors is a slow process. "
"Remove them when you are done instead!\n");
max = ctx->maxFd + 1;
for (i = 0; i < max; i++) {
used = 0;
for (evt = 0; evt < EVENT_MAX; evt++)
used = used || FD_ISSET(i, &ctx->eventSet[evt]);
if (!used){
continue;
}
FD_ZERO(&testSet);
FD_SET(i, &testSet);
poll.tv_sec = 0;
poll.tv_usec = 0;
res = select(i + 1, &testSet, NULL, NULL, &poll);
if (res == -1 && errno == EBADF) {
foundOne = 1;
fprintf(stderr, "\tFound fd %d for events: ", i);
if (FD_ISSET(i, &ctx->eventSet[EVENT_FD_READ]))
fprintf(stderr, "read ");
if (FD_ISSET(i, &ctx->eventSet[EVENT_FD_WRITE]))
fprintf(stderr, "write ");
if (FD_ISSET(i, &ctx->eventSet[EVENT_FD_ERROR]))
fprintf(stderr, "error ");
fprintf(stderr, "\n");
EVT_fd_remove(ctx, i, EVENT_FD_READ);
EVT_fd_remove(ctx, i, EVENT_FD_WRITE);
EVT_fd_remove(ctx, i, EVENT_FD_ERROR);
}
}
return foundOne;
}
/**
* Enable libproc virtual clock. Sets EventTimer to a new
* instance of a VirtualEventTimer.
*
* @param ctx EVTHandler struct
* @param tv A pointer to the timeval with the desired initial time.
*/
char EVT_enable_virt(EVTHandler *ctx, struct timeval *initTime)
{
struct EventTimer *et;
assert(ctx);
et = ET_virt_init(initTime);
if (!et)
return -1;
EVT_set_evt_timer(ctx, et);
DBG_set_timer(et);
return 0;
}
/**
* Enable libproc global virtual clock. Sets EventTimer to a new
* instance of a GlobalVirtualEventTimer.
*
* @param ctx EVTHandler struct
* @param path The file system path to the file used to synchronize between
* processes
* @param pauseMode The pause mode to initialize the timer with
*/
char EVT_enable_gvirt(EVTHandler *ctx, const char *path, char pauseMode)
{
struct EventTimer *et;
assert(ctx);
et = ET_gvirt_init(path, pauseMode);
if (!et)
return -1;
EVT_set_evt_timer(ctx, et);
DBG_set_timer(et);
return 0;
}
/**
* Get current libproc EventTimer.
*
* @param ctx EVTHandler struct
* @param et The EventTimer instance.
*/
struct EventTimer *EVT_get_evt_timer(EVTHandler *ctx)
{
return ctx->evt_timer;
}
/**
* Set libproc EventTimer.
*
* @param ctx EVTHandler struct
* @param et The EventTimer instance.
*/
void EVT_set_evt_timer(EVTHandler *ctx, struct EventTimer *et)
{
assert(ctx);
assert(et);
if (ctx->evt_timer)
ctx->evt_timer->cleanup(ctx->evt_timer);
ctx->evt_timer = et;
ctx->custom_timer = 1;
DBG_set_timer(et);
}
/**
* Get the system's current absolute GMT time.
*
* @param tv A pointer to the timeval structure where the time gets stored
*
*/
int EVT_get_gmt_time(EVTHandler *ctx, struct timeval *tv)
{
return ctx->evt_timer->get_gmt_time(ctx->evt_timer, tv);
}
/**
* USE ONLY WHEN ABSOLUTELY NECESSARY!
*
* Stateless version of EVT_get_gmt_time. Get the system's current
* absolute GMT time without passing a EvtHander context. Use
* only when absolutely necessary!
*
* @param tv A pointer to the timeval structure where the time gets stored
*/
int EVT_get_gmt_time_virt(struct timeval *tv)
{
return global_evt->evt_timer->get_gmt_time(global_evt->evt_timer, tv);
}
/**
* Get the system's current time since an unknown reference point. This
* increases monotonically despite any changes to the system's current
* understanding of GMT.
*
* @param tv A pointer to the timeval structure where the time gets stored
*/
int EVT_get_monotonic_time(EVTHandler *ctx, struct timeval *tv)
{
return ctx->evt_timer->get_monotonic_time(ctx->evt_timer, tv);
}
static void edbg_breakpoint(EVTHandler *ctx)
{
ctx->debuggerState = EDBG_STOPPED;
ctx->break_on_next = 0;
edbg_report_state(ctx, ctx->full_dump_format);
}
static int evt_process_timed_event(EVTHandler *ctx,
ScheduleCB *curProc, struct timeval curTime, int stepping)
{
if (!stepping && (ctx->break_on_next || curProc->breakpoint) ) {
if (--ctx->steps_to_break <= 0) {
ctx->next_timed_event = curProc;
edbg_breakpoint(ctx);
return 0;
}
}
// Pop event from the queue
ctx->timed_event_counter++;
curProc->count++;
// Call the callback and see if it wants to be kept
curProc->inCallback = 1;
if (curProc->callback(curProc->arg) == EVENT_KEEP) {
if (curProc->inCallback == 1) {
curProc->scheduleTime = curTime;
timeradd(&curProc->nextAwake,
&curProc->timeStep, &curProc->nextAwake);
}
curProc->inCallback = 0;
ps_pqueue_insert(curProc->queue, curProc);
} else {
if (curProc->critical)
ctx->critical_sched_count--;
if (curProc != &ctx->null_evt) {
free(curProc);
}
}
return 1;
}
int evt_process_fd_event(EVTHandler *ctx, struct EventCB **evtCurr, int event,
int stepping)
{
int keep = EVENT_KEEP;
if (!evtCurr || !*evtCurr)
return 1;
if (!stepping && (*evtCurr)->pausable &&
(ctx->break_on_next || (*evtCurr)->breakpoint[event])) {
if (--ctx->steps_to_break <= 0) {
ctx->next_fd_event = *evtCurr;
ctx->next_fd_event_evt = event;
edbg_breakpoint(ctx);
return 0;
}
}
if ((*evtCurr)->cb[event]) {
(*evtCurr)->counts[event]++;
(*evtCurr)->inCallback[event] = 1;
keep = (*(*evtCurr)->cb[event])((*evtCurr)->fd, event,
(*evtCurr)->arg[event]);
ctx->fd_event_counter++;
}
if ((*evtCurr)->inCallback[event] == 3 ||
(EVENT_REMOVE == keep && (*evtCurr)->inCallback[event] == 1)) {
(*evtCurr)->inCallback[event] = 0;
EVT_remove_internal(ctx, evtCurr, event);
}
else
(*evtCurr)->inCallback[event] = 0;
return 1;
}
struct EVT_select_cb_args {
fd_set *eventSetPtrs[EVENT_MAX];
int maxFd;
struct timeval *mono_to;
};
static int select_event_loop_cb(struct EventTimer *et,
struct timeval *nextAwake, void *opaque)
{
struct EVT_select_cb_args *args = (struct EVT_select_cb_args*)opaque;
struct timeval *to = nextAwake, now, diff;
if (args->mono_to) {
ET_default_monotonic(NULL, &now);
if (timercmp(&now, args->mono_to, >=))
diff.tv_sec = diff.tv_usec = 0;
else
timersub(args->mono_to, &now, &diff);
if (!to || timercmp(&diff, to, <))
to = &diff;
}
return select(args->maxFd, args->eventSetPtrs[EVENT_FD_READ],
args->eventSetPtrs[EVENT_FD_WRITE],
args->eventSetPtrs[EVENT_FD_ERROR], to);
}
static int edbg_response_timeout(void *arg)
{
EVTHandler *ctx = (EVTHandler*)arg;
ctx->dbg_reply_evt = NULL;
return EVENT_KEEP;
}
char EVT_start_loop(EVTHandler *ctx)
{
return EVT_start_loop_auto_exit(ctx, EVT_NEVER_EXIT);
}
char EVT_start_loop_auto_exit(EVTHandler *ctx, int auto_exit)
{
fd_set eventSets[EVENT_MAX];
struct EVT_select_cb_args args;
int i;
int retval;
int event, fd;
struct EventCB **evtCurr;
int startEvent = EVENT_FD_READ;
int startFd = 0;
struct timeval curTime, *nextAwake;
ScheduleCB *curProc;
int time_paused = 0;
int fd_paused = 0;
int real_event;
int remaining_work;
struct DeferredEvent *def;
ctx->in_loop = 1;
ctx->keepGoing = 1;
ctx->break_on_next = ctx->initialDebuggerState == EDBG_STOPPED;
edbg_init(ctx);
while(ctx->keepGoing) {
PT_run_all();
if (!ctx->keepGoing)
break;
real_event = 0;
remaining_work = 0;
// Process any single-step events
if (ctx->dbg_step && ctx->next_timed_event) {
ctx->evt_timer->get_monotonic_time(ctx->evt_timer, &curTime);
evt_process_timed_event(ctx, ctx->next_timed_event, curTime, 1);
ctx->next_timed_event = NULL;
ctx->debuggerState = EDBG_ENABLED;
real_event = 1;
}
if (ctx->dbg_step && ctx->next_fd_event) {
evt_process_fd_event(ctx, &ctx->next_fd_event,
ctx->next_fd_event_evt, 1);
ctx->next_fd_event = NULL;
ctx->debuggerState = EDBG_ENABLED;
real_event = 1;
}
ctx->dbg_step = 0;
time_paused = fd_paused = ctx->next_timed_event || ctx->next_fd_event ||
ctx->dbg_reply_evt;
for (i = 0; i < EVENT_MAX; i++) {
if (ctx->eventCnt[i] > 0) {
args.eventSetPtrs[i] = &eventSets[i];
if (fd_paused)
memcpy(args.eventSetPtrs[i], &ctx->blockedSet[i],
sizeof(*(&ctx->blockedSet[i])));
else
memcpy(args.eventSetPtrs[i], &ctx->eventSet[i],
sizeof(*(&ctx->eventSet[i])));
}
else
args.eventSetPtrs[i] = NULL;
}
args.maxFd = ctx->maxFd + 1;
args.mono_to = NULL;
curProc = ps_pqueue_peek(ctx->queue);
if (!time_paused && curProc)
nextAwake = &curProc->nextAwake;
else
nextAwake = NULL;
curProc = ps_pqueue_peek(ctx->dbg_queue);
if (curProc)
args.mono_to = &curProc->nextAwake;
// Check to see if we need to auto-stop the event loop
if (ctx->critical_sched_count > 0)
remaining_work |= EVT_EXIT_SCHED;
if (ctx->critical_fd_count > 0)
remaining_work |= EVT_EXIT_FD;
if (auto_exit && !(auto_exit & remaining_work)) {
ctx->keepGoing = 0;
break;
}
// Call blocking function of event timer
retval = ctx->evt_timer->block(ctx->evt_timer, nextAwake, time_paused,
&select_event_loop_cb, &args);
// Process Timed Events
while (!time_paused && (curProc = ps_pqueue_peek(ctx->queue))) {
ctx->evt_timer->get_monotonic_time(ctx->evt_timer, &curTime);
if (timercmp(&curProc->nextAwake, &curTime, >)) {