1#include "gmock/gmock.h"
2#include "gtest/gtest.h"
3#include <limits.h>
4
5#include "lldb/Utility/StringExtractorGDBRemote.h"
6#include "lldb/lldb-defines.h"
7
8TEST(StringExtractorGDBRemoteTest, GetPidTid) {
9 StringExtractorGDBRemote ex("");
10 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
11
12 // invalid/short values
13
14 ex.Reset(str: "narf");
15 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
16
17 ex.Reset(str: ";1234");
18 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
19
20 ex.Reset(str: ".1234");
21 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
22
23 ex.Reset(str: "p");
24 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
25
26 ex.Reset(str: "pnarf");
27 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
28
29 ex.Reset(str: "p;1234");
30 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
31
32 ex.Reset(str: "p.1234");
33 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
34
35 ex.Reset(str: "p1234.");
36 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
37
38 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
39
40 ex.Reset(str: "p1234.;1234");
41 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
42
43 ex.Reset(str: "-2");
44 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
45
46 ex.Reset(str: "p1234.-2");
47 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
48
49 ex.Reset(str: "p-2");
50 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
51
52 ex.Reset(str: "p-2.1234");
53 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
54
55 // overflow
56
57 ex.Reset(str: "p10000000000000000");
58 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
59
60 ex.Reset(str: "p10000000000000000.0");
61 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
62
63 ex.Reset(str: "10000000000000000");
64 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
65
66 ex.Reset(str: "p0.10000000000000000");
67 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
68
69 ex.Reset(str: "p10000000000000000.10000000000000000");
70 EXPECT_EQ(ex.GetPidTid(0), std::nullopt);
71
72 // invalid: all processes but specific thread
73
74 ex.Reset(str: "p-1.0");
75 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
76
77 ex.Reset(str: "p-1.1234");
78 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
79
80 ex.Reset(str: "p-1.123456789ABCDEF0");
81 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
82
83 // unsupported: pid/tid 0
84
85 ex.Reset(str: "0");
86 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
87
88 ex.Reset(str: "p0");
89 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
90
91 ex.Reset(str: "p0.0");
92 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
93
94 ex.Reset(str: "p0.-1");
95 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
96
97 ex.Reset(str: "p0.1234");
98 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
99
100 ex.Reset(str: "p0.123456789ABCDEF0");
101 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
102
103 ex.Reset(str: "p1234.0");
104 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
105
106 ex.Reset(str: "p123456789ABCDEF0.0");
107 EXPECT_EQ(ex.GetPidTid(100), std::nullopt);
108
109 // pure thread id
110
111 ex.Reset(str: "-1");
112 EXPECT_THAT(*ex.GetPidTid(100),
113 ::testing::Pair(100, StringExtractorGDBRemote::AllThreads));
114
115 ex.Reset(str: "1234");
116 EXPECT_THAT(*ex.GetPidTid(100), ::testing::Pair(100, 0x1234ULL));
117
118 ex.Reset(str: "123456789ABCDEF0");
119 EXPECT_THAT(*ex.GetPidTid(100), ::testing::Pair(100, 0x123456789ABCDEF0ULL));
120
121 // pure process id
122
123 ex.Reset(str: "p-1");
124 EXPECT_THAT(*ex.GetPidTid(100),
125 ::testing::Pair(StringExtractorGDBRemote::AllProcesses,
126 StringExtractorGDBRemote::AllThreads));
127
128 ex.Reset(str: "p1234");
129 EXPECT_THAT(*ex.GetPidTid(100),
130 ::testing::Pair(0x1234ULL, StringExtractorGDBRemote::AllThreads));
131
132 ex.Reset(str: "p123456789ABCDEF0");
133 EXPECT_THAT(*ex.GetPidTid(100),
134 ::testing::Pair(0x123456789ABCDEF0ULL,
135 StringExtractorGDBRemote::AllThreads));
136
137 ex.Reset(str: "pFFFFFFFFFFFFFFFF");
138 EXPECT_THAT(*ex.GetPidTid(100),
139 ::testing::Pair(StringExtractorGDBRemote::AllProcesses,
140 StringExtractorGDBRemote::AllThreads));
141
142 // combined thread id + process id
143
144 ex.Reset(str: "p-1.-1");
145 EXPECT_THAT(*ex.GetPidTid(100),
146 ::testing::Pair(StringExtractorGDBRemote::AllProcesses,
147 StringExtractorGDBRemote::AllThreads));
148
149 ex.Reset(str: "p1234.-1");
150 EXPECT_THAT(*ex.GetPidTid(100),
151 ::testing::Pair(0x1234ULL, StringExtractorGDBRemote::AllThreads));
152
153 ex.Reset(str: "p1234.123456789ABCDEF0");
154 EXPECT_THAT(*ex.GetPidTid(100),
155 ::testing::Pair(0x1234ULL, 0x123456789ABCDEF0ULL));
156
157 ex.Reset(str: "p123456789ABCDEF0.-1");
158 EXPECT_THAT(*ex.GetPidTid(100),
159 ::testing::Pair(0x123456789ABCDEF0ULL,
160 StringExtractorGDBRemote::AllThreads));
161
162 ex.Reset(str: "p123456789ABCDEF0.1234");
163 EXPECT_THAT(*ex.GetPidTid(100),
164 ::testing::Pair(0x123456789ABCDEF0ULL, 0x1234ULL));
165
166 ex.Reset(str: "p123456789ABCDEF0.123456789ABCDEF0");
167 EXPECT_THAT(*ex.GetPidTid(100),
168 ::testing::Pair(0x123456789ABCDEF0ULL, 0x123456789ABCDEF0ULL));
169
170 ex.Reset(str: "p123456789ABCDEF0.123456789ABCDEF0");
171 EXPECT_THAT(*ex.GetPidTid(100),
172 ::testing::Pair(0x123456789ABCDEF0ULL, 0x123456789ABCDEF0ULL));
173}
174
175TEST(StringExtractorGDBRemoteTest, GetPidTidMultipleValues) {
176 StringExtractorGDBRemote ex("1234;p12;p1234.-1");
177 ASSERT_THAT(*ex.GetPidTid(100), ::testing::Pair(100, 0x1234ULL));
178 ASSERT_EQ(ex.GetChar(), ';');
179 ASSERT_THAT(*ex.GetPidTid(100),
180 ::testing::Pair(0x12ULL, StringExtractorGDBRemote::AllThreads));
181 ASSERT_EQ(ex.GetChar(), ';');
182 ASSERT_THAT(*ex.GetPidTid(100),
183 ::testing::Pair(0x1234ULL, StringExtractorGDBRemote::AllThreads));
184}
185

source code of lldb/unittests/Utility/StringExtractorGDBRemoteTest.cpp