forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.c
11312 lines (9712 loc) · 292 KB
/
task.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
/* task.c - core analysis suite
*
* Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
* Copyright (C) 2002-2018 David Anderson
* Copyright (C) 2002-2018 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "defs.h"
static ulong get_panic_context(void);
static int sort_by_pid(const void *, const void *);
static void show_ps(ulong, struct psinfo *);
static struct task_context *panic_search(void);
static void allocate_task_space(int);
static void refresh_fixed_task_table(void);
static void refresh_unlimited_task_table(void);
static void refresh_pidhash_task_table(void);
static void refresh_pid_hash_task_table(void);
static void refresh_hlist_task_table(void);
static void refresh_hlist_task_table_v2(void);
static void refresh_hlist_task_table_v3(void);
static void refresh_active_task_table(void);
static int radix_tree_task_callback(ulong);
static void refresh_radix_tree_task_table(void);
static void refresh_xarray_task_table(void);
static struct task_context *add_context(ulong, char *);
static void refresh_context(ulong, ulong);
static ulong parent_of(ulong);
static void parent_list(ulong);
static void child_list(ulong);
static void initialize_task_state(void);
static void dump_task_states(void);
static void show_ps_data(ulong, struct task_context *, struct psinfo *);
static void show_task_times(struct task_context *, ulong);
static void show_task_args(struct task_context *);
static void show_task_rlimit(struct task_context *);
static void show_tgid_list(ulong);
static int compare_start_time(const void *, const void *);
static int start_time_timespec(void);
static ulonglong convert_start_time(ulonglong, ulonglong);
static ulong search_panic_task_by_cpu(char *);
static ulong search_panic_task_by_keywords(char *, int *);
static ulong get_log_panic_task(void);
static ulong get_dumpfile_panic_task(void);
static ulong get_active_set_panic_task(void);
static void populate_panic_threads(void);
static int verify_task(struct task_context *, int);
static ulong get_idle_task(int, char *);
static ulong get_curr_task(int, char *);
static long rq_idx(int);
static long cpu_idx(int);
static void dump_runq(void);
static void dump_on_rq_timestamp(void);
static void dump_on_rq_lag(void);
static void dump_on_rq_milliseconds(void);
static void dump_runqueues(void);
static void dump_prio_array(int, ulong, char *);
static void dump_task_runq_entry(struct task_context *, int);
static void print_group_header_fair(int, ulong, void *);
static void print_parent_task_group_fair(void *, int);
static int dump_tasks_in_lower_dequeued_cfs_rq(int, ulong, int, struct task_context *);
static int dump_tasks_in_cfs_rq(ulong);
static int dump_tasks_in_task_group_cfs_rq(int, ulong, int, struct task_context *);
static void dump_on_rq_tasks(void);
static void cfs_rq_offset_init(void);
static void task_group_offset_init(void);
static void dump_CFS_runqueues(void);
static void print_group_header_rt(ulong, void *);
static void print_parent_task_group_rt(void *, int);
static int dump_tasks_in_lower_dequeued_rt_rq(int, ulong, int);
static int dump_RT_prio_array(ulong, char *);
static void dump_tasks_in_task_group_rt_rq(int, ulong, int);
static char *get_task_group_name(ulong);
static void sort_task_group_info_array(void);
static void print_task_group_info_array(void);
static void reuse_task_group_info_array(void);
static void free_task_group_info_array(void);
static void fill_task_group_info_array(int, ulong, char *, int);
static void dump_tasks_by_task_group(void);
static void task_struct_member(struct task_context *,unsigned int, struct reference *);
static void signal_reference(struct task_context *, ulong, struct reference *);
static void do_sig_thread_group(ulong);
static void dump_signal_data(struct task_context *, ulong);
#define TASK_LEVEL (0x1)
#define THREAD_GROUP_LEVEL (0x2)
#define TASK_INDENT (0x4)
static int sigrt_minmax(int *, int *);
static void signame_list(void);
static void sigqueue_list(ulong);
static ulonglong task_signal(ulong, ulong*);
static ulonglong task_blocked(ulong);
static void translate_sigset(ulonglong);
static ulonglong sigaction_mask(ulong);
static int task_has_cpu(ulong, char *);
static int is_foreach_keyword(char *, int *);
static void foreach_cleanup(void *);
static void ps_cleanup(void *);
static char *task_pointer_string(struct task_context *, ulong, char *);
static int panic_context_adjusted(struct task_context *tc);
static void show_last_run(struct task_context *, struct psinfo *);
static void show_milliseconds(struct task_context *, struct psinfo *);
static char *translate_nanoseconds(ulonglong, char *);
static int sort_by_last_run(const void *arg1, const void *arg2);
static void sort_context_array_by_last_run(void);
static void show_ps_summary(ulong);
static void irqstacks_init(void);
static void parse_task_thread(int argcnt, char *arglist[], struct task_context *);
static void stack_overflow_check_init(void);
static int has_sched_policy(ulong, ulong);
static ulong task_policy(ulong);
static ulong sched_policy_bit_from_str(const char *);
static ulong make_sched_policy(const char *);
void crash_get_current_task_info(unsigned long *, char **);
static struct sched_policy_info {
ulong value;
char *name;
} sched_policy_info[] = {
{ SCHED_NORMAL, "NORMAL" },
{ SCHED_FIFO, "FIFO" },
{ SCHED_RR, "RR" },
{ SCHED_BATCH, "BATCH" },
{ SCHED_ISO, "ISO" },
{ SCHED_IDLE, "IDLE" },
{ SCHED_DEADLINE, "DEADLINE" },
{ ULONG_MAX, NULL }
};
enum PANIC_TASK_FOUND_RESULT {
FOUND_NO_PANIC_KEYWORD,
FOUND_PANIC_KEYWORD,
FOUND_PANIC_TASK
};
const char *panic_keywords[] = {
"Unable to handle kernel",
"BUG: unable to handle kernel",
"Kernel BUG at",
"kernel BUG at",
"Bad mode in",
"Oops",
"Kernel panic",
NULL,
};
/*
* Figure out how much space will be required to hold the task context
* data, malloc() it, and call refresh_task_table() to fill it up.
* Gather a few key offset and size values. Lastly, get, and then set,
* the initial context.
*/
void
task_init(void)
{
long len;
int dim, task_struct_size;
struct syment *nsp;
long tss_offset, thread_offset;
long eip_offset, esp_offset, ksp_offset;
struct gnu_request req;
ulong active_pid;
if (!(tt->idle_threads = (ulong *)calloc(NR_CPUS, sizeof(ulong))))
error(FATAL, "cannot malloc idle_threads array");
if (DUMPFILE() &&
!(tt->panic_threads = (ulong *)calloc(NR_CPUS, sizeof(ulong))))
error(FATAL, "cannot malloc panic_threads array");
if (kernel_symbol_exists("nr_tasks")) {
/*
* Figure out what maximum NR_TASKS would be by getting the
* address of the next symbol after "task".
*/
tt->task_start = symbol_value("task");
if ((nsp = next_symbol("task", NULL)) == NULL)
error(FATAL, "cannot determine size of task table\n");
tt->flags |= TASK_ARRAY_EXISTS;
tt->task_end = nsp->value;
tt->max_tasks = (tt->task_end-tt->task_start) / sizeof(void *);
allocate_task_space(tt->max_tasks);
tss_offset = MEMBER_OFFSET_INIT(task_struct_tss,
"task_struct", "tss");
eip_offset = MEMBER_OFFSET_INIT(thread_struct_eip,
"thread_struct", "eip");
esp_offset = MEMBER_OFFSET_INIT(thread_struct_esp,
"thread_struct", "esp");
ksp_offset = MEMBER_OFFSET_INIT(thread_struct_ksp,
"thread_struct", "ksp");
ASSIGN_OFFSET(task_struct_tss_eip) =
(eip_offset == INVALID_OFFSET) ?
INVALID_OFFSET : tss_offset + eip_offset;
ASSIGN_OFFSET(task_struct_tss_esp) =
(esp_offset == INVALID_OFFSET) ?
INVALID_OFFSET : tss_offset + esp_offset;
ASSIGN_OFFSET(task_struct_tss_ksp) =
(ksp_offset == INVALID_OFFSET) ?
INVALID_OFFSET : tss_offset + ksp_offset;
tt->flags |= TASK_REFRESH;
tt->refresh_task_table = refresh_fixed_task_table;
readmem(tt->task_start, KVADDR, &tt->idle_threads[0],
kt->cpus * sizeof(void *), "idle threads",
FAULT_ON_ERROR);
} else {
/*
* Make the task table big enough to hold what's running.
* It can be realloc'd later if it grows on a live system.
*/
get_symbol_data("nr_threads", sizeof(int), &tt->nr_threads);
tt->max_tasks = tt->nr_threads + NR_CPUS + TASK_SLUSH;
allocate_task_space(tt->max_tasks);
thread_offset = MEMBER_OFFSET_INIT(task_struct_thread,
"task_struct", "thread");
eip_offset = MEMBER_OFFSET_INIT(thread_struct_eip,
"thread_struct", "eip");
esp_offset = MEMBER_OFFSET_INIT(thread_struct_esp,
"thread_struct", "esp");
/*
* Handle x86/x86_64 merger.
*/
if (eip_offset == INVALID_OFFSET)
eip_offset = MEMBER_OFFSET_INIT(thread_struct_eip,
"thread_struct", "ip");
if (esp_offset == INVALID_OFFSET)
esp_offset = MEMBER_OFFSET_INIT(thread_struct_esp,
"thread_struct", "sp");
ksp_offset = MEMBER_OFFSET_INIT(thread_struct_ksp,
"thread_struct", "ksp");
ASSIGN_OFFSET(task_struct_thread_eip) =
(eip_offset == INVALID_OFFSET) ?
INVALID_OFFSET : thread_offset + eip_offset;
ASSIGN_OFFSET(task_struct_thread_esp) =
(esp_offset == INVALID_OFFSET) ?
INVALID_OFFSET : thread_offset + esp_offset;
ASSIGN_OFFSET(task_struct_thread_ksp) =
(ksp_offset == INVALID_OFFSET) ?
INVALID_OFFSET : thread_offset + ksp_offset;
tt->flags |= TASK_REFRESH;
tt->refresh_task_table = refresh_unlimited_task_table;
get_idle_threads(&tt->idle_threads[0], kt->cpus);
}
/*
* Handle CONFIG_THREAD_INFO_IN_TASK changes
*/
MEMBER_OFFSET_INIT(task_struct_stack, "task_struct", "stack");
MEMBER_OFFSET_INIT(task_struct_thread_info, "task_struct", "thread_info");
if (VALID_MEMBER(task_struct_thread_info)) {
switch (MEMBER_TYPE("task_struct", "thread_info"))
{
case TYPE_CODE_PTR:
break;
case TYPE_CODE_STRUCT:
tt->flags |= THREAD_INFO_IN_TASK;
break;
default:
error(FATAL,
"unexpected type code for task_struct.thread_info: %ld\n",
MEMBER_TYPE("task_struct", "thread_info"));
break;
}
} else if (VALID_MEMBER(task_struct_stack))
MEMBER_OFFSET_INIT(task_struct_thread_info, "task_struct", "stack");
MEMBER_OFFSET_INIT(task_struct_cpu, "task_struct", "cpu");
if (VALID_MEMBER(task_struct_thread_info)) {
if (tt->flags & THREAD_INFO_IN_TASK && VALID_MEMBER(task_struct_cpu)) {
MEMBER_OFFSET_INIT(thread_info_flags, "thread_info", "flags");
/* (unnecessary) reminders */
ASSIGN_OFFSET(thread_info_task) = INVALID_OFFSET;
ASSIGN_OFFSET(thread_info_cpu) = INVALID_OFFSET;
ASSIGN_OFFSET(thread_info_previous_esp) = INVALID_OFFSET;
} else {
MEMBER_OFFSET_INIT(thread_info_task, "thread_info", "task");
MEMBER_OFFSET_INIT(thread_info_cpu, "thread_info", "cpu");
MEMBER_OFFSET_INIT(thread_info_flags, "thread_info", "flags");
MEMBER_OFFSET_INIT(thread_info_previous_esp, "thread_info",
"previous_esp");
}
STRUCT_SIZE_INIT(thread_info, "thread_info");
tt->flags |= THREAD_INFO;
}
MEMBER_OFFSET_INIT(task_struct_state, "task_struct", "state");
MEMBER_SIZE_INIT(task_struct_state, "task_struct", "state");
if (INVALID_MEMBER(task_struct_state)) {
MEMBER_OFFSET_INIT(task_struct_state, "task_struct", "__state");
MEMBER_SIZE_INIT(task_struct_state, "task_struct", "__state");
}
MEMBER_OFFSET_INIT(task_struct_exit_state, "task_struct", "exit_state");
MEMBER_OFFSET_INIT(task_struct_pid, "task_struct", "pid");
MEMBER_OFFSET_INIT(task_struct_comm, "task_struct", "comm");
MEMBER_OFFSET_INIT(task_struct_next_task, "task_struct", "next_task");
MEMBER_OFFSET_INIT(task_struct_processor, "task_struct", "processor");
MEMBER_OFFSET_INIT(task_struct_p_pptr, "task_struct", "p_pptr");
MEMBER_OFFSET_INIT(task_struct_parent, "task_struct", "parent");
if (INVALID_MEMBER(task_struct_parent))
MEMBER_OFFSET_INIT(task_struct_parent, "task_struct",
"real_parent");
MEMBER_OFFSET_INIT(task_struct_has_cpu, "task_struct", "has_cpu");
MEMBER_OFFSET_INIT(task_struct_cpus_runnable,
"task_struct", "cpus_runnable");
MEMBER_OFFSET_INIT(task_struct_active_mm, "task_struct", "active_mm");
MEMBER_OFFSET_INIT(task_struct_next_run, "task_struct", "next_run");
MEMBER_OFFSET_INIT(task_struct_flags, "task_struct", "flags");
MEMBER_SIZE_INIT(task_struct_flags, "task_struct", "flags");
MEMBER_OFFSET_INIT(task_struct_policy, "task_struct", "policy");
MEMBER_SIZE_INIT(task_struct_policy, "task_struct", "policy");
MEMBER_OFFSET_INIT(task_struct_pidhash_next,
"task_struct", "pidhash_next");
MEMBER_OFFSET_INIT(task_struct_pgrp, "task_struct", "pgrp");
MEMBER_OFFSET_INIT(task_struct_tgid, "task_struct", "tgid");
MEMBER_OFFSET_INIT(task_struct_pids, "task_struct", "pids");
MEMBER_OFFSET_INIT(task_struct_last_run, "task_struct", "last_run");
MEMBER_OFFSET_INIT(task_struct_timestamp, "task_struct", "timestamp");
MEMBER_OFFSET_INIT(task_struct_sched_info, "task_struct", "sched_info");
if (VALID_MEMBER(task_struct_sched_info))
MEMBER_OFFSET_INIT(sched_info_last_arrival,
"sched_info", "last_arrival");
if (VALID_MEMBER(task_struct_last_run) ||
VALID_MEMBER(task_struct_timestamp) ||
VALID_MEMBER(sched_info_last_arrival)) {
char buf[BUFSIZE];
strcpy(buf, "alias last ps -l");
alias_init(buf);
}
MEMBER_OFFSET_INIT(task_struct_pid_links, "task_struct", "pid_links");
MEMBER_OFFSET_INIT(pid_link_pid, "pid_link", "pid");
MEMBER_OFFSET_INIT(pid_hash_chain, "pid", "hash_chain");
STRUCT_SIZE_INIT(pid_link, "pid_link");
STRUCT_SIZE_INIT(upid, "upid");
if (VALID_STRUCT(upid)) {
MEMBER_OFFSET_INIT(upid_nr, "upid", "nr");
MEMBER_OFFSET_INIT(upid_ns, "upid", "ns");
MEMBER_OFFSET_INIT(upid_pid_chain, "upid", "pid_chain");
MEMBER_OFFSET_INIT(pid_numbers, "pid", "numbers");
ARRAY_LENGTH_INIT(len, pid_numbers, "pid.numbers", NULL, 0);
MEMBER_OFFSET_INIT(pid_tasks, "pid", "tasks");
tt->init_pid_ns = symbol_value("init_pid_ns");
}
MEMBER_OFFSET_INIT(pid_pid_chain, "pid", "pid_chain");
STRUCT_SIZE_INIT(task_struct, "task_struct");
if (kernel_symbol_exists("arch_task_struct_size") &&
readmem(symbol_value("arch_task_struct_size"), KVADDR,
&task_struct_size, sizeof(int),
"arch_task_struct_size", RETURN_ON_ERROR)) {
ASSIGN_SIZE(task_struct) = task_struct_size;
if (STRUCT_SIZE("task_struct") != SIZE(task_struct))
add_to_downsized("task_struct");
if (CRASHDEBUG(1))
fprintf(fp, "downsize task_struct: %ld to %ld\n",
STRUCT_SIZE("task_struct"),
SIZE(task_struct));
}
MEMBER_OFFSET_INIT(task_struct_sig, "task_struct", "sig");
MEMBER_OFFSET_INIT(task_struct_signal, "task_struct", "signal");
MEMBER_OFFSET_INIT(task_struct_blocked, "task_struct", "blocked");
MEMBER_OFFSET_INIT(task_struct_sigpending, "task_struct", "sigpending");
MEMBER_OFFSET_INIT(task_struct_pending, "task_struct", "pending");
MEMBER_OFFSET_INIT(task_struct_sigqueue, "task_struct", "sigqueue");
MEMBER_OFFSET_INIT(task_struct_sighand, "task_struct", "sighand");
MEMBER_OFFSET_INIT(signal_struct_count, "signal_struct", "count");
MEMBER_OFFSET_INIT(signal_struct_nr_threads, "signal_struct", "nr_threads");
MEMBER_OFFSET_INIT(signal_struct_action, "signal_struct", "action");
MEMBER_OFFSET_INIT(signal_struct_shared_pending, "signal_struct",
"shared_pending");
MEMBER_OFFSET_INIT(k_sigaction_sa, "k_sigaction", "sa");
MEMBER_OFFSET_INIT(sigaction_sa_handler, "sigaction", "sa_handler");
MEMBER_OFFSET_INIT(sigaction_sa_mask, "sigaction", "sa_mask");
MEMBER_OFFSET_INIT(sigaction_sa_flags, "sigaction", "sa_flags");
MEMBER_OFFSET_INIT(sigpending_head, "sigpending", "head");
if (INVALID_MEMBER(sigpending_head))
MEMBER_OFFSET_INIT(sigpending_list, "sigpending", "list");
MEMBER_OFFSET_INIT(sigpending_signal, "sigpending", "signal");
MEMBER_SIZE_INIT(sigpending_signal, "sigpending", "signal");
STRUCT_SIZE_INIT(sigqueue, "sigqueue");
STRUCT_SIZE_INIT(signal_queue, "signal_queue");
STRUCT_SIZE_INIT(sighand_struct, "sighand_struct");
if (VALID_STRUCT(sighand_struct))
MEMBER_OFFSET_INIT(sighand_struct_action, "sighand_struct",
"action");
MEMBER_OFFSET_INIT(siginfo_si_signo, "siginfo", "si_signo");
STRUCT_SIZE_INIT(signal_struct, "signal_struct");
STRUCT_SIZE_INIT(k_sigaction, "k_sigaction");
MEMBER_OFFSET_INIT(task_struct_start_time, "task_struct", "start_time");
MEMBER_SIZE_INIT(task_struct_start_time, "task_struct", "start_time");
MEMBER_SIZE_INIT(task_struct_utime, "task_struct", "utime");
MEMBER_SIZE_INIT(task_struct_stime, "task_struct", "stime");
MEMBER_OFFSET_INIT(task_struct_times, "task_struct", "times");
MEMBER_OFFSET_INIT(tms_tms_utime, "tms", "tms_utime");
MEMBER_OFFSET_INIT(tms_tms_stime, "tms", "tms_stime");
MEMBER_OFFSET_INIT(task_struct_utime, "task_struct", "utime");
MEMBER_OFFSET_INIT(task_struct_stime, "task_struct", "stime");
STRUCT_SIZE_INIT(cputime_t, "cputime_t");
if ((THIS_KERNEL_VERSION < LINUX(4,8,0)) &&
symbol_exists("cfq_slice_async")) {
uint cfq_slice_async;
get_symbol_data("cfq_slice_async", sizeof(int),
&cfq_slice_async);
if (cfq_slice_async) {
machdep->hz = cfq_slice_async * 25;
if (CRASHDEBUG(2))
fprintf(fp,
"cfq_slice_async exists: setting hz to %d\n",
machdep->hz);
}
} else if ((symbol_exists("dd_init_queue") &&
gdb_set_crash_scope(symbol_value("dd_init_queue"), "dd_init_queue")) ||
(symbol_exists("dd_init_sched") &&
gdb_set_crash_scope(symbol_value("dd_init_sched"), "dd_init_sched")) ||
(symbol_exists("deadline_init_queue") &&
gdb_set_crash_scope(symbol_value("deadline_init_queue"), "deadline_init_queue"))) {
char buf[BUFSIZE];
uint write_expire = 0;
open_tmpfile();
sprintf(buf, "printf \"%%d\", write_expire");
if (gdb_pass_through(buf, pc->tmpfile, GNU_RETURN_ON_ERROR)) {
rewind(pc->tmpfile);
if (fgets(buf, BUFSIZE, pc->tmpfile))
sscanf(buf, "%d", &write_expire);
}
close_tmpfile();
if (write_expire) {
machdep->hz = write_expire / 5;
if (CRASHDEBUG(2))
fprintf(fp, "write_expire exists: setting hz to %d\n",
machdep->hz);
}
gdb_set_crash_scope(0, NULL);
}
if (VALID_MEMBER(runqueue_arrays))
MEMBER_OFFSET_INIT(task_struct_run_list, "task_struct",
"run_list");
MEMBER_OFFSET_INIT(task_struct_rss_stat, "task_struct",
"rss_stat");
MEMBER_OFFSET_INIT(task_rss_stat_count, "task_rss_stat",
"count");
if ((tt->task_struct = (char *)malloc(SIZE(task_struct))) == NULL)
error(FATAL, "cannot malloc task_struct space.");
if ((tt->mm_struct = (char *)malloc(SIZE(mm_struct))) == NULL)
error(FATAL, "cannot malloc mm_struct space.");
if ((tt->flags & THREAD_INFO) &&
((tt->thread_info = (char *)malloc(SIZE(thread_info))) == NULL))
error(FATAL, "cannot malloc thread_info space.");
STRUCT_SIZE_INIT(task_union, "task_union");
STRUCT_SIZE_INIT(thread_union, "thread_union");
if (VALID_SIZE(task_union) && (SIZE(task_union) != STACKSIZE())) {
error(WARNING, "\nnon-standard stack size: %ld\n",
len = SIZE(task_union));
machdep->stacksize = len;
} else if (VALID_SIZE(thread_union) &&
((len = SIZE(thread_union)) != STACKSIZE())) {
machdep->stacksize = len;
} else if (!VALID_SIZE(thread_union) && !VALID_SIZE(task_union)) {
len = 0;
if (kernel_symbol_exists("__start_init_stack") &&
kernel_symbol_exists("__end_init_stack")) {
len = symbol_value("__end_init_stack");
len -= symbol_value("__start_init_stack");
} else if (kernel_symbol_exists("__start_init_task") &&
kernel_symbol_exists("__end_init_task")) {
len = symbol_value("__end_init_task");
len -= symbol_value("__start_init_task");
}
if (len) {
ASSIGN_SIZE(thread_union) = len;
machdep->stacksize = len;
}
}
MEMBER_OFFSET_INIT(pid_namespace_idr, "pid_namespace", "idr");
MEMBER_OFFSET_INIT(idr_idr_rt, "idr", "idr_rt");
if (symbol_exists("height_to_maxindex") ||
symbol_exists("height_to_maxnodes")) {
int newver = symbol_exists("height_to_maxnodes");
int tmp ATTRIBUTE_UNUSED;
if (!newver) {
if (LKCD_KERNTYPES())
ARRAY_LENGTH_INIT_ALT(tmp, "height_to_maxindex",
"radix_tree_preload.nodes", NULL, 0);
else
ARRAY_LENGTH_INIT(tmp, height_to_maxindex,
"height_to_maxindex", NULL, 0);
} else {
if (LKCD_KERNTYPES())
ARRAY_LENGTH_INIT_ALT(tmp, "height_to_maxnodes",
"radix_tree_preload.nodes", NULL, 0);
else
ARRAY_LENGTH_INIT(tmp, height_to_maxnodes,
"height_to_maxnodes", NULL, 0);
}
STRUCT_SIZE_INIT(radix_tree_root, "radix_tree_root");
STRUCT_SIZE_INIT(radix_tree_node, "radix_tree_node");
MEMBER_OFFSET_INIT(radix_tree_root_height,
"radix_tree_root","height");
MEMBER_OFFSET_INIT(radix_tree_root_rnode,
"radix_tree_root","rnode");
MEMBER_OFFSET_INIT(radix_tree_node_slots,
"radix_tree_node","slots");
MEMBER_OFFSET_INIT(radix_tree_node_height,
"radix_tree_node","height");
MEMBER_OFFSET_INIT(radix_tree_node_shift,
"radix_tree_node","shift");
}
STRUCT_SIZE_INIT(xarray, "xarray");
STRUCT_SIZE_INIT(xa_node, "xa_node");
MEMBER_OFFSET_INIT(xarray_xa_head, "xarray","xa_head");
MEMBER_OFFSET_INIT(xa_node_slots, "xa_node","slots");
MEMBER_OFFSET_INIT(xa_node_shift, "xa_node","shift");
if (symbol_exists("pidhash") && symbol_exists("pid_hash") &&
!symbol_exists("pidhash_shift"))
error(FATAL,
"pidhash and pid_hash both exist -- cannot distinquish between them\n");
if (VALID_MEMBER(pid_namespace_idr)) {
STRUCT_SIZE_INIT(pid, "pid");
if (STREQ(MEMBER_TYPE_NAME("idr", "idr_rt"), "xarray")) {
tt->refresh_task_table = refresh_xarray_task_table;
tt->pid_xarray = symbol_value("init_pid_ns") +
OFFSET(pid_namespace_idr) + OFFSET(idr_idr_rt);
tt->flags |= PID_XARRAY;
} else if STREQ(MEMBER_TYPE_NAME("idr", "idr_rt"), "radix_tree_root") {
if (MEMBER_EXISTS("radix_tree_root", "rnode")) {
tt->refresh_task_table = refresh_radix_tree_task_table;
tt->pid_radix_tree = symbol_value("init_pid_ns") +
OFFSET(pid_namespace_idr) + OFFSET(idr_idr_rt);
tt->flags |= PID_RADIX_TREE;
} else if (MEMBER_EXISTS("radix_tree_root", "xa_head")) {
tt->refresh_task_table = refresh_xarray_task_table;
tt->pid_xarray = symbol_value("init_pid_ns") +
OFFSET(pid_namespace_idr) + OFFSET(idr_idr_rt);
tt->flags |= PID_XARRAY;
}
} else
error(FATAL, "unknown pid_namespace.idr type: %s\n",
MEMBER_TYPE_NAME("idr", "idr_rt"));
} else if (symbol_exists("pid_hash") && symbol_exists("pidhash_shift")) {
int pidhash_shift;
if (get_symbol_type("PIDTYPE_PID", NULL, &req) !=
TYPE_CODE_ENUM)
error(FATAL,
"cannot determine PIDTYPE_PID pid_hash dimension\n");
get_symbol_data("pidhash_shift", sizeof(int), &pidhash_shift);
tt->pidhash_len = 1 << pidhash_shift;
get_symbol_data("pid_hash", sizeof(ulong), &tt->pidhash_addr);
if (VALID_MEMBER(pid_link_pid) && VALID_MEMBER(pid_hash_chain)) {
get_symbol_data("pid_hash", sizeof(ulong), &tt->pidhash_addr);
tt->refresh_task_table = refresh_pid_hash_task_table;
} else {
tt->pidhash_addr = symbol_value("pid_hash");
if (LKCD_KERNTYPES()) {
if (VALID_STRUCT(pid_link)) {
if (VALID_STRUCT(upid) && VALID_MEMBER(pid_numbers))
tt->refresh_task_table =
refresh_hlist_task_table_v3;
else
tt->refresh_task_table =
refresh_hlist_task_table_v2;
} else
tt->refresh_task_table =
refresh_hlist_task_table;
builtin_array_length("pid_hash",
tt->pidhash_len, NULL);
} else {
if (!get_array_length("pid_hash", NULL,
sizeof(void *)) && VALID_STRUCT(pid_link)) {
if (VALID_STRUCT(upid) && VALID_MEMBER(pid_numbers))
tt->refresh_task_table =
refresh_hlist_task_table_v3;
else
tt->refresh_task_table =
refresh_hlist_task_table_v2;
}
else
tt->refresh_task_table =
refresh_hlist_task_table;
}
}
tt->flags |= PID_HASH;
} else if (symbol_exists("pid_hash")) {
if (get_symbol_type("PIDTYPE_PGID", NULL, &req) !=
TYPE_CODE_ENUM)
error(FATAL,
"cannot determine PIDTYPE_PID pid_hash dimension\n");
if (!(tt->pidhash_len = get_array_length("pid_hash",
&dim, SIZE(list_head))))
error(FATAL,
"cannot determine pid_hash array dimensions\n");
tt->pidhash_addr = symbol_value("pid_hash");
tt->refresh_task_table = refresh_pid_hash_task_table;
tt->flags |= PID_HASH;
} else if (symbol_exists("pidhash")) {
tt->pidhash_addr = symbol_value("pidhash");
tt->pidhash_len = get_array_length("pidhash", NULL, 0);
if (tt->pidhash_len == 0) {
if (!(nsp = next_symbol("pidhash", NULL)))
error(FATAL,
"cannot determine pidhash length\n");
tt->pidhash_len =
(nsp->value-tt->pidhash_addr) / sizeof(void *);
}
if (ACTIVE())
tt->refresh_task_table = refresh_pidhash_task_table;
tt->flags |= PIDHASH;
}
tt->pf_kthread = UNINITIALIZED;
get_active_set();
if (tt->flags & ACTIVE_ONLY)
tt->refresh_task_table = refresh_active_task_table;
tt->refresh_task_table();
if (tt->flags & TASK_REFRESH_OFF)
tt->flags &= ~(TASK_REFRESH|TASK_REFRESH_OFF);
/*
* Get the IRQ stacks info if it's configured.
*/
if (VALID_STRUCT(irq_ctx))
irqstacks_init();
if (ACTIVE()) {
active_pid = REMOTE() ? pc->server_pid :
LOCAL_ACTIVE() ? pc->program_pid : 1;
set_context(NO_TASK, active_pid, FALSE);
tt->this_task = pid_to_task(active_pid);
}
else {
if (INVALID_SIZE(note_buf))
STRUCT_SIZE_INIT(note_buf, "note_buf_t");
if (KDUMP_DUMPFILE())
map_cpus_to_prstatus();
else if (ELF_NOTES_VALID() && DISKDUMP_DUMPFILE())
map_cpus_to_prstatus_kdump_cmprs();
please_wait("determining panic task");
set_context(get_panic_context(), NO_PID, TRUE);
please_wait_done();
}
sort_context_array();
sort_tgid_array();
if (pc->flags & SILENT)
initialize_task_state();
stack_overflow_check_init();
if (machdep->hz) {
ulonglong uptime_jiffies;
ulong uptime_sec;
get_uptime(NULL, &uptime_jiffies);
uptime_sec = (uptime_jiffies)/(ulonglong)machdep->hz;
kt->boot_date.tv_sec = kt->date.tv_sec - uptime_sec;
kt->boot_date.tv_nsec = 0;
}
tt->flags |= TASK_INIT_DONE;
}
/*
* Store the pointers to the hard and soft irq_ctx arrays as well as
* the task pointers contained within each of them.
*/
static void
irqstacks_init(void)
{
int i;
char *thread_info_buf;
struct syment *hard_sp, *soft_sp;
ulong ptr, hardirq_next_sp = 0;
if (!(tt->hardirq_ctx = (ulong *)calloc(NR_CPUS, sizeof(ulong))))
error(FATAL, "cannot malloc hardirq_ctx space.");
if (!(tt->hardirq_tasks = (ulong *)calloc(NR_CPUS, sizeof(ulong))))
error(FATAL, "cannot malloc hardirq_tasks space.");
if (!(tt->softirq_ctx = (ulong *)calloc(NR_CPUS, sizeof(ulong))))
error(FATAL, "cannot malloc softirq_ctx space.");
if (!(tt->softirq_tasks = (ulong *)calloc(NR_CPUS, sizeof(ulong))))
error(FATAL, "cannot malloc softirq_tasks space.");
thread_info_buf = GETBUF(SIZE(irq_ctx));
if ((hard_sp = per_cpu_symbol_search("per_cpu__hardirq_ctx")) ||
(hard_sp = per_cpu_symbol_search("per_cpu__hardirq_stack"))) {
if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF)) {
for (i = 0; i < NR_CPUS; i++) {
if (!kt->__per_cpu_offset[i])
continue;
ptr = hard_sp->value + kt->__per_cpu_offset[i];
if (!readmem(ptr, KVADDR, &ptr,
sizeof(void *), "hardirq ctx",
RETURN_ON_ERROR)) {
error(INFO, "cannot read hardirq_ctx[%d] at %lx\n",
i, ptr);
continue;
}
tt->hardirq_ctx[i] = ptr;
}
} else
tt->hardirq_ctx[0] = hard_sp->value;
} else if (symbol_exists("hardirq_ctx")) {
i = get_array_length("hardirq_ctx", NULL, 0);
get_symbol_data("hardirq_ctx",
sizeof(long)*(i <= NR_CPUS ? i : NR_CPUS),
&tt->hardirq_ctx[0]);
} else
error(WARNING, "cannot determine hardirq_ctx addresses\n");
/* TODO: Use multithreading to parallely update irq_tasks. */
for (i = 0; i < NR_CPUS; i++) {
if (!(tt->hardirq_ctx[i]))
continue;
if (!readmem(tt->hardirq_ctx[i], KVADDR, thread_info_buf,
SIZE(irq_ctx), "hardirq thread_union",
RETURN_ON_ERROR)) {
error(INFO, "cannot read hardirq_ctx[%d] at %lx\n",
i, tt->hardirq_ctx[i]);
continue;
}
if (MEMBER_EXISTS("irq_ctx", "tinfo"))
tt->hardirq_tasks[i] =
ULONG(thread_info_buf+OFFSET(thread_info_task));
else {
hardirq_next_sp = ULONG(thread_info_buf);
tt->hardirq_tasks[i] = stkptr_to_task(hardirq_next_sp);
}
}
if ((soft_sp = per_cpu_symbol_search("per_cpu__softirq_ctx")) ||
(soft_sp = per_cpu_symbol_search("per_cpu__softirq_stack"))) {
if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF)) {
for (i = 0; i < NR_CPUS; i++) {
if (!kt->__per_cpu_offset[i])
continue;
ptr = soft_sp->value + kt->__per_cpu_offset[i];
if (!readmem(ptr, KVADDR, &ptr,
sizeof(void *), "softirq ctx",
RETURN_ON_ERROR)) {
error(INFO, "cannot read softirq_ctx[%d] at %lx\n",
i, ptr);
continue;
}
tt->softirq_ctx[i] = ptr;
}
} else
tt->softirq_ctx[0] = soft_sp->value;
} else if (symbol_exists("softirq_ctx")) {
i = get_array_length("softirq_ctx", NULL, 0);
get_symbol_data("softirq_ctx",
sizeof(long)*(i <= NR_CPUS ? i : NR_CPUS),
&tt->softirq_ctx[0]);
} else
error(WARNING, "cannot determine softirq_ctx addresses\n");
for (i = 0; i < NR_CPUS; i++) {
if (!(tt->softirq_ctx[i]))
continue;
if (!readmem(tt->softirq_ctx[i], KVADDR, thread_info_buf,
SIZE(irq_ctx), "softirq thread_union",
RETURN_ON_ERROR)) {
error(INFO, "cannot read softirq_ctx[%d] at %lx\n",
i, tt->hardirq_ctx[i]);
continue;
}
if (MEMBER_EXISTS("irq_ctx", "tinfo"))
tt->softirq_tasks[i] =
ULONG(thread_info_buf+OFFSET(thread_info_task));
else {
tt->softirq_tasks[i] = stkptr_to_task(ULONG(thread_info_buf));
/* Checking if softirq => hardirq nested stack */
if ((tt->softirq_tasks[i] != NO_TASK) && hardirq_next_sp) {
if ((tt->softirq_ctx[i] <= hardirq_next_sp) &&
(hardirq_next_sp < tt->softirq_ctx[i] + STACKSIZE()))
tt->hardirq_tasks[i] = tt->softirq_tasks[i];
}
}
}
tt->flags |= IRQSTACKS;
FREEBUF(thread_info_buf);
}
int
in_irq_ctx(ulonglong type, int cpu, ulong addr)
{
if (!(tt->flags & IRQSTACKS))
return FALSE;
switch (type)
{
case BT_SOFTIRQ:
if (tt->softirq_ctx[cpu] &&
(addr >= tt->softirq_ctx[cpu]) &&
(addr < (tt->softirq_ctx[cpu] + STACKSIZE())))
return TRUE;
break;
case BT_HARDIRQ:
if (tt->hardirq_ctx[cpu] &&
(addr >= tt->hardirq_ctx[cpu]) &&
(addr < (tt->hardirq_ctx[cpu] + STACKSIZE())))
return TRUE;
break;
}
return FALSE;
}
/*
* Allocate or re-allocated space for the task_context array and task list.
*/
static void
allocate_task_space(int cnt)
{
if (tt->context_array == NULL) {
if (!(tt->task_local = (void *)
malloc(cnt * sizeof(void *))))
error(FATAL,
"cannot malloc kernel task array (%d tasks)", cnt);
if (!(tt->context_array = (struct task_context *)
malloc(cnt * sizeof(struct task_context))))
error(FATAL, "cannot malloc context array (%d tasks)",
cnt);
if (!(tt->context_by_task = (struct task_context **)
malloc(cnt * sizeof(struct task_context*))))
error(FATAL, "cannot malloc context_by_task array (%d tasks)",
cnt);
if (!(tt->tgid_array = (struct tgid_context *)
malloc(cnt * sizeof(struct tgid_context))))
error(FATAL, "cannot malloc tgid array (%d tasks)",
cnt);
} else {
if (!(tt->task_local = (void *)
realloc(tt->task_local, cnt * sizeof(void *))))
error(FATAL,
"%scannot realloc kernel task array (%d tasks)",
(pc->flags & RUNTIME) ? "" : "\n", cnt);
if (!(tt->context_array = (struct task_context *)
realloc(tt->context_array,
cnt * sizeof(struct task_context))))
error(FATAL,
"%scannot realloc context array (%d tasks)",
(pc->flags & RUNTIME) ? "" : "\n", cnt);
if (!(tt->context_by_task = (struct task_context **)
realloc(tt->context_by_task,
cnt * sizeof(struct task_context*))))
error(FATAL,
"%scannot realloc context_by_task array (%d tasks)",
(pc->flags & RUNTIME) ? "" : "\n", cnt);
if (!(tt->tgid_array = (struct tgid_context *)
realloc(tt->tgid_array,
cnt * sizeof(struct tgid_context))))
error(FATAL,
"%scannot realloc tgid array (%d tasks)",
(pc->flags & RUNTIME) ? "" : "\n", cnt);
}
}
/*
* This routine runs one time on dumpfiles, and constantly on live systems.
* It walks through the kernel task array looking for active tasks, and
* populates the local task table with their essential data.
*/
static void
refresh_fixed_task_table(void)
{
int i;
ulong *tlp;
ulong curtask;
ulong retries;
ulong curpid;
char *tp;
#define TASK_FREE(x) ((x == 0) || (((ulong)(x) >= tt->task_start) && \
((ulong)(x) < tt->task_end)))
#define TASK_IN_USE(x) (!TASK_FREE(x))
if (DUMPFILE() && (tt->flags & TASK_INIT_DONE))
return;
if (DUMPFILE()) {
fprintf(fp, (pc->flags & SILENT) || !(pc->flags & TTY) ?
"" : "%splease wait... (gathering task table data)",
GDB_PATCHED() ? "" : "\n");
fflush(fp);
if (!symbol_exists("panic_threads"))
tt->flags |= POPULATE_PANIC;
}
if (ACTIVE() && !(tt->flags & TASK_REFRESH))
return;
curpid = NO_PID;
curtask = NO_TASK;
/*
* The current task's task_context entry may change,
* or the task may not even exist anymore.
*/
if (ACTIVE() && (tt->flags & TASK_INIT_DONE)) {
curtask = CURRENT_TASK();
curpid = CURRENT_PID();
}
retries = 0;
retry:
if (!readmem(tt->task_start, KVADDR, tt->task_local,
tt->max_tasks * sizeof(void *), "kernel task array",
RETURN_ON_ERROR))
error(FATAL, "cannot read kernel task array");
clear_task_cache();
for (i = 0, tlp = (ulong *)tt->task_local, tt->running_tasks = 0;
i < tt->max_tasks; i++, tlp++) {
if (TASK_IN_USE(*tlp)) {
if (!(tp = fill_task_struct(*tlp))) {
if (DUMPFILE())
continue;
retries++;
goto retry;
}
add_context(*tlp, tp);
}
}