I117 II I117 PROGRAMMING PRACTICE II 2 SOFTWARE DEVELOPMENT ENV. 2 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu

Similar documents
I117 II I117 PROGRAMMING PRACTICE II SOFTWARE DEVELOPMENT ENV. 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

I117 II I117 PROGRAMMING PRACTICE II DEBUG Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

I117 II I117 PROGRAMMING PRACTICE II HASH Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

r07.dvi

ohp07.dvi

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental data structures /06/14(Tue) / Memory Management

I117 II I117 PROGRAMMING PRACTICE II SCRIPT LANGUAGE 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

Introduction Purpose This training course demonstrates the use of the High-performance Embedded Workshop (HEW), a key tool for developing software for

ohp03.dvi

joho07-1.ppt

r08.dvi

ohp08.dvi

r03.dvi

untitled

untitled

XMPによる並列化実装2

,,,,., C Java,,.,,.,., ,,.,, i

tuat1.dvi

2 2 3 dbxtool dbx (GUI) dbxtool dbx C++ Oracle Solaris Studio 12.2 examples/debugger/debug_tutorial 1. cp -r /opt/solstudio12.2/examples/de

Introduction Purpose This training module provides an overview of debugging features in H8S Simulator. Objectives Understand how to use the Simulator.

28 Docker Design and Implementation of Program Evaluation System Using Docker Virtualized Environment

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

超初心者用

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose



I117 プログラミング演習II

橡Pro PDF

ex01.dvi

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

第5回お試しアカウント付き並列プログラミング講習会

C言語によるアルゴリズムとデータ構造

Copyright Oracle Parkway, Redwood City, CA U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated softw

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

untitled

