CS246 Unix: More Shell Scripting C:Queues April 8 Apropos

Total Page:16

File Type:pdf, Size:1020Kb

CS246 Unix: More Shell Scripting C:Queues April 8 Apropos CS246 Unix: more shell scripting C:queues April 8 apropos • get a listing of all commands that have the word in a short version of their man page • UNIX> apropos file • not working on my mac • UNIX> apropos file | wc returns 1198 possible items • Contrast with “which” gtowell@benz:~$ apropos gzip gzip (1) - compress or expand files lz (1) - gunzips and shows a listing of a gzip'd tar'd archive tgz (1) - makes a gzip'd tar archive uz (1) - gunzips and extracts a gzip'd tar'd archive zforce (1) - force a '.gz' extension on all gzip files 2 Shell script loops • while loop looks a lot like if execpt 1.#!/bin/bash 2.# Basic while loop • if then if • while do done 4.counter=1 5.while [ $counter -le 10 ] 6.do 7.echo $counter 8.((counter++)) 9.done 11.echo All done 12. 3 loops in shell scripts • $(( )) in shell script mean — roughly “I am doing #!/bin/bash math inside here” counter=1 • hence “counter=$(($counter+1))” does what you think while [ $counter -le 10 ] • unlike [] not space sensitive do echo $counter • ${} notation for vars is never wrong to use counter=$(($counter+1)) • counter=$((${counter}+1)) ((counter++)) # shortcut form • Sometimes there are shortcuts done • ((counter++)) echo All done • note no leading $ • putting $ would indicate that the math returned a value • I tend away from shortcuts because I never remember them 4 shell loops #!/bin/bash • Several forms of for # Basic for loop loops exist. names='Stan Kyle Cartman' • This one takes # names=`ls` string, splits it and for name in $names works with each part do echo $name • The string to be split done can come from echo All done anywhere • for instance, from executing ls 5 more shell loops • note (( )) to #!/bin/bash #!/bin/bash # Basic range in for loop # Basic C-like for loop invoke math- for (( i=1; i<=5; i++ )) like for value in {1..5} do do operations, echo $value echo $i done done • again, the echo All done shortcut echo All done form 6 file l5.sh Space used by files FILES=`ls *.${1}` with an extension TOT=0 for FILE in $FILES • usage: UNIX> l5.sh sh do DET=`ls -l $FILE` • This gives the number of bytes CNT=0 used by all files with .sh extension for DETP in $DET in the current directory do #echo "$CNT $DETP" if [ $CNT -eq 4 ] • The FILES var is actually then unnecessary TOT=$(( $TOT + $DETP )) • kind of wasteful also #echo $DETP • change fi for FILE in $FILES ((CNT++)) to done for FILE in *.$1 done echo $TOT 7 the publish problem Solution #!/bin/bash param="java" bash has some built in text • make a link to the file manipulation. This uses it if [ "$1" != "" ]; then ${VAR/replace/replacement} param=$1 using ln -s where the fi link has a browser- for f in *.$param; known extension do Suppose you did not know of echo ${f/./} bash text manipulation • e.g. “.txt” ln -s $f ${f/./}.txt • Allows updating aa=`echo $f | tr -d .` done without having to recopy a lot or • Create a shell script to aa=`echo $f | tr . d` do this in bulk. ln -s $f ${a}.txt 8 LAB • write a shell script that lists all files in the current directory and all direct subdirectories. • This should not be a recursive listing of all subdirectories. Just go one down 9 char* removeTail(DLL *dll) { if (dll->count<=0) Queues return NULL; dll->count--; DLLItem *itm = dll->tail; • Use the DLL struct DLLItem *tprev = itm->prev; • needs more if (tprev==NULL) { • removeTail dll->head = NULL; dll->tail = NULL; return; • Revisit DLLItem } constructor/destructor dll->tail = tprev; and eliminate the copy tprev->next = NULL; into new memory. Just char *rtn = malloc((strlen(itm->payload) + 1) * sizeof(char)); take the thing supplied strcpy(rtn, itm->payload); freeDLLItem(itm); • Otherwise need to take } care to free the returned thing!! required because the info would be lost otherwise, but!!!! 10 typedef struct { Q Basics DLL *internal; } Queue; • Constructor, Queue* makeQueue() { destructor, and Queue *rtn = malloc(1 * sizeof(Queue)); struct are pretty rtn->internal = makeDLL(); minimal return rtn; } void freeQueue(Queue* q) { freeDLL(q->internal); free(q); } 11 Q more void add2Queue(Queue* q, char* item) { • rest is pretty basic addDLLHead(q->internal, item); also } char* pullFromQueue(Queue* q) { • Essentially all work return removeTail(q->internal); done by DLL! } 12 Splitting & Making CFLAGS = -g -O2 • I made a single dll.c and dll: dll.c dll.h dll.h gcc $(CFLAGS) -o dll dll.c • IMHO DLLItem is more queue: dll.o queue.c a private inner class gcc $(CFLAGS) -o queue dll.o queue.c and so it does not get its own file(s) dll.o: dll.c dll.h gcc $(CFLAGS) -c -D DOTO=1 dll.c • but this is a style choice clean: • Also a .c and .h for rm *.o dll queue queue 13 file a.h file b.h typedef struct { #include "a.h" Splitting Issues 1 int v1; typedef struct { } A; A oneA; } B; • C does not allow typedefs to be declared multiple times • explicitly forbidden in C11 • Consider three files at right file b.c • when compile get error • a.h:3:3: note: previous declaration of #include "a.h" ‘A’ was here #include "b.h" 3 | } A; B b; • Solution 1 … instruct users to avoid the issue … A a; • do not actually need the a.h include since it comes int main(int argc, char const *argv[]) { in with b.h return 0; • lousy solution since it relies on everyone knowing and REMEMBERING } • Also, a.h may come in via some other .h file so even if you assume people remember a conflict can occur 14 file a.h Splitting issues 1 #ifndef TYPE_A_DEFINED typedef struct { continued int v1; } A; • Better solution is to ensure #define TYPE_A_DEFINED 1 that definitions can only occur #endif once!! • use #ifndef … #endif file b.h #include "a.h" #ifndef TYPE_B_DEFINED typedef struct { A oneA; } B; #define TYPE_B_DEFINED 1 #endif 15 file s1.h file s1.c int doS1(); #include "s1.h" Splitting Issues 2 int doS1() { return 42; } • Problem • this all looks correct … and it is file s2.c • but gcc fails #include "s1.h" [gtowell@powerpuff L14]$ gcc s2.c #include <stdio.h> /bin/ld: /tmp/cc8w1v2M.o: in function `main': s2.c:(.text+0x15): undefined reference to `doS1' int main(int argc, char const *argv[]) collect2: error: ld returned 1 exit status { printf("%d\n", doS1()); • Why? return 0; } • Linking issues!!! 16 Splitting Issues 2 Continued • Recall that gcc actually takes 3 steps • 1. Preprocess • 2. Compile • 3. Link • In this case preprocess and compile both work fine. • Those steps take the function prototypes as promises that the definition will appear later • later MUST be in the link. • in this case there is no body supplied for Use a makefile!!! doS1() [gtowell@powerpuff L14]$ gcc -c s1.c [gtowell@powerpuff L14]$ gcc -o s2 s2.c s1.o 17 file m1.h file m2.h Splitting issue 3 int dom1(); int doS1(); • I want each of my .c files to file m1.c file m2.c have a main function for int dom1() { #include "m1.h" testing purposes. But if I do return 42; that, the compiler (actually } int dom2() { the linker) complains return 84; int main(int argc, } [gtowell@powerpuff L14]$ gcc m1.c char const [gtowell@powerpuff L14]$ ./a.out *argv[]) int main(int argc, M1 42 char const *argv[]) [gtowell@powerpuff L14]$ gcc -c m1.c { [gtowell@powerpuff L14]$ gcc m2.c m1.o printf("M1 { /bin/ld: m1.o: in function `main': %d\n", dom1()); printf("M2 %d m1.c:(.text+0xb): multiple definition of `main'; /tmp/ return 0; %d", dom1(), cctzIOI8.o:m2.c:(.text+0xb): first defined here } dom2()); collect2: error: ld returned 1 exit status return 0; } 18 Splitting issue 3 continued file m1.c • The problem is that there are two implementations of main and they conflict. int dom1() { • Once solution would be to put the main functions into return 42; their own file and compile/link appropriately } • This gets cumbersome (lots of files) and it looses the #ifndef M1C_MAIN clear linkage between the main and the functions being int main(int argc, tested. char const • Better is to wrap main #ifndef *argv[]) { • then when compiling with -c flag a -D to define printf("M1 M1C_MAIN %d\n", dom1()); return 0; [gtowell@powerpuff L14]$ gcc -c -DM1C_MAIN m1.c } [gtowell@powerpuff L14]$ gcc m2.c m1.o #endif [gtowell@powerpuff L14]$ a.out 19 Review • Pointers, pointers and more pointers • recursion and tail recursion • why tail recursion matters • typedefs • structs • why not pass by value • constructors and destructors • malloc and free • UNIX • putting it all together with scripts 20 STOP HERE 21 Garbage Collection in Java • Why doesn’t java need free? 22 GDB • “Gnu DeBugger” • Allows you to inspect program while running • breakpoints • conditional breakpoints • Another way to attack segmentation faults • arguably better • Debuggers arguably give a lot more flexibility than print statements 23 A Program 1 #include <stdio.h> 2 #include <stdlib.h> that breaks 3 4 void smashing() { • gdb loves line numbers 5 int aa[10]; 6 for (int i = 0; i < 20; i++) { • cat -n xxx.c 7 aa[i] = i; 8 } 9 } 10 int main(int argc, char const *argv[]) • Program has three 11 { issues 12 int strt = atoi(argv[1]); 13 int aa[strt]; 14 smashing(); 15 for (int i = 0; i < 1000; i++) 16 { 17 printf("%d %d\n", i, aa[i]); 18 } 19 return 0; 20 } 24 [gtowell@powerpuff L14]$ gcc -g broken.c [gtowell@powerpuff L14]$ gdb a.out gbd usage GNU gdb (GDB) 9.1 Copyright (C) 2020 Free Software Foundation, Inc.
Recommended publications
  • Red Hat Enterprise Linux 6 Developer Guide
    Red Hat Enterprise Linux 6 Developer Guide An introduction to application development tools in Red Hat Enterprise Linux 6 Dave Brolley William Cohen Roland Grunberg Aldy Hernandez Karsten Hopp Jakub Jelinek Developer Guide Jeff Johnston Benjamin Kosnik Aleksander Kurtakov Chris Moller Phil Muldoon Andrew Overholt Charley Wang Kent Sebastian Red Hat Enterprise Linux 6 Developer Guide An introduction to application development tools in Red Hat Enterprise Linux 6 Edition 0 Author Dave Brolley [email protected] Author William Cohen [email protected] Author Roland Grunberg [email protected] Author Aldy Hernandez [email protected] Author Karsten Hopp [email protected] Author Jakub Jelinek [email protected] Author Jeff Johnston [email protected] Author Benjamin Kosnik [email protected] Author Aleksander Kurtakov [email protected] Author Chris Moller [email protected] Author Phil Muldoon [email protected] Author Andrew Overholt [email protected] Author Charley Wang [email protected] Author Kent Sebastian [email protected] Editor Don Domingo [email protected] Editor Jacquelynn East [email protected] Copyright © 2010 Red Hat, Inc. and others. The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version. Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
    [Show full text]
  • UNIX Workshop Series: Quick-Start Objectives
    Part I UNIX Workshop Series: Quick-Start Objectives Overview – Connecting with ssh Command Window Anatomy Command Structure Command Examples Getting Help Files and Directories Wildcards, Redirection and Pipe Create and edit files Overview Connecting with ssh Open a Terminal program Mac: Applications > Utilities > Terminal ssh –Y [email protected] Linux: In local shell ssh –Y [email protected] Windows: Start Xming and PuTTY Create a saved session for the remote host name centos.css.udel.edu using username Connecting with ssh First time you connect Unix Basics Multi-user Case-sensitive Bash shell, command-line Commands Command Window Anatomy Title bar Click in the title bar to bring the window to the front and make it active. Command Window Anatomy Login banner Appears as the first line of a login shell. Command Window Anatomy Prompts Appears at the beginning of a line and usually ends in $. Command Window Anatomy Command input Place to type commands, which may have options and/or arguments. Command Window Anatomy Command output Place for command response, which may be many lines long. Command Window Anatomy Input cursor Typed text will appear at the cursor location. Command Window Anatomy Scroll Bar Will appear as needed when there are more lines than fit in the window. Command Window Anatomy Resize Handle Use the mouse to change the window size from the default 80x24. Command Structure command [arguments] Commands are made up of the actual command and its arguments. command -options [arguments] The arguments are further broken down into the command options which are single letters prefixed by a “-” and other arguments that identify data for the command.
    [Show full text]
  • CIS 90 - Lesson 2
    CIS 90 - Lesson 2 Lesson Module Status • Slides - draft • Properties - done • Flash cards - NA • First minute quiz - done • Web calendar summary - done • Web book pages - gillay done • Commands - done • Lab tested – done • Print latest class roster - na • Opus accounts created for students submitting Lab 1 - • CCC Confer room whiteboard – done • Check that headset is charged - done • Backup headset charged - done • Backup slides, CCC info, handouts on flash drive - done 1 CIS 90 - Lesson 2 [ ] Has the phone bridge been added? [ ] Is recording on? [ ] Does the phone bridge have the mike? [ ] Share slides, putty, VB, eko and Chrome [ ] Disable spelling on PowerPoint 2 CIS 90 - Lesson 2 Instructor: Rich Simms Dial-in: 888-450-4821 Passcode: 761867 Emanuel Tanner Merrick Quinton Christopher Zachary Bobby Craig Jeff Yu-Chen Greg L Tommy Eric Dan M Geoffrey Marisol Jason P David Josh ? ? ? ? Leobardo Gabriel Jesse Tajvia Daniel W Jason W Terry? James? Glenn? Aroshani? ? ? ? ? ? ? = need to add (with add code) to enroll in Ken? Luis? Arturo? Greg M? Ian? this course Email me ([email protected]) a relatively current photo of your face for 3 points extra credit CIS 90 - Lesson 2 First Minute Quiz Please close your books, notes, lesson materials, forum and answer these questions in the order shown: 1. What command shows the other users logged in to the computer? 2. What is the lowest level, inner-most component of a UNIX/Linux Operating System called? 3. What part of UNIX/Linux is both a user interface and a programming language? email answers to: [email protected] 4 CIS 90 - Lesson 2 Commands Objectives Agenda • Understand how the UNIX login • Quiz operation works.
    [Show full text]
  • Winappdbg Documentation Release 1.6
    WinAppDbg Documentation Release 1.6 Mario Vilas Aug 28, 2019 Contents 1 Introduction 1 2 Programming Guide 3 i ii CHAPTER 1 Introduction The WinAppDbg python module allows developers to quickly code instrumentation scripts in Python under a Win- dows environment. It uses ctypes to wrap many Win32 API calls related to debugging, and provides a powerful abstraction layer to manipulate threads, libraries and processes, attach your script as a debugger, trace execution, hook API calls, handle events in your debugee and set breakpoints of different kinds (code, hardware and memory). Additionally it has no native code at all, making it easier to maintain or modify than other debuggers on Windows. The intended audience are QA engineers and software security auditors wishing to test or fuzz Windows applications with quickly coded Python scripts. Several ready to use tools are shipped and can be used for this purposes. Current features also include disassembling x86/x64 native code, debugging multiple processes simultaneously and produce a detailed log of application crashes, useful for fuzzing and automated testing. Here is a list of software projects that use WinAppDbg in alphabetical order: • Heappie! is a heap analysis tool geared towards exploit writing. It allows you to visualize the heap layout during the heap spray or heap massaging stage in your exploits. The original version uses vtrace but here’s a patch to use WinAppDbg instead. The patch also adds 64 bit support. • PyPeElf is an open source GUI executable file analyzer for Windows and Linux released under the BSD license. • python-haystack is a heap analysis framework, focused on classic C structure matching.
    [Show full text]
  • Using the GNU Debugger (Gdb) a Debugger Is a Very Useful Tool for Finding Bugs in a Program
    Using the GNU debugger (gdb) A debugger is a very useful tool for finding bugs in a program. You can interact with a program while it is running, start and stop it whenever, inspect the current values of variables and modify them, etc. If your program runs and crashes, it will produce a ‘core dump’. You can also use a debugger to look at the core dump and give you extra information about where the crash happened and what triggered it. Some debuggers (including recent versions of gdb) can also go backwards through your code: you run your code forwards in time to the point of an error, and then go backwards looking at the values of the key variables until you get to the start of the error. This can be slow but useful sometimes! To use a debugger effectively, you need to get the compiler to put extra ‘symbol’ information into the binary, otherwise all it will contain is machine code level – it is much more useful to have the actual variable names you used. To do this, you use: gfortran –g –O0 mycode.f90 –o mybinary where ‘-g’ is the compiler option to include extra symbols, -O0 is no optimization so the code is compiled exactly as written, and the output binary is called ’mybinary’. If the source files and executable file is in the same directory, then you can run the binary through the debugger by simply doing: gdb ./mybinary This will then put you into an interactive debugging session. Most commands can be shortened (eg ‘b’ instead of ‘break’) and pressing ‘enter’ will repeat the last command.
    [Show full text]
  • Bringing GNU Emacs to Native Code
    Bringing GNU Emacs to Native Code Andrea Corallo Luca Nassi Nicola Manca [email protected] [email protected] [email protected] CNR-SPIN Genoa, Italy ABSTRACT such a long-standing project. Although this makes it didactic, some Emacs Lisp (Elisp) is the Lisp dialect used by the Emacs text editor limitations prevent the current implementation of Emacs Lisp to family. GNU Emacs can currently execute Elisp code either inter- be appealing for broader use. In this context, performance issues preted or byte-interpreted after it has been compiled to byte-code. represent the main bottleneck, which can be broken down in three In this work we discuss the implementation of an optimizing com- main sub-problems: piler approach for Elisp targeting native code. The native compiler • lack of true multi-threading support, employs the byte-compiler’s internal representation as input and • garbage collection speed, exploits libgccjit to achieve code generation using the GNU Com- • code execution speed. piler Collection (GCC) infrastructure. Generated executables are From now on we will focus on the last of these issues, which con- stored as binary files and can be loaded and unloaded dynamically. stitutes the topic of this work. Most of the functionality of the compiler is written in Elisp itself, The current implementation traditionally approaches the prob- including several optimization passes, paired with a C back-end lem of code execution speed in two ways: to interface with the GNU Emacs core and libgccjit. Though still a work in progress, our implementation is able to bootstrap a func- • Implementing a large number of performance-sensitive prim- tional Emacs and compile all lexically scoped Elisp files, including itive functions (also known as subr) in C.
    [Show full text]
  • A.5.1. Linux Programming and the GNU Toolchain
    Making the Transition to Linux A Guide to the Linux Command Line Interface for Students Joshua Glatt Making the Transition to Linux: A Guide to the Linux Command Line Interface for Students Joshua Glatt Copyright © 2008 Joshua Glatt Revision History Revision 1.31 14 Sept 2008 jg Various small but useful changes, preparing to revise section on vi Revision 1.30 10 Sept 2008 jg Revised further reading and suggestions, other revisions Revision 1.20 27 Aug 2008 jg Revised first chapter, other revisions Revision 1.10 20 Aug 2008 jg First major revision Revision 1.00 11 Aug 2008 jg First official release (w00t) Revision 0.95 06 Aug 2008 jg Second beta release Revision 0.90 01 Aug 2008 jg First beta release License This document is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License [http:// creativecommons.org/licenses/by-nc-sa/3.0/us/]. Legal Notice This document is distributed in the hope that it will be useful, but it is provided “as is” without express or implied warranty of any kind; without even the implied warranties of merchantability or fitness for a particular purpose. Although the author makes every effort to make this document as complete and as accurate as possible, the author assumes no responsibility for errors or omissions, nor does the author assume any liability whatsoever for incidental or consequential damages in connection with or arising out of the use of the information contained in this document. The author provides links to external websites for informational purposes only and is not responsible for the content of those websites.
    [Show full text]
  • The Glib/GTK+ Development Platform
    The GLib/GTK+ Development Platform A Getting Started Guide Version 0.8 Sébastien Wilmet March 29, 2019 Contents 1 Introduction 3 1.1 License . 3 1.2 Financial Support . 3 1.3 Todo List for this Book and a Quick 2019 Update . 4 1.4 What is GLib and GTK+? . 4 1.5 The GNOME Desktop . 5 1.6 Prerequisites . 6 1.7 Why and When Using the C Language? . 7 1.7.1 Separate the Backend from the Frontend . 7 1.7.2 Other Aspects to Keep in Mind . 8 1.8 Learning Path . 9 1.9 The Development Environment . 10 1.10 Acknowledgments . 10 I GLib, the Core Library 11 2 GLib, the Core Library 12 2.1 Basics . 13 2.1.1 Type Definitions . 13 2.1.2 Frequently Used Macros . 13 2.1.3 Debugging Macros . 14 2.1.4 Memory . 16 2.1.5 String Handling . 18 2.2 Data Structures . 20 2.2.1 Lists . 20 2.2.2 Trees . 24 2.2.3 Hash Tables . 29 2.3 The Main Event Loop . 31 2.4 Other Features . 33 II Object-Oriented Programming in C 35 3 Semi-Object-Oriented Programming in C 37 3.1 Header Example . 37 3.1.1 Project Namespace . 37 3.1.2 Class Namespace . 39 3.1.3 Lowercase, Uppercase or CamelCase? . 39 3.1.4 Include Guard . 39 3.1.5 C++ Support . 39 1 3.1.6 #include . 39 3.1.7 Type Definition . 40 3.1.8 Object Constructor . 40 3.1.9 Object Destructor .
    [Show full text]
  • Mandoc: Becoming the Main BSD Manual Toolbox
    mandoc: becoming the main BSD manual toolbox BSDCan 2015, June 13, Ottawa Ingo Schwarze <[email protected]> Cynthia Livingston’sOTTB “Bedifferent” (c) 2013 C. Livingston (with permission) > Ingo Schwarze: mandoc page 2: INTROI BSDCan 2015, June 13, Ottawa Brief history of UNIX documentation • The key point: All documentation in one place and one format. Easy to find, uniform and easy to read and write. Be correct, complete, concise. • 1964: RUNOFF/roffmarkup syntax by Jerome H. Saltzer,MIT. Unobtrusive,diff(1)-friendly,easy to hand-edit, simple tools, high quality output. • 1971: Basic manual structure by Ken Thompson and Dennis Ritchie for the AT&T Version 1 UNIX manuals, Bell Labs. • 1979: man(7) physical markup language for AT&T Version 7 UNIX. • 1989: mdoc(7) semantic markup by Cynthia Livingston for 4.3BSD-Reno. Powerful, self-contained, portable. • 1989: GNU troffbyJames Clarke. • 2001: mdoc(7) rewrite by Werner Lemberg and Ruslan Ermilovfor groff-1.17. • 2008: mandoc(1) started by Kristaps Dzonsons. • 2010: mandoc(1) is the only documentation formatter in the OpenBSD base system. • 2014: mandoc(1) used by default in OpenBSD, FreeBSD, NetBSD, illumos. 16:19:30 What is the mandoc toolbox? → < > Ingo Schwarze: mandoc page 3: INTROIIBSDCan 2015, June 13, Ottawa What is the mandoc toolbox? User perspective:man(1), the manual viewer One comprehensive tool! Normal operation always proceeds in three steps: 1. Find one or more manuals in the file system or using a database by manual name — man(1) — or by search query — apropos(1) =man -k The result of this step can be printed out with man -w.
    [Show full text]
  • GDB Tutorial a Walkthrough with Examples
    GDB Tutorial A Walkthrough with Examples CMSC 212 - Spring 2009 Last modified March 22, 2009 GDB Tutorial What is gdb? “GNU Debugger” A debugger for several languages, including C and C++ It allows you to inspect what the program is doing at a certain point during execution. Errors like segmentation faults may be easier to find with the help of gdb. http://sourceware.org/gdb/current/onlinedocs/gdb toc.html - online manual GDB Tutorial Additional step when compiling program Normally, you would compile a program like: gcc [flags] <source files> -o <output file> For example: gcc -Wall -Werror -ansi -pedantic-errors prog1.c -o prog1.x Now you add a -g option to enable built-in debugging support (which gdb needs): gcc [other flags] -g <source files> -o <output file> For example: gcc -Wall -Werror -ansi -pedantic-errors -g prog1.c -o prog1.x GDB Tutorial Starting up gdb Just try “gdb” or “gdb prog1.x.” You’ll get a prompt that looks like this: (gdb) If you didn’t specify a program to debug, you’ll have to load it in now: (gdb) file prog1.x Here, prog1.x is the program you want to load, and “file” is the command to load it. GDB Tutorial Before we go any further gdb has an interactive shell, much like the one you use as soon as you log into the linux grace machines. It can recall history with the arrow keys, auto-complete words (most of the time) with the TAB key, and has other nice features. Tip If you’re ever confused about a command or just want more information, use the “help” command, with or without an argument: (gdb) help [command] You should get a nice description and maybe some more useful tidbits.
    [Show full text]
  • R for Beginners
    R for Beginners Emmanuel Paradis Institut des Sciences de l'Evolution´ Universit´e Montpellier II F-34095 Montpellier c´edex 05 France E-mail: [email protected] I thank Julien Claude, Christophe Declercq, Elo´ die Gazave, Friedrich Leisch, Louis Luangkesron, Fran¸cois Pinard, and Mathieu Ros for their comments and suggestions on earlier versions of this document. I am also grateful to all the members of the R Development Core Team for their considerable efforts in developing R and animating the discussion list `rhelp'. Thanks also to the R users whose questions or comments helped me to write \R for Beginners". Special thanks to Jorge Ahumada for the Spanish translation. c 2002, 2005, Emmanuel Paradis (12th September 2005) Permission is granted to make and distribute copies, either in part or in full and in any language, of this document on any support provided the above copyright notice is included in all copies. Permission is granted to translate this document, either in part or in full, in any language provided the above copyright notice is included. Contents 1 Preamble 1 2 A few concepts before starting 3 2.1 How R works . 3 2.2 Creating, listing and deleting the objects in memory . 5 2.3 The on-line help . 7 3 Data with R 9 3.1 Objects . 9 3.2 Reading data in a file . 11 3.3 Saving data . 14 3.4 Generating data . 15 3.4.1 Regular sequences . 15 3.4.2 Random sequences . 17 3.5 Manipulating objects . 18 3.5.1 Creating objects .
    [Show full text]
  • Introduction to GDB the GNU Debugger, Usually Called Just GDB and Named Gdb As an Executable File, Is the Standard Debugger for the GNU Software System
    Introduction to GDB The GNU Debugger, usually called just GDB and named gdb as an executable file, is the standard debugger for the GNU software system. It is a portable debugger that works for many programming languages, including Ada, C, C++, Objective-C, and Pascal. It allows you to see what is going on `inside' another program while it executes -- or what another program was doing at the moment it crashed. GNU Debugger helps you in finding out followings: 1) If a core dump happened then what statement or expression did the program crash on? 2) If an error occurs while executing a function, what line of the program contains the call to that function, and what are the parameters? 3) What are the values of program variables at a particular point during execution of the program? 4) What is the result of a particular expression in a program? How GDB Debugs? GDB allows you to do things like run the program up to a certain point then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line. GDB uses a simple command line interface. How it works? To let GDB be able to read all that information line by line from the symbol table, we need to compile it a bit differently. Normally we compile things as: gcc hello.c Instead of doing this, we need to compile with the -g flag as such: gcc -g hello.c It will Compile hello.c with the debugging option (-g).
    [Show full text]