Setting up notifications for long operations

With large or long running applications, some operations in UDB will take a long time to complete.

It is possible to set up UDB to notify you (via email, or Slack, or other methods) by using standard GDB machinery.

The key functionality that enables this is the GDB ‘stop’ command hook, and the ability to run Python code directly from GDB. For more details, see the section on User-defined Command Hooks and Python Commands in the GDB documentation.

The rest of this page shows an example of how to set up a simple email notification. The example

  • sends a notification if it has been more than a minute since the last stop

  • emails the named user with the time taken for the operation to complete and the machine it was running on.

Example hook file

Here is an example hook file and a Python script to send an email.

hook.txt

python

import time
import os

# Info for notification
import socket
hostname = socket.gethostname()

# Email address to send notification to
to_email = 'your email address'

# Initialize time
lasttime = time.time()

# Send notification if this many seconds have elapsed
notify_elapsed_secs = 60

end

# Define the hook
define hook-stop
python
difference = time.time() - lasttime
if difference >= notify_elapsed_secs:
    subject = 'Undo operation completed on %s in %d seconds' % (hostname, difference)
    msg = subject
    # Invoke external tool to send email.
    os.system('python /path/to/send_email.py %s \'%s\' \'%s\'' % (to_email, subject, msg))
lasttime = time.time()

# End of Python block
end

# End of hook-stop
end

send_email.py

# Usage: python send_email.py to_email_addr subject message

import smtplib
import sys

# Import the email modules we'll need
from email.mime.text import MIMEText

from_email = 'address to send from'
to_email = sys.argv[1]
subject = sys.argv[2]
msg_text = sys.argv[3]

# Create a text/plain message
msg = MIMEText(msg_text)

msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email

# Send the message
s = smtplib.SMTP_SSL()
s.connect('SMTP server') # e.g. smtp.gmail.com
s.login('your username', 'your password') # e.g. your usual login and password
s.sendmail(from_email, to_email, msg.as_string())
s.quit()

You need to make the following modifications:

  • Change to_email to your email address.

  • Replace /path/to/send_email.py with the path to send_email.py.

  • Set from_email to the address you would like the email to come from.

  • Adapt the connection and authentication methods and details to your SMTP server.

Note

This example invokes a separate Python script with os.system() to show that an external command (for example sendmail), could be used in place of the Python script. This separation is not necessary for any technical reason.

Example usage

To define and enable the hook:

start 1> source /path/to/hook.txt

To remove the hook:

start 1> define hook-stop
Redefine command "hook-stop"? (y or n) y
Type commands for definition of "hook-stop".
End with a line saying just "end".
>end

Other notifications

Some other notification possibilities:

  • Slack has a command line interface.

  • notify-send provides a local graphical desktop environment pop-up.

  • Emitting a bell character to the terminal.

It might also be useful to include more information about the completed operation: for example the time from info time or the current backtrace.