Skip to content

Commit

Permalink
Add interactive GDB option for Simer tool.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <[email protected]>
  • Loading branch information
timrulebosch committed Sep 11, 2024
1 parent ff861f0 commit c9311a7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 23 deletions.
37 changes: 31 additions & 6 deletions doc/content/docs/user/simer/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ $ docker pull ghcr.io/boschglobal/dse-simer:2.0
```bash
# Define a shell function (or add to .profile file).
$ export SIMER_IMAGE=ghcr.io/boschglobal/dse-simer:latest
$ simer() { ( cd "$1" && shift && docker run -it --rm -v $(pwd):/sim -p 2159:2159 $SIMER_IMAGE "$@"; ) }
$ simer() { ( if test -d "$1"; then cd "$1" && shift; fi && docker run -it --rm -v $(pwd):/sim -p 2159:2159 $SIMER_IMAGE "$@"; ) }
# Run the simulation.
$ simer dse/modelc/build/_out/examples/minimal -endtime 0.04
Expand All @@ -336,18 +336,23 @@ Examples:
simer -stack minimal_stack -endtime 0.1 -logger 2
simer -stepsize 0.0005 -endtime 0.005 -transport redis -uri redis://redis:6379
simer -tmux -endtime 0.02
simer -gdb simbus -tmux -endtime 0.004 -logger 2 -timeout 5
Flags:
-endtime *flag.float64Value
simulation end time (0.002)
-env *simer.listFlag
environment modifier, in format '-env MODEL:NAME=VAL'
-gdb *flag.stringValue
run this model instance via GDB, use CSL in format '-gdb simbus,model'
-gdbserver *flag.stringValue
attach this model instance to GDB server
-logger *flag.intValue
log level (select between 0..4) (3)
-modelcI386 *flag.stringValue
path to ModelC i386 executable (/usr/local/bin/modelc32_i386)
-modelcX32 *flag.stringValue
path to ModelC x32 executable (modelc32)
path to ModelC x32 executable (/usr/local/bin/modelc32_x86)
-stack *flag.stringValue
run the named simulation stack(s)
-stepsize *flag.float64Value
Expand All @@ -361,7 +366,7 @@ Flags:
-uri *flag.stringValue
SimBus connection URI (redis://localhost:6379)
-valgrind *flag.stringValue
run this model instance via Valgrind
run this model instance via Valgrind, use CSL in format '-valgrind simbus,model'
```


Expand Down Expand Up @@ -423,15 +428,32 @@ Finally, use:
: Detach from the _tmux_ session (__and exit the simulation!__).


### GDB Debugging
### GDB Debug

#### GDB

Models (and the SimBus) running in a simulation can be run under GDB to facilitate
interactive debugging. This method of debugging requires the use of the Tmux
interface.

```bash
# Start the simulation with the SimBus running under GDB.
$ simer dse/modelc/build/_out/examples/gdb -tmux -gdb simbus -endtime 0.04 -timeout 5
# Start the simulation with a model and the SimBus running under GDB.
$ simer dse/modelc/build/_out/examples/gdb -tmux -gdb simbus,gdb_inst -endtime 0.04 -timeout 5
```


#### GDB Server (remote)

Models running in a simulation can be debugged with GDB by indicating which model
is being debugged via the option `-gdb`, and then connecting a GDB instance to
is being debugged via the option `-gdbserver`, and then connecting a GDB instance to
that model running "remotely" in the Simer simulation.

