-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://memfault.com/ Adds support for pulling coredump data, events and traces from the device.
- Loading branch information
Michał Narajowski
committed
May 25, 2020
1 parent
4f3783e
commit 732b88d
Showing
6 changed files
with
257 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,110 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"mynewt.apache.org/newt/util" | ||
"mynewt.apache.org/newtmgr/newtmgr/nmutil" | ||
"mynewt.apache.org/newtmgr/nmxact/nmp" | ||
"mynewt.apache.org/newtmgr/nmxact/xact" | ||
) | ||
|
||
func memfaultDownloadCmd(cmd *cobra.Command, args []string) { | ||
if len(args) < 1 { | ||
nmUsage(cmd, nil) | ||
} | ||
|
||
tmpName := args[0] + ".tmp" | ||
progressBytes := 0 | ||
file, err := os.OpenFile(tmpName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660) | ||
if err != nil { | ||
nmUsage(cmd, util.NewNewtError(fmt.Sprintf( | ||
"Cannot open file %s - %s", tmpName, err.Error()))) | ||
} | ||
defer os.Remove(tmpName) | ||
defer file.Close() | ||
|
||
s, err := GetSesn() | ||
if err != nil { | ||
nmUsage(nil, err) | ||
} | ||
|
||
c := xact.NewMemfaultPullCmd() | ||
c.SetTxOptions(nmutil.TxOptions()) | ||
c.ProgressCb = func(c *xact.MemfaultPullCmd, rsp *nmp.MemfaultPullRsp) { | ||
progressBytes += len(rsp.Data) | ||
fmt.Printf("Pulled %d bytes\n", progressBytes) | ||
if _, err := file.Write(rsp.Data); err != nil { | ||
nmUsage(nil, util.ChildNewtError(err)) | ||
} | ||
} | ||
|
||
res, err := c.Run(s) | ||
if err != nil { | ||
nmUsage(nil, util.ChildNewtError(err)) | ||
} | ||
|
||
sres := res.(*xact.MemfaultPullResult) | ||
if sres.Status() != 0 { | ||
fmt.Printf("Error: %d\n", sres.Status()) | ||
return | ||
} | ||
|
||
err = file.Sync() | ||
if err != nil { | ||
nmUsage(nil, util.ChildNewtError(err)) | ||
} | ||
|
||
err = file.Close() | ||
if err != nil { | ||
nmUsage(nil, util.ChildNewtError(err)) | ||
} | ||
|
||
os.Rename(tmpName, args[0]) | ||
fmt.Printf("Done writing memfault file to %s\n", args[0]) | ||
} | ||
|
||
func memfaultCmd() *cobra.Command { | ||
memfaultCmd := &cobra.Command{ | ||
Use: "memfault", | ||
Short: "Manage Memfault data on a device", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.HelpFunc()(cmd, args) | ||
}, | ||
} | ||
|
||
memfaultEx := " " + nmutil.ToolInfo.ExeName + | ||
" -c olimex memfault pull\n" | ||
|
||
memfaultDownloadCmd := &cobra.Command{ | ||
Use: "pull <memfault-file> -c <conn_profile>", | ||
Short: "Pull memfault data from a device", | ||
Example: memfaultEx, | ||
Run: memfaultDownloadCmd, | ||
} | ||
memfaultCmd.AddCommand(memfaultDownloadCmd) | ||
|
||
return memfaultCmd | ||
} |
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
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,49 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
package nmp | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// $memfaultpull // | ||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
type MemfaultPullReq struct { | ||
NmpBase `codec:"-"` | ||
} | ||
|
||
type MemfaultPullRsp struct { | ||
NmpBase | ||
Status int `codec:"status"` | ||
Data []byte `codec:"data"` | ||
} | ||
|
||
func NewMemfaultPullReq() *MemfaultPullReq { | ||
r := &MemfaultPullReq{} | ||
fillNmpReq(r, NMP_OP_READ, NMP_GROUP_MEMFAULT, NMP_ID_MEMFAULT_PULL) | ||
return r | ||
} | ||
|
||
func (r *MemfaultPullReq) Msg() *NmpMsg { return MsgFromReq(r) } | ||
|
||
func NewMemfaultPullRsp() *MemfaultPullRsp { | ||
return &MemfaultPullRsp{} | ||
} | ||
|
||
func (r *MemfaultPullRsp) Msg() *NmpMsg { return MsgFromReq(r) } | ||
|
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,88 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
package xact | ||
|
||
import ( | ||
"mynewt.apache.org/newtmgr/nmxact/nmp" | ||
"mynewt.apache.org/newtmgr/nmxact/sesn" | ||
) | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// $memfaultpull // | ||
////////////////////////////////////////////////////////////////////////////// | ||
|
||
const ( | ||
MEMFAULT_PACKETIZER_STATUS_NO_MORE_DATA = 0 | ||
MEMFAULT_PACKETIZER_STATUS_END_OF_CHUNK = 1 | ||
MEMFAULT_PACKETIZER_STATUS_MORE_DATA_FOR_CHUNK = 2 | ||
) | ||
|
||
type MemfaultPullProgressFn func(c *MemfaultPullCmd, r *nmp.MemfaultPullRsp) | ||
type MemfaultPullCmd struct { | ||
CmdBase | ||
ProgressCb MemfaultPullProgressFn | ||
} | ||
|
||
type MemfaultPullResult struct { | ||
Rsps []*nmp.MemfaultPullRsp | ||
} | ||
|
||
func NewMemfaultPullCmd() *MemfaultPullCmd { | ||
return &MemfaultPullCmd{ | ||
CmdBase: NewCmdBase(), | ||
} | ||
} | ||
|
||
func newMemfaultPullResult() *MemfaultPullResult { | ||
return &MemfaultPullResult{} | ||
} | ||
|
||
func (r *MemfaultPullResult) Status() int { | ||
return 0 | ||
} | ||
|
||
func (c *MemfaultPullCmd) Run(s sesn.Sesn) (Result, error) { | ||
res := newMemfaultPullResult() | ||
|
||
for { | ||
r := nmp.NewMemfaultPullReq() | ||
|
||
rsp, err := txReq(s, r.Msg(), &c.CmdBase) | ||
if err != nil { | ||
return nil, err | ||
} | ||
irsp := rsp.(*nmp.MemfaultPullRsp) | ||
|
||
if c.ProgressCb != nil { | ||
c.ProgressCb(c, irsp) | ||
} | ||
|
||
res.Rsps = append(res.Rsps, irsp) | ||
if irsp.Status == MEMFAULT_PACKETIZER_STATUS_NO_MORE_DATA || | ||
irsp.Status == MEMFAULT_PACKETIZER_STATUS_END_OF_CHUNK || | ||
len(irsp.Data) == 0 { | ||
// Download complete. | ||
break | ||
} | ||
} | ||
|
||
return res, nil | ||
} | ||
|