Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: base/trace_event/process_memory_maps_dump_provider_unittest.cc

Issue 951463002: [tracing] Add memory maps dumper impl for Linux/Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mmaps_skeleton
Patch Set: Add Kb check + malformed regions Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/trace_event/process_memory_maps_dump_provider.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/process_memory_maps_dump_provider.h" 5 #include "base/trace_event/process_memory_maps_dump_provider.h"
6 6
7 #include <fstream> 7 #include <fstream>
8 #include <sstream> 8 #include <sstream>
9 9
10 #include "base/trace_event/process_memory_dump.h" 10 #include "base/trace_event/process_memory_dump.h"
11 #include "base/trace_event/process_memory_maps.h" 11 #include "base/trace_event/process_memory_maps.h"
12 #include "base/trace_event/trace_event_argument.h" 12 #include "base/trace_event/trace_event_argument.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace {
16 const char kTestSmaps1[] =
17 "00400000-004be000 r-xp 00000000 fc:01 1234 /file/1\n"
18 "Size: 760 kB\n"
19 "Rss: 296 kB\n"
20 "Pss: 162 kB\n"
21 "Shared_Clean: 228 kB\n"
22 "Shared_Dirty: 0 kB\n"
23 "Private_Clean: 0 kB\n"
24 "Private_Dirty: 68 kB\n"
25 "Referenced: 296 kB\n"
26 "Anonymous: 68 kB\n"
27 "AnonHugePages: 0 kB\n"
28 "Swap: 0 kB\n"
29 "KernelPageSize: 4 kB\n"
30 "MMUPageSize: 4 kB\n"
31 "Locked: 0 kB\n"
32 "VmFlags: rd ex mr mw me dw sd\n"
33 "ff000000-ff800000 -w-p 00001080 fc:01 0 /file/name with space\n"
34 "Size: 0 kB\n"
35 "Rss: 128 kB\n"
36 "Pss: 128 kB\n"
37 "Shared_Clean: 124 kB\n"
38 "Shared_Dirty: 0 kB\n"
39 "Private_Clean: 68 kB\n"
40 "Private_Dirty: 0 kB\n"
41 "Referenced: 296 kB\n"
42 "Anonymous: 0 kB\n"
43 "AnonHugePages: 0 kB\n"
44 "Swap: 0 kB\n"
45 "KernelPageSize: 4 kB\n"
46 "MMUPageSize: 4 kB\n"
47 "Locked: 0 kB\n"
48 "VmFlags: rd ex mr mw me dw sd";
49
50 const char kTestSmaps2[] =
51 // An invalid region, with zero size and overlapping with the last one
Sami 2015/02/24 14:22:43 nit: extra space here
Primiano Tucci (use gerrit) 2015/02/24 15:43:25 Done.
52 // (See crbug.com/461237).
53 "7fe7ce79c000-7fe7ce79c000 ---p 00000000 00:00 0 \n"
54 "Size: 4 kB\n"
55 "Rss: 0 kB\n"
56 "Pss: 0 kB\n"
57 "Shared_Clean: 0 kB\n"
58 "Shared_Dirty: 0 kB\n"
59 "Private_Clean: 0 kB\n"
60 "Private_Dirty: 0 kB\n"
61 "Referenced: 0 kB\n"
62 "Anonymous: 0 kB\n"
63 "AnonHugePages: 0 kB\n"
64 "Swap: 0 kB\n"
65 "KernelPageSize: 4 kB\n"
66 "MMUPageSize: 4 kB\n"
67 "Locked: 0 kB\n"
68 "VmFlags: rd ex mr mw me dw sd\n"
69 // A invalid region with its range going backwards.
70 "00400000-00200000 ---p 00000000 00:00 0 \n"
71 "Size: 4 kB\n"
72 "Rss: 0 kB\n"
73 "Pss: 0 kB\n"
74 "Shared_Clean: 0 kB\n"
75 "Shared_Dirty: 0 kB\n"
76 "Private_Clean: 0 kB\n"
77 "Private_Dirty: 0 kB\n"
78 "Referenced: 0 kB\n"
79 "Anonymous: 0 kB\n"
80 "AnonHugePages: 0 kB\n"
81 "Swap: 0 kB\n"
82 "KernelPageSize: 4 kB\n"
83 "MMUPageSize: 4 kB\n"
84 "Locked: 0 kB\n"
85 "VmFlags: rd ex mr mw me dw sd\n"
86 // A good anonymous region at the end.
87 "7fe7ce79c000-7fe7ce7a8000 ---p 00000000 00:00 0 \n"
88 "Size: 48 kB\n"
89 "Rss: 40 kB\n"
90 "Pss: 0 kB\n"
91 "Shared_Clean: 16 kB\n"
92 "Shared_Dirty: 12 kB\n"
93 "Private_Clean: 8 kB\n"
94 "Private_Dirty: 4 kB\n"
95 "Referenced: 40 kB\n"
96 "Anonymous: 16 kB\n"
97 "AnonHugePages: 0 kB\n"
98 "Swap: 0 kB\n"
99 "KernelPageSize: 4 kB\n"
100 "MMUPageSize: 4 kB\n"
101 "Locked: 0 kB\n"
102 "VmFlags: rd wr mr mw me ac sd\n";
103 } // namespace
104
15 namespace base { 105 namespace base {
16 namespace trace_event { 106 namespace trace_event {
17 107
18 #if defined(OS_LINUX) || defined(OS_ANDROID) 108 #if defined(OS_LINUX) || defined(OS_ANDROID)
19 TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) { 109 TEST(ProcessMemoryMapsDumpProviderTest, ParseProcSmaps) {
110 const uint32 kProtR = ProcessMemoryMaps::VMRegion::kProtectionFlagsRead;
111 const uint32 kProtW = ProcessMemoryMaps::VMRegion::kProtectionFlagsWrite;
112 const uint32 kProtX = ProcessMemoryMaps::VMRegion::kProtectionFlagsExec;
113
20 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance(); 114 auto pmmdp = ProcessMemoryMapsDumpProvider::GetInstance();
21 115
22 // Emulate a non-existent /proc/self/smaps. 116 // Emulate a non-existent /proc/self/smaps.
23 ProcessMemoryDump pmd_invalid; 117 ProcessMemoryDump pmd_invalid;
24 std::ifstream non_existent_file("/tmp/does-not-exist"); 118 std::ifstream non_existent_file("/tmp/does-not-exist");
25 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file; 119 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &non_existent_file;
26 CHECK_EQ(false, non_existent_file.good()); 120 CHECK_EQ(false, non_existent_file.good());
27 pmmdp->DumpInto(&pmd_invalid); 121 pmmdp->DumpInto(&pmd_invalid);
28 ASSERT_FALSE(pmd_invalid.has_process_mmaps()); 122 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
29 123
30 // Emulate an empty /proc/self/smaps. 124 // Emulate an empty /proc/self/smaps.
31 std::ifstream empty_file("/dev/null"); 125 std::ifstream empty_file("/dev/null");
32 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &empty_file; 126 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &empty_file;
33 CHECK_EQ(true, empty_file.good()); 127 CHECK_EQ(true, empty_file.good());
34 pmmdp->DumpInto(&pmd_invalid); 128 pmmdp->DumpInto(&pmd_invalid);
35 ASSERT_FALSE(pmd_invalid.has_process_mmaps()); 129 ASSERT_FALSE(pmd_invalid.has_process_mmaps());
36 130
37 // TODO(primiano): in the next CLs add the code to parse some actual smaps 131 // Parse the 1st smaps file.
38 // files and check that the parsing logic of the dump provider holds. 132 ProcessMemoryDump pmd_1;
133 std::istringstream test_smaps_1(kTestSmaps1);
134 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_1;
135 pmmdp->DumpInto(&pmd_1);
136 ASSERT_TRUE(pmd_1.has_process_mmaps());
137 const auto& regions_1 = pmd_1.process_mmaps()->vm_regions();
138 ASSERT_EQ(2UL, regions_1.size());
139
140 EXPECT_EQ(0x00400000UL, regions_1[0].start_address);
141 EXPECT_EQ(0x004be000UL - 0x00400000UL, regions_1[0].size_in_bytes);
142 EXPECT_EQ(kProtR | kProtX, regions_1[0].protection_flags);
143 EXPECT_EQ("/file/1", regions_1[0].mapped_file);
144 EXPECT_EQ(0UL, regions_1[0].mapped_file_offset);
145 EXPECT_EQ(296 * 1024UL, regions_1[0].byte_stats_resident);
146 EXPECT_EQ(68 * 1024UL, regions_1[0].byte_stats_anonymous);
147
148 EXPECT_EQ(0xff000000UL, regions_1[1].start_address);
149 EXPECT_EQ(0xff800000UL - 0xff000000UL, regions_1[1].size_in_bytes);
150 EXPECT_EQ(kProtW, regions_1[1].protection_flags);
151 EXPECT_EQ("/file/name with space", regions_1[1].mapped_file);
152 EXPECT_EQ(0x00001080UL, regions_1[1].mapped_file_offset);
153 EXPECT_EQ(128 * 1024UL, regions_1[1].byte_stats_resident);
154 EXPECT_EQ(0UL, regions_1[1].byte_stats_anonymous);
155
156 // Parse the 2nd smaps file.
157 ProcessMemoryDump pmd_2;
158 std::istringstream test_smaps_2(kTestSmaps2);
159 ProcessMemoryMapsDumpProvider::proc_smaps_for_testing = &test_smaps_2;
160 pmmdp->DumpInto(&pmd_2);
161 ASSERT_TRUE(pmd_2.has_process_mmaps());
162 const auto& regions_2 = pmd_2.process_mmaps()->vm_regions();
163 ASSERT_EQ(1UL, regions_2.size());
164 EXPECT_EQ(0x7fe7ce79c000UL, regions_2[0].start_address);
165 EXPECT_EQ(0x7fe7ce7a8000UL - 0x7fe7ce79c000UL, regions_2[0].size_in_bytes);
166 EXPECT_EQ(0U, regions_2[0].protection_flags);
167 EXPECT_EQ("", regions_2[0].mapped_file);
168 EXPECT_EQ(0UL, regions_2[0].mapped_file_offset);
169 EXPECT_EQ(40 * 1024UL, regions_2[0].byte_stats_resident);
170 EXPECT_EQ(16 * 1024UL, regions_2[0].byte_stats_anonymous);
39 } 171 }
40 #endif // defined(OS_LINUX) || defined(OS_ANDROID) 172 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
41 173
42 } // namespace trace_event 174 } // namespace trace_event
43 } // namespace base 175 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/process_memory_maps_dump_provider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698