Home > OS >  Setting a return value in debugger in C in order to find input arguments
Setting a return value in debugger in C in order to find input arguments

Time:12-30

Is there a way in C (using a debugger) to set the return value and find out what arguments would return that value? For example, if I have a function that takes in three integers, does something inside the function, and returns an integer, is there a way for me to set the return value as 0, and find out which integers when inputted give that return value?

I tried using an online GDB debugger, as well as the gdbgui debugger (from Linux). I understand how the debugger works and have been setting breakpoints but was wondering if there is an easy way to do this.

Wording of question: "Using a source-level debugger, determine for what values of arguments the function returns a zero."

CodePudding user response:

The assignment doesn't mean you should figure out which magic command inverts a function and paste it to the debugger window. There is no such command1.

The assignment as I understand it means you should investigate what the function does, understand its logic, and derive the inverse function, using the debugger as a tool that helps you observe behaviour of the code.


1 That can be used in practice, at any rate. There is a finite number of machine integers. You could write a script that tries every possible input, but you may or may not live long enough to see it finished running.

CodePudding user response:

Since this operation is about learning the gdb debugger ...

  1. Set a breakpoint on the return statement of the function. In the example below, this is line 13, so we want b 13.
  2. Only execute the breakpoint if the return value is 0. We want: cond 1 ret == 0
  3. Print the argument values at the breakpoint. We use commands 1 to set gdb commands to be executed at the breakpoint. We want p a1, p a2, and p a3 to print the arguments. And, then, we want c to continue execution after the breakpoint.

Here is a simple sample program:

#include <stdio.h>

#ifndef LIM
#define LIM     5
#endif

int
func(int a1,int a2,int a3)
{

    int ret = a1   a2   a3;

    return ret;
}

int
main(void)
{
    int sum = 0;

    for (int a1 = -LIM;  a1 <= LIM;    a1) {
        for (int a2 = -LIM;  a2 <= LIM;    a2) {
            for (int a3 = -LIM;  a3 <= LIM;    a3)
                sum  = func(a1,a2,a3);
        }
    }

    printf("%d\n",sum);
}

We build the above program with (e.g.):

cc -g -o program program.c

In the above program, the return for func is line 13.

Here are the gdb commands we'd use. In this example, we put them in a file (e.g. cmds), but they could just as easily be entered at the command prompt:

b 13
cond 1 ret == 0
commands 1
echo "a1 is "
p a1
echo "a2 is "
p a2
echo "a3 is "
p a3
c
end
run
quit

We invoke gdb with the command:

gdb -q -x cmds ./program > out

Here is the output (e.g. out):

Reading symbols from ./program...done.
Breakpoint 1 at 0x401143: file program.c, line 13.

Breakpoint 1, func (a1=-5, a2=0, a3=5) at program.c:13
13      return ret;
"a1 is "$1 = -5
"a2 is "$2 = 0
"a3 is "$3 = 5

Breakpoint 1, func (a1=-5, a2=1, a3=4) at program.c:13
13      return ret;
"a1 is "$4 = -5
"a2 is "$5 = 1
"a3 is "$6 = 4

Breakpoint 1, func (a1=-5, a2=2, a3=3) at program.c:13
13      return ret;
"a1 is "$7 = -5
"a2 is "$8 = 2
"a3 is "$9 = 3

Breakpoint 1, func (a1=-5, a2=3, a3=2) at program.c:13
13      return ret;
"a1 is "$10 = -5
"a2 is "$11 = 3
"a3 is "$12 = 2

Breakpoint 1, func (a1=-5, a2=4, a3=1) at program.c:13
13      return ret;
"a1 is "$13 = -5
"a2 is "$14 = 4
"a3 is "$15 = 1

Breakpoint 1, func (a1=-5, a2=5, a3=0) at program.c:13
13      return ret;
"a1 is "$16 = -5
"a2 is "$17 = 5
"a3 is "$18 = 0

Breakpoint 1, func (a1=-4, a2=-1, a3=5) at program.c:13
13      return ret;
"a1 is "$19 = -4
"a2 is "$20 = -1
"a3 is "$21 = 5

Breakpoint 1, func (a1=-4, a2=0, a3=4) at program.c:13
13      return ret;
"a1 is "$22 = -4
"a2 is "$23 = 0
"a3 is "$24 = 4

