Reputation: 3
In a linux terminal, I typed the command why (this command does not exist), and got this response
No command 'why'found, did you mean:
....
why: command not found
to get the exit code, this is what I typed in:
$?
and got the response:
127: command not found
I am assuming that when most of the commands run, they return codes. Now with gdb, after it executes the next line of the C program being debugged, does it return any codes? And if so, how do I get those codes (a command like $? in gdb? - I haven't found a documentation in man gdb pages of such a command.
*In any case, gdb should know (and probably log) whether the line executed successfully and if not, what went wrong.
Upvotes: 0
Views: 991
Reputation: 36401
Unix processes always return a value: their exit code; named such because most of programs terminate with a call to exit
(there is other possibilities but with no importance here). Beware that all processes return such a code, not most of them. Shells interpret user typed lines and transform them into processes. When a process terminates, the shel let you know the value of the return code of the last process that it controlled. In bash this variable is $?
. If you want to print it, you can use echo $?
. If you type $?
it will interpret it as a command but 127
(the exit value of the last command) is not a command. The standard is to have a return value of 0 for correctly terminated processes, and any non nul value to signal that an error occurred somewhere in the logic of the program.
gdb also launch processes (to control them) but, executing a single line of code, is not running a process, so there is no exit code after executing one step of a program code. When you execute, in gdb, something that terminates the process its exit code is available in a internal gdb variable $_exitcode
.
Upvotes: 1
Reputation: 223972
There's no such thing as a line of code executing successfully, at least not in same way that commands given to the shell do.
When you're at the shell, you're inputting either the name of an internal shell command or an external program. In the case of an external program, the value of $?
is the value the program passed to the exit
system call. If the shell can't find a program or command with that name, it gives you the error message you described.
In a compiled C program, there are no "codes" returned after a statement runs. There is only a state change in the program. For example, if a statement is i = i + 1;
, then the value of i
is 1 greater than its value prior to the statement running.
Upvotes: 1