• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <fcntl.h>
17 #include <unistd.h>
18 
19 #include "memory_reader.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 
ThreadMemoryReader(uintptr_t stackBegin,uintptr_t stackEnd)24 ThreadMemoryReader::ThreadMemoryReader(uintptr_t stackBegin, uintptr_t stackEnd) : MemoryReader(),
25     stackBegin_(stackBegin), stackEnd_(stackEnd) {}
26 
ReadMemory(const uint64_t src,void * dst,size_t dataSize)27 bool ThreadMemoryReader::ReadMemory(const uint64_t src, void* dst, size_t dataSize)
28 {
29     if (src >= stackBegin_ && src < stackEnd_  && (stackEnd_ - src) > dataSize) {
30         for (size_t i = 0; i < dataSize; i++) {
31             static_cast<unsigned char*>(dst)[i] = reinterpret_cast<const unsigned char*>(src)[i];
32         }
33         return true;
34     }
35     return false;
36 }
37 
ProcessMemoryReader()38 ProcessMemoryReader::ProcessMemoryReader() : MemoryReader()
39 {
40     int pipe[2]{-1};
41     if (pipe2(pipe, O_CLOEXEC) == 0) {
42         readFd_ = SmartFd(pipe[0], false);
43         writeFd_ = SmartFd(pipe[1], false);
44     }
45 }
46 
ReadMemory(const uint64_t src,void * dst,size_t dataSize)47 bool ProcessMemoryReader::ReadMemory(const uint64_t src, void* dst, size_t dataSize)
48 {
49     auto ret = write(writeFd_.GetFd(), reinterpret_cast<const void*>(src), dataSize);
50     return ret > 0 && ret == read(readFd_.GetFd(), dst, dataSize);
51 }
52 }
53 }