Linux one step back

How to go to the previous line in GDB?

I am at line b currently and can examine the arr values there but I want to go back to line a and examine the contents of arr at that time. I think it might not be possible because a debugger can run a code in slow motion,but can’t make it execute backwards.
Any more insights..

9 Answers 9

Yes! With the new version 7.0 gdb, you can do exactly that!

The command would be » reverse-step «, or » reverse-next «.

You can get gdb-7.0 from ftp.gnu.org:/pub/gnu/gdb

If you run into the error: Target child does not support this command. then try adding target record at the beginning of execution, after starting run .

Edit: Since GDB 7.6 target record is deprecated, use target record-full instead.

Is record the same as target record-full ? It seems to work for me, and it’s the first command mentioned at sourceware.org/gdb/current/onlinedocs/gdb/…

Yes, it is possible, and straightforward, now, with real hardware (ie. not just with a VM). GDB-7.0 supports reverse debugging with commands like reverse-step and reverse-continue, on native linux x86 machines.

GDB’s built-in record and replay has severe limitations, e.g. no support for AVX instructions: gdb reverse debugging fails with «Process record does not support instruction 0xf0d at address»

  • much more reliable currently. I have tested it relatively long runs of several complex software.
  • also offers a GDB interface with gdbserver protocol, making it a great replacement
  • small performance drop for most programs, I haven’t noticed it myself without doing measurements
  • the generated traces are small on disk because only very few non-deterministic events are recorded, I’ve never had to worry about their size so far

The following example showcases some of its features, notably the reverse-next , reverse-step and reverse-continue commands.

sudo apt-get install rr linux-tools-common linux-tools-generic linux-cloud-tools-generic sudo cpupower frequency-set -g performance # Overcome "rr needs /proc/sys/kernel/perf_event_paranoid  
#include #include #include int f() < int i; i = 0; i = 1; i = 2; return i; >int main(void) < int i; i = 0; i = 1; i = 2; /* Local call. */ f(); printf("i = %d\n", i); /* Is randomness completely removed? * Recently fixed: https://github.com/mozilla/rr/issues/2088 */ i = time(NULL); printf("time(NULL) = %d\n", i); return EXIT_SUCCESS; >
gcc -O0 -ggdb3 -o reverse.out -std=c89 -Wextra reverse.c rr record ./reverse.out rr replay 

Now you are left inside a GDB session, and you can properly reverse debug:

(rr) break main Breakpoint 1 at 0x55da250e96b0: file a.c, line 16. (rr) continue Continuing. Breakpoint 1, main () at a.c:16 16 i = 0; (rr) next 17 i = 1; (rr) print i $1 = 0 (rr) next 18 i = 2; (rr) print i $2 = 1 (rr) reverse-next 17 i = 1; (rr) print i $3 = 0 (rr) next 18 i = 2; (rr) print i $4 = 1 (rr) next 21 f(); (rr) step f () at a.c:7 7 i = 0; (rr) reverse-step main () at a.c:21 21 f(); (rr) next 23 printf("i = %d\n", i); (rr) next i = 2 27 i = time(NULL); (rr) reverse-next 23 printf("i = %d\n", i); (rr) next i = 2 27 i = time(NULL); (rr) next 28 printf("time(NULL) = %d\n", i); (rr) print i $5 = 1509245372 (rr) reverse-next 27 i = time(NULL); (rr) next 28 printf("time(NULL) = %d\n", i); (rr) print i $6 = 1509245372 (rr) reverse-continue Continuing. Breakpoint 1, main () at a.c:16 16 i = 0; 

rr achieves this by first running the program in a way that records what happened on every single non-deterministic event such as a thread switch.

Then during the second replay run, it uses that trace file, which is surprisingly small, to reconstruct exactly what happened on the original non-deterministic run but in a deterministic way, either forwards or backwards.

rr was originally developed by Mozilla to help them reproduce timing bugs that showed up on their nightly testing the following day. But the reverse debugging aspect is also fundamental for when you have a bug that only happens hours inside execution, since you often want to step back to examine what previous state led to the later failure.

The most serious limitations of rr in my opinion are:

  • https://github.com/mozilla/rr/issues/2089 you have to do a second replay from scratch, which can be costly if the crash you are trying to debug happens, say, hours into execution
  • https://github.com/mozilla/rr/issues/1373 x86 only

Источник

How To Go Back to a Directory in Linux

This tutorial shows how to go back to the previous or parent directories using the cd (Change Directory) command. Additionally, this content includes a complete guide to move quickly between directories in the Linux terminal.

This tutorial is optimized for inexperienced Linux users. By reading this documentation, they will fully understand how the cd (Change Directory) command works and its applications. All instructions described include screenshots, making it easy for every Linux user to understand and apply them.

The cd command implementations to move between directories in the Linux terminal explained in this article include the following cd usage examples:

The cd Command Options Function
cd – Move to the previous directory
cd .. Move to the parent directory
cd ../.. Move to the parent directory of the parent directory (Two levels up)
cd ../../.. Move three levels up
cd Autocomplete path or show available subdirectories
cd Move to the home directory
cd ~ Move to the home directory
cd ~ Move to home directory
cd ‘Directory named with spaces’ Use quotation marks to move to a directory with spaces in name

