printk messages not showing up in dmesg log - linux

I'm developing a simple driver for fun, and I'm trying to debug it with printk messages. It seems that dmesg doesn't display the last printk message, and the only way to get it to show the last message is by doing another printk, which in turn is also swallowed by dmesg.
Some examples for clarification:
in my module's init function I print the following statement:
printk(KERN_INFO "%s loaded with major: %u, minor: 0", ...);
After insmoding for the first time after a boot, I see a "tainted kernel" message with dmesg, however I can't see the message I printed.
Then, I run the following command:
cat /dev/mymodule
which invokes my open function which prints:
printk(KERN_DEBUG "%s opened", ...);
and then invokes my read function which prints:
printk(KERN_DEBUG "%s reading", current->comm);
and then goes to sleep. After running dmesg (while the read function sleeps), I can suddenly see my module init message, and I can also see the "cat opened" message, however the "cat reading" message is missing.
Terminating cat with ctrl+c causes two prints: "sleep interrupted", and then "cat released". Running dmesg shows the "sleep interrupted" message, but the "cat released" message is missing, and so on.
Does anyone know what the problem might be?

You should add \n at end of every printk message not to miss any logs in the dmesg. This will solve your problem.

Related

Run AT commands from adb shell on Redmi 7

I tried this:
echo -e "ATD123456789;\r" > /dev/smd0
and then when I ran:
cat /dev/smd0
I got this output:
ATD123456789;
Is that what I'm supposed to see? The phone didn't respond to the command.
Update: The phone made a call when I used smd7 or smd11. The problem is I'm trying to send SMS messages using AT+CMGS and it's not working.
Update2: I run this command:cat /dev/smd7 & echo -e "AT+CMGS=24;\r" > /dev/smd7.
Then I enter the PDU message and I get this: /system/bin/sh: 079...771B: not found
As you probably know, the command
ATD<number>;\r
performs a voice call to the destination number <number> (without the semicolon ; the call type would depend o the current settings of AT+FCLASS command).
By default the OK result code would be received as soon as it starts remotely ringing, so after some seconds. But it would take even more if there are network problems or the remote number is unavailable/doesn't exist.
The default timeout of ATD command during a voice call is 30s, and can be changed by issuing ATS7 command. For example, to set a 1 minute timeout:
ATS7=60
The answer you get is the command echo: in fact the modem, by default, echoes every character sent to its AT port (the echo can be desabled through ATE0 command and aenabled again with ATE1). Receiving it **is the proof that the modem is correctly powered on and that it communicates correctly.
So, even though I'm aware that's not the only thing you expect to seee (you would like to see an answer!) you are actually supposed to see it.
Some pieces of advice in order to receive your answer:
Start providing simplier commands with shorter timeouts. For example the very basic AT.
Make sure to wait at least the maximum command timeout
Set the cat command in background and before starting providing commands:
cat /dev/smd0 &
echo -e "AT\r" > /dev/smd0
OK
Note: I'm not aware of any timeout in cat command.
To have an interactive session you can use:
strace 2>/dev/null -e inject=ioctl:retval=0 microcom /dev/smdXX
Without the strace command, microcom returns an ioctl error.
Strace makes microcom think the ioctl succeeded and so it allows it to continue and run.

How can I detect if any error was given by Helgrind(valgrind)

I have a bash script that needs to detect whether any error was given by Helgrind. I have tried using the following:
thread_race=$(valgrind --error-exitcode=10 --tool=helgrind ./$2.out $3; echo $?)
$2 is a .cpp file and $3 is an argument. However, this doesn't work because it doesn't actually change the exit code.
I would expect thread race to be 10 if any errors are detected, and something else otherwise.
Is there any way to know if helgrind has detected any errors at all and then save the answer? I'm using Ubuntu linux with clang++.
EDIT:
In both cases when helgrind detects an error, and doesn't detect an error, thread_race returns 134.
I have a similar test without helgrind that works perfectly fine;
leak=$(valgrind --leak-check=full --error-exitcode=55 ./$2.out $3 >/dev/null; echo $?)
This would change the exit code to 55 when an error is given. I have been notified that --leak-check=full is needed for proper exit codes, using that results in more errors when using helgrind:
thread_race=$(valgrind --leak-check=full --error-exitcode=10 --tool=helgrind ./$2.out $3; echo $?)
outputs
valgrind: Unknown option: --leak-check=full
and thread_race = 1

How to hide or edit a message on closure of a ssh ,when ssh was handled using interact in expect script

