forked from haproxy/haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.c
1207 lines (1021 loc) · 35.7 KB
/
debug.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
/*
* Process debugging functions.
*
* Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
*/
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <haproxy/api.h>
#include <haproxy/buf.h>
#include <haproxy/cli.h>
#include <haproxy/debug.h>
#include <haproxy/fd.h>
#include <haproxy/global.h>
#include <haproxy/hlua.h>
#include <haproxy/log.h>
#include <haproxy/net_helper.h>
#include <haproxy/stream_interface.h>
#include <haproxy/task.h>
#include <haproxy/thread.h>
#include <haproxy/tools.h>
#include <import/ist.h>
/* mask of threads still having to dump, used to respect ordering. Only used
* when USE_THREAD_DUMP is set.
*/
volatile unsigned long threads_to_dump = 0;
unsigned int debug_commands_issued = 0;
/* dumps a backtrace of the current thread that is appended to buffer <buf>.
* Lines are prefixed with the string <prefix> which may be empty (used for
* indenting). It is recommended to use this at a function's tail so that
* the function does not appear in the call stack. The <dump> argument
* indicates what dump state to start from, and should usually be zero. It
* may be among the following values:
* - 0: search usual callers before step 1, or directly jump to 2
* - 1: skip usual callers before step 2
* - 2: dump until polling loop, scheduler, or main() (excluded)
* - 3: end
* - 4-7: like 0 but stops *after* main.
*/
void ha_dump_backtrace(struct buffer *buf, const char *prefix, int dump)
{
struct buffer bak;
char pfx2[100];
void *callers[100];
int j, nptrs;
const void *addr;
nptrs = my_backtrace(callers, sizeof(callers)/sizeof(*callers));
if (!nptrs)
return;
if (snprintf(pfx2, sizeof(pfx2), "%s| ", prefix) > sizeof(pfx2))
pfx2[0] = 0;
/* The call backtrace_symbols_fd(callers, nptrs, STDOUT_FILENO would
* produce similar output to the following:
*/
chunk_appendf(buf, "%scall trace(%d):\n", prefix, nptrs);
for (j = 0; (j < nptrs || (dump & 3) < 2); j++) {
if (j == nptrs && !(dump & 3)) {
/* we failed to spot the starting point of the
* dump, let's start over dumping everything we
* have.
*/
dump += 2;
j = 0;
}
bak = *buf;
dump_addr_and_bytes(buf, pfx2, callers[j], 8);
addr = resolve_sym_name(buf, ": ", callers[j]);
if ((dump & 3) == 0) {
/* dump not started, will start *after*
* ha_thread_dump_all_to_trash, ha_panic and ha_backtrace_to_stderr
*/
if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
addr == ha_backtrace_to_stderr)
dump++;
*buf = bak;
continue;
}
if ((dump & 3) == 1) {
/* starting */
if (addr == ha_thread_dump_all_to_trash || addr == ha_panic ||
addr == ha_backtrace_to_stderr) {
*buf = bak;
continue;
}
dump++;
}
if ((dump & 3) == 2) {
/* still dumping */
if (dump == 6) {
/* we only stop *after* main and we must send the LF */
if (addr == main) {
j = nptrs;
dump++;
}
}
else if (addr == run_poll_loop || addr == main || addr == run_tasks_from_lists) {
dump++;
*buf = bak;
break;
}
}
/* OK, line dumped */
chunk_appendf(buf, "\n");
}
}
/* dump a backtrace of current thread's stack to stderr. */
void ha_backtrace_to_stderr()
{
char area[2048];
struct buffer b = b_make(area, sizeof(area), 0, 0);
ha_dump_backtrace(&b, " ", 4);
if (b.data)
DISGUISE(write(2, b.area, b.data));
}
/* Dumps to the buffer some known information for the desired thread, and
* optionally extra info for the current thread. The dump will be appended to
* the buffer, so the caller is responsible for preliminary initializing it.
* The calling thread ID needs to be passed in <calling_tid> to display a star
* in front of the calling thread's line (usually it's tid). Any stuck thread
* is also prefixed with a '>'.
*/
void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
{
unsigned long thr_bit = 1UL << thr;
unsigned long long p = ha_thread_info[thr].prev_cpu_time;
unsigned long long n = now_cpu_time_thread(&ha_thread_info[thr]);
int stuck = !!(ha_thread_info[thr].flags & TI_FL_STUCK);
chunk_appendf(buf,
"%c%cThread %-2u: id=0x%llx act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
" stuck=%d prof=%d",
(thr == calling_tid) ? '*' : ' ', stuck ? '>' : ' ', thr + 1,
ha_get_pthread_id(thr),
thread_has_tasks(),
!!(global_tasks_mask & thr_bit),
!eb_is_empty(&task_per_thread[thr].timers),
!eb_is_empty(&task_per_thread[thr].rqueue),
!(LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_URGENT]) &&
LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_NORMAL]) &&
LIST_ISEMPTY(&task_per_thread[thr].tasklets[TL_BULK]) &&
MT_LIST_ISEMPTY(&task_per_thread[thr].shared_tasklet_list)),
task_per_thread[thr].tasks_in_list,
task_per_thread[thr].rq_total,
stuck,
!!(task_profiling_mask & thr_bit));
chunk_appendf(buf,
" harmless=%d wantrdv=%d",
!!(threads_harmless_mask & thr_bit),
!!(threads_want_rdv_mask & thr_bit));
chunk_appendf(buf, "\n");
chunk_appendf(buf, " cpu_ns: poll=%llu now=%llu diff=%llu\n", p, n, n-p);
/* this is the end of what we can dump from outside the thread */
if (thr != tid)
return;
chunk_appendf(buf, " curr_task=");
ha_task_dump(buf, sched->current, " ");
if (stuck) {
/* We only emit the backtrace for stuck threads in order not to
* waste precious output buffer space with non-interesting data.
* Please leave this as the last instruction in this function
* so that the compiler uses tail merging and the current
* function does not appear in the stack.
*/
ha_dump_backtrace(buf, " ", 0);
}
}
/* dumps into the buffer some information related to task <task> (which may
* either be a task or a tasklet, and prepend each line except the first one
* with <pfx>. The buffer is only appended and the first output starts by the
* pointer itself. The caller is responsible for making sure the task is not
* going to vanish during the dump.
*/
void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
{
const struct stream *s = NULL;
const struct appctx __maybe_unused *appctx = NULL;
struct hlua __maybe_unused *hlua = NULL;
if (!task) {
chunk_appendf(buf, "0\n");
return;
}
if (TASK_IS_TASKLET(task))
chunk_appendf(buf,
"%p (tasklet) calls=%u\n",
task,
task->calls);
else
chunk_appendf(buf,
"%p (task) calls=%u last=%llu%s\n",
task,
task->calls,
task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
task->call_date ? " ns ago" : "");
chunk_appendf(buf, "%s fct=%p(", pfx, task->process);
resolve_sym_name(buf, NULL, task->process);
chunk_appendf(buf,") ctx=%p", task->context);
if (task->process == task_run_applet && (appctx = task->context))
chunk_appendf(buf, "(%s)\n", appctx->applet->name);
else
chunk_appendf(buf, "\n");
if (task->process == process_stream && task->context)
s = (struct stream *)task->context;
else if (task->process == task_run_applet && task->context)
s = si_strm(((struct appctx *)task->context)->owner);
else if (task->process == si_cs_io_cb && task->context)
s = si_strm((struct stream_interface *)task->context);
if (s)
stream_dump(buf, s, pfx, '\n');
#ifdef USE_LUA
hlua = NULL;
if (s && (hlua = s->hlua)) {
chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx);
}
else if (task->process == hlua_process_task && (hlua = task->context)) {
chunk_appendf(buf, "%sCurrent executing a Lua task -- ", pfx);
}
else if (task->process == task_run_applet && (appctx = task->context) &&
(appctx->applet->fct == hlua_applet_tcp_fct && (hlua = appctx->ctx.hlua_apptcp.hlua))) {
chunk_appendf(buf, "%sCurrent executing a Lua TCP service -- ", pfx);
}
else if (task->process == task_run_applet && (appctx = task->context) &&
(appctx->applet->fct == hlua_applet_http_fct && (hlua = appctx->ctx.hlua_apphttp.hlua))) {
chunk_appendf(buf, "%sCurrent executing a Lua HTTP service -- ", pfx);
}
if (hlua && hlua->T) {
chunk_appendf(buf, "stack traceback:\n ");
append_prefixed_str(buf, hlua_traceback(hlua->T, "\n "), pfx, '\n', 0);
b_putchr(buf, '\n');
}
else
b_putchr(buf, '\n');
#endif
}
/* This function dumps all profiling settings. It returns 0 if the output
* buffer is full and it needs to be called again, otherwise non-zero.
*/
static int cli_io_handler_show_threads(struct appctx *appctx)
{
struct stream_interface *si = appctx->owner;
int thr;
if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
return 1;
if (appctx->st0)
thr = appctx->st1;
else
thr = 0;
chunk_reset(&trash);
ha_thread_dump_all_to_trash();
if (ci_putchk(si_ic(si), &trash) == -1) {
/* failed, try again */
si_rx_room_blk(si);
appctx->st1 = thr;
return 0;
}
return 1;
}
/* dumps a state of all threads into the trash and on fd #2, then aborts. */
void ha_panic()
{
chunk_reset(&trash);
chunk_appendf(&trash, "Thread %u is about to kill the process.\n", tid + 1);
ha_thread_dump_all_to_trash();
DISGUISE(write(2, trash.area, trash.data));
for (;;)
abort();
}
/* parse a "debug dev exit" command. It always returns 1, though it should never return. */
static int debug_parse_cli_exit(char **args, char *payload, struct appctx *appctx, void *private)
{
int code = atoi(args[3]);
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
_HA_ATOMIC_INC(&debug_commands_issued);
exit(code);
return 1;
}
/* parse a "debug dev bug" command. It always returns 1, though it should never return.
* Note: we make sure not to make the function static so that it appears in the trace.
*/
int debug_parse_cli_bug(char **args, char *payload, struct appctx *appctx, void *private)
{
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
_HA_ATOMIC_INC(&debug_commands_issued);
BUG_ON(one > zero);
return 1;
}
/* parse a "debug dev close" command. It always returns 1. */
static int debug_parse_cli_close(char **args, char *payload, struct appctx *appctx, void *private)
{
int fd;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
if (!*args[3])
return cli_err(appctx, "Missing file descriptor number.\n");
fd = atoi(args[3]);
if (fd < 0 || fd >= global.maxsock)
return cli_err(appctx, "File descriptor out of range.\n");
if (!fdtab[fd].owner)
return cli_msg(appctx, LOG_INFO, "File descriptor was already closed.\n");
_HA_ATOMIC_INC(&debug_commands_issued);
fd_delete(fd);
return 1;
}
/* parse a "debug dev delay" command. It always returns 1. */
static int debug_parse_cli_delay(char **args, char *payload, struct appctx *appctx, void *private)
{
int delay = atoi(args[3]);
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
_HA_ATOMIC_INC(&debug_commands_issued);
usleep((long)delay * 1000);
return 1;
}
/* parse a "debug dev log" command. It always returns 1. */
static int debug_parse_cli_log(char **args, char *payload, struct appctx *appctx, void *private)
{
int arg;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
_HA_ATOMIC_INC(&debug_commands_issued);
chunk_reset(&trash);
for (arg = 3; *args[arg]; arg++) {
if (arg > 3)
chunk_strcat(&trash, " ");
chunk_strcat(&trash, args[arg]);
}
send_log(NULL, LOG_INFO, "%s\n", trash.area);
return 1;
}
/* parse a "debug dev loop" command. It always returns 1. */
static int debug_parse_cli_loop(char **args, char *payload, struct appctx *appctx, void *private)
{
struct timeval deadline, curr;
int loop = atoi(args[3]);
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
_HA_ATOMIC_INC(&debug_commands_issued);
gettimeofday(&curr, NULL);
tv_ms_add(&deadline, &curr, loop);
while (tv_ms_cmp(&curr, &deadline) < 0)
gettimeofday(&curr, NULL);
return 1;
}
/* parse a "debug dev panic" command. It always returns 1, though it should never return. */
static int debug_parse_cli_panic(char **args, char *payload, struct appctx *appctx, void *private)
{
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
_HA_ATOMIC_INC(&debug_commands_issued);
ha_panic();
return 1;
}
/* parse a "debug dev exec" command. It always returns 1. */
#if defined(DEBUG_DEV)
static int debug_parse_cli_exec(char **args, char *payload, struct appctx *appctx, void *private)
{
int pipefd[2];
int arg;
int pid;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
_HA_ATOMIC_INC(&debug_commands_issued);
chunk_reset(&trash);
for (arg = 3; *args[arg]; arg++) {
if (arg > 3)
chunk_strcat(&trash, " ");
chunk_strcat(&trash, args[arg]);
}
thread_isolate();
if (pipe(pipefd) < 0)
goto fail_pipe;
if (fcntl(pipefd[0], F_SETFD, fcntl(pipefd[0], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
goto fail_fcntl;
if (fcntl(pipefd[1], F_SETFD, fcntl(pipefd[1], F_GETFD, FD_CLOEXEC) | FD_CLOEXEC) == -1)
goto fail_fcntl;
pid = fork();
if (pid < 0)
goto fail_fork;
else if (pid == 0) {
/* child */
char *cmd[4] = { "/bin/sh", "-c", 0, 0 };
close(0);
dup2(pipefd[1], 1);
dup2(pipefd[1], 2);
cmd[2] = trash.area;
execvp(cmd[0], cmd);
printf("execvp() failed\n");
exit(1);
}
/* parent */
thread_release();
close(pipefd[1]);
chunk_reset(&trash);
while (1) {
size_t ret = read(pipefd[0], trash.area + trash.data, trash.size - 20 - trash.data);
if (ret <= 0)
break;
trash.data += ret;
if (trash.data + 20 == trash.size) {
chunk_strcat(&trash, "\n[[[TRUNCATED]]]\n");
break;
}
}
close(pipefd[0]);
waitpid(pid, NULL, WNOHANG);
trash.area[trash.data] = 0;
return cli_msg(appctx, LOG_INFO, trash.area);
fail_fork:
fail_fcntl:
close(pipefd[0]);
close(pipefd[1]);
fail_pipe:
thread_release();
return cli_err(appctx, "Failed to execute command.\n");
}
#endif
/* parse a "debug dev hex" command. It always returns 1. */
static int debug_parse_cli_hex(char **args, char *payload, struct appctx *appctx, void *private)
{
unsigned long start, len;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
if (!*args[3])
return cli_err(appctx, "Missing memory address to dump from.\n");
start = strtoul(args[3], NULL, 0);
if (!start)
return cli_err(appctx, "Will not dump from NULL address.\n");
_HA_ATOMIC_INC(&debug_commands_issued);
/* by default, dump ~128 till next block of 16 */
len = strtoul(args[4], NULL, 0);
if (!len)
len = ((start + 128) & -16) - start;
chunk_reset(&trash);
dump_hex(&trash, " ", (const void *)start, len, 1);
trash.area[trash.data] = 0;
return cli_msg(appctx, LOG_INFO, trash.area);
}
/* parse a "debug dev sym <addr>" command. It always returns 1. */
static int debug_parse_cli_sym(char **args, char *payload, struct appctx *appctx, void *private)
{
unsigned long addr;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
if (!*args[3])
return cli_err(appctx, "Missing memory address to be resolved.\n");
_HA_ATOMIC_INC(&debug_commands_issued);
addr = strtoul(args[3], NULL, 0);
chunk_printf(&trash, "%#lx resolves to ", addr);
resolve_sym_name(&trash, NULL, (const void *)addr);
chunk_appendf(&trash, "\n");
return cli_msg(appctx, LOG_INFO, trash.area);
}
/* parse a "debug dev tkill" command. It always returns 1. */
static int debug_parse_cli_tkill(char **args, char *payload, struct appctx *appctx, void *private)
{
int thr = 0;
int sig = SIGABRT;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
if (*args[3])
thr = atoi(args[3]);
if (thr < 0 || thr > global.nbthread)
return cli_err(appctx, "Thread number out of range (use 0 for current).\n");
if (*args[4])
sig = atoi(args[4]);
_HA_ATOMIC_INC(&debug_commands_issued);
if (thr)
ha_tkill(thr - 1, sig);
else
raise(sig);
return 1;
}
/* parse a "debug dev write" command. It always returns 1. */
static int debug_parse_cli_write(char **args, char *payload, struct appctx *appctx, void *private)
{
unsigned long len;
if (!*args[3])
return cli_err(appctx, "Missing output size.\n");
len = strtoul(args[3], NULL, 0);
if (len >= trash.size)
return cli_err(appctx, "Output too large, must be <tune.bufsize.\n");
_HA_ATOMIC_INC(&debug_commands_issued);
chunk_reset(&trash);
trash.data = len;
memset(trash.area, '.', trash.data);
trash.area[trash.data] = 0;
for (len = 64; len < trash.data; len += 64)
trash.area[len] = '\n';
return cli_msg(appctx, LOG_INFO, trash.area);
}
/* parse a "debug dev stream" command */
/*
* debug dev stream [strm=<ptr>] [strm.f[{+-=}<flags>]] [txn.f[{+-=}<flags>]] \
* [req.f[{+-=}<flags>]] [res.f[{+-=}<flags>]] \
* [sif.f[{+-=<flags>]] [sib.f[{+-=<flags>]] \
* [sif.s[=<state>]] [sib.s[=<state>]]
*/
static int debug_parse_cli_stream(char **args, char *payload, struct appctx *appctx, void *private)
{
struct stream *s = si_strm(appctx->owner);
int arg;
void *ptr;
int size;
const char *word, *end;
struct ist name;
char *msg = NULL;
char *endarg;
unsigned long long old, new;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
ptr = NULL; size = 0;
if (!*args[3]) {
return cli_err(appctx,
"Usage: debug dev stream { <obj> <op> <value> | wake }*\n"
" <obj> = {strm | strm.f | sif.f | sif.s | sif.x | sib.f | sib.s | sib.x |\n"
" txn.f | req.f | req.r | req.w | res.f | res.r | res.w}\n"
" <op> = {'' (show) | '=' (assign) | '^' (xor) | '+' (or) | '-' (andnot)}\n"
" <value> = 'now' | 64-bit dec/hex integer (0x prefix supported)\n"
" 'wake' wakes the stream asssigned to 'strm' (default: current)\n"
);
}
_HA_ATOMIC_INC(&debug_commands_issued);
for (arg = 3; *args[arg]; arg++) {
old = 0;
end = word = args[arg];
while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
end++;
name = ist2(word, end - word);
if (isteq(name, ist("strm"))) {
ptr = (!s || !may_access(s)) ? NULL : &s; size = sizeof(s);
} else if (isteq(name, ist("strm.f"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->flags; size = sizeof(s->flags);
} else if (isteq(name, ist("txn.f"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->txn->flags; size = sizeof(s->txn->flags);
} else if (isteq(name, ist("req.f"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->req.flags; size = sizeof(s->req.flags);
} else if (isteq(name, ist("res.f"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->res.flags; size = sizeof(s->res.flags);
} else if (isteq(name, ist("req.r"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->req.rex; size = sizeof(s->req.rex);
} else if (isteq(name, ist("res.r"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->res.rex; size = sizeof(s->res.rex);
} else if (isteq(name, ist("req.w"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->req.wex; size = sizeof(s->req.wex);
} else if (isteq(name, ist("res.w"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->res.wex; size = sizeof(s->res.wex);
} else if (isteq(name, ist("sif.f"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->si[0].flags; size = sizeof(s->si[0].flags);
} else if (isteq(name, ist("sib.f"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->si[1].flags; size = sizeof(s->si[1].flags);
} else if (isteq(name, ist("sif.x"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->si[0].exp; size = sizeof(s->si[0].exp);
} else if (isteq(name, ist("sib.x"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->si[1].exp; size = sizeof(s->si[1].exp);
} else if (isteq(name, ist("sif.s"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->si[0].state; size = sizeof(s->si[0].state);
} else if (isteq(name, ist("sib.s"))) {
ptr = (!s || !may_access(s)) ? NULL : &s->si[1].state; size = sizeof(s->si[1].state);
} else if (isteq(name, ist("wake"))) {
if (s && may_access(s) && may_access((void *)s + sizeof(*s) - 1))
task_wakeup(s->task, TASK_WOKEN_TIMER|TASK_WOKEN_IO|TASK_WOKEN_MSG);
continue;
} else
return cli_dynerr(appctx, memprintf(&msg, "Unsupported field name: '%s'.\n", word));
/* read previous value */
if ((s || ptr == &s) && ptr && may_access(ptr) && may_access(ptr + size - 1)) {
if (size == 8)
old = read_u64(ptr);
else if (size == 4)
old = read_u32(ptr);
else if (size == 2)
old = read_u16(ptr);
else
old = *(const uint8_t *)ptr;
} else {
memprintf(&msg,
"%sSkipping inaccessible pointer %p for field '%.*s'.\n",
msg ? msg : "", ptr, (int)(end - word), word);
continue;
}
/* parse the new value . */
new = strtoll(end + 1, &endarg, 0);
if (end[1] && *endarg) {
if (strcmp(end + 1, "now") == 0)
new = now_ms;
else {
memprintf(&msg,
"%sIgnoring unparsable value '%s' for field '%.*s'.\n",
msg ? msg : "", end + 1, (int)(end - word), word);
continue;
}
}
switch (*end) {
case '\0': /* show */
memprintf(&msg, "%s%.*s=%#llx ", msg ? msg : "", (int)(end - word), word, old);
new = old; // do not change the value
break;
case '=': /* set */
break;
case '^': /* XOR */
new = old ^ new;
break;
case '+': /* OR */
new = old | new;
break;
case '-': /* AND NOT */
new = old & ~new;
break;
default:
break;
}
/* write the new value */
if (new != old) {
if (size == 8)
write_u64(ptr, new);
else if (size == 4)
write_u32(ptr, new);
else if (size == 2)
write_u16(ptr, new);
else
*(uint8_t *)ptr = new;
}
}
if (msg && *msg)
return cli_dynmsg(appctx, LOG_INFO, msg);
return 1;
}
static struct task *debug_task_handler(struct task *t, void *ctx, unsigned int state)
{
unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
unsigned long inter = tctx[1];
unsigned long rnd;
t->expire = tick_add(now_ms, inter);
/* half of the calls will wake up another entry */
rnd = statistical_prng();
if (rnd & 1) {
rnd >>= 1;
rnd %= tctx[0];
rnd = tctx[rnd + 2];
if (rnd & 1)
task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
else
tasklet_wakeup((struct tasklet *)rnd);
}
return t;
}
static struct task *debug_tasklet_handler(struct task *t, void *ctx, unsigned int state)
{
unsigned long *tctx = ctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
unsigned long rnd;
int i;
/* wake up two random entries */
for (i = 0; i < 2; i++) {
rnd = statistical_prng() % tctx[0];
rnd = tctx[rnd + 2];
if (rnd & 1)
task_wakeup((struct task *)(rnd - 1), TASK_WOKEN_MSG);
else
tasklet_wakeup((struct tasklet *)rnd);
}
return t;
}
/* parse a "debug dev sched" command
* debug dev sched {task|tasklet} [count=<count>] [mask=<mask>] [single=<single>] [inter=<inter>]
*/
static int debug_parse_cli_sched(char **args, char *payload, struct appctx *appctx, void *private)
{
int arg;
void *ptr;
int size;
const char *word, *end;
struct ist name;
char *msg = NULL;
char *endarg;
unsigned long long new;
unsigned long count = 0;
unsigned long thrid = 0;
unsigned int inter = 0;
unsigned long mask, tmask;
unsigned long i;
int mode = 0; // 0 = tasklet; 1 = task
int single = 0;
unsigned long *tctx; // [0] = #tasks, [1] = inter, [2+] = { tl | (tsk+1) }
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
ptr = NULL; size = 0;
mask = all_threads_mask;
if (strcmp(args[3], "task") != 0 && strcmp(args[3], "tasklet") != 0) {
return cli_err(appctx,
"Usage: debug dev sched {task|tasklet} { <obj> = <value> }*\n"
" <obj> = {count | mask | inter | single }\n"
" <value> = 64-bit dec/hex integer (0x prefix supported)\n"
);
}
mode = strcmp(args[3], "task") == 0;
_HA_ATOMIC_INC(&debug_commands_issued);
for (arg = 4; *args[arg]; arg++) {
end = word = args[arg];
while (*end && *end != '=' && *end != '^' && *end != '+' && *end != '-')
end++;
name = ist2(word, end - word);
if (isteq(name, ist("count"))) {
ptr = &count; size = sizeof(count);
} else if (isteq(name, ist("mask"))) {
ptr = &mask; size = sizeof(mask);
} else if (isteq(name, ist("tid"))) {
ptr = &thrid; size = sizeof(thrid);
} else if (isteq(name, ist("inter"))) {
ptr = &inter; size = sizeof(inter);
} else if (isteq(name, ist("single"))) {
ptr = &single; size = sizeof(single);
} else
return cli_dynerr(appctx, memprintf(&msg, "Unsupported setting: '%s'.\n", word));
/* parse the new value . */
new = strtoll(end + 1, &endarg, 0);
if (end[1] && *endarg) {
memprintf(&msg,
"%sIgnoring unparsable value '%s' for field '%.*s'.\n",
msg ? msg : "", end + 1, (int)(end - word), word);
continue;
}
/* write the new value */
if (size == 8)
write_u64(ptr, new);
else if (size == 4)
write_u32(ptr, new);
else if (size == 2)
write_u16(ptr, new);
else
*(uint8_t *)ptr = new;
}
tctx = calloc(sizeof(*tctx), count + 2);
if (!tctx)
goto fail;
tctx[0] = (unsigned long)count;
tctx[1] = (unsigned long)inter;
mask &= all_threads_mask;
if (!mask)
mask = tid_bit;
tmask = 0;
for (i = 0; i < count; i++) {
if (single || mode == 0) {
/* look for next bit matching a bit in mask or loop back to zero */
for (tmask <<= 1; !(mask & tmask); ) {
if (!(mask & -tmask))
tmask = 1;
else
tmask <<= 1;
}
} else {
/* multi-threaded task */
tmask = mask;
}
/* now, if poly or mask was set, tmask corresponds to the
* valid thread mask to use, otherwise it remains zero.
*/
//printf("%lu: mode=%d mask=%#lx\n", i, mode, tmask);
if (mode == 0) {
struct tasklet *tl = tasklet_new();
if (!tl)
goto fail;
if (tmask)
tl->tid = my_ffsl(tmask) - 1;
tl->process = debug_tasklet_handler;
tl->context = tctx;
tctx[i + 2] = (unsigned long)tl;
} else {
struct task *task = task_new(tmask ? tmask : tid_bit);
if (!task)
goto fail;
task->process = debug_task_handler;
task->context = tctx;
tctx[i + 2] = (unsigned long)task + 1;
}
}
/* start the tasks and tasklets */
for (i = 0; i < count; i++) {
unsigned long ctx = tctx[i + 2];
if (ctx & 1)
task_wakeup((struct task *)(ctx - 1), TASK_WOKEN_INIT);
else
tasklet_wakeup((struct tasklet *)ctx);
}
if (msg && *msg)
return cli_dynmsg(appctx, LOG_INFO, msg);
return 1;
fail:
/* free partially allocated entries */
for (i = 0; tctx && i < count; i++) {
unsigned long ctx = tctx[i + 2];
if (!ctx)
break;
if (ctx & 1)
task_destroy((struct task *)(ctx - 1));
else
tasklet_free((struct tasklet *)ctx);
}
free(tctx);
return cli_err(appctx, "Not enough memory");
}
#if defined(DEBUG_MEM_STATS)
/* CLI parser for the "debug dev memstats" command */
static int debug_parse_cli_memstats(char **args, char *payload, struct appctx *appctx, void *private)
{
extern __attribute__((__weak__)) struct mem_stats __start_mem_stats;
extern __attribute__((__weak__)) struct mem_stats __stop_mem_stats;
if (!cli_has_level(appctx, ACCESS_LVL_OPER))
return 1;
if (strcmp(args[3], "reset") == 0) {
struct mem_stats *ptr;
if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
return 1;
for (ptr = &__start_mem_stats; ptr < &__stop_mem_stats; ptr++) {
_HA_ATOMIC_STORE(&ptr->calls, 0);
_HA_ATOMIC_STORE(&ptr->size, 0);
}
return 1;
}
if (strcmp(args[3], "all") == 0)
appctx->ctx.cli.i0 = 1;
/* otherwise proceed with the dump from p0 to p1 */
appctx->ctx.cli.p0 = &__start_mem_stats;
appctx->ctx.cli.p1 = &__stop_mem_stats;
return 0;
}
/* CLI I/O handler for the "debug dev memstats" command. Dumps all mem_stats
* structs referenced by pointers located between p0 and p1. Dumps all entries
* if i0 > 0, otherwise only non-zero calls.
*/
static int debug_iohandler_memstats(struct appctx *appctx)
{
struct stream_interface *si = appctx->owner;
struct mem_stats *ptr = appctx->ctx.cli.p0;
int ret = 1;
if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
goto end;