GDB is the next powerful debugging tool in Linux, Windows corresponding to the WinDbg, the following examples of common program error Resolution
1.GDB Boot
To use GDB debugging, compile-time specifies the-G option to add debugging information, GDB can start the execution file, attach is running the program, debug the program crashes produce core files
Start GDB, enter run Run, continue continue, quiet exit, the following is debugging a crash and deadlock of the source code
#include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <s
Tring.h> pthread_mutex_t Mutex;
int count = 0;
void Print_pid_tid () {pid_t pid;
pthread_t Tid;
PID = Getpid ();
Tid = Pthread_self ();
printf ("pid%u tid%u (0x%x) \ n", (unsigned int) PID, (unsigned int) tid, (unsigned int) tid);
} void Callback_func () {pthread_mutex_lock (&mutex);
printf ("count:%d\n", count);
} void *thread_func1 (void *arg) {while (1) {int n = * (int *) arg);
Pthread_mutex_lock (&mutex);
Print_pid_tid ();
Count + 2;
for (int i=0 i < 5; ++i) {count = n;
} callback_func ();
Pthread_mutex_unlock (&mutex);
Sleep (1);
return 0;
} void *thread_func2 (void *arg) {while (1) {pthread_mutex_lock (&mutex);
printf ("Thread_func2 run\n");
Pthread_mutex_unlock (&mutex);
Sleep (1);
return 0;
} void *thread_func3 (void *arg) {while (1) {char *str = NULL; //strcpy (str, "Hello World");
Sleep (1);
return 0;
int main (void) {pthread_t ntid;
int count = 10;
Pthread_mutex_init (&mutex, NULL);
int err = Pthread_create (&ntid, NULL, THREAD_FUNC1, &count);
if (0!= err) {printf ("pthread_create1:%s\n", strerror (err));
Err = Pthread_create (&ntid, NULL, THREAD_FUNC2, &count);
if (0!= err) {printf ("pthread_create2:%s\n", strerror (err));
Err = Pthread_create (&ntid, NULL, thread_func3, &count);
if (0!= err) {printf ("pthread_create3:%s\n", strerror (err));
} getchar ();
int **ret = NULL;
Pthread_join (Ntid, (void**) ret);
printf ("pthread_join:%p\n", *ret);
Pthread_mutex_destroy (&mutex);
return 0; }
2. Debug crash
GDB binding program running crashes, GDB will stay at the end of the program run stack position, general input BT view stack, frame n toggle stack frame, print print whether null pointer cause crash, where to view the source code location or list list of sources, crashes generally have null pointer, array out of bounds, Illegal memory access
3. Debug deadlock
The program will be deadlocked when the deadlock occurs, if the GDB binding runs using CTRL + C Interrupt program, input info Threads View all threads, use thread n to switch threads, enter BT in the thread stack, locate program location, General comparison of multiple thread locks or whether there is a dead loop
4. Breakpoint Debugging
Set breakpoints, such as B main.cpp:31, execute next step after breakpoint, steps into function, continue continue running