Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

Similar documents
{C} Tools of the Trade

CS2141 Software Development using C/C++ Debugging

Reviewing gcc, make, gdb, and Linux Editors 1

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Intermediate Programming, Spring 2017*

PRINCIPLES OF OPERATING SYSTEMS

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Exercise Session 6 Computer Architecture and Systems Programming

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

DAY 4. CS3600, Northeastern University. Alan Mislove

Lecture 07 Debugging Programs with GDB

independent compilation and Make

2 Compiling a C program

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Problem Set 1: Unix Commands 1

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CptS 360 (System Programming) Unit 4: Debugging

CSE 333 Lecture 6 - data structures

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Programming in C First meeting

18-600: Recitation #3

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

CS Basics 15) Compiling a C prog.

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

Debugging and Profiling

CMPT 300. Operating Systems. Brief Intro to UNIX and C

CSC 1600 Memory Layout for Unix Processes"

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

15213 Recitation Section C

Programming in C week 1 meeting Tiina Niklander

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

Quiz 0 Review Session. October 13th, 2014

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Question 1. Part (a) CSC 209H1 S 2017

Recitation: Cache Lab & C

Programming in C First meeting Tiina Niklander

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

o Code, executable, and process o Main memory vs. virtual memory

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam 5/10/13

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Programming Tips for CS758/858

CSE 374 Programming Concepts & Tools

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

CSE 374 Final Exam 3/15/17. Name UW ID#

CSE 333 Midterm Exam 7/27/15 Sample Solution

Data and File Structures Laboratory

CSE 374 Final Exam Sample Solution 3/15/12

CSE 333 Midterm Exam Sample Solution 5/10/13

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Dynamic memory. EECS 211 Winter 2019

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

High-performance computing and programming I. Introduction to C programming in *NIX

Debugging and Debugger. Terminology. GNU gcc and gdb. Debugging C programs in Unix and Windows Environments - Part One

Debugging. Erwan Demairy Dream

CSE 12 Spring 2016 Week One, Lecture Two

Buffer Overflow Defenses

Martin Brunner, Jonas Pfoh Sept. 6, Sept. 24, 2010

Advanced Debugging and the Address Sanitizer

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Class Information ANNOUCEMENTS

TI2725-C, C programming lab, course

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CS201: Lab #4 Writing a Dynamic Storage Allocator

Debugging with GDB and DDT

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Lecture 7: file I/O, more Unix

Debugging. John Lockman Texas Advanced Computing Center

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

High Performance Programming Programming in C part 1

System Assertions. Andreas Zeller

Undefined Behaviour in C

CS201 Lecture 2 GDB, The C Library

Programming in C S c o t t S c h r e m m e r

Debugging with gdb and valgrind

UNIX Makefile. C Project Library Distribution and Installation.

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Computer Systems Lecture 9

Using a debugger. Segmentation fault? GDB to the rescue!

ECE 3210 Laboratory 1: Develop an Assembly Program

Buffer overflow prevention, and other attacks

Computer Systems Programming. Practice Midterm. Name:

6.S096: Introduction to C/C++

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

CSE 303 Concepts and Tools for Software Development. Magdalena Balazinska Winter 2010 Lecture 27 Final Exam Revision

DAY 3. CS3600, Northeastern University. Alan Mislove

Systems software design. Software build configurations; Debugging, profiling & Quality Assurance tools

Programming in C and C++

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Making things work as expected

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

ECE551 Midterm Version 2

Unix and C Program Development SEEM

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Transcription:

Embedded Software 3. C tools TI2726 B Koen Langendoen Embedded Software Group

C development cycle 1. [Think] 2. Edit 3. Compile 4. Test 5. Debug 6. Tune UNIX toolbox 2. vi, emacs, gedit 3. gcc, make 4. a.out 5. printf, gdb, valgrind 6. gprof IDE 2. eclipse 3. eclipse 4. eclipse 5. 6. eclipse old school, but common practice 3

3. Compiling and linking x.c gcc c x.o y.c gcc c y.o gcc a.out z.c gcc c z.o lm 6

Automating compilation Dependencies if one file changes many others may need to be recompiled.c depends on.h > recompile executable depends on.o > re link transitive closure Make program to generate/check/activate compilation process driven by Makefile capturing dependencies and actions 7

Makefile Rule looks like target: dependencies action <tab>!! CC = gcc CFLAGS += -Wall -g -std=c99 SOURCES := $(wildcard *.c) OBJECTS := $(SOURCES:.c=.o) TARGET := prog $(TARGET): $(OBJECTS) $(CC) -o $@ $(OBJECTS) clean: rm -f $(TARGET) $(OBJECTS) Example Makefile to compile series of C files into one binary built in rules $make gcc Wall g std=c99 c o a.o a.c gcc Wall g std=c99 c o b.o b.c gcc o prog a.o b.o 8

Beyond make Writing Makefiles is cumbersome and error prone tools to auto generate Makefiles makedepend automake cmake IDE Eclipse + CDT plug in 9

4. Testing Every module and functionality needs to have an (automated) test Regression testing: change > test old functionality Easy for simple functions (unit testing), but what about Input (keyboard) & output (screen)? cross compilation? multi threading? Complicated test harness 10

5. Debugging Typical C bugs memory management buffer overflows / out of bounds dangling pointers memory leaks pointers casting arithmetic exception handling none! segmentation fault 11

Prevention is better than cure Defensive coding use asserts a lot! use const qualifier check return values of all system/library calls char *s = malloc(123); assert(s!= NULL); check sanity of parameters inside functions void copy(int A[], const int B[], int N) { assert( N > 0); for (int i = 0; i < N; i++) {... 12

Printf() 13

Valgrind Programming tool for memory debugging memory leak detection profiling Named after the main entrance to Valhalla in Norse mythology Executes your program in a safe environment valgrind [valgrind options] program [program options] checks every memory reference keeps track of who allocated/freed what memory when 14

Example: concat() #include <stdio.h> #include <string.h> char *concat(char *s, char *t) { return strcpy(s+strlen(s), t); } int main() { char *a = "hello "; char *b = "world"; printf( "%s + %s = %s\n", a, b, concat(a, b)); } return 0; 15

GDB Core files are your best friends state of the program at the time of crash $ limit coredumpsize unlimited $ gdb a.out core Post mortem inspection call stack global data local variables Controlled execution break points step by step 16

GDB Quick reference Command gdb program [core] bt up [n] down [n] l p expr b [file:]function run [arglist] c n s Action debug program [using coredump core] backtrace: display program stack select frame n frames up select frame n frames down list source code of current frame display the value of an expression set breakpoint at function [in file] start your program [with arglist] continue running your program next line, stepping over function calls next line, stepping into function calls 17

DDD = GDB with graphical UI 18

Take 2: concat() char *concat(const char *s, const char *t) { char *result = (char *) malloc(strlen(s) + strlen(t)); strcpy(result, s); strcpy(result+strlen(s), t); return result; } int main() { char *a = "hello "; char *b = "world"; } printf( "%s + %s = %s\n", a, b, concat(a, b)); return 0; 19

Take 3: concat() char *concat(const char *s, const char *t) { char *result = (char *) malloc(strlen(s) + strlen(t) + 1); strcpy(result, s); strcpy(result+strlen(s), t); return result; } int main() { char *a = "hello "; char *b = "world"; } printf( "%s + %s = %s\n", a, b, concat(a, b)); return 0; 20

Take 4: concat() char *concat(const char *s, const char *t) { char *result = (char *) malloc(strlen(s) + strlen(t) + 1); strcpy(result, s); strcpy(result+strlen(s), t); return result; } int main() { char *a = "hello "; char *b = "world"; char *c = concat(a,b); printf( "%s + %s = %s\n", a, b, c); free( c); return 0; } 21

Take that: concat() char *concat(const char *s, const char *t) { size_t ns = strlen(s) + 1; size_t nt = strlen(t) + 1; char *result = (char *) malloc( ns+nt-1); strncpy(result, s, ns); strncpy(result+ns-1, t, nt); safe guard against buffer overflow (attacks) } return result; int main() { char *a = "hello "; char *b = "world"; char *c = concat(a,b); 22

6. Performance tuning What resources are consumed? Code $ size concat7 text data bss dec hex filename 1770 592 8 2370 942 concat7 Memory top valgrind/massif heap profile over time CPU gprof breakdown per function 24

Before you go. Hints for successful programming (in C) Haste makes waste copy & paste considered harmful code no special casing follow the rule, not the exception refactoring Seeing is believing test everything (unit testing) and always (regression testing) [C] assert( the obvious ) [C] check return values of (system) calls The right tool for the job is worth its weight in gold [C] valgrind Two heads are better than one the visitor effect sharing ideas is great, but sharing code is plagiarism 25