Trace / Debug Howto?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Trace / Debug Howto?
# 8  
Old 07-01-2014
"man select" will tell you all about the select API.

Quote:
select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking.
# 9  
Old 07-01-2014
The following script can demonstrate; please test it yourself:
Code:
{
suck_stdin() {
local my_line
read my_line
echo "suck_stdin: $my_line"
}

while read line
do
 echo "passwd: $line"
 suck_stdin
done < /etc/passwd
} < /etc/group

You see that the suck_stdin() function reads a line, therefore it obscures the while loop. Here the suck_stdin draws every second line from /etc/passwd.
Now please harden it with the suggested descriptor magic:
Code:
{
suck_stdin() {
local my_line
read my_line
echo "suck_stdin: $my_line"
}

while read line <&3
do
 echo "passwd: $line"
 suck_stdin
done 3< /etc/passwd
} < /etc/group

and the suck_stdin() won't obscure the while loop - instead it reads from the outer stdin (here: /etc/group).
Commands like ssh behave like the suck_stdin (ssh can usually be fixed with the -n option), and maybe also your m_* functions contain read or ssh.
Conclusion:
without knowing your m_* commands, I suggest to harden your while loop.

Last edited by MadeInGermany; 07-01-2014 at 01:22 PM.. Reason: descriptor was mixed up
# 10  
Old 07-01-2014
Quote:
Originally Posted by bbq
Like I said in the OP, I'm really looking for online tutorials. Especially anything that can give me more insight into the output from strace for example.
The output of strace means really doesn't mean much out of context. Have you followed any of my advice at all yet? Do you know what those file descriptors are?

Quote:
I still don't know what that 'select' is about.
The manpages contain much information about it.
# 11  
Old 07-01-2014
Consider the bashdb debugger as well. It has some useful features, but requires serious reading beforehand.

BASH with Debugger and Improved Debug Support and Error Handling
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 12  
Old 07-03-2014
Thanks very much guys

That's the kind of stuff I have been looking for.

I'll certainly play around with the code posted and I wasn't aware of the select command, I assumed it was something low level in the kernal maybe Smilie

I still don't know what the file descriptors are, as since running under strace it hasn't failed. But if it does I should be able to discover where the problem is.

I don't know if the -n will help with this version of the harness. But if things get bad I'll try it.

The harness uses ssh multiplexing to add the ssh connections to NIX domain sockets and then echos stdout from the remote server into a local report file in real time.

The new version I'm working on uses a messaging framework to send a report file so shouldn't have any issues like this.

I hope Smilie

Thanks for the feed back, really helpful stuff.

---------- Post updated at 05:37 PM ---------- Previous update was at 09:50 AM ----------

That's really interesting MIG Smilie

I wasn't aware of that behaviour before.

Thanks

---------- Post updated 03-07-14 at 02:18 PM ---------- Previous update was 02-07-14 at 05:37 PM ----------

Thanks to all of you for your help.

The job eventually failed again and I was able to trace the problem to a hanging write to a named pipe that the client process had died on.

I've put in a rather ugly fix that does the write in the background and then kills $! if it is still running.

It would be better if I could test if a process is listening on the end of the pipe but I haven't found a way of doing so.

Does anyone know if that can be done?

Thanks again for all the helpful input
# 13  
Old 07-03-2014
Some Linux ship with a timeout command.
Otherwise this function can be used:
Code:
timeout () {
case $1 in
[1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])
 to=$1
 shift
 ;;
*)
 echo "usage: $0 timeoutseconds command..."
 return 1
 ;;
esac
perl -e "alarm $to; exec @ARGV" "$@"
# without perl one can try
# ("$@"; kill 0) & (sleep $to; kill 0) &
}

Now, replace your
Code:
hanging_command

by e.g.
Code:
timeout 5 hanging_command

and hanging_command will get a SIGALRM after 5 seconds, that hopefully kills it.
# 14  
Old 07-03-2014
Quote:
Originally Posted by bbq
It would be better if I could test if a process is listening on the end of the pipe but I haven't found a way of doing so.
If you can use a non-named pipe, and keep it open, if the client dies, the writer will receive SIGPIPE.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Trace su to root

Hi, is it possible to trace everything about user that changes from its own user to root user, failed and successful attempts (I would need user and IP address of user that was trying to do that)? I tried adding auth.notice and auth.info in syslog.conf but it only tracks user withoud IP... (6 Replies)
Discussion started by: sprehodec
6 Replies

2. Shell Programming and Scripting

Stack Trace

Hi All Thought it would be kind of fun to implement a stack trace for a shell script that calls functions within a sub shell. This is for bash under Linux and probably not portable - #! /bin/bash error_exit() { echo "=======================" echo $1 echo... (4 Replies)
Discussion started by: steadyonabix
4 Replies

3. UNIX for Dummies Questions & Answers

Help with trace file

Hi, I am an oracle DBA pretty new to unix. We had one of the filesystems full and a colleague cleared some stuffs to create more space. I just checked now and found there is now more space available. How do i find exactly what he cleared? We have oracle database installed and its a RAC... (4 Replies)
Discussion started by: dollypee
4 Replies

4. Solaris

Log Trace

Hi I would like to display only error messages from my log files while monotring application on my solaris box using tail command. Is there other way we can monitor please let me know? In general # tail -f "xyz.log' ---> this will display current activity of the logs, instead i would like... (4 Replies)
Discussion started by: gkrishnag
4 Replies

5. Shell Programming and Scripting

how to supress the trace

Hi I am working in ksh and getting the trace after trying to remove the file which in some cases does not exist: $ my_script loadfirm.dta.master: No such file or directory The code inside the script which produces this trace is the following: ] || rm ${FILE}.master >> /dev/null for... (3 Replies)
Discussion started by: aoussenko
3 Replies

6. HP-UX

how to trace the logs

Hi, Last day, In one of our unix boxes there was an issue wherein few of the directory structures were missing / got deleted. Is there any way by which we can find how it happened, I mean by going through syslog / which user had run what command? Thanks for your help (3 Replies)
Discussion started by: vivek_damodaran
3 Replies

7. UNIX for Dummies Questions & Answers

Trace DHCP - Help!

Can someone help me with commands to trace DHCP on an HP_UX box? Thanks! (0 Replies)
Discussion started by: nuGuy
0 Replies

8. Shell Programming and Scripting

Function Trace

Does anyone know if there is a util out there to run through a shell script and be able to trace the function call tree. I have inherited some code and the original author was ****mad**** keen on functions - even ones called only once! If anyone knows of anything I would appreciate it - web... (3 Replies)
Discussion started by: ajcannon
3 Replies

9. IP Networking

trace route ip

hi everybody , i have a solaris 5.6 box and i want to trace the route on an ip i treid traceroute but soalris 5.6 does not support it ... is there a command that can be used equivelent to traceroute ? thanks for your help (2 Replies)
Discussion started by: ppass
2 Replies

10. UNIX for Advanced & Expert Users

Trace connections

In my organization in order for anyone to go to any Unix server they have to go through "SERVER A" and login as themselves. Then people are free to go enywhere they please. For example: SERVER A, loggs in as himself telnets to SERVER B, loggs in as guest telnets to SERVER C, loggs in as... (8 Replies)
Discussion started by: jraitsev
8 Replies
Login or Register to Ask a Question