Breakpoint 1, func (a1=-4, a2=1, a3=3) at program.c:13
13      return ret;
"a1 is "$25 = -4
"a2 is "$26 = 1
"a3 is "$27 = 3

Breakpoint 1, func (a1=-4, a2=2, a3=2) at program.c:13
13      return ret;
"a1 is "$28 = -4
"a2 is "$29 = 2
"a3 is "$30 = 2

Breakpoint 1, func (a1=-4, a2=3, a3=1) at program.c:13
13      return ret;
"a1 is "$31 = -4
"a2 is "$32 = 3
"a3 is "$33 = 1

Breakpoint 1, func (a1=-4, a2=4, a3=0) at program.c:13
13      return ret;
"a1 is "$34 = -4
"a2 is "$35 = 4
"a3 is "$36 = 0

Breakpoint 1, func (a1=-4, a2=5, a3=-1) at program.c:13
13      return ret;
"a1 is "$37 = -4
"a2 is "$38 = 5
"a3 is "$39 = -1

Breakpoint 1, func (a1=-3, a2=-2, a3=5) at program.c:13
13      return ret;
"a1 is "$40 = -3
"a2 is "$41 = -2
"a3 is "$42 = 5

Breakpoint 1, func (a1=-3, a2=-1, a3=4) at program.c:13
13      return ret;
"a1 is "$43 = -3
"a2 is "$44 = -1
"a3 is "$45 = 4

Breakpoint 1, func (a1=-3, a2=0, a3=3) at program.c:13
13      return ret;
"a1 is "$46 = -3
"a2 is "$47 = 0
"a3 is "$48 = 3

Breakpoint 1, func (a1=-3, a2=1, a3=2) at program.c:13
13      return ret;
"a1 is "$49 = -3
"a2 is "$50 = 1
"a3 is "$51 = 2

Breakpoint 1, func (a1=-3, a2=2, a3=1) at program.c:13
13      return ret;
"a1 is "$52 = -3
"a2 is "$53 = 2
"a3 is "$54 = 1

Breakpoint 1, func (a1=-3, a2=3, a3=0) at program.c:13
13      return ret;
"a1 is "$55 = -3
"a2 is "$56 = 3
"a3 is "$57 = 0

Breakpoint 1, func (a1=-3, a2=4, a3=-1) at program.c:13
13      return ret;
"a1 is "$58 = -3
"a2 is "$59 = 4
"a3 is "$60 = -1

Breakpoint 1, func (a1=-3, a2=5, a3=-2) at program.c:13
13      return ret;
"a1 is "$61 = -3
"a2 is "$62 = 5
"a3 is "$63 = -2

Breakpoint 1, func (a1=-2, a2=-3, a3=5) at program.c:13
13      return ret;
"a1 is "$64 = -2
"a2 is "$65 = -3
"a3 is "$66 = 5

Breakpoint 1, func (a1=-2, a2=-2, a3=4) at program.c:13
13      return ret;
"a1 is "$67 = -2
"a2 is "$68 = -2
"a3 is "$69 = 4

Breakpoint 1, func (a1=-2, a2=-1, a3=3) at program.c:13
13      return ret;
"a1 is "$70 = -2
"a2 is "$71 = -1
"a3 is "$72 = 3

Breakpoint 1, func (a1=-2, a2=0, a3=2) at program.c:13
13      return ret;
"a1 is "$73 = -2
"a2 is "$74 = 0
"a3 is "$75 = 2

Breakpoint 1, func (a1=-2, a2=1, a3=1) at program.c:13
13      return ret;
"a1 is "$76 = -2
"a2 is "$77 = 1
"a3 is "$78 = 1

Breakpoint 1, func (a1=-2, a2=2, a3=0) at program.c:13
13      return ret;
"a1 is "$79 = -2
"a2 is "$80 = 2
"a3 is "$81 = 0

Breakpoint 1, func (a1=-2, a2=3, a3=-1) at program.c:13
13      return ret;
"a1 is "$82 = -2
"a2 is "$83 = 3
"a3 is "$84 = -1

Breakpoint 1, func (a1=-2, a2=4, a3=-2) at program.c:13
13      return ret;
"a1 is "$85 = -2
"a2 is "$86 = 4
"a3 is "$87 = -2

