1. Debugging Uses of sysfs Attributes
- Online Dynamic Debug Parameters
- Real-time adjustment of driver variables (such as baud rate, mode, strategy, threshold, etc.)
- No need to restart the device or reload the driver
- Status Monitoring
- Export read-only attributes of hardware/driver internal status: temperature/battery level/register values/internal statistics, etc.
- Facilitates remote collection and monitoring of hardware health
- Auxiliary Problem Location and Troubleshooting
- Allow temporarily enabling detailed logs, or exporting low-level register information
- Support viewing "last error code", debugging switches, etc.
- Control Behavior/Function Switching
- Simple echo can temporarily modify behavior configuration, such as switching working modes, resetting statistics, forcing power on/off, etc.
2. Typical Operation Examples
We will explain in combination with the source code of 10_touch_sysfs_file/ from the previous chapter.
1. View Attributes
View value
bash
cat /sys/class/class_test/device_test/myparam
# Output: 421
2
2
2. Configure Parameters
Write new value
bash
# Write 88
echo 88 | sudo tee /sys/class/class_test/device_test/myparam
cat /sys/class/class_test/device_test/myparam
# Output: 881
2
3
4
5
2
3
4
5
3. Typical Attributes for Enabling/Disabling Debugging Functions
Suppose we designed a switch in the driver:
c
static int debug_enable = 0; // Debug switch
// Called on read operation
static ssize_t debug_show(struct device *dev, struct device_attribute *attr, char *buf)
{
// Format debug_enable value as string and write to buf buffer
return sprintf(buf, "%d\n", debug_enable);
}
// Called on write operation
static ssize_t debug_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
int val;
// kstrtoint is a kernel helper function: used to parse strings (such as "88\n", "123", "007", etc.) into integer values.
if (kstrtoint(buf, 10, &val) == 0) {
// Successfully parsed, update debug_enable value
debug_enable = !!val; // Convert non-zero values to 1, zero values stay as 0
printk(KERN_INFO "mychardev: debug_enable set to %d\n", debug_enable); // Print new value to kernel log
}
// Return the number of bytes written
return count;
}
DEVICE_ATTR_RW(debug); // Create read-write device attribute debug
// ...in init: device_create_file(dev, &dev_attr_debug);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
debug_enable = !!val;is equivalent to:cif (val) debug_enable = 1; else debug_enable = 0;1
2
3
4
Explanation
!val:- Logical NOT, returns 1 when val is 0, returns 0 when val is non-zero.
!!val:- Negate once more.
- Converts any "non-zero" number to 1, keeps 0 as 0.
| val | !val | !!val | Assigned to debug_enable |
|---|---|---|---|
| 0 | 1 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| -3 | 0 | 1 | 1 |
| 99 | 0 | 1 | 1 |
Clean or normalize boolean variable values, avoiding cases where val is a number other than 0/1 (such as 2, -4, 123), so that
debug_enableis not a clean 0 or 1. Ensures the switch variable is only 0 or 1.
Used during debugging:
bash
# Enable debugging
echo 1 | sudo tee /sys/class/class_test/device_test/debug
# Disable debugging
echo 0 | sudo tee /sys/class/class_test/device_test/debug1
2
3
4
5
2
3
4
5
4. Query Multiple States or Configurations
For outputting structured information, common usage:
c
static ssize_t status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE,
"param=%d\nerror=%d\nstatus=%s\n", myparam, last_error, state_str);
}
DEVICE_ATTR_RO(status);1
2
3
4
5
6
2
3
4
5
6
3. Norms and Suggestions
1. Keep Names Simple and Intuitive
Attribute names should be all lowercase, no spaces, with clear meanings such as enable, mode, threshold, debug.
2. Permission Configuration
RO(read-only): Such as status, statistics, only useshowRW(read-write): Such as parameters, configurable items, bothshowandstoreneed to be writtenWO(write-only, rare): Such as sensitive operations (e.g., triggering a reset once)
3. Error and Input Checking
- store callbacks must validate input to avoid chaos caused by writing illegal values
- Such as value range, format checking, return
-EINVALwhen necessary
4. Synchronization and Safety
- If callbacks involve global variables, hardware operations, pay attention to locking to avoid concurrent access issues
5. Return Value Explanation
- show returns the actual number of bytes written to buf
- store returns count (how many bytes were successfully processed), otherwise the write operation may be abnormal/incomplete