This section introduces several useful functions to create debugging messages. As was mentioned earlier, they are somewhat similar to the functions provided by standard C libraries. sprintf(buffer, str,arg1,...) is a function very useful for converting certain variable types into strings. Its functionality is almost identical to that of the sprintf() function in the C library <stdio.h>. sprintf() also supports the formatting options known from printf(). The character string str consists of regular characters and control characters, if present. Instead of control characters that begin with a % sign and end with a type descriptor (c, s, p, n, o, x, X, d, i, u), variable values are inserted according to the formatting specification. The nth formatting specification refers to the nth argument in the str string. The created string is written to the buffer, and the set of written characters (including the closing null byte) is returned as the result. The formatting options of sprintf() are as follows: A format specification begins with the % sign and ends with one of the type descriptors mentioned. Between these two characters, there may be the following format specifications (in this order): Blank No leading plus sign is used for a positive number, but instead a blank. A minus sign is inserted for negative numbers. This enables positive and negative numbers to appear aligned (if they have the same length). This argument is inserted left-justified in the string. + A plus sign is inserted if the argument is positive. # If the octal system (o) was selected as the output form, then a leading null is added to the argument; 0x or 0X are inserted for the hexadecimal system (x or X). min, max The numbers min and max specify the minimum or maximum length of an output. min or max can be omitted if the respective option is not desired. If min begins with a null, then the output is padded with zeros to the minimum format length. h, l, or L denote that a variable is of short or long type. type A formatting specification ends with the type of variable to be output. The following types are available: c (character) The character arg is output. s (string) The string arg is output to the first null byte (unless limited by the maximum format length). p (pointer) The pointer address is output in hexadecimal system. o (integer) The number arg is output in octal system. x, X (integer) The number arg is output in hexadecimal system. d, i (integer) The leading sign for the number arg should be considered in the output. u (integer) The number arg is considered to be an unsigned number.
The formatting options described correspond to the options of printk(), because printk() itself uses sprintf() to format an output string. However, sprintf() is useful not only in connection with printk(), but also to generate outputs in the proc directory. As was described in Section 2.8, the output is recreated upon each read operation on the proc directory. This is not different from a string that includes output data. We can use sprintf() to output not only fixed text blocks, but also formatted contents of variables and memory addresses. String Operations | lib/string.c |
The Linux kernel has several functions for simple (and familiar) work with strings. These functions are similar to the string operations of the C library. We will briefly introduce them here: |