Breakpoint 1, func (a1=-2, a2=5, a3=-3) at program.c:13
13      return ret;
"a1 is "$88 = -2
"a2 is "$89 = 5
"a3 is "$90 = -3

Breakpoint 1, func (a1=-1, a2=-4, a3=5) at program.c:13
13      return ret;
"a1 is "$91 = -1
"a2 is "$92 = -4
"a3 is "$93 = 5

Breakpoint 1, func (a1=-1, a2=-3, a3=4) at program.c:13
13      return ret;
"a1 is "$94 = -1
"a2 is "$95 = -3
"a3 is "$96 = 4

Breakpoint 1, func (a1=-1, a2=-2, a3=3) at program.c:13
13      return ret;
"a1 is "$97 = -1
"a2 is "$98 = -2
"a3 is "$99 = 3

Breakpoint 1, func (a1=-1, a2=-1, a3=2) at program.c:13
13      return ret;
"a1 is "$100 = -1
"a2 is "$101 = -1
"a3 is "$102 = 2

Breakpoint 1, func (a1=-1, a2=0, a3=1) at program.c:13
13      return ret;
"a1 is "$103 = -1
"a2 is "$104 = 0
"a3 is "$105 = 1

Breakpoint 1, func (a1=-1, a2=1, a3=0) at program.c:13
13      return ret;
"a1 is "$106 = -1
"a2 is "$107 = 1
"a3 is "$108 = 0

Breakpoint 1, func (a1=-1, a2=2, a3=-1) at program.c:13
13      return ret;
"a1 is "$109 = -1
"a2 is "$110 = 2
"a3 is "$111 = -1

Breakpoint 1, func (a1=-1, a2=3, a3=-2) at program.c:13
13      return ret;
"a1 is "$112 = -1
"a2 is "$113 = 3
"a3 is "$114 = -2

Breakpoint 1, func (a1=-1, a2=4, a3=-3) at program.c:13
13      return ret;
"a1 is "$115 = -1
"a2 is "$116 = 4
"a3 is "$117 = -3

Breakpoint 1, func (a1=-1, a2=5, a3=-4) at program.c:13
13      return ret;
"a1 is "$118 = -1
"a2 is "$119 = 5
"a3 is "$120 = -4

Breakpoint 1, func (a1=0, a2=-5, a3=5) at program.c:13
13      return ret;
"a1 is "$121 = 0
"a2 is "$122 = -5
"a3 is "$123 = 5

Breakpoint 1, func (a1=0, a2=-4, a3=4) at program.c:13
13      return ret;
"a1 is "$124 = 0
"a2 is "$125 = -4
"a3 is "$126 = 4

Breakpoint 1, func (a1=0, a2=-3, a3=3) at program.c:13
13      return ret;
"a1 is "$127 = 0
"a2 is "$128 = -3
"a3 is "$129 = 3

Breakpoint 1, func (a1=0, a2=-2, a3=2) at program.c:13
13      return ret;
"a1 is "$130 = 0
"a2 is "$131 = -2
"a3 is "$132 = 2

Breakpoint 1, func (a1=0, a2=-1, a3=1) at program.c:13
13      return ret;
"a1 is "$133 = 0
"a2 is "$134 = -1
"a3 is "$135 = 1

Breakpoint 1, func (a1=0, a2=0, a3=0) at program.c:13
13      return ret;
"a1 is "$136 = 0
"a2 is "$137 = 0
"a3 is "$138 = 0

Breakpoint 1, func (a1=0, a2=1, a3=-1) at program.c:13
13      return ret;
"a1 is "$139 = 0
"a2 is "$140 = 1
"a3 is "$141 = -1

Breakpoint 1, func (a1=0, a2=2, a3=-2) at program.c:13
13      return ret;
"a1 is "$142 = 0
"a2 is "$143 = 2
"a3 is "$144 = -2

Breakpoint 1, func (a1=0, a2=3, a3=-3) at program.c:13
13      return ret;
"a1 is "$145 = 0
"a2 is "$146 = 3
"a3 is "$147 = -3