¥×¥í¥°¥é¥ß¥ó¥°±é½¬I Exercise on Programming I [1zh] ` `%%%`#`&12_`__~~~ alse

yacc.dvi

C B

WinHPC ppt

main

pptx

lexex.dvi

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

Introduction Purpose The course describes library configuration and usage in the High Performance Embedded Workshop (HEW), which speeds development of

IPSJ SIG Technical Report Vol.2014-EIP-63 No /2/21 1,a) Wi-Fi Probe Request MAC MAC Probe Request MAC A dynamic ads control based on tra

programmingII2019-v01

cpp1.dvi

program.dvi

国際恋愛で避けるべき7つの失敗と解決策

Agenda Intro & history LLVM overview Demo Pros & Cons LLVM Intermediate Language LLVM tools


2) TA Hercules CAA 5 [6], [7] CAA BOSS [8] 2. C II C. ( 1 ) C. ( 2 ). ( 3 ) 100. ( 4 ) () HTML NFS Hercules ( )

Microsoft Word - C.....u.K...doc

fx-9860G Manager PLUS_J

1.ppt

AtCoder Regular Contest 073 Editorial Kohei Morita(yosupo) A: Shiritori if python3 a, b, c = input().split() if a[len(a)-1] == b[0] and b[len(

Informatics 2014

main.dvi

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

I 2 tutimura/ I 2 p.1/??

Introduction Purpose This training course describes the configuration and session features of the High-performance Embedded Workshop (HEW), a key tool

slide5.pptx

comment.dvi

Ver.1 1/17/2003 2

(2 Linux Mozilla [ ] [ ] [ ] [ ] URL 2 qkc, nkc ~/.cshrc (emacs 2 set path=($path /usr/meiji/pub/linux/bin tcsh b

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

ex01.dvi

25 II :30 16:00 (1),. Do not open this problem booklet until the start of the examination is announced. (2) 3.. Answer the following 3 proble

4.1 % 7.5 %

joho09.ppt

¥Ñ¥Ã¥±¡¼¥¸ Rhpc ¤Î¾õ¶·

1) OOP 2) ( ) 3.2) printf Number3-2.cpp #include <stdio.h> class Number Number(); // ~Number(); // void setnumber(float n); float getnumber();

Linuxデバイスドライバ.PDF

Intel® Compilers Professional Editions

ex12.dvi

1.3 ( ) ( ) C

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

2008 IIA (program) pro(before)+gram(write) (artificial language) (programming languege) (programming) (machine language) (assembly language) ( )


I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

ex14.dvi

OpenCV IS Report No Report Medical Information System Labratry

2

LAN Copyright c Daikoku Manabu This tutorial is licensed under a Creative Commons Attribution 2.1 Japan License

LiveCode初心者開発入門サンプル


C

Complex Lab – Operating Systems - Graphical Console

1 Code Generation Part I Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

AN 100: ISPを使用するためのガイドライン

Informatics 2015

L3 Japanese (90570) 2008

( CUDA CUDA CUDA CUDA ( NVIDIA CUDA I

Transcription:

I117 II I117 PROGRAMMING PRACTICE II 2 SOFTWARE DEVELOPMENT ENV. 2 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu@jaist.ac.jp

/ SCHEDULE 1. 2011/06/07(Tue) / Basic of Programming 2. 2011/06/09(Thu) / Fundamental data structures 3. 2011/06/14(Tue) / Memory Management 1 4. 2011/06/16(Thu) / Memory Management 2 5. 2011/06/21(Tue) / Debugging 6. 2011/06/23(Thu) / Software Development Env. 1 7. 2011/06/28(Tue) / Software Development Env. 2 8. 2011/06/30(Thu) / Data Structure : Tree 9. 2011/07/05(Tue) / Data Structure: Hash 10. 2011/07/07(Thu) / Understanding Programs 1 11. 2011/07/12(Tue) / Understanding Programs 2 12. 2011/07/14(Thu) / Script Language 1 13. 2011/07/19(Tue) / Script Language 2 14. 2011/07/21(Thu) / Other Languages 15. 2011/07/26(Tue) / Examination

/ FINAL REPORT free topic : decide topic by 7/5(Tue) report us of the topic by e-mail: i117report@jaist.ac.jp / Sound file converter / web crawler (CalDAV) calendar program new shell, editor, window manager rsync encrypt/decrypt, rsync markov-chain program choose good theme TAask TA and me The final report must have deep consideration

/ TODAY'S INDEX / debugging / debugger / memory checker

/ DEBUGGER

DBX / DEBUG USING DBX dbx / what is dbx Unix / source level debugger Sun / attached in Sun compiler C, C++, Java, Fortran 77, Fortran 99 / what can be done / step runs for each line / trace of function call / set a break point / display and set a variable / display a register / display contents of memory etc...

DBX -g / with g option, debugging info is attached in the executable file / run the debugger with the executable jjj $ cc g sample.c $ dbx./a.out a.out ld.so.1 libc.so.1 / display help (dbx) help... (dbx) help stop... # # stop

/ EXAMPLE fib.c and fibmain.c fib.c 1 int fibonacci(int n) 2 { 3 int num1 = 0, num2 = 1; 4 int tmp, i; 5 6 for (i = 0; i < n; i++) 7 { 8 tmp = num1; 9 num1 = num2; 10 num2 += tmp; 11 } 12 13 return num1; 14 } fibmain.c 1 #include <stdio.h> 2 3 extern int fibonacci(int n); 4 5 void main(void) 6 { 7 int f10 = fibonacci(10); 8 printf( F(10) = %d\n, 9 f10); 10 }

NUM1, NUM2 / DISPLAY VARIABLE dbx / compile, start dbx $ cc g -c fib.c $ cc g fib.o fibmain.c $ dbx a.out fib.c9 set a break point (dbx) stop at fib.c:9 (2) stop at fib.c :9 / run the program on the debugger (dbx) run... stopped in fibonacci : 9 fib.c 9 num1 = num2; num1, num2 / print variables (dbx) print num1, num2 num1 = 0 num2 = 1

/ STEP RUNS AND ASSIGNMENT 1 execute for a single line (dbx) next stoopped in fibonacci : 6 fib.c 6 for (i = 0; i < n; i++) (dbx) print num1, num2 num1 = 1 num2 = 1 1next step / what's the difference in "next" and "step"? num1 0 / assign a value (dbx) assign num1 = 0 (dbx) print num1 num1 = 0

/ PRINT CALL STACK AND CHANGE FRAME / display call stack and print a register called "program counter" (dbx) where =>[1] fibonacci(n = 10), 6 fib.c [2] main(), 7 fibmain.c (dbx) print fx $pc $pc = 0x10c84 main / go back to main() (dbx) frame 2 :main 7 int f10 = fibonacci(10) (dbx) where [1] fibonacci(n = 10), 6 fib.c =>[2] main(), 7 fibmain.c (dbx) print fx $pc $pc = 0x10cd8

STACK / HOW STACK WORKS PUSH POP SPARC : SP x86 : SP(16bit)/ESP(32bit)/RSP(64bit) SPARC: FP x86 : BP(16bit)/EBP(32bit)/RBP(64bit) high-address

/ CONTINUE AND DELETE BREAKPOINTS / continue the run (dbx) cont stoopped in fibonacci : 9 fib.c 9 num1 = num2; / delete the break points and continue (dbx) delete all (dbx) cont F(10) = 55 1

/ MEMORY CHECKER

/ ERRORS REGARDING MEMORY AND MEMORY DEBUGGER C / The C compiler does not check memory access error and memory leaks / familiarity with Assembly Language OS / Designed also for OS and device drivers C / errors regarding memory happen quite often / buffer overrun / memory leak 2 / free doubly / read from uninitialized memory / The memory debugger is for these troubles

dbx Sun / attached in Sun C Compiler Valgrind LinuxMacOS X / for Linux, MacOS X Electric Fence (efence) LinuxWindows*BSD / for Linux, Windows, *BSD google-perftools Google / performance profiler by Google / checks only for memory leaks LinuxWindows*BSDMacOS X / for Linux, Windows, *BSD, MacOS X

/ EXAMPLE OF MEMORY LEAK / source code $ cat leak.c #include <stdlib.h> #include <string.h> void mem_leak() { char *str = (char*)malloc(sizeof(char) * 32); strncpy(str, Hello, World!, 32); } void main(void) { mem_leak(); }

DBX / DETECT MEMORY LEAK IN DBX / enable check for memory leaks (dbx) check -memuse (dbx) run... (: 1 : 32 ) ====== ====== ======== ========================= 32 1 0x220d8 mem_leak < main

/ READ FROM UNINITIALIZED MEMORY / source code $ cat mcopy.c #include <stdlib.h> #include <string.h> void mem_copy() { char *hello = (char*)malloc(sizeof(char) * 32); strncpy(hello, Hello, World!, 32); char *str = (char*)malloc(sizeof(char) * 32); strncpy(hello, str, 32); /* */ } void main(void) { mem_copy(); }

DBX / DETECT INVALID MEMORY ACCESS IN DBX / enable checks for memory access (dbx) check -access (dbx) run... 1 0x22180 0x22180 32 : [1] mem_copy() 8 "mcopy.c [2] main() 14 "mcopy.c stopped in mem_copy : 9 "mcopy.c 9 strncpy(hello, str, 32);

/ PRACTICE

DBX / LET'S USE DBX dbx / let's use dbx / debugger function / memory checker function

MAIN() Exercise: PRINTS MAIN() ARGUMENTS / prints main() arguments.! #include <stdio.h>! int main(int argc, char* argv[])! {! int i;! for(i = 0; i < argc; i++)! {! }! }! printf("argv[%d]: %s\n", i, argv[i]);!

EXERCISE: DYNAMIC ALLOCATION OF 2-D ARRAY malloc() / use malloc() twice.! char array[m][n + 1] / take 2 arguments and create char array[m][n + 1].! array[0] "ABC..." / Fill "ABC..." in array [0].! array[1] "BCD..." / Fill with shifted string like "BCD..." in array[1] and after.! array[x]nul/ Terminate with NUL string in each array[x].! print / print the two arguments.! array[x] print / print each array[x] as one line.!

/ ANSWER 1. #include <stdio.h>! 2. #include <stdlib.h>! 3. void! 4. main(int argc, char **argv)! 5. {! 6. int m, n;! 7. int i, j;! 8. char **array;! 9. if (argc < 3)! 10. {! 11. printf ("%s <m> <n>\n", argv[0]);! 12. exit (1);! 13. }! 14. m = atoi (argv[1]);! 15. n = atoi (argv[2]);! 1. printf ("m = %d, n = %d\n", m, n);! 2. array = (char **) malloc (m * sizeof (char *));! 3. for (i = 0; i < m; i++)! 4. array[i] = (char *) malloc (n + 1);! 5. for (i = 0; i < m; i++)! 6. {! 7. for (j = 0; j < n; j++)! 8. array[i][j] = 'A' + i + j;! 9. array[i][n] = '\0';! 10. }! 11. for (i = 0; i < m; i++)! 12. printf ("%02d: %s\n", i, array[i]);! 13. }!

: / EXERCISE: CALCULATE FIBONACCI ARRAY #include <stdio.h> #include <stdlib.h> int fibonacci (int n) { } int i; int F[3]; F[0] = 0; F[1] = 1; for (i = 0; i < n; i++) { printf ("F_%d = %d + %d = %d \n", + F[1]); } i + 2, F[0], F[1], F[0] F[2] = F[0] + F[1]; F[0] = F[1]; F[1] = F[2]; return F[1]; int main (int argc, char **argv) { int n = 10; int ret; if (argc >= 2) n = atoi (argv[1]); ret = fibonacci (n); printf ("fibo[%d] = %d\n", n, ret); return 0; }

/ MARKOV CHAIN ALGORITHM / generate text without sense (for fun) / read ordinary text / split to words / create dictionary that records chain of words / records chains that happened at least once / generate text randomly along with the dictionary / the text will have no sense.