Read trace file from android APP - android

I am trying to read content of /sys/kernel/debug/tracing/trace file from my code but i am unable to do so.I have tried mounting using
mount -o rw,remount -t debugfs nodev /sys/kernel/debug
and have also done chmod -R 777 /sys/kernel/debug
File myFile = new File("/sys/kernel/debug/tracing/trace");
FileInputStream fIn = null;
try {
fIn = new FileInputStream(myFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow;
try {
while ((aDataRow = myReader.readLine()) != null) {
// Toast.makeText(getBaseContext(), aDataRow, Toast.LENGTH_LONG).show();
t.setText(aDataRow);
break;
}
} catch (IOException e) {
e.printStackTrace();
}

You need to use a "su" command in your program.
Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", "cat /sys/kernel/debug/tracing/trace" });
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {

Related

start test apk in another apk

Android version: 4.1 .
I want to test my apk without pc, so i write android apk to do it.
Process proc = Runtime.getRuntime().exec(command);
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
// proc.waitFor();
}
BufferedReader in = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
stringBuffer.append(line+"\n");
}
// proc.waitFor();
System.out.println(stringBuffer.toString());
Log.i("info",stringBuffer.toString());
return stringBuffer.toString();
} catch (InterruptedException e) {
System.err.println(e);
}
and the command is
am instrument -w -e deleteCase no com.calculator.test/com.xx.test.runner.Runner1
but it's not worked.

Write output of android shell command to a file

I am trying to write output of atrace to sdcard. I am using
getRuntime().exec()("atrace gfx > /storage/sdcard0/trace.txt")
in the app.App is signed as a system app and the command is working fine from terminal.But no file is created when running it from the app.
I solved it by reading data from inputstream and writing it to a file
try {
Process p = Runtime.getRuntime().exec("atrace -t 5 gfx");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
File myFile = new File("/storage/sdcard0/trac.txt");
FileOutputStream f = null;
try {
f = new FileOutputStream(myFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(f);
while (line != null) {
pw.println(line);
//Toast.makeText(getBaseContext(), line, Toast.LENGTH_LONG).show();
line = reader.readLine();
}
pw.flush();
pw.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

make android source using adb shell command "cat"

I want to use shell command, 'cat'.
I want to copy the "abc.jpg" file..But It didn't operate.
What is the problem? Thank you.
public void onClick(View v) {
Runtime runtime = Runtime.getRuntime();
Process process;
try {
String cmd = "cat /sdcard/0/Pikicast/abc.jpg>/sdcard/0/apk_backups/abc.jpg";
process = runtime.exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
Log.i("test",line);
}
} catch (Exception e) {
e.fillInStackTrace();
Log.e("Process Manager", "Unable to execute top command");
}
}
I have modified your code a little. Kindly try this.
String[] command = {"cat","/sdcard/0/Pikicast/abc.jpg>/sdcard/0/apk_backups/abc.jpg"};
StringBuilder cmdReturn = new StringBuilder();
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
int c;
while ((c = inputStream.read()) != -1) {
cmdReturn.append((char) c);
}
prompt.setText(cmdReturn.toString());
Log.w("test",cmdReturn);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("Process Manager", "Unable to execute top command");
}

Filter Logs in Android programmatically

This is method to fetch and dump logs in SDCard for Android. This is working fine, but I am wondering if we can put filter to trace logs related to specific tags.
I tried logcat -s *TagIWant; but it didn't work. Any other suggestion?
public static void Log(Context context){
String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
String command = "logcat -d *:V";
Log.d(TAG, "command: " + command);
try{
Process process = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try{
File file = new File(filename);
file.createNewFile();
FileWriter writer = new FileWriter(file);
while((line = in.readLine()) != null){
writer.write(line + "\n");
}
writer.flush();
writer.close();
}
catch(IOException e){
e.printStackTrace();
}
}
catch(IOException e){
e.printStackTrace();
}
}
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
Pattern pattern = Pattern.compile("XXX", 0);
while ((line = bufferedReader.readLine()) != null) {
if (patternu != null
&& !pattern.matcher(line).find()) {
continue;
}
log.append(line);
log.append('\n');
}
} catch (IOException e) {
}
Replace "Your_Tag" with your tag, that will give you the logs with tag as specified in with priority "Debug" and Above.
String command = "logcat Your_Tag:D *:S";
Process process = Runtime.getRuntime().exec(command);
Check more on http://developer.android.com/tools/debugging/debugging-log.html
You can easily filter the line that you want to write like the code below:
while ((line = bufferedReader.readLine()) != null) {
if(line.contains("The String you want to exist in your log")){
writer.write(line + "\n");
}
else{continue;}
}

How can I send the log file of the android device via email from inside my app?

as an error report I want to send the device and/or application log to a email account. For that I prepared a button in my view. Can someone help me with that, how can I retrieve the log files?
Thanks
String separator = System.getProperty("line.separator");
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append(separator);
}
} catch (Exception e) {
}
Here's a slightly improved version, adapted from Ramesh (thanks). The main difference is to do your own buffering. It's fast.
private void extractLogToFile()
{
// Make file name.
String fullName = ...;
// Extract to file.
File file = new File (fullName);
InputStreamReader reader = null;
FileWriter writer = null;
try
{
// get input stream
String cmd = "logcat -d -v time";
Process process = Runtime.getRuntime().exec(cmd);
reader = new InputStreamReader (process.getInputStream());
// write output stream
writer = new FileWriter (file);
char[] buffer = new char[10000];
do
{
int n = reader.read (buffer, 0, buffer.length);
if (n == -1)
break;
writer.write (buffer, 0, n);
} while (true);
reader.close();
writer.close();
}
catch (IOException e)
{
if (writer != null)
try {
writer.close();
} catch (IOException e1) {
}
if (reader != null)
try {
reader.close();
} catch (IOException e1) {
}
e.printStackTrace();
return;
}
}

Categories

Resources