Breakpoint 1, func (a1=0, a2=4, a3=-4) at program.c:13
13      return ret;
"a1 is "$148 = 0
"a2 is "$149 = 4
"a3 is "$150 = -4

Breakpoint 1, func (a1=0, a2=5, a3=-5) at program.c:13
13      return ret;
"a1 is "$151 = 0
"a2 is "$152 = 5
"a3 is "$153 = -5

Breakpoint 1, func (a1=1, a2=-5, a3=4) at program.c:13
13      return ret;
"a1 is "$154 = 1
"a2 is "$155 = -5
"a3 is "$156 = 4

Breakpoint 1, func (a1=1, a2=-4, a3=3) at program.c:13
13      return ret;
"a1 is "$157 = 1
"a2 is "$158 = -4
"a3 is "$159 = 3

Breakpoint 1, func (a1=1, a2=-3, a3=2) at program.c:13
13      return ret;
"a1 is "$160 = 1
"a2 is "$161 = -3
"a3 is "$162 = 2

Breakpoint 1, func (a1=1, a2=-2, a3=1) at program.c:13
13      return ret;
"a1 is "$163 = 1
"a2 is "$164 = -2
"a3 is "$165 = 1

Breakpoint 1, func (a1=1, a2=-1, a3=0) at program.c:13
13      return ret;
"a1 is "$166 = 1
"a2 is "$167 = -1
"a3 is "$168 = 0

Breakpoint 1, func (a1=1, a2=0, a3=-1) at program.c:13
13      return ret;
"a1 is "$169 = 1
"a2 is "$170 = 0
"a3 is "$171 = -1

Breakpoint 1, func (a1=1, a2=1, a3=-2) at program.c:13
13      return ret;
"a1 is "$172 = 1
"a2 is "$173 = 1
"a3 is "$174 = -2

Breakpoint 1, func (a1=1, a2=2, a3=-3) at program.c:13
13      return ret;
"a1 is "$175 = 1
"a2 is "$176 = 2
"a3 is "$177 = -3

Breakpoint 1, func (a1=1, a2=3, a3=-4) at program.c:13
13      return ret;
"a1 is "$178 = 1
"a2 is "$179 = 3
"a3 is "$180 = -4

Breakpoint 1, func (a1=1, a2=4, a3=-5) at program.c:13
13      return ret;
"a1 is "$181 = 1
"a2 is "$182 = 4
"a3 is "$183 = -5

Breakpoint 1, func (a1=2, a2=-5, a3=3) at program.c:13
13      return ret;
"a1 is "$184 = 2
"a2 is "$185 = -5
"a3 is "$186 = 3

Breakpoint 1, func (a1=2, a2=-4, a3=2) at program.c:13
13      return ret;
"a1 is "$187 = 2
"a2 is "$188 = -4
"a3 is "$189 = 2

Breakpoint 1, func (a1=2, a2=-3, a3=1) at program.c:13
13      return ret;
"a1 is "$190 = 2
"a2 is "$191 = -3
"a3 is "$192 = 1

Breakpoint 1, func (a1=2, a2=-2, a3=0) at program.c:13
13      return ret;
"a1 is "$193 = 2
"a2 is "$194 = -2
"a3 is "$195 = 0

Breakpoint 1, func (a1=2, a2=-1, a3=-1) at program.c:13
13      return ret;
"a1 is "$196 = 2
"a2 is "$197 = -1
"a3 is "$198 = -1

Breakpoint 1, func (a1=2, a2=0, a3=-2) at program.c:13
13      return ret;
"a1 is "$199 = 2
"a2 is "$200 = 0
"a3 is "$201 = -2

Breakpoint 1, func (a1=2, a2=1, a3=-3) at program.c:13
13      return ret;
"a1 is "$202 = 2
"a2 is "$203 = 1
"a3 is "$204 = -3

Breakpoint 1, func (a1=2, a2=2, a3=-4) at program.c:13
13      return ret;
"a1 is "$205 = 2
"a2 is "$206 = 2
"a3 is "$207 = -4

Breakpoint 1, func (a1=2, a2=3, a3=-5) at program.c:13
13      return ret;
"a1 is "$208 = 2
"a2 is "$209 = 3
"a3 is "$210 = -5