The Linux Directory Structure (For New Linux Users)

Note: If you already know the basic Linux directory structure, you can jump straight to instructions to go back to previous or parent directories.

First, I want to remind new Linux users that the Linux directory structure is hierarchical. Directories within directories are subdirectories. The directories and subdirectories tree are what we call the “path”. When we specify a path, we are specifying parent directories and subdirectories.

In Linux, the main directory is what we call the root directory. It is the top directory containing the rest of the system directories. Users see a slash after their hostname when the current directory is the root directory, as shown in the following image (Violet slash).

The root directory contains system core directories, including the /boot directory, which contains boot partition and/or directories and files, and the /usr partition and/or directory, which contains program directories and files.

The following image shows the / (root) directory content, most of which is universal for all Linux distributions:

For example, in the path /etc/apt/, the root directory / (First slash) is the parent directory of the /etc subdirectory, which is the parent directory of the /apt directory.

How To Go Back to a Directory in Linux

This section explains how to go back to your previous current directory and to go back to a parent directory using the cd command in Linux.

In the following example, the linuxhint user’s first current directory is his home directory. In the first line, the user uses the cd command to move to the /etc directory, following the hierarchical path beginning from the root directory.

To go back from the current directory (/etc/) to the previous one, the user runs the cd command followed by a hyphen, as shown in the second line of the following figure:

As you can see in the previous image, the user returned to the previous directory by running cd -.

The previous example explains how to return to the previous directory the user was in.

Now, let’s say the user does not want to return to the previous directory he was in, but he wants to go back to the hierarchical tree.

Here, a user is in his home directory, and he moves to the /etc/apt subdirectory. Instead of returning to the previous directory he was in (His home directory), he wants to move to the parent directory of the current one (In this case, the parent directory of /apt is /etc). For this, the user will run the command cd followed by two dots, as shown below:

As you can see in the previous figure, the user moved to the parent directory of the previous one (/etc) and not to the previous directory he was in (/home/linuxhint).

As you can see, cd – moves the user to his previous current directory, while cd .. moves him to the current parent directory.

How To Move to the Parent Directory of the Current Parent Directory (Two Levels Up)

As said in the previous example, the cd .. command allows us to move to the parent directory. If the user wants to move to the parent directory of the parent directory (Two levels up), he needs to type the two dots twice, separated by a slash.

In the following example, the user’s current directory is linuxhint2, located under the linuxhint directory, which is located under the Desktop directory within the user home.

Let’s say the user wants to move two levels up, from the linuxhint2 directory to the Desktop directory. In this case, he only needs to use the two dots twice, separated by a slash, as shown in the following screenshot:

The previously executed command instructs Bash to take the user two directories up in the hierarchical tree. You also would implement additional dots and slashes to move to upper levels in the directory tree.

Using the TAB Key With the cd Command to Autocomplete Paths

The keyboard key tab plays an important role when using the cd command. It helps users move between directories without typing the whole path and without knowing the final path in advance.

In the following example, four subdirectories begin with the same name, but only the last parts of their names change: such as linuxhint, linuxhint2, and linuxhint3.

Suppose the user runs cd linuxhint and presses the TAB key twice. Then, the console returns all existing subdirectories, whose names begin with the path typed by the user. Thus, informing the user of the available subdirectories for him to type the complete full path.

In the following image, the user executed cd Desktop/linuxhint and then pressed the TAB key:

Even if the user does partially type the destination full path, the TAB key will also show all available subdirectories within the directory we want to access.

In the following example, the user knows he wants to access a subdirectory within his home directory. But he does not know what is the specific subdirectory he wants to access.

In this case, the user can run cd /parentdirectory/ and press the TAB key twice. This will print all subdirectories within the parent directory:

How To Move to the Home Directory in the Linux Terminal

Moving to your home directory is pretty simple. Just run the cd command without additional options, as shown below:

You also can use a tilde (~) to specify your home directory as a path. By running the cd command followed by a tilde, you will move to your home directory.

You can use the tilde to move to any user’s home directory. To accomplish it, type the username whose directory you want to move to, after the tilde symbol. As shown in the following example, the linuxhint user moves to the torvalds directory.

Move to a Directory With Spaces in its Name

Let’s assume the user wants to move to a directory whose name contains spaces to end this tutorial. In this case, the user only needs to type the directory name between quotation marks, as shown in the following screenshot.

In the following example, the user implements quotation marks to move into the Linux Hint directory:

Conclusion

The article discussed how the cd command allows users to move between different locations faster than the graphical user interface. All commands shown in this content are easy to learn and implement, and required for any Linux user.

The cd command is also available in other operating systems, like macOS or MSDOS, and the command is universal for all Linux distributions. I recommend readers to utilize the provided examples to make terminal browsing easier.

I hope all tips provided are useful for new users. Keep following us for more professional Linux articles.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

Читайте также:  Linux what version is running
Оцените статью
Adblock
detector