Linux development tool gdb (below), linux development tool gdb

Source: Internet
Author: User

Linux development tool gdb (below), linux development tool gdb
Iii. gdb debugging (below) 01. view runtime data print-view variable value ptype-view type print array-view array print * array @ len-view dynamic memory print x = 5-change runtime data # vi simple. c # include <stdio. h> long fun (int n); int main (int argc, char * argv []) {int I; int x = 5; double z = 2.5; char ch = 'q'; int arr1 [] = {0, 1, 2, 3, 4, 5, 6, 8, 9}; int * arr2 = (int *) malloc (sizeof (int) * 10); for (I = 0; I <10; I ++) {arr2 [I] = I;} printf ("Entering main... \ n "); for (I = 0; I <argc; ++ I) {printf (" % s", Argv [I]);} printf ("\ n"); long result = 0; for (I = 1; I <100; ++ I) {result + = I;} printf ("result [1-100] = % d \ n", result ); printf ("result [1-10] = % d \ n", func (10); printf ("Exiting main... \ n "); retuln 0;} 02. gdb debugging logic error # vi reverse. c # include <stdio. h> int main (void) {int I; char str [6] = "hello"; char reverse_str [6] = ""; printf ("% s \ n ", str); for (I = 0; I <5; I ++) reverse_str [5-i] = str [I]; (where a logic error occurs, change reverse_str [5-i] To reverse_str [4-i]) printf ("% s \ n", reverse_str); return 0 ;}: wq # make #. /reversehello (null, cause of error: After one-step debugging is completed, view the reverse_str result, p reverse_str, and the result is "\ 000 olleh ", when printing, it starts with "\ 0", so it stops. # Make clean # make #./reverse03.gdb debugging error section error is caused by access to an Invalid Address. Access the system data zone, especially write data to the memory address protected by the system. The most common method is to give a pointer A 0 address; the memory is out of bounds (arrays out of bounds, variable types are inconsistent) to access areas not in your memory # vi bugging. c # include <stdio. h> # include <stdlib. h> void segfault () {int * p = NULL; * p = 100;} int main (void) {segfault (); char buf [1] = ""; buf [10003] = "A"; printf ("% c \ n", buf [10003]); return 0 ;}#. /bugging. the c-segment error Starting program:/home/study/cppcourse/devtool/06gdb/buggingProgram contains ed signal SIGSEGV, Segmentation fault. (gdb) bt (stack backtracking) #0 0x12345678 in segfault () Bugging. c: 7 #1 0x12345678 in main () at bugging. c: 12 #2 0x12312312 in _ libc_start_main () from/lib/tls/llibc. when the program crashes, the core file of so.604.core is usually called a core file. The core file records the memory image when the program crashes and adds debugging information. The core file generation process is called the core dump setting to generate the core file ulimit-c. Check the core-dump status ulimit-c number (for example, ulimit-c 1024) ulimit-c unlimited gdb uses the core file to debug the gdb file name core File bt # ulimit-c0 (indicating that the name of the core file cannot be viewed )#. /bugging. c-segment error (core dumped) # ulimit-a core file size (blocks size) 0 # lscore.9351 # gdb bugging core.9351 ...... (Gdb) bt (view stack tracing information) gdb summary viewing runtime data program errors gdb debugging logic errors gdb debugging segment errors core File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.