1
00:00:00,005 --> 00:00:04,002
- [Instructor] So you want to read Linux kernel source code.

2
00:00:04,002 --> 00:00:05,006
Good for you.

3
00:00:05,006 --> 00:00:07,006
That's a fun hobby.

4
00:00:07,006 --> 00:00:09,009
So searching through the source code,

5
00:00:09,009 --> 00:00:13,006
something you want to do to find where in the source code

6
00:00:13,006 --> 00:00:16,007
is a thing that you're interested in.

7
00:00:16,007 --> 00:00:18,009
Well, of course you can use grep.

8
00:00:18,009 --> 00:00:21,001
I often do a recursive grep,

9
00:00:21,001 --> 00:00:23,001
maybe a dash little I to ignore case,

10
00:00:23,001 --> 00:00:25,009
maybe a dash l just to get the names of the files

11
00:00:25,009 --> 00:00:27,002
to look in further.

12
00:00:27,002 --> 00:00:31,008
For example, if I was interested in sys_read,

13
00:00:31,008 --> 00:00:35,003
which turns out to be a function for doing reading,

14
00:00:35,003 --> 00:00:38,008
maybe I want to find a prototype, something like that.

15
00:00:38,008 --> 00:00:40,003
I could look in the include directory.

16
00:00:40,003 --> 00:00:43,003
So if I'm in the top level of the kernel source,

17
00:00:43,003 --> 00:00:48,003
I'm going to do grep -rl sys_read include,

18
00:00:48,003 --> 00:00:50,004
it will look in the include directory

19
00:00:50,004 --> 00:00:52,009
and everything underneath the include directory

20
00:00:52,009 --> 00:00:55,008
and report the path to any file

21
00:00:55,008 --> 00:00:58,005
that had the string sys read.

22
00:00:58,005 --> 00:01:01,004
And you know, maybe get about a half a dozen or so of those

23
00:01:01,004 --> 00:01:04,000
that you can go look at.

24
00:01:04,000 --> 00:01:07,001
What I frequently use, this is the classic way

25
00:01:07,001 --> 00:01:13,002
of looking at kernel source, is the cscope tool.

26
00:01:13,002 --> 00:01:16,002
So the kernel make file supports building

27
00:01:16,002 --> 00:01:20,001
a cscope index file or files.

28
00:01:20,001 --> 00:01:22,003
So you can say make cscope

29
00:01:22,003 --> 00:01:24,007
and that takes a little while to run.

30
00:01:24,007 --> 00:01:29,002
And then you typically launch it by saying cscope -d.

31
00:01:29,002 --> 00:01:31,002
And then you're going to get this

32
00:01:31,002 --> 00:01:35,002
old-school kind of text interface,

33
00:01:35,002 --> 00:01:36,009
but it's very fast.

34
00:01:36,009 --> 00:01:38,002
So you can look for strings,

35
00:01:38,002 --> 00:01:40,001
you can look for definitions of things,

36
00:01:40,001 --> 00:01:43,006
you can look for who's calling what and so forth.

37
00:01:43,006 --> 00:01:47,001
We'll look at that and we'll let you give that a try.

38
00:01:47,001 --> 00:01:48,007
So you're going to get a list of output.

39
00:01:48,007 --> 00:01:52,001
You'll move between those with arrow keys,

40
00:01:52,001 --> 00:01:57,000
but you're going to have a search part and a result part,

41
00:01:57,000 --> 00:02:00,001
and you move between those parts with the tab key,

42
00:02:00,001 --> 00:02:02,000
so remember that tab key.

43
00:02:02,000 --> 00:02:05,006
And you get out of cscope with control D.

44
00:02:05,006 --> 00:02:07,002
And when you find a hit

45
00:02:07,002 --> 00:02:08,007
and you hit enter to go to it,

46
00:02:08,007 --> 00:02:11,005
it's going to launch an editor on that file

47
00:02:11,005 --> 00:02:15,006
and show you the line that it found.

48
00:02:15,006 --> 00:02:19,004
And usually that editor is VI or vim.

49
00:02:19,004 --> 00:02:25,005
So knowing how to use VI is always valuable on Linux.

50
00:02:25,005 --> 00:02:30,009
And the kernel make file also supports tags.

51
00:02:30,009 --> 00:02:33,006
And VI is an editor that supports tags,

52
00:02:33,006 --> 00:02:36,004
so within the editor you can search for things

53
00:02:36,004 --> 00:02:41,002
like you could launch VI -t and give it a symbol.

54
00:02:41,002 --> 00:02:46,002
Now it's going to go find sys read in a file.

55
00:02:46,002 --> 00:02:49,003
So the make tags also created an index.

56
00:02:49,003 --> 00:02:52,008
And while you're in VI, you could do

57
00:02:52,008 --> 00:02:58,001
control close square bracket on a symbol to go look it up.

58
00:02:58,001 --> 00:03:00,005
And so you can go from place to place to place.

59
00:03:00,005 --> 00:03:03,005
If you want to go back, you do control T, right?

60
00:03:03,005 --> 00:03:07,002
It's all cryptic, it's all VI kind of cryptic,

61
00:03:07,002 --> 00:03:11,004
but it could be fast and convenient and reliable.

62
00:03:11,004 --> 00:03:15,002
So I use cscope and tags in combination

63
00:03:15,002 --> 00:03:18,000
all the time looking at kernel source.

