There is a c program running in the docker container named main. And I need to do some additional operations with the process everyday. So I put a shell script named gdb_op.sh in "/opt" path of a docker container. The content of gdb_op.sh is:

#/bin/bash

file_path="$1"
/usr/bin/expect <<-EOF
set time 30
spawn gdb - `pidof main`
expect "(gdb)"
send "set \\\$len = ((int (*)(char*))getFileLen)(\"${file_path}\")\r"
send "set \\\$buf = malloc(\\\$len)\r"
send "set \\\$file = ((void*(*)(char*, char*))openFile)(\"${file_path}\", \"rb\")\r"
send "p/x ((int (*)(void*, int, int, void*))readFile)(\\\$buf, \\\$len, \\\$file)\r"
send "p/x ((int (*)(void*))closeFile)(\\\$file)\r"
send "detach\r"
send "q\r"
expect eof
EOF

And I run this script within "docker exec" like:

docker exec -i $container_id bash -c "cd /opt;./gdb_op.sh $file_path"

Then it seems like the line of 'expect "(gdb)"' was ignored.

GNU gdb (GDB) 10.1
...
Attaching to process 3146162
...
<*))getFileLen("...")
0x00007fce324eb8f3 in clock_nanosleep () from /usr/lib64/libc.so.6
(gdb) set $buf = malloc($len)
<))openFile("...", "rb")
<t, int, void*))readFile)($buf, $len, $file)
Invalid cast.
(gdb) p/x ((int (*)(void*))closeFile)($file)
$1 = 0x0
(gdb) detach
Detaching from program: /home/vfabric/bin/main, process 3146162
[Inferior 1 (process 3146162) detached]
(gdb) q

The line of getFileLen, openFile and readFile, part of the charactors of casting missed , and "set $len =" missed too. This makes the script unable to use. Is there any solution?

The target is to do the gdb operations (in script) to the process out of the container. These gdb operations have to be contained in a script so it's easy to maintain.

0

There are 0 best solutions below