Debian Codegraph @

tests/test_thread.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import os
5 import os.path
6 import tempfile
7
8 from drgn import Program
9 from tests import TestCase
10 from tests.resources import get_resource
11
12
13 class TestLive(TestCase):
14 @classmethod
15 def setUpClass(cls):
16 super().setUpClass()
17 cls.prog = Program()
18 cls.prog.set_pid(os.getpid())
19
20 @classmethod
21 def tearDownClass(cls):
22 del cls.prog
23
24 def test_threads(self):
25 tids = [thread.tid for thread in self.prog.threads()]
26 self.assertIn(os.getpid(), tids)
27 for tid in tids:
28 self.assertEqual(self.prog.thread(tid).tid, tid)
29
30 def test_thread_not_found(self):
31 self.assertRaises(LookupError, self.prog.thread, 1)
32
33 def test_main_thread(self):
34 self.assertEqual(self.prog.main_thread().tid, os.getpid())
35
36 def test_crashed_thread(self):
37 self.assertRaisesRegex(
38 ValueError,
39 "crashed thread is only defined for core dumps",
40 self.prog.crashed_thread,
41 )
42
43 def test_thread_name(self):
44 with open(f"/proc/{os.getpid()}/comm", "r") as f:
45 comm = f.read().strip()
46 self.assertEqual(self.prog.main_thread().name, comm)
47 for thread in self.prog.threads():
48 self.assertIsNotNone(thread.name)
49
50
51 class TestCoreDump(TestCase):
52 TIDS = (
53 2265413,
54 2265414,
55 2265415,
56 2265416,
57 2265417,
58 2265418,
59 2265419,
60 2265420,
61 2265421,
62 2265422,
63 2265423,
64 2265424,
65 2265425,
66 )
67
68 MAIN_TID = 2265413
69 CRASHED_TID = 2265419
70
71 MAIN_THREAD_NAME = "segfault_random"
72
73 @classmethod
74 def setUpClass(cls):
75 super().setUpClass()
76 cls.prog = Program()
77 cls.prog.set_core_dump(get_resource("multithreaded.core"))
78
79 @classmethod
80 def tearDownClass(cls):
81 del cls.prog
82
83 def test_threads(self):
84 self.assertSequenceEqual(
85 sorted(thread.tid for thread in self.prog.threads()),
86 self.TIDS,
87 )
88
89 def test_thread(self):
90 for tid in self.TIDS:
91 self.assertEqual(self.prog.thread(tid).tid, tid)
92
93 def test_thread_not_found(self):
94 self.assertRaises(LookupError, self.prog.thread, 99)
95
96 def test_main_thread(self):
97 self.assertEqual(self.prog.main_thread().tid, self.MAIN_TID)
98
99 def test_crashed_thread(self):
100 self.assertEqual(self.prog.crashed_thread().tid, self.CRASHED_TID)
101
102 def test_thread_name(self):
103 self.assertEqual(self.prog.main_thread().name, self.MAIN_THREAD_NAME)
104 for tid in self.TIDS:
105 if tid != self.MAIN_TID:
106 self.assertIsNone(self.prog.thread(tid).name)
107
108
109 class TestCoreDumpLongName(TestCase):
110 @classmethod
111 def setUpClass(cls):
112 super().setUpClass()
113 cls.prog = Program()
114 with open(get_resource("crashme_static_pie.core"), "rb") as f:
115 data = f.read()
116 data = data.replace(b"crashme_static_\x00", b"crashme_static_p")
117 with tempfile.NamedTemporaryFile("wb") as f:
118 f.write(data)
119 f.flush()
120 cls.prog.set_core_dump(f.name)
121
122 @classmethod
123 def tearDownClass(cls):
124 del cls.prog
125
126 def test_thread_name(self):
127 self.assertEqual(self.prog.main_thread().name, "crashme_static_p")