Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added the possibility to provide cortex-debug custom configs
  • Loading branch information
cmaglie committed Oct 4, 2023
commit 78197c062abb3221b418d0ee37de7f73a09821e9
46 changes: 46 additions & 0 deletions commands/debug/debug_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package debug

import (
"context"
"encoding/json"
"regexp"
"strings"

"github.com/arduino/arduino-cli/arduino"
Expand Down Expand Up @@ -180,6 +182,10 @@ func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Expl
}
}

cortexDebugCustomJson := ""
if cortexDebugProps := debugProperties.SubTree("cortex-debug.custom"); cortexDebugProps.Size() > 0 {
cortexDebugCustomJson = convertToJsonMap(cortexDebugProps)
}
return &rpc.GetDebugConfigResponse{
Executable: debugProperties.Get("executable"),
Server: server,
Expand All @@ -189,5 +195,45 @@ func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Expl
ToolchainPath: debugProperties.Get("toolchain.path"),
ToolchainPrefix: debugProperties.Get("toolchain.prefix"),
ToolchainConfiguration: &toolchainConfiguration,
CortexDebugCustomJson: cortexDebugCustomJson,
}, nil
}

// Extract a JSON from a given properies.Map and converts key-indexed arrays
// like:
//
// my.indexed.array.0=first
// my.indexed.array.1=second
// my.indexed.array.2=third
//
// into the corresponding JSON arrays.
func convertToJsonMap(in *properties.Map) string {
// XXX: Maybe this method could be a good candidate for propertis.Map?

// Find the values that should be kept as is, and the indexed arrays
// that should be later converted into arrays.
arraysKeys := map[string]bool{}
stringKeys := []string{}
trailingNumberMatcher := regexp.MustCompile(`^(.*)\.[0-9]+$`)
for _, k := range in.Keys() {
match := trailingNumberMatcher.FindAllStringSubmatch(k, -1)
if len(match) > 0 && len(match[0]) > 1 {
arraysKeys[match[0][1]] = true
} else {
stringKeys = append(stringKeys, k)
}
}

// Compose a map that can be later marshaled into JSON keeping
// the arrays where they are expected to be.
res := map[string]any{}
for _, k := range stringKeys {
res[k] = in.Get(k)
}
for k := range arraysKeys {
res[k] = in.ExtractSubIndexLists(k)
}

data, _ := json.MarshalIndent(res, "", " ")
return string(data)
}
41 changes: 25 additions & 16 deletions internal/cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package debug

import (
"context"
"encoding/json"
"os"
"os/signal"

Expand Down Expand Up @@ -115,14 +116,15 @@ func runDebugCommand(command *cobra.Command, args []string) {
}

type debugInfoResult struct {
Executable string `json:"executable,omitempty"`
Toolchain string `json:"toolchain,omitempty"`
ToolchainPath string `json:"toolchain_path,omitempty"`
ToolchainPrefix string `json:"toolchain_prefix,omitempty"`
ToolchainConfig any `json:"toolchain_configuration,omitempty"`
Server string `json:"server,omitempty"`
ServerPath string `json:"server_path,omitempty"`
ServerConfig any `json:"server_configuration,omitempty"`
Executable string `json:"executable,omitempty"`
Toolchain string `json:"toolchain,omitempty"`
ToolchainPath string `json:"toolchain_path,omitempty"`
ToolchainPrefix string `json:"toolchain_prefix,omitempty"`
ToolchainConfig any `json:"toolchain_configuration,omitempty"`
Server string `json:"server,omitempty"`
ServerPath string `json:"server_path,omitempty"`
ServerConfig any `json:"server_configuration,omitempty"`
CortexDebugCustomConfig any `json:"cortex-debug_custom_configuration,omitempty"`
}

type openOcdServerConfigResult struct {
Expand All @@ -146,15 +148,22 @@ func newDebugInfoResult(info *rpc.GetDebugConfigResponse) *debugInfoResult {
Scripts: openocdConf.Scripts,
}
}
var cortexDebugCustomConfig any
if info.CortexDebugCustomJson != "" {
if err := json.Unmarshal([]byte(info.CortexDebugCustomJson), &cortexDebugCustomConfig); err != nil {
feedback.Fatal(tr("Error during Debug: %v", err), feedback.ErrGeneric)
}
}
return &debugInfoResult{
Executable: info.Executable,
Toolchain: info.Toolchain,
ToolchainPath: info.ToolchainPath,
ToolchainPrefix: info.ToolchainPrefix,
ToolchainConfig: toolchaingConfig,
Server: info.Server,
ServerPath: info.ServerPath,
ServerConfig: serverConfig,
Executable: info.Executable,
Toolchain: info.Toolchain,
ToolchainPath: info.ToolchainPath,
ToolchainPrefix: info.ToolchainPrefix,
ToolchainConfig: toolchaingConfig,
Server: info.Server,
ServerPath: info.ServerPath,
ServerConfig: serverConfig,
CortexDebugCustomConfig: cortexDebugCustomConfig,
}
}

Expand Down
16 changes: 16 additions & 0 deletions internal/integrationtest/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ func testAllDebugInformation(t *testing.T, env *integrationtest.Environment, cli
"second",
"third"
]
},
"cortex-debug_custom_configuration": {
"anotherStringParamer": "hellooo",
"overrideRestartCommands": [
"monitor reset halt",
"monitor gdb_sync",
"thb setup",
"c"
],
"postAttachCommands": [
"set remote hardware-watchpoint-limit 2",
"monitor reset halt",
"monitor gdb_sync",
"thb setup",
"c"
]
}
}`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ my.debug.server.openocd.script=
my.debug.server.openocd.scripts.0=first
my.debug.server.openocd.scripts.1=second
my.debug.server.openocd.scripts.2=third
my.debug.cortex-debug.custom.postAttachCommands.0=set remote hardware-watchpoint-limit 2
my.debug.cortex-debug.custom.postAttachCommands.1=monitor reset halt
my.debug.cortex-debug.custom.postAttachCommands.2=monitor gdb_sync
my.debug.cortex-debug.custom.postAttachCommands.3=thb setup
my.debug.cortex-debug.custom.postAttachCommands.4=c
my.debug.cortex-debug.custom.overrideRestartCommands.0=monitor reset halt
my.debug.cortex-debug.custom.overrideRestartCommands.1=monitor gdb_sync
my.debug.cortex-debug.custom.overrideRestartCommands.2=thb setup
my.debug.cortex-debug.custom.overrideRestartCommands.3=c
my.debug.cortex-debug.custom.anotherStringParamer=hellooo
46 changes: 30 additions & 16 deletions rpc/cc/arduino/cli/commands/v1/debug.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/cc/arduino/cli/commands/v1/debug.proto
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ message GetDebugConfigResponse {
google.protobuf.Any toolchain_configuration = 7;
// Extra configuration parameters wrt GDB server
google.protobuf.Any server_configuration = 8;
// cortex-debug custom JSON configuration, it is provided as is from
// the platform developers.
string cortex_debug_custom_json = 9;
}

// Configurations specific for the 'gcc' toolchain
Expand Down