-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[darwin-framework-tool] Add a memory dump-graph command (#36017)
- Loading branch information
1 parent
1bb9f63
commit 1744732
Showing
8 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "commands/common/Commands.h" | ||
#include "commands/memory/DumpMemoryGraphCommand.h" | ||
|
||
void registerCommandsMemory(Commands & commands) | ||
{ | ||
const char * clusterName = "memory"; | ||
commands_list clusterCommands = { | ||
make_unique<DumpMemoryGraphCommand>(), | ||
}; | ||
|
||
commands.RegisterCommandSet(clusterName, clusterCommands, "Commands for inspecting program memory."); | ||
} |
42 changes: 42 additions & 0 deletions
42
examples/darwin-framework-tool/commands/memory/DumpMemoryGraphCommand.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#include "../common/CHIPCommandBridge.h" | ||
|
||
#include <lib/core/Optional.h> | ||
|
||
/** | ||
* This command dump the memory graph of the program. | ||
*/ | ||
|
||
class DumpMemoryGraphCommand : public CHIPCommandBridge | ||
{ | ||
public: | ||
DumpMemoryGraphCommand() : CHIPCommandBridge("dump-graph") | ||
{ | ||
AddArgument("filepath", &mFilePath, | ||
"An optional filepath to save the memory graph to. Defaults to 'darwin-framework-tool.memgraph"); | ||
} | ||
|
||
/////////// CHIPCommandBridge Interface ///////// | ||
CHIP_ERROR RunCommand() override; | ||
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Milliseconds32(0); } | ||
|
||
private: | ||
chip::Optional<char *> mFilePath; | ||
}; |
28 changes: 28 additions & 0 deletions
28
examples/darwin-framework-tool/commands/memory/DumpMemoryGraphCommand.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#include "DumpMemoryGraphCommand.h" | ||
|
||
#include "LeaksTool.h" | ||
|
||
CHIP_ERROR DumpMemoryGraphCommand::RunCommand() | ||
{ | ||
auto err = DumpMemoryGraph(mFilePath); | ||
SetCommandExitStatus(err); | ||
return err; | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/darwin-framework-tool/commands/memory/LeaksTool.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <lib/core/Optional.h> | ||
|
||
CHIP_ERROR DumpMemoryGraph(chip::Optional<char *> filePath); | ||
CHIP_ERROR PrintPools(); |
79 changes: 79 additions & 0 deletions
79
examples/darwin-framework-tool/commands/memory/LeaksTool.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
|
||
#include "LeaksTool.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
#include <unistd.h> // For getpid() | ||
|
||
constexpr const char * kDefaultOutputGraphPath = "darwin-framework-tool.memgraph"; | ||
|
||
@interface LeaksTool : NSObject | ||
- (BOOL)runWithArguments:(NSArray<NSString *> * _Nullable)arguments; | ||
@end | ||
|
||
@implementation LeaksTool | ||
|
||
- (BOOL)runWithArguments:(NSArray<NSString *> * _Nullable)arguments | ||
{ | ||
pid_t pid = getpid(); | ||
__auto_type * pidString = [NSString stringWithFormat:@"%d", pid]; | ||
|
||
__auto_type * task = [[NSTask alloc] init]; | ||
task.launchPath = @"/usr/bin/leaks"; | ||
task.arguments = @[ pidString ]; | ||
if (arguments) { | ||
task.arguments = [task.arguments arrayByAddingObjectsFromArray:arguments]; | ||
} | ||
|
||
__auto_type * pipe = [NSPipe pipe]; | ||
task.standardOutput = pipe; | ||
task.standardError = pipe; | ||
|
||
__auto_type * fileHandle = [pipe fileHandleForReading]; | ||
[task launch]; | ||
[task waitUntilExit]; | ||
|
||
int exitCode = [task terminationStatus]; | ||
if (exitCode != EXIT_SUCCESS) { | ||
return NO; | ||
} | ||
|
||
__auto_type * data = [fileHandle readDataToEndOfFile]; | ||
__auto_type * output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | ||
NSLog(@"%@", output); | ||
|
||
return YES; | ||
} | ||
|
||
@end | ||
|
||
CHIP_ERROR DumpMemoryGraph(chip::Optional<char *> filePath) | ||
{ | ||
NSMutableString * outputGraphArgument = [NSMutableString stringWithFormat:@"--outputGraph=%s", kDefaultOutputGraphPath]; | ||
if (filePath.HasValue()) { | ||
outputGraphArgument = [NSMutableString stringWithFormat:@"--outputGraph=%s", filePath.Value()]; | ||
} | ||
|
||
__auto_type * leaksTool = [[LeaksTool alloc] init]; | ||
if (![leaksTool runWithArguments:@[ outputGraphArgument ]]) { | ||
return CHIP_ERROR_INTERNAL; | ||
} | ||
|
||
return CHIP_NO_ERROR; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters