• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (c) 2023 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 "dfx_memory.h"
17  #include <algorithm>
18  #include <securec.h>
19  #include "dfx_define.h"
20  #include "dfx_log.h"
21  
22  namespace OHOS {
23  namespace HiviewDFX {
MemoryCopy(void * dst,uint8_t * data,size_t copySize,size_t offset,size_t dataSize)24  size_t DfxMemory::MemoryCopy(void *dst, uint8_t *data, size_t copySize, size_t offset, size_t dataSize)
25  {
26      if (offset >= dataSize) {
27          return 0;
28      }
29  
30      size_t bytesLeft = dataSize - static_cast<size_t>(offset);
31      const unsigned char *actualBase = static_cast<const unsigned char *>(data) + offset;
32      size_t actualLen = std::min(bytesLeft, copySize);
33      if (memcpy_s(dst, actualLen, actualBase, actualLen) != 0) {
34          DFXLOG_ERROR("%s :: memcpy error\n", __func__);
35      }
36      return actualLen;
37  }
38  
ReadFully(uint64_t addr,void * dst,size_t size)39  bool DfxMemory::ReadFully(uint64_t addr, void* dst, size_t size)
40  {
41      size_t rc = Read(addr, dst, size);
42      return rc == size;
43  }
44  
ReadString(uint64_t addr,std::string * dst,size_t maxRead)45  bool DfxMemory::ReadString(uint64_t addr, std::string* dst, size_t maxRead)
46  {
47      char buffer[LOG_BUF_LEN] = {0};
48      size_t size = 0;
49      for (size_t nRead = 0; nRead < maxRead; nRead += size) {
50          size_t read = std::min(sizeof(buffer), maxRead - nRead);
51          size = Read(addr + nRead, buffer, read);
52          if (size == 0) {
53              return false;
54          }
55          size_t length = strnlen(buffer, size);
56          if (length < size) {
57              if (nRead == 0) {
58                  dst->assign(buffer, length);
59                  return true;
60              } else {
61                  dst->assign(nRead + length, '\0');
62                  return ReadFully(addr, (void*)(dst->data()), dst->size());
63              }
64          }
65      }
66      return false;
67  }
68  
Read32(uint64_t addr,uint32_t * dst)69  inline bool DfxMemory::Read32(uint64_t addr, uint32_t* dst)
70  {
71      return ReadFully(addr, dst, sizeof(uint32_t));
72  }
73  
Read64(uint64_t addr,uint64_t * dst)74  inline bool DfxMemory::Read64(uint64_t addr, uint64_t* dst)
75  {
76      return ReadFully(addr, dst, sizeof(uint64_t));
77  }
78  
79  } // namespace HiviewDFX
80  } // namespace OHOS
81