ctags, cscope
description
Transcript of ctags, cscope
1
ctags, cscope
제 15 강 : ctages, 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 . . . . . . . .
. . . . .
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
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
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)
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
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
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
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
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
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)
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)
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
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
15
Awk is ….
• Script– Awk –f X read program from awk
script file
• Example– Given *.c *.h files– Awk program– tags
file
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
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
18
More on Ctags
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 …)
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
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
22
http://lxr.linux.no/source
click here
23
type any symbollike sys_fork
24
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
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
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
28
29
sys_fork
30
31
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)
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
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