```bash
# Start the simulation and indicate the Model being debugged.
$ simer dse/modelc/build/_out/examples/gdb -endtime 0.04 -tmux -gdb gdb_inst
$ simer dse/modelc/build/_out/examples/gdb -endtime 0.04 -tmux -gdbserver gdb_inst
# Start GDB (in a second terminal), and connect to the simulation.
$ gdb dse/modelc/build/_out/bin/modelc
Expand All @@ -449,6 +471,9 @@ $ gdb dse/modelc/build/_out/bin/modelc
Channel
: Represents a grouping of signals which are exchanged between models.

GDB
: A tool for debugging software and investigating bugs (typically segmentation faults).

Manifest
: A simulation project definition format which is used to define and build a simulation package.

Expand Down
3 changes: 1 addition & 2 deletions dse/modelc/adapter/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ int32_t wait_message(Adapter* adapter, const char** channel_name,
NOTE: the message will already be encoded to the builder object on the
adapter->vtable, therefore, this function should only be called once per
message (i.e. send_all should be unwound to a for loop with a
flacc_builder_reset() call on each loop ... or a buffer memcpy).
message.
*/
int32_t send_message(Adapter* adapter, void* endpoint_channel,
uint32_t model_uid, ns(MessageType_union_ref_t) message, bool ack)
Expand Down
1 change: 1 addition & 0 deletions extra/tools/simer/build/package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ RUN set -eux; \
bash \
ca-certificates \
curl \
gdb \
gdbserver \
jq \
less \
Expand Down
6 changes: 4 additions & 2 deletions extra/tools/simer/cmd/simer/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Examples:
simer -stack minimal_stack -endtime 0.1 -logger 2
simer -stepsize 0.0005 -endtime 0.005 -transport redis -uri redis://redis:6379
simer -tmux -endtime 0.02
simer -gdb simbus -tmux -endtime 0.004 -logger 2 -timeout 5
Flags:
`
Expand Down Expand Up @@ -61,8 +62,9 @@ func parseFlags() {

flag.Var(&flags.EnvModifier, "env", "environment modifier, in format '-env MODEL:NAME=VAL'")

flag.StringVar(&flags.Gdb, "gdb", "", "attach this model instance to GDB server")
flag.StringVar(&flags.Valgrind, "valgrind", "", "run this model instance via Valgrind")
flag.StringVar(&flags.Gdb, "gdb", "", "run this model instance via GDB, use CSL in format '-gdb simbus,model'")
flag.StringVar(&flags.GdbServer, "gdbserver", "", "attach this model instance to GDB server")
flag.StringVar(&flags.Valgrind, "valgrind", "", "run this model instance via Valgrind, use CSL in format '-valgrind simbus,model'")

flag.Parse()
if flagSpecified("transport") == false {
Expand Down
8 changes: 4 additions & 4 deletions extra/tools/simer/cmd/simer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ var (
logger = flag.Int("logger", 3, "log level (select between 0..4)")
// Binary flags (adjusted only during development).
redisPath = flag.String("redis", "redis-server", "path to redis-server executable")
simbusPath = flag.String("simbus", "simbus", "path to SimBus executable (set to \"\" to disable)")
modelcPath = flag.String("modelc", "modelc", "path to ModelC executable")
modelcX32Path = flag.String("modelcX32", "modelc32_x86", "path to ModelC x32 executable")
modelcI386Path = flag.String("modelcI386", "modelc32_i386", "path to ModelC i386 executable")
simbusPath = flag.String("simbus", "/usr/local/bin/simbus", "path to SimBus executable (set to \"\" to disable)")
modelcPath = flag.String("modelc", "/usr/local/bin/modelc", "path to ModelC executable")
modelcX32Path = flag.String("modelcX32", "/usr/local/bin/modelc32_x86", "path to ModelC x32 executable")
modelcI386Path = flag.String("modelcI386", "/usr/local/bin/modelc32_i386", "path to ModelC i386 executable")
)

func main() {
Expand Down
5 changes: 3 additions & 2 deletions extra/tools/simer/internal/app/simer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Flags struct {

EnvModifier listFlag

Gdb string
Valgrind string
Gdb string
GdbServer string
Valgrind string
}
30 changes: 23 additions & 7 deletions extra/tools/simer/internal/app/simer/simer.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ func SimbusCommand(docMap map[string][]kind.KindDoc, simbusPath string, flags Fl
if model.Runtime != nil && model.Runtime.Files != nil {
yamlFiles = append(yamlFiles, *model.Runtime.Files...)
}
// Base command.
yamlFiles = removeDuplicate(yamlFiles)
path, args := processCmdModifiers("simbus", simbusPath, flags)
// Arguments.
args := []string{}
//args = append(args, "--help")
//args = append(args, "--logger", "2")
if flags.Transport != "" {
Expand All @@ -125,7 +127,7 @@ func SimbusCommand(docMap map[string][]kind.KindDoc, simbusPath string, flags Fl
}
cmd := session.Command{
Name: "SimBus",
Prog: simbusPath,
Prog: path,
Args: append(args, yamlFiles...),
Env: calculateEnv(stackSpec, &model, flags),
}
Expand Down Expand Up @@ -266,14 +268,21 @@ func removeDuplicate[T comparable](s []T) []T {
})
}

func buildModelCmd(name string, path string, yamlFiles []string, env map[string]string, flags Flags) *session.Command {

yamlFiles = removeDuplicate(yamlFiles)
func processCmdModifiers(name string, path string, flags Flags) (string, []string) {
args := []string{}

// Command modifiers.
if len(flags.Gdb) != 0 {
if slices.Contains(strings.Split(name, ","), flags.Gdb) == true {
// Prefix 'path' with the GDB command and options.
gdbArgs := []string{
"--eval-command=run",
"--args",
}
args = append(args, gdbArgs...)
args = append(args, path)
path = "/usr/bin/gdb"
}
} else if len(flags.GdbServer) != 0 {
if slices.Contains(strings.Split(name, ","), flags.GdbServer) == true {
// Run model 'name' via gdbserver.
args = append(args, "localhost:2159", path)
path = "/usr/bin/gdbserver"
Expand All @@ -291,6 +300,13 @@ func buildModelCmd(name string, path string, yamlFiles []string, env map[string]
path = "/usr/bin/valgrind"
}
}
return path, args
}

func buildModelCmd(name string, path string, yamlFiles []string, env map[string]string, flags Flags) *session.Command {

yamlFiles = removeDuplicate(yamlFiles)
path, args := processCmdModifiers(name, path, flags)

// Continue building the command.
args = append(args, "--name", name)
Expand Down

0 comments on commit c9311a7

Please sign in to comment.