summaryrefslogtreecommitdiffstats
path: root/libmemunreachable
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2018-05-15 18:01:56 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2018-05-15 18:01:56 +0000
commit0f837fe8ca3a9494876abc481dbe73b0c07d16bb (patch)
treef3967373b991f135e9018aee07877f306cd9bb12 /libmemunreachable
parentda6ab73420760da9fe39ba3f26068c6d1ad46537 (diff)
parent3841accba82b47087647d023e670219ac3627710 (diff)
downloadsystem_core-0f837fe8ca3a9494876abc481dbe73b0c07d16bb.tar.gz
system_core-0f837fe8ca3a9494876abc481dbe73b0c07d16bb.tar.bz2
system_core-0f837fe8ca3a9494876abc481dbe73b0c07d16bb.zip
Merge "libprocinfo: add functions reading process map file."
Diffstat (limited to 'libmemunreachable')
-rw-r--r--libmemunreachable/Android.bp2
-rw-r--r--libmemunreachable/LineBuffer.cpp66
-rw-r--r--libmemunreachable/LineBuffer.h40
-rw-r--r--libmemunreachable/ProcessMappings.cpp51
-rw-r--r--libmemunreachable/ProcessMappings.h9
5 files changed, 28 insertions, 140 deletions
diff --git a/libmemunreachable/Android.bp b/libmemunreachable/Android.bp
index f164a19e0..248a9d25c 100644
--- a/libmemunreachable/Android.bp
+++ b/libmemunreachable/Android.bp
@@ -29,7 +29,6 @@ cc_library {
"HeapWalker.cpp",
"LeakFolding.cpp",
"LeakPipe.cpp",
- "LineBuffer.cpp",
"MemUnreachable.cpp",
"ProcessMappings.cpp",
"PtracerThread.cpp",
@@ -38,6 +37,7 @@ cc_library {
static_libs: [
"libc_malloc_debug_backtrace",
+ "libprocinfo",
],
// Only need this for arm since libc++ uses its own unwind code that
// doesn't mix with the other default unwind code.
diff --git a/libmemunreachable/LineBuffer.cpp b/libmemunreachable/LineBuffer.cpp
deleted file mode 100644
index 4ea0542c7..000000000
--- a/libmemunreachable/LineBuffer.cpp
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// Copied from system/extras/memory_replay/LineBuffer.cpp
-// TODO(ccross): find a way to share between libmemunreachable and memory_replay?
-
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "LineBuffer.h"
-
-namespace android {
-
-LineBuffer::LineBuffer(int fd, char* buffer, size_t buffer_len)
- : fd_(fd), buffer_(buffer), buffer_len_(buffer_len) {}
-
-bool LineBuffer::GetLine(char** line, size_t* line_len) {
- while (true) {
- if (bytes_ > 0) {
- char* newline = reinterpret_cast<char*>(memchr(buffer_ + start_, '\n', bytes_));
- if (newline != nullptr) {
- *newline = '\0';
- *line = buffer_ + start_;
- start_ = newline - buffer_ + 1;
- bytes_ -= newline - *line + 1;
- *line_len = newline - *line;
- return true;
- }
- }
- if (start_ > 0) {
- // Didn't find anything, copy the current to the front of the buffer.
- memmove(buffer_, buffer_ + start_, bytes_);
- start_ = 0;
- }
- ssize_t bytes = TEMP_FAILURE_RETRY(read(fd_, buffer_ + bytes_, buffer_len_ - bytes_ - 1));
- if (bytes <= 0) {
- if (bytes_ > 0) {
- // The read data might not contain a nul terminator, so add one.
- buffer_[bytes_] = '\0';
- *line = buffer_ + start_;
- *line_len = bytes_;
- bytes_ = 0;
- start_ = 0;
- return true;
- }
- return false;
- }
- bytes_ += bytes;
- }
-}
-
-} // namespace android
diff --git a/libmemunreachable/LineBuffer.h b/libmemunreachable/LineBuffer.h
deleted file mode 100644
index cc6cd0c6a..000000000
--- a/libmemunreachable/LineBuffer.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _LIBMEMUNREACHABLE_LINE_BUFFER_H
-#define _LIBMEMUNREACHABLE_LINE_BUFFER_H
-
-#include <stdint.h>
-
-namespace android {
-
-class LineBuffer {
- public:
- LineBuffer(int fd, char* buffer, size_t buffer_len);
-
- bool GetLine(char** line, size_t* line_len);
-
- private:
- int fd_;
- char* buffer_ = nullptr;
- size_t buffer_len_ = 0;
- size_t start_ = 0;
- size_t bytes_ = 0;
-};
-
-} // namespace android
-
-#endif // _LIBMEMUNREACHABLE_LINE_BUFFER_H
diff --git a/libmemunreachable/ProcessMappings.cpp b/libmemunreachable/ProcessMappings.cpp
index 9a0687052..701ce1670 100644
--- a/libmemunreachable/ProcessMappings.cpp
+++ b/libmemunreachable/ProcessMappings.cpp
@@ -14,21 +14,30 @@
* limitations under the License.
*/
+#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <android-base/unique_fd.h>
+#include <procinfo/process_map.h>
-#include "LineBuffer.h"
#include "ProcessMappings.h"
-#include "log.h"
namespace android {
-// This function is not re-entrant since it uses a static buffer for
-// the line data.
+struct ReadMapCallback {
+ ReadMapCallback(allocator::vector<Mapping>& mappings) : mappings_(mappings) {}
+
+ void operator()(uint64_t start, uint64_t end, uint16_t flags, uint64_t, const char* name) const {
+ mappings_.emplace_back(start, end, flags & PROT_READ, flags & PROT_WRITE, flags & PROT_EXEC,
+ name);
+ }
+
+ allocator::vector<Mapping>& mappings_;
+};
+
bool ProcessMappings(pid_t pid, allocator::vector<Mapping>& mappings) {
char map_buffer[1024];
snprintf(map_buffer, sizeof(map_buffer), "/proc/%d/maps", pid);
@@ -36,35 +45,13 @@ bool ProcessMappings(pid_t pid, allocator::vector<Mapping>& mappings) {
if (fd == -1) {
return false;
}
-
- LineBuffer line_buf(fd, map_buffer, sizeof(map_buffer));
- char* line;
- size_t line_len;
- while (line_buf.GetLine(&line, &line_len)) {
- int name_pos;
- char perms[5];
- Mapping mapping{};
- if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %*x %*x:%*x %*d %n", &mapping.begin,
- &mapping.end, perms, &name_pos) == 3) {
- if (perms[0] == 'r') {
- mapping.read = true;
- }
- if (perms[1] == 'w') {
- mapping.write = true;
- }
- if (perms[2] == 'x') {
- mapping.execute = true;
- }
- if (perms[3] == 'p') {
- mapping.priv = true;
- }
- if ((size_t)name_pos < line_len) {
- strlcpy(mapping.name, line + name_pos, sizeof(mapping.name));
- }
- mappings.emplace_back(mapping);
- }
+ allocator::string content(mappings.get_allocator());
+ ssize_t n;
+ while ((n = TEMP_FAILURE_RETRY(read(fd, map_buffer, sizeof(map_buffer)))) > 0) {
+ content.append(map_buffer, n);
}
- return true;
+ ReadMapCallback callback(mappings);
+ return android::procinfo::ReadMapFileContent(&content[0], callback);
}
} // namespace android
diff --git a/libmemunreachable/ProcessMappings.h b/libmemunreachable/ProcessMappings.h
index a0e97e9fc..94da69b11 100644
--- a/libmemunreachable/ProcessMappings.h
+++ b/libmemunreachable/ProcessMappings.h
@@ -17,6 +17,8 @@
#ifndef LIBMEMUNREACHABLE_PROCESS_MAPPING_H_
#define LIBMEMUNREACHABLE_PROCESS_MAPPING_H_
+#include <string.h>
+
#include "Allocator.h"
namespace android {
@@ -27,8 +29,13 @@ struct Mapping {
bool read;
bool write;
bool execute;
- bool priv;
char name[96];
+
+ Mapping() {}
+ Mapping(uintptr_t begin, uintptr_t end, bool read, bool write, bool execute, const char* name)
+ : begin(begin), end(end), read(read), write(write), execute(execute) {
+ strlcpy(this->name, name, sizeof(this->name));
+ }
};
// This function is not re-entrant since it uses a static buffer for