Breakpoint 1, func (a1=3, a2=-5, a3=2) at program.c:13
13      return ret;
"a1 is "$211 = 3
"a2 is "$212 = -5
"a3 is "$213 = 2

Breakpoint 1, func (a1=3, a2=-4, a3=1) at program.c:13
13      return ret;
"a1 is "$214 = 3
"a2 is "$215 = -4
"a3 is "$216 = 1

Breakpoint 1, func (a1=3, a2=-3, a3=0) at program.c:13
13      return ret;
"a1 is "$217 = 3
"a2 is "$218 = -3
"a3 is "$219 = 0

Breakpoint 1, func (a1=3, a2=-2, a3=-1) at program.c:13
13      return ret;
"a1 is "$220 = 3
"a2 is "$221 = -2
"a3 is "$222 = -1

Breakpoint 1, func (a1=3, a2=-1, a3=-2) at program.c:13
13      return ret;
"a1 is "$223 = 3
"a2 is "$224 = -1
"a3 is "$225 = -2

Breakpoint 1, func (a1=3, a2=0, a3=-3) at program.c:13
13      return ret;
"a1 is "$226 = 3
"a2 is "$227 = 0
"a3 is "$228 = -3

Breakpoint 1, func (a1=3, a2=1, a3=-4) at program.c:13
13      return ret;
"a1 is "$229 = 3
"a2 is "$230 = 1
"a3 is "$231 = -4

Breakpoint 1, func (a1=3, a2=2, a3=-5) at program.c:13
13      return ret;
"a1 is "$232 = 3
"a2 is "$233 = 2
"a3 is "$234 = -5

Breakpoint 1, func (a1=4, a2=-5, a3=1) at program.c:13
13      return ret;
"a1 is "$235 = 4
"a2 is "$236 = -5
"a3 is "$237 = 1

Breakpoint 1, func (a1=4, a2=-4, a3=0) at program.c:13
13      return ret;
"a1 is "$238 = 4
"a2 is "$239 = -4
"a3 is "$240 = 0

Breakpoint 1, func (a1=4, a2=-3, a3=-1) at program.c:13
13      return ret;
"a1 is "$241 = 4
"a2 is "$242 = -3
"a3 is "$243 = -1

Breakpoint 1, func (a1=4, a2=-2, a3=-2) at program.c:13
13      return ret;
"a1 is "$244 = 4
"a2 is "$245 = -2
"a3 is "$246 = -2

Breakpoint 1, func (a1=4, a2=-1, a3=-3) at program.c:13
13      return ret;
"a1 is "$247 = 4
"a2 is "$248 = -1
"a3 is "$249 = -3

Breakpoint 1, func (a1=4, a2=0, a3=-4) at program.c:13
13      return ret;
"a1 is "$250 = 4
"a2 is "$251 = 0
"a3 is "$252 = -4

Breakpoint 1, func (a1=4, a2=1, a3=-5) at program.c:13
13      return ret;
"a1 is "$253 = 4
"a2 is "$254 = 1
"a3 is "$255 = -5

Breakpoint 1, func (a1=5, a2=-5, a3=0) at program.c:13
13      return ret;
"a1 is "$256 = 5
"a2 is "$257 = -5
"a3 is "$258 = 0

Breakpoint 1, func (a1=5, a2=-4, a3=-1) at program.c:13
13      return ret;
"a1 is "$259 = 5
"a2 is "$260 = -4
"a3 is "$261 = -1

Breakpoint 1, func (a1=5, a2=-3, a3=-2) at program.c:13
13      return ret;
"a1 is "$262 = 5
"a2 is "$263 = -3
"a3 is "$264 = -2

Breakpoint 1, func (a1=5, a2=-2, a3=-3) at program.c:13
13      return ret;
"a1 is "$265 = 5
"a2 is "$266 = -2
"a3 is "$267 = -3

Breakpoint 1, func (a1=5, a2=-1, a3=-4) at program.c:13
13      return ret;
"a1 is "$268 = 5
"a2 is "$269 = -1
"a3 is "$270 = -4

Breakpoint 1, func (a1=5, a2=0, a3=-5) at program.c:13
13      return ret;
"a1 is "$271 = 5
"a2 is "$272 = 0
"a3 is "$273 = -5
0
[Inferior 1 (process 1816035) exited normally]
  • Related