How to set a gdb convenience variable to the result of a command

Viewed 225

I have user-defined gdb commands in Python that look along the lines of:

#!/usr/bin/env python
import gdb

class ConcatenateStrings(gdb.Command):
    def __init__(self):
        super(ConcatenateStrings, self).__init__("strcat", gdb.COMMAND_DATA)

    def invoke(self, arg, from_tty):
        args = arg.split(' ')

        var = args[0]
        result = args[1] + args[2]

        gdb.execute('set ${0} = "{1}"'.format(var, result))

ConcatenateStrings()

(Safety checks and the like are removed for simplicity's sake)

They expect an argument that specifies what variable the result should be assigned to as such:

(gdb) source strcat.py
(gdb) strcat result a b
(gdb) print $result
ab

This works but I'm curious if it's possible to directly assign the result of a gdb command to a convenience variable.

Basically, is there a way to do this?

(gdb) source strcat.py
(gdb) set $result = $(strcat a b)
(gdb) print $result
ab
0 Answers
Related