Skip to content

Commit

Permalink
feat(am-dbg): add --client-list file.txt for a live detailed dump
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta committed Feb 15, 2025
1 parent 21c2034 commit fc1a2ec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions tools/cmd/am-dbg/cmd_dbg.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func cliRun(_ *cobra.Command, _ []string, p cli.Params) {
DbgLogLevel: p.LogLevel,
DbgLogger: logger,
ImportData: p.ImportData,
ClientList: p.ClientList,
ServerAddr: p.ServerAddr,
EnableMouse: p.EnableMouse,
SelectConnected: p.SelectConnected,
Expand Down
8 changes: 7 additions & 1 deletion tools/debugger/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
pCleanOnConnect = "clean-on-connect"
pVersion = "version"
pImport = "import-data"
pClientList = "client-list"
pImportShort = "i"
pSelectConn = "select-connected"
pSelectConnShort = "c"
Expand Down Expand Up @@ -60,6 +61,7 @@ type Params struct {
ServerAddr string
DebugAddr string
ImportData string
ClientList string
StartupMachine string
StartupView string
StartupTx int
Expand Down Expand Up @@ -107,6 +109,8 @@ func AddFlags(rootCmd *cobra.Command) {
f.StringP(pStartupMach,
pStartupMachShort, "",
"Select a machine by (partial) ID on startup (requires --"+pImport+")")
f.String(pClientList, "", "Keep this file up-to-date with connected"+
` clients (eg. "am-dbg.txt")`)

// TODO parse copy-paste commas, eg 1,001
f.IntP(pStartupTx, pStartupTxShort, 0,
Expand Down Expand Up @@ -151,6 +155,7 @@ func ParseParams(cmd *cobra.Command, _ []string) Params {
serverAddr := cmd.Flag(pServerAddr).Value.String()
debugAddr := cmd.Flag(pAmDbgAddr).Value.String()
importData := cmd.Flag(pImport).Value.String()
clientList := cmd.Flag(pClientList).Value.String()
fwdDataRaw := cmd.Flag(pFwdData).Value.String()
profSrv := cmd.Flag(pProfSrv).Value.String()
startupMachine := cmd.Flag(pStartupMach).Value.String()
Expand Down Expand Up @@ -201,6 +206,7 @@ func ParseParams(cmd *cobra.Command, _ []string) Params {
ServerAddr: serverAddr,
DebugAddr: debugAddr,
ImportData: importData,
ClientList: clientList,
StartupMachine: startupMachine,
StartupView: startupView,
StartupTx: startupTx,
Expand Down Expand Up @@ -278,7 +284,7 @@ func StartCpuProfileSrv(ctx context.Context, logger *log.Logger, p *Params) {
return
}
go func() {
logger.Println("Starting pprof server on "+p.ProfSrv)
logger.Println("Starting pprof server on " + p.ProfSrv)
// TODO support ctx cancel
if err := http.ListenAndServe(p.ProfSrv, nil); err != nil {
logger.Fatalf("could not start pprof server: %v", err)
Expand Down
21 changes: 21 additions & 0 deletions tools/debugger/client_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func (d *Debugger) doUpdateClientList(immediate bool) {
longestName = maxLen
}

txtFile := ""

// update
for i, item := range d.clientList.GetItems() {
ref := item.GetReference().(*sidebarRef)
Expand All @@ -174,6 +176,18 @@ func (d *Debugger) doUpdateClientList(immediate bool) {
strings.Repeat(" ", spaceCount) + spacePost
label := d.getClientListLabel(namePad, c, i)
item.SetMainText(label)

// txt file
if d.Opts.ClientList != "" {
if !hasParent {
txtFile += "\n"
}
txtFile += string(cview.StripTags([]byte(label), true, false)) + "\n"
for _, tag := range c.MsgStruct.Tags {
txtFile += strings.Repeat(" ", ref.lvl) + spacePre +
" #" + tag + "\n"
}
}
}

if len(d.Clients) > 0 {
Expand All @@ -192,6 +206,13 @@ func (d *Debugger) doUpdateClientList(immediate bool) {
if !immediate {
d.draw(d.clientList)
}

// save to a file
if d.Opts.ClientList != "" {
_, _ = d.clientListFile.Seek(0, 0)
_ = d.clientListFile.Truncate(0)
_, _ = d.clientListFile.Write([]byte(txtFile))
}
}

// avoid eval in handlers
Expand Down
12 changes: 12 additions & 0 deletions tools/debugger/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ type Opts struct {
Filters *OptsFilters
// File path to import (brotli)
ImportData string
// File to dump client list into.
ClientList string
// Screen overload for tests & ssh
Screen tcell.Screen
// Debugger's ID
Expand Down Expand Up @@ -398,6 +400,7 @@ type Debugger struct {
clip clipper.Clipboard
// toolbarItems is a list of row of toolbars items
toolbarItems [][]toolbarItem
clientListFile *os.File
msgsDelayed []*telemetry.DbgMsgTx
msgsDelayedConns []string
}
Expand Down Expand Up @@ -471,6 +474,15 @@ func New(ctx context.Context, opts Opts) (*Debugger, error) {
}
d.clip = clip

// client list file
if d.Opts.ClientList != "" {
clientListFile, err := os.Create(d.Opts.ClientList)
if err != nil {
mach.AddErr(fmt.Errorf("client list file open: %w", err), nil)
}
d.clientListFile = clientListFile
}

return d, nil
}

Expand Down

0 comments on commit fc1a2ec

Please sign in to comment.