• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "executor/memory_dumper.h"
16 #include "dump_common_utils.h"
17 
18 using namespace std;
19 namespace OHOS {
20 namespace HiviewDFX {
MemoryDumper()21 MemoryDumper::MemoryDumper()
22 {
23     if (memoryInfo_ == nullptr) {
24         memoryInfo_ = std::make_unique<MemoryInfo>();
25     }
26     if (smapsMemoryInfo_ == nullptr) {
27         smapsMemoryInfo_ = std::make_unique<SmapsMemoryInfo>();
28     }
29 }
30 
~MemoryDumper()31 MemoryDumper::~MemoryDumper()
32 {
33 }
34 
PreExecute(const shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)35 DumpStatus MemoryDumper::PreExecute(const shared_ptr<DumperParameter> &parameter, StringMatrix dumpDatas)
36 {
37     pid_ = parameter->GetOpts().memPid_;
38     isShowMaps_ = parameter->GetOpts().isShowSmaps_;
39     isShowSmapsInfo_ =  parameter->GetOpts().isShowSmapsInfo_;
40     dumpDatas_ = dumpDatas;
41     return DumpStatus::DUMP_OK;
42 }
43 
Execute()44 DumpStatus MemoryDumper::Execute()
45 {
46     if (dumpDatas_ != nullptr && memoryInfo_ != nullptr) {
47         if (pid_ >= 0) {
48             if (isShowMaps_) {
49                 bool isShowMapsFlag = smapsMemoryInfo_->ShowMemorySmapsByPid(pid_, dumpDatas_, isShowSmapsInfo_);
50                 status_ = isShowMapsFlag ? DumpStatus::DUMP_OK : DumpStatus::DUMP_FAIL;
51                 return status_;
52             }
53             bool success = memoryInfo_->GetMemoryInfoByPid(pid_, dumpDatas_);
54             if (success) {
55                 status_ = DumpStatus::DUMP_OK;
56             } else {
57                 status_ = DumpStatus::DUMP_FAIL;
58             }
59         } else {
60             status_ = memoryInfo_->GetMemoryInfoNoPid(dumpDatas_);
61         }
62     }
63     return status_;
64 }
65 
AfterExecute()66 DumpStatus MemoryDumper::AfterExecute()
67 {
68     return status_;
69 }
70 } // namespace HiviewDFX
71 } // namespace OHOS