4. Reading code in MP
description
Transcript of 4. Reading code in MP
1
4. Reading code in MP
제 13 강 : Reading Code in Modular Process(MP)
2
Modular Programming ...• Solves
– Faster compile– Many people can edit
• New Problems– Inter-dependency between files (eg define - use)– Which information - which file?– Change a file – what next?– N people update a.c simultaneously?– Debugging?
src
3
src
include sysvmnet
sys.h inode.h user.h driver.c stream.c hd.cvm.h file.h error.h intrp.c signal.c strategy.ccdev.h bdev.h type.h read.c write.c sleep.cinit.h flt.h mount.h nfs.c super.c win.cobj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . .
. . . . .
4
#include "user.h"#include "sys.h"#include "type.h"
int sys_read (pst)struct buf *pst;{char ps[MAX];int index;
while (i ... ){ ... }
Reading Codes . . . read.c src
sysvm
net
sys.h inode.h user.h driver.c stream.c hd.cvm.h file.h error.h intrp.c signal.c strategy.ccdev.h bdev.h type.h read.c write.c sleep.cinit.h flt.h mount hnfs.c super.c win.cobj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . .
. . . . .
ex cd linux-practice/srccd lions/usr/syslscd kenvi fio.c
5
#include "user.h"#include "sys.h"#include "type.h"
int sys_read (pst) struct buf *pst; {char ps[MAX];int index;
while ((index < MAX ) && (read (temp) != LEVEL) ) {. . . .
}
Reading Codes . . . read.c
6
#include "user.h"#include "sys.h"#include "type.h"
int sys_read (pst) --------------- who calls this?struct buf *pst; ------- what is “buf”?{char ps[MAX];int index;
while ((index < MAX ) && (read (temp) != LEVEL) ) {. . . .
------------ where is read()? what does it perform?}
Reading Codes . . . read.c
7
src
include sys vm net
sys.h inode.h user.h driver.c stream.c hd.cvm.h file.h error.h intrp.c signal.c strategy.ccdev.h bdev.h type.h read.c write.c sleep.cinit.h flt.h mount.h nfs.c super.c win.cobj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . .
. . . . .
Where do I find “read()" ?
Reading Codes
Manual or automatic search?
8
Where is getc() function? (1/5)grep
• Character String Search Command command string file
grep read *.c
Output: eg function (1), calls(30),
comments(100) ex cd linux-practice/src
cd lions/usr/sys/kengrep read *.c | more #--- find read() function
#--- too many lines
9
Where is getc() function? (2/5)
Regular Expression• Previous approach: too much output • Find out exact location where function is
defined: eg Find the line where function read()
begins
Regular Expression
10
Skip RE if you already know
11
finding exact location of a function definition
egreturn type space function name para
comment
[ C syntax ]– return type alphabets [a-Z]*– space bar | tab [\b \t]*– function name alphabet + any char [a-Z] .*– parameters ( expression )
int read (parameters); /* sys call */
12
---- Regular Expression: Metacharacters ----
. Any single char
[ ] any single char from this set [abc] [a-z] [a-zA-Z]+ one or more repetition of preceding item (.+ [a-z]+)* zero or more repetition of preceding item (.* [a-z]*)
[^ ] negate the set (“caret”) ([^0-9] – any char except digit)
[ \t] tab[ \b] blank
^ BOL$ EOL
13
---- RE Examples ----
. Any char
[ ] a char from this set[^ ] negate the set
+ one or more repeat* zero or more repeat
[ \t] tab[ \b] blank
^ BOL$ EOL
. a b c d O
.* O a aa aaa abc
.+ a aa aaa abc
a a ( 一般 search : no meta-char)
abc abc ( 一般 search :no
meta-char )
ab+ ab abb abbb
ab* a ab abb abbb
ab. abc abd abf
abc+ abc abccc abccccc
ab[1-3] ab1 ab2 ab3
ab[1-3]+ ab11 ab1233
a[1-5]b[6-9] a1b6 a5b7
c[^a-Z]+ c1 c23 c243
Strings in the target fileRE
14
grep---regular expression
• grep ^[a-Z]* [ ]*read( *.c search in all .c files
left-parenthesis
match “read” string space-or-tab
zero-or-more-repetition-of-previous-char any-alphabet-charbeginning-of-new-line
New line, function type, function_name ( The target pattern
type space name para
15
Escape – “Don’t handle this part”
(1) user sees shell prompt(2) types command, say, grep [a-Z]* read( *.c(3) shell reads input (line edit)
– (Meaning of *’s are different)• grep’s interpretation of * (0 or more repetition)• sh’s interpretation of * (all .c files under cwd)
– (1st *) is for grep (child process)– (2nd *) is for sh (current process)– escape – “Current process should not handle this part”
(4) Escaped parts are un-touched by current process
grep ‘ [a-Z]* read(’ *.c shell
grep
RE for grep
[a-Z]* read(’
16
Shell Escape• Escape a single char (\ -- back slash)
– echo * vs echo \*
• Escape string ( ‘ ‘ -- single quote), – echo ‘this part is not for shell’
• Escape string ( “ ” -- double quote), except– echo “this part not for shell except $PATH”– echo “Today is `date`”– A few exceptions such as:
($) variable expansion (``) command substitution (see
next)
17
Command substitution ( ` ` back-quote)1. execute inside ` `2. substitute command arg by output from
` `
eg echo date; echo `date`
– mail `cat name_file` name_file:bob jim
– mail bob jim dan dan
18
shell escape exerciseecho date
echo `date`
echo *echo \*
echo ‘PATH is $PATH’echo “PATH is $PATH”
echo ‘Today is `date`’echo “Today is `date`”
19
---- Regular Expression ----
. Any char
[ ] a char from this set[^ ] negate the set
+ one or more repetetion* zero or more repetetion
[ \t] tab[ \b] blank
^ BOL$ EOL
In Linux: -E should be used (grep -E …) -[ ] type blank as it is (not [\b]) -\{ escape needed for { e.g. grep -E “struct[ ]+buf[ ]+\{” -f file_name containing RE pattern
Used in many commands vi, sh, perl, sed, awk minor differences …
20
To find MAX• char ps[MAX]; --- It must be a C constant
looking for #define MAX
123 RE ^#define[ ]+MAX [ ]
+[0-9]+To find buf
• struct buf *pst; --- It must be a C struct
looking for struct buf { RE ^struct[ ]+ buf[ ]*\{
See next page for this
21
ex cd cd lions/usr/sys/ken
(1) to find struct buf
grep –E ‘struct[ ]+buf[ ]*’ *.c
(2) to find macro MAX
grep –E ‘#define[ ]+MAX[ ]+’ *.c
22
Where is read() function?find
• What if many directories? – Run grep many times (each dir)?
• find: traverse all nodes in a sub-tree • find /x -print
– Visit each file under /x and print its pathname
• find / -name ‘a.out’ -print– all files with name a.out (why single quote here?)
• find . -size +12 -print– size 12 blocks
• find / -mtime 7 -print– not modified for a week
src
bobdan
jim
ex find /^c
23
Where is read() function?find
• find /x -exec wc {} \; for this pathname (file)
end of this command (wc)– Visit each file under /x and – perform given command (wc) for the current given
file
• find /x -exec cat {} \;– Visit each file under /x and – cat each current given file
• find / -name ‘a.out’ -exec rm {} \;
src
bobdan
jim
** locate command find is slow locate builds DB fast search
24
• find / -type d -user root -print– !: negate– O: or– conatenation: and
• find /x -exec grep read {} \;• find /x -exec grep “^[a-Z]*[ ]+read\(” {} \;• find /x -exec grep -f use_file {} \; use_file: ^[a-Z]*[ ]+read\(
• FIND: find /x -exec grep “^[a-Z]*[ ]+$1\(” {} \; FIND getc
src
bobdan
jim
2 conditions
25
ex cd cd lions/usr/sys/ken
#----- to find definition struct “inode” #----- but it is not here in ken/
cd .. #--- to lions/usr/sys #--- visit every file below #--- and check definition of
inode#--- use grep command
find . -exec grep -E “struct[ ]+inode[ ]*” {} \;
lions/src/usr/sys
kendmr buf.h ino.h