Skip to content

Commit 34cee2b

Browse files
enh-googleGerrit Code Review
authored andcommitted
Merge "Use %n to parse maps in libpagemap."
2 parents 2f9d35a + 72f4a2f commit 34cee2b

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

libpagemap/Android.mk

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@
1313
# limitations under the License.
1414

1515
LOCAL_PATH := $(call my-dir)
16-
include $(CLEAR_VARS)
1716

17+
pagemap_src_files := \
18+
pm_kernel.c \
19+
pm_process.c \
20+
pm_map.c \
21+
pm_memusage.c \
22+
23+
include $(CLEAR_VARS)
1824
LOCAL_MODULE := libpagemap
1925
LOCAL_MODULE_TAGS := debug
20-
21-
LOCAL_SRC_FILES := \
22-
pm_kernel.c \
23-
pm_process.c \
24-
pm_map.c \
25-
pm_memusage.c
26-
26+
LOCAL_SRC_FILES := $(pagemap_src_files)
2727
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
28-
28+
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
2929
include $(BUILD_SHARED_LIBRARY)
30+
31+
include $(CLEAR_VARS)
32+
LOCAL_MODULE := pagemap_test
33+
LOCAL_SRC_FILES := pagemap_test.cpp
34+
LOCAL_SHARED_LIBRARIES := libpagemap
35+
include $(BUILD_NATIVE_TEST)

libpagemap/pagemap_test.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2015 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <gtest/gtest.h>
18+
19+
#include <pagemap/pagemap.h>
20+
21+
TEST(pagemap, maps) {
22+
pm_kernel_t* kernel;
23+
ASSERT_EQ(0, pm_kernel_create(&kernel));
24+
25+
pm_process_t* process;
26+
ASSERT_EQ(0, pm_process_create(kernel, getpid(), &process));
27+
28+
pm_map_t** maps;
29+
size_t num_maps;
30+
ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps));
31+
32+
bool found_heap = false;
33+
bool found_stack = false;
34+
for (size_t i = 0; i < num_maps; i++) {
35+
if (strcmp(maps[i]->name, "[heap]") == 0) found_heap = true;
36+
if (strcmp(maps[i]->name, "[stack]") == 0) found_stack = true;
37+
}
38+
39+
ASSERT_TRUE(found_heap);
40+
ASSERT_TRUE(found_stack);
41+
42+
free(maps);
43+
pm_process_destroy(process);
44+
pm_kernel_destroy(kernel);
45+
}

libpagemap/pm_process.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,13 @@ int pm_process_destroy(pm_process_t *proc) {
231231
}
232232

233233
#define INITIAL_MAPS 10
234-
#define MAX_LINE 1024
235234
#define MAX_PERMS 5
236235

237-
/*
238-
* #define FOO 123
239-
* S(FOO) => "123"
240-
*/
241-
#define _S(n) #n
242-
#define S(n) _S(n)
243-
244236
static int read_maps(pm_process_t *proc) {
245237
char filename[MAX_FILENAME];
246-
char line[MAX_LINE], name[MAX_LINE], perms[MAX_PERMS];
238+
char *line = NULL;
239+
size_t line_length = 0;
240+
char perms[MAX_PERMS];
247241
FILE *maps_f;
248242
pm_map_t *map, **maps, **new_maps;
249243
int maps_count, maps_size;
@@ -269,12 +263,15 @@ static int read_maps(pm_process_t *proc) {
269263
return errno;
270264
}
271265

272-
while (fgets(line, MAX_LINE, maps_f)) {
266+
while (getline(&line, &line_length, maps_f) != -1) {
267+
line[strlen(line) - 1] = '\0'; // Lose the newline.
268+
273269
if (maps_count >= maps_size) {
274270
new_maps = realloc(maps, 2 * maps_size * sizeof(pm_map_t*));
275271
if (!new_maps) {
276272
error = errno;
277273
free(maps);
274+
free(line);
278275
fclose(maps_f);
279276
return error;
280277
}
@@ -286,27 +283,29 @@ static int read_maps(pm_process_t *proc) {
286283

287284
map->proc = proc;
288285

289-
name[0] = '\0';
290-
sscanf(line, "%" SCNx64 "-%" SCNx64 " %s %" SCNx64 " %*s %*d %" S(MAX_LINE) "s",
291-
&map->start, &map->end, perms, &map->offset, name);
286+
int name_offset;
287+
sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %" SCNx64 " %*s %*d %n",
288+
&map->start, &map->end, perms, &map->offset, &name_offset);
292289

293-
map->name = malloc(strlen(name) + 1);
290+
map->name = strdup(line + name_offset);
294291
if (!map->name) {
295292
error = errno;
296293
for (; maps_count > 0; maps_count--)
297294
pm_map_destroy(maps[maps_count]);
298295
free(maps);
296+
free(line);
299297
fclose(maps_f);
300298
return error;
301299
}
302-
strcpy(map->name, name);
300+
303301
if (perms[0] == 'r') map->flags |= PM_MAP_READ;
304302
if (perms[1] == 'w') map->flags |= PM_MAP_WRITE;
305303
if (perms[2] == 'x') map->flags |= PM_MAP_EXEC;
306304

307305
maps_count++;
308306
}
309307

308+
free(line);
310309
fclose(maps_f);
311310

312311
new_maps = realloc(maps, maps_count * sizeof(pm_map_t*));

0 commit comments

Comments
 (0)