System Programming With Linux Debugging Using C and C++ Programming Topics
System Programming With Linux Debugging Using C and C++ Programming Topics
1
Constant Pointers C Pointers Complexity Chart
Pointer Arithmetic int **p
String Handling Functions int (*p)()
String Conversion Functions int (*p)[]
Efficient usage of sscanf()/sprintf() int *p()
int *(*p[])()
Computing Basic int *(**p)[]
Binary & Octal Systems
Decimal & Hexadecimal Systems Preprocessor
Signed Representations in Memory Preprocessor – #include statements
Binary Shifts – Right & Left Preprocessor – #define statements
Sign Bits and Bit-Shift Operations Preprocessor – Conditional Compilation
ASCII Representations Preprocessor – Nested Macros
Endian-ness – Little Vs Big Preprocessor – Multiline Macros
Endian-ness – Portability Issues Preprocessor – Stringizer
Operators Preprocessor – Token Concatenation
Bitwise Operations Preprocessor – Useful Directives
Logical Operators – Short Circuit Conditional Directives for Debugging
Bitwise Vs Logical Operations Where Macros are Heavily Used
sizeof() operator Practical Examples of Macros
Pitfalls/Issues with sizeof() usage Macros Pitfalls
Pointer Increment & Scaling Macros Vs Enums
Operator Precedence Inline Functions
Operator Associativity Macros Vs Inline
True meaning of Associativity Inline Recursive Functions
Examples of Precedence & Associativity
Ternary Operator Associativity Rule Command Line Argument
Data-type Conversion Rules Environment Variables in C Programs
Float to Int to Float Conversions Recursion Example
Printf Idiosynchracies Recursion Vs Iteration
Signed Vs Unsigned – Pitfalls Code/Space/Time Complexity
Evaluation of i = ++i + ++i
Evaluation of i = ++i + ++i + ++i Standard I/O Library
Concept of Sequence Points Files & Streams
Example of Sequence Points Streams Buffers
No Buffer, Line Buffer and Full Buffer
Storage Classes Setting & Flushing Buffers
Storage Class Specifiers File Access
Scope of a Variable File Access Modes
Register, Auto, Static, Extern Sequential Vs Random Access
Why Register Class and Practical Examples Concept of File Offsets
Automatic Variables and Stack File Operation Errors
Static Variables and Functions End-of-File Condition?
True meaning of Extern Return Values and Error Values
How to Use extern across Multiple Files with Character Based File I/O
Examples Line Based File I/O
2
Best Practices for Extern Usage Formatted File I/O
Local/Block/Global Scope Block File I/O
Nesting of Scope File
Lifetime of a Variable
Linkage of a Variable
What is Const?
Practical Examples of Const Qualifier
What is Volatile?
Practical Examples of Volatile Qualifier
Const Volatile Together?
Practical Examples of Const Volatile
Pointer Aliasing
What is Restrict Qualifier?
Examples of Restrict Qualifier
3
Linux Kernel Introduction Process Focus
Linux System Viewing Processes
Linux Organization Process Tree & Process Status
Linux Version Identifying System & Daemon Processes
Basic OS Concept Foreground and Background Processes
Linux N+1 System Tuning Process Scheduling
Linux Filesystem Process Priority
File Types nice & renice commands
Processes & File Objects Killing / Terminating a Processes
File Object Access Max Processes per User – ulimit
The Process/Kernel Model Per Process Resource Limits
Re-entrant Kernel Stack Sizes
Interleaved Kernel Control Paths Core File Limits
Preemptive Kernel IPCs Consumption and Limits
Preemptive Context Switch
Synchronization Process Internals
Deadlock Anatomy of a Process
Memory Management Process Map in Linux
Memory Layout
Memory Addressing Code Segment
Logical, Linear & Physical Addresses Data Segment
Translating a Logical Address Stack Segment
Segmentation in Linux Heap Segment
Paging in Linux Decoding Process Map Lab
Large Pages
Physical Page Extension(PAE) System Focus
Hardware Cache Performance Monitoring & Troubleshooting
Cache Coherency System Status – Resource Consumption
Translation Lookaside Buffers (TLBs) System Status – Memory Usage
Linux 4 Table Paging Model System Status – I/O Activity
Physical Memory Layout System Status – CPU Usage
Kernel Physical Memory Variables Resource Usage – Trends
Process Page Tables top, iostat, vmstat, sar commands
Kernel Page Tables Sample Programs to Consume CPU and
TLB Management Memory
Memory Eater Lab
Processes in Linux Kernel CPU Loader Lab
Process
Lightweight Process Troubleshooting Methods
Threads & Thread Groups Process Syscall Tracing – strace
Process Descriptions Process Libcall Tracing – ltrace
The task_struct Process Kernel/User Space Time Check
Linux Process States Profiling Data using System & Library Traces
Thread Group Leader
The Process List GDB Debugging
Waiting Processes GDB Deep Dive Lab
4
Wait Queues Debugging a Live Process
Awakening Processes Starting a Program with GDB
Process Creation Debugging a Memory/Core Dump
Kernel Threads Stack Traces
Process 0 & Process 1 Common Failures
Destroying Processes Failure Notifications
Process Removal Common Faults
Segmentation Violation
Process Scheduling Stack Overflow
Process Preemption Heap Overflow
Quantum Duration
Scheduling Policies Linux Kernel Crash Dump Analysis
Scheduling of Conventional Processes Crash Dump Process
Dynamic Priority How kexec / kdump Works
Realtime Processes Settinp up Kexec/Kdump
Realtime Process Scheduling crashkernel parameter
Runqueue Balancing in Multiprocessing Set up, Testing and Running Crash
Systems Crash Analysis
Scheduling Domains
Load Balancing
CPU Binding
Signals in Linux
Signal Related System Calls
Signal Generation and Delivery
Pending Signals
Action Performed Upon Delivery
Signals @ Multithreaded Application
Delivering the Signal
Catching the Signal
Restarting System Calls
=====================================================================================
5
Re-entrant Kernel C Library or Standard Headers?
Interleaved Kernel Control Paths GNU C Extensions
Preemptive Kernel Inline Functions
Preemptive Context Switch Inline Assembly
Synchronization Branch Annotation
Deadlock Memory Protection?
Memory Management Floating Point?
Stack Sizes
Processes in Linux Kernel
Process
Lightweight Process
Memory Addressing
Threads & Thread Groups
Logical, Linear & Physical Addresses
Process Descriptions
Translating a Logical Address
The task_struct
Segmentation in Linux
Linux Process States
Paging in Linux
Thread Group Leader
Large Pages
thread_info Structure
Physical Page Extension(PAE)
Kernel Stack Structure
Hardware Cache
The Process List
Cache Coherency
Waiting Processes
Translation Lookaside Buffers (TLBs)
Wait Queues
Linux 4 Table Paging Model
Awakening Processes
Physical Memory Layout
Process Creation
Kernel Physical Memory Variables
clone() / fork() / vfork()
Process Page Tables
Kernel Threads
Kernel Page Tables
Process 0 & Process 1
TLB Management
Destroying Processes
exit_group() / _exit()
Process Removal
Kernel Synchronization
Kernel Preemption
When Synchronization is Necessary
Timing Measurements in Linux
Interleaved Kernel Control Paths
Clocks and Timer Circuits
When Synchronization is Not Necessary
Linux Timekeeping Architecture
Synchronization Constraints
The jiffies Variable
Synchronization Primitives
Updating System Statistics
Per-CPU Variables
Software Timers
Atomic Operations
Dynamic Timer List Structure
Optimization & Memory Barriers
Timer List Data-Structure
Spin Locks
Per-CPU Timer List
Read/Write Spin Locks
Delay Functions
Seqlocks
udelay() and ndelay()
Read-Copy Update (RCU) Locks
System Calls for POSIX Timers and Clocks
Semaphores
Read/Write Semaphores
Local Interrupt Disabling
6
Disabling Deferrable Functions
Process Scheduling Memory Management
Process Preemption Page Frame / Page
Quantum Duration Memory Zones
Scheduling Policies Reserved Page Frames
Scheduling of Conventional Processes High Memory Page Frames
Dynamic Priority Temporary Kernel Mappings
Realtime Processes Dynamic Contigous Page Allocation
Realtime Process Scheduling Slab Allocator
Runqueue Data Structure Object Caches
Runqueue Balancing in Multiprocessing Systems Slab Cache Allocation
Scheduling Domains Object Allocation
Load Balancing General Purpose Allocation – kmalloc()
CPU Binding Non-contigous Memory Area Management
Process Address Space
Kernel Address Space
Process Address Space
System Calls for Memory Region System Calls
brk(), execve(), _exit(), fork() System Call Handler
mmap(), mmap2(), munmap(), Service Routines
shmat(), shmdt() Invoking a System Call
Page Faults Leaving a System Call
Valid & Invalid Addresses Arguments to System Call
The Memory Descriptor Arguments in Registers
The mem_struct Structure Verifying the Arguments
Memory Regions Accessing Process Address Space
Linear Address Intervals – do_mmap() Exception Tables
Page Fault Exception Handling Fixing Address Exceptions
Demand Paging Kernel Wrapper Routines
Copy on Write (COW)
Creating Process Address Space
Managing Heap Space
Signals in Linux Linux Page Cache
Signal Related System Calls Page Cache Contents
Signal Generation and Delivery Page Cache Requirements
Pending Signals The address_space Object
Action Performed Upon Delivery Block Buffers and Page Cache
Signals @ Multithreaded Application The Buffer Head
Data Structures for Signal Handling Buffer Pages
Delivering the Signal Writing Dirty Pages to Disk
Catching the Signal bdflush & pdflush
Restarting System Calls sync(), fsync() and fdatasync() calls
Page Frame Reclamation Linux Virtual Filesystem
Page Frame Reclaiming Algorithm VFS Role in File Operations
Unreclaimable pages VFS Supported Filesystem Classes
7
Swappable Pages Common File Model
Syncable Pages Process & VFS Object Interaction
Discardable Pages VFS Data Structures
Design of the Algorithm Super Block Object
Reclaimation Tigger Points Inode Object
Low on Memory File Object
Periodic Reclaiming Dentry Object
The Out-of-memory Killer Processes and Files
Swapping Features The fd array
Constraints on a Process
File System Types
Filesystem Type Registration
Filesystem Handling
Mounting a Filesystem
Pathname Lookup
Reads & Writes
Other File Operations
Kernel Debuggers
gdb analysis of Live Kernel
Limitations of gdb analysis
kdb analysis of Live Kernel
Limitations of kdb analysis
kgdb analysis of Live Kernel
Setting up kdb and kgdb
kexec / kdump
LKCD
8
Linux Kernel Crash Dump Analysis
Crash Dump Process
How kexec / kdump Works
Settinp up Kexec/Kdump
crashkernel parameter
Set up, Testing and Running Crash
Crash Analysis
Hands-on Lab
Configuring the Kernel for Debugging
Kernel Parameters for KDB and KGDB
Building Kernel for Debugging
Turning on Magic Sysrq Keys
Debugging a Live Kernel using gdb, kdb and kgdb
Printing Kernel Global Variables and Code
Loading a Debug Module using kdb and kgdb
Insert bug(s)
make system hang
Crash and Analyze
9
File Operations Structure Deadlock – Sources
Driver-User Data Transfer Deadlock Prevention
Driver-Kernel Communication Waitqueues
Driver-Device Rules for Sleeping
Communication Waitqueue APIs
Device File Creation Linux Kernel Tree
Device File Control Linux Source Code
Operations Linux Kernel Configuration
Lab:
Lab1 – Identification of major and minor numbers for various popular (reserved) devices.
Lab2 – Writing simple kernel module with command line arguments.
Lab3 – Identification of currently allocated IO-ports, IO-memory & IRQs on your system/laptop.
Lab4 – Writing a memory based character device driver (DLKM Kernel Module) of fixed size
Lab5 – Writing an advanced memory based character device driver of dynamic size
Lab6 – Writing /dev/sanfd_zero device driver (reading any sized data from this device returns zero-filled
data).
Lab7 – Writing /dev/sanfd_null device driver (ala bit-bucket / black-hole driver).
Lab8 – Implementation of ioctls – RESET (it should reset the device to its default size and initial values),
GETSIZE (should return the current size of the device), EXPAND X (will expand the size of
/dev/sanfd_dynamic device by X bytes).
Lab9 – Writing a userspace program to get the device size.
Lab10 – Writing a userspace program to expand the dynamic device size by 1MB and verify the working
of the driver.
Lab11 – Writing user-space code to parallelly generate load on the devices, generate race conditions and
implement locks in the driver to fix all the issues.
===================================================================================
Linux Threads
Introduction to Threads
Parallelism
Multi-cores
Introduction to Thread
Threads – Application
User Level Threads (ULT)
Advantage of ULT
Disadvantage of ULT
Kernel Level Threads (KLT)
Advantage of KLT
Disadvantage of KLT
10
Threads Vs Processes
Process Memory Layout
Child Process Memory
Threads Inside Processes
Thread Memory Layout
Thread Programming – I
Introduction to Pthreads (POSIX Threads)
Thread Creation
Thread Termination
Thread Id & Attributes
Thread Joining & Detaching
Thread Priorities
Thread Cancellations
Thread Cleanup Handling
Passing Info to Threads
Thread Programming – II
Thread Synchronizations
Mutual Exclusions
Condition Variables
Thread Safe Operations
Thread Signal Handling
Thread Signal Masking
Issues @ Threads
Races & Deadlocks
Threads & Process Resources
Threads – File Handling
Threads – IPC Handling
11
Concurrent Server Design
Iterative Server Design
Socket Debugging Techniques
Advanced Socket Options
Changing Socket Buffer Sizes
Reusing Socket Addresses
Keeping the Connections Alive
TCP Socket Options
Setting Max Segment Size
Setting TCP No-Delay
Advanced I/O Functions
Socket Timeouts
Vectored Read/Writes
Netstat – Network Stat Utility
Tcpdump – Packet Dump Utility
Introduction to Unix
Linux Layered Architecture Why IPCs
Bootup Sequence Type of IPC Mechanisms
File Tree & Types Primitive IPCs
File Systems Pipes & FIFOs
Super Block & Inode Full Duplex Pipes
System Vs Function Calls Persistence of Pipes & FIFOs
System Call Sequence Pros and Cons of Pipes/FIFOs
File descriptor table Limitation of Pipes/FIFOs
File Management System V IPCs
File related System Calls IPC Attributes
File Control Operations IPC Persistence
File Locking Kernel Limits for these IPCs
fcntl() calls Shared Memory
Process Management Shared Memory System Calls
Mode of Execution Pros and Cons of Shared Memory
Address Space Message Queues
Context Switching Message Queue System Calls
Process Structure & States Pros and Cons of Message Queues
Scheduling & Priority Semaphores
Process Creation & Exec Classical P() & V() Operations
fork()/exec() calls Binary & Counting Semaphores
Memory Management Semaphore System Calls
Virtual Memory Pros and Cons of Semaphores
Paging & Swapping IPC Control Operations
Memory Mapping IPC Removal
Demand Paging
12
Signals Vs. Interrupts
Signals in Unix
Receiving a Signal
Handling a Signal
Signal System Calls
User Vs Daemon Process
Characteristics of a Daemon
Writing a Daemon
Timers & Resource Limits
Interval Timers
High Resolution Timers
System calls for Timers
Resource Limits
Hard Limit / Soft Limit
The structure of Linux is that it has a layer that interacts with programs, a layer that interacts with the
hardware and there is not much I'm leaving out. With Windows there are so many virtual machines and
other things involved it's crazy. Answerfrom top:
3. Applications
2. Layer
1. Kernel
0. Hierachical file system
13
FINALLY
Advanced C++
Session – 1 – C++ Overview
Need for C++
Session – 2 – Class Details
Differences of C++ compared to C# and Java
Pointer to Members
C++ as a federation of many concepts
Nested Classes
Is C++ so big and complex?
The const function, the const Variable
Is there a sweet subset of C++ that everyone
The const Object, the const Argument
can learn?
Multi argument Constructors
Structure Extension and Introduction to
Overloaded Constructors
classes
Virtual Constructors
Modularity and Data Abstraction Evolution
Virtual Destructors
Classes, Constructors, Destructors and The
Order of calling constructors and destructors
this pointer
Uses of :: Scope Resolution Operator
Major and Minor Pillars of OO and how they
Pointer to class object as data members
are implemented in C++
Array of Class Objects
Object Initialization and Cleaning Up
Static Keyword in C and C++
The “this” Pointer
Static Data Members
Data Members and Member function
Static functions and static objects
Member Qualifications
Problem solving and lab session
Problem solving and exercises
Writing code in C++
Problem solving and lab session
Discussion
Writing code in C++
Discussion
Session – 3 – Compile time polymorphism Session – 4 – Compilation and Linkage
Compile time polymorphism Compile time polymorphism
14
Function overloading Function overloading
Operator overloading – with strings and Operator overloading
iostream What is a reference and how is reference different
When and how to use function overloading from a pointer?
When and how to use operator overloading Where is it used?
This Pointer in C++ Inline function and Default arguments
Void type Const modifier Introduction to Linkage
Prototyping a function Internal Linkage and External Linkage
Types of Operators Header files and Linkage to non-C++ code
Extension, Linkage & Compilation Compilation procedure and how to make to make
Problem solving and lab session C and C++ Libraries
Writing code in C++ Real examples of using operator overloading
Discussion Friend function and friend class
Solving problems on levels of inheritance Operator overloading and friend functions
Problems in function overloading Operator overloading and Input output streams in
C++
Problem solving and lab session
Writing code in C++
Discussion
Session – 6 – More C++ Conversions
Session – 5 – C++ Conversions
Conversions in C++
Conversion between basic data type and user
What is a conversion and the need for it
defined data type
Solving problems
Conversion using conversion function in the
Simple conversions
source class
Problems in built-in conversion
Conversion using overloading assignment
User defined data types
operator in the destination
Conversion between user defined to basic
Is the conversion code clean?
data type
Can we get away without using conversions?
Design Issues and Implementation issues
Variants of the design pattern
Problem solving and lab session
Problem solving and lab session
Writing code in C++
Writing code in C++
Discussion
Discussion
Session – 7 – Run time Polymorphism and Session – 8 – Run time Polymorphism and design
design guidelines guidelines
Run time polymorphism Back to polymorphism
How is this polymorphism runtime? How polymorphism is implemented in C++
The difference between runtime and compile VTable and its implementation
polymorphism Use of function pointers
Static and dynamic binding What does the C++ 11 and C++ 14 standard say?
Rectangle and Square problem What the instructions generated by the compiler
‘Is a’ and ‘has a’ relationship for runtime polymorphism?
‘whole part’ relationship Polymorphism – the fundamental building block
‘using’, ‘implemented in terms of’ and of Object Oriented Analysis,
‘realization’ relationship Design and Programming
Simple virtual functions Introduction to multithreading in C++
15
Pure Virtual functions Uses of multithreading
Non Virtual functions Thread pooling pattern
Difference between Abstract class and Active Object design pattern
interface in C++ Worker thread pattern
Problem solving and lab session Problem solving and lab session
Writing code in C++ Writing code in C++
Discussion Discussion
Session 9 : Templates and Exception
handling
Session 10: Introduction Generic Programming
Introduction to templates
with STL and Course Review
Templates and macros
Smart Pointers and their applications
Advantages of using templates
Smart Pointers and raw pointers
Are templates Turing complete?
Resource allocation
Template Specialization and partial
Introduction to Generic Programming
specialization
STL and its significance in clean and efficient
Template Meta Programming
programming
Usage of type names in template
Algorithms, Iterators and Containers
What are exceptions?
Types of Containers available and programming
Does exception handling against the idea of
using them
structured programming?
Iterator traits
try, catch and throw
Iterator types and how they are used
Nested Exceptions
Review of the course
Writing to code in C++ to understand
exceptions
16
ress the power button on your system, and after few moments you see the Linux login prompt.
Have you ever wondered what happens behind the scenes from the time you press the power
button until the Linux login prompt appears?
The following are the 6 high level stages of a typical Linux boot process.
1. BIOS
2. MBR
17
3. GRUB
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-194.el5PAE)
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-194.el5PAE ro root=LABEL=/
initrd /boot/initrd-2.6.18-194.el5PAE.img
As you notice from the above info, it contains kernel and initrd image.
So, in simple terms GRUB just loads and executes Kernel and initrd images.
4. Kernel
5. Init
18
Execute ‘grep initdefault /etc/inittab’ on your system to identify the default run level
If you want to get into trouble, you can set the default run level to 0 or 6. Since you know what 0
and 6 means, probably you might not do that.
Typically you would set the default run level to either 3 or 5.
6. Runlevel programs
When the Linux system is booting up, you might see various services getting started. For
example, it might say “starting sendmail …. OK”. Those are the runlevel programs, executed
from the run level directory as defined by your run level.
Depending on your default init level setting, the system will execute the programs from one of
the following directories.
o Run level 0 – /etc/rc.d/rc0.d/
o Run level 1 – /etc/rc.d/rc1.d/
o Run level 2 – /etc/rc.d/rc2.d/
o Run level 3 – /etc/rc.d/rc3.d/
o Run level 4 – /etc/rc.d/rc4.d/
o Run level 5 – /etc/rc.d/rc5.d/
o Run level 6 – /etc/rc.d/rc6.d/
Please note that there are also symbolic links available for these directory under /etc directly.
So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.
Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.
Programs starts with S are used during startup. S for startup.
Programs starts with K are used during shutdown. K for kill.
There are numbers right next to S and K in the program names. Those are the sequence number
in which the programs should be started or killed.
For example, S12syslog is to start the syslog deamon, which has the sequence number of 12.
S80sendmail is to start the sendmail daemon, which has the sequence number of 80. So, syslog
program will be started before sendmail.
19