I have an expect script which makes user able to login and print some specific info before login,but when I am coming out it it prints "Connection to machine closed." ,I want to change the message which is getting displayed.Script kept it in interact mode.Please help me in finding a solution.
A quick check of the ssh manual reveals that ssh has a -q option:
-q Quiet mode. Causes most warning and diagnostic messages to be suppressed.
You can probably read the ssh manual on your own system by running man ssh.
Alternately, you could set the ssh_config parameter to quiet:
LogLevel Gives the verbosity level that is used when logging messages
from ssh(1). The possible values are: QUIET, FATAL, ERROR, INFO,
VERBOSE, DEBUG, DEBUG1, DEBUG2, and DEBUG3. The default is INFO. DEBUG
and DEBUG1 are equivalent. DEBUG2 and DEBUG3 each specify higher
levels of verbose output.
Here is the actual code block which emits that message. You can see that it's suppressed when the loglevel is quiet:
if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
snprintf(buf, sizeof buf,
"Connection to %.64s closed.\r\n", host);
buffer_append(&stderr_buffer, buf, strlen(buf));
}

printk() doesn't print in /var/log/messages

My OS Ubuntu 12.04. I wrote this Kernel Module and i use insmod and rmmod command but there isn't anything in /var/log messages. how can i fix this problem?
/*
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
Check whether syslog daemon process is running, since this is the process which copies printk messages from kernel ring/log message buffer to /var/log/messages if I am correct. printk messages can be seen using dmesg utility/command or messages will be in /var/log/messages. If correct loglevel is set then printk messages will be displayed on the console right away, no need to use dmesg or no need to check in /var/log/messages. printk debug messages can also be part of /var/log/syslog.
Modern Linux distributions don't use rsyslog (or any other syslog daemon) anymore. They rely on journald which is part of systemd, so the /var/log/messages file is missing and you have to use the journalctl command to read the system log.
Firstly, you should check that whether your module is properly loaded or not, by using this command
lsmod | grep "hello-1" //hello-1 is the name of your module
Since you wrote a kernel module, which prints some message. The messages from kernel and its module can be found in /var/log/syslog or you can view these kind of messages using dmesg command.
As your module prints "Hello World 1.", you should use following command to see message from your module.
dmesg | grep "Hello World 1."
Look for this in /etc/syslog.conf, the *.info... lines. These seem to control what gets logged via printk.
*.=info;*.=notice;*.=warn;\
auth,authpriv.none;\
cron,daemon.none;\
mail,news.none -/var/log/messages
I found that /proc/sys/kernel/printk only really controlled the console logging levels, not the logging to the file. And I guess check syslog is running too ;) We had exactly the same issue, KERN_INFO not going to log files and this fixed it. hth
I tried to print the kernel buffer by typing the following command :
dmesg
This will print the data written in printk

Redirect stdout to fifo immediately

I have, for example, a c program that prints three lines, two seconds apart, that is:
printf("Wait 2 seconds...\n");
sleep(2);
printf("Two more\n");
sleep(2);
printf("Quitting in 2 seconds...\n");
sleep(2);
I execute the program and redirect it to a pipe:
./printer > myPipe
On another terminal
cat < myPipe
The second terminal prints all at once, 6 seconds later! I would like it to print the available lines immediatly. How can i do it?
Obs: I can't change the source code. It's actually the output of a boardgame algorithm, i have to get it immediatly so that i can plug it into another algorithm, get the answer back and plug into the first one...
Change the program to this approach:
printf("Wait 2 seconds...\n");
fflush (stdout);
sleep(2);
printf("Two more\n");
fflush (stdout);
sleep(2);
printf("Quitting in 2 seconds...\n");
fflush (stdout);
sleep(2);
Additional:
If you can't change the program, there really is no way to affect the program's built-in buffering without hacking it.
If you can relink the program, you could substitute a printf() function which flushes after each call. Or changes the startup initialization of stdout to be unbuffered—or at least line buffered.
If you can't change the source, you might want to try some of the solutions to this related question:
bash: force exec'd process to have unbuffered stdout
Basically, you have to make the OS execute this program interactively.
I'm assuming that the actual source file is complete. If so, then you have to compile the source and run it to get it to do anything. Using cat will just print the contents of the file, not run it.
If it was written in bash then it would have to be set mode bit +x, which would then make it executable. Allowing you to run it from a terminal ./script
No need to worry about the syntax since you've stated it's not an option to change it and... It's correctly written in C.

Resources