kernel/trace/trace_printk.c
Source file repositories/reference/linux-study-clean/kernel/trace/trace_printk.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/trace/trace_printk.c- Extension
.c- Size
- 20294 bytes
- Lines
- 832
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/seq_file.hlinux/security.hlinux/uaccess.hlinux/kernel.hlinux/ftrace.hlinux/string.hlinux/module.hlinux/mutex.hlinux/ctype.hlinux/list.hlinux/slab.htrace.h
Detected Declarations
struct trace_bprintk_fmtstruct trace_buffer_structfunction list_for_each_entryfunction hold_module_trace_bprintk_formatfunction module_trace_bprintk_format_notifyfunction find_next_mod_formatfunction list_for_each_entryfunction format_mod_startfunction format_mod_stopfunction module_trace_bprintk_format_notifyfunction find_next_mod_formatfunction format_mod_startfunction trace_printk_controlfunction __trace_bprintkfunction __ftrace_vbprintkfunction __trace_printkfunction __ftrace_vprintkfunction trace_is_tracepoint_stringfunction t_startfunction t_showfunction t_stopfunction ftrace_formats_openfunction printk_binsafefunction __trace_array_putsfunction __trace_putsfunction __trace_bputsfunction put_trace_buffunction alloc_percpu_trace_bufferfunction trace_printk_init_buffersfunction trace_printk_start_commfunction trace_printk_start_stop_commfunction trace_vbprintkfunction __printffunction trace_array_vprintkfunction bufferfunction trace_array_printkfunction trace_array_printk_buffunction trace_vprintkfunction init_trace_printk_function_exportfunction init_trace_printkmodule init init_trace_printk_function_exportexport __trace_bprintkexport __ftrace_vbprintkexport __trace_printkexport __ftrace_vprintkexport __trace_array_putsexport __trace_putsexport __trace_bputs
Annotated Snippet
static const struct file_operations ftrace_formats_fops = {
.open = ftrace_formats_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
static __always_inline bool printk_binsafe(struct trace_array *tr)
{
/*
* The binary format of traceprintk can cause a crash if used
* by a buffer from another boot. Force the use of the
* non binary version of trace_printk if the trace_printk
* buffer is a boot mapped ring buffer.
*/
return !(tr->flags & TRACE_ARRAY_FL_BOOT);
}
int __trace_array_puts(struct trace_array *tr, unsigned long ip,
const char *str, int size)
{
struct ring_buffer_event *event;
struct trace_buffer *buffer;
struct print_entry *entry;
unsigned int trace_ctx;
int alloc;
if (!(tr->trace_flags & TRACE_ITER(PRINTK)))
return 0;
if (unlikely(tracing_selftest_running &&
(tr->flags & TRACE_ARRAY_FL_GLOBAL)))
return 0;
if (unlikely(tracing_disabled))
return 0;
alloc = sizeof(*entry) + size + 2; /* possible \n added */
trace_ctx = tracing_gen_ctx();
buffer = tr->array_buffer.buffer;
guard(ring_buffer_nest)(buffer);
event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
trace_ctx);
if (!event)
return 0;
entry = ring_buffer_event_data(event);
entry->ip = ip;
memcpy(&entry->buf, str, size);
/* Add a newline if necessary */
if (entry->buf[size - 1] != '\n') {
entry->buf[size] = '\n';
entry->buf[size + 1] = '\0';
} else
entry->buf[size] = '\0';
__buffer_unlock_commit(buffer, event);
ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
return size;
}
EXPORT_SYMBOL_GPL(__trace_array_puts);
/**
* __trace_puts - write a constant string into the trace buffer.
* @ip: The address of the caller
* @str: The constant string to write
*/
int __trace_puts(unsigned long ip, const char *str)
{
return __trace_array_puts(printk_trace, ip, str, strlen(str));
}
EXPORT_SYMBOL_GPL(__trace_puts);
/**
* __trace_bputs - write the pointer to a constant string into trace buffer
* @ip: The address of the caller
* @str: The constant string to write to the buffer to
*/
int __trace_bputs(unsigned long ip, const char *str)
{
struct trace_array *tr = READ_ONCE(printk_trace);
struct ring_buffer_event *event;
struct trace_buffer *buffer;
struct bputs_entry *entry;
unsigned int trace_ctx;
int size = sizeof(struct bputs_entry);
Annotation
- Immediate include surface: `linux/seq_file.h`, `linux/security.h`, `linux/uaccess.h`, `linux/kernel.h`, `linux/ftrace.h`, `linux/string.h`, `linux/module.h`, `linux/mutex.h`.
- Detected declarations: `struct trace_bprintk_fmt`, `struct trace_buffer_struct`, `function list_for_each_entry`, `function hold_module_trace_bprintk_format`, `function module_trace_bprintk_format_notify`, `function find_next_mod_format`, `function list_for_each_entry`, `function format_mod_start`, `function format_mod_stop`, `function module_trace_bprintk_format_notify`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: pattern implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.