1/55
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
GDB
GDB (GNU Debugger) lets you inspect a running program—setting breakpoints, stepping through code, and examining variables—to diagnose issues like segmentation faults and logic errors.
-g flag
The -g flag includes debugging symbols in the executable so GDB can map binary code back to source code lines and variable names.
Starting GDB for an executable
Run gdb prog1 in the terminal (or start GDB and then use file prog1).
Breakpoint
A breakpoint pauses execution at a specific point. Set one by using break filename:linenumber (e.g., break file1.c:15) or by function name (e.g., break my_function).
Conditional breakpoint
A conditional breakpoint stops execution only if a given condition is met (e.g., break file1.c:20 if counter > 10), which helps focus on specific problem scenarios.
GDB command: run
Starts the program from the beginning.
GDB command: continue
Resumes execution until the next breakpoint.
GDB command: step
Executes the next line and steps into functions.
GDB command: next
Executes the next line but skips over function calls.
Inspecting a variable's value in GDB
Use print variable_name (add /x for hexadecimal, e.g., print/x counter).
Backtrace (bt) command
It displays the call stack, showing the sequence of function calls that led to the current point in execution.
Watch command in GDB
It monitors a variable and pauses execution whenever its value changes.
Finish command in GDB
It runs until the current function returns, letting you quickly exit deep function calls.
Shell script
A shell script is a text file with a series of commands that the shell executes, automating repetitive tasks and simplifying complex command sequences.
Defining and accessing a variable in a shell script
Define with no spaces (e.g., name="Alice") and access using $name (e.g., echo $name).
Command substitution
It replaces a command with its output using backticks (`command`) or the syntax $(command).
Redirecting output to a file in a shell script
Use > to write (overwrite) and >> to append to a file.
Pipe (|) operator
It takes the output of one command and sends it as input to another (e.g., ls -l | grep ".txt").
If-then-else statement syntax in a shell script
if [ condition ]; then
# commands if true
elif [ another_condition ]; then
# commands if second condition is true
else
# commands if none are true
fi
For loop in a shell script
for num in 1 2 3 4 5; do
echo "Number: $num"
done
Difference between while loop and until loop
A while loop executes as long as a condition is true; an until loop executes until a condition becomes true.
Executing a shell script file
First, make it executable with chmod +x myscript.sh and then run it using ./myscript.sh.
Execute a command in the background
Append an ampersand (&) to the command (e.g., sleep 10 &).
Grouping commands using parentheses vs curly braces
Parentheses run grouped commands in a subshell (isolated environment), while curly braces group commands in the current shell without spawning a new process.
fork() system call in C
It creates a new process (child) that is a nearly identical copy of the calling process (parent), enabling concurrent execution.
Return values of fork() in parent and child processes
The child process receives 0, while the parent receives the child's PID (Process ID).
Purpose of using pipe() in C
pipe() creates a unidirectional communication channel with two file descriptors—one for reading and one for writing—allowing inter-process communication.
File descriptors returned by pipe()
An array with two elements: fd[0] for reading and fd[1] for writing.
File descriptor usage in forked process with pipe
The child closes the read end (fd[0]) and writes to the write end (fd[1]); the parent closes the write end (fd[1]) and reads from the read end (fd[0]).
Roles of dup() and dup2() functions
They duplicate file descriptors—dup() to the lowest available descriptor, and dup2() to a specified descriptor (after closing it if necessary)—which is useful for redirecting I/O.
Usage of execvp() in a forked process
In the child process, execvp() replaces the current process image with a new program, effectively running a different command.
Basic workflow of using a pipe for communication
The parent creates a pipe, forks a child, the child writes data to the pipe (after closing its read end), and the parent reads the data (after closing its write end).
Makefile
A makefile automates the build process for C/C++ projects by specifying dependencies and rules, ensuring that only changed files are recompiled.
Main components of a rule in a makefile
A target (the file to be built), dependencies (files needed for the build), and a recipe (the commands to build the target).
Indentation requirement in makefiles
The make utility requires a tab to distinguish command lines from other text; using spaces will result in an error.
Automatic variables in makefiles
Automatic variables are placeholders within rules—for example, $@ represents the target name and $< represents the first dependency.
Declaring a phony target in a makefile
Declare it using .PHONY: (e.g., .PHONY: clean) to indicate that the target is not a file but a command to execute, such as cleaning up build files.
Recompilation in makefiles
It uses dependency rules and file modification timestamps so that only targets with changed dependencies are rebuilt.
Simple makefile rule example
program.o: program.cpp
g++ -Wall -g -c program.cpp -o program.o
Purpose of the clean target in a makefile
The clean target is used to remove files generated by the build process.
clean target in a makefile
It removes compiled objects and executables, allowing a fresh rebuild.
makefiles and shell scripting
Makefiles execute shell commands (and can call shell scripts) to compile and link code; proper file permissions are crucial for these operations.
three basic Unix file permissions
Read (r): view file contents; Write (w): modify or delete the file; Execute (x): run the file as a program or script.
file permissions representation
Symbolically with letters (e.g., rwxr-xr--) and numerically (e.g., 755), where each digit represents the permissions for owner, group, and others.
chmod command
chmod changes permissions. You can specify them symbolically (e.g., chmod u+x file) or numerically (e.g., chmod 755 file).
file ownership and group membership
Each file is assigned an owner and a group; permissions are set separately for the owner, group, and others, determining who can read, write, or execute the file.
impact of file permissions on execution
Scripts or executables must have the execute permission set (e.g., via chmod +x) or they won't run, regardless of how correct the code is.
setuid and setgid bits
They allow programs to run with the privileges of the file's owner or group, which is crucial for tasks requiring elevated permissions.
security risk of overly permissive settings
They let any user read, write, and execute the file, potentially enabling unauthorized modifications or exploitation.
file permissions in IPC using named pipes
Named pipes (FIFOs) are filesystem objects; their permissions determine who can read from or write to them, enforcing access control.
relationship between file permissions and sudo
sudo temporarily grants elevated privileges, allowing execution of commands that would otherwise be blocked by restrictive file permissions.
file permissions in collaborative projects
Correctly set permissions ensure only authorized users can access or modify files, preventing accidental or malicious changes while supporting collaboration.
troubleshooting script execution issues
Use ls -l to inspect file permissions, verify that the execute bit is set (e.g., chmod +x script.sh), and check file ownership.
differences between while loop and for loop
A while loop runs as long as a condition is true (useful for indeterminate iteration), whereas a for loop iterates over a fixed list of values; they serve different use cases based on whether the number of iterations is known.
file permission management and debugging
Correct permissions prevent unauthorized changes and accidental execution errors, ensuring only intended users can modify or run files—thereby reducing debugging complexity and enhancing security.
common pitfalls related to file permissions
Common pitfalls include missing execute permissions on scripts, improper file ownership, and overly permissive settings that might lead to security vulnerabilities or unintended access.