Skip to content

Commit

Permalink
[darwin-framework-tool] Add a memory dump-graph command
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Oct 10, 2024
1 parent 8a01f93 commit 6a49702
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/darwin-framework-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ executable("darwin-framework-tool") {
"commands/discover/Commands.h",
"commands/discover/DiscoverCommissionablesCommand.h",
"commands/discover/DiscoverCommissionablesCommand.mm",
"commands/memory/Commands.h",
"commands/memory/DumpMemoryGraphCommand.h",
"commands/memory/DumpMemoryGraphCommand.mm",
"commands/memory/LeaksTool.h",
"commands/memory/LeaksTool.mm",
"commands/pairing/Commands.h",
"commands/pairing/DeviceControllerDelegateBridge.mm",
"commands/pairing/GetCommissionerNodeIdCommand.h",
Expand Down
32 changes: 32 additions & 0 deletions examples/darwin-framework-tool/commands/memory/Commands.h
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.");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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;
};
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 examples/darwin-framework-tool/commands/memory/LeaksTool.h
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 examples/darwin-framework-tool/commands/memory/LeaksTool.mm
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;
}
2 changes: 2 additions & 0 deletions examples/darwin-framework-tool/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "commands/delay/Commands.h"
#include "commands/discover/Commands.h"
#include "commands/interactive/Commands.h"
#include "commands/memory/Commands.h"
#include "commands/pairing/Commands.h"
#include "commands/payload/Commands.h"
#include "commands/provider/Commands.h"
Expand All @@ -46,6 +47,7 @@ int main(int argc, const char * argv[])
registerCommandsDelay(commands);
registerCommandsDiscover(commands);
registerCommandsInteractive(commands);
registerCommandsMemory(commands);
registerCommandsPayload(commands);
registerClusterOtaSoftwareUpdateProviderInteractive(commands);
registerCommandsStorage(commands);
Expand Down

0 comments on commit 6a49702

Please sign in to comment.