Debugging and Scripting in C and Unix/Linux

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/55

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

56 Terms

1
New cards

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.

2
New cards

-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.

3
New cards

Starting GDB for an executable

Run gdb prog1 in the terminal (or start GDB and then use file prog1).

4
New cards

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).

5
New cards

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.

6
New cards

GDB command: run

Starts the program from the beginning.

7
New cards

GDB command: continue

Resumes execution until the next breakpoint.

8
New cards

GDB command: step

Executes the next line and steps into functions.

9
New cards

GDB command: next

Executes the next line but skips over function calls.

10
New cards

Inspecting a variable's value in GDB

Use print variable_name (add /x for hexadecimal, e.g., print/x counter).

11
New cards

Backtrace (bt) command

It displays the call stack, showing the sequence of function calls that led to the current point in execution.

12
New cards

Watch command in GDB

It monitors a variable and pauses execution whenever its value changes.

13
New cards

Finish command in GDB

It runs until the current function returns, letting you quickly exit deep function calls.

14
New cards

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.

15
New cards

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).

16
New cards

Command substitution

It replaces a command with its output using backticks (`command`) or the syntax $(command).

17
New cards

Redirecting output to a file in a shell script

Use > to write (overwrite) and >> to append to a file.

18
New cards

Pipe (|) operator

It takes the output of one command and sends it as input to another (e.g., ls -l | grep ".txt").

19
New cards

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

20
New cards

For loop in a shell script

for num in 1 2 3 4 5; do

echo "Number: $num"

done

21
New cards

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.

22
New cards

Executing a shell script file

First, make it executable with chmod +x myscript.sh and then run it using ./myscript.sh.

23
New cards

Execute a command in the background

Append an ampersand (&) to the command (e.g., sleep 10 &).

24
New cards

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.

25
New cards

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.

26
New cards

Return values of fork() in parent and child processes

The child process receives 0, while the parent receives the child's PID (Process ID).

27
New cards

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.

28
New cards

File descriptors returned by pipe()

An array with two elements: fd[0] for reading and fd[1] for writing.

29
New cards

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]).

30
New cards

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.

31
New cards

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.

32
New cards

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).

33
New cards

Makefile

A makefile automates the build process for C/C++ projects by specifying dependencies and rules, ensuring that only changed files are recompiled.

34
New cards

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).

35
New cards

Indentation requirement in makefiles

The make utility requires a tab to distinguish command lines from other text; using spaces will result in an error.

36
New cards

Automatic variables in makefiles

Automatic variables are placeholders within rules—for example, $@ represents the target name and $< represents the first dependency.

37
New cards

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.

38
New cards

Recompilation in makefiles

It uses dependency rules and file modification timestamps so that only targets with changed dependencies are rebuilt.

39
New cards

Simple makefile rule example

program.o: program.cpp

g++ -Wall -g -c program.cpp -o program.o

40
New cards

Purpose of the clean target in a makefile

The clean target is used to remove files generated by the build process.

41
New cards

clean target in a makefile

It removes compiled objects and executables, allowing a fresh rebuild.

42
New cards

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.

43
New cards

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.

44
New cards

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.

45
New cards

chmod command

chmod changes permissions. You can specify them symbolically (e.g., chmod u+x file) or numerically (e.g., chmod 755 file).

46
New cards

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.

47
New cards

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.

48
New cards

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.

49
New cards

security risk of overly permissive settings

They let any user read, write, and execute the file, potentially enabling unauthorized modifications or exploitation.

50
New cards

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.

51
New cards

relationship between file permissions and sudo

sudo temporarily grants elevated privileges, allowing execution of commands that would otherwise be blocked by restrictive file permissions.

52
New cards

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.

53
New cards

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.

54
New cards

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.

55
New cards

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.

56
New cards

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.