ctags, cscope

34
1 ctags, cscope 제 15 제 : ctages, cscope

description

제 15 강 : ctages , cscope. ctags, cscope. src. include. net. vm. sys. sys.h inode.h user.h driver.c stream.c hd.c vm.h file.h error.h intrp.c signal.c strategy.c cdev.h bdev.h type.h read.c write.c sleep.c - PowerPoint PPT Presentation

Transcript of ctags, cscope

Page 1: ctags,  cscope

1

ctags, cscope

제 15 강 : ctages, cscope

Page 2: ctags,  cscope

2

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 . . . . . . . .

. . . . .

Page 3: ctags,  cscope

3

#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

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 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

Page 4: ctags,  cscope

4

#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?}

src

kendmr buf.h

bio

dir

Reading Codes . . . read.c

Page 5: ctags,  cscope

5

Looking for identifier

• grep s f -- search string• regular expression -- too much

output• Awk -- search, what next?• find -- too many directories

• Still too slow – read entire source in disk – physical limit– eg 100 men 1 year 5 minutes (1 pass)

Page 6: ctags,  cscope

6

Where is read() function? index file (tags file: UNIX)

from all files make index file

. . .apple p. 41orange p. 77

read file/line/patternMAX file/line/pattern

index: tags file:

word page where word is defined

Indexage 173AI 31apple 65bit 891

id file/line where id is defined

Page 7: ctags,  cscope

7

ctags command• man ctags

– generate tags file– used by editor– ctags –R files …

• “tag”– language objects– function name, var name, type name

• tags file– each line: (1) tag, (2) file, (3) line # or RE– eg

ex cd linux-practice cd lionsctags –Rvi tags

src

kendmr buf.h

bio

dir

Page 8: ctags,  cscope

8

generating tags file- awk exercise -

cd linux-practice/src/lions/usr/sys/kenlsawk ‘$0~/read\(/ {print $0}’ *.cawk '$0~/^read\(/ {print FILENAME $0}'

*.cawk '$0~/^read\(/ {print FILENAME " " NR " "

$0}' *.c

Page 9: ctags,  cscope

9

• [using tags index]– vi -t read when starting vi– :ta read at vi colon mode– ctrl-] to go, at vi command mode– ctrl-t to return, at vi command mode

<example> vi –t read

goto rdwr ^]goto FPIPE ^]^t

• Try long command to create my tags file for #define:

find /lions/src/usr/sys -name “*.h”

-exec awk '$0~/#define/{print FILENAME “ ” $0}' > my_tags_file {} \;

lions/src/usr/sys

kendmr buf.h ino.h

Page 10: ctags,  cscope

10

• [Specify which tags file]– if many tags file (projects) exist– need to tell vi which tags file to use– [on login]

• .bashrc file: export TAGPATH=/usr/src/linux/tags

– [when starting vi]• .exrc file: set tags= /usr/src/linux/tags

– [to change tags within vi]• vi• :set tags= /usr/src/linux/tags

ex try with your vi

ex does not work on this system!

ex try with your .exrc

vi –t read|

vi process looks for tags file

|which tags file?

|vi (sh var)

orsh (env var)

lions/src/usr/sys

kendmr buf.h ino.h

Page 11: ctags,  cscope

11

• Caveat– pathname-- use absolute pathname!

• $ ctags -R• $ ctags -R /usr/src/linux/kernel/*• $ ctags -R `pwd`/*• 3 pathnames

– Where I ran ctags (past)– Where I stored tags (many)– Where I run vi (now)

– sort -- check the version• some versions do not sort automatically

– nesting level• some versions have limits on the level of

nesting

vi searches tags file|

fetch file pathname (from tags)

|cd pathname

|relative pathname?

(./src / file.c)

Page 12: ctags,  cscope

12

main(){ float x, y;

x = sin(y); /*where is sin()?*/....

}

float sin(float a);{ /* ...... main body of sin() ....... }

vi editor moves to target page (using tags file)

Page 13: ctags,  cscope

13

• In terms of number of disk block accesses– size of (index file) << size of (entire

source)

• vi editor accesses index file (tags)

• In UNIX:– ctags command: creates tags file (index file)– vi editor: look for the identifier in the tags

file and display the new file content

Page 14: ctags,  cscope

14

File B

x = x + 1;

z = z + 5;

y = y - 1;

File A File C

id(tag) File loc in file

X A 133

Y B 24

Z C 177

index file (tags)

editor accesses

vi -t Y

Page 15: ctags,  cscope

15

Awk is ….

• Script– Awk –f X read program from awk

script file

• Example– Given *.c *.h files– Awk program– tags

file

Page 16: ctags,  cscope

16

Productivity• Time to find read() function?

1 second? 1 minute? productivity 10 minutes?

• Repeated hundreds, thousands of times per day no matter who/project/language/…

• Goal of timesharing system (UNIX)– Program Development Efficiency– C/UNIX mature environment (still changing ...)– java, web need more time to mature

Page 17: ctags,  cscope

17

Summary• Disk speed, Separate compile• Many files, directories• source code working• Need to move quickly to read()

– string search -- regular expression, grep

– traverse subtrees -- find– more faster -- ctags

Page 18: ctags,  cscope

18

More on Ctags

Page 19: ctags,  cscope

19

Ctags (Unix)

• Creates tags from – C, C++, Yacc, Lex, Pascal, Fortran,

Lisp, …

• tags : sorted index locations of object declarations

• Objects include – functions, procedures, global variables,

macros, structs, typedefs, yacc tokens, methods and classes

– Still changing (you must check ctags version …)

Page 20: ctags,  cscope

20

Exuberant Ctags• Less fooled by code containing #if • Included in RedHat Linux 5.2• Can find all types of C language tags, including

– macro definitions– enumerated values(the values inside enum{…})– method definitions– external function prototypes– typedef names– ….

• Compiles on UNIX, QNX, MS-DOS, NT, Windows 95, OS/2 and the Amiga

Page 21: ctags,  cscope

21

Linux Cross-Reference (lxr)

• General hypertext cross-referencing tool• Versatile cross-referencing tool for

relatively large code repositories• Based on stock web technology, so

the codeview client may be chosen from the full range of available web browsers

• Main Features– the ability to jump easily to the declaration

of any global identifier(even all references to global identifiers are indexed

Page 22: ctags,  cscope

22

http://lxr.linux.no/source

click here

Page 23: ctags,  cscope

23

type any symbollike sys_fork

Page 24: ctags,  cscope

24

Page 25: ctags,  cscope

25

(Cont’d)

– quick access to function declarations, data(type) definitions and preprocessor macros -> makes code browsing more convenient

– at-a-glance overview(e.g. which code areas that will be affected by changing a function or type definition)

– hypertextual sugar, such as e-mail and include file links, are provided as well

Page 26: ctags,  cscope

26

cscope• symbol DB

– Rebuilds the DB only if a source file has changed– or the list of source files is different

• displays the following input fields :– Find this C symbol– Find this definition– Find functions called by this function– Find functions calling this function– Find assignments to

Page 27: ctags,  cscope

27

preperation for cscope

• find ….. > cscope.files /* files to parse*/

• cscope –R /* parses ALL subdirectories*/• default DB is “cscope.out”

– cscope –b /*rebuild DB, not start cscope GUI*/– or cscope –b –q /* for large DB, quick search*/

• set env variable CSCOPE_DB • set env variable EDITOR (my favorite

editor)

lions/src/usr/sys

kendmr buf.h ino.h

Page 28: ctags,  cscope

28

Page 29: ctags,  cscope

29

sys_fork

Page 30: ctags,  cscope

30

Page 31: ctags,  cscope

31

Page 32: ctags,  cscope

32

using cscope DB• [Method 1] Simply say “cscope” (ctrl-d to exit)

– you are inside cscope GUI (try man cscope)• “arrow” move between search types • “tab” switch between search type / result

– cscope searches & launches favorite editor ($EDITOR)

• [Method 2] Vim– you are inside Vim, then invoke cscope within Vim

• Inside vim– ctrl-] (to go) ctrl-t (to return)– ctrl-\s menu at bottom (uses of symbols)– ctrl-spacebar s repeat same search – (new window for 2nd search)

Page 33: ctags,  cscope

33

cscope & unwanted directories

Removing unwanted files/directories HERE=/home/my/src cd / find $HERE -path "$HERE/arch/*" ! -path "$HERE/arch/386*“ -

prune -o \ -path "$HERE/include/asm/*" ! -path "$HERE/include/asm/386/*" -

prune -o \ -name "*.[chxs]“ -print >

/home/my/cscope/cscope.files

Generating cscope database. cd /home/my/cscope # directory with 'cscope.files' cscope -b -q -k # k means “don’t include user level info.--

it is kernel”

read: http://cscope.sourceforge.net/large_projects.html

src

arch dev

386 mips

Page 34: ctags,  cscope

34

man find-path pattern To ignore a whole directory tree, use -

prune. For example, to skip the directory

'src/junk' and all files and directories under it,

and print the names of the other files found, use following command :

find . -path ! /src/junk‘ –prune -o -print