Skip to content

Instantly share code, notes, and snippets.

@NorimasaNabeta
Created April 28, 2019 07:10
Show Gist options
  • Save NorimasaNabeta/7d2ec4330eaef92bf0fcf4c59b5e59b7 to your computer and use it in GitHub Desktop.
Save NorimasaNabeta/7d2ec4330eaef92bf0fcf4c59b5e59b7 to your computer and use it in GitHub Desktop.
parse proc_smaps and insert into sqlite3 database.
# -*- mode: python; coding: utf-8-unix -*-
#
#
# @see man proc (5)
#
import os, re
import sqlite3
#
#
#
def parse_proc(cur, filename, debug=False):
pmid = 0
pmmid = 0
with open(filename, "r") as fd:
lines = fd.readlines()
for lm in lines:
lm = lm.strip()
#print (lm)
# @brief this does not work, because of the case pathname being void.
# address perms offset dev inode pathname
# 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
# 00400000-0048a000 r-xp 00000000 fd:03 960637 /bin/bash
gr1 = re.search("(\w+)-(\w+)\W*([\-\w]+)\W*(\w+)\W+([\:\w]+)\W+(\w+)[ \t]+(.*)", lm)
if gr1:
gr1sz = len(gr1.groups())
print (len(gr1.groups()), gr1.groups())
address_start = gr1.group(1)
address_end = gr1.group(2)
perms = gr1.group(3)
offset = gr1.group(4)
dev = gr1.group(5)
inode = gr1.group(6)
if gr1sz >= 7:
pathname = gr1.group(7)
else:
pathname = ""
size = int("0x"+address_end, 0) - int("0x"+ address_start, 0)
if debug:
print (gr1.groups())
print ("address : ", address_start, address_end, size)
print ("perms : ", perms)
print ("offset : ", offset)
print ("dev : ", dev)
print ("inode : ", inode)
print ("pathname : ", pathname)
pmid = pmid + 1
cur.execute("INSERT INTO proc_map VALUES (?,?,?,?,?,?,?,?)",
(pmid, address_start, address_end, perms, offset,
dev, inode, pathname))
else:
gr2 = re.search("(\w+):\W*(\w+)\W*\w+", lm)
if gr2:
#print (gr.group(1))
token1 = gr2.group(1)
value1 = gr2.group(2)
if token1 in [ "Size", "Rss", "Pss",
"Shared_Clean", "Shared_Dirty",
"Private_Clean", "Private_Dirty",
"Referenced", "Anonymous",
"AnonHugePages", "ShmemHugePages", "ShmemPmdMapped",
"Swap", "KernelPageSize", "MMUPageSize", "KernelPageSize", "MMUPageSize"]:
if debug:
print (">>>", token1, ":", int(value1), "kB")
pmmid = pmmid +1
cur.execute('INSERT INTO proc_map_smap VALUES (?,?,?,?)',
(pmmid, pmid, token1, value1))
elif token1 in ["Locked", "gVmFlags", ]:
# ProtectionKey: 0
# gVmFlags: rd ex mr mw me dw
if debug:
print (">>>", token1, ":", value1)
pmmid = pmmid +1
cur.execute('INSERT INTO proc_map_smap VALUES (?,?,?,?)',
(pmmid, pmid, token1, value1))
return
#
#
#
if __name__ == "__main__":
conn = sqlite3.connect('example.db')
conn.execute('''CREATE TABLE IF NOT EXISTS proc_map (
pm_id int,
pm_addr_start varchar(8),
pm_addr_end varchar(8),
pm_perms varchar(6),
pm_offset varchar(8),
pm_dev varchar(6),
pm_inode varchar(8),
pm_pathname varchar(64))''')
conn.execute('''CREATE TABLE IF NOT EXISTS proc_map_smap (
pmm_id int,
pmm_pid int,
pmm_tag varchar(16),
pmm_value varchar(16))''')
conn.commit()
parse_proc(conn.cursor(), "sample_proc.txt", True)
conn.commit()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment