1 / 34

ctags, cscope

제 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

sierra
Télécharger la présentation

ctags, cscope

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제15강 : ctages, cscope ctags, cscope

  2. 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 init.h flt.h mount.h nfs.c super.c win.c obj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . .

  3. Reading Codes . . . read.c src • #include "user.h" • #include "sys.h" • #include "type.h" • int sys_read (pst) • struct buf *pst; • { • char ps[MAX]; • int index; • while (i ... ) • { ... } sys vm net . . . . . 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 init.h flt.h mount hnfs.c super.c win.c obj.h text.h abs.h ftp.c telnet.c fill.c . . . . . . . . ex cd linux-practice/src cd lions/usr/sys ls cd ken vi fio.c

  4. Reading Codes . . . read.c src dmr ken buf.h bio dir • #include "user.h" • #include "sys.h" • #include "type.h" • intsys_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? • }

  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 (tagsfile: UNIX) Index age 173 AI 31 apple 65 bit 891 from all files make index file index: tags file: apple p. 41 orange p. 77 read file/line/pattern MAX file/line/pattern . . . word page where word is defined id file/line where id is defined

  7. src dmr ken buf.h bio dir 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 lions ctags –R vi tags

  8. generating tags file- awk exercise - cd linux-practice/src/lions/usr/sys/ken ls awk ‘$0~/read\(/ {print $0}’ *.c awk '$0~/^read\(/ {print FILENAME $0}' *.c awk '$0~/^read\(/ {print FILENAME " " NR " " $0}' *.c

  9. lions/src/usr/sys dmr ken buf.h ino.h • [using tags index] • vi -t read when starting vi • :ta read at vi colon mode • ctrl-]to go, at vi command mode • ctrl-tto 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 {}\;

  10. lions/src/usr/sys dmr ken buf.h ino.h • [Specify whichtags 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 does not work on this system! vi –t read | vi process looks for tags file | which tags file? | vi (sh var) or sh (env var) ex try with your .exrc ex try with your vi

  11. vi searches tags file | fetch file pathname (from tags) | cd pathname | relative pathname? (./src / file.c) • 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

  12. vi editor moves to target page (using tags file) main() { float x, y; x = sin(y); /*where is sin()?*/ .... } float sin(float a); { /* ...... main body of sin() ....... }

  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 A index file (tags) File B File C id(tag) File loc in file X A 133 Y B 24 Z C 177 x = x + 1; y = y - 1; editor accesses vi -t Y z = z + 5;

  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. click here http://lxr.linux.no/source

  23. type any symbol like sys_fork

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

  25. cscope • symbol DB • Rebuilds the DB only if asource 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

  26. lions/src/usr/sys dmr ken buf.h ino.h 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)

  27. sys_fork

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

  29. src arch dev 386 mips 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

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

More Related