10 days of learning the first day of the Linux kernel---kernel exploration tool class

Source: Internet
Author: User
Tags system log tools and utilities volatile dmesg

Original: 10 days the first day of learning the Linux kernel---kernel exploration tool class

Winter vacation idle down, can enjoy doing their favorite things, concentrate on the lab in the dry up, because when the sophomore contact Linux, just about the kernel is really not understand, so 10 days still hope to be able to add the Linux kernel related knowledge, Then continue to stay in the lab to summarize the Linux kernel programming, 10 days will definitely not grasp the Linux kernel, here I just think I am not very understood and very important points of difficulty to write, and everyone to share, I hope you correct each other to learn.

A specific overview of Linux here is not much to say, today is mainly about the Linux kernel commonly used in the use of data structure and syntax, and briefly describe some tools and utilities to get the information needed to understand the kernel insider, but also to describe the basic concepts involved in each kernel subsystem, and a rough analysis of the system functions of the necessary code.

Data types commonly found in the kernel are linked lists, lookups, trees, and so on. Linked list can be said throughout the Linux kernel, in Linux, linked list is often in the form of circular doubly linked list, so given a list of any node, can find the next node and the previous node, linked list is a very important point of knowledge, need to learn data structure to master, there is not much to explain, All the code for the linked list definition can be viewed in the header file include/linux/list.h. As for the search algorithm, mainly manifested in the large O notation, which represents a fixed value of n in the worst case of the time spent in the search, but also the number, the number is used in the memory management of Linux, can effectively access and manipulate the data, a few days ago, the knowledge of the tree and the code, harvest quite abundant, Trees are often implemented in Linux in the form of linked lists and arrays, where two of forks and red-black trees are used most in Linux, and are presented here to help you understand the Linux kernel, so you can better understand complex operations.

As an operating system, the processor is of course an inaccessible knowledge point, some parts of Linux and it is closely related, the assembly is the best understanding of the underlying language, because in the previous sophomore learned the principle of microcomputer, in this will not introduce the x86 and PowerPC architecture assembly language. However, there are several special C language usages that need to be outlined, focusing on the common C language specifications in the Linux2.6 kernel and clarifying a few C language knowledge.

Asmlinkage

  Asmlinkage to use a local stack to pass parameters, which involves macro fastcall, which informs the assembler to use general-purpose registers to pass parameters, as an example of asmlinkage:

long sys_gettimeofday (structstruct timezone __user *tz)

 Ul

  UL is actually unsigned long, it tells the compiler to use this value as a long numeric processing, using UL to help write the architecture-independent code, some examples in the kernel, such as include/linux.h

#define Ulong_max  (~0ul)

Inline

Inline indicates that to optimize the executable code of the function, most of the inline functions used in the Linux kernel are defined as static types (static inline), which means that it is added directly to the calling program, with the advantage of eliminating any overhead of function calls. The disadvantage is that using inline will increase the size of the binary image, thus reducing the speed of accessing the CPU cache, so you cannot always use inline.

Const and Volatile

These two are really easy to confuse and not very understood, Const does not necessarily mean that constants are sometimes "read-only" meaning, here ask "const int *x" and "int const * x" What is the difference?? is a const example.

Static void prefetch (constvoid *x) {    __asm__ __volatile__ ("dcbt 0,%0  " :  "R"  (x));}

The keyword volatile indicates that the variable can be modified without warning, and it informs the compiler to reload its value every time the variable is used, a include/linux/spinlock.h example of how to use volatile

struct  {... volatile int Lock ;...} spinlock_t;

  

So what is the internal mystery of the kernel running, and the next step is to introduce the tools used in the kernel to explore various kernel files.

Objdump/readelf

Objdump/readelf can be used to display any information of the target file and elf file respectively, can use command line parameter on terminal to view the header file, file size and structure of the given target file.

Hexdump

 Hexdump can display the contents of a given hexadecimal/ascii/octal file. These require a few commands to give an example, and you can learn how to use the Hexdump command in practice.

Nm

NM can list symbols for the specified target file, displaying the value, type, and name of the symbol.

Objcopy

Objcopy, see half a word to know is the meaning of replication, when you want to copy a target file and ignore or change the content of a certain aspect, you can use the command.

Ar

Ar commands are most commonly used in make files, which connect some common functions to a single library file, or separate the target files from a single library to help maintain the library of indexed libraries that are used by the linker.

How do we "listen" to the kernel when the Linux system is running and the kernel logs some messages and provides status information for the entire operating system? The following are some of the most commonly used kernel and terminal dialog methods.

  PRINTK ()

Everyone knows the printf () function, so why doesn't Linux use it because the kernel doesn't have a link to the standard C library, but the interface is the same, PRINTK is the function that runs in the kernel to display to the console output, The Linux kernel first allocates a static buffer in the kernel space, as a display space, then calls sprintf, formats the display string, and finally calls Tty_write to display the information to the terminal.

Dmesg

DMESG cores can be used in a variety of ways to store logs and information, DMESG is a program for detecting and controlling the kernel ring buffer, which can be used to display buffered content stored in/proc/kmsg, and to choose whether to filter the buffer based on the message level And this program is used to help users understand the system's startup information.

/var/log/messages

 Most of the   /var/log/messages stored in Linux systems are messages that have been logged into the system. The messages log is the core system log file. It contains boot messages at system startup and other status messages when the system is running. IO errors, network errors, and other system errors are recorded in this file. Other information, such as a person's identity switch to root, is also listed here. If the service is running, such as a DHCP server, you can observe its activity in the messages file. Typically,/var/log/messages is the first file you want to view when troubleshooting.

  

 Summary:

  Today is mainly about these, before exploring the Linux kernel should have the basic knowledge and background, the above-mentioned data structure to understand the fundamental understanding of the process and the paging mechanism, about the kernel, the tool set is also a bit of a rough explanation, But need to use in the actual operation in order to realize the more profound understanding, what problems I hope you correct.

 

  All rights reserved, reprint please specify reprint address: http://www.cnblogs.com/lihuidashen/p/4236635.html

10 days of learning the first day of the Linux kernel---kernel exploration tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.